Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Connection URIs with Read Replication in Sequelize

If I have a connection URI, I can use that normally with Sequelize as such:

const sequelize = new Sequelize('postgres://user:[email protected]:5432/dbname');

However, if I want to use Read and Write replication (https://sequelize.org/master/manual/read-replication.html), then there doesn't seem an option to use connection URI. Can I pass connection URI strings to read and write in the replication option as in:

const sequelize = new Sequelize(null, null, null, {
  dialect: 'postgres',
  replication: {
    read: [
      'postgres://user:[email protected]:5432/dbname',
      'postgres://user:[email protected]:5432/dbname'
    ],
    write: 'postgres://user:[email protected]:5432/dbname'
  }
})

EDIT:

I have already found a solution to the issue. and that is using an npm library like connection string to parse the connection string as shown below:

const write_uri = new ConnectionString(uri);
const sequelize = new Sequelize(null, null, null, {
  dialect: 'postgres',
  replication: {
    read: [
      'postgres://user:[email protected]:5432/dbname',
      'postgres://user:[email protected]:5432/dbname'
    ],
    write: {
      host: write_uri.hosts[0].name,
      username: write_uri.user,
      password: write_uri.password,
      database: write_uri.path[0],
      port: write_uri.hosts[0].port
    }
  }
});

But, that is not what I'm looking for.

like image 263
Parth Avatar asked Oct 16 '22 07:10

Parth


1 Answers

As per sequelize source at master, you can't.

According to the sequelize source docs at sequelize.js, The Sequelize Constructor accepts options agrument, like this

constructor(database, username, password, options){

}

Where options.replication should be an object with two properties, read and write. Write should be an object (a single server for handling writes), and read an array of object (several servers to handle reads). Each read/write server can have the following properties: host, port, username, password, database.

you need to pass an array of objects to read:[] with connection values as props instead of passing strings.

like image 85
Tridev Mishra Avatar answered Oct 20 '22 09:10

Tridev Mishra