import { n as IdColumnCodec, r as IdGeneratingCodec } from "./adapter-types-B-GLKPOm.mjs"; import { n as IdsErrorCode, r as isIdsError, t as IdsError } from "./error-JLZfeGOx.mjs"; import { ValueTransformer } from "typeorm"; //#region src/adapters/typeorm.d.ts /** * TypeORM column transformer for `Id`. * * Returns a `ValueTransformer` object suitable for use in a TypeORM `@Column` * decorator's `transformer` option. * * **Write path (`to`):** validates the value via `codec.safeParse` and throws * `IdsError("invalid_id")` if validation fails. * * **Read path (`from`):** normalises the raw database value via `codec.safeParse()`. * Throws `IdsError` with code `"invalid_id"` if the value does not parse as a valid * `Id`. * * **TypeORM branding caveat:** TypeORM cannot brand a generated entity field type at * the schema level. Annotate the entity field explicitly: `id!: Id<"usr">`. * * @example * ```ts * import { idTransformer } from "@smonn/ids/typeorm"; * import { createTimestampId } from "@smonn/ids"; * import type { Id } from "@smonn/ids"; * import { Column, Entity } from "typeorm"; * * const usr = createTimestampId("usr"); * * @Entity() * class User { * @Column({ type: "text", transformer: idTransformer(usr) }) * id!: Id<"usr">; * } * ``` */ declare function idTransformer(codec: IdColumnCodec): ValueTransformer; /** * Returns a function suitable for use inside a TypeORM `@BeforeInsert()` lifecycle * hook that auto-generates an `Id` for `fieldName` when the field is absent * (`null` or `undefined`) on the entity at insert time. If the field already has a * value it is left unchanged. * * Requires a codec variant that exposes a synchronous `generate()` — see * {@link IdGeneratingCodec}. Only the **Timestamp codec** and **Reverse Timestamp * codec** qualify; passing an async-generate codec (Opaque, Signed, Wrapped, Digest) * is a **compile-time type error**. * * Pair with {@link idTransformer} on the same column: `idTransformer` handles the * read/write path; `beforeInsertHook` handles generation. * * @example * ```ts * import { idTransformer, beforeInsertHook } from "@smonn/ids/typeorm"; * import { createTimestampId } from "@smonn/ids"; * import type { Id } from "@smonn/ids"; * import { BeforeInsert, Column, Entity } from "typeorm"; * * const usr = createTimestampId("usr"); * const fillUserId = beforeInsertHook("id", usr); * * @Entity() * class User { * @Column({ type: "text", transformer: idTransformer(usr) }) * id!: Id<"usr">; * * @BeforeInsert() * generateId() { * fillUserId(this); * } * } * ``` * * @remarks * **Gating rule:** `beforeInsertHook` requires a synchronous `generate()` codec * (`IdGeneratingCodec`). Async-generate codecs — Opaque Timestamp, Signed Timestamp, * Wrapped key, and Digest — do not satisfy `IdGeneratingCodec` and cannot be passed * here. TypeORM `@BeforeInsert` can be async, but synchronous generation keeps the * hook ergonomic and matches the full Prisma parity shape. * * **Read/write path:** Use {@link idTransformer} on the column for database * read/write transforms. `beforeInsertHook` only handles the generation step — it * does not replace the transformer. */ declare function beforeInsertHook(fieldName: string, codec: IdGeneratingCodec): (entity: Record) => void; /** * TypeORM column transformer for a **nullable** `Id` column. * * Behaves like {@link idTransformer} but `from` returns `null` for `null` / * `undefined` database values and `to` passes `null` / `undefined` through * unchanged. Use for optional foreign keys. * * @example * ```ts * import { nullableIdTransformer } from "@smonn/ids/typeorm"; * import { createTimestampId } from "@smonn/ids"; * import type { Id } from "@smonn/ids"; * import { Column, Entity } from "typeorm"; * * const usr = createTimestampId("usr"); * * @Entity() * class Post { * @Column({ type: "text", nullable: true, transformer: nullableIdTransformer(usr) }) * authorId!: Id<"usr"> | null; * } * ``` */ declare function nullableIdTransformer(codec: IdColumnCodec): ValueTransformer; //#endregion export { type IdColumnCodec, type IdGeneratingCodec, IdsError, type IdsErrorCode, beforeInsertHook, idTransformer, isIdsError, nullableIdTransformer }; //# sourceMappingURL=typeorm.d.mts.map