{"version":3,"file":"mikro-orm.mjs","names":[],"sources":["../src/adapters/mikro-orm.ts"],"sourcesContent":["import { Type } from \"@mikro-orm/core\";\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 * Returns a MikroORM property option object that wires `codec.generate()` into the\n * `onCreate` lifecycle hook, so the field auto-fills on first persist.\n *\n * Requires a codec variant that exposes a synchronous `generate()` —\n * see {@link IdGeneratingCodec}. Only the **Timestamp codec** and **Reverse\n * Timestamp codec** qualify; Opaque, Signed, Wrapped, and Digest codecs cannot\n * be passed here.\n *\n * Pass the result as options to `@PrimaryKey()` (the typical case for auto-generated primary keys) or any other MikroORM property decorator:\n *\n * @example\n * ```ts\n * import { PrimaryKey } from \"@mikro-orm/core\";\n * import { idField } from \"@smonn/ids/mikro-orm\";\n * import { createTimestampId } from \"@smonn/ids\";\n * import type { Id } from \"@smonn/ids\";\n *\n * const usr = createTimestampId(\"usr\");\n *\n * class User {\n *   @PrimaryKey(idField(usr))\n *   id!: Id<\"usr\">;\n * }\n * ```\n */\nexport function idField<Brand extends string>(\n  codec: IdGeneratingCodec<Brand>,\n): {\n  type: new () => Type<Id<Brand>, string>;\n  onCreate: () => Id<Brand>;\n} {\n  return {\n    type: idType(codec),\n    onCreate: () => codec.generate(),\n  };\n}\n\n/**\n * Factory that returns a MikroORM `Type` subclass bound to a codec.\n *\n * **Write path** (`convertToDatabaseValue`): validates the value via\n * `codec.safeParse` and throws `IdsError(\"invalid_id\")` if validation fails.\n *\n * **Read path** (`convertToJSValue`): normalises the raw DB value via\n * `codec.safeParse()`. Throws `IdsError(\"invalid_id\")` if the stored value\n * does not parse as a valid `Id<Brand>`.\n *\n * **Column type** (`getColumnType`): returns `\"text\"` by default, or the\n * `options.columnType` override when provided.\n *\n * @param codec - The brand-scoped codec used to parse values read from the database.\n * @param options - Optional column configuration.\n * @param options.columnType - SQL column type to use (default: `\"text\"`). Pass\n *   `\"varchar(30)\"` or `\"char(26)\"` to match an existing DDL or index strategy.\n *   The value is passed through verbatim — no validation is performed.\n *\n * @example\n * ```ts\n * import { PrimaryKey } from \"@mikro-orm/core\";\n * import { idType } from \"@smonn/ids/mikro-orm\";\n * import { createTimestampId } from \"@smonn/ids\";\n * import type { Id } from \"@smonn/ids\";\n *\n * const usr = createTimestampId(\"usr\");\n *\n * // default: text column\n * class User {\n *   @PrimaryKey({ type: idType(usr) })\n *   id!: Id<\"usr\">;\n * }\n *\n * // explicit varchar column\n * class Org {\n *   @PrimaryKey({ type: idType(usr, { columnType: \"varchar(30)\" }) })\n *   id!: Id<\"usr\">;\n * }\n * ```\n */\nexport function idType<Brand extends string>(\n  codec: IdColumnCodec<Brand>,\n  options?: { columnType?: string },\n): new () => Type<Id<Brand>, string> {\n  const columnType = options?.columnType ?? \"text\";\n  return class extends Type<Id<Brand>, string> {\n    override convertToDatabaseValue(value: Id<Brand>): string {\n      return writeIdColumn(codec, value);\n    }\n    override convertToJSValue(value: string): Id<Brand> {\n      return readIdColumn(codec, value);\n    }\n    override getColumnType(): string {\n      return columnType;\n    }\n  };\n}\n\n/**\n * Factory that returns a MikroORM `Type` subclass for a **nullable** `Id<Brand>` column.\n *\n * Behaves like {@link idType} but `convertToJSValue` returns `null` for `null` /\n * `undefined` database values and `convertToDatabaseValue` normalises `null` and\n * `undefined` to `null`. Use for optional foreign keys.\n *\n * @param codec - The brand-scoped codec used to parse values read from the database.\n * @param options - Optional column configuration.\n * @param options.columnType - SQL column type to use (default: `\"text\"`). Pass\n *   `\"varchar(30)\"` or `\"char(26)\"` to match an existing DDL or index strategy.\n *   The value is passed through verbatim — no validation is performed.\n *\n * @example\n * ```ts\n * import { Property } from \"@mikro-orm/core\";\n * import { nullableIdType } from \"@smonn/ids/mikro-orm\";\n * import { createTimestampId } from \"@smonn/ids\";\n * import type { Id } from \"@smonn/ids\";\n *\n * const usr = createTimestampId(\"usr\");\n *\n * class Post {\n *   @Property({ type: nullableIdType(usr), nullable: true })\n *   authorId!: Id<\"usr\"> | null;\n * }\n *\n * // explicit varchar column\n * class Comment {\n *   @Property({ type: nullableIdType(usr, { columnType: \"varchar(30)\" }), nullable: true })\n *   authorId!: Id<\"usr\"> | null;\n * }\n * ```\n */\nexport function nullableIdType<Brand extends string>(\n  codec: IdColumnCodec<Brand>,\n  options?: { columnType?: string },\n): new () => Type<Id<Brand> | null, string | null> {\n  const columnType = options?.columnType ?? \"text\";\n  return class extends Type<Id<Brand> | null, string | null> {\n    override convertToDatabaseValue(value: Id<Brand> | null | undefined): string | null {\n      return writeIdColumnNullable(codec, value);\n    }\n    override convertToJSValue(value: string | null): Id<Brand> | null {\n      return readIdColumnNullable(codec, value);\n    }\n    override getColumnType(): string {\n      return columnType;\n    }\n  };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0CA,SAAgB,QACd,OAIA;CACA,OAAO;EACL,MAAM,OAAO,KAAK;EAClB,gBAAgB,MAAM,SAAS;CACjC;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,SAAgB,OACd,OACA,SACmC;CACnC,MAAM,aAAa,SAAS,cAAc;CAC1C,OAAO,cAAc,KAAwB;EAC3C,uBAAgC,OAA0B;GACxD,OAAO,cAAc,OAAO,KAAK;EACnC;EACA,iBAA0B,OAA0B;GAClD,OAAO,aAAa,OAAO,KAAK;EAClC;EACA,gBAAiC;GAC/B,OAAO;EACT;CACF;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCA,SAAgB,eACd,OACA,SACiD;CACjD,MAAM,aAAa,SAAS,cAAc;CAC1C,OAAO,cAAc,KAAsC;EACzD,uBAAgC,OAAoD;GAClF,OAAO,sBAAsB,OAAO,KAAK;EAC3C;EACA,iBAA0B,OAAwC;GAChE,OAAO,qBAAqB,OAAO,KAAK;EAC1C;EACA,gBAAiC;GAC/B,OAAO;EACT;CACF;AACF"}