drizzle-cuid2
Version:
A utility package for generating CUID2 columns in Drizzle ORM
1 lines • 3.05 kB
Source Map (JSON)
{"version":3,"sources":["../src/sqlite-core/builder.ts","../src/sqlite-core/index.ts"],"sourcesContent":["import { createId } 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 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\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 // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any\n this.config as ColumnBuilderRuntimeConfig<any, any>\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 = () => createId();\n this.config.hasDefault = true;\n return this as HasDefault<this>;\n }\n}\n\nexport class SQLiteCuid2<\n T extends ColumnBaseConfig<'string', 'SQLiteCuid2'>\n> extends SQLiteColumn<T> {\n static readonly [entityKind]: string = 'SQLiteCuid2';\n\n getSQLType(): string {\n return `text(24)`;\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,gBAAgB;AACzB;AAAA,EAEE;AAAA,OAKK;AACP;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AAcA,IAAM,qBAAN,cAEG,oBAAuB;AAAA,EAC/B,QAAiB,UAAU,IAAY;AAAA,EAEvC,YAAY,MAAc;AACxB,UAAM,MAAM,UAAU,aAAa;AAAA,EACrC;AAAA,EAEA,MACE,OAC8C;AAC9C,WAAO,IAAI;AAAA,MACT;AAAA;AAAA,MAEA,KAAK;AAAA,IACP;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,gBAAkC;AAChC,SAAK,OAAO,YAAY,MAAM,SAAS;AACvC,SAAK,OAAO,aAAa;AACzB,WAAO;AAAA,EACT;AACF;AAEO,IAAM,cAAN,cAEG,aAAgB;AAAA,EACxB,QAAiB,UAAU,IAAY;AAAA,EAEvC,aAAqB;AACnB,WAAO;AAAA,EACT;AACF;;;AC3DO,SAAS,MACd,MACkC;AAClC,SAAO,IAAI,mBAAmB,QAAS,EAAY;AACrD;","names":[]}