开发者

How to generate CouchDB UUID with Node.js?

开发者 https://www.devze.com 2023-02-10 08:32 出处:网络
Is there a way to generate random UUID li开发者_StackOverflow社区ke the ones used in CouchDB but with Node.js?There are different ways to generate UUIDs.If you are already using CouchDB, you can just

Is there a way to generate random UUID li开发者_StackOverflow社区ke the ones used in CouchDB but with Node.js?


There are different ways to generate UUIDs. If you are already using CouchDB, you can just ask CouchDB for some like this:

http://127.0.0.1:5984/_uuids?count=10  

CouchDB has three different UUID generation algorithms. You can specify which one CouchDB uses in the CouchDB configuration as uuids/algorithm. There could be benefits to asking CouchDB for UUIDs. Specifically, if you are using the "sequence" generation algorithm. The UUIDs you get from CouchDB will fall into that sequence.

If you want to do it in node.js without relying on CouchDB, then you'll need a UUID function written JavaScript. node-uuid is a JavaScript implementation that uses "Version 4" (random numbers) or "Version 1" (timestamp-based). It works with node.js or hosted in a browser: https://github.com/broofa/node-uuid

If you're on Linux, there is also a JavaScript wrapper for libuuid. It is called uuidjs. There is a performance comparison to node-uuid in the ReadMe of node-uuid.

If you want to do something, and it doesn't look like it's supported in node.js, be sure to check the modules available for npm.


I had the same question and found that simply passing a 'null' for the couchdb id in the insert statement also did the trick:

var newdoc = { "foo":"bar", "type": "my_couch_doctype" };

mycouchdb.insert(newdoc, null /* <- let couchdb generate for you. */, function(err, body){

});

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号