Skip to content

Commit e8c1298

Browse files
committed
Added new README.md
1 parent 0641153 commit e8c1298

File tree

1 file changed

+77
-21
lines changed

1 file changed

+77
-21
lines changed

README.md

Lines changed: 77 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
# nodejs-snowflake
22

3-
[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://gitHub.com/utkarsh-pro/nodejs-snowflake/graphs/commit-activity)
4-
[![GitHub issues](https://img.shields.io/github/issues/utkarsh-pro/nodejs-snowflake.svg)](https://gitHub.com/utkarsh-pro/nodejs-snowflake/issues/)
3+
[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/utkarsh-pro/nodejs-snowflake/graphs/commit-activity)
4+
[![GitHub issues](https://img.shields.io/github/issues/utkarsh-pro/nodejs-snowflake.svg)](https://github.com/utkarsh-pro/nodejs-snowflake/issues/)
55
![Dependencies](https://img.shields.io/david/utkarsh-pro/nodejs-snowflake)
66

77
nodejs-snowflake is a fast and reliable way to generate time sortable 64 bit ids written for distributed systems.
8-
The main id generation function is written in C++ using N-API which makes the process of id generation extremely fast. The usage of C++
9-
for id generation also guaratees that the generated number will be of size 64 bits.
10-
11-
### NOTE
12-
The ID generator produces ids faster if the return type is bigint, but this option is disabled by default because of the time consumed by javascript in conversion of bigint to string. This conversion is thus by default handled by C++ internally.
8+
The main id generation function is written in C++ using N-API which makes the process of id generation extremely fast. The usage of C++ for id generation also guaratees that the generated number will be of size 64 bits.
139

1410
## How to install
1511

@@ -18,34 +14,94 @@ npm install nodejs-snowflake --save
1814
yarn add nodejs-snowflake
1915
```
2016

21-
## Usage
17+
### NOTE
18+
The ID generator produces ids faster if the return type is bigint, but this option is disabled by default. Do the following to enable this feature.
19+
20+
```javascript
21+
22+
const { UniqueID } = require('nodejs-snowflake');
23+
24+
const uid = new UniqueID({
25+
returnAsNumber: true
26+
});
27+
28+
const ID = uid.getUniqueID(); // This id is in javascript bigint format
29+
30+
```
31+
32+
### VERSION 1.5.x Notice
33+
In earlier versions of nodejs-snowflake, mac address was used for generating the unique ids. This is **no** longer supported in versions 1.5.x due to multiple reasons. Instead of the mac address it now uses "machine id" (value can range from 0 - 4095) which are supposed to be passed by the user. If no machine id is passed by the user then a random number would be used. The benefit of this approach is now the library supports extraction of machine id from the generated ids (irrespective of the machine used to generate it) which can be very useful in error detection in a clustered environment.
34+
2235
```javascript
2336

2437
const { UniqueID } = require('nodejs-snowflake');
2538

26-
const uid = new UniqueID();
39+
const uid = new UniqueID({
40+
...,
41+
machineID: 2345 // Any number between 0 - 4095. If not provided then a random number will be used
42+
});
43+
44+
```
45+
46+
## Usage
47+
48+
### Generate ID
49+
50+
```javascript
51+
const { UniqueID } = require('nodejs-snowflake');
2752

28-
// A config object can be passed into the constructor -> { customEpoch: some_value, returnAsNumber: true | false }
29-
// Default custom epoch is 1546300800000 (01-01-2019)
30-
// OR
3153
const uid = new UniqueID(config);
3254

33-
// Returns a 64 bit id as a string if returnAsNumber is set to false
34-
const ID = uid.getUniqueID(); // 116321924208963580
55+
uid.getUniqueID(); // A 64 bit id is returned
3556

36-
// Returns a 64 bit id as a bigint if returnAsNumber is set to true
37-
const ID_AS_BIGINT = uid.getUniqueID(); // 116321924208963580
57+
uid.asyncGetUniqueID().then(id => console.log(id)); // Promisified version of the above method
3858

39-
// Promisified getUniqueID
40-
uid.asyncGetUniqueID().then(id => console.log(id)) // 116321924208963580
59+
```
60+
61+
#### Configuration
62+
UniqueID constructor takes in the following configuration
63+
64+
```javascript
65+
{
66+
returnAsNumber: boolean, // Defaults to false. If set to true, the returned ids will be of type bigint or else of type string
67+
customEpoch: number, // Defaults to 1546300800000 (01-01-2019). This is UNIX timestamp in ms
68+
machineID: number // A value ranging between 0 - 4095. If not provided then a random value will be used
69+
}
70+
```
4171

42-
// Returns the epoch timestamp of creation of the id
43-
// independent of the machine it was created
44-
uid.getTimestampFromID(ID); // 1574034107888
72+
### Get timestamp from the ID
73+
Get the timestamp of creation of the ID can be extracted by using this method. This method will work even if this instance or machine wasn't actually used to generate this id.
4574

75+
```javascript
76+
...
77+
78+
uid.getTimestampFromID(id); // Here id can be either as as string or as a bigint
4679

4780
```
4881

82+
### Get machine id from the ID
83+
Get the machine id of the machine on which the token was generated. This method will work even if this instance or machine wasn't actually used to generate this id.
84+
85+
```javascript
86+
...
87+
88+
const mid = uid.getMachineIDFromID(id); // Here id can be either as as string or as a bigint
89+
90+
console.log(mid); // 2345 -> This is the 12 bit long machine id where this token was generated
91+
92+
```
93+
94+
### Get the current machine id
95+
This solely exits to find the machine id of current machine in case the user didn't provided as machine id and relied on the randomly generated value.
96+
97+
```javascript
98+
...
99+
100+
uid.machineID; // The machine id of the current machine, set either by user or randomly generated
101+
102+
```
103+
104+
49105
## Basic benchmark
50106
```bash
51107
# Run for 1sec while invoking function every ms (default)

0 commit comments

Comments
 (0)