简体   繁体   English

如何使用Node.js生成CouchDB UUID?

[英]How to generate CouchDB UUID with Node.js?

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

There are different ways to generate UUIDs . 不同的方法来生成UUID If you are already using CouchDB, you can just ask CouchDB for some like this: 如果您已经在使用CouchDB,您可以向CouchDB询问以下内容:

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

CouchDB has three different UUID generation algorithms . CouchDB有三种不同的UUID生成算法 You can specify which one CouchDB uses in the CouchDB configuration as uuids/algorithm. 您可以指定CouchDB在CouchDB配置中使用哪个作为uuids / algorithm。 There could be benefits to asking CouchDB for UUIDs. 向CouchDB询问UUID可能会有好处。 Specifically, if you are using the "sequence" generation algorithm. 具体来说,如果您使用的是“序列”生成算法。 The UUIDs you get from CouchDB will fall into that sequence. 您从CouchDB获得的UUID将属于该序列。

If you want to do it in node.js without relying on CouchDB, then you'll need a UUID function written JavaScript. 如果你想在node.js中这样做而不依赖于CouchDB,那么你需要一个用JavaScript编写的UUID函数。 node-uuid is a JavaScript implementation that uses "Version 4" (random numbers) or "Version 1" (timestamp-based). node-uuid是一个JavaScript实现,它使用“Version 4”(随机数)或“Version 1”(基于时间戳)。 It works with node.js or hosted in a browser: https://github.com/broofa/node-uuid 它适用于node.js或托管在浏览器中: https//github.com/broofa/node-uuid

If you're on Linux, there is also a JavaScript wrapper for libuuid . 如果你在Linux上,还有一个libuuid的JavaScript包装器。 It is called uuidjs . 它被称为uuidjs There is a performance comparison to node-uuid in the ReadMe of node-uuid. 与node-uuid的自述文件中的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 . 如果你想做某件事,并且它看起来不像node.js那样,请务必检查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: 我有同样的问题,发现只是在insert语句中为couchdb id传递'null'也可以解决问题:

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

mycouchdb.insert(newdoc, null /* <- let couchdb generate for you. */, function(err, body){ mycouchdb.insert(newdoc,null / * < - 让couchdb为你生成。* /,function(err,body){

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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