btuid
Version:
Btuid library provides an interface to generate UIDs (Unique Identifiers) that are safe, cryptographically secure, and unpredictable, ensuring that every UUID is unique. The library also supports B+tree indexing, which makes it ideal for use in database s
135 lines (97 loc) โข 4.24 kB
Markdown
````markdown
# ๐ Btuid
**A fast, indexable, secure, and unique UUID generator for Node.js and modern JavaScript environments.**
## โ
Features
- โก๏ธ **Ultra-fast generation** โ optimized for high-throughput environments.
- ๐ **Cryptographically secure** โ uses secure random generation under the hood.
- ๐งฌ **Globally unique** โ guarantees no collisions, even across distributed systems.
- ๐งผ **Privacy-friendly** โ no sensitive information (e.g., MAC, IP, PID) is encoded.
- ๐ **Highly indexable** โ designed for optimal performance with B-tree and B+tree indexes.
- ๐ฏ **Unpredictable** โ the next UUID cannot be guessed based on the previous one.
## โ ๏ธ Limitations
- ๐ฒ **Not fully random** โ the ID is partially structured to ensure sortability and indexability.
- ๐พ **Requires storage access** โ to fully benefit from index performance Requires storage access.
| Feature | `btuid` ๐ฅ | `uuid v4` ๐ฒ | `nanoid` โจ |
| ------------------ | ------------------------- | ------------------------- | ---------------------- |
| **generate Speed** | โก๏ธ Very fast | ๐ข Slower | โก๏ธ Fast |
| **Security** | โ
High but less than v4 | โ
High | โ
High |
| **Length** | ๐ด Long (32 chars + '-') | ๐ด Long (36 chars) | ๐ก Medium (~21 chars) |
| **Index-friendly** | โ
B/B+Tree optimized | โ No | โ No |
| **Predictability** | โ
Unpredictable | โ
Unpredictable | โ
Unpredictable |
| **DB Performance** | โ
Excellent for indexing | โ Poor due to randomness | โ Poor for indexing |
| **Dependencies** | โ
Zero | โ Requires uuid lib | โ Requires nanoid lib |
| **Uniqueness** | โ
best | โ
high | โ
high |
## ๐ฆ Installation
```bash
npm install btuid
# or
yarn add btuid
````
## ๐ฆ Usage
```js
import { BtuidGenerator } from 'btuid';
import path from 'path';
// create btuidFiles folder in your project root
const filePath = path.join("your_project_root/btuidFiles", 'dataTableName.json');
const generator = new BtuidGenerator({ path: filePath ,securityKey : "mykey"});
const extraBtuid = generator.getExtraBtuid(); // get hex btuid
console.log(extraBtuid); // 06e77028e74c0082-26c4838e4a1f408b
## ๐ฆ Advance Usage
### Postgres database (auto degree calculation)
```ts
let degreeConfig: DegreeConfig = {
pageSize: yourTablePageSize,
keySize: yourTableKeySize,
TIDSize: yourTableTIDSize,
indexTupleDataSize: yourTableIndexTupleDataSize,
linePointerSize: yourTableLinePointerSize,
addingPaddingSize: yourTableAddingPaddingSize,
degree: 0 // auto-calculate B+Tree degree (compatible with Postgres)
};
const generator = new BtuidGenerator({
degreeConfig : degreeConfig,
startValue: startValue, // optional bigint offset
displacementRate: displacementRate, // 0 --> 200
path: filePath,
saveTime: saveTime // in mseconds, default is one day
});
```
### Manual add B-Tree,B+Tree degree
```ts
let degreeConfig: DegreeConfig = {
pageSize: 0,
keySize: 0,
TIDSize: 0,
indexTupleDataSize: 0,
linePointerSize: 0,
addingPaddingSize: 0,
degree: yourBtreeDegree // manually set degree
};
const generator = new BtuidGenerator({
degreeConfig:degreeConfig,
startValue: startValue,
displacementRate: displacementRate,
path: filePath,
saveTime: saveTime,
hexLength:hexPartLength, //beta
randomLength:randomPartLength //beta
,securityKey : "mykey"
});
```
## ๐ค Contributing
We welcome contributions, issues, and feature requests!
Please open an issue or submit a PR via GitHub.
## ๐ Keywords
`uuid` ย โขย `btuid` ย โขย `id generator` ย โขย `secure id` ย โขย `indexable uuid`
`typescript` ย โขย `javascript` ย โขย `performance` ย โขย `fast uuid` ย โขย `crypto uuid`
## ๐ Acknowledgments
Special thanks to [Laila](https://github.com/laila0010) for her valuable contribution to this project.