I am using Node, Express, Angular and Sequelize for database ORM. Everything is working great so far, but I just got a requirement to add an additional datasource to the sequelize/node backend.
How would I go about setting up two different databases (db1 on one server and db2 on a different one) in Sequelize and using them side by side?
Is this even supported, do I need to write up something of my own for it? I have looked around everywhere, but cannot find any mention of doing something like this.
Thanks in advance for any help.
If the schemas are different (for example, db1 is your User/Groups database and db2 is your Orders database), you can just create two Sequelize instances:
var db1sequelize = new Sequelize('db1', 'db1user', 'db1pass'...);
db1sequelize.define('User', {...})
db1sequelize.define('Group', {...})
var db2sequelize = new Sequelize('db2', 'db2user', 'db2pass'...);
db2sequelize.define('Order', {...})
User.findAll(...) // queries db1
Order.findAll(...) // queries db2
I suspect, however, that associations between those wouldn't work well. For example, you could definitely associate Users and Groups because they would all fall in the same schema:
User.hasMany(Group);
but to associate between Orders and Users I would instead just add the column manually.
db2sequelize.define('Order', {
userId: Sequelize.INTEGER,
...
})
User.create({...}).then(function(createdUser) { // inserts in db1
return Order.create({userId: createdUser.id, ...}) // inserts in db2
});
I'm also not sure how transactions would work between those databases...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With