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'
}
})
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.
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
.
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