From 9aa2cedf8527f4d6ac331732f4c0e405a0f3f80c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Georget?= Date: Mon, 23 Jan 2017 16:28:20 +0100 Subject: [PATCH] Allow custom tableName on M2M associations and select PG schema --- packages/strapi-bookshelf/lib/index.js | 2 +- packages/strapi-knex/lib/index.js | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/packages/strapi-bookshelf/lib/index.js b/packages/strapi-bookshelf/lib/index.js index 0342b4d65d..2486cd0182 100755 --- a/packages/strapi-bookshelf/lib/index.js +++ b/packages/strapi-bookshelf/lib/index.js @@ -192,7 +192,7 @@ module.exports = function (strapi) { break; } case 'belongsToMany': { - const tableName = _.map(_.sortBy([strapi.models[details.collection].attributes[details.via], details], 'collection'), table => { + const tableName = _.get(details, 'tableName') || _.map(_.sortBy([strapi.models[details.collection].attributes[details.via], details], 'collection'), table => { return _.snakeCase(pluralize.plural(table.collection) + ' ' + pluralize.plural(table.via)); }).join('__'); diff --git a/packages/strapi-knex/lib/index.js b/packages/strapi-knex/lib/index.js index c3143d9b0d..fccf9b01e4 100755 --- a/packages/strapi-knex/lib/index.js +++ b/packages/strapi-knex/lib/index.js @@ -91,13 +91,29 @@ module.exports = strapi => { client: connection.settings.client, connection: { host: _.get(connection.settings, 'host'), - user: _.get(connection.settings, 'username'), + user: _.get(connection.settings, 'username') || _.get(connection.settings, 'user'), password: _.get(connection.settings, 'password'), database: _.get(connection.settings, 'database'), - charset: _.get(connection.settings, 'charset') - } + charset: _.get(connection.settings, 'charset'), + schema: _.get(connection.settings, 'schema'), + }, + debug: _.get(connection, 'debug') || false }, strapi.config.hooks.knex); + 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); + }); + } + }; + } + + console.log(options); + // Finally, use the client via `knex`. // If anyone has a solution to use different paths for `knex` and clients // please drop us an email at support@strapi.io-- it would avoid the Strapi