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