francescov1 / mongoose-tsgen

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

Conflict with methods (mongoose-delete ?) #45

Closed www-chique closed 3 years ago

www-chique commented 3 years ago

Hi there. I'm back with another problem. The tool works amazing out of the box, and most of the other issues you've already helped me in the past and ironed them out. But it came to this one:

I am using mongoose-delete plugin. And on the models where I've used the plugin, it shows a Typescript error on the generated interface such as follows: Image 1 image

Image 2 It also generated the following type, which is correct image

Now I am a newbie at this, so I am not entirely sure what would be the correct solution. But if I do this, it gets fixed. (Copy pasted the line from Image 2 into Image 1) image

You're extremely good at this, so I'll look up-to your solution to this problem or how you would approach this.

Many thanks, and appreciate this tool so much every day.

francescov1 commented 3 years ago

Hey @www-chique, good to hear from you again, glad to hear everything else is going well with the tool!

Currently in a bit of a time crunch with my other work but I'm looking to tackle this early next week! This plugin is actually something I've been meaning to setup for one of my projects so your question comes at a perfect time. I'll keep you posted!

www-chique commented 3 years ago

Hey @www-chique, good to hear from you again, glad to hear everything else is going well with the tool!

Currently in a bit of a time crunch with my other work but I'm looking to tackle this early next week! This plugin is actually something I've been meaning to setup for one of my projects so your question comes at a perfect time. I'll keep you posted!

Sure, there is absolutely no rush. For these kind of special cases, I always run a custom fix afterwards as a workaround. I just let you know, so maybe the tool gets more robust with time. Thanks a lot again.

francescov1 commented 3 years ago

I investigated your issue, it occurs because mongoose.Document defines the default document delete function, which is different from the generated version. To get around this, I'm thinking we could do something like this:

// before
export interface EmailTemplateDocument extends mongoose.Document<mongoose.Types.ObjectId>, EmailTemplateMethods { ... }

// after
export interface EmailTemplateDocument extends Omit<mongoose.Document<mongoose.Types.ObjectId>, keyof EmailTemplateMethods>, EmailTemplateMethods { ... }

Could you confirm that the fix above fixes your issue? It essentially removes any properties from mongoose.Document that are already defined in EmailTemplateMethods.

I'd like to look into any potential consequences & edge cases with implementing this. Should be able to do that in the next few days, once that's confirmed it will be a quick fix. One thing to mention is that we recently released mongoose-tsgen v8.0.0. This introduced a few (very minor) breaking changes, but requires mongoose 5.11.19 or higher. I suggest you upgrade to this version in the meantime so that you can get the fix easily once its released, since we won't be updating older versions of mongoose-tsgen moving forward.

www-chique commented 3 years ago

Hey. I can confirm, this fixes the problem. I tried it, works great. Thank you very much.

francescov1 commented 3 years ago

Fixed in 8.3.0 🚀 Ended up finding a simpler option, while improving general typing!