Skip to content

Instantly share code, notes, and snippets.

@kosssi
Last active December 24, 2015 07:56
Show Gist options
  • Save kosssi/00b8054e42943ef4585d to your computer and use it in GitHub Desktop.
Save kosssi/00b8054e42943ef4585d to your computer and use it in GitHub Desktop.
PouchDB = require 'pouchdb'
async = require 'async'
class ReplicationWithFilter
cozy: null
mobile: null
design:
_id: "_design/myfilter"
filters:
myfilter: Object.toString.apply (doc) ->
doc.type == 'marsupial'
doc1: _id: 'a', name: 'Kangaroo', type: 'marsupial'
doc2: _id: 'b', name: 'Koala', type: 'marsupial'
doc3: _id: 'c', name: 'Platypus', type: 'monotreme'
constructor: () ->
console.log '# constructor'
# version avec pouch
#@cozy = new PouchDB 'cozy', {db: require 'memdown'}
# version avec couch
@cozy = new PouchDB('http://localhost:5984/test');
@mobile = new PouchDB 'mobile', {db: require 'memdown'}
addAllCozyDocs: (display = false) ->
console.log '# enter addCozyDesign'
@_addDoc @cozy, @design, display
@_addDoc @cozy, @doc1, display
@_addDoc @cozy, @doc2, display
@_addDoc @cozy, @doc3, display
_addDoc: (db, doc, display = false) ->
console.log '# enter _addDoc'
db.put doc
.then (response) ->
console.log '# _addDoc response:' if display
console.log response if display
.catch (err) ->
console.log '# _addDoc error:' if display
console.log err if display
displayAllMobileDoc: (display = false) ->
console.log '# enter displayMobile'
@_displayAllDocs @mobile, display
displayAllCozyDocs: (display = false) ->
console.log '# enter displayCozy'
@_displayAllDocs @cozy, display
_displayAllDocs: (db, display = false) ->
console.log '# enter _display'
db.allDocs
include_docs: true
.then (result) ->
console.log '# _display: result'
console.log result if display
.catch (err) ->
console.log '# _display: error'
console.log err if display
syncCozyToMobile: (display = false) ->
console.log '# enter syncCozyToMobile'
@mobile.replicate.from @cozy,
filter: 'myfilter/myfilter'
.on 'complete', (result) ->
console.log '# syncCozyToMobile: result' if display
console.log result if display
.catch (err) ->
console.log '# syncCozyToMobile: err' if display
console.log err if display
test = new ReplicationWithFilter()
test.addAllCozyDocs(true)
.then () ->
test.displayAllCozyDocs(true)
.then () ->
test.syncCozyToMobile(true)
.then () ->
test.displayAllMobileDoc(true)
@kosssi
Copy link
Author

kosssi commented Dec 24, 2015

Il faut avoir une base de données couch nommé 'test', puis tu peux executer la commande suivante.

coffee replication_with_filter.coffee

Voici le resultat.

# constructor
# enter addCozyDesign
# enter _addDoc
# enter _addDoc
# enter _addDoc
# enter _addDoc
# enter displayCozy
# enter _display
# _display: result
{ total_rows: 4,
  offset: 0,
  rows: 
   [ { id: '_design/myfilter',
       key: '_design/myfilter',
       value: [Object],
       doc: [Object] },
     { id: 'a', key: 'a', value: [Object], doc: [Object] },
     { id: 'b', key: 'b', value: [Object], doc: [Object] },
     { id: 'c', key: 'c', value: [Object], doc: [Object] } ] }
# enter syncCozyToMobile
# syncCozyToMobile: result
{ ok: true,
  start_time: Thu Dec 24 2015 08:40:42 GMT+0100 (CET),
  docs_read: 2,
  docs_written: 2,
  doc_write_failures: 0,
  errors: [],
  last_seq: 4,
  status: 'complete',
  end_time: Thu Dec 24 2015 08:40:42 GMT+0100 (CET) }
# enter displayMobile
# enter _display
# _display: result
{ total_rows: 2,
  offset: 0,
  rows: 
   [ { id: 'a', key: 'a', value: [Object], doc: [Object] },
     { id: 'b', key: 'b', value: [Object], doc: [Object] } ] }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment