4

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.

2
  • Is there some reason you don't want to just create two Adapters? Commented Apr 8, 2013 at 22:33
  • What do you mean create two adapters? Commented Apr 8, 2013 at 22:33

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.