Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate CouchDB UUID with Node.js?

Is there a way to generate random UUID like the ones used in CouchDB but with Node.js?

like image 414
ajsie Avatar asked Feb 13 '11 03:02

ajsie


1 Answers

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.

like image 58
Lance Fisher Avatar answered Sep 28 '22 01:09

Lance Fisher