Skip to content

Instantly share code, notes, and snippets.

@jmbarbier
Created May 17, 2014 22:49
Show Gist options
  • Save jmbarbier/bfdc279f49036637dfce to your computer and use it in GitHub Desktop.
Save jmbarbier/bfdc279f49036637dfce to your computer and use it in GitHub Desktop.
@data = new PouchDB(@id)
# Code for 2-way sync, à mettre après la synchro des docs de design pour éviter
# une "course" potentielle.
# <code>
# @data = new PouchDB(@access)
# @cursync = @data.sync(@access).on("complete", () =>
# console.log "Replication OK, switching dbs", @id, @access
# @cursync.cancel()
# @data = new PouchDB(@id)
# @cursync = @data.sync(@access, {live: true}).on("change", (data) ->
# console.log "Something changed somewhere", data, data.change
# )
# )
# </code>
_designdocs = [
{
_id: "_design/labitems",
language: "javascript"
version: "4"
views:
tags:
map: ((doc) ->
if doc.type == "labitem"
for tag in doc.categs
emit(tag, 1)
).toString()
reduce: ((tag, values) ->
count = 0
for val in values
count += val
return count
).toString()
bytag:
map: ((doc) ->
if doc.type == "labitem"
for tag in doc.categs
tags = tag.split(":")
emit(tags, doc)
).toString()
}
]
# Si les documents de _design ne sont pas sauvés et à la
# dernière version, il faut les sauver ou les updater.
update = (data, ddoc) ->
data.get(ddoc._id).then (doc) ->
console.log "Got", doc
if doc.version != ddoc.version
console.log "Putting ", ddoc, doc._rev
return data.put(ddoc, doc._rev)
, (err) ->
console.log "Saving ", ddoc
return data.put(ddoc)
ds = []
for ddoc in _designdocs
ds.push(update(@data, ddoc))
return $q.all(ds).then () =>
return @
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment