drizzle-cuid2
Version:
A utility package for generating CUID2 columns in Drizzle ORM
103 lines (100 loc) • 3.15 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/sqlite-core/index.ts
var sqlite_core_exports = {};
__export(sqlite_core_exports, {
SQLiteCuid2: () => SQLiteCuid2,
SQLiteCuid2Builder: () => SQLiteCuid2Builder,
cuid2: () => cuid2
});
module.exports = __toCommonJS(sqlite_core_exports);
// src/sqlite-core/builder.ts
var import_cuid2 = require("@paralleldrive/cuid2");
var import_drizzle_orm = require("drizzle-orm");
var import_sqlite_core = require("drizzle-orm/sqlite-core");
var createId = (length) => (0, import_cuid2.init)({ length });
var SQLiteCuid2Builder = class extends import_sqlite_core.SQLiteColumnBuilder {
static [import_drizzle_orm.entityKind] = "SQLiteCuid2Builder";
length = 24;
prefix;
constructor(name) {
super(name, "string", "SQLiteCuid2");
}
build(table) {
return new SQLiteCuid2(
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 SQLiteCuid2 = class extends import_sqlite_core.SQLiteColumn {
static [import_drizzle_orm.entityKind] = "SQLiteCuid2";
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 `text(${totalLength})`;
}
};
// src/sqlite-core/index.ts
function cuid2(name) {
return new SQLiteCuid2Builder(name ?? "");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
SQLiteCuid2,
SQLiteCuid2Builder,
cuid2
});
//# sourceMappingURL=index.js.map