97 lines
2.6 KiB
JavaScript
97 lines
2.6 KiB
JavaScript
'use strict';
|
|
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
|
|
function isPlainObject(value) {
|
|
return !!value && typeof value === 'object' && !Array.isArray(value);
|
|
}
|
|
|
|
function mergeDeep(target, source) {
|
|
const output = isPlainObject(target) ? { ...target } : {};
|
|
if (!isPlainObject(source)) {
|
|
return output;
|
|
}
|
|
|
|
for (const [ key, value ] of Object.entries(source)) {
|
|
if (isPlainObject(value) && isPlainObject(output[key])) {
|
|
output[key] = mergeDeep(output[key], value);
|
|
} else if (isPlainObject(value)) {
|
|
output[key] = mergeDeep({}, value);
|
|
} else {
|
|
output[key] = value;
|
|
}
|
|
}
|
|
|
|
return output;
|
|
}
|
|
|
|
function loadEggConfig(configPath) {
|
|
if (!fs.existsSync(configPath)) {
|
|
return {};
|
|
}
|
|
|
|
const configFactory = require(configPath);
|
|
const config = typeof configFactory === 'function'
|
|
? configFactory({ name: 'sequelize-cli', baseDir: path.dirname(path.dirname(configPath)) })
|
|
: configFactory;
|
|
|
|
return isPlainObject(config) ? config : {};
|
|
}
|
|
|
|
function resolveMysqlClient(appConfig, clientName) {
|
|
const clients = appConfig && appConfig.mysql && appConfig.mysql.clients;
|
|
if (clients && clients[clientName]) {
|
|
return clients[clientName];
|
|
}
|
|
|
|
if (appConfig && appConfig.mysql && appConfig.mysql.host) {
|
|
return appConfig.mysql;
|
|
}
|
|
|
|
throw new Error(`MySQL client "${clientName}" not found in Egg config.`);
|
|
}
|
|
|
|
const projectRoot = path.resolve(__dirname, '..');
|
|
|
|
let mysqlClient;
|
|
if (process.env.DB_HOST) {
|
|
mysqlClient = {
|
|
host: process.env.DB_HOST,
|
|
port: process.env.DB_PORT || 3306,
|
|
user: process.env.DB_USER,
|
|
password: process.env.DB_PASSWORD,
|
|
database: process.env.DB_NAME || 'wms',
|
|
};
|
|
} else {
|
|
const defaultConfig = loadEggConfig(path.join(projectRoot, 'config', 'config.default.js'));
|
|
const isProduction = process.env.NODE_ENV === 'production';
|
|
const localConfig = isProduction ? {} : loadEggConfig(path.join(projectRoot, 'config', 'config.local.js'));
|
|
const appConfig = mergeDeep(defaultConfig, localConfig);
|
|
const clientName = process.env.SEQUELIZE_DB_CLIENT || 'wms';
|
|
mysqlClient = resolveMysqlClient(appConfig, clientName);
|
|
}
|
|
|
|
const baseConfig = {
|
|
username: mysqlClient.user,
|
|
password: mysqlClient.password,
|
|
database: mysqlClient.database,
|
|
host: mysqlClient.host,
|
|
port: Number(mysqlClient.port || 3306),
|
|
dialect: 'mysql',
|
|
logging: false,
|
|
define: {
|
|
freezeTableName: true,
|
|
underscored: true,
|
|
timestamps: true,
|
|
paranoid: true,
|
|
deletedAt: 'deleted_at',
|
|
},
|
|
};
|
|
|
|
module.exports = {
|
|
development: { ...baseConfig },
|
|
test: { ...baseConfig },
|
|
production: { ...baseConfig },
|
|
};
|