-
-
Save jo/c818e9147e38ee2fe39cc938a37e451e to your computer and use it in GitHub Desktop.
Using Pouchdb _selector plug-in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PouchDB from 'pouchdb-browser' // Or however you are importing PouchDB using your module system | |
PouchDB.plugin((PouchDB) => { // Add the 'plug-in' to the Pouchdb instance | |
let pouchReplicate = PouchDB.replicate | |
PouchDB.replicate = (source, target, repOptions) => { | |
let sourceAjax = source._ajax | |
source._ajax = (ajaxOps, callback) => { | |
if (ajaxOps.url.includes('_selector')) { | |
ajaxOps.url = ajaxOps.url.replace('filter=_selector%2F_selector', 'filter=_selector') | |
ajaxOps.method = 'POST' | |
ajaxOps.body = { | |
selector: repOptions.selector | |
} | |
} | |
return sourceAjax(ajaxOps, callback) | |
} | |
return pouchReplicate(source, target, repOptions) | |
} | |
}) | |
let remotedb = new PouchDB('http://my.couchd.db:5984/database') | |
// put some docs in the remote db | |
remotedb.bulkDocs([ | |
{kind: 'apple'}, | |
{kind: 'orange'}, | |
{kind: 'pear'} | |
]) | |
let localdb = new PouchDB('mydb') | |
const replication = PouchDB.replicate(remotedb, localdb, { | |
live: true, | |
filter: '_selector', | |
selector: { | |
kind: { | |
$in: [ | |
'apple', | |
'pear', | |
] | |
} | |
} | |
}) | |
// Now localdb only has 'apple' and 'pear' documents | |
// now lets cancel the request: | |
replication.cancel() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment