Skip to content

idType

idType<Brand>(codec, options?): () => Type<Id<Brand>, string>

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

Factory that returns a MikroORM Type subclass bound to a codec.

Write path (convertToDatabaseValue): validates the value via codec.safeParse and throws IdsError("invalid_id") if validation fails.

Read path (convertToJSValue): normalises the raw DB value via codec.safeParse(). Throws IdsError("invalid_id") if the stored value does not parse as a valid Id<Brand>.

Column type (getColumnType): returns "text" by default, or the options.columnType override when provided.

Brand extends string

IdColumnCodec<Brand>

The brand-scoped codec used to parse values read from the database.

Optional column configuration.

string

SQL column type to use (default: "text"). Pass "varchar(30)" or "char(26)" to match an existing DDL or index strategy. The value is passed through verbatim — no validation is performed.

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

import { PrimaryKey } from "@mikro-orm/core";
import { idType } from "@smonn/ids/mikro-orm";
import { createTimestampId } from "@smonn/ids";
import type { Id } from "@smonn/ids";
const usr = createTimestampId("usr");
// default: text column
class User {
@PrimaryKey({ type: idType(usr) })
id!: Id<"usr">;
}
// explicit varchar column
class Org {
@PrimaryKey({ type: idType(usr, { columnType: "varchar(30)" }) })
id!: Id<"usr">;
}