From 8b6bfdae70be1d5b25fdd5b5ce05c0b90d252779 Mon Sep 17 00:00:00 2001 From: Javier Castro Date: Fri, 23 Nov 2018 14:36:31 -0300 Subject: [PATCH 1/2] Allow customization for mongoose connection (ej. load some mongoose plugins) configurations.md: Added section under Functions about optional bookshelf/mongoose customization --- docs/3.x.x/configurations/configurations.md | 20 ++++++++++++++++++++ packages/strapi-hook-mongoose/lib/index.js | 13 +++++++++++++ 2 files changed, 33 insertions(+) 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-mongoose/lib/index.js b/packages/strapi-hook-mongoose/lib/index.js index cdfdc16569..00b980f267 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; @@ -90,6 +91,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, strapi.config.connections[connectionName]); + } 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) => { From b0a21b46f059d6675f26b4835321e3d2a15e9f5a Mon Sep 17 00:00:00 2001 From: Javier Castro Date: Wed, 12 Dec 2018 12:35:16 -0300 Subject: [PATCH 2/2] strapi-hook-bookshelf: minor change for code clarity --- packages/strapi-hook-bookshelf/lib/index.js | 2 +- packages/strapi-hook-mongoose/lib/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/strapi-hook-bookshelf/lib/index.js b/packages/strapi-hook-bookshelf/lib/index.js index 8688d498a0..c150009f7d 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 00b980f267..3f2f6c858f 100644 --- a/packages/strapi-hook-mongoose/lib/index.js +++ b/packages/strapi-hook-mongoose/lib/index.js @@ -98,7 +98,7 @@ module.exports = function (strapi) { 'config', 'functions', 'mongoose.js' - ))(instance, strapi.config.connections[connectionName]); + ))(instance, connection); } catch (err) { // This is not an error if the file is not found. }