Nanoid

Unique String Generator

Nanoid as its name implies is a tiny, secure, URL-friendly, unique string ID generator for JavaScript.

265

Some of the salient features of using Nanoid are:

  • Small: 130 bytes (minified and gzipped). No dependencies. Size Limit controls the size.
  • Fast: It is two times faster than UUID.
  • Safe: It uses a random hardware generator. It can be used in clusters.
  • Short IDs: It uses a larger alphabet than UUID (A-Za-z0-9_-). So ID size was reduced from 36 to 21 symbols.

📘

References

For more details over Nanoid, please visit the official documentation at https://github.com/ai/nanoid

Using Nanoid

Nano ID is a library for generating random IDs. Likewise, with UUID, there is a probability of duplicate IDs. However, this probability is extremely small. Meanwhile, a lot of projects generate IDs in small numbers. For those projects, the ID length could be reduced without risk.

All POST methods within Samdock Microservices POST methods require Nanoid JS generated ids. Here is how it can be generated.

import { nanoid } from 'nanoid'
model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"

If you want to reduce the ID size (and increase collisions probability), you can pass the size as an argument. This is however not recommended due to the increased collision probability.

nanoid(10) //=> "IRFa-VaY2b"