orchid-orm 1.25.3 → 1.26.1

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.
@@ -899,7 +899,7 @@ const changePrimaryKey = (columnsPrimaryKey, {
899
899
  changeTableAst: { shape, add, drop }
900
900
  }) => {
901
901
  var _a;
902
- const tablePrimaryKey = codeTable.internal.primaryKey;
902
+ const tablePrimaryKey = codeTable.internal.tableData.primaryKey;
903
903
  const primaryKey = [
904
904
  .../* @__PURE__ */ new Set([...columnsPrimaryKey, ...(_a = tablePrimaryKey == null ? void 0 : tablePrimaryKey.columns) != null ? _a : []])
905
905
  ];
@@ -908,13 +908,13 @@ const changePrimaryKey = (columnsPrimaryKey, {
908
908
  (key) => !checkForColumnChange(shape, key)
909
909
  );
910
910
  if (toDrop == null ? void 0 : toDrop.length) {
911
- drop.primaryKey = { columns: toDrop, options: dbPrimaryKey == null ? void 0 : dbPrimaryKey.options };
911
+ drop.primaryKey = { columns: toDrop, name: dbPrimaryKey == null ? void 0 : dbPrimaryKey.name };
912
912
  }
913
913
  const toAdd = primaryKey.filter((key) => !checkForColumnChange(shape, key));
914
914
  if (toAdd.length) {
915
915
  add.primaryKey = {
916
916
  columns: toAdd,
917
- options: tablePrimaryKey == null ? void 0 : tablePrimaryKey.options
917
+ name: tablePrimaryKey == null ? void 0 : tablePrimaryKey.name
918
918
  };
919
919
  }
920
920
  }
@@ -924,16 +924,16 @@ const renamePrimaryKey = (ast, {
924
924
  dbTableData: { primaryKey: dbPrimaryKey },
925
925
  schema
926
926
  }) => {
927
- var _a, _b, _c, _d, _e, _f;
928
- const tablePrimaryKey = codeTable.internal.primaryKey;
929
- if (dbPrimaryKey && tablePrimaryKey && ((_a = dbPrimaryKey == null ? void 0 : dbPrimaryKey.options) == null ? void 0 : _a.name) !== ((_b = tablePrimaryKey == null ? void 0 : tablePrimaryKey.options) == null ? void 0 : _b.name)) {
927
+ var _a, _b;
928
+ const tablePrimaryKey = codeTable.internal.tableData.primaryKey;
929
+ if (dbPrimaryKey && tablePrimaryKey && (dbPrimaryKey == null ? void 0 : dbPrimaryKey.name) !== (tablePrimaryKey == null ? void 0 : tablePrimaryKey.name)) {
930
930
  ast.push({
931
931
  type: "renameTableItem",
932
932
  kind: "CONSTRAINT",
933
933
  tableSchema: schema,
934
934
  tableName: codeTable.table,
935
- from: (_d = (_c = dbPrimaryKey.options) == null ? void 0 : _c.name) != null ? _d : `${codeTable.table}_pkey`,
936
- to: (_f = (_e = tablePrimaryKey.options) == null ? void 0 : _e.name) != null ? _f : `${codeTable}_pkey`
935
+ from: (_a = dbPrimaryKey.name) != null ? _a : `${codeTable.table}_pkey`,
936
+ to: (_b = tablePrimaryKey.name) != null ? _b : `${codeTable}_pkey`
937
937
  });
938
938
  }
939
939
  };
@@ -1085,17 +1085,21 @@ const collectCodeIndexes = ({
1085
1085
  codeIndexes.push(
1086
1086
  ...column.data.indexes.map(
1087
1087
  (_b) => {
1088
- var _c = _b, { collate, opclass, order, weight } = _c, options = __objRest(_c, ["collate", "opclass", "order", "weight"]);
1089
- return {
1088
+ var _c = _b, {
1089
+ options: _d
1090
+ } = _c, _e = _d, { collate, opclass, order, weight } = _e, options = __objRest(_e, ["collate", "opclass", "order", "weight"]), index = __objRest(_c, [
1091
+ "options"
1092
+ ]);
1093
+ return __spreadValues$3({
1090
1094
  columns: [{ collate, opclass, order, weight, column: name }],
1091
1095
  options
1092
- };
1096
+ }, index);
1093
1097
  }
1094
1098
  )
1095
1099
  );
1096
1100
  }
1097
- if (codeTable.internal.indexes) {
1098
- codeIndexes.push(...codeTable.internal.indexes);
1101
+ if (codeTable.internal.tableData.indexes) {
1102
+ codeIndexes.push(...codeTable.internal.tableData.indexes);
1099
1103
  }
1100
1104
  return codeIndexes;
1101
1105
  };
@@ -1104,14 +1108,15 @@ const collectCodeComparableIndexes = (codeIndexes) => {
1104
1108
  normalizeIndex(codeIndex.options);
1105
1109
  return indexToComparable(__spreadProps$3(__spreadValues$3({}, codeIndex.options), {
1106
1110
  include: codeIndex.options.include === void 0 ? void 0 : orchidCore.toArray(codeIndex.options.include),
1107
- columns: codeIndex.columns
1111
+ columns: codeIndex.columns,
1112
+ name: codeIndex.name
1108
1113
  }));
1109
1114
  });
1110
1115
  };
1111
1116
  const normalizeIndex = (index) => {
1112
1117
  if (index.using === "btree")
1113
1118
  index.using = void 0;
1114
- if (index.unique === false)
1119
+ if (!index.unique)
1115
1120
  index.unique = void 0;
1116
1121
  if (index.nullsNotDistinct === false)
1117
1122
  index.nullsNotDistinct = void 0;
@@ -1180,7 +1185,8 @@ const handleIndexChange = (ast, { changeTableAst, schema, codeTable }, dbIndex,
1180
1185
  if (!found) {
1181
1186
  ((_b = (_a = changeTableAst.drop).indexes) != null ? _b : _a.indexes = []).push({
1182
1187
  columns: dbColumns,
1183
- options: dbIndex
1188
+ options: dbIndex,
1189
+ name: dbIndex.name === rakeDb.getIndexName(changeTableAst.name, dbColumns) ? void 0 : dbIndex.name
1184
1190
  });
1185
1191
  } else if (rename) {
1186
1192
  ast.push({
@@ -1290,21 +1296,17 @@ const collectCodeFkeys = ({ codeTable, changeTableAst: { shape } }, currentSchem
1290
1296
  continue;
1291
1297
  codeForeignKeys.push(
1292
1298
  ...column.data.foreignKeys.map((x) => {
1299
+ var _a2;
1293
1300
  const columns = [name];
1294
1301
  const references = {
1295
1302
  columns,
1296
- fnOrTable: fnOrTableToString("fn" in x ? x.fn : x.table),
1297
- foreignColumns: x.columns,
1298
- options: {
1299
- name: x.name,
1300
- match: x.match,
1301
- onUpdate: x.onUpdate,
1302
- onDelete: x.onDelete
1303
- }
1303
+ fnOrTable: fnOrTableToString(x.fnOrTable),
1304
+ foreignColumns: x.foreignColumns,
1305
+ options: x.options
1304
1306
  };
1305
1307
  return parseForeignKey(
1306
1308
  {
1307
- name: x.name,
1309
+ name: (_a2 = x.options) == null ? void 0 : _a2.name,
1308
1310
  references
1309
1311
  },
1310
1312
  references,
@@ -1313,8 +1315,8 @@ const collectCodeFkeys = ({ codeTable, changeTableAst: { shape } }, currentSchem
1313
1315
  })
1314
1316
  );
1315
1317
  }
1316
- if (codeTable.internal.constraints) {
1317
- for (const constraint of codeTable.internal.constraints) {
1318
+ if (codeTable.internal.tableData.constraints) {
1319
+ for (const constraint of codeTable.internal.tableData.constraints) {
1318
1320
  const { references } = constraint;
1319
1321
  if (!references)
1320
1322
  continue;
@@ -1378,13 +1380,7 @@ const processChecks = (ast, changeTableData, compareExpressions) => {
1378
1380
  if (!hasDbChecks) {
1379
1381
  if (codeChecks.length) {
1380
1382
  ((_a = add.constraints) != null ? _a : add.constraints = []).push(
1381
- ...codeChecks.map((check) => {
1382
- var _a2;
1383
- return {
1384
- name: (_a2 = check.options) == null ? void 0 : _a2.name,
1385
- check: check.sql
1386
- };
1387
- })
1383
+ ...codeChecks.map((check) => ({ check: check.sql, name: check.name }))
1388
1384
  );
1389
1385
  }
1390
1386
  return;
@@ -1417,13 +1413,10 @@ const processChecks = (ast, changeTableData, compareExpressions) => {
1417
1413
  if (--wait === 0 && !changeTableData.pushedAst) {
1418
1414
  changeTableData.pushedAst = true;
1419
1415
  ((_a2 = add.constraints) != null ? _a2 : add.constraints = []).push(
1420
- ...codeChecks.filter((_, i) => !foundCodeChecks.has(i)).map((check) => {
1421
- var _a3;
1422
- return {
1423
- name: (_a3 = check.options) == null ? void 0 : _a3.name,
1424
- check: check.sql
1425
- };
1426
- })
1416
+ ...codeChecks.filter((_, i) => !foundCodeChecks.has(i)).map((check) => ({
1417
+ name: check.name,
1418
+ check: check.sql
1419
+ }))
1427
1420
  );
1428
1421
  ast.push(changeTableData.changeTableAst);
1429
1422
  }
@@ -1449,11 +1442,11 @@ const collectCodeChecks = ({
1449
1442
  continue;
1450
1443
  codeChecks.push(column.data.check);
1451
1444
  }
1452
- if (codeTable.internal.constraints) {
1453
- for (const constraint of codeTable.internal.constraints) {
1445
+ if (codeTable.internal.tableData.constraints) {
1446
+ for (const constraint of codeTable.internal.tableData.constraints) {
1454
1447
  const { check } = constraint;
1455
1448
  if (check) {
1456
- codeChecks.push({ sql: check, options: { name: constraint.name } });
1449
+ codeChecks.push({ sql: check, name: constraint.name });
1457
1450
  }
1458
1451
  }
1459
1452
  }
@@ -1711,16 +1704,15 @@ const addChangeTable = (dbStructure, changeTables, tableShapes, currentSchema, d
1711
1704
  tableShapes[`${schema}.${codeTable.table}`] = shape;
1712
1705
  };
1713
1706
  const createTableAst = (currentSchema, table) => {
1714
- return __spreadProps$2(__spreadValues$2({
1707
+ return __spreadValues$2({
1715
1708
  type: "table",
1716
1709
  action: "create",
1717
1710
  schema: table.q.schema === currentSchema ? void 0 : table.q.schema,
1718
1711
  comment: table.internal.comment,
1719
1712
  name: table.table,
1720
- shape: makeTableShape(table)
1721
- }, table.internal), {
1713
+ shape: makeTableShape(table),
1722
1714
  noPrimaryKey: table.internal.noPrimaryKey ? "ignore" : "error"
1723
- });
1715
+ }, table.internal.tableData);
1724
1716
  };
1725
1717
  const makeTableShape = (table) => {
1726
1718
  const shape = {};
@@ -1803,7 +1795,11 @@ const verifyMigration = async (adapter, config, migrationCode, generateMigration
1803
1795
  });
1804
1796
  const { log } = config;
1805
1797
  config.log = false;
1806
- const db = rakeDb.createMigrationInterface(trx, true, config);
1798
+ const db = rakeDb.createMigrationInterface(
1799
+ trx,
1800
+ true,
1801
+ config
1802
+ );
1807
1803
  config.log = log;
1808
1804
  for (const changeFn of changeFns) {
1809
1805
  await changeFn(db, true);
@@ -1903,8 +1899,8 @@ const report = (ast, config, currentSchema) => {
1903
1899
  inner.push(
1904
1900
  `${change.type === "add" ? green("+ add column") : red("- drop column")} ${name} ${(_h = column.data.alias) != null ? _h : getColumnDbType(column, currentSchema)}${column.data.isNullable ? " nullable" : ""}${primaryKey ? " primary key" : ""}${foreignKeys ? ` references ${foreignKeys.map((fk) => {
1905
1901
  return `${fnOrTableToString(
1906
- "fn" in fk ? fk.fn : fk.table
1907
- )}(${fk.columns.join(", ")})`;
1902
+ fk.fnOrTable
1903
+ )}(${fk.foreignColumns.join(", ")})`;
1908
1904
  }).join(", ")}` : ""}${(indexes == null ? void 0 : indexes.length) ? indexes.length === 1 ? ", has index" : `, has ${indexes.length} indexes` : ""}${check ? `, checks ${check.sql.toSQL({ values: [] })}` : ""}`
1909
1905
  );
1910
1906
  } else if (change.type === "change") {
@@ -2428,8 +2424,10 @@ const processHasAndBelongsToManyColumn = (column, habtmTables, codeItems) => {
2428
2424
  shape[key] = column2;
2429
2425
  }
2430
2426
  joinTable.shape = shape;
2431
- joinTable.internal.primaryKey = {
2432
- columns: Object.keys(shape)
2427
+ joinTable.internal.tableData = {
2428
+ primaryKey: {
2429
+ columns: Object.keys(shape)
2430
+ }
2433
2431
  };
2434
2432
  joinTable.internal.noPrimaryKey = false;
2435
2433
  codeItems.tables.push(joinTable);
@@ -2494,6 +2492,7 @@ const getTableInfosAndFKeys = (asts, config) => {
2494
2492
  return { tableInfos, fkeys };
2495
2493
  };
2496
2494
  const appCodeGenTable = (tableInfos, fkeys, ast, baseTablePath, baseTableExportedAs) => {
2495
+ var _a, _b;
2497
2496
  const tableInfo = tableInfos[ast.schema ? `${ast.schema}.${ast.name}` : ast.name];
2498
2497
  const imports = {
2499
2498
  "orchid-orm": "Selectable, Insertable, Updatable",
@@ -2510,11 +2509,18 @@ const appCodeGenTable = (tableInfos, fkeys, ast, baseTablePath, baseTableExporte
2510
2509
  if (ast.noPrimaryKey === "ignore") {
2511
2510
  props.push("noPrimaryKey = true;");
2512
2511
  }
2512
+ const hasTableData = Boolean(
2513
+ ast.primaryKey || ((_a = ast.indexes) == null ? void 0 : _a.length) || ((_b = ast.constraints) == null ? void 0 : _b.length)
2514
+ );
2515
+ const shapeCode = pqb.columnsShapeToCode(ast.shape, "t");
2513
2516
  props.push(
2514
- "columns = this.setColumns((t) => ({",
2515
- pqb.columnsShapeToCode(ast.shape, ast, "t"),
2516
- "}));"
2517
+ `columns = this.setColumns(${hasTableData ? "\n " : ""}(t) => ({`,
2518
+ hasTableData ? [shapeCode] : shapeCode,
2519
+ hasTableData ? " })," : "}));"
2517
2520
  );
2521
+ if (hasTableData) {
2522
+ props.push(pqb.pushTableDataCode([], ast), ");");
2523
+ }
2518
2524
  const relations = [];
2519
2525
  const fullTableName = ast.schema ? `${ast.schema}.${ast.name}` : ast.name;
2520
2526
  const belongsTo = fkeys[fullTableName];