arkormx 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.mjs +281 -7
- package/dist/index.cjs +288 -7
- package/dist/index.d.cts +108 -2
- package/dist/index.d.mts +108 -2
- package/dist/index.mjs +282 -8
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -238,6 +238,95 @@ var ForeignKeyBuilder = class {
|
|
|
238
238
|
|
|
239
239
|
//#endregion
|
|
240
240
|
//#region src/database/TableBuilder.ts
|
|
241
|
+
const PRISMA_ENUM_MEMBER_REGEX$1 = /^[A-Za-z][A-Za-z0-9_]*$/;
|
|
242
|
+
const normalizeEnumMember = (columnName, value) => {
|
|
243
|
+
const normalized = value.trim();
|
|
244
|
+
if (!normalized) throw new Error(`Enum column [${columnName}] must define only non-empty values.`);
|
|
245
|
+
if (!PRISMA_ENUM_MEMBER_REGEX$1.test(normalized)) throw new Error(`Enum column [${columnName}] contains invalid Prisma enum value [${normalized}].`);
|
|
246
|
+
return normalized;
|
|
247
|
+
};
|
|
248
|
+
const normalizeEnumMembers = (columnName, values) => {
|
|
249
|
+
const normalizedValues = values.map((value) => normalizeEnumMember(columnName, value));
|
|
250
|
+
const seen = /* @__PURE__ */ new Set();
|
|
251
|
+
for (const value of normalizedValues) {
|
|
252
|
+
if (seen.has(value)) throw new Error(`Enum column [${columnName}] contains duplicate enum value [${value}].`);
|
|
253
|
+
seen.add(value);
|
|
254
|
+
}
|
|
255
|
+
return normalizedValues;
|
|
256
|
+
};
|
|
257
|
+
/**
|
|
258
|
+
* The EnumBuilder class provides a fluent interface for configuring enum columns
|
|
259
|
+
* after they are defined on a table.
|
|
260
|
+
*
|
|
261
|
+
* @author Legacy (3m1n3nc3)
|
|
262
|
+
* @since 0.2.3
|
|
263
|
+
*/
|
|
264
|
+
var EnumBuilder = class {
|
|
265
|
+
tableBuilder;
|
|
266
|
+
columnName;
|
|
267
|
+
constructor(tableBuilder, columnName) {
|
|
268
|
+
this.tableBuilder = tableBuilder;
|
|
269
|
+
this.columnName = columnName;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Defines the Prisma enum name for this column.
|
|
273
|
+
*
|
|
274
|
+
* @param name
|
|
275
|
+
* @returns
|
|
276
|
+
*/
|
|
277
|
+
enumName(name) {
|
|
278
|
+
this.tableBuilder.enumName(name, this.columnName);
|
|
279
|
+
return this;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Marks the enum column as nullable.
|
|
283
|
+
*
|
|
284
|
+
* @returns
|
|
285
|
+
*/
|
|
286
|
+
nullable() {
|
|
287
|
+
this.tableBuilder.nullable(this.columnName);
|
|
288
|
+
return this;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Marks the enum column as unique.
|
|
292
|
+
*
|
|
293
|
+
* @returns
|
|
294
|
+
*/
|
|
295
|
+
unique() {
|
|
296
|
+
this.tableBuilder.unique(this.columnName);
|
|
297
|
+
return this;
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Sets a default value for the enum column.
|
|
301
|
+
*
|
|
302
|
+
* @param value
|
|
303
|
+
* @returns
|
|
304
|
+
*/
|
|
305
|
+
default(value) {
|
|
306
|
+
this.tableBuilder.default(value, this.columnName);
|
|
307
|
+
return this;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Positions the enum column after another column when supported.
|
|
311
|
+
*
|
|
312
|
+
* @param referenceColumn
|
|
313
|
+
* @returns
|
|
314
|
+
*/
|
|
315
|
+
after(referenceColumn) {
|
|
316
|
+
this.tableBuilder.after(referenceColumn, this.columnName);
|
|
317
|
+
return this;
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Maps the enum column to a custom database column name.
|
|
321
|
+
*
|
|
322
|
+
* @param name
|
|
323
|
+
* @returns
|
|
324
|
+
*/
|
|
325
|
+
map(name) {
|
|
326
|
+
this.tableBuilder.map(name, this.columnName);
|
|
327
|
+
return this;
|
|
328
|
+
}
|
|
329
|
+
};
|
|
241
330
|
/**
|
|
242
331
|
* The TableBuilder class provides a fluent interface for defining
|
|
243
332
|
* the structure of a database table in a migration, including columns to add or drop.
|
|
@@ -290,6 +379,27 @@ var TableBuilder = class {
|
|
|
290
379
|
return this.column(name, "uuid", options);
|
|
291
380
|
}
|
|
292
381
|
/**
|
|
382
|
+
* Defines an enum column in the table.
|
|
383
|
+
*
|
|
384
|
+
* @param name The name of the enum column.
|
|
385
|
+
* @param values Either an array of string values for the enum or the name of an existing enum to reuse.
|
|
386
|
+
* @param options Additional options for the enum column.
|
|
387
|
+
* @returns
|
|
388
|
+
*/
|
|
389
|
+
enum(name, valuesOrEnumName, options = {}) {
|
|
390
|
+
const isEnumReuse = typeof valuesOrEnumName === "string";
|
|
391
|
+
if (!isEnumReuse && valuesOrEnumName.length === 0) throw new Error(`Enum column [${name}] must define at least one value.`);
|
|
392
|
+
const normalizedEnumValues = isEnumReuse ? void 0 : normalizeEnumMembers(name, valuesOrEnumName);
|
|
393
|
+
const enumName = isEnumReuse ? valuesOrEnumName.trim() : options.enumName?.trim();
|
|
394
|
+
if (isEnumReuse && !enumName) throw new Error(`Enum column [${name}] must define an enum name.`);
|
|
395
|
+
this.column(name, "enum", {
|
|
396
|
+
...options,
|
|
397
|
+
enumName,
|
|
398
|
+
enumValues: normalizedEnumValues
|
|
399
|
+
});
|
|
400
|
+
return new EnumBuilder(this, name);
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
293
403
|
* Defines a string column in the table.
|
|
294
404
|
*
|
|
295
405
|
* @param name The name of the string column.
|
|
@@ -459,6 +569,21 @@ var TableBuilder = class {
|
|
|
459
569
|
return this;
|
|
460
570
|
}
|
|
461
571
|
/**
|
|
572
|
+
* Sets the Prisma enum name for an enum column.
|
|
573
|
+
*
|
|
574
|
+
* @param name The enum name to assign.
|
|
575
|
+
* @param columnName Optional explicit target column name. When omitted, applies to the latest defined column.
|
|
576
|
+
* @returns The current TableBuilder instance for chaining.
|
|
577
|
+
*/
|
|
578
|
+
enumName(name, columnName) {
|
|
579
|
+
const column = this.resolveColumn(columnName);
|
|
580
|
+
if (column.type !== "enum") throw new Error(`Column [${column.name}] is not an enum column.`);
|
|
581
|
+
const enumName = name.trim();
|
|
582
|
+
if (!enumName) throw new Error(`Enum column [${column.name}] must define an enum name.`);
|
|
583
|
+
column.enumName = enumName;
|
|
584
|
+
return this;
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
462
587
|
* Sets a default value for a column.
|
|
463
588
|
*
|
|
464
589
|
* @param value The default scalar value or Prisma expression (e.g. 'now()').
|
|
@@ -541,7 +666,10 @@ var TableBuilder = class {
|
|
|
541
666
|
* @returns
|
|
542
667
|
*/
|
|
543
668
|
getColumns() {
|
|
544
|
-
return this.columns.map((column) => ({
|
|
669
|
+
return this.columns.map((column) => ({
|
|
670
|
+
...column,
|
|
671
|
+
enumValues: column.enumValues ? [...column.enumValues] : void 0
|
|
672
|
+
}));
|
|
545
673
|
}
|
|
546
674
|
/**
|
|
547
675
|
* Returns a copy of the defined column names to be dropped from the table.
|
|
@@ -582,6 +710,8 @@ var TableBuilder = class {
|
|
|
582
710
|
this.columns.push({
|
|
583
711
|
name,
|
|
584
712
|
type,
|
|
713
|
+
enumName: options.enumName,
|
|
714
|
+
enumValues: options.enumValues ? [...options.enumValues] : void 0,
|
|
585
715
|
map: options.map,
|
|
586
716
|
nullable: options.nullable,
|
|
587
717
|
unique: options.unique,
|
|
@@ -681,7 +811,10 @@ var SchemaBuilder = class {
|
|
|
681
811
|
return this.operations.map((operation) => {
|
|
682
812
|
if (operation.type === "createTable") return {
|
|
683
813
|
...operation,
|
|
684
|
-
columns: operation.columns.map((column) => ({
|
|
814
|
+
columns: operation.columns.map((column) => ({
|
|
815
|
+
...column,
|
|
816
|
+
enumValues: column.enumValues ? [...column.enumValues] : void 0
|
|
817
|
+
})),
|
|
685
818
|
indexes: operation.indexes.map((index) => ({
|
|
686
819
|
...index,
|
|
687
820
|
columns: [...index.columns]
|
|
@@ -690,7 +823,10 @@ var SchemaBuilder = class {
|
|
|
690
823
|
};
|
|
691
824
|
if (operation.type === "alterTable") return {
|
|
692
825
|
...operation,
|
|
693
|
-
addColumns: operation.addColumns.map((column) => ({
|
|
826
|
+
addColumns: operation.addColumns.map((column) => ({
|
|
827
|
+
...column,
|
|
828
|
+
enumValues: column.enumValues ? [...column.enumValues] : void 0
|
|
829
|
+
})),
|
|
694
830
|
dropColumns: [...operation.dropColumns],
|
|
695
831
|
addIndexes: operation.addIndexes.map((index) => ({
|
|
696
832
|
...index,
|
|
@@ -706,6 +842,8 @@ var SchemaBuilder = class {
|
|
|
706
842
|
//#endregion
|
|
707
843
|
//#region src/helpers/migrations.ts
|
|
708
844
|
const PRISMA_MODEL_REGEX = /model\s+(\w+)\s*\{[\s\S]*?\n\}/g;
|
|
845
|
+
const PRISMA_ENUM_REGEX = /enum\s+(\w+)\s*\{[\s\S]*?\n\}/g;
|
|
846
|
+
const PRISMA_ENUM_MEMBER_REGEX = /^[A-Za-z][A-Za-z0-9_]*$/;
|
|
709
847
|
/**
|
|
710
848
|
* Convert a table name to a PascalCase model name, with basic singularization.
|
|
711
849
|
*
|
|
@@ -733,6 +871,7 @@ const escapeRegex = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
|
733
871
|
*/
|
|
734
872
|
const resolvePrismaType = (column) => {
|
|
735
873
|
if (column.type === "id") return "Int";
|
|
874
|
+
if (column.type === "enum") return resolveEnumName(column);
|
|
736
875
|
if (column.type === "uuid") return "String";
|
|
737
876
|
if (column.type === "string" || column.type === "text") return "String";
|
|
738
877
|
if (column.type === "integer") return "Int";
|
|
@@ -742,6 +881,11 @@ const resolvePrismaType = (column) => {
|
|
|
742
881
|
if (column.type === "json") return "Json";
|
|
743
882
|
return "DateTime";
|
|
744
883
|
};
|
|
884
|
+
const resolveEnumName = (column) => {
|
|
885
|
+
if (column.type !== "enum") throw new ArkormException(`Column [${column.name}] is not an enum column.`);
|
|
886
|
+
if (column.enumName && column.enumName.trim().length > 0) return column.enumName.trim();
|
|
887
|
+
throw new ArkormException(`Enum column [${column.name}] must define an enum name.`);
|
|
888
|
+
};
|
|
745
889
|
/**
|
|
746
890
|
* Format a default value for inclusion in a Prisma schema field definition, based on its type.
|
|
747
891
|
*
|
|
@@ -756,6 +900,61 @@ const formatDefaultValue = (value) => {
|
|
|
756
900
|
if (typeof value === "boolean") return `@default(${value ? "true" : "false"})`;
|
|
757
901
|
};
|
|
758
902
|
/**
|
|
903
|
+
* Format a default value for an enum column as a Prisma @default attribute, validating that it is a non-empty string.
|
|
904
|
+
*
|
|
905
|
+
* @param value
|
|
906
|
+
* @returns
|
|
907
|
+
*/
|
|
908
|
+
const formatEnumDefaultValue = (value) => {
|
|
909
|
+
if (value == null) return void 0;
|
|
910
|
+
if (typeof value !== "string" || value.trim().length === 0) throw new ArkormException("Enum default values must be provided as non-empty strings.");
|
|
911
|
+
return `@default(${value.trim()})`;
|
|
912
|
+
};
|
|
913
|
+
/**
|
|
914
|
+
* Normalize an enum value by ensuring it is a non-empty string and trimming whitespace.
|
|
915
|
+
*
|
|
916
|
+
* @param value
|
|
917
|
+
* @returns
|
|
918
|
+
*/
|
|
919
|
+
const normalizeEnumValue = (value) => {
|
|
920
|
+
if (typeof value !== "string" || value.trim().length === 0) throw new ArkormException("Enum values must be provided as non-empty strings.");
|
|
921
|
+
const normalized = value.trim();
|
|
922
|
+
if (!PRISMA_ENUM_MEMBER_REGEX.test(normalized)) throw new ArkormException(`Enum value [${normalized}] is not a valid Prisma enum member name.`);
|
|
923
|
+
return normalized;
|
|
924
|
+
};
|
|
925
|
+
/**
|
|
926
|
+
* Extract the enum values from a Prisma enum block string.
|
|
927
|
+
*
|
|
928
|
+
* @param block
|
|
929
|
+
* @returns
|
|
930
|
+
*/
|
|
931
|
+
const extractEnumBlockValues = (block) => {
|
|
932
|
+
return block.split("\n").slice(1, -1).map((line) => line.trim()).filter(Boolean);
|
|
933
|
+
};
|
|
934
|
+
const validateEnumValues = (column, enumName, enumValues) => {
|
|
935
|
+
const normalizedValues = enumValues.map(normalizeEnumValue);
|
|
936
|
+
const seen = /* @__PURE__ */ new Set();
|
|
937
|
+
for (const value of normalizedValues) {
|
|
938
|
+
if (seen.has(value)) throw new ArkormException(`Prisma enum [${enumName}] for column [${column.name}] contains duplicate value [${value}].`);
|
|
939
|
+
seen.add(value);
|
|
940
|
+
}
|
|
941
|
+
return normalizedValues;
|
|
942
|
+
};
|
|
943
|
+
/**
|
|
944
|
+
* Validate that a default value for an enum column is included in the defined enum values.
|
|
945
|
+
*
|
|
946
|
+
* @param column
|
|
947
|
+
* @param enumName
|
|
948
|
+
* @param enumValues
|
|
949
|
+
* @returns
|
|
950
|
+
*/
|
|
951
|
+
const validateEnumDefaultValue = (column, enumName, enumValues) => {
|
|
952
|
+
if (column.default == null) return;
|
|
953
|
+
const normalizedDefault = normalizeEnumValue(column.default);
|
|
954
|
+
if (enumValues.includes(normalizedDefault)) return;
|
|
955
|
+
throw new ArkormException(`Enum default value [${normalizedDefault}] is not defined in Prisma enum [${enumName}] for column [${column.name}].`);
|
|
956
|
+
};
|
|
957
|
+
/**
|
|
759
958
|
* Build a single line of a Prisma model field definition based on a SchemaColumn, including type and modifiers.
|
|
760
959
|
*
|
|
761
960
|
* @param column
|
|
@@ -776,11 +975,82 @@ const buildFieldLine = (column) => {
|
|
|
776
975
|
const primary = column.primary ? " @id" : "";
|
|
777
976
|
const mapped = typeof column.map === "string" && column.map.trim().length > 0 ? ` @map("${column.map.replace(/"/g, "\\\"")}")` : "";
|
|
778
977
|
const updatedAt = column.updatedAt ? " @updatedAt" : "";
|
|
779
|
-
const defaultValue = formatDefaultValue(column.default) ?? (column.type === "uuid" && column.primary ? "@default(uuid())" : void 0);
|
|
978
|
+
const defaultValue = column.type === "enum" ? formatEnumDefaultValue(column.default) : formatDefaultValue(column.default) ?? (column.type === "uuid" && column.primary ? "@default(uuid())" : void 0);
|
|
780
979
|
const defaultSuffix = defaultValue ? ` ${defaultValue}` : "";
|
|
781
980
|
return ` ${column.name} ${scalar}${nullable}${primary}${unique}${defaultSuffix}${updatedAt}${mapped}`;
|
|
782
981
|
};
|
|
783
982
|
/**
|
|
983
|
+
* Build a Prisma enum block string based on an enum name and its values, validating that
|
|
984
|
+
* at least one value is provided.
|
|
985
|
+
*
|
|
986
|
+
* @param enumName The name of the enum to create.
|
|
987
|
+
* @param values The array of values for the enum.
|
|
988
|
+
* @returns The Prisma enum block string.
|
|
989
|
+
*/
|
|
990
|
+
const buildEnumBlock = (enumName, values) => {
|
|
991
|
+
if (values.length === 0) throw new ArkormException(`Enum [${enumName}] must define at least one value.`);
|
|
992
|
+
return `enum ${enumName} {\n${values.map((value) => ` ${value}`).join("\n")}\n}`;
|
|
993
|
+
};
|
|
994
|
+
/**
|
|
995
|
+
* Find the Prisma enum block in a schema string that corresponds to a given enum
|
|
996
|
+
* name, returning its details if found.
|
|
997
|
+
*
|
|
998
|
+
* @param schema The Prisma schema string to search for the enum block.
|
|
999
|
+
* @param enumName The name of the enum to find in the schema.
|
|
1000
|
+
* @returns
|
|
1001
|
+
*/
|
|
1002
|
+
const findEnumBlock = (schema, enumName) => {
|
|
1003
|
+
const candidates = [...schema.matchAll(PRISMA_ENUM_REGEX)];
|
|
1004
|
+
for (const match of candidates) {
|
|
1005
|
+
const block = match[0];
|
|
1006
|
+
const matchedEnumName = match[1];
|
|
1007
|
+
const start = match.index ?? 0;
|
|
1008
|
+
const end = start + block.length;
|
|
1009
|
+
if (matchedEnumName === enumName) return {
|
|
1010
|
+
enumName: matchedEnumName,
|
|
1011
|
+
block,
|
|
1012
|
+
start,
|
|
1013
|
+
end
|
|
1014
|
+
};
|
|
1015
|
+
}
|
|
1016
|
+
return null;
|
|
1017
|
+
};
|
|
1018
|
+
/**
|
|
1019
|
+
* Ensure that Prisma enum blocks exist in the schema for any enum columns defined in a
|
|
1020
|
+
* create or alter table operation, adding them if necessary and validating against
|
|
1021
|
+
* existing blocks.
|
|
1022
|
+
*
|
|
1023
|
+
* @param schema The current Prisma schema string to check and modify.
|
|
1024
|
+
* @param columns The array of schema column definitions to check for enum types and ensure corresponding blocks exist for.
|
|
1025
|
+
* @returns
|
|
1026
|
+
*/
|
|
1027
|
+
const ensureEnumBlocks = (schema, columns) => {
|
|
1028
|
+
let nextSchema = schema;
|
|
1029
|
+
for (const column of columns) {
|
|
1030
|
+
if (column.type !== "enum") continue;
|
|
1031
|
+
const enumName = resolveEnumName(column);
|
|
1032
|
+
const enumValues = column.enumValues ?? [];
|
|
1033
|
+
const existing = findEnumBlock(nextSchema, enumName);
|
|
1034
|
+
if (existing) {
|
|
1035
|
+
const existingValues = validateEnumValues(column, enumName, extractEnumBlockValues(existing.block));
|
|
1036
|
+
if (enumValues.length === 0) {
|
|
1037
|
+
validateEnumDefaultValue(column, enumName, existingValues);
|
|
1038
|
+
continue;
|
|
1039
|
+
}
|
|
1040
|
+
const normalizedEnumValues = validateEnumValues(column, enumName, enumValues);
|
|
1041
|
+
if (existingValues.join("|") !== normalizedEnumValues.join("|")) throw new ArkormException(`Prisma enum [${enumName}] already exists with different values.`);
|
|
1042
|
+
validateEnumDefaultValue(column, enumName, existingValues);
|
|
1043
|
+
continue;
|
|
1044
|
+
}
|
|
1045
|
+
if (enumValues.length === 0) throw new ArkormException(`Prisma enum [${enumName}] was not found for column [${column.name}].`);
|
|
1046
|
+
const normalizedEnumValues = validateEnumValues(column, enumName, enumValues);
|
|
1047
|
+
validateEnumDefaultValue(column, enumName, normalizedEnumValues);
|
|
1048
|
+
const block = buildEnumBlock(enumName, normalizedEnumValues);
|
|
1049
|
+
nextSchema = `${nextSchema.trimEnd()}\n\n${block}\n`;
|
|
1050
|
+
}
|
|
1051
|
+
return nextSchema;
|
|
1052
|
+
};
|
|
1053
|
+
/**
|
|
784
1054
|
* Build a Prisma model-level @@index definition line.
|
|
785
1055
|
*
|
|
786
1056
|
* @param index The schema index definition to convert to a Prisma \@\@index line.
|
|
@@ -982,8 +1252,9 @@ const findModelBlock = (schema, table) => {
|
|
|
982
1252
|
*/
|
|
983
1253
|
const applyCreateTableOperation = (schema, operation) => {
|
|
984
1254
|
if (findModelBlock(schema, operation.table)) throw new ArkormException(`Prisma model for table [${operation.table}] already exists.`);
|
|
1255
|
+
const schemaWithEnums = ensureEnumBlocks(schema, operation.columns);
|
|
985
1256
|
const block = buildModelBlock(operation);
|
|
986
|
-
return applyInverseRelations(`${
|
|
1257
|
+
return applyInverseRelations(`${schemaWithEnums.trimEnd()}\n\n${block}\n`, toModelName(operation.table), operation.foreignKeys ?? []);
|
|
987
1258
|
};
|
|
988
1259
|
/**
|
|
989
1260
|
* Apply an alter table operation to a Prisma schema string, modifying the model
|
|
@@ -996,7 +1267,10 @@ const applyCreateTableOperation = (schema, operation) => {
|
|
|
996
1267
|
const applyAlterTableOperation = (schema, operation) => {
|
|
997
1268
|
const model = findModelBlock(schema, operation.table);
|
|
998
1269
|
if (!model) throw new ArkormException(`Prisma model for table [${operation.table}] was not found.`);
|
|
999
|
-
|
|
1270
|
+
const schemaWithEnums = ensureEnumBlocks(schema, operation.addColumns);
|
|
1271
|
+
const refreshedModel = findModelBlock(schemaWithEnums, operation.table);
|
|
1272
|
+
if (!refreshedModel) throw new ArkormException(`Prisma model for table [${operation.table}] was not found.`);
|
|
1273
|
+
let block = refreshedModel.block;
|
|
1000
1274
|
const bodyLines = block.split("\n");
|
|
1001
1275
|
operation.dropColumns.forEach((column) => {
|
|
1002
1276
|
const columnRegex = new RegExp(`^\\s*${escapeRegex(column)}\\s+`);
|
|
@@ -1026,7 +1300,7 @@ const applyAlterTableOperation = (schema, operation) => {
|
|
|
1026
1300
|
injectLineIntoModelBody(bodyLines, relationLine, (line) => relationRegex.test(line));
|
|
1027
1301
|
}
|
|
1028
1302
|
block = bodyLines.join("\n");
|
|
1029
|
-
return applyInverseRelations(`${
|
|
1303
|
+
return applyInverseRelations(`${schemaWithEnums.slice(0, refreshedModel.start)}${block}${schemaWithEnums.slice(refreshedModel.end)}`, model.modelName, operation.addForeignKeys ?? []);
|
|
1030
1304
|
};
|
|
1031
1305
|
/**
|
|
1032
1306
|
* Apply a drop table operation to a Prisma schema string, removing the model block
|
|
@@ -6135,6 +6409,7 @@ exports.ArkormCollection = ArkormCollection;
|
|
|
6135
6409
|
exports.ArkormException = ArkormException;
|
|
6136
6410
|
exports.Attribute = Attribute;
|
|
6137
6411
|
exports.CliApp = CliApp;
|
|
6412
|
+
exports.EnumBuilder = EnumBuilder;
|
|
6138
6413
|
exports.ForeignKeyBuilder = ForeignKeyBuilder;
|
|
6139
6414
|
exports.InitCommand = InitCommand;
|
|
6140
6415
|
exports.InlineFactory = InlineFactory;
|
|
@@ -6153,6 +6428,8 @@ exports.Model = Model;
|
|
|
6153
6428
|
exports.ModelFactory = ModelFactory;
|
|
6154
6429
|
exports.ModelNotFoundException = ModelNotFoundException;
|
|
6155
6430
|
exports.ModelsSyncCommand = ModelsSyncCommand;
|
|
6431
|
+
exports.PRISMA_ENUM_MEMBER_REGEX = PRISMA_ENUM_MEMBER_REGEX;
|
|
6432
|
+
exports.PRISMA_ENUM_REGEX = PRISMA_ENUM_REGEX;
|
|
6156
6433
|
exports.PRISMA_MODEL_REGEX = PRISMA_MODEL_REGEX;
|
|
6157
6434
|
exports.Paginator = Paginator;
|
|
6158
6435
|
exports.QueryBuilder = QueryBuilder;
|
|
@@ -6173,6 +6450,7 @@ exports.applyDropTableOperation = applyDropTableOperation;
|
|
|
6173
6450
|
exports.applyMigrationRollbackToPrismaSchema = applyMigrationRollbackToPrismaSchema;
|
|
6174
6451
|
exports.applyMigrationToPrismaSchema = applyMigrationToPrismaSchema;
|
|
6175
6452
|
exports.applyOperationsToPrismaSchema = applyOperationsToPrismaSchema;
|
|
6453
|
+
exports.buildEnumBlock = buildEnumBlock;
|
|
6176
6454
|
exports.buildFieldLine = buildFieldLine;
|
|
6177
6455
|
exports.buildIndexLine = buildIndexLine;
|
|
6178
6456
|
exports.buildInverseRelationLine = buildInverseRelationLine;
|
|
@@ -6194,8 +6472,10 @@ exports.deriveRelationFieldName = deriveRelationFieldName;
|
|
|
6194
6472
|
exports.ensureArkormConfigLoading = ensureArkormConfigLoading;
|
|
6195
6473
|
exports.escapeRegex = escapeRegex;
|
|
6196
6474
|
exports.findAppliedMigration = findAppliedMigration;
|
|
6475
|
+
exports.findEnumBlock = findEnumBlock;
|
|
6197
6476
|
exports.findModelBlock = findModelBlock;
|
|
6198
6477
|
exports.formatDefaultValue = formatDefaultValue;
|
|
6478
|
+
exports.formatEnumDefaultValue = formatEnumDefaultValue;
|
|
6199
6479
|
exports.formatRelationAction = formatRelationAction;
|
|
6200
6480
|
exports.generateMigrationFile = generateMigrationFile;
|
|
6201
6481
|
exports.getActiveTransactionClient = getActiveTransactionClient;
|
|
@@ -6219,6 +6499,7 @@ exports.readAppliedMigrationsState = readAppliedMigrationsState;
|
|
|
6219
6499
|
exports.removeAppliedMigration = removeAppliedMigration;
|
|
6220
6500
|
exports.resetArkormRuntimeForTests = resetArkormRuntimeForTests;
|
|
6221
6501
|
exports.resolveCast = resolveCast;
|
|
6502
|
+
exports.resolveEnumName = resolveEnumName;
|
|
6222
6503
|
exports.resolveMigrationClassName = resolveMigrationClassName;
|
|
6223
6504
|
exports.resolveMigrationStateFilePath = resolveMigrationStateFilePath;
|
|
6224
6505
|
exports.resolvePrismaType = resolvePrismaType;
|
package/dist/index.d.cts
CHANGED
|
@@ -24,10 +24,12 @@ type FactoryDefinition<TAttributes extends FactoryAttributes> = (sequence: numbe
|
|
|
24
24
|
type FactoryState<TAttributes extends FactoryAttributes> = (attributes: TAttributes, sequence: number) => TAttributes;
|
|
25
25
|
//#endregion
|
|
26
26
|
//#region src/types/migrations.d.ts
|
|
27
|
-
type SchemaColumnType = 'id' | 'uuid' | 'string' | 'text' | 'integer' | 'bigInteger' | 'float' | 'boolean' | 'json' | 'date' | 'timestamp';
|
|
27
|
+
type SchemaColumnType = 'id' | 'uuid' | 'enum' | 'string' | 'text' | 'integer' | 'bigInteger' | 'float' | 'boolean' | 'json' | 'date' | 'timestamp';
|
|
28
28
|
interface SchemaColumn {
|
|
29
29
|
name: string;
|
|
30
30
|
type: SchemaColumnType;
|
|
31
|
+
enumName?: string;
|
|
32
|
+
enumValues?: string[];
|
|
31
33
|
map?: string;
|
|
32
34
|
nullable?: boolean;
|
|
33
35
|
unique?: boolean;
|
|
@@ -2670,6 +2672,58 @@ declare class ForeignKeyBuilder {
|
|
|
2670
2672
|
}
|
|
2671
2673
|
//#endregion
|
|
2672
2674
|
//#region src/database/TableBuilder.d.ts
|
|
2675
|
+
/**
|
|
2676
|
+
* The EnumBuilder class provides a fluent interface for configuring enum columns
|
|
2677
|
+
* after they are defined on a table.
|
|
2678
|
+
*
|
|
2679
|
+
* @author Legacy (3m1n3nc3)
|
|
2680
|
+
* @since 0.2.3
|
|
2681
|
+
*/
|
|
2682
|
+
declare class EnumBuilder {
|
|
2683
|
+
private readonly tableBuilder;
|
|
2684
|
+
private readonly columnName;
|
|
2685
|
+
constructor(tableBuilder: TableBuilder, columnName: string);
|
|
2686
|
+
/**
|
|
2687
|
+
* Defines the Prisma enum name for this column.
|
|
2688
|
+
*
|
|
2689
|
+
* @param name
|
|
2690
|
+
* @returns
|
|
2691
|
+
*/
|
|
2692
|
+
enumName(name: string): this;
|
|
2693
|
+
/**
|
|
2694
|
+
* Marks the enum column as nullable.
|
|
2695
|
+
*
|
|
2696
|
+
* @returns
|
|
2697
|
+
*/
|
|
2698
|
+
nullable(): this;
|
|
2699
|
+
/**
|
|
2700
|
+
* Marks the enum column as unique.
|
|
2701
|
+
*
|
|
2702
|
+
* @returns
|
|
2703
|
+
*/
|
|
2704
|
+
unique(): this;
|
|
2705
|
+
/**
|
|
2706
|
+
* Sets a default value for the enum column.
|
|
2707
|
+
*
|
|
2708
|
+
* @param value
|
|
2709
|
+
* @returns
|
|
2710
|
+
*/
|
|
2711
|
+
default(value: unknown): this;
|
|
2712
|
+
/**
|
|
2713
|
+
* Positions the enum column after another column when supported.
|
|
2714
|
+
*
|
|
2715
|
+
* @param referenceColumn
|
|
2716
|
+
* @returns
|
|
2717
|
+
*/
|
|
2718
|
+
after(referenceColumn: string): this;
|
|
2719
|
+
/**
|
|
2720
|
+
* Maps the enum column to a custom database column name.
|
|
2721
|
+
*
|
|
2722
|
+
* @param name
|
|
2723
|
+
* @returns
|
|
2724
|
+
*/
|
|
2725
|
+
map(name: string): this;
|
|
2726
|
+
}
|
|
2673
2727
|
/**
|
|
2674
2728
|
* The TableBuilder class provides a fluent interface for defining
|
|
2675
2729
|
* the structure of a database table in a migration, including columns to add or drop.
|
|
@@ -2714,6 +2768,17 @@ declare class TableBuilder {
|
|
|
2714
2768
|
* @returns The current TableBuilder instance for chaining.
|
|
2715
2769
|
*/
|
|
2716
2770
|
uuid(name: string, options?: Partial<SchemaColumn>): this;
|
|
2771
|
+
/**
|
|
2772
|
+
* Defines an enum column in the table.
|
|
2773
|
+
*
|
|
2774
|
+
* @param name The name of the enum column.
|
|
2775
|
+
* @param values Either an array of string values for the enum or the name of an existing enum to reuse.
|
|
2776
|
+
* @param options Additional options for the enum column.
|
|
2777
|
+
* @returns
|
|
2778
|
+
*/
|
|
2779
|
+
enum(name: string, valuesOrEnumName: string[] | string, options?: Partial<SchemaColumn> & {
|
|
2780
|
+
enumName?: string;
|
|
2781
|
+
}): EnumBuilder;
|
|
2717
2782
|
/**
|
|
2718
2783
|
* Defines a string column in the table.
|
|
2719
2784
|
*
|
|
@@ -2835,6 +2900,14 @@ declare class TableBuilder {
|
|
|
2835
2900
|
* @returns The current TableBuilder instance for chaining.
|
|
2836
2901
|
*/
|
|
2837
2902
|
nullable(columnName?: string): this;
|
|
2903
|
+
/**
|
|
2904
|
+
* Sets the Prisma enum name for an enum column.
|
|
2905
|
+
*
|
|
2906
|
+
* @param name The enum name to assign.
|
|
2907
|
+
* @param columnName Optional explicit target column name. When omitted, applies to the latest defined column.
|
|
2908
|
+
* @returns The current TableBuilder instance for chaining.
|
|
2909
|
+
*/
|
|
2910
|
+
enumName(name: string, columnName?: string): this;
|
|
2838
2911
|
/**
|
|
2839
2912
|
* Sets a default value for a column.
|
|
2840
2913
|
*
|
|
@@ -3124,6 +3197,8 @@ declare const getLatestAppliedMigrations: (state: AppliedMigrationsState, steps:
|
|
|
3124
3197
|
//#endregion
|
|
3125
3198
|
//#region src/helpers/migrations.d.ts
|
|
3126
3199
|
declare const PRISMA_MODEL_REGEX: RegExp;
|
|
3200
|
+
declare const PRISMA_ENUM_REGEX: RegExp;
|
|
3201
|
+
declare const PRISMA_ENUM_MEMBER_REGEX: RegExp;
|
|
3127
3202
|
/**
|
|
3128
3203
|
* Convert a table name to a PascalCase model name, with basic singularization.
|
|
3129
3204
|
*
|
|
@@ -3145,6 +3220,7 @@ declare const escapeRegex: (value: string) => string;
|
|
|
3145
3220
|
* @returns
|
|
3146
3221
|
*/
|
|
3147
3222
|
declare const resolvePrismaType: (column: SchemaColumn) => string;
|
|
3223
|
+
declare const resolveEnumName: (column: SchemaColumn) => string;
|
|
3148
3224
|
/**
|
|
3149
3225
|
* Format a default value for inclusion in a Prisma schema field definition, based on its type.
|
|
3150
3226
|
*
|
|
@@ -3152,6 +3228,13 @@ declare const resolvePrismaType: (column: SchemaColumn) => string;
|
|
|
3152
3228
|
* @returns
|
|
3153
3229
|
*/
|
|
3154
3230
|
declare const formatDefaultValue: (value: unknown) => string | undefined;
|
|
3231
|
+
/**
|
|
3232
|
+
* Format a default value for an enum column as a Prisma @default attribute, validating that it is a non-empty string.
|
|
3233
|
+
*
|
|
3234
|
+
* @param value
|
|
3235
|
+
* @returns
|
|
3236
|
+
*/
|
|
3237
|
+
declare const formatEnumDefaultValue: (value: unknown) => string | undefined;
|
|
3155
3238
|
/**
|
|
3156
3239
|
* Build a single line of a Prisma model field definition based on a SchemaColumn, including type and modifiers.
|
|
3157
3240
|
*
|
|
@@ -3159,6 +3242,29 @@ declare const formatDefaultValue: (value: unknown) => string | undefined;
|
|
|
3159
3242
|
* @returns
|
|
3160
3243
|
*/
|
|
3161
3244
|
declare const buildFieldLine: (column: SchemaColumn) => string;
|
|
3245
|
+
/**
|
|
3246
|
+
* Build a Prisma enum block string based on an enum name and its values, validating that
|
|
3247
|
+
* at least one value is provided.
|
|
3248
|
+
*
|
|
3249
|
+
* @param enumName The name of the enum to create.
|
|
3250
|
+
* @param values The array of values for the enum.
|
|
3251
|
+
* @returns The Prisma enum block string.
|
|
3252
|
+
*/
|
|
3253
|
+
declare const buildEnumBlock: (enumName: string, values: string[]) => string;
|
|
3254
|
+
/**
|
|
3255
|
+
* Find the Prisma enum block in a schema string that corresponds to a given enum
|
|
3256
|
+
* name, returning its details if found.
|
|
3257
|
+
*
|
|
3258
|
+
* @param schema The Prisma schema string to search for the enum block.
|
|
3259
|
+
* @param enumName The name of the enum to find in the schema.
|
|
3260
|
+
* @returns
|
|
3261
|
+
*/
|
|
3262
|
+
declare const findEnumBlock: (schema: string, enumName: string) => {
|
|
3263
|
+
enumName: string;
|
|
3264
|
+
block: string;
|
|
3265
|
+
start: number;
|
|
3266
|
+
end: number;
|
|
3267
|
+
} | null;
|
|
3162
3268
|
/**
|
|
3163
3269
|
* Build a Prisma model-level @@index definition line.
|
|
3164
3270
|
*
|
|
@@ -3492,4 +3598,4 @@ declare class URLDriver {
|
|
|
3492
3598
|
url(page: number): string;
|
|
3493
3599
|
}
|
|
3494
3600
|
//#endregion
|
|
3495
|
-
export { ArkormCollection, ArkormErrorContext, ArkormException, Attribute, AttributeOptions, CliApp, ForeignKeyBuilder, InitCommand, InlineFactory, LengthAwarePaginator, MIGRATION_BRAND, MakeFactoryCommand, MakeMigrationCommand, MakeModelCommand, MakeSeederCommand, MigrateCommand, MigrateRollbackCommand, Migration, MigrationHistoryCommand, MissingDelegateException, Model, ModelFactory, ModelNotFoundException, ModelsSyncCommand, PRISMA_MODEL_REGEX, Paginator, PrismaDelegateMap, QueryBuilder, QueryConstraintException, RelationResolutionException, SEEDER_BRAND, SchemaBuilder, ScopeNotDefinedException, SeedCommand, Seeder, SeederCallArgument, SeederConstructor, SeederInput, TableBuilder, URLDriver, UniqueConstraintResolutionException, UnsupportedAdapterFeatureException, applyAlterTableOperation, applyCreateTableOperation, applyDropTableOperation, applyMigrationRollbackToPrismaSchema, applyMigrationToPrismaSchema, applyOperationsToPrismaSchema, buildFieldLine, buildIndexLine, buildInverseRelationLine, buildMigrationIdentity, buildMigrationRunId, buildMigrationSource, buildModelBlock, buildRelationLine, computeMigrationChecksum, configureArkormRuntime, createMigrationTimestamp, createPrismaAdapter, createPrismaDelegateMap, defineConfig, defineFactory, deriveCollectionFieldName, deriveInverseRelationAlias, deriveRelationFieldName, ensureArkormConfigLoading, escapeRegex, findAppliedMigration, findModelBlock, formatDefaultValue, formatRelationAction, generateMigrationFile, getActiveTransactionClient, getDefaultStubsPath, getLastMigrationRun, getLatestAppliedMigrations, getMigrationPlan, getRuntimePaginationCurrentPageResolver, getRuntimePaginationURLDriverFactory, getRuntimePrismaClient, getUserConfig, inferDelegateName, isDelegateLike, isMigrationApplied, isTransactionCapableClient, loadArkormConfig, markMigrationApplied, markMigrationRun, pad, readAppliedMigrationsState, removeAppliedMigration, resetArkormRuntimeForTests, resolveCast, resolveMigrationClassName, resolveMigrationStateFilePath, resolvePrismaType, runArkormTransaction, runMigrationWithPrisma, runPrismaCommand, toMigrationFileSlug, toModelName, writeAppliedMigrationsState };
|
|
3601
|
+
export { ArkormCollection, ArkormErrorContext, ArkormException, Attribute, AttributeOptions, CliApp, EnumBuilder, ForeignKeyBuilder, InitCommand, InlineFactory, LengthAwarePaginator, MIGRATION_BRAND, MakeFactoryCommand, MakeMigrationCommand, MakeModelCommand, MakeSeederCommand, MigrateCommand, MigrateRollbackCommand, Migration, MigrationHistoryCommand, MissingDelegateException, Model, ModelFactory, ModelNotFoundException, ModelsSyncCommand, PRISMA_ENUM_MEMBER_REGEX, PRISMA_ENUM_REGEX, PRISMA_MODEL_REGEX, Paginator, PrismaDelegateMap, QueryBuilder, QueryConstraintException, RelationResolutionException, SEEDER_BRAND, SchemaBuilder, ScopeNotDefinedException, SeedCommand, Seeder, SeederCallArgument, SeederConstructor, SeederInput, TableBuilder, URLDriver, UniqueConstraintResolutionException, UnsupportedAdapterFeatureException, applyAlterTableOperation, applyCreateTableOperation, applyDropTableOperation, applyMigrationRollbackToPrismaSchema, applyMigrationToPrismaSchema, applyOperationsToPrismaSchema, buildEnumBlock, buildFieldLine, buildIndexLine, buildInverseRelationLine, buildMigrationIdentity, buildMigrationRunId, buildMigrationSource, buildModelBlock, buildRelationLine, computeMigrationChecksum, configureArkormRuntime, createMigrationTimestamp, createPrismaAdapter, createPrismaDelegateMap, defineConfig, defineFactory, deriveCollectionFieldName, deriveInverseRelationAlias, deriveRelationFieldName, ensureArkormConfigLoading, escapeRegex, findAppliedMigration, findEnumBlock, findModelBlock, formatDefaultValue, formatEnumDefaultValue, formatRelationAction, generateMigrationFile, getActiveTransactionClient, getDefaultStubsPath, getLastMigrationRun, getLatestAppliedMigrations, getMigrationPlan, getRuntimePaginationCurrentPageResolver, getRuntimePaginationURLDriverFactory, getRuntimePrismaClient, getUserConfig, inferDelegateName, isDelegateLike, isMigrationApplied, isTransactionCapableClient, loadArkormConfig, markMigrationApplied, markMigrationRun, pad, readAppliedMigrationsState, removeAppliedMigration, resetArkormRuntimeForTests, resolveCast, resolveEnumName, resolveMigrationClassName, resolveMigrationStateFilePath, resolvePrismaType, runArkormTransaction, runMigrationWithPrisma, runPrismaCommand, toMigrationFileSlug, toModelName, writeAppliedMigrationsState };
|