英文:
Sequelize: Connect multiple databases with the Different schema/model
问题
posAdminDB.users = require("../models/user/user")(sequelize, DataTypes);
- Then
error:
***/models/user/user.js:2
const User = sequelize.define("user", {
^
TypeError: Cannot read properties of undefined (reading 'define')
user model
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define("user", {
name: {
type: DataTypes.STRING,
},
});
return User;
};
Configuration:
DB: 1
mainDB = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
dialect: dbConfig.DIALECT,
host: dbConfig.HOST,
port: dbConfig.PORT,
define: {
timestamps: false,
},
operatorsAliases: 0,
pool: {
max: dbConfig.pool.max,
mix: dbConfig.pool.min,
acquire: dbConfig.pool.acquire,
idle: dbConfig.pool.idle,
},
});
DB: 2
posAdminDB = new Sequelize(adminDB.DB, adminDB.USER, adminDB.PASSWORD, {
dialect: adminDB.DIALECT,
host: adminDB.HOST,
port: adminDB.PORT,
define: {
timestamps: false,
},
operatorsAliases: 0,
pool: {
max: adminDB.pool.max,
mix: adminDB.pool.min,
acquire: adminDB.pool.acquire,
idle: adminDB.pool.idle,
},
});
Authenticate
mainDB.Sequelize = Sequelize;
mainDB.sequelize = sequelize;
posAdminDB.Sequelize = Sequelize;
posAdminDB.sequelize = sequelize;
try {
mainDB.authenticate().then(() => {
console.log("Connection has been established with DB:1 successfully");
});
posAdminDB.authenticate().then(() => {
console.log("Connection has been established with DB:2 successfully");
});
} catch (error) {
console.error("Unable to connect to the database:", error);
}
There is no error in authenticating
authenticate output: <br/>
server is running port 8080
<br/>
Executing (default): SELECT 1+1 AS result
<br/>
Executing (default): SELECT 1+1 AS result
<br/>
Connection has been established with DB:2 successfully
<br/>
Connection has been established with DB:1 successfully
<br/>
英文:
There are two databases. Couldn't use different schema for different databases. After authenticating successfully. How to connect different schema/model? <br/>
1.
posAdminDB.users = require("../models/user/user")(sequelize, DataTypes);
- Then
error:
***/models/user/user.js:2
const User = sequelize.define("user", {
^
TypeError: Cannot read properties of undefined (reading 'define')
> user model
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define("user", {
name: {
type: DataTypes.STRING,
},
});
return User;
};
<br/>
Configuration:
> DB: 1
mainDB = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
dialect: dbConfig.DIALECT,
host: dbConfig.HOST,
port: dbConfig.PORT,
define: {
timestamps: false,
},
operatorsAliases: 0,
pool: {
max: dbConfig.pool.max,
mix: dbConfig.pool.min,
acquire: dbConfig.pool.acquire,
idle: dbConfig.pool.idle,
},
});
> DB: 2
posAdminDB = new Sequelize(adminDB.DB, adminDB.USER, adminDB.PASSWORD, {
dialect: adminDB.DIALECT,
host: adminDB.HOST,
port: adminDB.PORT,
define: {
timestamps: false,
},
operatorsAliases: 0,
pool: {
max: adminDB.pool.max,
mix: adminDB.pool.min,
acquire: adminDB.pool.acquire,
idle: adminDB.pool.idle,
},
});
> Authenticate
mainDB.Sequelize = Sequelize;
mainDB.sequelize = sequelize;
posAdminDB.Sequelize = Sequelize;
posAdminDB.sequelize = sequelize;
try {
mainDB.authenticate().then(() => {
console.log("Connection has been established with DB:1 successfully");
});
posAdminDB.authenticate().then(() => {
console.log("Connection has been established with DB:2 successfully");
});
} catch (error) {
console.error("Unable to connect to the database:", error);
}
> There is no error in authenticating
authenticate output: <br/>
server is running port 8080
<br/>
Executing (default): SELECT 1+1 AS result
<br/>
Executing (default): SELECT 1+1 AS result
<br/>
Connection has been established with DB:2 successfully
<br/>
Connection has been established with DB:1 successfully
<br/>
答案1
得分: 1
应该将正确的 Sequelize 实例传递给特定的模型注册函数:
posAdminDB.users = require("../models/user/user")(posAdminDB.sequelize, DataTypes);
mainDB.users = require("../models/user/user")(mainDB.sequelize, DataTypes);
英文:
You should pass a correct Sequelize instance to a certain model registration function:
posAdminDB.users = require("../models/user/user")(posAdminDB.sequelize, DataTypes);
mainDB.users = require("../models/user/user")(mainDB.sequelize, DataTypes);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论