-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Avoid global plugins in model #9780
Comments
Global plugins are not applied after compilation. You may use this to achieve desired behavior |
const schema = Schema(...)
const m = mongoose.model(“Mutex”, schema)
mongoose.plugin(...) // this won’t be applied to Mutex |
Thanks @stalniy for the comment, but all related with mongoose it's attached to my fw independently, so as this solutions works it'll implies i'd have to change all my inner logic to load other modules, for example some dev uses my fw and loads the mutex module, it'll be loaded after the fw global plugins. I think this change could be easy because it's an if before load plugins, maybe the mongoose logic ain't allow it, but if it's easy to develop I think it would be nice. Regards. |
You can also tell the plugin to explicitly avoid a certain schema: const plugin = require('myplugin');
mongoose.plugin((schema, options) => {
if (schema === schemaToExclude) {
return;
}
return plugin(schema, options);
}); |
Yes I know, I can do it, but what about a developer who uses my module?, my issue is to avoid that anyone who uses the module ain't have to acknowledge that he needs to do this, I'm trying to solve a generic problem, if a module must not use any global plugin, is there a way to do it? Regards. |
Hi @vkarpov15 how are you? Any news about this? I think this could be something like I'm developing 3 modules, mutex, semaphore and queue, all for multi servers, using mongoose to keep the data structure, this modules uses error control flow, so any mongoose plugin that will change errors will make those modules to fail. Regards. |
@chumager no progress yet, but we can add a tags system for v6.1.0. Something like: Schema.set('tags', ['mutex']); And mongoose.plugin(mutexPlugin, { tags: 'mutex' }); // Only apply plugin to schemas with 'mutex' tag Would that help? |
Hi @vkarpov15, I already solved for mi FW, my problem is if someone uses my modules, I added caveats in the readme. instead of //for each global plugin
mongoose.plugin((schema, options)=>{
if (!schema.get("noGlobalPlugin")) schema.plugin(plugin, options);
}, options); |
…ins to only schemas with matching tags Fix #9780
Do you want to request a feature or report a bug?
Feature
What is the current behavior?
Hi, I've a module who uses mongoose as mutex (with it's own schema and model), to validate the mutex is taken I just insert the document with a unique index, so if I get an E11000 error I know the mutex is taken.
In other hand I've a plugin who checks if a document with unique index can be saved, so instead of throwing a E11000 error it throws a validation error.
My module must be agnostic to the plugins.
Is there a way to tell a schema/model not to apply a global plugin?
If the current behavior is a bug, please provide the steps to reproduce.
In my code the plugin and the model doesn't know about each other and never will, this is only one example, I've several problems like this.
What is the expected behavior?
That I could use a external module who creates a model and this model don't get global plugins applied.
maybe in the schema options like
{plugins: false}
or something like that.What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
mongoose: 5.11.10
mongo: 4.2
node: 14.15.4
The text was updated successfully, but these errors were encountered: