2016-03-18 11:12:50 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Node.js core.
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
// Public node modules.
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
2016-03-28 18:58:42 +02:00
|
|
|
// Array of supported clients.
|
|
|
|
const CLIENTS = [
|
|
|
|
'pg',
|
|
|
|
'mysql', 'mysql2',
|
|
|
|
'sqlite3',
|
|
|
|
'mariasql',
|
|
|
|
'oracle', 'strong-oracle',
|
|
|
|
'mssql',
|
|
|
|
'websql'
|
|
|
|
];
|
|
|
|
|
2016-03-18 11:12:50 +01:00
|
|
|
/**
|
|
|
|
* Knex hook
|
|
|
|
*/
|
|
|
|
|
2016-07-11 13:03:35 +02:00
|
|
|
module.exports = strapi => {
|
2016-03-18 11:12:50 +01:00
|
|
|
const hook = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default options
|
|
|
|
*/
|
|
|
|
|
|
|
|
defaults: {
|
2017-01-06 12:25:21 +01:00
|
|
|
connection: {
|
|
|
|
host: 'locahost',
|
2017-01-06 15:31:11 +01:00
|
|
|
charset: 'utf8'
|
2016-03-18 11:12:50 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the hook
|
|
|
|
*/
|
|
|
|
|
2016-07-11 13:03:35 +02:00
|
|
|
initialize: cb => {
|
2016-03-18 11:12:50 +01:00
|
|
|
// For each connection in the config register a new Knex connection.
|
2017-01-04 17:21:24 +01:00
|
|
|
_.forEach(_.pickBy(strapi.config.connections, {connector: 'strapi-bookshelf'}), (connection, name) => {
|
2016-03-18 11:12:50 +01:00
|
|
|
|
2016-03-28 18:58:42 +02:00
|
|
|
// Make sure we use the client even if the typo is not the exact one.
|
2017-01-04 17:21:24 +01:00
|
|
|
switch (connection.settings.client) {
|
2016-03-28 18:58:42 +02:00
|
|
|
case 'postgre':
|
|
|
|
case 'postgres':
|
|
|
|
case 'postgresql':
|
2017-01-04 17:21:24 +01:00
|
|
|
connection.settings.client = 'pg';
|
2016-04-04 22:09:19 +02:00
|
|
|
break;
|
2016-03-28 18:58:42 +02:00
|
|
|
case 'sqlite':
|
2017-01-04 17:21:24 +01:00
|
|
|
connection.settings.client = 'sqlite3';
|
2016-04-04 22:09:19 +02:00
|
|
|
break;
|
2016-03-28 18:58:42 +02:00
|
|
|
case 'maria':
|
|
|
|
case 'mariadb':
|
2017-01-04 17:21:24 +01:00
|
|
|
connection.settings.client = 'mariasql';
|
2016-04-04 22:09:19 +02:00
|
|
|
break;
|
2016-03-28 18:58:42 +02:00
|
|
|
case 'ms':
|
2017-01-04 17:21:24 +01:00
|
|
|
connection.settings.client = 'mssql';
|
2016-04-04 22:09:19 +02:00
|
|
|
break;
|
2016-03-28 18:58:42 +02:00
|
|
|
case 'web':
|
2017-01-04 17:21:24 +01:00
|
|
|
connection.settings.client = 'websql';
|
2016-04-04 22:09:19 +02:00
|
|
|
break;
|
2016-03-28 18:58:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the client is supported.
|
2017-01-04 17:21:24 +01:00
|
|
|
if (!_.includes(CLIENTS, connection.settings.client)) {
|
|
|
|
strapi.log.error('The client `' + connection.settings.client + '` for the `' + name + '` connection is not supported.');
|
2016-03-28 18:58:42 +02:00
|
|
|
strapi.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the client is installed in the application
|
|
|
|
// `node_modules` directory.
|
2016-03-18 11:12:50 +01:00
|
|
|
try {
|
2017-01-04 17:21:24 +01:00
|
|
|
require(path.resolve(strapi.config.appPath, 'node_modules', connection.settings.client));
|
2016-03-18 11:12:50 +01:00
|
|
|
} catch (err) {
|
2017-01-04 17:21:24 +01:00
|
|
|
strapi.log.error('The client `' + connection.settings.client + '` is not installed.');
|
|
|
|
strapi.log.error('You can install it with `$ npm install ' + connection.settings.client + ' --save`.');
|
2016-03-18 11:12:50 +01:00
|
|
|
strapi.stop();
|
|
|
|
}
|
2016-03-28 18:58:42 +02:00
|
|
|
|
2017-01-06 12:25:21 +01:00
|
|
|
const options = _.defaultsDeep({
|
|
|
|
client: connection.settings.client,
|
|
|
|
connection: {
|
|
|
|
host: _.get(connection.settings, 'host'),
|
2017-01-23 16:28:20 +01:00
|
|
|
user: _.get(connection.settings, 'username') || _.get(connection.settings, 'user'),
|
2017-01-15 16:33:53 +01:00
|
|
|
password: _.get(connection.settings, 'password'),
|
2017-01-06 12:25:21 +01:00
|
|
|
database: _.get(connection.settings, 'database'),
|
2017-01-23 16:28:20 +01:00
|
|
|
charset: _.get(connection.settings, 'charset'),
|
|
|
|
schema: _.get(connection.settings, 'schema'),
|
2017-03-15 16:45:53 +01:00
|
|
|
port: _.get(connection.settings, 'port'),
|
2017-01-23 16:28:20 +01:00
|
|
|
},
|
|
|
|
debug: _.get(connection, 'debug') || false
|
2017-01-06 12:25:21 +01:00
|
|
|
}, strapi.config.hooks.knex);
|
|
|
|
|
2017-01-23 16:28:20 +01:00
|
|
|
if (options.client === 'pg' && _.isString(_.get(options.connection, 'schema'))) {
|
|
|
|
options.pool = {
|
|
|
|
min: 0,
|
|
|
|
max: 10,
|
|
|
|
afterCreate: (conn, cb) => {
|
|
|
|
conn.query(`SET SESSION SCHEMA '${options.connection.schema}';`, (err) => {
|
|
|
|
cb(err, conn);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-03-28 18:58:42 +02:00
|
|
|
// Finally, use the client via `knex`.
|
|
|
|
// If anyone has a solution to use different paths for `knex` and clients
|
2016-08-10 11:31:27 +02:00
|
|
|
// please drop us an email at support@strapi.io-- it would avoid the Strapi
|
2016-03-28 18:58:42 +02:00
|
|
|
// applications to have `knex` as a dependency.
|
|
|
|
try {
|
2017-01-06 12:25:21 +01:00
|
|
|
// Try to require from local dependency.
|
|
|
|
strapi.connections[name] = require(path.resolve(strapi.config.appPath, 'node_modules', 'knex'))(options);
|
2016-03-28 18:58:42 +02:00
|
|
|
} catch (err) {
|
|
|
|
strapi.log.error('Impossible to use the `' + name + '` connection...');
|
2017-01-06 12:25:21 +01:00
|
|
|
strapi.log.warn('Be sure that your client `' + name + '` are in the same node_modules directory');
|
2016-03-28 18:58:42 +02:00
|
|
|
strapi.log.error(err);
|
|
|
|
strapi.stop();
|
|
|
|
}
|
2016-03-18 11:12:50 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return hook;
|
|
|
|
};
|