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)
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.
Models can be defined in two equivalent ways in Sequelize: Calling sequelize.define(modelName, attributes, options) Extending Model and calling init(attributes, options)
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.
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 } );
I found a link to the bug here.
https://github.com/sequelize/sequelize/pull/1251
You need to add your own pool property.
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