我正在开发一个应用程序.在那个应用程序中,我使用了couchbase Lite移动版.我从couchbase服务器同步数据库中的所有文档.但问题是数据库很大.我不想同步来自couchbase服务器的所有文件.我想
我的问题是,如何同步与该特定用户相关的特定文档?
特定用户的文档访问权限在同步功能中完成.它是用 JavaScript编写的函数,驻留在Sync Gateway的配置文件中.同步功能中可用的方法是:
> channel(channelname):将文档路由到通道.
> access(用户名,channelname):授予用户对频道的访问权限(也可以将角色授予频道,因此具有该角色的所有用户都可以访问该频道).
> role(username,rolename):为具有角色的用户分配.
> requireAccess(channelname):如果上下文中的用户尚未访问该频道,则会引发错误.
> requireUser(username):如果上下文中的用户不是用户名,则会引发错误.
> requireRole(rolename):如果上下文中的用户没有角色rolename,则抛出错误.
> throw({forbidden:“error message”}):抛出自定义验证的异常.
以下是带有内联注释的配置文件示例:
{
"log": ["REST", "CRUD"],
"users": {
"foo1": {"password": "letmein", "admin_roles": ["admin"]},
"foo2": {"password": "letmein"}
},
"databases": {
"quizz": {
"sync": `function(doc, oldDoc) {
// The owner field shouldn't change during updates
if (doc.owner != oldDoc.owner) {
throw({forbidden: "Can't change the owner field on existing documents"});
}
switch(doc.type) {
case "list":
// only users with admin role can create/update list documents
requireRole("admin");
break;
case "todo":
// only the owner of a todo document can create/update it
require(doc.owner);
break;
}
}`
}
}
}
注意:同步功能应该是纯的,这意味着给定输入(文档修订),输出应该保持不变,无论时间如何(例如,您不能发出数据库/ http请求).此外,无法修改同步功能中的修订版本.
有关更多详细信息,请参阅同步功能上的docs;有关更深入的示例,请参阅this tutorial.
