Skip to content

Define replication config in beforeConnect hook is not working #14136

Closed

Description

@collierrgbsitisfise

Define replication config asynchronous

There is cases when application receive necessary configs asynchronous. In such cases hooks are used to setup necessary values

export const sequelize = new Sequelize(database, user, password, {
  ...baseConfig,
   hooks: {
     beforeConnect: async (config) => {
       config.replication = await getReplicationAsyncWay();
     },
   },
});

What do you expect to happen?

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 ConnectionManager initialization. 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.

Activity

  1. changed the title [-]Define replication config asynchronous is not working[/-] [+]Define replication config in beforeConnect hook is not working[/+] on Feb 18, 2022
  2. ephys commented on Feb 18, 2022

    @ephys
    Member

    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 :/

  3. collierrgbsitisfise commented on Feb 20, 2022

    @collierrgbsitisfise
    ContributorAuthor

    @ephys Ok, thanks ! In case it's expected behavior I am closing this issue

  4. otottkovacstibor commented on Mar 23, 2022

    @otottkovacstibor

    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.

    sequelize.beforeConnect(async (config) => {
      config.password = await getAuthToken();
    });

    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

    export const sequelize = new Sequelize(database, user, password, {
      ...baseConfig,
       hooks: {
         beforeConnect: async (config) => {
           config.replication.write.password = await getAuthToken('master');
           config.replication.read[0].password = await getAuthToken('replica');
         },
       },
    });

    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

  5. ephys commented on Mar 23, 2022

    @ephys
    Member

    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.

    When using replication, the config object that is passed to beforeConnect is equal to config.replication.read[x] or config.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:

    export const sequelize = new Sequelize(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 = await getAuthToken('master');
           } else if (config.host === REPLICA_HOST) {
             config.password = await getAuthToken('replica');
           }
         },
       },
    });
  6. otottkovacstibor commented on Mar 23, 2022

    @otottkovacstibor

    @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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions