大型数据库的 Pouchdb 过滤复制
Pouchdb filtered replication for large database
下面是我编写的一个服务,用于尝试将远程 couchdb 位置的数据同步到设备。但是,我遇到的最大问题是只想检索具有 type: document
的数据(如果它在时间戳期间内)。
现在,我很确定它会抓取每个文档,然后将其过滤掉。
我想在它通过之前先过滤掉它,因为我有很多文档。
有谁知道我是怎么做到的?
.service("$pouchDB", ["$rootScope", "$q", "$cordovaNetwork", "$state", function($rootScope, $q, $cordovaNetwork, $state) {
var self = this;
self.db = new PouchDB("IDHERE", {auto_compaction: true});
self.remoteToDevice = function(s) {
var remote_db = new PouchDB('https://SOMETHING@URL/IDHERE', {ajax: {timeout: 180000}});
return self.db.replicate.from(remote_db, {
filter: function (doc) {
if(doc.type == 'document')
if(doc.timestamp >= (Math.floor(Date.now() / 1000)-2419200))
return doc;
else return doc;
}
}).on('complete', function () {
console.log("Remote to Device - Success");
})
.on('error', function (err) {
console.log("Remote to Device - Error", JSON.stringify(err));
return err;
});
}
}
编辑:
感谢 Alexis,这是我认为可行的解决方案
新增remoteToDevice
过滤功能
{
filter: "filters/device",
query_params: {
"timestamp": (Math.floor(Date.now() / 1000)-2419200)
}
}
couchdb 中的过滤函数
"filters": {
"device": "function(doc, req) {
if(doc.type == \"document\") {
if(doc.timestamp >= req.query.timestamp) return true;
else return false;
}
else return true;
}"
}
您应该在 CouchDB 的设计文档中定义一个过滤器函数。
复制时,您需要指定过滤器名称。
下面是我编写的一个服务,用于尝试将远程 couchdb 位置的数据同步到设备。但是,我遇到的最大问题是只想检索具有 type: document
的数据(如果它在时间戳期间内)。
现在,我很确定它会抓取每个文档,然后将其过滤掉。
我想在它通过之前先过滤掉它,因为我有很多文档。
有谁知道我是怎么做到的?
.service("$pouchDB", ["$rootScope", "$q", "$cordovaNetwork", "$state", function($rootScope, $q, $cordovaNetwork, $state) {
var self = this;
self.db = new PouchDB("IDHERE", {auto_compaction: true});
self.remoteToDevice = function(s) {
var remote_db = new PouchDB('https://SOMETHING@URL/IDHERE', {ajax: {timeout: 180000}});
return self.db.replicate.from(remote_db, {
filter: function (doc) {
if(doc.type == 'document')
if(doc.timestamp >= (Math.floor(Date.now() / 1000)-2419200))
return doc;
else return doc;
}
}).on('complete', function () {
console.log("Remote to Device - Success");
})
.on('error', function (err) {
console.log("Remote to Device - Error", JSON.stringify(err));
return err;
});
}
}
编辑:
感谢 Alexis,这是我认为可行的解决方案
新增remoteToDevice
过滤功能
{
filter: "filters/device",
query_params: {
"timestamp": (Math.floor(Date.now() / 1000)-2419200)
}
}
couchdb 中的过滤函数
"filters": {
"device": "function(doc, req) {
if(doc.type == \"document\") {
if(doc.timestamp >= req.query.timestamp) return true;
else return false;
}
else return true;
}"
}
您应该在 CouchDB 的设计文档中定义一个过滤器函数。
复制时,您需要指定过滤器名称。