153 lines
3.4 KiB
JavaScript
Raw Normal View History

2016-03-18 11:12:50 +01:00
'use strict';
/**
* Module dependencies
*/
// 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');
2016-03-18 11:12:50 +01:00
/**
* Bookshelf hook
*/
2019-06-03 21:00:03 +02:00
/**
* Default options
*/
2019-06-03 21:00:03 +02:00
const defaults = {
defaultConnection: 'default',
host: 'localhost',
};
2019-07-02 13:56:14 +02:00
const isBookshelfConnection = ({ connector }) =>
connector === 'strapi-hook-bookshelf';
2019-06-03 21:00:03 +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;
2019-06-03 21:00:03 +02:00
const GLOBALS = {};
2019-07-02 13:56:14 +02:00
const connectionsPromises = Object.keys(connections)
.filter(key => isBookshelfConnection(connections[key]))
.map(connectionName => {
const connection = connections[connectionName];
_.defaults(connection.settings, strapi.config.hook.settings.bookshelf);
// Create Bookshelf instance for this connection.
const ORM = new bookshelf(strapi.connections[connectionName]);
const initFunctionPath = path.resolve(
strapi.config.appPath,
'config',
'functions',
'bookshelf.js'
);
if (fs.existsSync(initFunctionPath)) {
require(initFunctionPath)(ORM, connection);
}
// Load plugins
if (_.get(connection, 'options.plugins', true) !== false) {
ORM.plugin('visibility');
ORM.plugin('pagination');
}
const ctx = {
GLOBALS,
connection,
ORM,
};
return Promise.all([
mountGroups(connectionName, ctx),
mountApis(connectionName, ctx),
mountAdmin(connectionName, ctx),
mountPlugins(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-06-03 21:00:03 +02:00
function mountGroups(connectionName, ctx) {
const options = {
models: _.pickBy(
strapi.groups,
({ connection }) => connection === connectionName
),
target: strapi.groups,
plugin: false,
};
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 = {
models: _.pickBy(
strapi.models,
({ connection }) => connection === connectionName
),
target: strapi.models,
plugin: false,
};
return mountModels(options, ctx);
}
2019-06-03 21:00:03 +02:00
function mountAdmin(connectionName, ctx) {
const options = {
models: _.pickBy(
strapi.admin.models,
({ connection }) => connection === connectionName
),
target: strapi.admin.models,
plugin: false,
};
return mountModels(options, ctx);
}
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(
{
models: _.pickBy(
plugin.models,
2019-06-03 21:00:03 +02:00
({ connection }) => connection === connectionName
),
target: plugin.models,
plugin: name,
},
ctx
);
})
);
}
2016-03-18 11:12:50 +01:00
2019-06-03 21:00:03 +02:00
return {
defaults,
initialize,
getQueryParams,
buildQuery,
2019-07-15 15:33:42 +02:00
queries,
2019-06-03 21:00:03 +02:00
...relations,
};
2016-03-18 11:12:50 +01:00
};