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 application object be accessed in mongoose plugins? #62

Open
wangyige0701 opened this issue Apr 13, 2024 · 0 comments
Open

Can application object be accessed in mongoose plugins? #62

wangyige0701 opened this issue Apr 13, 2024 · 0 comments

Comments

@wangyige0701
Copy link

I insert some configs in app object, and I hope to get it in mongoose plugins, can this feature be added.
And I think it can be handled this way.

module.exports = app => {
  const { client, clients, url, options, defaultDB, customPromise, loadModel, plugins, aPlugins } = app.config.mongoose;
  ...
  if (Array.isArray(aPlugins)) {
    aPlugins.forEach(plugin => {
      mongoose.plugin.apply(mongoose, insertApp(app, plugin));
    });
  }
  ...
};
function createOneClient(config, app) {
  const { url, options, plugins, aPlugins } = config;
  ...
  [].concat(plugins || [], (aPlugins || []).map(plugin => insertApp(app, plugin)), globalPlugins).forEach(plugin => {
    mongoose.plugin.apply(mongoose, Array.isArray(plugin) ? plugin : [ plugin ]);
  });
  ...
}
...
function insertApp(app, plugin) {
  if (Array.isArray(plugin)) {
    plugin[0] = plugin[0](app);
    return plugin;
  }
  return [ plugin(app) ];
}

config like this

exports.mongoose = {
  url: process.env.MONGODB_URL_3,
  options: {},
  aPlugins: [ lastModifiedPlugin, [ lastModifiedPlugin, { field: 'updatedAt' }]],
};
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

1 participant