简体   繁体   中英

How to generate CouchDB UUID with Node.js?

有没有办法生成随机UUID,如CouchDB中使用但具有Node.js的UUID?

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){

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM