Skip to content

Instantly share code, notes, and snippets.

@jo
Forked from jefbarn/my_code.js
Last active March 9, 2017 09:48

Revisions

  1. jo revised this gist Mar 9, 2017. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion my_code.js
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,8 @@ remotedb.bulkDocs([

    let localdb = new PouchDB('mydb')

    PouchDB.replicate(remotedb, localdb, {
    const replication = PouchDB.replicate(remotedb, localdb, {
    live: true,
    filter: '_selector',
    selector: {
    kind: {
    @@ -44,3 +45,6 @@ PouchDB.replicate(remotedb, localdb, {
    })

    // Now localdb only has 'apple' and 'pear' documents

    // now lets cancel the request:
    replication.cancel()
  2. @jefbarn jefbarn created this gist Feb 11, 2017.
    46 changes: 46 additions & 0 deletions my_code.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    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')

    PouchDB.replicate(remotedb, localdb, {
    filter: '_selector',
    selector: {
    kind: {
    $in: [
    'apple',
    'pear',
    ]
    }
    }
    })

    // Now localdb only has 'apple' and 'pear' documents