PouchDB 删除设备上的数据而不影响远程同步
PouchDB delete data on device without affecting remote sync
现在我正在将我的整个设备数据库复制到我的远程数据库。
完成后,我使用过滤器从我的远程数据库中抓取所有不超过 1 个月的数据,并将其带到我的设备中。
过滤器
{
_id: '_design/filters',
"filters": {
"device": function(doc, req) {
if(doc.type == "document" || doc.type == "signature") {
if(doc.created >= req.query.date) return true;
else return false;
}
else return true;
}
}
}
复制
device_db.replicate.to(remote_db)
.on('complete', function () {
device_db.replicate.from(remote_db, {
filter: "filters/device",
query_params: { "date": (Math.floor(Date.now() / 1000)-2419200) }
})
.on('complete', function () {
console.log("localtoRemoteSync replicate.to success");
callback(true);
});
});
我的问题:
我希望能够定期从我的设备中删除超过 3 个月的数据(足够旧的数据,我已经知道它已经同步)
但仅仅因为我从我的设备中删除了它,当我将数据复制回我的 remote_db 时,我不希望它在那里被删除也是。
如何删除我设备上的特定数据,但在复制时不翻译该删除?
过滤器
在这里,我们有 2 个过滤器:
noDeleted:此过滤器不推送 _deleted 个文档。
设备:过滤以仅获取最新数据。
{
_id: '_design/filters',
"filters": {
"device": function(doc, req) {
if (doc.type == "document" || doc.type == "signature") {
if (doc.created >= req.query.date) return true;
else return false;
}
return true;
},
"noDeleted": function(doc, req) {
//Document _deleted won't pass through this filter.
//If we delete the document locally, the delete won't be replicated to the remote DB
return !doc._deleted;
}
}
}
复制
device_db.replicate.to(remote_db, {
filter: "filters/noDeleted"
})
.on('complete', function() {
device_db.replicate.from(remote_db, {
filter: "filters/device",
query_params: { "date": (Math.floor(Date.now() / 1000) - 2419200) }
})
.on('complete', function() {
console.log("localtoRemoteSync replicate.to success");
callback(true);
});
});
工作流程
- 您推送了所有文档,而没有推送已删除的文档。
- 您获得了最新数据的所有更新
- 您删除了旧文档
- 您可以查询远程数据库以获取太旧的文档的 ID,然后在本地将其删除。请注意,文档仍将以 _deleted 的形式存在。要完全删除它们,需要进行压缩。
- 您也可以在第 1 步之后完全销毁本地数据库并从头开始。
- 回调(真);
现在我正在将我的整个设备数据库复制到我的远程数据库。
完成后,我使用过滤器从我的远程数据库中抓取所有不超过 1 个月的数据,并将其带到我的设备中。
过滤器
{
_id: '_design/filters',
"filters": {
"device": function(doc, req) {
if(doc.type == "document" || doc.type == "signature") {
if(doc.created >= req.query.date) return true;
else return false;
}
else return true;
}
}
}
复制
device_db.replicate.to(remote_db)
.on('complete', function () {
device_db.replicate.from(remote_db, {
filter: "filters/device",
query_params: { "date": (Math.floor(Date.now() / 1000)-2419200) }
})
.on('complete', function () {
console.log("localtoRemoteSync replicate.to success");
callback(true);
});
});
我的问题:
我希望能够定期从我的设备中删除超过 3 个月的数据(足够旧的数据,我已经知道它已经同步)
但仅仅因为我从我的设备中删除了它,当我将数据复制回我的 remote_db 时,我不希望它在那里被删除也是。
如何删除我设备上的特定数据,但在复制时不翻译该删除?
过滤器
在这里,我们有 2 个过滤器:
noDeleted:此过滤器不推送 _deleted 个文档。
设备:过滤以仅获取最新数据。
{
_id: '_design/filters',
"filters": {
"device": function(doc, req) {
if (doc.type == "document" || doc.type == "signature") {
if (doc.created >= req.query.date) return true;
else return false;
}
return true;
},
"noDeleted": function(doc, req) {
//Document _deleted won't pass through this filter.
//If we delete the document locally, the delete won't be replicated to the remote DB
return !doc._deleted;
}
}
}
复制
device_db.replicate.to(remote_db, {
filter: "filters/noDeleted"
})
.on('complete', function() {
device_db.replicate.from(remote_db, {
filter: "filters/device",
query_params: { "date": (Math.floor(Date.now() / 1000) - 2419200) }
})
.on('complete', function() {
console.log("localtoRemoteSync replicate.to success");
callback(true);
});
});
工作流程
- 您推送了所有文档,而没有推送已删除的文档。
- 您获得了最新数据的所有更新
- 您删除了旧文档
- 您可以查询远程数据库以获取太旧的文档的 ID,然后在本地将其删除。请注意,文档仍将以 _deleted 的形式存在。要完全删除它们,需要进行压缩。
- 您也可以在第 1 步之后完全销毁本地数据库并从头开始。
- 回调(真);