162 lines
5.4 KiB
JavaScript
Raw Normal View History

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');
2018-05-04 18:03:41 +02:00
/* eslint-disable prefer-template */
2016-03-28 18:58:42 +02:00
// Array of supported clients.
const CLIENTS = [
'pg',
'mysql', 'mysql2',
'sqlite3',
'mariasql',
'oracle', 'strong-oracle',
2019-01-10 23:39:32 +13:00
'mssql'
2016-03-28 18:58:42 +02:00
];
2016-03-18 11:12:50 +01:00
/**
* Knex hook
*/
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: {
2017-07-25 17:12:18 +02:00
host: 'localhost',
2017-01-06 15:31:11 +01:00
charset: 'utf8'
2016-03-18 11:12:50 +01:00
}
},
/**
* Initialize the hook
*/
initialize: cb => {
2016-03-18 11:12:50 +01:00
// For each connection in the config register a new Knex connection.
2018-07-11 16:23:14 +02:00
_.forEach(_.pickBy(strapi.config.connections, {connector: 'strapi-hook-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.
switch (connection.settings.client) {
2016-03-28 18:58:42 +02:00
case 'postgre':
case 'postgres':
case 'postgresql':
connection.settings.client = 'pg';
2016-04-04 22:09:19 +02:00
break;
2016-03-28 18:58:42 +02:00
case 'sqlite':
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':
connection.settings.client = 'mariasql';
2016-04-04 22:09:19 +02:00
break;
2016-03-28 18:58:42 +02:00
case 'ms':
connection.settings.client = 'mssql';
2016-04-04 22:09:19 +02:00
break;
2016-03-28 18:58:42 +02:00
}
// Make sure the client is supported.
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.
2018-06-04 18:22:21 +02:00
let client;
2016-03-18 11:12:50 +01:00
try {
2018-06-04 18:22:21 +02:00
client = require(path.resolve(strapi.config.appPath, 'node_modules', connection.settings.client));
2016-03-18 11:12:50 +01:00
} catch (err) {
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'),
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'),
charset: _.get(connection.settings, 'charset'),
2019-01-17 22:24:09 +13:00
schema: _.get(connection.settings, 'schema', 'public'),
2017-03-15 16:45:53 +01:00
port: _.get(connection.settings, 'port'),
socket: _.get(connection.settings, 'socketPath'),
2019-01-17 22:24:09 +13:00
ssl: _.get(connection.settings, 'ssl', false),
timezone: _.get(connection.settings, 'timezone', 'utc'),
},
2019-01-17 22:24:09 +13:00
debug: _.get(connection.options, 'debug', false),
acquireConnectionTimeout: _.get(connection.options, 'acquireConnectionTimeout'),
migrations: _.get(connection.options, 'migrations')
2017-07-26 18:53:48 +02:00
}, strapi.config.hook.settings.knex);
2017-01-06 12:25:21 +01:00
2019-01-09 23:24:45 +13:00
options.pool = {
2019-01-17 22:24:09 +13:00
min: _.get(connection.options, 'pool.min', 0),
max: _.get(connection.options, 'pool.max', 10),
acquireTimeoutMillis: _.get(connection.options, 'pool.acquireTimeoutMillis', 2000),
createTimeoutMillis: _.get(connection.options, 'pool.createTimeoutMillis', 2000),
idleTimeoutMillis: _.get(connection.options, 'pool.idleTimeoutMillis', 30000),
reapIntervalMillis: _.get(connection.options, 'pool.reapIntervalMillis', 1000),
createRetryIntervalMillis: _.get(connection.options, 'pool.createRetryIntervalMillis', 200),
2019-01-09 23:24:45 +13:00
};
2018-06-04 18:22:21 +02:00
if (options.client === 'pg') {
client.types.setTypeParser(1700, 'text', parseFloat);
if (_.isString(_.get(options.connection, 'schema'))) {
options.pool = {
afterCreate: (conn, cb) => {
2019-01-09 23:24:45 +13:00
// conn.query(`SET SESSION SCHEMA '${options.connection.schema}';`, (err) => { // It seems the right way is the one below
conn.query(`SET search_path TO '${options.connection.schema}';`, (err) => {
2018-06-04 18:22:21 +02:00
cb(err, conn);
});
}
};
} else {
delete options.connection.schema;
}
}
if (options.client === 'mysql') {
options.connection.typeCast = (field, next) => {
if (field.type === 'TINY' && field.length === 1) {
return (field.string() === '1');
}
return next();
};
}
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.
2017-07-24 19:58:03 +02:00
_.set(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;
};