   dbUrl = $location.absUrl().split('#')[0]
   dbUrl += 'lupo-proto/'
   remoteDb = new PouchDB(dbUrl)
-  localDb = new PouchDB('lupo-proto')
+  #localDb = new PouchDB('lupo-proto')
   #TMP
-  PouchDB.replicate(localDb, remoteDb)
-  #.on('change', (e) -> console.log(e))
-  #.on('complete', (e) -> console.log(e))
-  #.on('error', (e) -> console.error(e))
-
+  #PouchDB.replicate(localDb, remoteDb, {live: true})
+  #.on('change', (e) -> console.log("upload", e))
+  #.on('complete', (e) -> console.log("upload", e))
+  #.on('error', (e) -> console.error("upload", e))
+  syncIds = {list: []}
+  ###
+  localDb.allDocs().then (list) =>
+    syncIds.list = (row.id for row in list.rows)
+    syncIds.list.push "proto"
+    PouchDB.replicate(localDb, remoteDb,
+      live: true
+    )
+    .on 'change', (change) =>
+      #syncIds.list.push result.id
+      console.log "local change", change
+    .on('complete', (e) -> console.log("complete", e))
+    .on('uptodate', (e) -> console.log("uptodate", e))
+    .on('error', (e) -> console.error("error", e))
+    remoteDb.change(
+      live: true
+    ).on 'change', (change) =>
+      console.log "remote change", change
+      if syncIds.indexOf(change.id) > -1
+        console.log "remote change!!"
+  ###
   _indent = "  "
   {
+    purge: (db) ->
+      if not db?
+        @purge(localDb).then =>
+          @purge(remoteDb)
+      else
+        db.allDocs().then (list) =>
+          for row in list.rows
+            db.remove({_id: row.id, _rev: row.value.rev})
+            .then =>
+              console.log "remove", row.id
+            .catch =>
+              console.error "error removing", row.id
+
     get: (_id) ->
       _funcName = _indent + "storage.get"
       console.log _funcName, _id
       deferred = $q.defer()
-      otherOneFailed = false
       remoteDb.get _id, (err, result) =>
         if err?
-          console.error "remote", err
-          if otherOneFailed
-            deferred.reject err
-          otherOneFailed = true
-        else
-          deferred.resolve result
-      localDb.get _id, (err, result) =>
-        if err?
-          console.error "local", err
-          if otherOneFailed
-            deferred.reject err
-          otherOneFailed = true
+          console.error "NOT FOUND", _id, err
+          deferred.reject err
         else
           deferred.resolve result
       return deferred.promise
 
-    _save: (doc, remoteAndLocal) ->
+    _save: (doc) ->
       deferred = $q.defer()
       if doc._id?
         method = "put"

    saveLocal: (doc) ->
      _funcName = _indent + "storage.saveLocal"
      console.log _funcName
      @_save doc, true

    saveRemoteOnly: (doc) ->
      _funcName = _indent + "storage.saveRemoteOnly"
      console.log _funcName
      @_save doc, false

    save: (doc) ->
      @_save(doc, true)

    del: (doc) ->
      deletedDoc = {_id: doc._id, _rev: doc._rev, _deleted: true}
      @saveLocal(deletedDoc).catch =>
        @saveRemoteOnly(deletedDoc)

    queryRemote: (fun, options) ->
      deferred = $q.defer()

        delete doc._id
        delete doc._rev
      console.log method, doc
      remoteDb[method](doc)
      .then (result) =>
        deferred.resolve(result)
      .catch (err) =>
