需要为每一个数据库连接创建sequelize不同的情况下,你要创建:
const Sequelize = require('Sequelize');
const userDb = new Sequelize(/* ... */);
const contentDb = new Sequelize(/* ... */);
此播放sequelize创建的每个实例都有自己的数据库信息,因此无法使用一个sequelize实例创建多个连接。
config.json
{
/*...*/
databases: {
user: {
path: 'xxxxxxxx'
},
content: {
path: 'xxxxxxxx'
}
}
}
应用程序中的连接
const Sequelize = require('sequelize');
const config = require('./config.json');
const db = {};
const databases = Object.keys(config.databases);
for(let i = 0; i < databases.length; ++i) {
let database = databases[i];
let dbPath = config.databases[database];
db[database] = new Sequelize(dbPath);
}
// Sequelize instances:
// db.user
// db.content