Skip to content

Update the datasource/database read preference at the run time.  #9388

@shivambmgupta

Description

@shivambmgupta

Describe the bug

For architectural reasons, I need to update the readPreference for a given method, on a given repository at the fly.
Please note, this change should be done at real time, and impact only this function invocation. However, the readPreferrence at the application level/datasource level should remain intact to "primaryPreferred".

For example,

this.userRepo.find(filter, { readPreference: "secondary" }); // request is still going to primary MongoDB server!
this.userRepo.findById(filter, { readPreference: "primary" });

I've tried this, but upon checking mongodb logs, this does not seem to work. All the request are going to primary server.

MongoDB version: v4.2

Logs

NA

Additional information

NA

Reproduction

https://github.com/shivambmgupta/loopback-next/tree/bug/read-preference-update

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions