Consider Using nanoid posted Thursday, January 9, 2025 by The Neighbourhoodie Team
CouchDB automatically assigns each piece of your data an unmistakable tag, called a UUID, to avoid conflicts in distributed systems even before syncing. If you assign these yourself, nanoid for JavaScript is the smaller, faster tool to get it done with CouchDB and PouchDB.
CouchDB identifies documents by their _id
field. You can provide your own values, or if you rather not, CouchDB will automatically assign a value for you. By default, CouchDB uses a method called sequential UUIDs. For about every 1000 UUIDs, it will use an incrementing prefix before prepending a random value to ensure conflict-free assigning of values without coordination.
In case you are assigning random _id
s yourself, check out nanoid, it can generate values faster than the regular UUID algorithm, while keeping the same uniqueness guarantees. You are, however, giving up the performance enhancing property of the incrementing UUIDs that CouchDB produces.
One note: since nanoids can be generated to start with an underscore, and since PouchDB and CouchDB do not permit that, you have to prefix any generated id or remove the underscore character from nanoid's pool. See the nanoid Readme for details.
« Back to the blog post overview