orchid-orm 1.64.10 → 1.65.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.
- package/dist/index.d.ts +5 -4
- package/dist/index.js +183 -182
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +4 -3
- package/dist/index.mjs.map +1 -1
- package/dist/migrations/index.d.ts +1 -1
- package/dist/migrations/index.js +99 -98
- package/dist/migrations/index.js.map +1 -1
- package/dist/migrations/index.mjs +5 -4
- package/dist/migrations/index.mjs.map +1 -1
- package/dist/migrations/node-postgres.d.ts +1 -1
- package/dist/migrations/node-postgres.js +99 -98
- package/dist/migrations/node-postgres.js.map +1 -1
- package/dist/migrations/node-postgres.mjs +5 -4
- package/dist/migrations/node-postgres.mjs.map +1 -1
- package/dist/migrations/postgres-js.d.ts +1 -1
- package/dist/migrations/postgres-js.js +99 -98
- package/dist/migrations/postgres-js.js.map +1 -1
- package/dist/migrations/postgres-js.mjs +5 -4
- package/dist/migrations/postgres-js.mjs.map +1 -1
- package/dist/node-postgres.d.ts +3 -3
- package/dist/node-postgres.js.map +1 -1
- package/dist/node-postgres.mjs.map +1 -1
- package/dist/postgres-js.d.ts +6 -5
- package/dist/postgres-js.js.map +1 -1
- package/dist/postgres-js.mjs.map +1 -1
- package/package.json +25 -21
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var rakeDb = require('rake-db');
|
|
4
|
-
var
|
|
4
|
+
var internal = require('pqb/internal');
|
|
5
5
|
var path = require('node:path');
|
|
6
6
|
var url = require('url');
|
|
7
7
|
var fs = require('fs/promises');
|
|
@@ -84,20 +84,20 @@ const promptCreateOrRename = (kind, name, drop, verifying) => {
|
|
|
84
84
|
}
|
|
85
85
|
const renameMessage = `rename ${kind}`;
|
|
86
86
|
return rakeDb.promptSelect({
|
|
87
|
-
message: `Create or rename ${
|
|
87
|
+
message: `Create or rename ${internal.colors.blueBold(
|
|
88
88
|
name
|
|
89
89
|
)} ${kind} from another ${kind}?`,
|
|
90
90
|
options: [
|
|
91
|
-
`${
|
|
91
|
+
`${internal.colors.greenBold("+")} ${name} ${internal.colors.pale(
|
|
92
92
|
`create ${kind}`.padStart(
|
|
93
93
|
hintPos + renameMessage.length - name.length - 4,
|
|
94
94
|
" "
|
|
95
95
|
)
|
|
96
96
|
)}`,
|
|
97
97
|
...drop.map(
|
|
98
|
-
(d) => `${
|
|
98
|
+
(d) => `${internal.colors.yellowBold("~")} ${d} ${internal.colors.yellowBold(
|
|
99
99
|
"=>"
|
|
100
|
-
)} ${name} ${
|
|
100
|
+
)} ${name} ${internal.colors.pale(
|
|
101
101
|
renameMessage.padStart(
|
|
102
102
|
hintPos + renameMessage.length - d.length - name.length - 8,
|
|
103
103
|
" "
|
|
@@ -340,7 +340,7 @@ const addOrRenameColumns = async (config, dbStructure, {
|
|
|
340
340
|
dbName: from,
|
|
341
341
|
column: column.name(codeName)
|
|
342
342
|
});
|
|
343
|
-
const to = config.snakeCase ?
|
|
343
|
+
const to = config.snakeCase ? internal.toSnakeCase(key) : key;
|
|
344
344
|
if (dbTableData.primaryKey) {
|
|
345
345
|
renameColumn(dbTableData.primaryKey.columns, from, to);
|
|
346
346
|
}
|
|
@@ -430,16 +430,16 @@ const changeColumns = async (adapter, config, structureToAstCtx, dbStructure, do
|
|
|
430
430
|
} else if (action !== "recreate") {
|
|
431
431
|
const to = codeColumn.data.name ?? codeKey;
|
|
432
432
|
if (dbName !== to) {
|
|
433
|
-
changeTableData.changeTableAst.shape[config.snakeCase ? dbName ===
|
|
433
|
+
changeTableData.changeTableAst.shape[config.snakeCase ? dbName === internal.toSnakeCase(codeKey) ? codeKey : dbName : dbName] = {
|
|
434
434
|
type: "rename",
|
|
435
|
-
name: config.snakeCase ? to ===
|
|
435
|
+
name: config.snakeCase ? to === internal.toSnakeCase(codeKey) ? codeKey : to : to
|
|
436
436
|
};
|
|
437
437
|
}
|
|
438
438
|
}
|
|
439
439
|
}
|
|
440
440
|
};
|
|
441
441
|
const compareColumns = async (adapter, domainsMap, ast, currentSchema, compareSql, changeTableData, typeCastsCache, verifying, key, dbName, dbColumn, codeColumn) => {
|
|
442
|
-
if (dbColumn instanceof
|
|
442
|
+
if (dbColumn instanceof internal.ArrayColumn && codeColumn instanceof internal.ArrayColumn) {
|
|
443
443
|
dbColumn = dbColumn.data.item;
|
|
444
444
|
codeColumn = codeColumn.data.item;
|
|
445
445
|
}
|
|
@@ -447,18 +447,18 @@ const compareColumns = async (adapter, domainsMap, ast, currentSchema, compareSq
|
|
|
447
447
|
const codeType = getColumnDbType(codeColumn, currentSchema);
|
|
448
448
|
if (dbType !== codeType) {
|
|
449
449
|
const typeCasts = await getTypeCasts(adapter, typeCastsCache);
|
|
450
|
-
const dbBaseType =
|
|
451
|
-
const codeBaseType =
|
|
450
|
+
const dbBaseType = internal.getColumnBaseType(dbColumn, domainsMap, dbType);
|
|
451
|
+
const codeBaseType = internal.getColumnBaseType(codeColumn, domainsMap, codeType);
|
|
452
452
|
if (!typeCasts.get(dbBaseType)?.has(codeBaseType)) {
|
|
453
|
-
if (!(dbColumn instanceof
|
|
453
|
+
if (!(dbColumn instanceof internal.EnumColumn) || !(codeColumn instanceof internal.EnumColumn) || !internal.deepCompare(dbColumn.options, codeColumn.options)) {
|
|
454
454
|
if (verifying) throw new AbortSignal();
|
|
455
455
|
const tableName = rakeDb.concatSchemaAndName(changeTableData.changeTableAst);
|
|
456
456
|
const abort = await rakeDb.promptSelect({
|
|
457
457
|
message: `Cannot cast type of ${tableName}'s column ${key} from ${dbType} to ${codeType}`,
|
|
458
458
|
options: [
|
|
459
|
-
`${
|
|
459
|
+
`${internal.colors.yellowBold(
|
|
460
460
|
`-/+`
|
|
461
|
-
)} recreate the column, existing data will be ${
|
|
461
|
+
)} recreate the column, existing data will be ${internal.colors.red(
|
|
462
462
|
"lost"
|
|
463
463
|
)}`,
|
|
464
464
|
`write migration manually`
|
|
@@ -501,7 +501,7 @@ const compareColumns = async (adapter, domainsMap, ast, currentSchema, compareSq
|
|
|
501
501
|
}
|
|
502
502
|
}
|
|
503
503
|
}
|
|
504
|
-
if (!
|
|
504
|
+
if (!internal.deepCompare(
|
|
505
505
|
dbData.identity,
|
|
506
506
|
codeData.identity ? {
|
|
507
507
|
always: false,
|
|
@@ -603,15 +603,15 @@ const changeColumn = (changeTableData, key, dbName, dbColumn, codeColumn) => {
|
|
|
603
603
|
};
|
|
604
604
|
};
|
|
605
605
|
const getColumnDbType = (column, currentSchema) => {
|
|
606
|
-
if (column instanceof
|
|
606
|
+
if (column instanceof internal.EnumColumn) {
|
|
607
607
|
const [schema = currentSchema, name] = rakeDb.getSchemaAndTableFromName(
|
|
608
608
|
currentSchema,
|
|
609
609
|
column.enumName
|
|
610
610
|
);
|
|
611
611
|
return `${schema}.${name}`;
|
|
612
|
-
} else if (column instanceof
|
|
612
|
+
} else if (column instanceof internal.ArrayColumn) {
|
|
613
613
|
const { item } = column.data;
|
|
614
|
-
let type = item instanceof
|
|
614
|
+
let type = item instanceof internal.EnumColumn ? item.enumName : item.dataType;
|
|
615
615
|
type = type.startsWith(currentSchema + ".") ? type.slice(currentSchema.length + 1) : type;
|
|
616
616
|
return type + "[]".repeat(column.data.arrayDims);
|
|
617
617
|
} else if (column.data.isOfCustomType) {
|
|
@@ -675,7 +675,7 @@ const processDomains = async (ast, adapter, domainsMap, dbStructure, {
|
|
|
675
675
|
);
|
|
676
676
|
if (domain.checks) {
|
|
677
677
|
dbColumn.data.checks = domain.checks.map((check) => ({
|
|
678
|
-
sql: new
|
|
678
|
+
sql: new internal.RawSql([[check]])
|
|
679
679
|
}));
|
|
680
680
|
}
|
|
681
681
|
const dbDomain = makeComparableDomain(
|
|
@@ -685,7 +685,7 @@ const processDomains = async (ast, adapter, domainsMap, dbStructure, {
|
|
|
685
685
|
dbColumn
|
|
686
686
|
);
|
|
687
687
|
const found = codeDomains.filter(
|
|
688
|
-
(codeDomain) =>
|
|
688
|
+
(codeDomain) => internal.deepCompare(dbDomain.compare, codeDomain.compare)
|
|
689
689
|
);
|
|
690
690
|
if ((domain.default || domain.checks?.length) && found.length) {
|
|
691
691
|
for (const codeDomain of found) {
|
|
@@ -752,7 +752,7 @@ const makeComparableDomain = (currentSchema, schemaName, name, column) => {
|
|
|
752
752
|
let arrayDims = 0;
|
|
753
753
|
const isNullable = column.data.isNullable ?? false;
|
|
754
754
|
let inner = column;
|
|
755
|
-
while (inner instanceof
|
|
755
|
+
while (inner instanceof internal.ArrayColumn) {
|
|
756
756
|
inner = inner.data.item;
|
|
757
757
|
arrayDims++;
|
|
758
758
|
}
|
|
@@ -799,7 +799,7 @@ const pushCompareChecks = (compare, domain, found) => {
|
|
|
799
799
|
const inCode = found.flatMap(
|
|
800
800
|
(codeDomain) => codeDomain.column.data.checks?.map(
|
|
801
801
|
(check) => typeof check === "string" ? check : check.sql
|
|
802
|
-
) ||
|
|
802
|
+
) || internal.emptyArray
|
|
803
803
|
);
|
|
804
804
|
compare.push(
|
|
805
805
|
...domain.checks.map((check) => ({
|
|
@@ -999,7 +999,7 @@ const changePrimaryKey = (config, columnsPrimaryKey, {
|
|
|
999
999
|
...columnsPrimaryKey,
|
|
1000
1000
|
...(config.snakeCase ? tablePrimaryKey?.columns.map((key) => ({
|
|
1001
1001
|
key,
|
|
1002
|
-
name:
|
|
1002
|
+
name: internal.toSnakeCase(key)
|
|
1003
1003
|
})) : tablePrimaryKey?.columns.map((key) => ({ key, name: key }))) ?? []
|
|
1004
1004
|
])
|
|
1005
1005
|
];
|
|
@@ -1008,7 +1008,7 @@ const changePrimaryKey = (config, columnsPrimaryKey, {
|
|
|
1008
1008
|
)) {
|
|
1009
1009
|
if (primaryKey.length === 1) {
|
|
1010
1010
|
const { key } = primaryKey[0];
|
|
1011
|
-
const changes = shape[key] &&
|
|
1011
|
+
const changes = shape[key] && internal.toArray(shape[key]);
|
|
1012
1012
|
if (changes) {
|
|
1013
1013
|
for (const change of changes) {
|
|
1014
1014
|
if (change.type !== "change") continue;
|
|
@@ -1244,7 +1244,7 @@ const pushCodeColumnItems = (config, codeItems, columnKey, name, column, key) =>
|
|
|
1244
1244
|
...index,
|
|
1245
1245
|
options: options.include ? config.snakeCase ? {
|
|
1246
1246
|
...options,
|
|
1247
|
-
include:
|
|
1247
|
+
include: internal.toArray(options.include).map(internal.toSnakeCase)
|
|
1248
1248
|
} : options : options,
|
|
1249
1249
|
columnKeys: [
|
|
1250
1250
|
{
|
|
@@ -1269,12 +1269,12 @@ const pushCodeCompositeItems = (config, codeTable, codeItems, key) => {
|
|
|
1269
1269
|
...items.map((x) => ({
|
|
1270
1270
|
...x,
|
|
1271
1271
|
columns: config.snakeCase ? x.columns.map(
|
|
1272
|
-
(c) => "column" in c ? { ...c, column:
|
|
1272
|
+
(c) => "column" in c ? { ...c, column: internal.toSnakeCase(c.column) } : c
|
|
1273
1273
|
) : x.columns,
|
|
1274
1274
|
columnKeys: x.columns,
|
|
1275
1275
|
options: x.options.include && config.snakeCase ? {
|
|
1276
1276
|
...x.options,
|
|
1277
|
-
include:
|
|
1277
|
+
include: internal.toArray(x.options.include).map(internal.toSnakeCase)
|
|
1278
1278
|
} : x.options,
|
|
1279
1279
|
includeKeys: x.options.include
|
|
1280
1280
|
}))
|
|
@@ -1291,7 +1291,7 @@ const collectCodeComparableItemsType = (config, codeItems, key) => {
|
|
|
1291
1291
|
normalizeItem(codeItem.options);
|
|
1292
1292
|
return itemToComparable({
|
|
1293
1293
|
...codeItem.options,
|
|
1294
|
-
include: codeItem.options.include === void 0 ? void 0 : config.snakeCase ?
|
|
1294
|
+
include: codeItem.options.include === void 0 ? void 0 : config.snakeCase ? internal.toArray(codeItem.options.include).map(internal.toSnakeCase) : internal.toArray(codeItem.options.include),
|
|
1295
1295
|
columns: codeItem.columns,
|
|
1296
1296
|
name: codeItem.options.name,
|
|
1297
1297
|
columnKeys: codeItem.columnKeys,
|
|
@@ -1363,7 +1363,7 @@ const findMatchingItemWithoutSql = (dbItem, codeComparableItems, codeItems, skip
|
|
|
1363
1363
|
for (let i = 0; i < codeComparableItems[key].length; i++) {
|
|
1364
1364
|
if (skipCodeItems[key].has(i)) continue;
|
|
1365
1365
|
const { columns: codeColumns, ...codeItem } = codeComparableItems[key][i];
|
|
1366
|
-
if (dbColumns.length === codeColumns.length && !dbColumns.some((dbColumn, i2) => !
|
|
1366
|
+
if (dbColumns.length === codeColumns.length && !dbColumns.some((dbColumn, i2) => !internal.deepCompare(dbColumn, codeColumns[i2]))) {
|
|
1367
1367
|
let a = dbItemWithoutColumns;
|
|
1368
1368
|
let b = codeItem;
|
|
1369
1369
|
const codeName = b.name ?? (key === "indexes" ? rakeDb.getIndexName : rakeDb.getExcludeName)(
|
|
@@ -1381,7 +1381,7 @@ const findMatchingItemWithoutSql = (dbItem, codeComparableItems, codeItems, skip
|
|
|
1381
1381
|
if (a.language && !b.language) {
|
|
1382
1382
|
b.language = config.language ?? "english";
|
|
1383
1383
|
}
|
|
1384
|
-
if (
|
|
1384
|
+
if (internal.deepCompare(a, b)) {
|
|
1385
1385
|
found.push(codeItems[key][i]);
|
|
1386
1386
|
rename.push(
|
|
1387
1387
|
dbItemWithoutColumns.name !== codeName ? codeName : void 0
|
|
@@ -1395,7 +1395,7 @@ const findMatchingItemWithoutSql = (dbItem, codeComparableItems, codeItems, skip
|
|
|
1395
1395
|
includeKeys,
|
|
1396
1396
|
...codeItemWithoutKeys
|
|
1397
1397
|
} = codeItem;
|
|
1398
|
-
if (
|
|
1398
|
+
if (internal.deepCompare(dbItemWithoutColumns, codeItemWithoutKeys)) {
|
|
1399
1399
|
found.push(codeItems[key][i]);
|
|
1400
1400
|
rename.push(void 0);
|
|
1401
1401
|
}
|
|
@@ -1524,8 +1524,7 @@ const processForeignKeys = (config, ast, changeTables, currentSchema, tableShape
|
|
|
1524
1524
|
if (hasChangedColumn) continue;
|
|
1525
1525
|
const foreignShape = tableShapes[`${dbReferences.foreignSchema}.${dbReferences.foreignTable}`];
|
|
1526
1526
|
const hasForeignChangedColumn = foreignShape && dbReferences.foreignColumns.some((column) => {
|
|
1527
|
-
|
|
1528
|
-
return res;
|
|
1527
|
+
return checkForColumnAddOrDrop(foreignShape, column);
|
|
1529
1528
|
});
|
|
1530
1529
|
if (hasForeignChangedColumn) continue;
|
|
1531
1530
|
let found = false;
|
|
@@ -1533,7 +1532,7 @@ const processForeignKeys = (config, ast, changeTables, currentSchema, tableShape
|
|
|
1533
1532
|
for (let i = 0; i < codeForeignKeys.length; i++) {
|
|
1534
1533
|
const codeForeignKey = codeForeignKeys[i];
|
|
1535
1534
|
const codeReferences = codeForeignKey.references;
|
|
1536
|
-
if (
|
|
1535
|
+
if (internal.deepCompare(dbReferences, codeReferences)) {
|
|
1537
1536
|
found = true;
|
|
1538
1537
|
codeForeignKeys.splice(i, 1);
|
|
1539
1538
|
const codeName = codeForeignKey.codeConstraint.name ?? rakeDb.getConstraintName(
|
|
@@ -1647,8 +1646,8 @@ const collectCodeFkeys = (config, { codeTable, changeTableAst: { shape } }, curr
|
|
|
1647
1646
|
{
|
|
1648
1647
|
...refs,
|
|
1649
1648
|
fnOrTable,
|
|
1650
|
-
columns: config.snakeCase ? refs.columns.map(
|
|
1651
|
-
foreignColumns: config.snakeCase ? refs.foreignColumns.map(
|
|
1649
|
+
columns: config.snakeCase ? refs.columns.map(internal.toSnakeCase) : refs.columns,
|
|
1650
|
+
foreignColumns: config.snakeCase ? refs.foreignColumns.map(internal.toSnakeCase) : refs.foreignColumns
|
|
1652
1651
|
},
|
|
1653
1652
|
currentSchema
|
|
1654
1653
|
)
|
|
@@ -1672,7 +1671,7 @@ const parseForeignKey = (config, codeConstraint, references, currentSchema) => {
|
|
|
1672
1671
|
foreignSchema: schema ?? currentSchema,
|
|
1673
1672
|
foreignTable: table,
|
|
1674
1673
|
columns,
|
|
1675
|
-
foreignColumns: config.snakeCase ? foreignColumns.map(
|
|
1674
|
+
foreignColumns: config.snakeCase ? foreignColumns.map(internal.toSnakeCase) : foreignColumns,
|
|
1676
1675
|
match: mapMatchToDb[options?.match || "SIMPLE"],
|
|
1677
1676
|
onUpdate: mapActionToDb[options?.onUpdate || "NO ACTION"],
|
|
1678
1677
|
onDelete: mapActionToDb[options?.onDelete || "NO ACTION"]
|
|
@@ -1791,7 +1790,7 @@ const collectCodeChecks = ({
|
|
|
1791
1790
|
const baseName = `${codeTable.table}_${columnName}_check`;
|
|
1792
1791
|
codeChecks.push(
|
|
1793
1792
|
...column.data.checks.map((check) => {
|
|
1794
|
-
const name = check.name ||
|
|
1793
|
+
const name = check.name || internal.getFreeSetAlias(names, baseName, 1);
|
|
1795
1794
|
names.add(name);
|
|
1796
1795
|
return {
|
|
1797
1796
|
check,
|
|
@@ -1806,7 +1805,7 @@ const collectCodeChecks = ({
|
|
|
1806
1805
|
const { check } = constraint;
|
|
1807
1806
|
if (check) {
|
|
1808
1807
|
const baseName = `${codeTable.table}_check`;
|
|
1809
|
-
const name = constraint.name ||
|
|
1808
|
+
const name = constraint.name || internal.getFreeSetAlias(names, baseName, 1);
|
|
1810
1809
|
names.add(name);
|
|
1811
1810
|
codeChecks.push({
|
|
1812
1811
|
check: { sql: check, name: constraint.name },
|
|
@@ -1819,7 +1818,7 @@ const collectCodeChecks = ({
|
|
|
1819
1818
|
};
|
|
1820
1819
|
const dropCheck = ({ changeTableAst: { drop }, changingColumns }, dbCheck, name) => {
|
|
1821
1820
|
var _a;
|
|
1822
|
-
const sql = new
|
|
1821
|
+
const sql = new internal.RawSql([
|
|
1823
1822
|
[dbCheck.expression]
|
|
1824
1823
|
]);
|
|
1825
1824
|
if (dbCheck.columns?.length === 1 && changingColumns[dbCheck.columns[0]]) {
|
|
@@ -2007,10 +2006,10 @@ const applyChangeTables = async (adapter, changeTables, structureToAstCtx, dbStr
|
|
|
2007
2006
|
}
|
|
2008
2007
|
};
|
|
2009
2008
|
const getColumnDbTypeForComparison = (column, currentSchema) => {
|
|
2010
|
-
if (column instanceof
|
|
2009
|
+
if (column instanceof internal.ArrayColumn) {
|
|
2011
2010
|
return getColumnDbTypeForComparison(column.data.item, currentSchema) + "[]".repeat(column.data.arrayDims);
|
|
2012
2011
|
}
|
|
2013
|
-
let type = column instanceof
|
|
2012
|
+
let type = column instanceof internal.EnumColumn ? column.enumName : column.dataType;
|
|
2014
2013
|
const i = type.indexOf("(");
|
|
2015
2014
|
let append = "";
|
|
2016
2015
|
if (i !== -1) {
|
|
@@ -2020,7 +2019,7 @@ const getColumnDbTypeForComparison = (column, currentSchema) => {
|
|
|
2020
2019
|
const j = type.indexOf(".");
|
|
2021
2020
|
if (j === -1) {
|
|
2022
2021
|
let result = `"${type}"${append}`;
|
|
2023
|
-
if (column.data.isOfCustomType || column instanceof
|
|
2022
|
+
if (column.data.isOfCustomType || column instanceof internal.EnumColumn) {
|
|
2024
2023
|
result = `"${currentSchema}".${result}`;
|
|
2025
2024
|
}
|
|
2026
2025
|
return result;
|
|
@@ -2133,7 +2132,7 @@ const makeTableShape = (table) => {
|
|
|
2133
2132
|
const shape = {};
|
|
2134
2133
|
for (const key in table.shape) {
|
|
2135
2134
|
const column = table.shape[key];
|
|
2136
|
-
if (!(column instanceof
|
|
2135
|
+
if (!(column instanceof internal.VirtualColumn)) {
|
|
2137
2136
|
shape[key] = column;
|
|
2138
2137
|
}
|
|
2139
2138
|
}
|
|
@@ -2198,7 +2197,7 @@ const processRoles = async (ast, dbStructure, { verifying, internal: { roles } }
|
|
|
2198
2197
|
);
|
|
2199
2198
|
if (codeRole) {
|
|
2200
2199
|
found.add(dbRole.name);
|
|
2201
|
-
if (!
|
|
2200
|
+
if (!internal.deepCompare(dbRoleWithoutPrivileges, codeRole)) {
|
|
2202
2201
|
ast.push({
|
|
2203
2202
|
type: "changeRole",
|
|
2204
2203
|
name: dbRole.name,
|
|
@@ -2243,7 +2242,7 @@ const processRoles = async (ast, dbStructure, { verifying, internal: { roles } }
|
|
|
2243
2242
|
const makeRenameOrChangeAst = (dbRole, codeRole) => {
|
|
2244
2243
|
const { name: dbRoleName, ...dbRoleRest } = dbRole;
|
|
2245
2244
|
const { name: codeRoleName, ...codeRoleRest } = codeRole;
|
|
2246
|
-
if (
|
|
2245
|
+
if (internal.deepCompare(dbRoleRest, codeRoleRest) && dbRoleName !== codeRoleName) {
|
|
2247
2246
|
return {
|
|
2248
2247
|
type: "renameRole",
|
|
2249
2248
|
from: dbRoleName,
|
|
@@ -2411,7 +2410,7 @@ const hasAnyPrivilege = (obj) => {
|
|
|
2411
2410
|
};
|
|
2412
2411
|
const processDefaultPrivileges = (ast, dbStructure, { internal: { roles } }) => {
|
|
2413
2412
|
if (!dbStructure.defaultPrivileges || !roles) return;
|
|
2414
|
-
const supportedPrivileges =
|
|
2413
|
+
const supportedPrivileges = internal.getSupportedDefaultPrivileges(
|
|
2415
2414
|
dbStructure.version
|
|
2416
2415
|
);
|
|
2417
2416
|
const objectTypeToAllPrivileges = {};
|
|
@@ -2651,7 +2650,9 @@ const verifyMigration = async (adapter, config, migrationCode, generateMigration
|
|
|
2651
2650
|
});
|
|
2652
2651
|
const { log } = config;
|
|
2653
2652
|
config.log = false;
|
|
2654
|
-
const db = rakeDb.createMigrationInterface(trx, true, config)
|
|
2653
|
+
const db = rakeDb.createMigrationInterface(trx, true, config).getDb(
|
|
2654
|
+
config.columnTypes
|
|
2655
|
+
);
|
|
2655
2656
|
config.log = log;
|
|
2656
2657
|
for (const changeFn of changeFns) {
|
|
2657
2658
|
await changeFn(db, true);
|
|
@@ -2693,7 +2694,7 @@ const report = (ast, config, currentSchema) => {
|
|
|
2693
2694
|
if (typeof config.log === "object" && config.log.colors === false) {
|
|
2694
2695
|
green = red = yellow = pale = (s) => s;
|
|
2695
2696
|
} else {
|
|
2696
|
-
({ green, red, yellow, pale } =
|
|
2697
|
+
({ green, red, yellow, pale } = internal.colors);
|
|
2697
2698
|
}
|
|
2698
2699
|
for (const a of ast) {
|
|
2699
2700
|
switch (a.type) {
|
|
@@ -2736,7 +2737,7 @@ const report = (ast, config, currentSchema) => {
|
|
|
2736
2737
|
const value = counters[key];
|
|
2737
2738
|
if (value || key === "column") {
|
|
2738
2739
|
summary.push(
|
|
2739
|
-
`${value} ${
|
|
2740
|
+
`${value} ${internal.pluralize(key, value, key === "index" ? "es" : "s")}`
|
|
2740
2741
|
);
|
|
2741
2742
|
}
|
|
2742
2743
|
}
|
|
@@ -2758,7 +2759,7 @@ const report = (ast, config, currentSchema) => {
|
|
|
2758
2759
|
snakeCase: config.snakeCase
|
|
2759
2760
|
};
|
|
2760
2761
|
for (const key in a.shape) {
|
|
2761
|
-
const changes =
|
|
2762
|
+
const changes = internal.toArray(a.shape[key]);
|
|
2762
2763
|
for (const change of changes) {
|
|
2763
2764
|
if (change.type === "add" || change.type === "drop") {
|
|
2764
2765
|
const column = change.item;
|
|
@@ -2772,29 +2773,29 @@ const report = (ast, config, currentSchema) => {
|
|
|
2772
2773
|
);
|
|
2773
2774
|
} else if (change.type === "change") {
|
|
2774
2775
|
let name = change.from.column?.data.name ?? key;
|
|
2775
|
-
if (config.snakeCase) name =
|
|
2776
|
+
if (config.snakeCase) name = internal.toCamelCase(name);
|
|
2776
2777
|
const changes2 = [];
|
|
2777
2778
|
inner.push(`${yellow("~ change column")} ${name}:`, changes2);
|
|
2778
2779
|
changes2.push(`${yellow("from")}: `);
|
|
2779
2780
|
const fromCode = change.from.column?.toCode(toCodeCtx, key);
|
|
2780
2781
|
if (fromCode) {
|
|
2781
2782
|
for (const code2 of fromCode) {
|
|
2782
|
-
|
|
2783
|
+
internal.addCode(changes2, code2);
|
|
2783
2784
|
}
|
|
2784
2785
|
}
|
|
2785
2786
|
changes2.push(` ${yellow("to")}: `);
|
|
2786
2787
|
const toCode = change.to.column?.toCode(toCodeCtx, key);
|
|
2787
2788
|
if (toCode) {
|
|
2788
2789
|
for (const code2 of toCode) {
|
|
2789
|
-
|
|
2790
|
+
internal.addCode(changes2, code2);
|
|
2790
2791
|
}
|
|
2791
2792
|
}
|
|
2792
2793
|
} else if (change.type === "rename") {
|
|
2793
2794
|
inner.push(
|
|
2794
|
-
`${yellow("~ rename column")} ${config.snakeCase ?
|
|
2795
|
+
`${yellow("~ rename column")} ${config.snakeCase ? internal.toCamelCase(key) : key} ${yellow("=>")} ${change.name}`
|
|
2795
2796
|
);
|
|
2796
2797
|
} else {
|
|
2797
|
-
|
|
2798
|
+
internal.exhaustive(change.type);
|
|
2798
2799
|
}
|
|
2799
2800
|
}
|
|
2800
2801
|
}
|
|
@@ -2999,7 +3000,7 @@ const report = (ast, config, currentSchema) => {
|
|
|
2999
3000
|
}
|
|
3000
3001
|
case "defaultPrivilege": {
|
|
3001
3002
|
const mapPrivilege = (p) => p === "ALL" ? "ALL PRIVILEGES" : p;
|
|
3002
|
-
const schema = a.schema ? ` in schema ${
|
|
3003
|
+
const schema = a.schema ? ` in schema ${internal.colors.pale(a.schema)}` : ` ${internal.colors.pale("in all schemas")}`;
|
|
3003
3004
|
const parts = [];
|
|
3004
3005
|
if (a.grant) {
|
|
3005
3006
|
for (const [objType, config2] of Object.entries(a.grant)) {
|
|
@@ -3043,10 +3044,10 @@ const report = (ast, config, currentSchema) => {
|
|
|
3043
3044
|
break;
|
|
3044
3045
|
}
|
|
3045
3046
|
default:
|
|
3046
|
-
|
|
3047
|
+
internal.exhaustive(a);
|
|
3047
3048
|
}
|
|
3048
3049
|
}
|
|
3049
|
-
const result =
|
|
3050
|
+
const result = internal.codeToString(code, "", " ");
|
|
3050
3051
|
config.logger.log(result);
|
|
3051
3052
|
};
|
|
3052
3053
|
const dbItemName = ({ schema, name }, currentSchema) => {
|
|
@@ -3072,13 +3073,13 @@ const generate = async (adapters, config, args, afterPull) => {
|
|
|
3072
3073
|
adapters = [afterPull.adapter];
|
|
3073
3074
|
}
|
|
3074
3075
|
const db = await getDbFromConfig(config, dbPath);
|
|
3075
|
-
const { columnTypes, internal } = db.$qb;
|
|
3076
|
-
const rolesDbStructureParam = internal.roles ? internal.managedRolesSql ? { whereSql: internal.managedRolesSql } :
|
|
3076
|
+
const { columnTypes, internal: internal$1 } = db.$qb;
|
|
3077
|
+
const rolesDbStructureParam = internal$1.roles ? internal$1.managedRolesSql ? { whereSql: internal$1.managedRolesSql } : internal.emptyObject : void 0;
|
|
3077
3078
|
const { dbStructure } = await migrateAndPullStructures(
|
|
3078
3079
|
adapters,
|
|
3079
3080
|
config,
|
|
3080
3081
|
rolesDbStructureParam,
|
|
3081
|
-
internal.roles ? { loadDefaultPrivileges: true } : void 0,
|
|
3082
|
+
internal$1.roles ? { loadDefaultPrivileges: true } : void 0,
|
|
3082
3083
|
afterPull
|
|
3083
3084
|
);
|
|
3084
3085
|
const [adapter] = adapters;
|
|
@@ -3087,7 +3088,7 @@ const generate = async (adapters, config, args, afterPull) => {
|
|
|
3087
3088
|
const codeItems = await getActualItems(
|
|
3088
3089
|
db,
|
|
3089
3090
|
currentSchema,
|
|
3090
|
-
internal,
|
|
3091
|
+
internal$1,
|
|
3091
3092
|
columnTypes
|
|
3092
3093
|
);
|
|
3093
3094
|
const structureToAstCtx = rakeDb.makeStructureToAstCtx(config, currentSchema);
|
|
@@ -3095,7 +3096,7 @@ const generate = async (adapters, config, args, afterPull) => {
|
|
|
3095
3096
|
structureToAstCtx,
|
|
3096
3097
|
codeItems,
|
|
3097
3098
|
currentSchema,
|
|
3098
|
-
internal
|
|
3099
|
+
internal: internal$1
|
|
3099
3100
|
};
|
|
3100
3101
|
const ast = [];
|
|
3101
3102
|
let migrationCode;
|
|
@@ -3121,7 +3122,7 @@ const generate = async (adapters, config, args, afterPull) => {
|
|
|
3121
3122
|
migrationCode,
|
|
3122
3123
|
generateMigrationParams,
|
|
3123
3124
|
rolesDbStructureParam,
|
|
3124
|
-
internal.roles ? { loadDefaultPrivileges: true } : void 0
|
|
3125
|
+
internal$1.roles ? { loadDefaultPrivileges: true } : void 0
|
|
3125
3126
|
);
|
|
3126
3127
|
if (result !== void 0) {
|
|
3127
3128
|
throw new Error(
|
|
@@ -3250,7 +3251,7 @@ const compareDbStructures = (a, b, i, path2) => {
|
|
|
3250
3251
|
throw new Error(`${path2} in the db 0 does not match db ${i}`);
|
|
3251
3252
|
}
|
|
3252
3253
|
};
|
|
3253
|
-
const getActualItems = async (db, currentSchema, internal, columnTypes) => {
|
|
3254
|
+
const getActualItems = async (db, currentSchema, internal$1, columnTypes) => {
|
|
3254
3255
|
const tableNames = /* @__PURE__ */ new Set();
|
|
3255
3256
|
const habtmTables = /* @__PURE__ */ new Map();
|
|
3256
3257
|
const codeItems = {
|
|
@@ -3267,7 +3268,7 @@ const getActualItems = async (db, currentSchema, internal, columnTypes) => {
|
|
|
3267
3268
|
if (!table.table) {
|
|
3268
3269
|
throw new Error(`Table ${key} is missing table property`);
|
|
3269
3270
|
}
|
|
3270
|
-
const schema =
|
|
3271
|
+
const schema = internal.getQuerySchema(table);
|
|
3271
3272
|
const name = rakeDb.concatSchemaAndName({ schema, name: table.table });
|
|
3272
3273
|
if (tableNames.has(name)) {
|
|
3273
3274
|
throw new Error(`Table ${name} is defined more than once`);
|
|
@@ -3279,7 +3280,7 @@ const getActualItems = async (db, currentSchema, internal, columnTypes) => {
|
|
|
3279
3280
|
shape: table.shape,
|
|
3280
3281
|
internal: table.internal,
|
|
3281
3282
|
q: {
|
|
3282
|
-
schema:
|
|
3283
|
+
schema: internal.getQuerySchema(table)
|
|
3283
3284
|
}
|
|
3284
3285
|
});
|
|
3285
3286
|
for (const key2 in table.relations) {
|
|
@@ -3292,7 +3293,7 @@ const getActualItems = async (db, currentSchema, internal, columnTypes) => {
|
|
|
3292
3293
|
const column = table.shape[key2];
|
|
3293
3294
|
if (column.data.computed) {
|
|
3294
3295
|
delete table.shape[key2];
|
|
3295
|
-
} else if (column instanceof
|
|
3296
|
+
} else if (column instanceof internal.DomainColumn) {
|
|
3296
3297
|
const [schemaName = currentSchema, name2] = rakeDb.getSchemaAndTableFromName(
|
|
3297
3298
|
currentSchema,
|
|
3298
3299
|
column.dataType
|
|
@@ -3300,29 +3301,29 @@ const getActualItems = async (db, currentSchema, internal, columnTypes) => {
|
|
|
3300
3301
|
domains.set(column.dataType, {
|
|
3301
3302
|
schemaName,
|
|
3302
3303
|
name: name2,
|
|
3303
|
-
column: column.data.as ?? new
|
|
3304
|
+
column: column.data.as ?? new internal.UnknownColumn(internal.defaultSchemaConfig)
|
|
3304
3305
|
});
|
|
3305
3306
|
} else {
|
|
3306
|
-
const en = column.dataType === "enum" ? column : column instanceof
|
|
3307
|
+
const en = column.dataType === "enum" ? column : column instanceof internal.ArrayColumn && column.data.item.dataType === "enum" ? column.data.item : void 0;
|
|
3307
3308
|
if (en) {
|
|
3308
3309
|
processEnumColumn(en, currentSchema, codeItems);
|
|
3309
3310
|
}
|
|
3310
3311
|
}
|
|
3311
3312
|
}
|
|
3312
3313
|
}
|
|
3313
|
-
if (internal.extensions) {
|
|
3314
|
-
for (const extension of internal.extensions) {
|
|
3314
|
+
if (internal$1.extensions) {
|
|
3315
|
+
for (const extension of internal$1.extensions) {
|
|
3315
3316
|
const [schema] = rakeDb.getSchemaAndTableFromName(currentSchema, extension.name);
|
|
3316
3317
|
if (schema) codeItems.schemas.add(schema);
|
|
3317
3318
|
}
|
|
3318
3319
|
}
|
|
3319
|
-
if (internal.domains) {
|
|
3320
|
-
for (const key in internal.domains) {
|
|
3320
|
+
if (internal$1.domains) {
|
|
3321
|
+
for (const key in internal$1.domains) {
|
|
3321
3322
|
const [schemaName = currentSchema, name] = rakeDb.getSchemaAndTableFromName(
|
|
3322
3323
|
currentSchema,
|
|
3323
3324
|
key
|
|
3324
3325
|
);
|
|
3325
|
-
const column = internal.domains[key](columnTypes);
|
|
3326
|
+
const column = internal$1.domains[key](columnTypes);
|
|
3326
3327
|
domains.set(key, {
|
|
3327
3328
|
schemaName,
|
|
3328
3329
|
name,
|
|
@@ -3334,8 +3335,8 @@ const getActualItems = async (db, currentSchema, internal, columnTypes) => {
|
|
|
3334
3335
|
codeItems.schemas.add(domain.schemaName);
|
|
3335
3336
|
codeItems.domains.push(domain);
|
|
3336
3337
|
}
|
|
3337
|
-
if (internal.roles) {
|
|
3338
|
-
for (const role of internal.roles) {
|
|
3338
|
+
if (internal$1.roles) {
|
|
3339
|
+
for (const role of internal$1.roles) {
|
|
3339
3340
|
if (role.defaultPrivileges) {
|
|
3340
3341
|
for (const privilege of role.defaultPrivileges) {
|
|
3341
3342
|
if (privilege.schema) codeItems.schemas.add(privilege.schema);
|
|
@@ -3381,7 +3382,7 @@ const processHasAndBelongsToManyColumn = (column, habtmTables, codeItems) => {
|
|
|
3381
3382
|
primaryKey: void 0,
|
|
3382
3383
|
default: void 0
|
|
3383
3384
|
};
|
|
3384
|
-
shape[
|
|
3385
|
+
shape[internal.toCamelCase(key)] = column2;
|
|
3385
3386
|
}
|
|
3386
3387
|
joinTable.shape = shape;
|
|
3387
3388
|
joinTable.internal = {
|
|
@@ -3408,11 +3409,11 @@ const getTableInfosAndFKeys = (asts, config) => {
|
|
|
3408
3409
|
const fkeys = {};
|
|
3409
3410
|
for (const ast of asts) {
|
|
3410
3411
|
if (ast.type === "table") {
|
|
3411
|
-
const tableKey =
|
|
3412
|
+
const tableKey = internal.toCamelCase(ast.name);
|
|
3412
3413
|
const dbTableName = ast.schema ? `${ast.schema}.${ast.name}` : ast.name;
|
|
3413
3414
|
let tablePath = path.resolve(config.basePath, generateTableTo(tableKey));
|
|
3414
3415
|
if (!tablePath.endsWith(".ts")) tablePath += ".ts";
|
|
3415
|
-
const name =
|
|
3416
|
+
const name = internal.toPascalCase(ast.name);
|
|
3416
3417
|
const info = {
|
|
3417
3418
|
dbTableName,
|
|
3418
3419
|
key: tableKey,
|
|
@@ -3438,15 +3439,15 @@ const appCodeGenTable = (tableInfos, fkeys, ast, baseTablePath, baseTableExporte
|
|
|
3438
3439
|
const tableInfo = tableInfos[ast.schema ? `${ast.schema}.${ast.name}` : ast.name];
|
|
3439
3440
|
const imports = {
|
|
3440
3441
|
"orchid-orm": "Selectable, Insertable, Updatable",
|
|
3441
|
-
[
|
|
3442
|
+
[internal.getImportPath(tableInfo.path, baseTablePath)]: baseTableExportedAs
|
|
3442
3443
|
};
|
|
3443
3444
|
const props = [];
|
|
3444
3445
|
if (ast.schema) {
|
|
3445
|
-
props.push(`schema = ${
|
|
3446
|
+
props.push(`schema = ${internal.singleQuote(ast.schema)};`);
|
|
3446
3447
|
}
|
|
3447
|
-
props.push(`readonly table = ${
|
|
3448
|
+
props.push(`readonly table = ${internal.singleQuote(ast.name)};`);
|
|
3448
3449
|
if (ast.comment) {
|
|
3449
|
-
props.push(`comment = ${
|
|
3450
|
+
props.push(`comment = ${internal.singleQuote(ast.comment)};`);
|
|
3450
3451
|
}
|
|
3451
3452
|
if (ast.noPrimaryKey === "ignore") {
|
|
3452
3453
|
props.push("noPrimaryKey = true;");
|
|
@@ -3454,7 +3455,7 @@ const appCodeGenTable = (tableInfos, fkeys, ast, baseTablePath, baseTableExporte
|
|
|
3454
3455
|
const hasTableData = Boolean(
|
|
3455
3456
|
ast.primaryKey || ast.indexes?.length || ast.excludes?.length || ast.constraints?.length
|
|
3456
3457
|
);
|
|
3457
|
-
const shapeCode =
|
|
3458
|
+
const shapeCode = internal.columnsShapeToCode(
|
|
3458
3459
|
{ t: "t", table: ast.name, currentSchema },
|
|
3459
3460
|
ast.shape
|
|
3460
3461
|
);
|
|
@@ -3464,19 +3465,19 @@ const appCodeGenTable = (tableInfos, fkeys, ast, baseTablePath, baseTableExporte
|
|
|
3464
3465
|
hasTableData ? " })," : "}));"
|
|
3465
3466
|
);
|
|
3466
3467
|
if (hasTableData) {
|
|
3467
|
-
props.push(
|
|
3468
|
+
props.push(internal.pushTableDataCode([], ast), ");");
|
|
3468
3469
|
}
|
|
3469
3470
|
const relations = [];
|
|
3470
3471
|
const fullTableName = ast.schema ? `${ast.schema}.${ast.name}` : ast.name;
|
|
3471
3472
|
const belongsTo = fkeys[fullTableName];
|
|
3472
3473
|
if (belongsTo) {
|
|
3473
3474
|
for (const { table, references } of belongsTo) {
|
|
3474
|
-
imports[
|
|
3475
|
+
imports[internal.getImportPath(tableInfo.path, table.path)] = table.className;
|
|
3475
3476
|
relations.push(
|
|
3476
3477
|
`${table.key}: this.belongsTo(() => ${table.className}, {`,
|
|
3477
3478
|
[
|
|
3478
|
-
`columns: [${references.foreignColumns.map(
|
|
3479
|
-
`references: [${references.columns.map(
|
|
3479
|
+
`columns: [${references.foreignColumns.map(internal.singleQuote).join(", ")}],`,
|
|
3480
|
+
`references: [${references.columns.map(internal.singleQuote).join(", ")}],`
|
|
3480
3481
|
],
|
|
3481
3482
|
"}),"
|
|
3482
3483
|
);
|
|
@@ -3486,12 +3487,12 @@ const appCodeGenTable = (tableInfos, fkeys, ast, baseTablePath, baseTableExporte
|
|
|
3486
3487
|
for (const { references } of ast.constraints) {
|
|
3487
3488
|
if (!references) continue;
|
|
3488
3489
|
const table = tableInfos[references.fnOrTable];
|
|
3489
|
-
imports[
|
|
3490
|
+
imports[internal.getImportPath(tableInfo.path, table.path)] = table.className;
|
|
3490
3491
|
relations.push(
|
|
3491
3492
|
`${table.key}: this.hasMany(() => ${table.className}, {`,
|
|
3492
3493
|
[
|
|
3493
|
-
`columns: [${references.columns.map(
|
|
3494
|
-
`references: [${references.foreignColumns.map(
|
|
3494
|
+
`columns: [${references.columns.map(internal.singleQuote).join(", ")}],`,
|
|
3495
|
+
`references: [${references.foreignColumns.map(internal.singleQuote).join(", ")}],`
|
|
3495
3496
|
],
|
|
3496
3497
|
"}),"
|
|
3497
3498
|
);
|
|
@@ -3513,7 +3514,7 @@ export class ${className} extends ${baseTableExportedAs} {`,
|
|
|
3513
3514
|
];
|
|
3514
3515
|
return {
|
|
3515
3516
|
...tableInfo,
|
|
3516
|
-
content: importsCode + "\n\n" +
|
|
3517
|
+
content: importsCode + "\n\n" + internal.codeToString(code, "", " ")
|
|
3517
3518
|
};
|
|
3518
3519
|
};
|
|
3519
3520
|
function importsToCode(imports) {
|
|
@@ -3539,14 +3540,14 @@ const appCodeGenUpdateDbFile = async (dbPath, tables, extensions, domains, curre
|
|
|
3539
3540
|
if (extensions.length) {
|
|
3540
3541
|
code += `
|
|
3541
3542
|
extensions: [${extensions.map(
|
|
3542
|
-
(ext) => ext.version ? `{ ${
|
|
3543
|
+
(ext) => ext.version ? `{ ${internal.quoteObjectKey(ext.name, false)}: '${ext.version}' }` : internal.singleQuote(ext.name)
|
|
3543
3544
|
).join(", ")}],`;
|
|
3544
3545
|
}
|
|
3545
3546
|
if (domains.length) {
|
|
3546
3547
|
code += `
|
|
3547
3548
|
domains: {
|
|
3548
3549
|
${domains.sort((a, b) => a.name > b.name ? 1 : -1).map(
|
|
3549
|
-
(ast) => `${
|
|
3550
|
+
(ast) => `${internal.quoteObjectKey(
|
|
3550
3551
|
ast.schema ? `${ast.schema}.${ast.name}` : ast.name,
|
|
3551
3552
|
false
|
|
3552
3553
|
)}: (t) => ${ast.baseType.toCode(
|
|
@@ -3614,7 +3615,7 @@ const makeTablesListChanges = (content, statements, object, tables, dbPath) => {
|
|
|
3614
3615
|
const tablesArr = Object.values(tables);
|
|
3615
3616
|
for (let i = 0; i < tablesArr.length; i++) {
|
|
3616
3617
|
const { path, className, key } = tablesArr[i];
|
|
3617
|
-
const importPath =
|
|
3618
|
+
const importPath = internal.getImportPath(dbPath, path);
|
|
3618
3619
|
imports += `
|
|
3619
3620
|
import { ${className} } from '${importPath}';`;
|
|
3620
3621
|
tablesList += `${i === 0 && prependComma ? "," : ""}
|
|
@@ -3755,7 +3756,7 @@ const pull = async (adapters, config) => {
|
|
|
3755
3756
|
await Promise.all(
|
|
3756
3757
|
pendingFileWrites.map(
|
|
3757
3758
|
([path2, content2, options]) => fs.writeFile(path2, content2, options).then(() => {
|
|
3758
|
-
config.logger?.log(`Created ${
|
|
3759
|
+
config.logger?.log(`Created ${internal.pathToLog(path2)}`);
|
|
3759
3760
|
})
|
|
3760
3761
|
)
|
|
3761
3762
|
);
|