Fix inconsistency between knexfile and runtime knex config

cshd
Dragory 2020-08-13 23:55:29 +03:00
parent 8ba25d9504
commit 77cb19e70c
No known key found for this signature in database
GPG Key ID: 5F387BA66DF8AAC1
3 changed files with 49 additions and 47 deletions

View File

@ -1,2 +1,2 @@
const config = require('./src/cfg'); const knexConfig = require("./src/knexConfig");
module.exports = config.knex; module.exports = knexConfig;

View File

@ -1,45 +1,2 @@
const path = require("path"); const knexConfig = require("./knexConfig");
const config = require("./cfg"); module.exports = require("knex")(knexConfig);
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);
},
},
});

45
src/knexConfig.js Normal file
View File

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