gpt4 book ai didi

swift - 如何从 couchbase 服务器同步特定文档?

转载 作者:搜寻专家 更新时间:2023-10-31 19:30:30 24 4
gpt4 key购买 nike

我正在开发一个应用程序。在那个应用程序中,我使用了 couchbase Lite for mobile。我从 couchbase 服务器同步了数据库中的所有文档。但问题是数据库很大。我不想从 couchbase 服务器同步所有文档。我只想从服务器同步特定数据/文档。

我的问题是,如何同步与特定用户相关的特定文档?

最佳答案

特定用户的文档访问是在同步功能中完成的。它是一个用 JavaScript 编写的函数,驻留在 Sync Gateway 的配置文件中。

Sync Function 中可用的方法有:

  • channel(channelname):将文档路由到 channel 。
  • access(username, channelname):授予用户名访问 channel 的权限(也可以向 channel 授予角色,因此所有具有该角色的用户都可以访问该 channel ) .
  • role(username, rolename):为用户分配一个角色。
  • requireAccess(channelname):如果上下文中的用户还没有访问 channel 的权限,则抛出错误。
  • 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以获得更深入的示例。

关于swift - 如何从 couchbase 服务器同步特定文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34268960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com