Using node.js, how can I write to one MySQL DB and read from another?
mysql, node.js, npm
Solution
You can just create two adapters like this:
var activeRecord = require('mysql-activerecord');
var readDB = new activeRecord.Adapter({
server: 'slave.db.com',
username: 'user',
password: 'pass',
database: 'db'
});
var writeDB = new activeRecord.Adapter({
server: 'master.db.com',
username: 'user',
password: 'pass',
database: 'db'
});
When you go to do an `update` or other writing query, use `writeDB`; when you go to do a `select` use `readDB`.
Another option would be to switch to another ORM that supports this out of the box. I know this isn't as nice of a solution though. Sequelize for example is widely used and supports this out of the box.
Problem
Specifically, I'm talking about `MySQL Master-Slave(s) Replication`. As I understand it, all of my reads should happen from a slave (or multiple slaves hid behind a load balancer) and all of my writes directly to the master. How can I do this with `node.js`? I'm using the MySQL Active Record package for my database queries and I don't think that supports it natively. I supposed I can hack away at the package and have a `if is write-query, then use db1` type of situation, but I wonder if there's an easier way.