mirror of
https://github.com/strapi/strapi.git
synced 2025-09-27 09:25:46 +00:00
Merge pull request #2370 from jacargentina/fix-2257
Allow customization for mongoose connection (ej. load some mongoose plugins)
This commit is contained in:
commit
e819432b40
@ -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
|
## Locales
|
||||||
|
@ -60,7 +60,7 @@ module.exports = function(strapi) {
|
|||||||
'config',
|
'config',
|
||||||
'functions',
|
'functions',
|
||||||
'bookshelf.js'
|
'bookshelf.js'
|
||||||
))(ORM, strapi.connections[connectionName]);
|
))(ORM, connection);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// This is not an error if the file is not found.
|
// This is not an error if the file is not found.
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
// Public node modules.
|
// Public node modules.
|
||||||
const url = require('url');
|
const url = require('url');
|
||||||
|
const path = require('path');
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const mongoose = require('mongoose');
|
const mongoose = require('mongoose');
|
||||||
const Mongoose = mongoose.Mongoose;
|
const Mongoose = mongoose.Mongoose;
|
||||||
@ -89,6 +90,18 @@ module.exports = function (strapi) {
|
|||||||
return cb(errMsg);
|
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]));
|
Object.keys(options, key => instance.set(key, options[key]));
|
||||||
|
|
||||||
const mountModels = (models, target, plugin = false) => {
|
const mountModels = (models, target, plugin = false) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user