Function: nanoId()
nanoId(args: { length?: number; lix: Pick<Lix, "call"> & Record<string, unknown>; }): Promise<string>
Generate a nano ID.
Deterministic in deterministic mode; otherwise returns a random 21-character nano ID using the nanoid algorithm.
Use nano IDs when IDs will appear in URLs – their shorter length makes links easier to share.
For better database performance with time-ordered queries, consider uuidV7 instead.
- Normal mode: URL-safe random ID using custom alphabet (no
- or _).
- Deterministic mode: "test_" + 10-digit zero-padded counter.
- Counter state shared with nextDetermininisticSequenceNumber | nextDeterministicSequenceNumber.
- The "test_" prefix makes deterministic IDs easily identifiable.
- Choose nano IDs for URL-friendly short IDs, uuidV7 for time-sortable database keys.
- Use the Nano ID collision calculator to choose optimal length.
Examples
const lix = await openLix();
const id = await nanoId({ lix }) // "V1StGXR8_Z5jdHi6B-myT"
const lix = await openLix({
keyValues: [{ key: "lix_deterministic_mode", value: { enabled: true }, lixcol_version_id: "global" }]
});
await nanoId({ lix }) // "test_0000000000"
await nanoId({ lix }) // "test_0000000001"
await nanoId({ lix }) // "test_0000000002"
await lix.db
.insertInto("label")
.values({ id: await nanoId({ lix }), name: "bug", color: "#ff0000" })
.execute();
Parameters
| Parameter | Type | Description |
|---|
args | { length?: number; lix: Pick<Lix, "call"> & Record<string, unknown>; } | - |
args.length? | number | Optional length for non-deterministic mode (default: 21) |
args.lix | Pick<Lix, "call"> & Record<string, unknown> | Lix instance used to call into the engine. |
Returns
Promise<string>