From 77cb19e70c3ab9168f246074e87efc81a476d482 Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Thu, 13 Aug 2020 23:55:29 +0300 Subject: [PATCH] Fix inconsistency between knexfile and runtime knex config --- knexfile.js | 4 ++-- src/knex.js | 47 ++--------------------------------------------- src/knexConfig.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 47 deletions(-) create mode 100644 src/knexConfig.js diff --git a/knexfile.js b/knexfile.js index 12606e3..623871c 100644 --- a/knexfile.js +++ b/knexfile.js @@ -1,2 +1,2 @@ -const config = require('./src/cfg'); -module.exports = config.knex; +const knexConfig = require("./src/knexConfig"); +module.exports = knexConfig; diff --git a/src/knex.js b/src/knex.js index 45aa9c5..b5f3f5c 100644 --- a/src/knex.js +++ b/src/knex.js @@ -1,45 +1,2 @@ -const path = require("path"); -const config = require("./cfg"); - -let knexOptions; -if (config.dbType === "sqlite") { - const resolvedPath = path.resolve(process.cwd(), config.sqliteOptions.filename); - console.log(`Using an SQLite database:\n ${resolvedPath}`); - - knexOptions = { - client: "sqlite", - connection: { - ...config.sqliteOptions, - }, - }; -} else if (config.dbType === "mysql") { - const host = config.mysqlOptions.host || "localhost"; - const port = config.mysqlOptions.port || 3306; - const mysqlStr = `${config.mysqlOptions.user}@${host}:${port}/${config.mysqlOptions.database}`; - console.log(`Using a MySQL database:\n ${mysqlStr}`); - - knexOptions = { - client: "mysql2", - connection: { - ...config.mysqlOptions, - }, - }; -} - -module.exports = require("knex")({ - ...knexOptions, - - useNullAsDefault: true, - migrations: { - directory: path.resolve(__dirname, "..", "db", "migrations"), - }, - log: { - warn(message) { - if (message.startsWith("FS-related option specified for migration configuration")) { - return; - } - - console.warn(message); - }, - }, -}); +const knexConfig = require("./knexConfig"); +module.exports = require("knex")(knexConfig); diff --git a/src/knexConfig.js b/src/knexConfig.js new file mode 100644 index 0000000..e72a52d --- /dev/null +++ b/src/knexConfig.js @@ -0,0 +1,45 @@ +const path = require("path"); +const config = require("./cfg"); + +let knexOptions; +if (config.dbType === "sqlite") { + const resolvedPath = path.resolve(process.cwd(), config.sqliteOptions.filename); + console.log(`Using an SQLite database:\n ${resolvedPath}`); + + knexOptions = { + client: "sqlite", + connection: { + ...config.sqliteOptions, + }, + }; +} else if (config.dbType === "mysql") { + const host = config.mysqlOptions.host || "localhost"; + const port = config.mysqlOptions.port || 3306; + const mysqlStr = `${config.mysqlOptions.user}@${host}:${port}/${config.mysqlOptions.database}`; + console.log(`Using a MySQL database:\n ${mysqlStr}`); + + knexOptions = { + client: "mysql2", + connection: { + ...config.mysqlOptions, + }, + }; +} + +module.exports = { + ...knexOptions, + + useNullAsDefault: true, + migrations: { + directory: path.resolve(__dirname, "..", "db", "migrations"), + }, + log: { + warn(message) { + if (message.startsWith("FS-related option specified for migration configuration")) { + return; + } + + console.warn(message); + }, + }, +};