{"version":3,"file":"typeorm.mjs","names":[],"sources":["../src/adapters/typeorm.ts"],"sourcesContent":["import type { ValueTransformer } from \"typeorm\";\nimport {\n  readIdColumn,\n  readIdColumnNullable,\n  writeIdColumn,\n  writeIdColumnNullable,\n  type IdColumnCodec,\n  type IdGeneratingCodec,\n} from \"./adapter-types.js\";\nimport type { Id } from \"../types.js\";\n\n/** {@link IdsError} class, {@link isIdsError} type guard, and {@link IdsErrorCode} union — re-exported from `\"@smonn/ids\"` for convenience. */\nexport { IdsError, isIdsError, type IdsErrorCode } from \"../error.js\";\n\nexport type { IdColumnCodec, IdGeneratingCodec };\n\n/**\n * TypeORM column transformer for `Id<Brand>`.\n *\n * Returns a `ValueTransformer` object suitable for use in a TypeORM `@Column`\n * decorator's `transformer` option.\n *\n * **Write path (`to`):** validates the value via `codec.safeParse` and throws\n * `IdsError(\"invalid_id\")` if validation fails.\n *\n * **Read path (`from`):** normalises the raw database value via `codec.safeParse()`.\n * Throws `IdsError` with code `\"invalid_id\"` if the value does not parse as a valid\n * `Id<Brand>`.\n *\n * **TypeORM branding caveat:** TypeORM cannot brand a generated entity field type at\n * the schema level. Annotate the entity field explicitly: `id!: Id<\"usr\">`.\n *\n * @example\n * ```ts\n * import { idTransformer } from \"@smonn/ids/typeorm\";\n * import { createTimestampId } from \"@smonn/ids\";\n * import type { Id } from \"@smonn/ids\";\n * import { Column, Entity } from \"typeorm\";\n *\n * const usr = createTimestampId(\"usr\");\n *\n * @Entity()\n * class User {\n *   @Column({ type: \"text\", transformer: idTransformer(usr) })\n *   id!: Id<\"usr\">;\n * }\n * ```\n */\nexport function idTransformer<Brand extends string>(codec: IdColumnCodec<Brand>): ValueTransformer {\n  return {\n    to(value: Id<Brand>): string {\n      return writeIdColumn(codec, value);\n    },\n    from(value: unknown): Id<Brand> {\n      return readIdColumn(codec, value);\n    },\n  };\n}\n\n/**\n * Returns a function suitable for use inside a TypeORM `@BeforeInsert()` lifecycle\n * hook that auto-generates an `Id<Brand>` for `fieldName` when the field is absent\n * (`null` or `undefined`) on the entity at insert time. If the field already has a\n * value it is left unchanged.\n *\n * Requires a codec variant that exposes a synchronous `generate()` — see\n * {@link IdGeneratingCodec}. Only the **Timestamp codec** and **Reverse Timestamp\n * codec** qualify; passing an async-generate codec (Opaque, Signed, Wrapped, Digest)\n * is a **compile-time type error**.\n *\n * Pair with {@link idTransformer} on the same column: `idTransformer` handles the\n * read/write path; `beforeInsertHook` handles generation.\n *\n * @example\n * ```ts\n * import { idTransformer, beforeInsertHook } from \"@smonn/ids/typeorm\";\n * import { createTimestampId } from \"@smonn/ids\";\n * import type { Id } from \"@smonn/ids\";\n * import { BeforeInsert, Column, Entity } from \"typeorm\";\n *\n * const usr = createTimestampId(\"usr\");\n * const fillUserId = beforeInsertHook(\"id\", usr);\n *\n * @Entity()\n * class User {\n *   @Column({ type: \"text\", transformer: idTransformer(usr) })\n *   id!: Id<\"usr\">;\n *\n *   @BeforeInsert()\n *   generateId() {\n *     fillUserId(this);\n *   }\n * }\n * ```\n *\n * @remarks\n * **Gating rule:** `beforeInsertHook` requires a synchronous `generate()` codec\n * (`IdGeneratingCodec`). Async-generate codecs — Opaque Timestamp, Signed Timestamp,\n * Wrapped key, and Digest — do not satisfy `IdGeneratingCodec` and cannot be passed\n * here. TypeORM `@BeforeInsert` can be async, but synchronous generation keeps the\n * hook ergonomic and matches the full Prisma parity shape.\n *\n * **Read/write path:** Use {@link idTransformer} on the column for database\n * read/write transforms. `beforeInsertHook` only handles the generation step — it\n * does not replace the transformer.\n */\nexport function beforeInsertHook<Brand extends string>(\n  fieldName: string,\n  codec: IdGeneratingCodec<Brand>,\n): (entity: Record<string, unknown>) => void {\n  return function (entity: Record<string, unknown>): void {\n    if (entity[fieldName] == null) {\n      entity[fieldName] = codec.generate();\n    }\n  };\n}\n\n/**\n * TypeORM column transformer for a **nullable** `Id<Brand>` column.\n *\n * Behaves like {@link idTransformer} but `from` returns `null` for `null` /\n * `undefined` database values and `to` passes `null` / `undefined` through\n * unchanged. Use for optional foreign keys.\n *\n * @example\n * ```ts\n * import { nullableIdTransformer } from \"@smonn/ids/typeorm\";\n * import { createTimestampId } from \"@smonn/ids\";\n * import type { Id } from \"@smonn/ids\";\n * import { Column, Entity } from \"typeorm\";\n *\n * const usr = createTimestampId(\"usr\");\n *\n * @Entity()\n * class Post {\n *   @Column({ type: \"text\", nullable: true, transformer: nullableIdTransformer(usr) })\n *   authorId!: Id<\"usr\"> | null;\n * }\n * ```\n */\nexport function nullableIdTransformer<Brand extends string>(\n  codec: IdColumnCodec<Brand>,\n): ValueTransformer {\n  return {\n    to(value: Id<Brand> | null | undefined): string | null {\n      return writeIdColumnNullable(codec, value);\n    },\n    from(value: unknown): Id<Brand> | null {\n      return readIdColumnNullable(codec, value);\n    },\n  };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgDA,SAAgB,cAAoC,OAA+C;CACjG,OAAO;EACL,GAAG,OAA0B;GAC3B,OAAO,cAAc,OAAO,KAAK;EACnC;EACA,KAAK,OAA2B;GAC9B,OAAO,aAAa,OAAO,KAAK;EAClC;CACF;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiDA,SAAgB,iBACd,WACA,OAC2C;CAC3C,OAAO,SAAU,QAAuC;EACtD,IAAI,OAAO,cAAc,MACvB,OAAO,aAAa,MAAM,SAAS;CAEvC;AACF;;;;;;;;;;;;;;;;;;;;;;;;AAyBA,SAAgB,sBACd,OACkB;CAClB,OAAO;EACL,GAAG,OAAoD;GACrD,OAAO,sBAAsB,OAAO,KAAK;EAC3C;EACA,KAAK,OAAkC;GACrC,OAAO,qBAAqB,OAAO,KAAK;EAC1C;CACF;AACF"}