Skip to content

Commit b54af7f

Browse files
Merge pull request #6 from Wojtach/push-pull-filter-docs
Added section about push pull replication filters
2 parents 1a37e05 + 3b50837 commit b54af7f

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

docs/DataSync/remote-sync-gateway.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,85 @@ config.addCollection(collection);
414414
config.setHeaders({ "CustomHeaderName": "Value" });
415415
```
416416

417+
### Replication filters
418+
419+
Replication Filters allow you to have quick control over the documents stored as the result of a push and/or pull replication.
420+
421+
##### Push Filter
422+
423+
The push filter allows an app to push a subset of a database to the server. This can be very useful. For instance, high-priority documents could be pushed first, or documents in a "draft" state could be skipped.
424+
425+
```typescript
426+
const targetUrl = new URLEndpoint('"ws://localhost:4984/mydatabase"');
427+
428+
const config = new ReplicatorConfiguration(targetUrl);
429+
const colConfig = new CollectionConfig(null, null);
430+
431+
colConfig.pushFilter((documents, flags) => {
432+
// (1)
433+
"show source"; // directive needed to persist function body as string
434+
435+
if (document?.["type"] === "draft") {
436+
return false;
437+
}
438+
return true;
439+
});
440+
441+
config.addCollection(collection, colConfig);
442+
443+
const replicator = await Replicator.create(config);
444+
await replicator.start();
445+
```
446+
447+
1. The callback should follow the semantics of a [pure function](https://en.wikipedia.org/wiki/Pure_function). Otherwise, long running functions would slow down the replicator considerably.
448+
449+
##### Pull Filter
450+
451+
The pull filter gives an app the ability to validate documents being pulled, and skip ones that fail. This is an important security mechanism in a peer-to-peer topology with peers that are not fully trusted.
452+
453+
:::note
454+
Pull replication filters are not a substitute for [channels](https://docs.couchbase.com/sync-gateway/current/channels.html). Sync Gateway channels are designed to be scalable (documents are filtered on the server) whereas a pull replication filter is applied to a document once it has been downloaded.
455+
:::
456+
457+
```typescript
458+
const targetUrl = new URLEndpoint('"ws://localhost:4984/mydatabase"');
459+
460+
const config = new ReplicatorConfiguration(targetUrl);
461+
const colConfig = new CollectionConfig(null, null);
462+
463+
colConfig.pullFilter((documents, flags) => {
464+
// (1)
465+
"show source"; // directive needed to persist function body as string
466+
467+
if (flags.includes(ReplicatedDocumentFlag.DELETED)) {
468+
return false;
469+
}
470+
return true;
471+
});
472+
473+
config.addCollection(collection, colConfig);
474+
475+
const replicator = await Replicator.create(config);
476+
await replicator.start();
477+
```
478+
479+
1. The callback should follow the semantics of a [pure function](https://en.wikipedia.org/wiki/Pure_function). Otherwise, long running functions would slow down the replicator considerably.
480+
481+
:::caution
482+
For now, pull filters can handle up to 100 documents per replication. If you exceed this number, the replicator may freeze. This will be fixed in the next version.
483+
:::
484+
485+
:::info Losing access to a document via the Sync Function
486+
487+
Losing access to a document (via the Sync Function) also triggers the pull replication filter.
488+
489+
Filtering out such an event would retain the document locally.
490+
491+
As a result, there would be a local copy of the document disjointed from the one that resides on Couchbase Server.
492+
493+
Further updates to the document stored on Couchbase Server would not be received in pull replications and further local edits could be pushed but the updated versions will not be visible.
494+
:::
495+
417496
### Channels
418497

419498
By default, Couchbase Lite gets all the channels to which the configured user account has access.

0 commit comments

Comments
 (0)