你是对的,PouchDB 同步不会真正做你想做的事。它旨在使用服务器端设计文档同步整个数据库或数据库的预定义子集。
如果我是你,我可能仍然会使用 PouchDB,但我会手动处理同步。像这样的东西:
var localDB = new PouchDB('localDB');
var remoteDB = new PouchDB('http://some-site.com:5984/somedb');
function searchForDocs(docId) {
// try the local DB first
localDB.get(docId).catch(function (err) {
if (err.name !== 'not_found') {
throw error;
}
// not found, so fall back to the remote DB
return remoteDB.get(docId).then(function (doc) {
// cache in the local DB
delete doc._rev;
return localDB.put(doc).then(function () {
return doc;
});
});
}).then(function (doc) {
// do something with our doc
}).catch(function (err) {
// handle any errors along the way
});
}
这里的使用get()
有点简单;在您的 Wikipedia 案例中,您可能想要allDocs({startkey: query, endkey: query + '\uffff'})
查找 ID 以查询开头的所有文档。或者您可以使用二级索引。
因此,尽管您不会获得 PouchDB 内置同步的好处,但您会获得能够针对服务器编写与客户端相同的代码以及 PouchDB 的跨浏览器支持的好处。所以我不认为这是一个糟糕的方法。