假设两个用户在脱机时对同一文档进行更改,但更改在文档的不同部分。如果用户2在用户1之后重新联机,用户1所做的更改会丢失吗?
在我的数据库中,每一行都包含一个JS对象,该对象的一个属性是数组。此数组绑定到接口上的一系列复选框。我希望的是,如果两个用户对这些复选框进行了更改,则会根据进行更改的时间而不是同步发生的时间,为每个复选框单独保留最新的更改。GroundDB是实现这一目标的合适工具吗?有没有办法添加一个事件处理程序,在其中我可以添加一些在同步发生时会触发的逻辑,并负责合并?
简短的回答是"是",因为逻辑是自定义的,这取决于冲突解决的行为,例如,如果您想自动化或让用户参与进来。
旧的Ground DB只是依赖Meteor的冲突解决方案(服务器的最新数据获胜)。我猜你可以看到一些问题,这取决于哪个客户端何时上线。
Ground db II没有恢复方法,它或多或少只是离线缓存数据的一种方式。它在一个可观测的源上进行观测。
我想您可以为GDB II创建一个中间件观测器,它在进行更新之前检查本地数据,并更新客户端或/和调用服务器来更新服务器数据。这样你就有办法处理冲突了。
我想我记得写过一些代码,支持"deletedAt"/"updatedAt"进行某些类型的冲突处理,但冲突处理程序在大多数情况下都应该是自定义的。(为可重复使用的冲突处理程序打开大门可能很有用)
特别是如果你不通过使用"deletedAt"实体之类的方式进行"软"删除,那么知道数据何时被删除可能会很棘手。
"rc"分支目前是grounddb-caching-2016版本"2.0.0-rc.4",
我在想这样的事情:(注意,它没有经过测试,直接用SO编写)
// Create the grounded collection
foo = new Ground.Collection('test');
// Make it observe a source (it's aware of createdAt/updatedAt and
// removedAt entities)
foo.observeSource(bar.find());
bar.find()
返回一个带有函数observe
的游标,我们的中间件也应该这样做。让我们为它创建一个createMiddleWare
助手:
function createMiddleWare(source, middleware) {
const cursor = (typeof (source||{}).observe === 'function') ? source : source.find();
return {
observe: function(observerHandle) {
const sourceObserverHandle = cursor.observe({
added: doc => {
middleware.added.call(observerHandle, doc);
},
updated: (doc, oldDoc) => {
middleware.updated.call(observerHandle, doc, oldDoc);
},
removed: doc => {
middleware.removed.call(observerHandle, doc);
},
});
// Return stop handle
return sourceObserverHandle;
}
};
}
用法:
foo = new Ground.Collection('test');
foo.observeSource(createMiddleware(bar.find(), {
added: function(doc) {
// just pass it through
this.added(doc);
},
updated: function(doc, oldDoc) {
const fooDoc = foo.findOne(doc._id);
// Example of a simple conflict handler:
if (fooDoc && doc.updatedAt < fooDoc.updatedAt) {
// Seems like the foo doc is newer? lets update the server...
// (we'll just use the regular bar, since thats the meteor
// collection and foo is the grounded data
bar.update(doc._id, fooDoc);
} else {
// pass through
this.updated(doc, oldDoc);
}
},
removed: function(doc) {
// again just pass through for now
this.removed(doc);
}
}));