Function: nanoId()
nanoId(args
: { length?
: number
; lix
: Pick
<Lix
, "sqlite"
| "db"
| "hooks"
>; }): string
Returns a nano ID that is deterministic in deterministic mode.
In normal mode, returns a random 21-character nano ID using the nanoid algorithm.
In deterministic mode, returns sequential IDs with "test_" prefix for easy identification.
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 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 find the optimal length for your shareability vs uniqueness needs
Examples
const lix = await openLix();
nanoId({ lix }); // "V1StGXR8_Z5jdHi6B-myT"
const lix = await openLix({
keyValues: [{ key: "lix_deterministic_mode", value: { enabled: true }, lixcol_version_id: "global" }]
});
nanoId({ lix }); // "test_0000000000"
nanoId({ lix }); // "test_0000000001"
nanoId({ lix }); // "test_0000000002"
await lix.db
.insertInto("label")
.values({
id: nanoId({ lix }),
name: "bug",
color: "#ff0000"
})
.execute();
Parameters
Parameter | Type | Description |
---|
args | { length? : number ; lix : Pick <Lix , "sqlite" | "db" | "hooks" >; } | - |
args.length? | number | Custom length for non-deterministic mode (default: 21) |
args.lix | Pick <Lix , "sqlite" | "db" | "hooks" > | The Lix instance with sqlite and db connections |
Returns
string
Nano ID string