CouchDB - 在创建或更新文档时触发代码 [英] CouchDB - trigger code when creating or updating document

查看:138
本文介绍了CouchDB - 在创建或更新文档时触发代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在CouchDB中存储数据的页面。该页面通过javascript直接访问数据库,因此浏览器中隐藏的逻辑不多。在创建新文档时,有一些逻辑可以将数据元素提取到单独的字段中,以便可以搜索它们。

I have a page which store data in CouchDB. The page accesses the database directly via javascript, so not much of the logic is hidden from the browser. When creating a new document there is some logic which extracts elements of the data into separate fields so that they can be searched on.

是否可以在服务器在创建或更新文档时,还是在遇到数据库之前我是坚持做的?

Is it possible to do this logic on the server when creating or updating the documents, or am I stuck doing it before hitting the database?

推荐答案

你有几个选择。

首先,请看这个关于 CouchDB更新功能。更新函数接收来自浏览器的请求,并且可以在最终将它们存储在CouchDB中之前以任何方式修改它们。例如,有些人使用它们来自动添加时间戳。另请参阅CouchDB上的wiki页面文档更新处理程序

First, see this question about CouchDB update functions. Update functions receive a request from the browser and can modify them in any way before finally storing them in CouchDB. For example, some people use them to automatically add a timestamp. Also see the wiki page on CouchDB document update handlers.

另一种选择是接收 CouchDB更改通知。在这种情况下,一个单独的程序(您自己的浏览器,甚至更好的,您运行的独立程序)可以在CouchDB中查询 _changes 。在保存文档后,CouchDB将通知此程序。接下来,程序可以获取文档,然后存储任何必要的新修订。

Another option is to receive CouchDB change notifications. In this case, a separate program (either your own browser, or even better, a standalone program that you run) can query CouchDB for _changes. CouchDB will notify this program after the document is saved. Next, the program can fetch the document and then store any new revisions that are necessary.

对我来说,听起来你应该尝试 _update 首先发挥作用。

To me, it sounds like you should try the _update function first.

这篇关于CouchDB - 在创建或更新文档时触发代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆