Generate unique ID in Node.js – UUID

In this tutorial, I have covered how to generate Unique ID (UUID) in Node.js

1). What is UUID & How it works
2). Generate UUID (using UUID V4 & V5)
3). Generate short UUID ( using Nano Id)

1. What is a UUID & How it works?

UUID is a 128-bit unique identifier that is used to identify data entities in computer systems. The unique nature of UUIDs is guaranteed by the fact that they are generated using a combination of time and space parameters, ensuring that no two UUIDs are the same.

The UUID is composed of 32 hexadecimal digits that are separated by hyphens into five groups. The first group contains eight digits, the second and third groups contain four digits each, the fourth group contains three digits, and the final group contains 12 digits.

UUIDs are generated using a combination of time and space parameters. The time parameter is based on the current time and the unique MAC address of the computer generating the UUID. The space parameter is a random number that is generated using a cryptographic algorithm.

The combination of these two parameters results in a unique identifier that is virtually impossible to replicate. UUIDs are commonly used in distributed systems to identify data entities across multiple computers.

2. Generate unique ID in Node.js

Node.js has a built-in module called uuid that can be used to generate UUID. To use this module, install it using NPM by running the following command in the terminal:

npm install uuid

Once the module is installed, UUID can be generated using the uuid.v4() method. This method generates a random UUID that is based on random numbers generated using the system clock and random number generator.

const uuid = require('uuid');

console.log(uuid.v4()); // Outputs something like: '46b9abfc-872a-4693-875a-f7fa8a5f1b58'

[demo title1=”node.js UUID demo” url1=”http://hayageek.com/examples/nodejs/generating-uuid/index.php?tab=v4″%5D

UUID also can be geenerate using the uuid.v5() method.

2.1 What is UUID V5 ?

UUID v5 is a type of Universally Unique Identifier (UUID) that is generated using a specific algorithm. Unlike UUID v4, which is generated using random numbers, UUID v5 is generated using a combination of a namespace and a name, using a hashing algorithm.

UUID v5 is also known as SHA-1 based UUID, as the hashing algorithm used to generate it is SHA-1. The namespace and name used to generate the UUID v5 ensure that it is unique, even if the same name is used in a different namespace.

UUID v5 is composed of 32 hexadecimal digits that are separated by hyphens into five groups. The first group contains eight digits, the second and third groups contain four digits each, the fourth group contains three digits, and the final group contains 12 digits.

UUID v5 is commonly used in applications that require a unique identifier for data entities, such as in distributed systems and databases. By using a combination of a namespace and a name, UUID v5 ensures that the generated UUID is unique and can be used to identify a specific data entity across different systems and databases.

const uuid = require('uuid');

//namespace needs to be uuid.
const namespace = uuid.parse('86044be7-fd2a-45d8-a091-988d63e74ab9')
const name = 'hayageek.com';

console.log(uuid.v5(name, namespace)); // Outputs something like: '60010313-b106-530f-8755-a9cc3e94c14a'

[demo title1=”node.js UUID V5 demo” url1=”http://hayageek.com/examples/nodejs/generating-uuid/index.php?tab=v5″%5D

3. Generate Short UUID using Nano ID

Nano ID is a small, secure, and URL-friendly unique string ID generator for Node.js. It is designed to generate compact and unique IDs with a minimum size of 21 bytes (or 16 URL-safe base64 characters) that can be easily used in URLs and other contexts where a shorter ID is required.

To use Nano ID in your Node.js project, install it using npm by running the following command in the terminal:

npm install nanoid

Once the module is installed,  nanoid() function is used to generate a unique ID. The nanoid() function generates a string that is unique for each call, using a cryptographically secure random number generator.

const { nanoid } = require('nanoid');

console.log(nanoid()); // Outputs something like: 'jmc2j5Nub7VoLBxVe84quU'

[demo title1=”node.js NanoID demo” url1=”http://hayageek.com/examples/nodejs/generating-uuid/index.php?tab=nanoid”%5D
The length of the ID also can be passed to the nanoid() function to limit the length of the UUID.  Note that the longer the ID, the lower the probability of collisions.

const { nanoid } = require('nanoid');

console.log(nanoid(10)); // Outputs something like: 'xkP7vQK7Cx'

 

Another feature of Nano ID is that custom alphabet can be specified to the generator. This can be useful to restrict the characters in the generated ID to a specific set of characters, such as lowercase letters or digits.

const { customAlphabet } = require('nanoid');

const alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';

const nanoid = customAlphabet(alphabet, 10);

console.log(nanoid()); // Outputs something like: ''suNNDWtPTC"

In the above example,  customAlphabet() function is used to create a new function that generates IDs using the specified alphabet and a length of 10 characters.

Nano ID is a great option for generating short, unique, and URL-friendly IDs in Node.js. With its small size and cryptographic security, it can be used in a variety of contexts where a compact and unique identifier is required.

 

References:
https://en.wikipedia.org/wiki/Universally_unique_identifier

https://www.npmjs.com/package/uuid

https://www.npmjs.com/package/nanoid