Sequelize:连接多个具有不同架构/模型的数据库

huangapple go评论69阅读模式
英文:

Sequelize: Connect multiple databases with the Different schema/model

问题

posAdminDB.users = require("../models/user/user")(sequelize, DataTypes);
  1. 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(&quot;../models/user/user&quot;)(sequelize, DataTypes);

  1. Then
    error:
***/models/user/user.js:2
  const User = sequelize.define(&quot;user&quot;, {
                         ^
TypeError: Cannot read properties of undefined (reading &#39;define&#39;)

> user model

module.exports = (sequelize, DataTypes) =&gt; {
  const User = sequelize.define(&quot;user&quot;, {
    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(() =&gt; {
        console.log(&quot;Connection has been established with DB:1 successfully&quot;);
    });
    posAdminDB.authenticate().then(() =&gt; {
        console.log(&quot;Connection has been established with DB:2 successfully&quot;);
    });
} catch (error) {
    console.error(&quot;Unable to connect to the database:&quot;, 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(&quot;../models/user/user&quot;)(posAdminDB.sequelize, DataTypes);
mainDB.users = require(&quot;../models/user/user&quot;)(mainDB.sequelize, DataTypes);

huangapple
  • 本文由 发表于 2023年1月4日 06:35:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/74999307.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定