drizzle-cuid2
Version:
A utility package for generating CUID2 columns in Drizzle ORM
64 lines (62 loc) • 1.52 kB
JavaScript
// src/sqlite-core/builder.ts
import { init } from "@paralleldrive/cuid2";
import {
entityKind
} from "drizzle-orm";
import {
SQLiteColumn,
SQLiteColumnBuilder
} from "drizzle-orm/sqlite-core";
var createId = (length) => init({ length });
var SQLiteCuid2Builder = class extends SQLiteColumnBuilder {
static [entityKind] = "SQLiteCuid2Builder";
length = 24;
constructor(name) {
super(name, "string", "SQLiteCuid2");
}
build(table) {
return new SQLiteCuid2(
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 SQLiteCuid2 = class extends SQLiteColumn {
static [entityKind] = "SQLiteCuid2";
length;
constructor(table, config, length) {
super(table, config);
this.length = length;
}
getSQLType() {
return `text(${this.length})`;
}
};
// src/sqlite-core/index.ts
function cuid2(name) {
return new SQLiteCuid2Builder(name ?? "");
}
export {
SQLiteCuid2Builder,
SQLiteCuid2,
cuid2
};
//# sourceMappingURL=chunk-B7WJQXIV.mjs.map