Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use one shared collection for multiple schemas #14

Open
StefanM98 opened this issue Dec 21, 2019 · 2 comments
Open

Can't use one shared collection for multiple schemas #14

StefanM98 opened this issue Dec 21, 2019 · 2 comments

Comments

@StefanM98
Copy link

Problem:
It is stated that we can use multiple history collections or one shared collection for the history schemas. However, it is not possible to use a shared collection to hold the history of more than one schema. A mongoose OverwriteModelError is thrown when using the plugin on multiple schemas with the same modelName.

Possible Solution:
After looking at the code, it seems that no check is performed to see if the model already exists before creating it. As a temporary fix, I changed the code to this:

...
let mongoose = pluginOptions.mongoose;
const collectionIdType = options.collectionIdType || mongoose.Schema.Types.ObjectId;
let Model;
  try {  // Check if the model exists
    Model = mongoose.model(pluginOptions.modelName);
  } catch (e) {}
  
  if (!Model) {
     ... // schema code
     Model = mongoose.model(pluginOptions.modelName, Schema);
  }
...

That seemed to fix the issue. Can you please make a new release with a similar fix? Thanks for the great plugin!

@Masquerade-Circus
Copy link
Owner

Hi @StefanM98 Can you put a full example to see the model/schemas declarations?

@kushalarora92
Copy link

Thanks, @StefanM98. You're are a savior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants