UNPKG

drizzle-cuid2

Version:

A utility package for generating CUID2 columns in Drizzle ORM

1 lines 4.77 kB
{"version":3,"sources":["../src/sqlite-core/builder.ts","../src/sqlite-core/index.ts"],"sourcesContent":["import { init } from '@paralleldrive/cuid2';\nimport {\n type ColumnBuilderRuntimeConfig,\n entityKind,\n type ColumnBaseConfig,\n type ColumnBuilderBaseConfig,\n type MakeColumnConfig,\n type HasDefault,\n} from 'drizzle-orm';\nimport {\n SQLiteColumn,\n SQLiteColumnBuilder,\n type AnySQLiteTable,\n} from 'drizzle-orm/sqlite-core';\n\nexport type SQLiteCuid2Config = {\n length: number;\n prefix?: string;\n};\n\nconst createId = (length: number) => init({ length });\n\nexport type SQLiteCuid2BuilderInitial<TName extends string> = Omit<\n SQLiteCuid2Builder<{\n name: TName;\n dataType: 'string';\n columnType: 'SQLiteCuid2';\n data: string;\n driverParam: string;\n enumValues: undefined;\n }>,\n 'default' | '$default' | '$defaultFn'\n>;\n\nexport class SQLiteCuid2Builder<\n T extends ColumnBuilderBaseConfig<'string', 'SQLiteCuid2'>\n> extends SQLiteColumnBuilder<T> {\n static readonly [entityKind]: string = 'SQLiteCuid2Builder';\n private length = 24;\n private prefix?: string;\n\n constructor(name: string) {\n super(name, 'string', 'SQLiteCuid2');\n }\n\n build<TTableName extends string>(\n table: AnySQLiteTable<{ name: TTableName }>\n ): SQLiteCuid2<MakeColumnConfig<T, TTableName>> {\n return new SQLiteCuid2<MakeColumnConfig<T, TTableName>>(\n table,\n this.config as ColumnBuilderRuntimeConfig<\n T extends { $type: infer U } ? U : T['data'],\n SQLiteCuid2Config\n >,\n this.length,\n this.prefix\n );\n }\n\n /***\n * Creates a random `cuid2` value as the default value for the column.\n * The function will be called when the row is inserted, and the returned value will be used as the column value.\n */\n defaultRandom(): HasDefault<this> {\n this.config.defaultFn = () => {\n const id = createId(this.length)();\n return this.prefix ? `${this.prefix}${id}` : id;\n };\n this.config.hasDefault = true;\n return this as HasDefault<this>;\n }\n\n /***\n * Sets the length of the CUID2 value.\n * @param length The length of the CUID2 value (default: 24)\n */\n setLength(length: number): this {\n this.length = length;\n return this;\n }\n\n /***\n * Sets the prefix for the CUID2 value.\n * @param prefix The prefix to prepend to the CUID2 value\n */\n setPrefix(prefix: string): this {\n this.prefix = prefix;\n return this;\n }\n}\n\nexport class SQLiteCuid2<\n T extends ColumnBaseConfig<'string', 'SQLiteCuid2'>\n> extends SQLiteColumn<T> {\n static readonly [entityKind]: string = 'SQLiteCuid2';\n private length: number;\n private prefix?: string;\n\n constructor(\n table: AnySQLiteTable<{ name: string }>,\n config: ColumnBuilderRuntimeConfig<\n T extends { $type: infer U } ? U : T['data'],\n SQLiteCuid2Config\n >,\n length: number,\n prefix?: string\n ) {\n super(table, config);\n this.length = length;\n this.prefix = prefix;\n }\n\n getSQLType(): string {\n const totalLength = this.prefix ? this.length + this.prefix.length : this.length;\n return `text(${totalLength})`;\n }\n}\n","import { SQLiteCuid2Builder, type SQLiteCuid2BuilderInitial } from './builder';\n\nexport function cuid2(): SQLiteCuid2BuilderInitial<''>;\nexport function cuid2<TName extends string>(\n name: TName\n): SQLiteCuid2BuilderInitial<TName>;\nexport function cuid2<TName extends string>(\n name?: TName\n): SQLiteCuid2BuilderInitial<TName> {\n return new SQLiteCuid2Builder(name ?? ('' as TName));\n}\n\nexport * from './builder';\n"],"mappings":";AAAA,SAAS,YAAY;AACrB;AAAA,EAEE;AAAA,OAKK;AACP;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AAOP,IAAM,WAAW,CAAC,WAAmB,KAAK,EAAE,OAAO,CAAC;AAc7C,IAAM,qBAAN,cAEG,oBAAuB;AAAA,EAC/B,QAAiB,UAAU,IAAY;AAAA,EAC/B,SAAS;AAAA,EACT;AAAA,EAER,YAAY,MAAc;AACxB,UAAM,MAAM,UAAU,aAAa;AAAA,EACrC;AAAA,EAEA,MACE,OAC8C;AAC9C,WAAO,IAAI;AAAA,MACT;AAAA,MACA,KAAK;AAAA,MAIL,KAAK;AAAA,MAC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