arkormx 2.4.6 → 2.4.8

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.
@@ -855,23 +855,6 @@ const getLatestAppliedMigrations = (state, steps) => {
855
855
  }).slice(0, Math.max(0, steps)).map((entry) => entry.migration);
856
856
  };
857
857
 
858
- //#endregion
859
- //#region src/helpers/PrimaryKeyGenerationPlanner.ts
860
- var PrimaryKeyGenerationPlanner = class {
861
- static plan(column) {
862
- if (!column.primary || column.default !== void 0) return void 0;
863
- if (column.type === "uuid" || column.type === "string") return {
864
- strategy: "uuid",
865
- prismaDefault: "@default(uuid())",
866
- databaseDefault: column.type === "uuid" ? "gen_random_uuid()" : "gen_random_uuid()::text",
867
- runtimeFactory: "uuid"
868
- };
869
- }
870
- static generate(generation) {
871
- if (generation?.runtimeFactory === "uuid") return (0, node_crypto.randomUUID)();
872
- }
873
- };
874
-
875
858
  //#endregion
876
859
  //#region src/database/ForeignKeyBuilder.ts
877
860
  /**
@@ -944,6 +927,23 @@ var ForeignKeyBuilder = class {
944
927
  }
945
928
  };
946
929
 
930
+ //#endregion
931
+ //#region src/helpers/PrimaryKeyGenerationPlanner.ts
932
+ var PrimaryKeyGenerationPlanner = class {
933
+ static plan(column) {
934
+ if (!column.primary || column.default !== void 0) return void 0;
935
+ if (column.type === "uuid" || column.type === "string") return {
936
+ strategy: "uuid",
937
+ prismaDefault: "@default(uuid())",
938
+ databaseDefault: column.type === "uuid" ? "gen_random_uuid()" : "gen_random_uuid()::text",
939
+ runtimeFactory: "uuid"
940
+ };
941
+ }
942
+ static generate(generation) {
943
+ if (generation?.runtimeFactory === "uuid") return (0, node_crypto.randomUUID)();
944
+ }
945
+ };
946
+
947
947
  //#endregion
948
948
  //#region src/database/TableBuilder.ts
949
949
  const PRISMA_ENUM_MEMBER_REGEX$1 = /^[A-Za-z][A-Za-z0-9_]*$/;
@@ -1046,18 +1046,21 @@ var TableBuilder = class {
1046
1046
  this.dropColumnNames = [];
1047
1047
  this.indexes = [];
1048
1048
  this.foreignKeys = [];
1049
+ this.compositeUniqueConstraints = [];
1049
1050
  }
1050
- /**
1051
- * Defines a primary key column in the table.
1052
- *
1053
- * @param columnNameOrOptions
1054
- * @param options
1055
- * @returns
1056
- */
1057
1051
  primary(columnNameOrOptions, options) {
1052
+ if (Array.isArray(columnNameOrOptions)) {
1053
+ const columns = this.normalizeCompositeColumns(columnNameOrOptions, "primary key");
1054
+ if (this.compositePrimaryKey) throw new Error("A composite primary key has already been defined for this table.");
1055
+ this.compositePrimaryKey = {
1056
+ columns,
1057
+ ...typeof options === "string" && options.trim() ? { name: options.trim() } : {}
1058
+ };
1059
+ return this;
1060
+ }
1058
1061
  const config = typeof columnNameOrOptions === "string" ? {
1059
1062
  columnName: columnNameOrOptions,
1060
- ...options ?? {}
1063
+ ...typeof options === "object" ? options : {}
1061
1064
  } : columnNameOrOptions ?? {};
1062
1065
  const column = this.resolveColumn(config.columnName);
1063
1066
  column.primary = true;
@@ -1157,15 +1160,19 @@ var TableBuilder = class {
1157
1160
  float(name, options = {}) {
1158
1161
  return this.column(name, "float", options);
1159
1162
  }
1160
- /**
1161
- * Marks a column as unique in the table.
1162
- *
1163
- * @param name Optional explicit column name.
1164
- * When omitted, applies to the latest defined column.
1165
- * @returns The current TableBuilder instance for chaining.
1166
- */
1167
- unique(name) {
1168
- const column = this.resolveColumn(name);
1163
+ unique(columnsOrName, name) {
1164
+ if (Array.isArray(columnsOrName)) {
1165
+ const columns = this.normalizeCompositeColumns(columnsOrName, "unique constraint");
1166
+ const normalizedName = name?.trim();
1167
+ if (this.compositeUniqueConstraints.find((constraint) => constraint.columns.length === columns.length && constraint.columns.every((column, index) => column === columns[index]))) throw new Error(`A unique constraint for columns [${columns.join(", ")}] has already been defined for this table.`);
1168
+ if (normalizedName && this.compositeUniqueConstraints.some((constraint) => constraint.name === normalizedName)) throw new Error(`A unique constraint named [${normalizedName}] has already been defined for this table.`);
1169
+ this.compositeUniqueConstraints.push({
1170
+ columns,
1171
+ ...normalizedName ? { name: normalizedName } : {}
1172
+ });
1173
+ return this;
1174
+ }
1175
+ const column = this.resolveColumn(columnsOrName);
1169
1176
  column.unique = true;
1170
1177
  return this;
1171
1178
  }
@@ -1211,6 +1218,17 @@ var TableBuilder = class {
1211
1218
  return this;
1212
1219
  }
1213
1220
  /**
1221
+ * Defines columns for a polymorphic relationship in the table with UUID ID.
1222
+ *
1223
+ * @param name The base name for the polymorphic relationship columns.
1224
+ * @returns
1225
+ */
1226
+ uuidMorphs(name, nullable = false) {
1227
+ this.string(`${name}Type`, { nullable });
1228
+ this.uuid(`${name}Id`, { nullable });
1229
+ return this;
1230
+ }
1231
+ /**
1214
1232
  * Defines nullable columns for a polymorphic relationship in the table.
1215
1233
  *
1216
1234
  * @param name The base name for the polymorphic relationship columns.
@@ -1220,6 +1238,15 @@ var TableBuilder = class {
1220
1238
  return this.morphs(name, true);
1221
1239
  }
1222
1240
  /**
1241
+ * Defines nullable columns for a polymorphic relationship in the table with UUID ID.
1242
+ *
1243
+ * @param name The base name for the polymorphic relationship columns.
1244
+ * @returns
1245
+ */
1246
+ nullableUuidMorphs(name) {
1247
+ return this.uuidMorphs(name, true);
1248
+ }
1249
+ /**
1223
1250
  * Defines a timestamp column in the table.
1224
1251
  *
1225
1252
  * @param name The name of the timestamp column.
@@ -1407,6 +1434,24 @@ var TableBuilder = class {
1407
1434
  return this.foreignKeys.map((foreignKey) => ({ ...foreignKey }));
1408
1435
  }
1409
1436
  /**
1437
+ * Returns a copy of the table-level composite primary key.
1438
+ */
1439
+ getPrimaryKey() {
1440
+ return this.compositePrimaryKey ? {
1441
+ ...this.compositePrimaryKey,
1442
+ columns: [...this.compositePrimaryKey.columns]
1443
+ } : void 0;
1444
+ }
1445
+ /**
1446
+ * Returns copies of table-level composite unique constraints.
1447
+ */
1448
+ getUniqueConstraints() {
1449
+ return this.compositeUniqueConstraints.map((constraint) => ({
1450
+ ...constraint,
1451
+ columns: [...constraint.columns]
1452
+ }));
1453
+ }
1454
+ /**
1410
1455
  * Defines a column in the table with the given name.
1411
1456
  *
1412
1457
  * @param name The name of the column.
@@ -1448,6 +1493,13 @@ var TableBuilder = class {
1448
1493
  if (!column) throw new Error(`Column [${targetName}] was not found in the table definition.`);
1449
1494
  return column;
1450
1495
  }
1496
+ normalizeCompositeColumns(columns, label) {
1497
+ const normalized = columns.map((column) => column.trim());
1498
+ if (normalized.length < 2) throw new Error(`A composite ${label} must contain at least two columns.`);
1499
+ if (normalized.some((column) => !column)) throw new Error(`Composite ${label} columns must be non-empty strings.`);
1500
+ if (new Set(normalized).size !== normalized.length) throw new Error(`Composite ${label} columns must be unique.`);
1501
+ return normalized;
1502
+ }
1451
1503
  };
1452
1504
 
1453
1505
  //#endregion
@@ -1473,12 +1525,17 @@ var SchemaBuilder = class {
1473
1525
  createTable(table, callback) {
1474
1526
  const builder = new TableBuilder();
1475
1527
  callback(builder);
1528
+ const primaryKey = builder.getPrimaryKey();
1529
+ this.validateCompositePrimaryKey(table, primaryKey, builder.getColumns(), true);
1530
+ this.validateCompositeUniqueConstraints(table, builder.getUniqueConstraints(), builder.getColumns(), true);
1476
1531
  this.operations.push({
1477
1532
  type: "createTable",
1478
1533
  table,
1479
1534
  columns: builder.getColumns(),
1480
1535
  indexes: builder.getIndexes(),
1481
- foreignKeys: builder.getForeignKeys()
1536
+ foreignKeys: builder.getForeignKeys(),
1537
+ primaryKey,
1538
+ uniqueConstraints: builder.getUniqueConstraints()
1482
1539
  });
1483
1540
  return this;
1484
1541
  }
@@ -1492,13 +1549,18 @@ var SchemaBuilder = class {
1492
1549
  alterTable(table, callback) {
1493
1550
  const builder = new TableBuilder();
1494
1551
  callback(builder);
1552
+ const primaryKey = builder.getPrimaryKey();
1553
+ this.validateCompositePrimaryKey(table, primaryKey, builder.getColumns(), false);
1554
+ this.validateCompositeUniqueConstraints(table, builder.getUniqueConstraints(), builder.getColumns(), false);
1495
1555
  this.operations.push({
1496
1556
  type: "alterTable",
1497
1557
  table,
1498
1558
  addColumns: builder.getColumns(),
1499
1559
  dropColumns: builder.getDropColumns(),
1500
1560
  addIndexes: builder.getIndexes(),
1501
- addForeignKeys: builder.getForeignKeys()
1561
+ addForeignKeys: builder.getForeignKeys(),
1562
+ addPrimaryKey: primaryKey,
1563
+ addUniqueConstraints: builder.getUniqueConstraints()
1502
1564
  });
1503
1565
  return this;
1504
1566
  }
@@ -1532,7 +1594,15 @@ var SchemaBuilder = class {
1532
1594
  ...index,
1533
1595
  columns: [...index.columns]
1534
1596
  })),
1535
- foreignKeys: operation.foreignKeys.map((foreignKey) => ({ ...foreignKey }))
1597
+ foreignKeys: operation.foreignKeys.map((foreignKey) => ({ ...foreignKey })),
1598
+ primaryKey: operation.primaryKey ? {
1599
+ ...operation.primaryKey,
1600
+ columns: [...operation.primaryKey.columns]
1601
+ } : void 0,
1602
+ uniqueConstraints: operation.uniqueConstraints?.map((constraint) => ({
1603
+ ...constraint,
1604
+ columns: [...constraint.columns]
1605
+ }))
1536
1606
  };
1537
1607
  if (operation.type === "alterTable") return {
1538
1608
  ...operation,
@@ -1545,11 +1615,37 @@ var SchemaBuilder = class {
1545
1615
  ...index,
1546
1616
  columns: [...index.columns]
1547
1617
  })),
1548
- addForeignKeys: operation.addForeignKeys.map((foreignKey) => ({ ...foreignKey }))
1618
+ addForeignKeys: operation.addForeignKeys.map((foreignKey) => ({ ...foreignKey })),
1619
+ addPrimaryKey: operation.addPrimaryKey ? {
1620
+ ...operation.addPrimaryKey,
1621
+ columns: [...operation.addPrimaryKey.columns]
1622
+ } : void 0,
1623
+ addUniqueConstraints: operation.addUniqueConstraints?.map((constraint) => ({
1624
+ ...constraint,
1625
+ columns: [...constraint.columns]
1626
+ }))
1549
1627
  };
1550
1628
  return { ...operation };
1551
1629
  });
1552
1630
  }
1631
+ validateCompositePrimaryKey(table, primaryKey, columns, requireColumns) {
1632
+ if (!primaryKey) return;
1633
+ if (columns.some((column) => column.primary)) throw new Error(`Table [${table}] cannot combine column primary keys with a composite primary key.`);
1634
+ if (!requireColumns) return;
1635
+ primaryKey.columns.forEach((columnName) => {
1636
+ const column = columns.find((candidate) => candidate.name === columnName);
1637
+ if (!column) throw new Error(`Composite primary key column [${columnName}] was not found on table [${table}].`);
1638
+ if (column.nullable) throw new Error(`Composite primary key column [${columnName}] on table [${table}] cannot be nullable.`);
1639
+ });
1640
+ }
1641
+ validateCompositeUniqueConstraints(table, constraints, columns, requireColumns) {
1642
+ if (!requireColumns) return;
1643
+ constraints.forEach((constraint) => {
1644
+ constraint.columns.forEach((columnName) => {
1645
+ if (!columns.some((column) => column.name === columnName)) throw new Error(`Composite unique constraint column [${columnName}] was not found on table [${table}].`);
1646
+ });
1647
+ });
1648
+ }
1553
1649
  };
1554
1650
 
1555
1651
  //#endregion
@@ -1773,6 +1869,24 @@ const buildIndexLine = (index) => {
1773
1869
  return ` @@index([${index.columns.join(", ")}]${typeof index.name === "string" && index.name.trim().length > 0 ? `, name: "${index.name.replace(/"/g, "\\\"")}"` : ""})`;
1774
1870
  };
1775
1871
  /**
1872
+ * Build a Prisma model-level composite primary key definition.
1873
+ *
1874
+ * @param primaryKey
1875
+ * @returns
1876
+ */
1877
+ const buildPrimaryKeyLine = (primaryKey) => {
1878
+ return ` @@id([${primaryKey.columns.join(", ")}]${typeof primaryKey.name === "string" && primaryKey.name.trim().length > 0 ? `, name: "${primaryKey.name.replace(/"/g, "\\\"")}"` : ""})`;
1879
+ };
1880
+ /**
1881
+ * Build a Prisma model-level composite unique constraint definition.
1882
+ *
1883
+ * @param constraint
1884
+ * @returns
1885
+ */
1886
+ const buildUniqueConstraintLine = (constraint) => {
1887
+ return ` @@unique([${constraint.columns.join(", ")}]${typeof constraint.name === "string" && constraint.name.trim().length > 0 ? `, name: "${constraint.name.replace(/"/g, "\\\"")}"` : ""})`;
1888
+ };
1889
+ /**
1776
1890
  * Derive a relation field name from a foreign key column name by applying
1777
1891
  * common conventions, such as removing "Id" suffixes and converting to camelCase.
1778
1892
  *
@@ -1921,7 +2035,14 @@ const buildModelBlock = (operation) => {
1921
2035
  const mapped = operation.table !== modelName.toLowerCase();
1922
2036
  const fields = operation.columns.map(buildFieldLine);
1923
2037
  const relations = (operation.foreignKeys ?? []).map((foreignKey) => buildRelationLine(modelName, foreignKey, operation.columns));
1924
- const metadata = [...(operation.indexes ?? []).map(buildIndexLine), ...mapped ? [` @@map("${(0, _h3ravel_support.str)(operation.table).snake()}")`] : []];
2038
+ const indexes = (operation.indexes ?? []).map(buildIndexLine);
2039
+ const uniqueConstraints = (operation.uniqueConstraints ?? []).map(buildUniqueConstraintLine);
2040
+ const metadata = [
2041
+ ...operation.primaryKey ? [buildPrimaryKeyLine(operation.primaryKey)] : [],
2042
+ ...uniqueConstraints,
2043
+ ...indexes,
2044
+ ...mapped ? [` @@map("${(0, _h3ravel_support.str)(operation.table).snake()}")`] : []
2045
+ ];
1925
2046
  return `model ${modelName} {\n${(metadata.length > 0 ? [
1926
2047
  ...fields,
1927
2048
  ...relations,
@@ -2018,6 +2139,18 @@ const applyAlterTableOperation = (schema, operation) => {
2018
2139
  const insertIndex = Math.max(1, bodyLines.length - 1);
2019
2140
  bodyLines.splice(insertIndex, 0, indexLine);
2020
2141
  });
2142
+ if (operation.addPrimaryKey) {
2143
+ const primaryKeyLine = buildPrimaryKeyLine(operation.addPrimaryKey);
2144
+ if (bodyLines.some((line) => line.includes(" @id") || line.trim().startsWith("@@id("))) throw new ArkormException(`Prisma model for table [${operation.table}] already defines a primary key.`);
2145
+ const insertIndex = Math.max(1, bodyLines.length - 1);
2146
+ bodyLines.splice(insertIndex, 0, primaryKeyLine);
2147
+ }
2148
+ for (const constraint of operation.addUniqueConstraints ?? []) {
2149
+ const constraintLine = buildUniqueConstraintLine(constraint);
2150
+ if (bodyLines.some((line) => line.trim() === constraintLine.trim())) continue;
2151
+ const insertIndex = Math.max(1, bodyLines.length - 1);
2152
+ bodyLines.splice(insertIndex, 0, constraintLine);
2153
+ }
2021
2154
  for (const foreignKey of operation.addForeignKeys ?? []) {
2022
2155
  const relationLine = buildRelationLine(model.modelName, foreignKey, operation.addColumns);
2023
2156
  const relationRegex = new RegExp(`^\\s*${escapeRegex(foreignKey.fieldAlias?.trim() || deriveRelationFieldName(foreignKey.column))}\\s+`);
@@ -5182,12 +5315,24 @@ Object.defineProperty(exports, 'buildModelBlock', {
5182
5315
  return buildModelBlock;
5183
5316
  }
5184
5317
  });
5318
+ Object.defineProperty(exports, 'buildPrimaryKeyLine', {
5319
+ enumerable: true,
5320
+ get: function () {
5321
+ return buildPrimaryKeyLine;
5322
+ }
5323
+ });
5185
5324
  Object.defineProperty(exports, 'buildRelationLine', {
5186
5325
  enumerable: true,
5187
5326
  get: function () {
5188
5327
  return buildRelationLine;
5189
5328
  }
5190
5329
  });
5330
+ Object.defineProperty(exports, 'buildUniqueConstraintLine', {
5331
+ enumerable: true,
5332
+ get: function () {
5333
+ return buildUniqueConstraintLine;
5334
+ }
5335
+ });
5191
5336
  Object.defineProperty(exports, 'computeMigrationChecksum', {
5192
5337
  enumerable: true,
5193
5338
  get: function () {