Generators
generators replaces a built-in client generator by name.
generators replaces a built-in client generator by name. The names are uuidv4, uuidv7, and okid. Each replacement returns a string. A generator runs when insert omits the field.
| Key | Insert |
|---|---|
| omitted | Calls the function stored on the column. |
uuidv4 | Calls the function you passed, for a uuidv4 generator. |
uuidv7 | Calls the function you passed, for a uuidv7 generator. |
okid | Calls the function you passed, for an okid generator. |
The function takes no arguments. One function covers every column of that name. A bare t.id() that took that generator from the schema id default uses it too. Each missing field calls the function once.
uuidv7
Insert stores this string when id is omitted.
import { connect } from "okmodel/pg/postgresjs";
import { schema, table, t } from "okmodel/pg";
import { uuidv7 } from "okmodel/ids";
const notes = table("notes", {
id: t.uuid().default(uuidv7),
title: t.text(),
});
const app = schema({ tables: [notes] });
export async function add(url: string, title: string) {
await using db = connect(url, {
schema: app,
generators: {
uuidv7: () => "00000000-0000-7000-8000-000000000000",
},
});
return db.notes.insert({ title });
}Left alone
A function you wrote and passed to .default() keeps its own call. A bare t.id() stores gen_random_uuid(). t.id({ default: "uuidv7" }) stores uuidv7(), which needs Postgres 18. Pick that for a large write-heavy table: the ids are time-ordered and the index stays local. Random ids are fine elsewhere.