Skip to content

Instantly share code, notes, and snippets.

@kosssi
Created February 5, 2016 12:24
Show Gist options
  • Save kosssi/a614fda9916c9ce2889b to your computer and use it in GitHub Desktop.
Save kosssi/a614fda9916c9ce2889b to your computer and use it in GitHub Desktop.
should = require('chai').should()
PouchDB = require('pouchdb')
async = require('async')
db = new PouchDB('mobile', db: require('memdown'))
remote = new PouchDB('remote', db: require('memdown'))
filter =
_id: '_design/myfilter'
filters: config: ((doc) ->
doc.type == 'ok'
).toString()
localDocs = [
{ _id: '0' }
{
_id: '1'
type: 'ok'
}
filter
]
remoteDocs = [
{ _id: 'a' }
{
_id: 'b'
type: 'ok'
}
filter
]
remote.bulkDocs(remoteDocs).then(->
db.bulkDocs localDocs
).then(->
new (PouchDB.utils.Promise)((resolve, reject) ->
filter = 'myfilter/config'
changes = 0
onChange = (c) ->
changes += c.change.docs.length
if changes == 2
sync.cancel()
return
sync = db.sync(remote,
filter: filter
live: true
retry: true
batch_size: 20
batches_limit: 5
back_off_function: (delay) ->
return 1000 if delay is 0
return delay if delay > 60000
return delay * 2).on('error', reject).on('change', onChange).on('complete', resolve)
return
)
).then(->
db.allDocs()
).then((docs) ->
console.log docs.total_rows
remote.allDocs()
).then (docs) ->
console.log docs.total_rows
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment