UNPKG

drizzle-cuid2

Version:

A utility package for generating CUID2 columns in Drizzle ORM

64 lines (62 loc) 1.5 kB
// src/mysql-core/builder.ts import { init } from "@paralleldrive/cuid2"; import { entityKind } from "drizzle-orm"; import { MySqlColumn, MySqlColumnBuilder } from "drizzle-orm/mysql-core"; var createId = (length) => init({ length }); var MySqlCuid2Builder = class extends MySqlColumnBuilder { static [entityKind] = "MySqlCuid2Builder"; length = 24; constructor(name) { super(name, "string", "MySqlCuid2"); } build(table) { return new MySqlCuid2( 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 MySqlCuid2 = class extends MySqlColumn { static [entityKind] = "MySqlCuid2"; length; constructor(table, config, length) { super(table, config); this.length = length; } getSQLType() { return `varchar(${this.length})`; } }; // src/mysql-core/index.ts function cuid2(name) { return new MySqlCuid2Builder(name ?? ""); } export { MySqlCuid2Builder, MySqlCuid2, cuid2 }; //# sourceMappingURL=chunk-D4VYKPMO.mjs.map