UNPKG

drizzle-cuid2

Version:

A utility package for generating CUID2 columns in Drizzle ORM

64 lines (62 loc) 1.46 kB
// src/pg-core/builder.ts import { init } from "@paralleldrive/cuid2"; import { entityKind } from "drizzle-orm"; import { PgColumn, PgColumnBuilder } from "drizzle-orm/pg-core"; var createId = (length) => init({ length }); var PgCuid2Builder = class extends PgColumnBuilder { static [entityKind] = "PgCuid2Builder"; length = 24; constructor(name) { super(name, "string", "PgCuid2"); } build(table) { return new PgCuid2( table, this.config, this.length ); } /*** * Creates a random `cuid2` value as the default value for the column. * The function will be called when the row is inserted, and the returned value will be used as the column value. */ defaultRandom() { this.config.defaultFn = () => createId(this.length)(); this.config.hasDefault = true; return this; } /*** * Sets the length of the CUID2 value. * @param length The length of the CUID2 value (default: 24) */ setLength(length) { this.length = length; return this; } }; var PgCuid2 = class extends PgColumn { static [entityKind] = "PgCuid2"; length; constructor(table, config, length) { super(table, config); this.length = length; } getSQLType() { return `varchar(${this.length})`; } }; // src/pg-core/index.ts function cuid2(name) { return new PgCuid2Builder(name ?? ""); } export { PgCuid2Builder, PgCuid2, cuid2 }; //# sourceMappingURL=chunk-NBM7R2QZ.mjs.map