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); + }, + }, +};