Sequelize: Using Multiple Databases

前端 未结 3 517
Happy的楠姐
Happy的楠姐 2020-12-02 17:26

Do I need to create multiple instances of Sequelize if I want to use two databases? That is, two databases on the same machine.

If not, what\'s the proper way to do

3条回答
  •  半阙折子戏
    2020-12-02 18:12

    Why don't you use raw query? With this you can connect to one database and query the other. See sample code below.

    const sequelize = require('db_config');
    function test(req, res){
      const qry = `SELECT * FROM db1.affiliates_order co
    LEFT JOIN db2.affiliates m ON m.id = co.campaign_id`;
      sequelize.query(qry, null, {  raw: true}).then(result=>{
        console.log(result);
      })
    }
    

提交回复
热议问题