drizzle-cuid2
Version:
A utility package for generating CUID2 columns in Drizzle ORM
80 lines (78 loc) • 1.88 kB
JavaScript
// 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;
prefix;
constructor(name) {
super(name, "string", "PgCuid2");
}
build(table) {
return new PgCuid2(
table,
this.config,
this.length,
this.prefix
);
}
/***
* 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 = () => {
const id = createId(this.length)();
return this.prefix ? `${this.prefix}${id}` : id;
};
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;
}
/***
* Sets the prefix for the CUID2 value.
* @param prefix The prefix to prepend to the CUID2 value
*/
setPrefix(prefix) {
this.prefix = prefix;
return this;
}
};
var PgCuid2 = class extends PgColumn {
static [entityKind] = "PgCuid2";
length;
prefix;
constructor(table, config, length, prefix) {
super(table, config);
this.length = length;
this.prefix = prefix;
}
getSQLType() {
const totalLength = this.prefix ? this.length + this.prefix.length : this.length;
return `varchar(${totalLength})`;
}
};
// src/pg-core/index.ts
function cuid2(name) {
return new PgCuid2Builder(name ?? "");
}
export {
PgCuid2Builder,
PgCuid2,
cuid2
};
//# sourceMappingURL=chunk-Y32T5D44.mjs.map