| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Module dependencies | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-30 15:04:21 +01:00
										 |  |  | // Core
 | 
					
						
							|  |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2019-06-03 21:00:03 +02:00
										 |  |  | const fs = require('fs'); | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  | // Public node modules.
 | 
					
						
							|  |  |  | const _ = require('lodash'); | 
					
						
							|  |  |  | const bookshelf = require('bookshelf'); | 
					
						
							| 
									
										
										
										
											2018-05-04 17:29:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Local helpers.
 | 
					
						
							| 
									
										
										
										
											2018-05-16 18:17:13 +02:00
										 |  |  | const relations = require('./relations'); | 
					
						
							| 
									
										
										
										
											2019-03-13 19:27:18 +01:00
										 |  |  | const buildQuery = require('./buildQuery'); | 
					
						
							| 
									
										
										
										
											2019-06-03 21:00:03 +02:00
										 |  |  | const mountModels = require('./mount-models'); | 
					
						
							|  |  |  | const getQueryParams = require('./get-query-params'); | 
					
						
							| 
									
										
										
										
											2019-07-15 15:33:42 +02:00
										 |  |  | const queries = require('./queries'); | 
					
						
							| 
									
										
										
										
											2019-09-20 12:44:24 +02:00
										 |  |  | const initKnex = require('./knex'); | 
					
						
							| 
									
										
										
										
											2021-02-15 08:43:59 +01:00
										 |  |  | const registerCoreMigrations = require('./migrations'); | 
					
						
							| 
									
										
										
										
											2019-04-18 16:01:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Bookshelf hook | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-03 21:00:03 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Default options | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-03-01 17:39:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-03 21:00:03 +02:00
										 |  |  | const defaults = { | 
					
						
							|  |  |  |   defaultConnection: 'default', | 
					
						
							|  |  |  |   host: 'localhost', | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2018-02-21 16:35:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-20 12:44:24 +02:00
										 |  |  | const isBookshelfConnection = ({ connector }) => connector === 'bookshelf'; | 
					
						
							| 
									
										
										
										
											2019-07-02 13:56:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-03 21:00:03 +02:00
										 |  |  | module.exports = function(strapi) { | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |   const { connections } = strapi.config; | 
					
						
							| 
									
										
										
										
											2020-12-29 17:44:14 +01:00
										 |  |  |   const bookshelfConnections = Object.keys(connections).filter(key => | 
					
						
							|  |  |  |     isBookshelfConnection(connections[key]) | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-14 14:15:45 +02:00
										 |  |  |   function initialize() { | 
					
						
							| 
									
										
										
										
											2019-09-20 12:44:24 +02:00
										 |  |  |     initKnex(strapi); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-15 08:43:59 +01:00
										 |  |  |     registerCoreMigrations(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-03 21:00:03 +02:00
										 |  |  |     const GLOBALS = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-29 17:44:14 +01:00
										 |  |  |     const connectionsPromises = bookshelfConnections.map(connectionName => { | 
					
						
							|  |  |  |       const connection = connections[connectionName]; | 
					
						
							| 
									
										
										
										
											2019-07-02 13:56:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-29 17:44:14 +01:00
										 |  |  |       _.defaults(connection.settings, strapi.config.hook.settings.bookshelf); | 
					
						
							| 
									
										
										
										
											2019-07-02 13:56:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-29 17:44:14 +01:00
										 |  |  |       // Create Bookshelf instance for this connection.
 | 
					
						
							|  |  |  |       const ORM = new bookshelf(strapi.connections[connectionName]); | 
					
						
							| 
									
										
										
										
											2019-07-02 13:56:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-29 17:44:14 +01:00
										 |  |  |       const initFunctionPath = path.resolve( | 
					
						
							|  |  |  |         strapi.config.appPath, | 
					
						
							|  |  |  |         'config', | 
					
						
							|  |  |  |         'functions', | 
					
						
							|  |  |  |         'bookshelf.js' | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2019-07-02 13:56:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-29 17:44:14 +01:00
										 |  |  |       if (fs.existsSync(initFunctionPath)) { | 
					
						
							|  |  |  |         require(initFunctionPath)(ORM, connection); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2019-07-02 13:56:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-29 17:44:14 +01:00
										 |  |  |       const ctx = { | 
					
						
							|  |  |  |         GLOBALS, | 
					
						
							|  |  |  |         connection, | 
					
						
							|  |  |  |         ORM, | 
					
						
							|  |  |  |       }; | 
					
						
							| 
									
										
										
										
											2019-07-02 13:56:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-29 17:44:14 +01:00
										 |  |  |       return mountConnection(connectionName, ctx); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-06-03 21:00:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-14 14:15:45 +02:00
										 |  |  |     return Promise.all(connectionsPromises); | 
					
						
							| 
									
										
										
										
											2019-06-03 21:00:03 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-12 18:50:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-27 20:26:56 +02:00
										 |  |  |   async function mountConnection(connectionName, ctx) { | 
					
						
							|  |  |  |     if (strapi.models['core_store'].connection === connectionName) { | 
					
						
							|  |  |  |       await mountCoreStore(ctx); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  |     const finalizeMountings = await Promise.all([ | 
					
						
							| 
									
										
										
										
											2020-07-27 20:26:56 +02:00
										 |  |  |       mountComponents(connectionName, ctx), | 
					
						
							|  |  |  |       mountApis(connectionName, ctx), | 
					
						
							|  |  |  |       mountAdmin(connectionName, ctx), | 
					
						
							|  |  |  |       mountPlugins(connectionName, ctx), | 
					
						
							|  |  |  |     ]); | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (const finalizeMounting of _.flattenDeep(finalizeMountings)) { | 
					
						
							|  |  |  |       await finalizeMounting(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-27 20:26:56 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function mountCoreStore(ctx) { | 
					
						
							|  |  |  |     return mountModels( | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         models: { | 
					
						
							|  |  |  |           core_store: strapi.models['core_store'], | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         target: strapi.models, | 
					
						
							|  |  |  |       }, | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  |       ctx, | 
					
						
							|  |  |  |       { selfFinalize: true } | 
					
						
							| 
									
										
										
										
											2020-07-27 20:26:56 +02:00
										 |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-22 18:01:03 +02:00
										 |  |  |   function mountComponents(connectionName, ctx) { | 
					
						
							| 
									
										
										
										
											2019-06-03 21:00:03 +02:00
										 |  |  |     const options = { | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |       models: _.pickBy(strapi.components, ({ connection }) => connection === connectionName), | 
					
						
							| 
									
										
										
										
											2019-10-22 18:01:03 +02:00
										 |  |  |       target: strapi.components, | 
					
						
							| 
									
										
										
										
											2019-06-03 21:00:03 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return mountModels(options, ctx); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-11-28 15:00:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-03 21:00:03 +02:00
										 |  |  |   function mountApis(connectionName, ctx) { | 
					
						
							|  |  |  |     const options = { | 
					
						
							| 
									
										
										
										
											2020-07-27 20:26:56 +02:00
										 |  |  |       models: _.pickBy( | 
					
						
							|  |  |  |         strapi.models, | 
					
						
							|  |  |  |         ({ connection }, name) => connection === connectionName && name !== 'core_store' | 
					
						
							|  |  |  |       ), | 
					
						
							| 
									
										
										
										
											2019-06-03 21:00:03 +02:00
										 |  |  |       target: strapi.models, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return mountModels(options, ctx); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-09 12:09:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-03 21:00:03 +02:00
										 |  |  |   function mountAdmin(connectionName, ctx) { | 
					
						
							|  |  |  |     const options = { | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |       models: _.pickBy(strapi.admin.models, ({ connection }) => connection === connectionName), | 
					
						
							| 
									
										
										
										
											2019-06-03 21:00:03 +02:00
										 |  |  |       target: strapi.admin.models, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return mountModels(options, ctx); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-09 12:09:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-03 21:00:03 +02:00
										 |  |  |   function mountPlugins(connectionName, ctx) { | 
					
						
							|  |  |  |     return Promise.all( | 
					
						
							|  |  |  |       Object.keys(strapi.plugins).map(name => { | 
					
						
							|  |  |  |         const plugin = strapi.plugins[name]; | 
					
						
							|  |  |  |         return mountModels( | 
					
						
							|  |  |  |           { | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |             models: _.pickBy(plugin.models, ({ connection }) => connection === connectionName), | 
					
						
							| 
									
										
										
										
											2019-06-03 21:00:03 +02:00
										 |  |  |             target: plugin.models, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           ctx | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |   async function destroy() { | 
					
						
							|  |  |  |     await Promise.all(bookshelfConnections.map(connName => strapi.connections[connName].destroy())); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-03 21:00:03 +02:00
										 |  |  |   return { | 
					
						
							|  |  |  |     defaults, | 
					
						
							|  |  |  |     initialize, | 
					
						
							|  |  |  |     getQueryParams, | 
					
						
							|  |  |  |     buildQuery, | 
					
						
							| 
									
										
										
										
											2019-07-15 15:33:42 +02:00
										 |  |  |     queries, | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |     destroy, | 
					
						
							| 
									
										
										
										
											2019-06-03 21:00:03 +02:00
										 |  |  |     ...relations, | 
					
						
							| 
									
										
										
										
											2020-04-26 15:32:57 +02:00
										 |  |  |     get defaultTimestamps() { | 
					
						
							|  |  |  |       return ['created_at', 'updated_at']; | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2019-06-03 21:00:03 +02:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  | }; |