10

I'm implementing a message application using CouchDB. I want to apply timestamps to each message. I found some references indicating that I should use document update handlers for this. In place updates seem like the right thing. But where would I get a timestamp from? Is it in the req object somewhere?

{
  updates: {
    "in-place" : function(doc, req) {
      doc.timestamp = "???";
      var message = "set timestamp to "+doc.timestamp;
      return [doc, message];
    }
  }
}
SorcyCat
  • 1,206
  • 10
  • 19
  • http://stackoverflow.com/questions/4812235/whats-the-best-way-to-store-datetimes-timestamps-in-couchdb – abernier Nov 11 '12 at 15:05

1 Answers1

7

The answer is to use javascript's date functions.

{
  updates: {
    "in-place" : function(doc, req) {
      doc.timestamp = new Date().getTime();
      var message = "set timestamp to "+doc.timestamp;
      return [doc, message];
    }
  }
}

Unfortunately, getting this update to trigger from jcouchdb is the next problem.

SorcyCat
  • 1,206
  • 10
  • 19
  • what do you mean "getting this update to trigger is the next problem": do you mean once the update-handler written, the problem is how to trigger it "automatically" as soon as the document is updated? – abernier Jun 15 '14 at 02:28