Skip to content

Commit 9e9d768

Browse files
committed
Update README.md to reflect changes in the API
Signed-off-by: Utkarsh Srivastava <srivastavautkarsh8097@gmail.com>
1 parent 0a02afd commit 9e9d768

File tree

1 file changed

+25
-72
lines changed

1 file changed

+25
-72
lines changed

README.md

Lines changed: 25 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22

33
[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/utkarsh-pro/nodejs-snowflake/graphs/commit-activity)
44
[![GitHub issues](https://img.shields.io/github/issues/utkarsh-pro/nodejs-snowflake.svg)](https://github.com/utkarsh-pro/nodejs-snowflake/issues/)
5-
![Dependencies](https://img.shields.io/david/utkarsh-pro/nodejs-snowflake)
5+
![License](https://img.shields.io/npm/l/nodejs-snowflake)
6+
![Top Language](https://img.shields.io/github/languages/top/utkarsh-pro/nodejs-snowflake)
7+
![Version](https://img.shields.io/npm/v/nodejs-snowflake)
8+
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/utkarsh-pro/nodejs-snowflake/Releases)
69

710
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++ for id generation also guaratees that the generated number will be of size 64 bits.
11+
The main ID generation function is written in Rust which interoperates with NodeJS via WASM, this makes the process of ID generation extremely fast.
912

10-
**Version 1.6 Updates**
11-
- Add `GetIDFromTimestamp` function which can be used in database queries.
12-
13-
**Version 1.5 Updates**
14-
- Add `GetMachineIDFromID` help extracting machine id from the generated ids, even if they were generated on different machines
13+
> ⚠️ Version 2 Alert! Version 2 of `nodejs-snowflake` has a lot of breaking changes and is a complete rewrite. Consider going through entire doc to understand the migration path.
1514
1615
## How to install
1716

@@ -20,92 +19,63 @@ npm install nodejs-snowflake --save
2019
yarn add nodejs-snowflake
2120
```
2221

23-
### NOTE
24-
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.
25-
26-
```javascript
27-
28-
const { UniqueID } = require('nodejs-snowflake');
29-
30-
const uid = new UniqueID({
31-
returnNumber: true
32-
});
33-
34-
const ID = uid.getUniqueID(); // This id is in javascript bigint format
35-
36-
```
37-
38-
### VERSION 1.5.x Notice
39-
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.
40-
41-
```javascript
42-
43-
const { UniqueID } = require('nodejs-snowflake');
44-
45-
const uid = new UniqueID({
46-
...,
47-
machineID: 2345 // Any number between 0 - 4095. If not provided then a random number will be used
48-
});
49-
50-
```
51-
5222
## Usage
5323

5424
### Generate ID
5525

5626
```javascript
57-
const { UniqueID } = require('nodejs-snowflake');
27+
const { Snowflake } = require('nodejs-snowflake');
5828

59-
const uid = new UniqueID(config);
29+
const uid = new Snowflake(config);
6030

6131
uid.getUniqueID(); // A 64 bit id is returned
6232

63-
uid.asyncGetUniqueID().then(id => console.log(id)); // Promisified version of the above method
64-
6533
```
6634

6735
#### Configuration
6836
UniqueID constructor takes in the following configuration
6937

7038
```javascript
7139
{
72-
returnNumber: boolean, // Defaults to false. If set to true, the returned ids will be of type bigint or else of type string
73-
customEpoch: number, // Defaults to 1546300800000 (01-01-2019). This is UNIX timestamp in ms
74-
machineID: number // A value ranging between 0 - 4095. If not provided then a random value will be used
40+
custom_epoch: number, // Defaults to Date.now(). This is UNIX timestamp in ms
41+
instance_id: number // A value ranging between 0 - 4095. If not provided then a random value will be used
7542
}
7643
```
7744

78-
### Get timestamp from the ID
79-
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+
### Get ID corresponding to a Timestamp
46+
This can be extremely helpful in writing database queries where the requirement could be to get entries created after a certain timestamp.
8047

8148
```javascript
8249
...
8350

84-
uid.getTimestampFromID(id); // Here id can be either as as string or as a bigint
51+
const id = uid.idFromTimestamp(Date.now()); // Here id will always be BigInt
52+
53+
console.log(id); // A 64 bit id is returned corresponding to the timestamp given
8554

8655
```
8756

88-
### Get machine id from the ID
89-
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.
57+
### Get timestamp from the ID
58+
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.
9059

9160
```javascript
9261
...
9362

94-
const mid = uid.getMachineIDFromID(id); // Here id can be either as as string or as a bigint
63+
// Pass the custom epoch that was used to generate this ID
64+
const ts = Snowflake.timestampFromId(id, uid.customEpoch());
9565

96-
console.log(mid); // 2345 -> This is the 12 bit long machine id where this token was generated
66+
console.log(ts) // Timestamp of creation of the id
9767

9868
```
9969

100-
### Get ID corresponding to a Timestamp
101-
This can be extremely helpful in writing database queries where the requirement could be to get entries created after a certain timestamp.
70+
### Get machine id from the ID
71+
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.
10272

10373
```javascript
10474
...
10575

106-
const id = uid.IDFromTimestamp(Date.now()); // Here id will always be BigInt
76+
const mid = Snowflake.instanceIDFromID(id);
10777

108-
console.log(id); // A 64 bit id is returned corresponding to the timestamp given
78+
console.log(mid); // 2345 -> This is the 12 bit long machine id where this token was generated
10979

11080
```
11181

@@ -115,23 +85,6 @@ This solely exits to find the machine id of current machine in case the user did
11585
```javascript
11686
...
11787

118-
uid.machineID; // The machine id of the current machine, set either by user or randomly generated
119-
120-
```
121-
122-
123-
## Basic benchmark
124-
```bash
125-
# Run for 1sec while invoking function every ms (default)
126-
npm run benchmark
127-
128-
# All the arguments
129-
# --time -> Total time for which the id should be generated
130-
# --type -> Return type of the id (could be number or string)
131-
# --interval -> Time interval in which id generation function should invoked
132-
# UNITS OF TIME
133-
# 1s = 1 second, 1m = 1 millisecond, 1u = 1 microsecond
134-
135-
npm run benchmark -- --time=2s --type=number --interval=1u
88+
uid.instanceID(); // The instance id of the current instance, set either by user or randomly generated
13689

13790
```

0 commit comments

Comments
 (0)