'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, '..'); const defaultConfig = loadEggConfig(path.join(projectRoot, 'config', 'config.default.js')); const localConfig = loadEggConfig(path.join(projectRoot, 'config', 'config.local.js')); const appConfig = mergeDeep(defaultConfig, localConfig); const clientName = process.env.SEQUELIZE_DB_CLIENT || 'wms'; const 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 }, };