@chronark/prefixed-id
pre_JUbF9zRGz9hrFXUyNJLXcowD9GsqCD
A minimal library to generate Stripe inspired predixed ids for your application.
Prefixed ids look like this: pre_JUbF9zRGz9hrFXUyNJLXcowD9GsqCD
. They can be very useful if you
have different entities and want to quickly identify them.
Generated ids rely on the provided crypto implementation. If you use either window.crypto
or
crypto
from "node:crypto", the ids will be cryptographically secure.
Works in
- Nodejs
- Cloudflare Workers
- Vercel Edge
Install
npm i @chronark/prefixed-id
Usage
Nodejs
import nodeCrypto from "node:crypto"
const idGenerator = new IdGenerator({
prefixes: {
"user": "u",
},
crypto: nodeCrypto
})
console.log(idGenerator.id("user"))
WebCrypto
- Cloudflare Workers
- Vercel Edge
const idGenerator = new IdGenerator({
prefixes: {
"user": "u",
},
crypto: crypto
})
console.log(idGenerator.id("user"))
Options
You may pass these options to the constructor to customize the id generation.
{
alphabet?: string;
crypto?: Crypto;
size?: number;
};