diff --git a/docs/3.x.x/configurations/configurations.md b/docs/3.x.x/configurations/configurations.md index 7aaf82b360..9436777cb2 100644 --- a/docs/3.x.x/configurations/configurations.md +++ b/docs/3.x.x/configurations/configurations.md @@ -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 diff --git a/packages/strapi-hook-bookshelf/lib/index.js b/packages/strapi-hook-bookshelf/lib/index.js index af4effbf10..c4e6b7a533 100644 --- a/packages/strapi-hook-bookshelf/lib/index.js +++ b/packages/strapi-hook-bookshelf/lib/index.js @@ -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. } diff --git a/packages/strapi-hook-mongoose/lib/index.js b/packages/strapi-hook-mongoose/lib/index.js index fd97747958..7725657fe0 100644 --- a/packages/strapi-hook-mongoose/lib/index.js +++ b/packages/strapi-hook-mongoose/lib/index.js @@ -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) => {