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