Typegoose: Update to work with mongoose 5.10.19 / 5.11

Created on 4 Dec 2020  路  10Comments  路  Source: typegoose/typegoose

Which feature

Since these versions mongoose has included its own typescript definitions

help wanted parity

All 10 comments

work has started in branch https://github.com/typegoose/typegoose/commits/update_mongoose5.11, but i have no clue on how to fix these types while not fully rewriting typegoose's types
-> the only "fixes" i could make would be very inconvenient to use much

@otroboe - It's not.

Scott

@otroboe #424 is about "typegoose not working with old types" (the types in the issue were @types/[email protected], while typegoose requires @types/[email protected])

Its not perfect and this still bug for create but its an first step.

export type DocumentType<T extends AnyParamConstructor<any>> = T extends AnyParamConstructor<infer U> ? mongoose.Document & U : mongoose.Document
export type ModelType<T extends AnyParamConstructor<any>> = mongoose.Model<DocumentType<T>>
export type ReturnModelType<T extends AnyParamConstructor<any>> = ModelType<T> & T;

// This is new and is an util
export type QueryMethod<ResultType, DocType extends AnyParamConstructor<any>> = mongoose.Query<ResultType | null, DocumentType<DocType>>

The points i have try to fix :

  • [ ] No require typeof (e.g DocumentType<Account> instead of DocumentType<typeof Account>)
    This is visibly very hard to fix
  • [ ] In the new QueryMethod find an way to specify only 1 arg for the end result QueryMethod<Account> or QueryMethod<Account[]> (if is not an findOne)

@orblazer did you already try to run the tests and ran the script build:tests without any error?
and confirm that all the properties that should be there are there?

@hasezoey I have explore more deep and i think the current types in mongoose its not fully compatible and i go check to this in the next days.

My current state :

type InternalDocumentType<T> = (T extends {_id?: RefType } ? mongoose.Document<T['_id']> : mongoose.Document) & T

export type DocumentType<T extends AnyParamConstructor<any>> = T extends AnyParamConstructor<infer U> ? InternalDocumentType<U> : InternalDocumentType<T>;
export type ReturnModelType<U extends AnyParamConstructor<any>> = mongoose.Model<DocumentType<U>> & U;

But with this the create method is broken. This method is my big problem currently, i think we need modify current mongoose type for fix our case or we need extends all class with mongoose.Document (but this require a lot changes).
And for now on this method we could "omit" some fields

I have start to improve the types of mongoose : https://github.com/Automattic/mongoose/pull/9698, https://github.com/Automattic/mongoose/pull/9699, https://github.com/Automattic/mongoose/pull/9700

EDIT: i have update InternalDocumentType with my accepted PR.

@orblazer any update with the types?

No sorry, i don't have enough time for find an easy way except if we extends the class with mongoose.Document

Updated branch to 5.11.9 and tried to upgrade again, this time Document with DocumentType was possible, but for many cases https://github.com/Automattic/mongoose/pull/9753 is still nedded, further testing & refactoring needed (and test references (Ref type))

Was this page helpful?
0 / 5 - 0 ratings