Sequelize: Using Multiple Databases

if you are trying to associate objects in the same RDS across multiple databases, you can use schema.

http://docs.sequelizejs.com/class/lib/model.js~Model.html#static-method-schema

this will prepend the db name to the table name so, presumably, your queries would come out like: SELECT A.ColA, B.ColB FROM SchemaA.ATable A INNER JOIN SchemaB.BTable B ON B.BId = A.BId


You need to create different instances of sequelize for each DB connection you want to create:

const { Sequelize } = require('sequelize');
const userDb = new Sequelize(/* ... */);
const contentDb = new Sequelize(/* ... */);

Each instance created from sequelize has its own DB info (db host, url, user, pass, etc...), and these values are not meant to be changed, so there is no "correct" way to create multiple connections with one instance of sequelize.

From their docs:

Observe that, in the examples above, Sequelize refers to the library itself while sequelize refers to an instance of Sequelize, which represents a connection to one database. This is the recommended convention and it will be followed throughout the documentation.

A "common" approach to do this, is having your databases in a config.json file and loop over it to create connections dinamically, something like this maybe:

config.json

{
    /*...*/
    databases: {
        user: {
            path: 'xxxxxxxx'
        },
        content: {
            path: 'xxxxxxxx'
        }
    }
}

Your app

const Sequelize = require('sequelize');
const config = require('./config.json');

// Loop through
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

You will need to do a little bit more coding to get it up and running but its a general idea.