You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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
+
constuid=newUniqueID({
25
+
returnAsNumber:true
26
+
});
27
+
28
+
constID=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
+
22
35
```javascript
23
36
24
37
const { UniqueID } =require('nodejs-snowflake');
25
38
26
-
constuid=newUniqueID();
39
+
constuid=newUniqueID({
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');
27
52
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
31
53
constuid=newUniqueID(config);
32
54
33
-
// Returns a 64 bit id as a string if returnAsNumber is set to false
34
-
constID=uid.getUniqueID(); // 116321924208963580
55
+
uid.getUniqueID(); // A 64 bit id is returned
35
56
36
-
// Returns a 64 bit id as a bigint if returnAsNumber is set to true
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
+
```
41
71
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.
45
74
75
+
```javascript
76
+
...
77
+
78
+
uid.getTimestampFromID(id); // Here id can be either as as string or as a bigint
46
79
47
80
```
48
81
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
+
constmid=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
+
49
105
## Basic benchmark
50
106
```bash
51
107
# Run for 1sec while invoking function every ms (default)
0 commit comments