简体   繁体   中英

PouchDB to CouchDB sync saves filters as views

I have simple PouchDB instances one for local browser and one remote. I have a design doc that takes a filter, when I issue a sync it saves as a view.

var localDB = new PouchDB('mydb');
var remoteDB = new PouchDB('http://anna:secret@127.0.0.1:5984/db');

When I execute the below it saves as a view

localDB.sync(remoteDB)

{
    "_id": "_design/sync",
    "_rev": "2-f6db221d90157a99f4e6e9e6e27ffe85",
    "views": {
        "by_user": {
            "map": "function (doc) {\n  emit(doc._id, 1);\n}"
        }
    }
}

when it is written as below on my client script

localDB.put({
        _id : '_design/sync',
        filters: {
            by_user : function(doc, req) {
                return doc._id.indexOf(req.query.user) > 0;
            }.toString()
        }
    }).then().catch(function(err) {});

So, why can't I see filters in my document when viewed in CouchDB ?

You are saving two different docs, one with a view (in CouchDB) and one with a filter (in PouchDB).

Rule of thumb, if you store a doc with the same id but different content, you will get conflicts when you sync (replicate). If you have conflict with a design document, then basically one revision (chosen pretty much arbitrarily) will be the "live" one and one will be inert, sort of like an old Git branch that nobody cares about anymore.

In other words (if I understand you correctly), you should probably try to store an identical doc in CouchDB and PouchDB. (An easy way to do this is to store the doc on PouchDB and then replicate that to CouchDB, or vice versa.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM