Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you specify a read replica using Sequelize?

How do you specify a read replica using Sequelize?

I have created a very basic test based on their documentation.

https://github.com/sequelize/sequelize/blob/master/docs/usage.md

var Sequelize = require("sequelize");

var sequelize = new Sequelize('database', 'root', '', {
    dialect: 'mysql',
    port: 3306,
    replication: {
        read: [
            { host: 'host1' }
        ],
        write: { host: 'host2' }
    },
    pool: { // If you want to override the options used for the read pool you can do so here
        maxConnections: 20,
        maxIdleTime: 30000
    }
})


var Test = sequelize.define('grammar_scores', {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    user_id: {
        type: Sequelize.INTEGER,
        index: true
    }
}, {
    underscored: true
});

Test.findAll({ where:{user_id:89} }).success(console.log);

which throws the following error...

/Users/tim/code/sequelizetest/node_modules/sequelize/lib/dialects/mysql/connector-manager.js:333
if (config.pool !== null && config.pool.handleDisconnects) {
^
    TypeError: Cannot read property 'handleDisconnects' of undefined
    at module.exports.connect (/Users/tim/code/sequelizetest/node_modules/sequelize/lib/dialects/mysql/connector-manager.js:333:44)
like image 900
Tim Fairbrother Avatar asked Jun 20 '14 06:06

Tim Fairbrother


People also ask

What is a Read replica?

A read replica is a copy of the primary instance that reflects changes to the primary in almost real time, in normal circumstances. You can use a read replica to offload read requests or analytics traffic from the primary instance. Additionally, for disaster recovery, you can perform a regional migration.

How do you define a model in Sequelize?

Models can be defined in two equivalent ways in Sequelize: Calling sequelize.define(modelName, attributes, options) Extending Model and calling init(attributes, options)

What is read replica in MySQL?

The read replica feature allows you to replicate data from an Azure Database for MySQL server to a read-only server. You can replicate from the source server to up to five replicas. Replicas are updated asynchronously using the MySQL engine's native binary log (binlog) file position-based replication technology.

How do you write a query in Sequelize?

Sequelize instance comes with the query() method which you can use to run a raw query. The syntax of the method is as shown below: const [results, metadata] = await sequelize. query( "Your query here", { options } );


1 Answers

I found a link to the bug here.

https://github.com/sequelize/sequelize/pull/1251

You need to add your own pool property.

like image 89
Tim Fairbrother Avatar answered Oct 24 '22 13:10

Tim Fairbrother