simple-flakeid is a JavaScript/TypeScript library for generating unique, time-ordered Snowflake IDs. Currently at version 0.0.5, it's in early development, aiming to provide robust ID generation with careful consideration for JavaScript's `Number.MAX_SAFE_INTEGER` limitation. It offers methods to generate IDs as standard `number` types (which can throw an error if exceeding `Number.MAX_SAFE_INTEGER`), `BigInt` types, or a dynamic type (`number` or `BigInt`) based on the ID's magnitude and configuration. The core algorithm is derived from yitter/idgenerator. While no explicit release cadence is stated, its recent low version indicates active and continuous development. A key differentiator is the explicit control and automatic handling of ID types to prevent overflow issues commonly found in JavaScript with large integer IDs.
npm install simple-flakeidVerified import paths — ran on the pinned version, not inferred.
Initializes a SnowflakeIdv1 generator and demonstrates generating IDs using NextId() and NextBigId() methods, showing dynamic type handling.
Use `NextId()` which dynamically returns `number` or `BigInt`, or `NextBigId()` to always get a `BigInt` for potentially larger IDs.
Always use `BIGINT` (or equivalent large integer type) for ID columns in your database schema to prevent data truncation or overflow errors.
Use type guards (`typeof id === 'bigint'`) or explicitly convert `BigInt` to `number` (with caution for precision) or `string` before use if your consuming code expects a specific type.
Pin your dependency to a specific patch version (`"simple-flakeid": "0.0.5"`) or thoroughly test updates before deploying to production.
Replace `generator.NextNumber()` with `generator.NextId()` (for dynamic type handling) or `generator.NextBigId()` (to always receive a BigInt).
Change the variable type to `bigint` or `number | bigint`. If you need a `number`, ensure the ID is within `Number.MAX_SAFE_INTEGER` and consider `Number(bigIntValue)` for explicit (and potentially lossy) conversion, or `.toString()` for string representation.
No dependency data recorded yet.