apache / apache/pouchdb

Replication with a view filter does not replicate the design doc

Open
#8,617 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
17.6k
Forks
1.5k
PR merge metrics
No merged PRs in 30d

Description

### Issue
I have a single database where each document has multiple users listed in an array field users: [users1, ...] (the user has a role, join, and update field). I'm creating a view to index those users and setting the replication filter with the said view.

This is the view filter I'm using:
````js
(doc) =>
doc._id !== "_design/app" &&
Object.keys(doc.users).forEach((user) => emit(user, null));
````

The problem I'm facing is that the replication with the view filter does not include the design doc, and I'm getting this error:
````js
CustomPouchError {stack: 'not_found: missing\n at https://..., status: 404, name: 'not_found', message: 'missing', error: true, …}
docId: "_design/app" ...
````
The only workaround that I can think of is to perform a first replication with a doc_ids filter to only get the design doc (from this [StackOverflow answer](https://stackoverflow.com/a/50995858/12594730), I assume it's fast), and then perform the normal live replication with the view filter.

````js
await new Promise((res) => {
const sync = this.local
.sync(url, {
doc_ids: [designDocID],
})
.on("complete", () => {
sync.cancel();
res(true);
});
});

this.local.sync(url, {
live,
retry,
view,
query_params: { key: user },
});
````

For now, it's working as expected, but I have a few questions:

- Is there a better way to achieve the above?

- Are filters with views faster? I don't have this problem with a normal filter, but I keep reading everywhere that it's slow. So, I'm assuming that filtered replication with views uses CouchDB indexes and should be faster.

Thanks.

### Info
- Environment: browser
- Platform: Chrome/FF/Safari/Edge/iOS/Android/etc.
- Adapter: Default
- Server: Docker CouchDB behind an Ngnix proxy then a NodeJS (for authentication).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.