Last active
December 24, 2015 07:56
-
-
Save kosssi/00b8054e42943ef4585d to your computer and use it in GitHub Desktop.
This file contains 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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Il faut avoir une base de données couch nommé 'test', puis tu peux executer la commande suivante.
Voici le resultat.