最近看了下couchbase以及同步模型sync-gateway,相关内容做下记录。http://developer.couchbase.com/
couchbase服务端提供了couchbase server,客户端提供couchbase lite,使用sync-gateway进行数据的同步
官网的sample里提供了三个样例可以使用,分别提供最简单的客户单db读写、简单的同步操作、带有facebook登陆验证等的复杂操作
关于sync-gateway的配置,默认使用walrus作为数据库,可以将其修改为couchbase server进行数据存储操作,如下面的配置
{
"interface": ":4984",
"adminInterface": ":4985",
"log": ["REST"],
"databases": {
"grocery-sync": {
"server": "http://127.0.0.1:8091",
"bucket": "default",
"users": {
"GUEST": {
"disabled": false,
"all_channels": ["b"]
}
},
"sync": `
function(doc) {
channel("a");
}`
}
}
}
客户端所做的更改会通过sync-gateway同步到server端数据库,并且在端之间进行同步,如果server挂掉,端上做更改后回保存版本信息,等server回复后会在各个端之间进行同步数据
{
"log": ["CRUD", "REST+", "Access"],
"facebook": {
"register": true
},
"databases": {
"todos": {
"server": "walrus:",
"users": {
"GUEST": {
"disabled": true
}
},
"sync": `
function(doc, oldDoc) {
// NOTE this function is the same across the iOS, Android, and PhoneGap versions.
if (doc.type == "task") {
if (!doc.list_id) {
throw ({
forbidden: "Items must have a list_id"
})
}
channel("list-" + doc.list_id);
} else if (doc.type == "list") {
channel("list-" + doc._id);
if (!doc.owner) {
throw ({
forbidden: "List must have an owner"
})
}
if (oldDoc) {
var oldOwnerName = oldDoc.owner.substring(oldDoc.owner.indexOf(":") + 1);
requireUser(oldOwnerName)
}
var ownerName = doc.owner.substring(doc.owner.indexOf(":") + 1);
access(ownerName, "list-" + doc._id);
if (Array.isArray(doc.members)) {
var memberNames = [];
for (var i = doc.members.length - 1; i >= 0; i--) {
memberNames.push(doc.members[i].substring(doc.members[i].indexOf(":") + 1))
};
access(memberNames, "list-" + doc._id);
}
} else if (doc.type == "profile") {
channel("profiles");
var user = doc._id.substring(doc._id.indexOf(":") + 1);
if (user !== doc.user_id) {
throw ({
forbidden: "Profile user_id must match docid : " + user + " : " + doc.user_id
})
}
requireUser(user);
access(user, "profiles"); // TODO this should use roles
}
}`
}
}
}
对channel等权限的改变也可以通过在服务端调用REST-API来进行操作