You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Define replication config in beforeConnect hook is not working #14136
On read(select) queries should be used read replica
What is actually happening?
Looks like replication ignored at all when we mutate config using beforeConnect hook.
From my investigation it is happens cause, initPools is called once on ConnectionManagerinitialization. Only here are defined and never mutated on lifecycle of sequelize instance.
Environment
Sequelize version: 6.6.5
Node.js version: v14.18.1
Bug Report Checklist
How does this problem relate to dialects?
I think this problem happens regardless of the dialect.
I think this problem happens only for the following dialect(s):
I don't know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX
Would you be willing to resolve this issue by submitting a Pull Request?
Yes, I have the time and I know how to start.
Yes, I have the time but I don't know how to start, I would need guidance.
No, I don't have the time, although I believe I could do it if I had the time...
No, I don't have the time and I wouldn't even know how to start.
changed the title [-]Define replication config asynchronous is not working[/-][+]Define replication config in beforeConnect hook is not working[/+]on Feb 18, 2022
config.replication cannot be changed in beforeConnnect (we should print an error if someone tries to do it because it won't have any effect), but what you're trying to do should be possible.
This means you should be able to generate the right token based on the host of the connection being created. Something along the lines of this:
exportconstsequelize=newSequelize(database,user,password,{
...baseConfig,hooks: {beforeConnect: async(config)=>{// the 'config' object is either equal to 'baseConfig.replication.write'// or one of the entries in 'baseConfig.replication.write'if(config.host===MASTER_HOST){config.password=awaitgetAuthToken('master');}elseif(config.host===REPLICA_HOST){config.password=awaitgetAuthToken('replica');}},},});
@ephys - The format of the incoming config object was the misunderstanding for me.
In this case I can easily handle the passwords for different hosts.
Thank you!
Activity
[-]Define replication config asynchronous is not working[/-][+]Define replication config in beforeConnect hook is not working[/+]ephys commentedon Feb 18, 2022
I don't think that's something we should support, it sounds unsafe.
Instead you should delay the creation of the sequelize instance to after your configuration is ready.
If you're using ES Modules, or can migrate to ES Modules, Top Level Await would let you do just that.
If you're using a framework, it's likely that the framework has something built-in for this (e.g. nest proposes https://docs.nestjs.com/fundamentals/async-providers)
If neither of those are possible, I'm afraid you're going to have to implement a mechanism in your app :/
collierrgbsitisfise commentedon Feb 20, 2022
@ephys Ok, thanks ! In case it's expected behavior I am closing this issue
otottkovacstibor commentedon Mar 23, 2022
Hi @ephys - I'm having almost the same issue with the beforeConnect hook and read replicas.
We're using this hook to set the temporary IAM password on each connection as it described in the docs.
However I would like to setup a read-replica set and use the beforeConnect hook to configure the temporary password by instance.
I'm trying to override the passwords in this way
Do you have any suggestion how can I achive this?
Unfortunately the password cannot be cached as it valid for 15 minutes only.
Thanks,
Tibor
ephys commentedon Mar 23, 2022
config.replicationcannot be changed inbeforeConnnect(we should print an error if someone tries to do it because it won't have any effect), but what you're trying to do should be possible.When using replication, the
configobject that is passed tobeforeConnectis equal toconfig.replication.read[x]orconfig.replication.write.This means you should be able to generate the right token based on the host of the connection being created. Something along the lines of this:
otottkovacstibor commentedon Mar 23, 2022
@ephys - The format of the incoming config object was the misunderstanding for me.
In this case I can easily handle the passwords for different hosts.
Thank you!