使用过滤复制同步 pouchdb

Jaf*_*ick 2 couchdb pouchdb reactjs

我使用 pouchdb 和 couchDb 作为我的离线第一个带有 react 的移动应用程序的数据库。

https://pouchdb.com/2015/04/05/filtered-replication.html

基于以上,就像我配置了 pouchdb 并与 couchdb 同步。我正在做基于用户的过滤。当用户注销并再次登录时,db 值可用。做到这一点的最佳设计方法是什么?

有什么例子可以参考吗?

configurePouchdb(user) {
var db = new PouchDB('dbname', {adapter: 'websql'});

 var serverSideFilter = {
        _id: "_design/app",
        filters: {
            "by_user": function (doc, req) {
                return doc._id === '_design/app' || (doc.userId != undefined && doc.userId === req.query.userId);
            }.toString()
        }
    };
    db.put(serverSideFilter).then(function (doc) {
        // design doc created!
    }).catch(function (err) {
        // if err.name === 'conflict', then
        // design doc already exists
    });
  db.sync('http://127.0.0.1:5984/dbname', {
        live: true,
        retry: true,
        filter: 'app/by_user',
        query_params: {"userId": user}
    });
    return db;
}
Run Code Online (Sandbox Code Playgroud)

ale*_*aui 5

据我所知,CouchDB 或 PouchDB 中的视图都没有 req 参数,因为视图只生成一个不会因用户而异的索引。

https://pouchdb.com/2015/04/05/filtered-replication.html 中所述,不建议使用过滤复制而不是正确的身份验证。PouchDB 的一位主要提交者在https://github.com/nolanlawson/pouchdb-authentication#couchdb-authentication-recipe 中有一个很好的配方

无论如何,PouchDB API 文档中有一个非常好的过滤复制指南:https ://pouchdb.com/api.html#filtered-replication 。