| 
									
										
										
										
											2016-07-12 11:15:01 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Module dependencies | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Public node modules.
 | 
					
						
							| 
									
										
										
										
											2018-11-23 14:36:31 -03:00
										 |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2019-07-04 09:29:29 +02:00
										 |  |  | const fs = require('fs'); | 
					
						
							|  |  |  | const url = require('url'); | 
					
						
							| 
									
										
										
										
											2016-07-12 11:15:01 +02:00
										 |  |  | const _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											2018-04-24 15:24:42 +02:00
										 |  |  | const mongoose = require('mongoose'); | 
					
						
							| 
									
										
										
										
											2019-08-08 14:35:33 +02:00
										 |  |  | require('mongoose-long')(mongoose); | 
					
						
							| 
									
										
										
										
											2018-04-24 15:24:42 +02:00
										 |  |  | const Mongoose = mongoose.Mongoose; | 
					
						
							| 
									
										
										
										
											2018-10-08 22:19:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-08 18:44:47 +02:00
										 |  |  | const relations = require('./relations'); | 
					
						
							| 
									
										
										
										
											2019-03-13 19:27:18 +01:00
										 |  |  | const buildQuery = require('./buildQuery'); | 
					
						
							| 
									
										
										
										
											2019-07-02 13:56:14 +02:00
										 |  |  | const getQueryParams = require('./get-query-params'); | 
					
						
							|  |  |  | const mountModels = require('./mount-models'); | 
					
						
							| 
									
										
										
										
											2019-07-15 15:33:42 +02:00
										 |  |  | const queries = require('./queries'); | 
					
						
							| 
									
										
										
										
											2016-07-14 11:06:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-12 11:15:01 +02:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2017-11-17 14:23:26 +01:00
										 |  |  |  * Mongoose hook | 
					
						
							| 
									
										
										
										
											2016-07-12 11:15:01 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-02 13:56:14 +02:00
										 |  |  | const defaults = { | 
					
						
							|  |  |  |   defaultConnection: 'default', | 
					
						
							|  |  |  |   host: 'localhost', | 
					
						
							|  |  |  |   port: 27017, | 
					
						
							|  |  |  |   database: 'strapi', | 
					
						
							|  |  |  |   authenticationDatabase: '', | 
					
						
							|  |  |  |   ssl: false, | 
					
						
							|  |  |  |   debug: false, | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2019-04-09 12:09:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-02 13:56:14 +02:00
										 |  |  | const isMongooseConnection = ({ connector }) => | 
					
						
							|  |  |  |   connector === 'strapi-hook-mongoose'; | 
					
						
							| 
									
										
										
										
											2019-04-09 12:09:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-02 13:56:14 +02:00
										 |  |  | module.exports = function(strapi) { | 
					
						
							| 
									
										
										
										
											2019-08-14 14:15:45 +02:00
										 |  |  |   function initialize() { | 
					
						
							| 
									
										
										
										
											2019-07-02 13:56:14 +02:00
										 |  |  |     const { connections } = strapi.config; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const connectionsPromises = Object.keys(connections) | 
					
						
							|  |  |  |       .filter(key => isMongooseConnection(connections[key])) | 
					
						
							|  |  |  |       .map(async connectionName => { | 
					
						
							|  |  |  |         const connection = connections[connectionName]; | 
					
						
							|  |  |  |         const instance = new Mongoose(); | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-02 13:56:14 +02:00
										 |  |  |         _.defaults(connection.settings, strapi.config.hook.settings.mongoose); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const { | 
					
						
							|  |  |  |           uri, | 
					
						
							|  |  |  |           host, | 
					
						
							|  |  |  |           port, | 
					
						
							|  |  |  |           username, | 
					
						
							|  |  |  |           password, | 
					
						
							|  |  |  |           database, | 
					
						
							|  |  |  |           srv, | 
					
						
							| 
									
										
										
										
											2019-10-14 11:48:42 +02:00
										 |  |  |           useUnifiedTopology, | 
					
						
							| 
									
										
										
										
											2019-07-02 13:56:14 +02:00
										 |  |  |         } = connection.settings; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const uriOptions = uri ? url.parse(uri, true).query : {}; | 
					
						
							|  |  |  |         const { authenticationDatabase, ssl, debug } = _.defaults( | 
					
						
							|  |  |  |           connection.options, | 
					
						
							|  |  |  |           uriOptions, | 
					
						
							|  |  |  |           strapi.config.hook.settings.mongoose | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         const isSrv = srv === true || srv === 'true'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Connect to mongo database
 | 
					
						
							|  |  |  |         const connectOptions = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!_.isEmpty(username)) { | 
					
						
							|  |  |  |           connectOptions.user = username; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           if (!_.isEmpty(password)) { | 
					
						
							|  |  |  |             connectOptions.pass = password; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-04-09 12:09:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-02 13:56:14 +02:00
										 |  |  |         if (!_.isEmpty(authenticationDatabase)) { | 
					
						
							|  |  |  |           connectOptions.authSource = authenticationDatabase; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-04-09 12:09:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-02 13:56:14 +02:00
										 |  |  |         connectOptions.ssl = ssl === true || ssl === 'true'; | 
					
						
							|  |  |  |         connectOptions.useNewUrlParser = true; | 
					
						
							|  |  |  |         connectOptions.dbName = database; | 
					
						
							|  |  |  |         connectOptions.useCreateIndex = true; | 
					
						
							| 
									
										
										
										
											2019-09-11 15:37:42 +02:00
										 |  |  |         connectOptions.useUnifiedTopology = useUnifiedTopology || 'false'; | 
					
						
							| 
									
										
										
										
											2019-07-02 13:56:14 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |           /* FIXME: for now, mongoose doesn't support srv auth except the way including user/pass in URI. | 
					
						
							|  |  |  |            * https://github.com/Automattic/mongoose/issues/6881 */
 | 
					
						
							|  |  |  |           await instance.connect( | 
					
						
							|  |  |  |             uri || | 
					
						
							| 
									
										
										
										
											2019-10-14 11:48:42 +02:00
										 |  |  |               `mongodb${isSrv ? '+srv' : ''}://${username}:${encodeURIComponent( | 
					
						
							|  |  |  |                 password | 
					
						
							|  |  |  |               )}@${host}${!isSrv ? ':' + port : ''}/`,
 | 
					
						
							| 
									
										
										
										
											2019-07-02 13:56:14 +02:00
										 |  |  |             connectOptions | 
					
						
							|  |  |  |           ); | 
					
						
							| 
									
										
										
										
											2019-08-19 17:32:33 +02:00
										 |  |  |         } catch (error) { | 
					
						
							|  |  |  |           const err = new Error( | 
					
						
							|  |  |  |             `Error connecting to the Mongo database. ${error.message}` | 
					
						
							|  |  |  |           ); | 
					
						
							|  |  |  |           delete err.stack; | 
					
						
							|  |  |  |           throw err; | 
					
						
							| 
									
										
										
										
											2019-04-09 12:09:03 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-04 09:29:29 +02:00
										 |  |  |         const initFunctionPath = path.resolve( | 
					
						
							|  |  |  |           strapi.config.appPath, | 
					
						
							|  |  |  |           'config', | 
					
						
							|  |  |  |           'functions', | 
					
						
							|  |  |  |           'mongoose.js' | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (fs.existsSync(initFunctionPath)) { | 
					
						
							|  |  |  |           require(initFunctionPath)(instance, connection); | 
					
						
							| 
									
										
										
										
											2019-07-02 13:56:14 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-07-12 11:15:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02:00
										 |  |  |         instance.set('debug', debug === true || debug === 'true'); | 
					
						
							|  |  |  |         instance.set('useFindAndModify', false); | 
					
						
							| 
									
										
										
										
											2019-07-02 13:56:14 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const ctx = { | 
					
						
							|  |  |  |           instance, | 
					
						
							|  |  |  |           connection, | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return Promise.all([ | 
					
						
							|  |  |  |           mountGroups(connectionName, ctx), | 
					
						
							|  |  |  |           mountApis(connectionName, ctx), | 
					
						
							|  |  |  |           mountAdmin(connectionName, ctx), | 
					
						
							|  |  |  |           mountPlugins(connectionName, ctx), | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-14 14:15:45 +02:00
										 |  |  |     return Promise.all(connectionsPromises); | 
					
						
							| 
									
										
										
										
											2019-07-02 13:56:14 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function mountGroups(connectionName, ctx) { | 
					
						
							|  |  |  |     const options = { | 
					
						
							|  |  |  |       models: _.pickBy( | 
					
						
							|  |  |  |         strapi.groups, | 
					
						
							|  |  |  |         ({ connection }) => connection === connectionName | 
					
						
							|  |  |  |       ), | 
					
						
							|  |  |  |       target: strapi.groups, | 
					
						
							|  |  |  |       plugin: false, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return mountModels(options, ctx); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function mountApis(connectionName, ctx) { | 
					
						
							|  |  |  |     const options = { | 
					
						
							|  |  |  |       models: _.pickBy( | 
					
						
							|  |  |  |         strapi.models, | 
					
						
							|  |  |  |         ({ connection }) => connection === connectionName | 
					
						
							|  |  |  |       ), | 
					
						
							|  |  |  |       target: strapi.models, | 
					
						
							|  |  |  |       plugin: false, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return mountModels(options, ctx); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function mountAdmin(connectionName, ctx) { | 
					
						
							|  |  |  |     const options = { | 
					
						
							|  |  |  |       models: _.pickBy( | 
					
						
							|  |  |  |         strapi.admin.models, | 
					
						
							|  |  |  |         ({ connection }) => connection === connectionName | 
					
						
							|  |  |  |       ), | 
					
						
							|  |  |  |       target: strapi.admin.models, | 
					
						
							|  |  |  |       plugin: false, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return mountModels(options, ctx); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function mountPlugins(connectionName, ctx) { | 
					
						
							|  |  |  |     return Promise.all( | 
					
						
							|  |  |  |       Object.keys(strapi.plugins).map(name => { | 
					
						
							|  |  |  |         const plugin = strapi.plugins[name]; | 
					
						
							|  |  |  |         return mountModels( | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             models: _.pickBy( | 
					
						
							|  |  |  |               plugin.models, | 
					
						
							|  |  |  |               ({ connection }) => connection === connectionName | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             target: plugin.models, | 
					
						
							|  |  |  |             plugin: name, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           ctx | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     defaults, | 
					
						
							|  |  |  |     initialize, | 
					
						
							|  |  |  |     getQueryParams, | 
					
						
							|  |  |  |     buildQuery, | 
					
						
							| 
									
										
										
										
											2019-07-15 15:33:42 +02:00
										 |  |  |     queries, | 
					
						
							| 
									
										
										
										
											2019-07-02 13:56:14 +02:00
										 |  |  |     ...relations, | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2019-01-28 11:56:27 +01:00
										 |  |  | }; |