Merge pull request #2370 from jacargentina/fix-2257

Allow customization for mongoose connection (ej. load some mongoose plugins)
This commit is contained in:
Jim LAURIE 2018-12-12 16:45:55 +01:00 committed by GitHub
commit e819432b40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 1 deletions

View File

@ -130,6 +130,26 @@ module.exports = {
};
```
### Bookshelf, Mongoose
**Path —** `./config/functions/bookshelf.js`.
**Path —** `./config/functions/mongoose.js`.
When present, they are loaded to let you customize your database connection instance, for example for adding some plugin, customizing parameters, etc.
As an example, for using the `mongoose-simple-random` plugin for MongoDB, you can register it like this:
**Path —** `./config/functions/mongoose.js`.
```js
'use strict';
const random = require('mongoose-simple-random');
module.exports = (mongoose, connection) => {
mongoose.plugin(random);
};
```
***
## Locales

View File

@ -60,7 +60,7 @@ module.exports = function(strapi) {
'config',
'functions',
'bookshelf.js'
))(ORM, strapi.connections[connectionName]);
))(ORM, connection);
} catch (err) {
// This is not an error if the file is not found.
}

View File

@ -6,6 +6,7 @@
// Public node modules.
const url = require('url');
const path = require('path');
const _ = require('lodash');
const mongoose = require('mongoose');
const Mongoose = mongoose.Mongoose;
@ -89,6 +90,18 @@ module.exports = function (strapi) {
return cb(errMsg);
}
try {
// Require `config/functions/mongoose.js` file to customize connection.
require(path.resolve(
strapi.config.appPath,
'config',
'functions',
'mongoose.js'
))(instance, connection);
} catch (err) {
// This is not an error if the file is not found.
}
Object.keys(options, key => instance.set(key, options[key]));
const mountModels = (models, target, plugin = false) => {