Reputation: 133
I am working on a node.js app where I need to connect to more than one databases. One of the database is central database which contains information common to all. And then there are country level databases where data is stored according to the countries.
I am using sequelize ORM in the app.
Database is postgresql.
Framework is express.
The problem is I want to decide on runtime based on the request which database to use and models should automatically connect to the appropriate database. I have seen this question but didn't found it helpful.
I have also checked in another forums but didn't find anything.
Upvotes: 3
Views: 3683
Reputation: 7401
You need to create objects corresponding to each of your database, and at each this object you need to instantiate the Sequelize. Further, for each sequelize instance you need to import the models (assumming that all those databases have exactly the same tables and model representations).
import Sequelize from 'sequelize';
let connectionsArray = [
'postgres://user:[email protected]:5432/country1',
'postgres://user:[email protected]:5432/country2',
'postgres://user:[email protected]:5432/country3',
];
let country1DB, country2DB, country3DB;
country1DB = country2DB = country3DB = {};
country1DB.Sequelize = country2DB.Sequelize = country3DB.Sequelize = Sequelize;
country1DB.sequelize = new Sequelize(connectionsArray[0]);
country2DB.sequelize = new Sequelize(connectionsArray[1]);
country3DB.sequelize = new Sequelize(connectionsArray[2]);
// here you need to access the models path, maybe with fs module
// iterate over every model and import it into every country sequelize instance
// let's assume that models' paths are in simple array
models.forEach(modelFile => {
let model1DB = country1DB.sequelize.import(modelFile);
let model2DB = country2DB.sequelize.import(modelFile);
let model3DB = country3DB.sequelize.import(modelFile);
country1DB[model1DB.name] = model1DB;
country2DB[model2DB.name] = model2DB;
country3DB[model3DB.name] = model3DB;
});
// now every country?DB object has it's own sequelize instance and all model definitions inside
export {
country1DB,
country2DB,
country3DB
};
This is just some example code, it would need refactor to be useful (introduce some loops etc.). It should just show you the idea of how to use multiple databases within single application. If you would like to use e.g. country1
database somewhere, you would simply do
import { country1DB } from './databases';
country1DB.User.findAll({...});
Above code would perform SELECT * FROM users
in previously specified country1
database. Example express
route could look like below:
import * as databases from './databases';
app.get('/:dbIndex/users', (req, res) => {
databases['country' + req.params.dbIndex + 'DB'].User.find().then(user => {
res.json(user.toJSON());
});
});
Or, even better, you could write some middleware
function that would be run before every request and which would be responsible for choosing proper database for further operations.
Upvotes: 3