CouchDb - 在视图中访问复制过滤器

发布于 2024-11-29 18:40:01 字数 215 浏览 0 评论 0原文

是否可以通过请求视图来使用 couchdb (http://wiki.apache.org/couchdb/Replication#Filtered_Replication) 的复制过滤器功能,fe:

.../_view/candidates?filter=hrtool/myfilter

这将最好根据用户会话或用户角色过滤文档,

提前致谢

fadh

is it possible to use the replication filter feature of couchdb (http://wiki.apache.org/couchdb/Replication#Filtered_Replication) by requesting a view, f.e.:

.../_view/candidates?filter=hrtool/myfilter

This would be nice to filter the documents based on the usersession or userrole

Thanks in advance

fadh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东京女 2024-12-06 18:40:01

这可以通过 _list 函数实现。

列表函数是 Javascript 代码,它在将视图输出发送到客户端之前对其进行预处理。您可以以任何方式修改视图输出,例如通过过滤某些行。

function(head, req) {
  // lists.filtered: filter view output by using a replication filter.

  var ddoc = this; // A common trick to explicitly identify the design document.
  function error(reason) {
    start({"code":400, "headers":{"content-type":"application/json"}});
    send(JSON.stringify({"error":reason}));
  }

  var filter_name = req.query.filter;
  if(!filter_name)
    return error("Need filter_name parameter");

  var filter_src = ddoc.filters[filter_name];
  if(!filter_src)
    return error("Invalid filter_name: " + filter_name);

  // Not 100% sure on this, you could also use new Function(args, src);
  // In the worst-case, the couchapp tool has the !code tool to copy code.
  var filter = eval(filter_src); // Not 100% sure on this
  var row;

  start({"headers":{"content-type":"application/json"}});
  send('{"rows":[\r\n');

  var first = true;
  while(row = getRow()) {
    if(filter(row)) { // Or perhaps use include_docs=true and filter(row.doc)
      if(! first)
        send(",\r\n");
      first = false;
      send(JSON.stringify(row));
    }
  }
  send("]}\r\n");
}

像任何过滤器功能一样使用此列表“过滤器”:

GET /db/_design/example/_list/filtered/candidates?filter=myfilter&include_docs=true

That is possible with a _list function.

List functions are Javascript code which pre-process the view output before sending it to the client. You can modify the view output in any way, such as by filtering some rows.

function(head, req) {
  // lists.filtered: filter view output by using a replication filter.

  var ddoc = this; // A common trick to explicitly identify the design document.
  function error(reason) {
    start({"code":400, "headers":{"content-type":"application/json"}});
    send(JSON.stringify({"error":reason}));
  }

  var filter_name = req.query.filter;
  if(!filter_name)
    return error("Need filter_name parameter");

  var filter_src = ddoc.filters[filter_name];
  if(!filter_src)
    return error("Invalid filter_name: " + filter_name);

  // Not 100% sure on this, you could also use new Function(args, src);
  // In the worst-case, the couchapp tool has the !code tool to copy code.
  var filter = eval(filter_src); // Not 100% sure on this
  var row;

  start({"headers":{"content-type":"application/json"}});
  send('{"rows":[\r\n');

  var first = true;
  while(row = getRow()) {
    if(filter(row)) { // Or perhaps use include_docs=true and filter(row.doc)
      if(! first)
        send(",\r\n");
      first = false;
      send(JSON.stringify(row));
    }
  }
  send("]}\r\n");
}

Use this list "filter" like any filter function:

GET /db/_design/example/_list/filtered/candidates?filter=myfilter&include_docs=true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文