![]() |
VOOZH | about |
NPM(Node Package Manager) is a package manager of Node.js packages. There is an NPM package called 'shortid' used to create short non-sequential url-friendly unique ids. Unique ids are created by Cryptographically-strong random values that's why it is very secure. It has support for cross-platform like Node, React Native, Chrome, Safari, Firefox, etc.
Command to install:
npm install uuid
Syntax to import the package in local file
const {v4 : uuidv4} = require('uuid')
Syntax to create unique id
const newId = uuidv4()
There are some methods defined on shortid modules to create unique ids and customize the ids. some of the methods are illustrates below:
| Method | Work |
| uuid.NIL | The nil UUID string (all zeros) |
| uuid.parse() | Convert UUID string to array of bytes |
| uuid.validate() | Test a string to see if it is a valid UUID |
| uuid.v1() | Create a version 1 (timestamp) UUID |
| uuid.v3() | Create a version 3 (namespace w/ MD5) UUID |
| uuid.v4() | Create a version 4 (random) UUID |
| uuid.v5() | Create a version 5 (namespace w/ SHA-1) UUID |
| uuid.stringify() | Convert array of bytes to UUID string |
Example 1: This example illustrates how to generate and use uuid package to create unique ids.
filename-index.js: This file contains all the logic to create unique ids and attach it with user information and save to the database.
filename - repository.js: This file contain all the logic to create database and interact with it.
filename - form.js: This file contain all the logic to render form.
Output:
Database:
Example 2: This example illustrates how to use uuid.parse() and uuid.stringify() methods.
filename-index.js: This file contains all the logic to create unique ids and attach it with user information and save to the database and also convert id to parsed bytes and parsed bytes to string id.
filename - repository.js: This file contain all the logic to create database and interact with it.
filename - form.js : This file contain all the logic to render form.
Output:
Database:
Note: We have used some Bulma classes in form.js file to design our content.