Skip to content

idField

idField<Brand>(codec): object

Defined in: src/adapters/mikro-orm.ts:43

Returns a MikroORM property option object that wires codec.generate() into the onCreate lifecycle hook, so the field auto-fills on first persist.

Requires a codec variant that exposes a synchronous generate() — see IdGeneratingCodec. Only the Timestamp codec and Reverse Timestamp codec qualify; Opaque, Signed, Wrapped, and Digest codecs cannot be passed here.

Pass the result as options to @PrimaryKey() (the typical case for auto-generated primary keys) or any other MikroORM property decorator:

Brand extends string

IdGeneratingCodec<Brand>

object

onCreate: () => Id<Brand>

Id<Brand>

type: () => Type<Id<Brand>, string>

Type<Id<Brand>, string>

import { PrimaryKey } from "@mikro-orm/core";
import { idField } from "@smonn/ids/mikro-orm";
import { createTimestampId } from "@smonn/ids";
import type { Id } from "@smonn/ids";
const usr = createTimestampId("usr");
class User {
@PrimaryKey(idField(usr))
id!: Id<"usr">;
}