loopbackio / loopbackio/loopback-next

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

Open
#9,388 1 comment 0 reactions 0 assignees View on GitHub
feature
Dominant language
TypeScript
Stars
5.1k
Forks
1.1k
Avg merge
2d 21h
Merged PRs (30d)
27

Description

### 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,

```Javascript
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

```shell
NA
```

### Additional information

NA

### Reproduction

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

Contributor guide

Open the contributing guide

Research direction

Start with the linked reproduction branch and trace the repository calls using find and findById with per-invocation readPreference options against MongoDB 4.2. Verify that a method-level preference changes only that invocation, while the application and datasource setting remains primaryPreferred; confirm the resulting server selection in MongoDB logs.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, typescript
Domain
backend, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.