{"version":3,"file":"drizzle.mjs","names":["customTypeMysql","customTypeSqlite"],"sources":["../src/adapters/drizzle.ts"],"sourcesContent":["import {\n  customType,\n  type ConvertCustomConfig,\n  type PgCustomColumnBuilder,\n} from \"drizzle-orm/pg-core\";\nimport {\n  customType as customTypeMysql,\n  type ConvertCustomConfig as ConvertCustomConfigMysql,\n  type MySqlCustomColumnBuilder,\n} from \"drizzle-orm/mysql-core\";\nimport {\n  customType as customTypeSqlite,\n  type ConvertCustomConfig as ConvertCustomConfigSqlite,\n  type SQLiteCustomColumnBuilder,\n} from \"drizzle-orm/sqlite-core\";\nimport type { HasDefault, HasRuntimeDefault } from \"drizzle-orm/column-builder\";\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\nfunction buildIdColumn<Brand extends string, Col>(\n  columnFactory: (config: {\n    dataType(): string;\n    toDriver(value: Id<Brand>): string;\n    fromDriver(value: string): Id<Brand>;\n  }) => Col,\n  codec: IdColumnCodec<Brand>,\n  columnType: string,\n): Col {\n  return columnFactory({\n    dataType() {\n      return columnType;\n    },\n    toDriver(value: Id<Brand>): string {\n      return writeIdColumn(codec, value);\n    },\n    fromDriver(value: string): Id<Brand> {\n      return readIdColumn(codec, value);\n    },\n  });\n}\n\nfunction buildNullableIdColumn<Brand extends string, Col>(\n  columnFactory: (config: {\n    dataType(): string;\n    toDriver(value: Id<Brand> | null | undefined): string | null;\n    fromDriver(value: string | null): Id<Brand> | null;\n  }) => Col,\n  codec: IdColumnCodec<Brand>,\n  columnType: string,\n): Col {\n  return columnFactory({\n    dataType() {\n      return columnType;\n    },\n    toDriver(value: Id<Brand> | null | undefined): string | null {\n      return writeIdColumnNullable(codec, value);\n    },\n    fromDriver(value: string | null): Id<Brand> | null {\n      return readIdColumnNullable(codec, value);\n    },\n  });\n}\n\n/**\n * Drizzle custom column type that stores an `Id<Brand>` as a canonical SQL string value in PostgreSQL.\n *\n * **Write path:** validates the value via `codec.safeParse` and throws\n * `IdsError(\"invalid_id\")` if validation fails.\n *\n * **Read path:** normalises the raw DB string via `codec.safeParse()`, not strict\n * `is()`. Data at rest should already be canonical per ADR-0003, but `safeParse`\n * is a safe boundary in case stale non-canonical values exist. Throws if the\n * value from the database does not parse as a valid `Id<Brand>`.\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 { idColumn } from \"@smonn/ids/drizzle\";\n * import { createTimestampId } from \"@smonn/ids\";\n *\n * const usr = createTimestampId(\"usr\");\n * // default: text column\n * export const users = pgTable(\"users\", { id: idColumn(usr).primaryKey() });\n * // explicit varchar column\n * export const orgs = pgTable(\"orgs\", { id: idColumn(usr, { columnType: \"varchar(30)\" }).primaryKey() });\n * ```\n */\nexport function idColumn<Brand extends string>(\n  codec: IdColumnCodec<Brand>,\n  options?: { columnType?: string },\n): PgCustomColumnBuilder<ConvertCustomConfig<\"\", { data: Id<Brand>; driverData: string }>> {\n  return buildIdColumn(\n    (config) => customType<{ data: Id<Brand>; driverData: string }>(config)(),\n    codec,\n    options?.columnType ?? \"text\",\n  );\n}\n\n/**\n * Drizzle custom column type that stores an `Id<Brand>` as a canonical `text` value in MySQL.\n *\n * **Write path:** validates the value via `codec.safeParse` and throws\n * `IdsError(\"invalid_id\")` if validation fails.\n *\n * **Read path:** normalises the raw DB string via `codec.safeParse()`, not strict\n * `is()`. Throws `IdsError(\"invalid_id\")` if the value from the database does not\n * parse as a valid `Id<Brand>`.\n *\n * @example\n * ```ts\n * import { idColumnMysql } from \"@smonn/ids/drizzle\";\n * import { createTimestampId } from \"@smonn/ids\";\n *\n * const usr = createTimestampId(\"usr\");\n * export const users = mysqlTable(\"users\", { id: idColumnMysql(usr).primaryKey() });\n * // users.id is Id<\"usr\"> end-to-end\n * ```\n */\nexport function idColumnMysql<Brand extends string>(\n  codec: IdColumnCodec<Brand>,\n): MySqlCustomColumnBuilder<ConvertCustomConfigMysql<\"\", { data: Id<Brand>; driverData: string }>> {\n  return buildIdColumn(\n    (config) => customTypeMysql<{ data: Id<Brand>; driverData: string }>(config)(),\n    codec,\n    \"text\",\n  );\n}\n\n/**\n * Drizzle custom column type that stores an `Id<Brand>` as a canonical `text` value in SQLite.\n *\n * **Write path:** validates the value via `codec.safeParse` and throws\n * `IdsError(\"invalid_id\")` if validation fails.\n *\n * **Read path:** normalises the raw DB string via `codec.safeParse()`, not strict\n * `is()`. Throws `IdsError(\"invalid_id\")` if the value from the database does not\n * parse as a valid `Id<Brand>`.\n *\n * @example\n * ```ts\n * import { idColumnSqlite } from \"@smonn/ids/drizzle\";\n * import { createTimestampId } from \"@smonn/ids\";\n *\n * const usr = createTimestampId(\"usr\");\n * export const users = sqliteTable(\"users\", { id: idColumnSqlite(usr).primaryKey() });\n * // users.id is Id<\"usr\"> end-to-end\n * ```\n */\nexport function idColumnSqlite<Brand extends string>(\n  codec: IdColumnCodec<Brand>,\n): SQLiteCustomColumnBuilder<\n  ConvertCustomConfigSqlite<\"\", { data: Id<Brand>; driverData: string }>\n> {\n  return buildIdColumn(\n    (config) => customTypeSqlite<{ data: Id<Brand>; driverData: string }>(config)(),\n    codec,\n    \"text\",\n  );\n}\n\n/**\n * Drizzle custom column type for a **nullable** `Id<Brand>` column.\n *\n * Behaves identically to {@link idColumn} except that `null` and `undefined`\n * driver values are passed through as `null` rather than throwing. Use for\n * optional foreign keys, `LEFT JOIN` results, and any column that is\n * legitimately absent.\n *\n * **There is no `generatedNullableIdColumn`.** A column that auto-generates its own ID should\n * never be null at write time — that is the entire purpose of the generated variant. A nullable\n * column is one that the caller explicitly sets to `null`; it cannot simultaneously be\n * auto-generated. Use {@link nullableIdColumn} for optional foreign keys and {@link generatedIdColumn}\n * for primary keys that must always be present. The same reasoning applies to the MySQL and\n * SQLite equivalents ({@link nullableIdColumnMysql}, {@link nullableIdColumnSqlite}).\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 { nullableIdColumn } from \"@smonn/ids/drizzle\";\n * import { createTimestampId } from \"@smonn/ids\";\n *\n * const usr = createTimestampId(\"usr\");\n * // default: text column\n * export const posts = pgTable(\"posts\", {\n *   authorId: nullableIdColumn(usr),\n * });\n * // posts.authorId is Id<\"usr\"> | null end-to-end\n *\n * // explicit char column\n * export const comments = pgTable(\"comments\", {\n *   authorId: nullableIdColumn(usr, { columnType: \"char(26)\" }),\n * });\n * ```\n */\nexport function nullableIdColumn<Brand extends string>(\n  codec: IdColumnCodec<Brand>,\n  options?: { columnType?: string },\n): PgCustomColumnBuilder<\n  ConvertCustomConfig<\"\", { data: Id<Brand> | null; driverData: string | null }>\n> {\n  return buildNullableIdColumn(\n    (config) => customType<{ data: Id<Brand> | null; driverData: string | null }>(config)(),\n    codec,\n    options?.columnType ?? \"text\",\n  );\n}\n\n/**\n * Drizzle custom column type for a **nullable** `Id<Brand>` column in MySQL.\n *\n * Behaves identically to {@link idColumnMysql} except that `null` and `undefined`\n * driver values are passed through as `null` rather than throwing. Use for\n * optional foreign keys, `LEFT JOIN` results, and any column that is\n * legitimately absent.\n *\n * Column type is always `text` and cannot be overridden in MySQL.\n *\n * @param codec - The brand-scoped codec used to parse values read from the database.\n *\n * @example\n * ```ts\n * import { nullableIdColumnMysql } from \"@smonn/ids/drizzle\";\n * import { createTimestampId } from \"@smonn/ids\";\n *\n * const usr = createTimestampId(\"usr\");\n * export const posts = mysqlTable(\"posts\", {\n *   authorId: nullableIdColumnMysql(usr),\n * });\n * // posts.authorId is Id<\"usr\"> | null end-to-end\n * ```\n */\nexport function nullableIdColumnMysql<Brand extends string>(\n  codec: IdColumnCodec<Brand>,\n): MySqlCustomColumnBuilder<\n  ConvertCustomConfigMysql<\"\", { data: Id<Brand> | null; driverData: string | null }>\n> {\n  return buildNullableIdColumn(\n    (config) => customTypeMysql<{ data: Id<Brand> | null; driverData: string | null }>(config)(),\n    codec,\n    \"text\",\n  );\n}\n\n/**\n * Drizzle custom column type for a **nullable** `Id<Brand>` column in SQLite.\n *\n * Behaves identically to {@link idColumnSqlite} except that `null` and `undefined`\n * driver values are passed through as `null` rather than throwing. Use for\n * optional foreign keys, `LEFT JOIN` results, and any column that is\n * legitimately absent.\n *\n * Column type is always `text` and cannot be overridden in SQLite.\n *\n * @param codec - The brand-scoped codec used to parse values read from the database.\n *\n * @example\n * ```ts\n * import { nullableIdColumnSqlite } from \"@smonn/ids/drizzle\";\n * import { createTimestampId } from \"@smonn/ids\";\n *\n * const usr = createTimestampId(\"usr\");\n * export const posts = sqliteTable(\"posts\", {\n *   authorId: nullableIdColumnSqlite(usr),\n * });\n * // posts.authorId is Id<\"usr\"> | null end-to-end\n * ```\n */\nexport function nullableIdColumnSqlite<Brand extends string>(\n  codec: IdColumnCodec<Brand>,\n): SQLiteCustomColumnBuilder<\n  ConvertCustomConfigSqlite<\"\", { data: Id<Brand> | null; driverData: string | null }>\n> {\n  return buildNullableIdColumn(\n    (config) => customTypeSqlite<{ data: Id<Brand> | null; driverData: string | null }>(config)(),\n    codec,\n    \"text\",\n  );\n}\n\n/**\n * Drizzle custom column type that stores an `Id<Brand>` as a canonical SQL string value\n * in PostgreSQL, with a client-side `.$defaultFn` so inserts that omit the field receive\n * a freshly generated ID automatically.\n *\n * **Write path:** `.$defaultFn(() => codec.generate())` is wired — if the field is absent\n * on insert, Drizzle calls `codec.generate()` to produce a new `Id<Brand>`.\n *\n * **Read path:** normalises the raw DB string via `codec.safeParse()`, not strict `is()`.\n * Throws `IdsError(\"invalid_id\")` if the value from the database does not parse as a\n * valid `Id<Brand>`.\n *\n * Requires a codec that exposes synchronous `generate()` — see {@link IdGeneratingCodec}.\n * Only the **Timestamp codec** and **Reverse Timestamp codec** qualify; Opaque, Signed,\n * Wrapped, and Digest codecs are a compile-time error.\n *\n * @param codec - The brand-scoped codec used to generate and parse values.\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 { generatedIdColumn } from \"@smonn/ids/drizzle\";\n * import { createTimestampId } from \"@smonn/ids\";\n *\n * const usr = createTimestampId(\"usr\");\n * export const users = pgTable(\"users\", { id: generatedIdColumn(usr).primaryKey() });\n * // id is auto-filled on insert — no hand-supplied id needed\n * ```\n */\nexport function generatedIdColumn<Brand extends string>(\n  codec: IdGeneratingCodec<Brand>,\n  options?: { columnType?: string },\n): HasRuntimeDefault<\n  HasDefault<\n    PgCustomColumnBuilder<ConvertCustomConfig<\"\", { data: Id<Brand>; driverData: string }>>\n  >\n> {\n  return buildIdColumn(\n    (config) => customType<{ data: Id<Brand>; driverData: string }>(config)(),\n    codec,\n    options?.columnType ?? \"text\",\n  ).$defaultFn(() => codec.generate());\n}\n\n/**\n * Drizzle custom column type that stores an `Id<Brand>` as a canonical `text` value\n * in MySQL, with a client-side `.$defaultFn` so inserts that omit the field receive\n * a freshly generated ID automatically.\n *\n * **Write path:** `.$defaultFn(() => codec.generate())` is wired — if the field is absent\n * on insert, Drizzle calls `codec.generate()` to produce a new `Id<Brand>`.\n *\n * **Read path:** normalises the raw DB string via `codec.safeParse()`, not strict `is()`.\n * Throws `IdsError(\"invalid_id\")` if the value from the database does not parse as a\n * valid `Id<Brand>`.\n *\n * Requires a codec that exposes synchronous `generate()` — see {@link IdGeneratingCodec}.\n * Only the **Timestamp codec** and **Reverse Timestamp codec** qualify; Opaque, Signed,\n * Wrapped, and Digest codecs are a compile-time error.\n *\n * @param codec - The brand-scoped codec used to generate and parse values.\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 { generatedIdColumnMysql } from \"@smonn/ids/drizzle\";\n * import { createTimestampId } from \"@smonn/ids\";\n *\n * const usr = createTimestampId(\"usr\");\n * export const users = mysqlTable(\"users\", { id: generatedIdColumnMysql(usr).primaryKey() });\n * // id is auto-filled on insert — no hand-supplied id needed\n * ```\n */\nexport function generatedIdColumnMysql<Brand extends string>(\n  codec: IdGeneratingCodec<Brand>,\n  options?: { columnType?: string },\n): HasRuntimeDefault<\n  HasDefault<\n    MySqlCustomColumnBuilder<ConvertCustomConfigMysql<\"\", { data: Id<Brand>; driverData: string }>>\n  >\n> {\n  return buildIdColumn(\n    (config) => customTypeMysql<{ data: Id<Brand>; driverData: string }>(config)(),\n    codec,\n    options?.columnType ?? \"text\",\n  ).$defaultFn(() => codec.generate());\n}\n\n/**\n * Drizzle custom column type that stores an `Id<Brand>` as a canonical `text` value\n * in SQLite, with a client-side `.$defaultFn` so inserts that omit the field receive\n * a freshly generated ID automatically.\n *\n * **Write path:** `.$defaultFn(() => codec.generate())` is wired — if the field is absent\n * on insert, Drizzle calls `codec.generate()` to produce a new `Id<Brand>`.\n *\n * **Read path:** normalises the raw DB string via `codec.safeParse()`, not strict `is()`.\n * Throws `IdsError(\"invalid_id\")` if the value from the database does not parse as a\n * valid `Id<Brand>`.\n *\n * Requires a codec that exposes synchronous `generate()` — see {@link IdGeneratingCodec}.\n * Only the **Timestamp codec** and **Reverse Timestamp codec** qualify; Opaque, Signed,\n * Wrapped, and Digest codecs are a compile-time error.\n *\n * @param codec - The brand-scoped codec used to generate and parse values.\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 { generatedIdColumnSqlite } from \"@smonn/ids/drizzle\";\n * import { createTimestampId } from \"@smonn/ids\";\n *\n * const usr = createTimestampId(\"usr\");\n * export const users = sqliteTable(\"users\", { id: generatedIdColumnSqlite(usr).primaryKey() });\n * // id is auto-filled on insert — no hand-supplied id needed\n * ```\n */\nexport function generatedIdColumnSqlite<Brand extends string>(\n  codec: IdGeneratingCodec<Brand>,\n  options?: { columnType?: string },\n): HasRuntimeDefault<\n  HasDefault<\n    SQLiteCustomColumnBuilder<\n      ConvertCustomConfigSqlite<\"\", { data: Id<Brand>; driverData: string }>\n    >\n  >\n> {\n  return buildIdColumn(\n    (config) => customTypeSqlite<{ data: Id<Brand>; driverData: string }>(config)(),\n    codec,\n    options?.columnType ?? \"text\",\n  ).$defaultFn(() => codec.generate());\n}\n"],"mappings":";;;;;;AA+BA,SAAS,cACP,eAKA,OACA,YACK;CACL,OAAO,cAAc;EACnB,WAAW;GACT,OAAO;EACT;EACA,SAAS,OAA0B;GACjC,OAAO,cAAc,OAAO,KAAK;EACnC;EACA,WAAW,OAA0B;GACnC,OAAO,aAAa,OAAO,KAAK;EAClC;CACF,CAAC;AACH;AAEA,SAAS,sBACP,eAKA,OACA,YACK;CACL,OAAO,cAAc;EACnB,WAAW;GACT,OAAO;EACT;EACA,SAAS,OAAoD;GAC3D,OAAO,sBAAsB,OAAO,KAAK;EAC3C;EACA,WAAW,OAAwC;GACjD,OAAO,qBAAqB,OAAO,KAAK;EAC1C;CACF,CAAC;AACH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BA,SAAgB,SACd,OACA,SACyF;CACzF,O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