Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CouchDB - trigger code when creating or updating document

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?

like image 849
Nippysaurus Avatar asked Sep 26 '11 00:09

Nippysaurus


1 Answers

You have a couple of options.

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.

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.

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

like image 151
JasonSmith Avatar answered Sep 29 '22 17:09

JasonSmith