Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequential unique IDs for PouchDB

Tags:

pouchdb

PouchDB best-practice recommendation is to use PUT instead of POST for creating a new document (analogous to a row in an rdbms) primarily because the latter generates a random ID that makes it inefficient for subsequent sorting of the data. PUT, on the other hand, requires providing a user-generated, unique ID.

I am a bit puzzled that PouchDB doesn't seem to provide this functionality out-of-the-box. So, what is the best way to generate a unique, sequential ID (analogous to PostgreSQL's sequence)? I could use something analogous to maxID, but the main issue, in my view, is to ensure no one else inserts a record between when I determine the maxID and when I actually succeed in inserting a record.

Suggestions?

like image 686
punkish Avatar asked Dec 06 '16 08:12

punkish


1 Answers

While I don't have the complete answer, a suggestion would be to try something like Twitter's Snowflake ID. If there is a JavaScript implementation, this might be an option.

A simpler version would be to simply use

var id = (new Date()).getTime();

which returns the number of milliseconds since 1 January 1970 00:00:00 UTC.

Edit: the following article suggests a couple of Node packages:

  • uuid
  • flake-idgen
like image 184
Alen Siljak Avatar answered Jan 04 '23 06:01

Alen Siljak