francescov1 / mongoose-tsgen

A plug-n-play Typescript generator for Mongoose.
105 stars 25 forks source link

Subdocument Array type problem #24

Closed www-chique closed 3 years ago

www-chique commented 3 years ago

Using "EmbeddedDocument" instead of "Embedded" in case of an array of Sub-document fixes a type issue.

More explanation:

Hey there. I'm a newbie, I might be wrong, or there is a better fix maybe.

I'll try my best to explain.

In a Schema, where I use an Array of Sub-documents, like

const UserSchema = new Schema(
    {
                //Other properties
        passwordReset: [
            {
                code: {
                    type: String,
                },
                time: {
                    type: Date,
                },
            },
        ],
    }
);

And in the generated interface: export type UserPasswordResetDocument = mongoose.Types.Embedded & {} & UserPasswordReset; this line creates a problem like so: Type 'UserPasswordResetDocument' does not satisfy the constraint 'Document'.

Once I replaced mongoose.Types.Embedded with mongoose.Types.EmbeddedDocument, the error got fixed.

francescov1 commented 3 years ago

hi @www-chique,

This change only works if you are using Mongoose version 5.11 or greater. As I mentioned here, Mongoose moved from using the community typings to their own official typings. In doing this, they introduced a few breaking changes. The main hold up is the issue I mentioned on the previous thread (finding a way providing query typings for the Model type).

Before I can merge this change in, that issue will need to be solved, as well as anything else that broke with the release of v5.11 (EmbeddedDocument doesn't actually exist in the community typings, only in the new official typings).

For the time being, you can downgrade to a previous version of Mongoose and install @types/mongoose to use this tool.

www-chique commented 3 years ago

Hey @francescov1 - thanks for your reply. I am aware of what you mentioned in the other thread. And I am too looking for a solution to that Query problem, hoping it might solve this hold up. In the meantime I'm doing some workaround, and stumbled upon this one accidentally.

Feel free to ignore, or close this request. Once again, I'm a newbie. I just posted here my work-around in case it helps someone or something.

P.S. I'm totally using mongoose-tsgen all the way. It's the best. Thanks for your work.

francescov1 commented 3 years ago

Sounds good, thanks again! 🙌 Check out the Mongoose issues, it seems there's quite a few related issues such as https://github.com/Automattic/mongoose/issues/9801, https://github.com/Automattic/mongoose/issues/9781.

Seems like most of these issues will be addressed in the next version (5.11.12)