UNPKG

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 # ๐Ÿ” 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.