From 60acfa339f2eeeba05b0709e89e34b5d46925b2a Mon Sep 17 00:00:00 2001 From: Jan Dolezel Date: Thu, 9 Nov 2017 14:58:09 +0100 Subject: [PATCH] Possibility to use function in multi-column index --- lib/operations/indexes.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/operations/indexes.js b/lib/operations/indexes.js index a1350a8d..925d09fb 100644 --- a/lib/operations/indexes.js +++ b/lib/operations/indexes.js @@ -10,14 +10,16 @@ function generateIndexName(table, columns, options) { : template`${table_name}_${_.isArray(columns) ? columns.join('_') : columns}${options.unique ? '_unique' : ''}_index`; } +function generateColumnString(column) { + return /.+\(.*\)/.test(column) + ? column // expression + : template`"${column}"`; // single column +} + function generateColumnsString(columns) { - if (_.isArray(columns)) { - return columns.map(name => template`"${name}"`).join(', '); - } - if (/.+\(.*\)/.test(columns)) { // expression - return columns; - } - return template`"${columns}"`; // single column + return _.isArray(columns) + ? columns.map(generateColumnString).join(', ') + : generateColumnString(columns); } export const create = (table_name, columns, options = {}) => {