Versioning models #11508
-
This might be a noob question, but I couldn't find a proper solution to overcome this at least for the architecture that I was about to build. So my question is, in my NodeJS backend I need to run multiple versions in the same repo. so what I did is started creating new folders for the versions. models/ something like this. and I'm importing both of these modules in my startup scripts so that they can be used interchangeably. v1/user.js
v2/user.js
So in this context, I'm getting the error 'Cannot overwrite User model once compiled.' which makes perfect sense as I use the same name in both occurrences. I just need to know if there is a way to eliminate it. because at the end of the day we refer to what model we need to use in each transaction right so I feel just eliminating the error with some trick is theoretically doable. or else please point me out a way to do this in another way, I need to keep the initial version of the model anyways because the v1 routes use that schema already where I can't randomly add more required fields into it. Any help regarding this is highly appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Mind if i ask, are you using the same database for both models? |
Beta Was this translation helpful? Give feedback.
-
but if it helps you can try either of this approach //add this line before exporting your model to empty the models each time
mongoose.models = {};
const User = mongoose.model("User", userSchema) or try this export const User = mongoose.models.User || mongoose.model('User', userSchema); |
Beta Was this translation helpful? Give feedback.
but if it helps you can try either of this approach
or try this