drizzle-cuid2
Version:
A utility package for generating CUID2 columns in Drizzle ORM
205 lines (198 loc) • 5.94 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/index.ts
var src_exports = {};
__export(src_exports, {
MySqlCuid2: () => MySqlCuid2,
MySqlCuid2Builder: () => MySqlCuid2Builder,
PgCuid2: () => PgCuid2,
PgCuid2Builder: () => PgCuid2Builder,
SQLiteCuid2: () => SQLiteCuid2,
SQLiteCuid2Builder: () => SQLiteCuid2Builder,
mysqlCuid2: () => cuid2,
pgCuid2: () => cuid22,
sqliteCuid2: () => cuid23
});
module.exports = __toCommonJS(src_exports);
// src/mysql-core/builder.ts
var import_cuid2 = require("@paralleldrive/cuid2");
var import_drizzle_orm = require("drizzle-orm");
var import_mysql_core = require("drizzle-orm/mysql-core");
var createId = (length) => (0, import_cuid2.init)({ length });
var MySqlCuid2Builder = class extends import_mysql_core.MySqlColumnBuilder {
static [import_drizzle_orm.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 import_mysql_core.MySqlColumn {
static [import_drizzle_orm.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 ?? "");
}
// src/pg-core/builder.ts
var import_cuid22 = require("@paralleldrive/cuid2");
var import_drizzle_orm2 = require("drizzle-orm");
var import_pg_core = require("drizzle-orm/pg-core");
var createId2 = (length) => (0, import_cuid22.init)({ length });
var PgCuid2Builder = class extends import_pg_core.PgColumnBuilder {
static [import_drizzle_orm2.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 = () => createId2(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 import_pg_core.PgColumn {
static [import_drizzle_orm2.entityKind] = "PgCuid2";
length;
constructor(table, config, length) {
super(table, config);
this.length = length;
}
getSQLType() {
return `varchar(${this.length})`;
}
};
// src/pg-core/index.ts
function cuid22(name) {
return new PgCuid2Builder(name ?? "");
}
// src/sqlite-core/builder.ts
var import_cuid23 = require("@paralleldrive/cuid2");
var import_drizzle_orm3 = require("drizzle-orm");
var import_sqlite_core = require("drizzle-orm/sqlite-core");
var createId3 = (length) => (0, import_cuid23.init)({ length });
var SQLiteCuid2Builder = class extends import_sqlite_core.SQLiteColumnBuilder {
static [import_drizzle_orm3.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 = () => createId3(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 import_sqlite_core.SQLiteColumn {
static [import_drizzle_orm3.entityKind] = "SQLiteCuid2";
length;
constructor(table, config, length) {
super(table, config);
this.length = length;
}
getSQLType() {
return `text(${this.length})`;
}
};
// src/sqlite-core/index.ts
function cuid23(name) {
return new SQLiteCuid2Builder(name ?? "");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
MySqlCuid2,
MySqlCuid2Builder,
PgCuid2,
PgCuid2Builder,
SQLiteCuid2,
SQLiteCuid2Builder,
mysqlCuid2,
pgCuid2,
sqliteCuid2
});
//# sourceMappingURL=index.js.map