Skip to content

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

@StefanM98

Description

@StefanM98

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions