orchid-orm 1.37.5 → 1.37.6
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 +72 -178
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +72 -178
- package/dist/index.mjs.map +1 -1
- package/dist/migrations.js +248 -521
- package/dist/migrations.js.map +1 -1
- package/dist/migrations.mjs +248 -521
- package/dist/migrations.mjs.map +1 -1
- package/package.json +4 -4
package/dist/migrations.mjs
CHANGED
|
@@ -58,13 +58,11 @@ const compareSqlExpressions = async (tableExpressions, adapter) => {
|
|
|
58
58
|
}
|
|
59
59
|
};
|
|
60
60
|
const promptCreateOrRename = (kind, name, drop, verifying) => {
|
|
61
|
-
if (verifying)
|
|
62
|
-
throw new AbortSignal();
|
|
61
|
+
if (verifying) throw new AbortSignal();
|
|
63
62
|
let hintPos = name.length + 4;
|
|
64
63
|
for (const from of drop) {
|
|
65
64
|
const value = from.length + 8 + name.length;
|
|
66
|
-
if (value > hintPos)
|
|
67
|
-
hintPos = value;
|
|
65
|
+
if (value > hintPos) hintPos = value;
|
|
68
66
|
}
|
|
69
67
|
let max = 0;
|
|
70
68
|
const add = name.length + 3;
|
|
@@ -183,25 +181,8 @@ const renameSchemaInStructures = (items, from, to) => {
|
|
|
183
181
|
}
|
|
184
182
|
};
|
|
185
183
|
|
|
186
|
-
var __defProp$9 = Object.defineProperty;
|
|
187
|
-
var __getOwnPropSymbols$9 = Object.getOwnPropertySymbols;
|
|
188
|
-
var __hasOwnProp$9 = Object.prototype.hasOwnProperty;
|
|
189
|
-
var __propIsEnum$9 = Object.prototype.propertyIsEnumerable;
|
|
190
|
-
var __defNormalProp$9 = (obj, key, value) => key in obj ? __defProp$9(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
191
|
-
var __spreadValues$9 = (a, b) => {
|
|
192
|
-
for (var prop in b || (b = {}))
|
|
193
|
-
if (__hasOwnProp$9.call(b, prop))
|
|
194
|
-
__defNormalProp$9(a, prop, b[prop]);
|
|
195
|
-
if (__getOwnPropSymbols$9)
|
|
196
|
-
for (var prop of __getOwnPropSymbols$9(b)) {
|
|
197
|
-
if (__propIsEnum$9.call(b, prop))
|
|
198
|
-
__defNormalProp$9(a, prop, b[prop]);
|
|
199
|
-
}
|
|
200
|
-
return a;
|
|
201
|
-
};
|
|
202
184
|
const processExtensions = (ast, dbStructure, currentSchema, extensions) => {
|
|
203
|
-
|
|
204
|
-
const codeExtensions = extensions == null ? void 0 : extensions.map((ext) => {
|
|
185
|
+
const codeExtensions = extensions?.map((ext) => {
|
|
205
186
|
const [schema, name] = getSchemaAndTableFromName(ext.name);
|
|
206
187
|
return { schema, name, version: ext.version };
|
|
207
188
|
});
|
|
@@ -210,14 +191,13 @@ const processExtensions = (ast, dbStructure, currentSchema, extensions) => {
|
|
|
210
191
|
let found = false;
|
|
211
192
|
for (let i = 0; i < codeExtensions.length; i++) {
|
|
212
193
|
const codeExt = codeExtensions[i];
|
|
213
|
-
if (dbExt.name === codeExt.name && dbExt.schemaName === (
|
|
194
|
+
if (dbExt.name === codeExt.name && dbExt.schemaName === (codeExt.schema ?? currentSchema) && (!codeExt.version || codeExt.version === dbExt.version)) {
|
|
214
195
|
found = true;
|
|
215
196
|
codeExtensions.splice(i, 1);
|
|
216
197
|
break;
|
|
217
198
|
}
|
|
218
199
|
}
|
|
219
|
-
if (found)
|
|
220
|
-
continue;
|
|
200
|
+
if (found) continue;
|
|
221
201
|
}
|
|
222
202
|
ast.push({
|
|
223
203
|
type: "extension",
|
|
@@ -227,35 +207,17 @@ const processExtensions = (ast, dbStructure, currentSchema, extensions) => {
|
|
|
227
207
|
version: dbExt.version
|
|
228
208
|
});
|
|
229
209
|
}
|
|
230
|
-
if (codeExtensions
|
|
210
|
+
if (codeExtensions?.length) {
|
|
231
211
|
ast.push(
|
|
232
|
-
...codeExtensions.map((ext) =>
|
|
212
|
+
...codeExtensions.map((ext) => ({
|
|
233
213
|
type: "extension",
|
|
234
|
-
action: "create"
|
|
235
|
-
|
|
214
|
+
action: "create",
|
|
215
|
+
...ext
|
|
216
|
+
}))
|
|
236
217
|
);
|
|
237
218
|
}
|
|
238
219
|
};
|
|
239
220
|
|
|
240
|
-
var __defProp$8 = Object.defineProperty;
|
|
241
|
-
var __defProps$7 = Object.defineProperties;
|
|
242
|
-
var __getOwnPropDescs$7 = Object.getOwnPropertyDescriptors;
|
|
243
|
-
var __getOwnPropSymbols$8 = Object.getOwnPropertySymbols;
|
|
244
|
-
var __hasOwnProp$8 = Object.prototype.hasOwnProperty;
|
|
245
|
-
var __propIsEnum$8 = Object.prototype.propertyIsEnumerable;
|
|
246
|
-
var __defNormalProp$8 = (obj, key, value) => key in obj ? __defProp$8(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
247
|
-
var __spreadValues$8 = (a, b) => {
|
|
248
|
-
for (var prop in b || (b = {}))
|
|
249
|
-
if (__hasOwnProp$8.call(b, prop))
|
|
250
|
-
__defNormalProp$8(a, prop, b[prop]);
|
|
251
|
-
if (__getOwnPropSymbols$8)
|
|
252
|
-
for (var prop of __getOwnPropSymbols$8(b)) {
|
|
253
|
-
if (__propIsEnum$8.call(b, prop))
|
|
254
|
-
__defNormalProp$8(a, prop, b[prop]);
|
|
255
|
-
}
|
|
256
|
-
return a;
|
|
257
|
-
};
|
|
258
|
-
var __spreadProps$7 = (a, b) => __defProps$7(a, __getOwnPropDescs$7(b));
|
|
259
221
|
const processColumns = async (adapter, config, structureToAstCtx, dbStructure, domainsMap, changeTableData, ast, currentSchema, compareSql, typeCastsCache, verifying) => {
|
|
260
222
|
const { dbTable } = changeTableData;
|
|
261
223
|
const dbColumns = Object.fromEntries(
|
|
@@ -295,7 +257,6 @@ const processColumns = async (adapter, config, structureToAstCtx, dbStructure, d
|
|
|
295
257
|
dropColumns(changeTableData, columnsToDrop);
|
|
296
258
|
};
|
|
297
259
|
const groupColumns = (structureToAstCtx, dbStructure, domainsMap, dbColumns, changeTableData) => {
|
|
298
|
-
var _a;
|
|
299
260
|
const columnsToAdd = [];
|
|
300
261
|
const columnsToDrop = [];
|
|
301
262
|
const columnsToChange = /* @__PURE__ */ new Map();
|
|
@@ -304,9 +265,8 @@ const groupColumns = (structureToAstCtx, dbStructure, domainsMap, dbColumns, cha
|
|
|
304
265
|
const checks = getDbTableColumnsChecks(changeTableData.dbTableData);
|
|
305
266
|
for (const key in codeTable.shape) {
|
|
306
267
|
const column = codeTable.shape[key];
|
|
307
|
-
if (!column.dataType)
|
|
308
|
-
|
|
309
|
-
const name = (_a = column.data.name) != null ? _a : key;
|
|
268
|
+
if (!column.dataType) continue;
|
|
269
|
+
const name = column.data.name ?? key;
|
|
310
270
|
if (dbColumns[name]) {
|
|
311
271
|
columnsToChange.set(key, { key, dbName: name, column });
|
|
312
272
|
columnsToChangeByDbName.set(name, true);
|
|
@@ -315,8 +275,7 @@ const groupColumns = (structureToAstCtx, dbStructure, domainsMap, dbColumns, cha
|
|
|
315
275
|
}
|
|
316
276
|
}
|
|
317
277
|
for (const name in dbColumns) {
|
|
318
|
-
if (columnsToChangeByDbName.has(name))
|
|
319
|
-
continue;
|
|
278
|
+
if (columnsToChangeByDbName.has(name)) continue;
|
|
320
279
|
const [key, column] = dbColumnToAst(
|
|
321
280
|
structureToAstCtx,
|
|
322
281
|
dbStructure,
|
|
@@ -340,10 +299,9 @@ const addOrRenameColumns = async (config, dbStructure, {
|
|
|
340
299
|
schema,
|
|
341
300
|
changeTableAst: { name: tableName, shape }
|
|
342
301
|
}, columnsToAdd, columnsToDrop, columnsToChange, verifying) => {
|
|
343
|
-
var _a, _b, _c;
|
|
344
302
|
for (const { key, column } of columnsToAdd) {
|
|
345
303
|
if (columnsToDrop.length) {
|
|
346
|
-
const codeName =
|
|
304
|
+
const codeName = column.data.name ?? key;
|
|
347
305
|
const index = await promptCreateOrRename(
|
|
348
306
|
"column",
|
|
349
307
|
codeName,
|
|
@@ -353,7 +311,7 @@ const addOrRenameColumns = async (config, dbStructure, {
|
|
|
353
311
|
if (index) {
|
|
354
312
|
const drop = columnsToDrop[index - 1];
|
|
355
313
|
columnsToDrop.splice(index - 1, 1);
|
|
356
|
-
const from =
|
|
314
|
+
const from = drop.column.data.name ?? drop.key;
|
|
357
315
|
columnsToChange.set(from, {
|
|
358
316
|
key,
|
|
359
317
|
dbName: from,
|
|
@@ -371,7 +329,7 @@ const addOrRenameColumns = async (config, dbStructure, {
|
|
|
371
329
|
}
|
|
372
330
|
}
|
|
373
331
|
for (const c of dbTableData.constraints) {
|
|
374
|
-
if (
|
|
332
|
+
if (c.check?.columns) {
|
|
375
333
|
renameColumn(c.check.columns, from, to);
|
|
376
334
|
}
|
|
377
335
|
if (c.references) {
|
|
@@ -401,7 +359,6 @@ const dropColumns = ({ changeTableAst: { shape } }, columnsToDrop) => {
|
|
|
401
359
|
}
|
|
402
360
|
};
|
|
403
361
|
const changeColumns = async (adapter, config, structureToAstCtx, dbStructure, domainsMap, ast, currentSchema, dbColumns, columnsToChange, compareSql, changeTableData, typeCastsCache, verifying) => {
|
|
404
|
-
var _a;
|
|
405
362
|
for (const [
|
|
406
363
|
key,
|
|
407
364
|
{ key: codeKey, dbName, column: codeColumn }
|
|
@@ -441,7 +398,7 @@ const changeColumns = async (adapter, config, structureToAstCtx, dbStructure, do
|
|
|
441
398
|
}
|
|
442
399
|
];
|
|
443
400
|
} else if (action !== "recreate") {
|
|
444
|
-
const to =
|
|
401
|
+
const to = codeColumn.data.name ?? codeKey;
|
|
445
402
|
if (dbName !== to) {
|
|
446
403
|
changeTableData.changeTableAst.shape[config.snakeCase ? dbName === toSnakeCase(codeKey) ? codeKey : dbName : dbName] = {
|
|
447
404
|
type: "rename",
|
|
@@ -452,7 +409,6 @@ const changeColumns = async (adapter, config, structureToAstCtx, dbStructure, do
|
|
|
452
409
|
}
|
|
453
410
|
};
|
|
454
411
|
const compareColumns = async (adapter, domainsMap, ast, currentSchema, compareSql, changeTableData, typeCastsCache, verifying, key, dbName, dbColumn, codeColumn) => {
|
|
455
|
-
var _a, _b, _c, _d;
|
|
456
412
|
const dbType = getColumnDbType(dbColumn, currentSchema);
|
|
457
413
|
const codeType = getColumnDbType(codeColumn, currentSchema);
|
|
458
414
|
if (dbColumn instanceof ArrayColumn && codeColumn instanceof ArrayColumn) {
|
|
@@ -490,12 +446,11 @@ JOIN pg_type AS t ON t.oid = casttarget`);
|
|
|
490
446
|
}
|
|
491
447
|
typeCastsCache.value = typeCasts;
|
|
492
448
|
}
|
|
493
|
-
const dbBaseType = dbColumn instanceof DomainColumn ?
|
|
494
|
-
const codeBaseType = codeColumn instanceof DomainColumn ?
|
|
495
|
-
if (!
|
|
449
|
+
const dbBaseType = dbColumn instanceof DomainColumn ? domainsMap[dbColumn.dataType]?.dataType : dbType;
|
|
450
|
+
const codeBaseType = codeColumn instanceof DomainColumn ? domainsMap[codeColumn.dataType]?.dataType : codeType;
|
|
451
|
+
if (!typeCasts.get(dbBaseType)?.has(codeBaseType)) {
|
|
496
452
|
if (!(dbColumn instanceof EnumColumn) || !(codeColumn instanceof EnumColumn) || !deepCompare(dbColumn.options, codeColumn.options)) {
|
|
497
|
-
if (verifying)
|
|
498
|
-
throw new AbortSignal();
|
|
453
|
+
if (verifying) throw new AbortSignal();
|
|
499
454
|
const tableName = concatSchemaAndName(changeTableData.changeTableAst);
|
|
500
455
|
const abort = await promptSelect({
|
|
501
456
|
message: `Cannot cast type of ${tableName}'s column ${key} from ${dbType} to ${codeType}`,
|
|
@@ -538,13 +493,14 @@ JOIN pg_type AS t ON t.oid = casttarget`);
|
|
|
538
493
|
}
|
|
539
494
|
if (!deepCompare(
|
|
540
495
|
dbData.identity,
|
|
541
|
-
codeData.identity ?
|
|
496
|
+
codeData.identity ? {
|
|
542
497
|
always: false,
|
|
543
498
|
start: 1,
|
|
544
499
|
increment: 1,
|
|
545
500
|
cache: 1,
|
|
546
|
-
cycle: false
|
|
547
|
-
|
|
501
|
+
cycle: false,
|
|
502
|
+
...codeData.identity ?? {}
|
|
503
|
+
} : void 0
|
|
548
504
|
)) {
|
|
549
505
|
return "change";
|
|
550
506
|
}
|
|
@@ -584,12 +540,13 @@ JOIN pg_type AS t ON t.oid = casttarget`);
|
|
|
584
540
|
const changeColumn = (changeTableData, key, dbName, dbColumn, codeColumn) => {
|
|
585
541
|
dbColumn.data.as = codeColumn.data.as = void 0;
|
|
586
542
|
const simpleCodeColumn = Object.create(codeColumn);
|
|
587
|
-
simpleCodeColumn.data =
|
|
543
|
+
simpleCodeColumn.data = {
|
|
544
|
+
...codeColumn.data,
|
|
588
545
|
primaryKey: void 0,
|
|
589
546
|
indexes: void 0,
|
|
590
547
|
foreignKeys: void 0,
|
|
591
548
|
check: void 0
|
|
592
|
-
}
|
|
549
|
+
};
|
|
593
550
|
changeTableData.changingColumns[dbName] = {
|
|
594
551
|
from: dbColumn,
|
|
595
552
|
to: simpleCodeColumn
|
|
@@ -725,9 +682,8 @@ const processDomains = async (ast, adapter, structureToAstCtx, domainsMap, dbStr
|
|
|
725
682
|
}
|
|
726
683
|
};
|
|
727
684
|
const makeComparableDomain = (currentSchema, schemaName, name, column) => {
|
|
728
|
-
var _a;
|
|
729
685
|
let arrayDims = 0;
|
|
730
|
-
const isNullable =
|
|
686
|
+
const isNullable = column.data.isNullable ?? false;
|
|
731
687
|
let inner = column;
|
|
732
688
|
while (inner instanceof ArrayColumn) {
|
|
733
689
|
inner = inner.data.item;
|
|
@@ -784,24 +740,7 @@ const createAst = (codeDomain) => ({
|
|
|
784
740
|
baseType: codeDomain.column
|
|
785
741
|
});
|
|
786
742
|
|
|
787
|
-
var __defProp$7 = Object.defineProperty;
|
|
788
|
-
var __getOwnPropSymbols$7 = Object.getOwnPropertySymbols;
|
|
789
|
-
var __hasOwnProp$7 = Object.prototype.hasOwnProperty;
|
|
790
|
-
var __propIsEnum$7 = Object.prototype.propertyIsEnumerable;
|
|
791
|
-
var __defNormalProp$7 = (obj, key, value) => key in obj ? __defProp$7(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
792
|
-
var __spreadValues$7 = (a, b) => {
|
|
793
|
-
for (var prop in b || (b = {}))
|
|
794
|
-
if (__hasOwnProp$7.call(b, prop))
|
|
795
|
-
__defNormalProp$7(a, prop, b[prop]);
|
|
796
|
-
if (__getOwnPropSymbols$7)
|
|
797
|
-
for (var prop of __getOwnPropSymbols$7(b)) {
|
|
798
|
-
if (__propIsEnum$7.call(b, prop))
|
|
799
|
-
__defNormalProp$7(a, prop, b[prop]);
|
|
800
|
-
}
|
|
801
|
-
return a;
|
|
802
|
-
};
|
|
803
743
|
const processEnums = async (ast, enums, dbStructure, currentSchema, verifying) => {
|
|
804
|
-
var _a, _b;
|
|
805
744
|
const createEnums = [];
|
|
806
745
|
const dropEnums = [];
|
|
807
746
|
for (const [, codeEnum] of enums) {
|
|
@@ -824,7 +763,7 @@ const processEnums = async (ast, enums, dbStructure, currentSchema, verifying) =
|
|
|
824
763
|
const codeEnum2 = createEnums[i];
|
|
825
764
|
createEnums.splice(i, 1);
|
|
826
765
|
const fromSchema = dbEnum.schemaName;
|
|
827
|
-
const toSchema =
|
|
766
|
+
const toSchema = codeEnum2.schema ?? currentSchema;
|
|
828
767
|
renameColumnsTypeSchema(dbStructure, fromSchema, toSchema);
|
|
829
768
|
ast.push({
|
|
830
769
|
type: "renameType",
|
|
@@ -852,7 +791,7 @@ const processEnums = async (ast, enums, dbStructure, currentSchema, verifying) =
|
|
|
852
791
|
dropEnums.splice(index - 1, 1);
|
|
853
792
|
const fromSchema = dbEnum.schemaName;
|
|
854
793
|
const from = dbEnum.name;
|
|
855
|
-
const toSchema =
|
|
794
|
+
const toSchema = codeEnum.schema ?? currentSchema;
|
|
856
795
|
const to = codeEnum.name;
|
|
857
796
|
if (fromSchema !== toSchema) {
|
|
858
797
|
renameColumnsTypeSchema(dbStructure, fromSchema, toSchema);
|
|
@@ -876,10 +815,11 @@ const processEnums = async (ast, enums, dbStructure, currentSchema, verifying) =
|
|
|
876
815
|
continue;
|
|
877
816
|
}
|
|
878
817
|
}
|
|
879
|
-
ast.push(
|
|
818
|
+
ast.push({
|
|
880
819
|
type: "enum",
|
|
881
|
-
action: "create"
|
|
882
|
-
|
|
820
|
+
action: "create",
|
|
821
|
+
...codeEnum
|
|
822
|
+
});
|
|
883
823
|
}
|
|
884
824
|
for (const dbEnum of dropEnums) {
|
|
885
825
|
ast.push({
|
|
@@ -937,33 +877,13 @@ const renameColumnsTypeSchema = (dbStructure, from, to) => {
|
|
|
937
877
|
}
|
|
938
878
|
};
|
|
939
879
|
|
|
940
|
-
var __defProp$6 = Object.defineProperty;
|
|
941
|
-
var __defProps$6 = Object.defineProperties;
|
|
942
|
-
var __getOwnPropDescs$6 = Object.getOwnPropertyDescriptors;
|
|
943
|
-
var __getOwnPropSymbols$6 = Object.getOwnPropertySymbols;
|
|
944
|
-
var __hasOwnProp$6 = Object.prototype.hasOwnProperty;
|
|
945
|
-
var __propIsEnum$6 = Object.prototype.propertyIsEnumerable;
|
|
946
|
-
var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
947
|
-
var __spreadValues$6 = (a, b) => {
|
|
948
|
-
for (var prop in b || (b = {}))
|
|
949
|
-
if (__hasOwnProp$6.call(b, prop))
|
|
950
|
-
__defNormalProp$6(a, prop, b[prop]);
|
|
951
|
-
if (__getOwnPropSymbols$6)
|
|
952
|
-
for (var prop of __getOwnPropSymbols$6(b)) {
|
|
953
|
-
if (__propIsEnum$6.call(b, prop))
|
|
954
|
-
__defNormalProp$6(a, prop, b[prop]);
|
|
955
|
-
}
|
|
956
|
-
return a;
|
|
957
|
-
};
|
|
958
|
-
var __spreadProps$6 = (a, b) => __defProps$6(a, __getOwnPropDescs$6(b));
|
|
959
880
|
const processPrimaryKey = (config, changeTableData) => {
|
|
960
|
-
var _a;
|
|
961
881
|
const { codeTable } = changeTableData;
|
|
962
882
|
const columnsPrimaryKey = [];
|
|
963
883
|
for (const key in codeTable.shape) {
|
|
964
884
|
const column = codeTable.shape[key];
|
|
965
885
|
if (column.data.primaryKey) {
|
|
966
|
-
columnsPrimaryKey.push({ key, name:
|
|
886
|
+
columnsPrimaryKey.push({ key, name: column.data.name ?? key });
|
|
967
887
|
}
|
|
968
888
|
}
|
|
969
889
|
changePrimaryKey(config, columnsPrimaryKey, changeTableData);
|
|
@@ -975,15 +895,14 @@ const changePrimaryKey = (config, columnsPrimaryKey, {
|
|
|
975
895
|
changeTableAst: { shape, add, drop },
|
|
976
896
|
changingColumns
|
|
977
897
|
}) => {
|
|
978
|
-
var _a, _b, _c;
|
|
979
898
|
const tablePrimaryKey = codeTable.internal.tableData.primaryKey;
|
|
980
899
|
const primaryKey = [
|
|
981
900
|
.../* @__PURE__ */ new Set([
|
|
982
901
|
...columnsPrimaryKey,
|
|
983
|
-
...(
|
|
902
|
+
...(config.snakeCase ? tablePrimaryKey?.columns.map((key) => ({
|
|
984
903
|
key,
|
|
985
904
|
name: toSnakeCase(key)
|
|
986
|
-
})) : tablePrimaryKey
|
|
905
|
+
})) : tablePrimaryKey?.columns.map((key) => ({ key, name: key }))) ?? []
|
|
987
906
|
])
|
|
988
907
|
];
|
|
989
908
|
if (dbPrimaryKey && primaryKey.length === dbPrimaryKey.columns.length && !primaryKey.some(
|
|
@@ -994,14 +913,13 @@ const changePrimaryKey = (config, columnsPrimaryKey, {
|
|
|
994
913
|
const changes = shape[key] && toArray(shape[key]);
|
|
995
914
|
if (changes) {
|
|
996
915
|
for (const change of changes) {
|
|
997
|
-
if (change.type !== "change")
|
|
998
|
-
continue;
|
|
916
|
+
if (change.type !== "change") continue;
|
|
999
917
|
if (change.from.column) {
|
|
1000
918
|
change.from.column.data.primaryKey = void 0;
|
|
1001
919
|
}
|
|
1002
920
|
if (change.to.column) {
|
|
1003
921
|
const column = Object.create(change.to.column);
|
|
1004
|
-
column.data =
|
|
922
|
+
column.data = { ...column.data, primaryKey: void 0 };
|
|
1005
923
|
change.to.column = column;
|
|
1006
924
|
}
|
|
1007
925
|
}
|
|
@@ -1009,15 +927,15 @@ const changePrimaryKey = (config, columnsPrimaryKey, {
|
|
|
1009
927
|
}
|
|
1010
928
|
return;
|
|
1011
929
|
}
|
|
1012
|
-
const toDrop = dbPrimaryKey
|
|
930
|
+
const toDrop = dbPrimaryKey?.columns.filter(
|
|
1013
931
|
(key) => !checkForColumnAddOrDrop(shape, key)
|
|
1014
932
|
);
|
|
1015
|
-
if (toDrop
|
|
933
|
+
if (toDrop?.length) {
|
|
1016
934
|
if (toDrop.length === 1 && changingColumns[toDrop[0]]) {
|
|
1017
935
|
const column = changingColumns[toDrop[0]];
|
|
1018
|
-
column.from.data.primaryKey =
|
|
936
|
+
column.from.data.primaryKey = dbPrimaryKey?.name ?? true;
|
|
1019
937
|
} else {
|
|
1020
|
-
drop.primaryKey = { columns: toDrop, name: dbPrimaryKey
|
|
938
|
+
drop.primaryKey = { columns: toDrop, name: dbPrimaryKey?.name };
|
|
1021
939
|
}
|
|
1022
940
|
}
|
|
1023
941
|
const toAdd = primaryKey.filter(
|
|
@@ -1026,11 +944,11 @@ const changePrimaryKey = (config, columnsPrimaryKey, {
|
|
|
1026
944
|
if (toAdd.length) {
|
|
1027
945
|
if (toAdd.length === 1 && changingColumns[toAdd[0].name]) {
|
|
1028
946
|
const column = changingColumns[toAdd[0].name];
|
|
1029
|
-
column.to.data.primaryKey =
|
|
947
|
+
column.to.data.primaryKey = tablePrimaryKey?.name ?? true;
|
|
1030
948
|
} else {
|
|
1031
949
|
add.primaryKey = {
|
|
1032
950
|
columns: toAdd.map((c) => c.key),
|
|
1033
|
-
name: tablePrimaryKey
|
|
951
|
+
name: tablePrimaryKey?.name
|
|
1034
952
|
};
|
|
1035
953
|
}
|
|
1036
954
|
}
|
|
@@ -1041,51 +959,19 @@ const renamePrimaryKey = ({
|
|
|
1041
959
|
schema,
|
|
1042
960
|
delayedAst
|
|
1043
961
|
}) => {
|
|
1044
|
-
var _a, _b;
|
|
1045
962
|
const tablePrimaryKey = codeTable.internal.tableData.primaryKey;
|
|
1046
|
-
if (dbPrimaryKey && tablePrimaryKey &&
|
|
963
|
+
if (dbPrimaryKey && tablePrimaryKey && dbPrimaryKey?.name !== tablePrimaryKey?.name) {
|
|
1047
964
|
delayedAst.push({
|
|
1048
965
|
type: "renameTableItem",
|
|
1049
966
|
kind: "CONSTRAINT",
|
|
1050
967
|
tableSchema: schema,
|
|
1051
968
|
tableName: codeTable.table,
|
|
1052
|
-
from:
|
|
1053
|
-
to:
|
|
969
|
+
from: dbPrimaryKey.name ?? `${codeTable.table}_pkey`,
|
|
970
|
+
to: tablePrimaryKey.name ?? `${codeTable}_pkey`
|
|
1054
971
|
});
|
|
1055
972
|
}
|
|
1056
973
|
};
|
|
1057
974
|
|
|
1058
|
-
var __defProp$5 = Object.defineProperty;
|
|
1059
|
-
var __defProps$5 = Object.defineProperties;
|
|
1060
|
-
var __getOwnPropDescs$5 = Object.getOwnPropertyDescriptors;
|
|
1061
|
-
var __getOwnPropSymbols$5 = Object.getOwnPropertySymbols;
|
|
1062
|
-
var __hasOwnProp$5 = Object.prototype.hasOwnProperty;
|
|
1063
|
-
var __propIsEnum$5 = Object.prototype.propertyIsEnumerable;
|
|
1064
|
-
var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1065
|
-
var __spreadValues$5 = (a, b) => {
|
|
1066
|
-
for (var prop in b || (b = {}))
|
|
1067
|
-
if (__hasOwnProp$5.call(b, prop))
|
|
1068
|
-
__defNormalProp$5(a, prop, b[prop]);
|
|
1069
|
-
if (__getOwnPropSymbols$5)
|
|
1070
|
-
for (var prop of __getOwnPropSymbols$5(b)) {
|
|
1071
|
-
if (__propIsEnum$5.call(b, prop))
|
|
1072
|
-
__defNormalProp$5(a, prop, b[prop]);
|
|
1073
|
-
}
|
|
1074
|
-
return a;
|
|
1075
|
-
};
|
|
1076
|
-
var __spreadProps$5 = (a, b) => __defProps$5(a, __getOwnPropDescs$5(b));
|
|
1077
|
-
var __objRest = (source, exclude) => {
|
|
1078
|
-
var target = {};
|
|
1079
|
-
for (var prop in source)
|
|
1080
|
-
if (__hasOwnProp$5.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
1081
|
-
target[prop] = source[prop];
|
|
1082
|
-
if (source != null && __getOwnPropSymbols$5)
|
|
1083
|
-
for (var prop of __getOwnPropSymbols$5(source)) {
|
|
1084
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum$5.call(source, prop))
|
|
1085
|
-
target[prop] = source[prop];
|
|
1086
|
-
}
|
|
1087
|
-
return target;
|
|
1088
|
-
};
|
|
1089
975
|
const processIndexes = (config, changeTableData, ast, compareExpressions) => {
|
|
1090
976
|
const codeIndexes = collectCodeIndexes(config, changeTableData);
|
|
1091
977
|
const codeComparableIndexes = collectCodeComparableIndexes(
|
|
@@ -1102,8 +988,7 @@ const processIndexes = (config, changeTableData, ast, compareExpressions) => {
|
|
|
1102
988
|
const hasAddedOrDroppedColumn = dbIndex.columns.some(
|
|
1103
989
|
(column) => "column" in column && checkForColumnAddOrDrop(shape, column.column)
|
|
1104
990
|
);
|
|
1105
|
-
if (hasAddedOrDroppedColumn)
|
|
1106
|
-
continue;
|
|
991
|
+
if (hasAddedOrDroppedColumn) continue;
|
|
1107
992
|
normalizeIndex(dbIndex);
|
|
1108
993
|
const { found, rename, foundAndHasSql } = findMatchingIndex(
|
|
1109
994
|
dbIndex,
|
|
@@ -1130,8 +1015,7 @@ const processIndexes = (config, changeTableData, ast, compareExpressions) => {
|
|
|
1130
1015
|
const compare = [];
|
|
1131
1016
|
for (let i = 0; i < dbIndex.columns.length; i++) {
|
|
1132
1017
|
const column = dbIndex.columns[i];
|
|
1133
|
-
if (!("expression" in column))
|
|
1134
|
-
continue;
|
|
1018
|
+
if (!("expression" in column)) continue;
|
|
1135
1019
|
compare.push({
|
|
1136
1020
|
inDb: column.expression,
|
|
1137
1021
|
inCode: found.map(
|
|
@@ -1182,63 +1066,60 @@ const processIndexes = (config, changeTableData, ast, compareExpressions) => {
|
|
|
1182
1066
|
if (indexesToAdd.length) {
|
|
1183
1067
|
addIndexes(
|
|
1184
1068
|
changeTableData,
|
|
1185
|
-
indexesToAdd.map((x) =>
|
|
1069
|
+
indexesToAdd.map((x) => ({
|
|
1070
|
+
...x,
|
|
1186
1071
|
columns: x.columnKeys,
|
|
1187
1072
|
columnNames: x.columns,
|
|
1188
|
-
options: x.options.include ?
|
|
1073
|
+
options: x.options.include ? { ...x.options, include: x.includeKeys } : x.options
|
|
1189
1074
|
}))
|
|
1190
1075
|
);
|
|
1191
1076
|
}
|
|
1192
1077
|
};
|
|
1193
1078
|
const collectCodeIndexes = (config, { codeTable, changeTableAst: { shape } }) => {
|
|
1194
|
-
var _a;
|
|
1195
1079
|
const codeIndexes = [];
|
|
1196
1080
|
for (const key in codeTable.shape) {
|
|
1197
1081
|
const column = codeTable.shape[key];
|
|
1198
|
-
if (!column.data.indexes)
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
if (checkForColumnAddOrDrop(shape, name))
|
|
1202
|
-
continue;
|
|
1082
|
+
if (!column.data.indexes) continue;
|
|
1083
|
+
const name = column.data.name ?? key;
|
|
1084
|
+
if (checkForColumnAddOrDrop(shape, name)) continue;
|
|
1203
1085
|
codeIndexes.push(
|
|
1204
1086
|
...column.data.indexes.map(
|
|
1205
|
-
(
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
includeKeys: options.include
|
|
1227
|
-
});
|
|
1228
|
-
}
|
|
1087
|
+
({
|
|
1088
|
+
options: { collate, opclass, order, weight, ...options },
|
|
1089
|
+
...index
|
|
1090
|
+
}) => ({
|
|
1091
|
+
columns: [
|
|
1092
|
+
{
|
|
1093
|
+
collate,
|
|
1094
|
+
opclass,
|
|
1095
|
+
order,
|
|
1096
|
+
weight,
|
|
1097
|
+
column: name
|
|
1098
|
+
}
|
|
1099
|
+
],
|
|
1100
|
+
...index,
|
|
1101
|
+
options: options.include ? config.snakeCase ? {
|
|
1102
|
+
...options,
|
|
1103
|
+
include: toArray(options.include).map(toSnakeCase)
|
|
1104
|
+
} : options : options,
|
|
1105
|
+
columnKeys: [{ collate, opclass, order, weight, column: key }],
|
|
1106
|
+
includeKeys: options.include
|
|
1107
|
+
})
|
|
1229
1108
|
)
|
|
1230
1109
|
);
|
|
1231
1110
|
}
|
|
1232
1111
|
if (codeTable.internal.tableData.indexes) {
|
|
1233
1112
|
codeIndexes.push(
|
|
1234
|
-
...codeTable.internal.tableData.indexes.map((x) =>
|
|
1113
|
+
...codeTable.internal.tableData.indexes.map((x) => ({
|
|
1114
|
+
...x,
|
|
1235
1115
|
columns: config.snakeCase ? x.columns.map(
|
|
1236
|
-
(c) => "column" in c ?
|
|
1116
|
+
(c) => "column" in c ? { ...c, column: toSnakeCase(c.column) } : c
|
|
1237
1117
|
) : x.columns,
|
|
1238
1118
|
columnKeys: x.columns,
|
|
1239
|
-
options: x.options.include && config.snakeCase ?
|
|
1119
|
+
options: x.options.include && config.snakeCase ? {
|
|
1120
|
+
...x.options,
|
|
1240
1121
|
include: toArray(x.options.include).map(toSnakeCase)
|
|
1241
|
-
}
|
|
1122
|
+
} : x.options,
|
|
1242
1123
|
includeKeys: x.options.include
|
|
1243
1124
|
}))
|
|
1244
1125
|
);
|
|
@@ -1248,35 +1129,34 @@ const collectCodeIndexes = (config, { codeTable, changeTableAst: { shape } }) =>
|
|
|
1248
1129
|
const collectCodeComparableIndexes = (config, codeIndexes) => {
|
|
1249
1130
|
return codeIndexes.map((codeIndex) => {
|
|
1250
1131
|
normalizeIndex(codeIndex.options);
|
|
1251
|
-
return indexToComparable(
|
|
1132
|
+
return indexToComparable({
|
|
1133
|
+
...codeIndex.options,
|
|
1252
1134
|
include: codeIndex.options.include === void 0 ? void 0 : config.snakeCase ? toArray(codeIndex.options.include).map(toSnakeCase) : toArray(codeIndex.options.include),
|
|
1253
1135
|
columns: codeIndex.columns,
|
|
1254
1136
|
name: codeIndex.name,
|
|
1255
1137
|
columnKeys: codeIndex.columnKeys,
|
|
1256
1138
|
includeKeys: codeIndex.includeKeys
|
|
1257
|
-
})
|
|
1139
|
+
});
|
|
1258
1140
|
});
|
|
1259
1141
|
};
|
|
1260
1142
|
const normalizeIndex = (index) => {
|
|
1261
|
-
if (index.using === "btree")
|
|
1262
|
-
|
|
1263
|
-
if (
|
|
1264
|
-
index.unique = void 0;
|
|
1265
|
-
if (index.nullsNotDistinct === false)
|
|
1266
|
-
index.nullsNotDistinct = void 0;
|
|
1143
|
+
if (index.using === "btree") index.using = void 0;
|
|
1144
|
+
if (!index.unique) index.unique = void 0;
|
|
1145
|
+
if (index.nullsNotDistinct === false) index.nullsNotDistinct = void 0;
|
|
1267
1146
|
};
|
|
1268
1147
|
const indexToComparable = (index) => {
|
|
1269
1148
|
let hasExpression = false;
|
|
1270
1149
|
const columns = index.columns.map((column) => {
|
|
1271
|
-
const result =
|
|
1150
|
+
const result = {
|
|
1151
|
+
...column,
|
|
1272
1152
|
expression: void 0,
|
|
1273
1153
|
hasExpression: "expression" in column
|
|
1274
|
-
}
|
|
1275
|
-
if (result.hasExpression)
|
|
1276
|
-
hasExpression = true;
|
|
1154
|
+
};
|
|
1155
|
+
if (result.hasExpression) hasExpression = true;
|
|
1277
1156
|
return result;
|
|
1278
1157
|
});
|
|
1279
|
-
return
|
|
1158
|
+
return {
|
|
1159
|
+
...index,
|
|
1280
1160
|
schemaName: void 0,
|
|
1281
1161
|
tableName: void 0,
|
|
1282
1162
|
with: void 0,
|
|
@@ -1285,7 +1165,7 @@ const indexToComparable = (index) => {
|
|
|
1285
1165
|
hasWhere: !!index.where,
|
|
1286
1166
|
columns,
|
|
1287
1167
|
hasExpression
|
|
1288
|
-
}
|
|
1168
|
+
};
|
|
1289
1169
|
};
|
|
1290
1170
|
const findMatchingIndex = (dbIndex, codeComparableIndexes, codeIndexes, skipCodeIndexes, tableName, config) => {
|
|
1291
1171
|
const dbComparableIndex = indexToComparable(dbIndex);
|
|
@@ -1301,27 +1181,26 @@ const findMatchingIndex = (dbIndex, codeComparableIndexes, codeIndexes, skipCode
|
|
|
1301
1181
|
return { found, rename, foundAndHasSql };
|
|
1302
1182
|
};
|
|
1303
1183
|
const findMatchingIndexWithoutSql = (dbIndex, codeComparableIndexes, codeIndexes, skipCodeIndexes, tableName, config) => {
|
|
1304
|
-
var _c, _d;
|
|
1305
1184
|
const found = [];
|
|
1306
1185
|
const rename = [];
|
|
1307
|
-
const
|
|
1186
|
+
const { columns: dbColumns, ...dbIndexWithoutColumns } = dbIndex;
|
|
1308
1187
|
for (let i = 0; i < codeComparableIndexes.length; i++) {
|
|
1309
|
-
if (skipCodeIndexes.has(i))
|
|
1310
|
-
|
|
1311
|
-
const _b = codeComparableIndexes[i], { columns: codeColumns } = _b, codeIndex = __objRest(_b, ["columns"]);
|
|
1188
|
+
if (skipCodeIndexes.has(i)) continue;
|
|
1189
|
+
const { columns: codeColumns, ...codeIndex } = codeComparableIndexes[i];
|
|
1312
1190
|
if (dbColumns.length === codeColumns.length && !dbColumns.some((dbColumn, i2) => !deepCompare(dbColumn, codeColumns[i2]))) {
|
|
1313
1191
|
let a = dbIndexWithoutColumns;
|
|
1314
1192
|
let b = codeIndex;
|
|
1315
|
-
const codeName =
|
|
1193
|
+
const codeName = b.name ?? getIndexName(tableName, dbColumns);
|
|
1316
1194
|
if (a.name !== b.name) {
|
|
1317
|
-
a =
|
|
1318
|
-
b =
|
|
1195
|
+
a = { ...a, name: void 0 };
|
|
1196
|
+
b = {
|
|
1197
|
+
...b,
|
|
1319
1198
|
name: void 0,
|
|
1320
1199
|
columnKeys: void 0,
|
|
1321
1200
|
includeKeys: void 0
|
|
1322
|
-
}
|
|
1201
|
+
};
|
|
1323
1202
|
if (a.language && !b.language) {
|
|
1324
|
-
b.language =
|
|
1203
|
+
b.language = config.language ?? "english";
|
|
1325
1204
|
}
|
|
1326
1205
|
if (deepCompare(a, b)) {
|
|
1327
1206
|
found.push(codeIndexes[i]);
|
|
@@ -1330,11 +1209,12 @@ const findMatchingIndexWithoutSql = (dbIndex, codeComparableIndexes, codeIndexes
|
|
|
1330
1209
|
);
|
|
1331
1210
|
}
|
|
1332
1211
|
} else {
|
|
1333
|
-
const
|
|
1212
|
+
const {
|
|
1334
1213
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1214
|
+
columnKeys,
|
|
1215
|
+
includeKeys,
|
|
1216
|
+
...codeIndexWithoutKeys
|
|
1217
|
+
} = codeIndex;
|
|
1338
1218
|
if (deepCompare(dbIndexWithoutColumns, codeIndexWithoutKeys)) {
|
|
1339
1219
|
found.push(codeIndexes[i]);
|
|
1340
1220
|
rename.push(void 0);
|
|
@@ -1355,20 +1235,20 @@ const handleIndexChange = ({
|
|
|
1355
1235
|
changingColumns,
|
|
1356
1236
|
delayedAst
|
|
1357
1237
|
}, dbIndex, dbColumns, found, rename) => {
|
|
1358
|
-
var _a, _b
|
|
1238
|
+
var _a, _b;
|
|
1359
1239
|
if (!found) {
|
|
1360
1240
|
const indexName = dbIndex.name === getIndexName(changeTableAst.name, dbColumns) ? void 0 : dbIndex.name;
|
|
1361
1241
|
if (dbColumns.length === 1 && "column" in dbColumns[0]) {
|
|
1362
1242
|
const column = changingColumns[dbColumns[0].column];
|
|
1363
1243
|
if (column) {
|
|
1364
|
-
((
|
|
1244
|
+
((_a = column.from.data).indexes ?? (_a.indexes = [])).push({
|
|
1365
1245
|
options: dbIndex,
|
|
1366
1246
|
name: indexName
|
|
1367
1247
|
});
|
|
1368
1248
|
return;
|
|
1369
1249
|
}
|
|
1370
1250
|
}
|
|
1371
|
-
((
|
|
1251
|
+
((_b = changeTableAst.drop).indexes ?? (_b.indexes = [])).push({
|
|
1372
1252
|
columns: dbColumns,
|
|
1373
1253
|
options: dbIndex,
|
|
1374
1254
|
name: indexName
|
|
@@ -1385,13 +1265,13 @@ const handleIndexChange = ({
|
|
|
1385
1265
|
}
|
|
1386
1266
|
};
|
|
1387
1267
|
const addIndexes = ({ changeTableAst, changingColumns }, add) => {
|
|
1388
|
-
var _a, _b
|
|
1389
|
-
const indexes = (
|
|
1268
|
+
var _a, _b;
|
|
1269
|
+
const indexes = (_a = changeTableAst.add).indexes ?? (_a.indexes = []);
|
|
1390
1270
|
for (const index of add) {
|
|
1391
1271
|
if (index.columns.length === 1 && "column" in index.columns[0]) {
|
|
1392
1272
|
const column = changingColumns[(index.columnNames || index.columns)[0].column];
|
|
1393
1273
|
if (column) {
|
|
1394
|
-
((
|
|
1274
|
+
((_b = column.to.data).indexes ?? (_b.indexes = [])).push(index);
|
|
1395
1275
|
continue;
|
|
1396
1276
|
}
|
|
1397
1277
|
}
|
|
@@ -1399,25 +1279,6 @@ const addIndexes = ({ changeTableAst, changingColumns }, add) => {
|
|
|
1399
1279
|
}
|
|
1400
1280
|
};
|
|
1401
1281
|
|
|
1402
|
-
var __defProp$4 = Object.defineProperty;
|
|
1403
|
-
var __defProps$4 = Object.defineProperties;
|
|
1404
|
-
var __getOwnPropDescs$4 = Object.getOwnPropertyDescriptors;
|
|
1405
|
-
var __getOwnPropSymbols$4 = Object.getOwnPropertySymbols;
|
|
1406
|
-
var __hasOwnProp$4 = Object.prototype.hasOwnProperty;
|
|
1407
|
-
var __propIsEnum$4 = Object.prototype.propertyIsEnumerable;
|
|
1408
|
-
var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1409
|
-
var __spreadValues$4 = (a, b) => {
|
|
1410
|
-
for (var prop in b || (b = {}))
|
|
1411
|
-
if (__hasOwnProp$4.call(b, prop))
|
|
1412
|
-
__defNormalProp$4(a, prop, b[prop]);
|
|
1413
|
-
if (__getOwnPropSymbols$4)
|
|
1414
|
-
for (var prop of __getOwnPropSymbols$4(b)) {
|
|
1415
|
-
if (__propIsEnum$4.call(b, prop))
|
|
1416
|
-
__defNormalProp$4(a, prop, b[prop]);
|
|
1417
|
-
}
|
|
1418
|
-
return a;
|
|
1419
|
-
};
|
|
1420
|
-
var __spreadProps$4 = (a, b) => __defProps$4(a, __getOwnPropDescs$4(b));
|
|
1421
1282
|
const mapMatchToDb = {
|
|
1422
1283
|
FULL: "f",
|
|
1423
1284
|
PARTIAL: "p",
|
|
@@ -1439,7 +1300,7 @@ for (const key in mapActionToDb) {
|
|
|
1439
1300
|
mapActionToCode[mapActionToDb[key]] = key;
|
|
1440
1301
|
}
|
|
1441
1302
|
const processForeignKeys = (config, ast, changeTables, currentSchema, tableShapes) => {
|
|
1442
|
-
var _a, _b
|
|
1303
|
+
var _a, _b;
|
|
1443
1304
|
for (const changeTableData of changeTables) {
|
|
1444
1305
|
const codeForeignKeys = collectCodeFkeys(
|
|
1445
1306
|
config,
|
|
@@ -1451,20 +1312,17 @@ const processForeignKeys = (config, ast, changeTables, currentSchema, tableShape
|
|
|
1451
1312
|
let changed = false;
|
|
1452
1313
|
for (const dbConstraint of dbTableData.constraints) {
|
|
1453
1314
|
const { references: dbReferences } = dbConstraint;
|
|
1454
|
-
if (!dbReferences)
|
|
1455
|
-
continue;
|
|
1315
|
+
if (!dbReferences) continue;
|
|
1456
1316
|
const hasChangedColumn = dbReferences.columns.some(
|
|
1457
1317
|
(column) => checkForColumnAddOrDrop(shape, column)
|
|
1458
1318
|
);
|
|
1459
|
-
if (hasChangedColumn)
|
|
1460
|
-
continue;
|
|
1319
|
+
if (hasChangedColumn) continue;
|
|
1461
1320
|
const foreignShape = tableShapes[`${dbReferences.foreignSchema}.${dbReferences.foreignTable}`];
|
|
1462
1321
|
const hasForeignChangedColumn = foreignShape && dbReferences.foreignColumns.some((column) => {
|
|
1463
1322
|
const res = checkForColumnAddOrDrop(foreignShape, column);
|
|
1464
1323
|
return res;
|
|
1465
1324
|
});
|
|
1466
|
-
if (hasForeignChangedColumn)
|
|
1467
|
-
continue;
|
|
1325
|
+
if (hasForeignChangedColumn) continue;
|
|
1468
1326
|
let found = false;
|
|
1469
1327
|
let rename;
|
|
1470
1328
|
for (let i = 0; i < codeForeignKeys.length; i++) {
|
|
@@ -1473,7 +1331,7 @@ const processForeignKeys = (config, ast, changeTables, currentSchema, tableShape
|
|
|
1473
1331
|
if (deepCompare(dbReferences, codeReferences)) {
|
|
1474
1332
|
found = true;
|
|
1475
1333
|
codeForeignKeys.splice(i, 1);
|
|
1476
|
-
const codeName =
|
|
1334
|
+
const codeName = codeForeignKey.codeConstraint.name ?? getConstraintName(
|
|
1477
1335
|
codeTable.table,
|
|
1478
1336
|
codeForeignKey,
|
|
1479
1337
|
config.snakeCase
|
|
@@ -1491,13 +1349,13 @@ const processForeignKeys = (config, ast, changeTables, currentSchema, tableShape
|
|
|
1491
1349
|
);
|
|
1492
1350
|
if (dbReferences.columns.length === 1 && changingColumns[dbReferences.columns[0]]) {
|
|
1493
1351
|
const column = changingColumns[dbReferences.columns[0]];
|
|
1494
|
-
((
|
|
1352
|
+
((_a = column.from.data).foreignKeys ?? (_a.foreignKeys = [])).push({
|
|
1495
1353
|
fnOrTable: foreignKey.references.fnOrTable,
|
|
1496
1354
|
foreignColumns: foreignKey.references.foreignColumns,
|
|
1497
1355
|
options: foreignKey.references.options
|
|
1498
1356
|
});
|
|
1499
1357
|
} else {
|
|
1500
|
-
(
|
|
1358
|
+
(drop.constraints ?? (drop.constraints = [])).push(foreignKey);
|
|
1501
1359
|
}
|
|
1502
1360
|
changed = true;
|
|
1503
1361
|
} else if (rename) {
|
|
@@ -1512,11 +1370,11 @@ const processForeignKeys = (config, ast, changeTables, currentSchema, tableShape
|
|
|
1512
1370
|
}
|
|
1513
1371
|
}
|
|
1514
1372
|
if (codeForeignKeys.length) {
|
|
1515
|
-
const constraints =
|
|
1373
|
+
const constraints = add.constraints ?? (add.constraints = []);
|
|
1516
1374
|
for (const { codeConstraint, references } of codeForeignKeys) {
|
|
1517
1375
|
if (references.columns.length === 1 && changingColumns[references.columns[0]]) {
|
|
1518
1376
|
const column = changingColumns[references.columns[0]];
|
|
1519
|
-
((
|
|
1377
|
+
((_b = column.to.data).foreignKeys ?? (_b.foreignKeys = [])).push({
|
|
1520
1378
|
fnOrTable: references.foreignTable,
|
|
1521
1379
|
foreignColumns: codeConstraint.references.foreignColumns,
|
|
1522
1380
|
options: codeConstraint.references.options
|
|
@@ -1534,24 +1392,20 @@ const processForeignKeys = (config, ast, changeTables, currentSchema, tableShape
|
|
|
1534
1392
|
}
|
|
1535
1393
|
};
|
|
1536
1394
|
const collectCodeFkeys = (config, { codeTable, changeTableAst: { shape } }, currentSchema) => {
|
|
1537
|
-
var _a;
|
|
1538
1395
|
const codeForeignKeys = [];
|
|
1539
1396
|
for (const key in codeTable.shape) {
|
|
1540
1397
|
const column = codeTable.shape[key];
|
|
1541
|
-
if (!column.data.foreignKeys)
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
if (checkForColumnAddOrDrop(shape, name))
|
|
1545
|
-
continue;
|
|
1398
|
+
if (!column.data.foreignKeys) continue;
|
|
1399
|
+
const name = column.data.name ?? key;
|
|
1400
|
+
if (checkForColumnAddOrDrop(shape, name)) continue;
|
|
1546
1401
|
codeForeignKeys.push(
|
|
1547
1402
|
...column.data.foreignKeys.map((x) => {
|
|
1548
|
-
var _a2;
|
|
1549
1403
|
const columns = [name];
|
|
1550
1404
|
const fnOrTable = fnOrTableToString(x.fnOrTable);
|
|
1551
1405
|
return parseForeignKey(
|
|
1552
1406
|
config,
|
|
1553
1407
|
{
|
|
1554
|
-
name:
|
|
1408
|
+
name: x.options?.name,
|
|
1555
1409
|
references: {
|
|
1556
1410
|
columns: [key],
|
|
1557
1411
|
fnOrTable,
|
|
@@ -1573,22 +1427,24 @@ const collectCodeFkeys = (config, { codeTable, changeTableAst: { shape } }, curr
|
|
|
1573
1427
|
if (codeTable.internal.tableData.constraints) {
|
|
1574
1428
|
for (const tableConstraint of codeTable.internal.tableData.constraints) {
|
|
1575
1429
|
const { references: refs } = tableConstraint;
|
|
1576
|
-
if (!refs)
|
|
1577
|
-
continue;
|
|
1430
|
+
if (!refs) continue;
|
|
1578
1431
|
const fnOrTable = fnOrTableToString(refs.fnOrTable);
|
|
1579
1432
|
codeForeignKeys.push(
|
|
1580
1433
|
parseForeignKey(
|
|
1581
1434
|
config,
|
|
1582
|
-
|
|
1583
|
-
|
|
1435
|
+
{
|
|
1436
|
+
...tableConstraint,
|
|
1437
|
+
references: {
|
|
1438
|
+
...refs,
|
|
1584
1439
|
fnOrTable
|
|
1585
|
-
}
|
|
1586
|
-
}
|
|
1587
|
-
|
|
1440
|
+
}
|
|
1441
|
+
},
|
|
1442
|
+
{
|
|
1443
|
+
...refs,
|
|
1588
1444
|
fnOrTable,
|
|
1589
1445
|
columns: config.snakeCase ? refs.columns.map(toSnakeCase) : refs.columns,
|
|
1590
1446
|
foreignColumns: config.snakeCase ? refs.foreignColumns.map(toSnakeCase) : refs.foreignColumns
|
|
1591
|
-
}
|
|
1447
|
+
},
|
|
1592
1448
|
currentSchema
|
|
1593
1449
|
)
|
|
1594
1450
|
);
|
|
@@ -1608,13 +1464,13 @@ const parseForeignKey = (config, codeConstraint, references, currentSchema) => {
|
|
|
1608
1464
|
const [schema, table] = getSchemaAndTableFromName(fnOrTable);
|
|
1609
1465
|
return {
|
|
1610
1466
|
references: {
|
|
1611
|
-
foreignSchema: schema
|
|
1467
|
+
foreignSchema: schema ?? currentSchema,
|
|
1612
1468
|
foreignTable: table,
|
|
1613
1469
|
columns,
|
|
1614
1470
|
foreignColumns: config.snakeCase ? foreignColumns.map(toSnakeCase) : foreignColumns,
|
|
1615
|
-
match: mapMatchToDb[
|
|
1616
|
-
onUpdate: mapActionToDb[
|
|
1617
|
-
onDelete: mapActionToDb[
|
|
1471
|
+
match: mapMatchToDb[options?.match || "SIMPLE"],
|
|
1472
|
+
onUpdate: mapActionToDb[options?.onUpdate || "NO ACTION"],
|
|
1473
|
+
onDelete: mapActionToDb[options?.onDelete || "NO ACTION"]
|
|
1618
1474
|
},
|
|
1619
1475
|
codeConstraint
|
|
1620
1476
|
};
|
|
@@ -1637,27 +1493,7 @@ const dbForeignKeyToCodeForeignKey = (config, dbConstraint, dbReferences) => ({
|
|
|
1637
1493
|
}
|
|
1638
1494
|
});
|
|
1639
1495
|
|
|
1640
|
-
var __defProp$3 = Object.defineProperty;
|
|
1641
|
-
var __defProps$3 = Object.defineProperties;
|
|
1642
|
-
var __getOwnPropDescs$3 = Object.getOwnPropertyDescriptors;
|
|
1643
|
-
var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
|
|
1644
|
-
var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
|
|
1645
|
-
var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
|
|
1646
|
-
var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1647
|
-
var __spreadValues$3 = (a, b) => {
|
|
1648
|
-
for (var prop in b || (b = {}))
|
|
1649
|
-
if (__hasOwnProp$3.call(b, prop))
|
|
1650
|
-
__defNormalProp$3(a, prop, b[prop]);
|
|
1651
|
-
if (__getOwnPropSymbols$3)
|
|
1652
|
-
for (var prop of __getOwnPropSymbols$3(b)) {
|
|
1653
|
-
if (__propIsEnum$3.call(b, prop))
|
|
1654
|
-
__defNormalProp$3(a, prop, b[prop]);
|
|
1655
|
-
}
|
|
1656
|
-
return a;
|
|
1657
|
-
};
|
|
1658
|
-
var __spreadProps$3 = (a, b) => __defProps$3(a, __getOwnPropDescs$3(b));
|
|
1659
1496
|
const processChecks = (ast, changeTableData, compareExpressions) => {
|
|
1660
|
-
var _a, _b;
|
|
1661
1497
|
const codeChecks = collectCodeChecks(changeTableData);
|
|
1662
1498
|
const {
|
|
1663
1499
|
dbTableData,
|
|
@@ -1666,7 +1502,7 @@ const processChecks = (ast, changeTableData, compareExpressions) => {
|
|
|
1666
1502
|
const hasDbChecks = dbTableData.constraints.some((c) => c.check);
|
|
1667
1503
|
if (!hasDbChecks) {
|
|
1668
1504
|
if (codeChecks.length) {
|
|
1669
|
-
const constraints =
|
|
1505
|
+
const constraints = add.constraints ?? (add.constraints = []);
|
|
1670
1506
|
for (const check of codeChecks) {
|
|
1671
1507
|
if (check.column && changeTableData.changingColumns[check.column]) {
|
|
1672
1508
|
const column = changeTableData.changingColumns[check.column];
|
|
@@ -1682,13 +1518,11 @@ const processChecks = (ast, changeTableData, compareExpressions) => {
|
|
|
1682
1518
|
const foundCodeChecks = /* @__PURE__ */ new Set();
|
|
1683
1519
|
for (const dbConstraint of dbTableData.constraints) {
|
|
1684
1520
|
const { check: dbCheck, name } = dbConstraint;
|
|
1685
|
-
if (!dbCheck)
|
|
1686
|
-
|
|
1687
|
-
const hasChangedColumn = (_b = dbCheck.columns) == null ? void 0 : _b.some(
|
|
1521
|
+
if (!dbCheck) continue;
|
|
1522
|
+
const hasChangedColumn = dbCheck.columns?.some(
|
|
1688
1523
|
(column) => checkForColumnAddOrDrop(shape, column)
|
|
1689
1524
|
);
|
|
1690
|
-
if (hasChangedColumn)
|
|
1691
|
-
continue;
|
|
1525
|
+
if (hasChangedColumn) continue;
|
|
1692
1526
|
if (codeChecks.length) {
|
|
1693
1527
|
wait++;
|
|
1694
1528
|
compareExpressions.push({
|
|
@@ -1699,13 +1533,11 @@ const processChecks = (ast, changeTableData, compareExpressions) => {
|
|
|
1699
1533
|
}
|
|
1700
1534
|
],
|
|
1701
1535
|
handle(index) {
|
|
1702
|
-
|
|
1703
|
-
if (index !== void 0)
|
|
1704
|
-
return;
|
|
1536
|
+
if (index !== void 0) return;
|
|
1705
1537
|
dropCheck(changeTableData, dbCheck, name);
|
|
1706
1538
|
if (--wait === 0 && !changeTableData.pushedAst) {
|
|
1707
1539
|
changeTableData.pushedAst = true;
|
|
1708
|
-
(
|
|
1540
|
+
(add.constraints ?? (add.constraints = [])).push(
|
|
1709
1541
|
...codeChecks.filter((_, i) => !foundCodeChecks.has(i)).map((check) => ({
|
|
1710
1542
|
name: check.name,
|
|
1711
1543
|
check: check.sql
|
|
@@ -1724,18 +1556,16 @@ const collectCodeChecks = ({
|
|
|
1724
1556
|
codeTable,
|
|
1725
1557
|
changeTableAst: { shape }
|
|
1726
1558
|
}) => {
|
|
1727
|
-
var _a;
|
|
1728
1559
|
const codeChecks = [];
|
|
1729
1560
|
for (const key in codeTable.shape) {
|
|
1730
1561
|
const column = codeTable.shape[key];
|
|
1731
|
-
if (!column.data.check)
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
codeChecks.push(__spreadProps$3(__spreadValues$3({}, column.data.check), {
|
|
1562
|
+
if (!column.data.check) continue;
|
|
1563
|
+
const name = column.data.name ?? key;
|
|
1564
|
+
if (checkForColumnAddOrDrop(shape, name)) continue;
|
|
1565
|
+
codeChecks.push({
|
|
1566
|
+
...column.data.check,
|
|
1737
1567
|
column: name
|
|
1738
|
-
})
|
|
1568
|
+
});
|
|
1739
1569
|
}
|
|
1740
1570
|
if (codeTable.internal.tableData.constraints) {
|
|
1741
1571
|
for (const constraint of codeTable.internal.tableData.constraints) {
|
|
@@ -1748,12 +1578,11 @@ const collectCodeChecks = ({
|
|
|
1748
1578
|
return codeChecks;
|
|
1749
1579
|
};
|
|
1750
1580
|
const dropCheck = ({ changeTableAst: { drop }, changingColumns }, dbCheck, name) => {
|
|
1751
|
-
|
|
1752
|
-
const constraints = (_a = drop.constraints) != null ? _a : drop.constraints = [];
|
|
1581
|
+
const constraints = drop.constraints ?? (drop.constraints = []);
|
|
1753
1582
|
const sql = new RawSQL([
|
|
1754
1583
|
[dbCheck.expression]
|
|
1755
1584
|
]);
|
|
1756
|
-
if (
|
|
1585
|
+
if (dbCheck.columns?.length === 1 && changingColumns[dbCheck.columns[0]]) {
|
|
1757
1586
|
const column = changingColumns[dbCheck.columns[0]];
|
|
1758
1587
|
column.from.data.name = "i_d";
|
|
1759
1588
|
column.from.data.check = {
|
|
@@ -1768,25 +1597,6 @@ const dropCheck = ({ changeTableAst: { drop }, changingColumns }, dbCheck, name)
|
|
|
1768
1597
|
}
|
|
1769
1598
|
};
|
|
1770
1599
|
|
|
1771
|
-
var __defProp$2 = Object.defineProperty;
|
|
1772
|
-
var __defProps$2 = Object.defineProperties;
|
|
1773
|
-
var __getOwnPropDescs$2 = Object.getOwnPropertyDescriptors;
|
|
1774
|
-
var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
|
|
1775
|
-
var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
|
|
1776
|
-
var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
|
|
1777
|
-
var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1778
|
-
var __spreadValues$2 = (a, b) => {
|
|
1779
|
-
for (var prop in b || (b = {}))
|
|
1780
|
-
if (__hasOwnProp$2.call(b, prop))
|
|
1781
|
-
__defNormalProp$2(a, prop, b[prop]);
|
|
1782
|
-
if (__getOwnPropSymbols$2)
|
|
1783
|
-
for (var prop of __getOwnPropSymbols$2(b)) {
|
|
1784
|
-
if (__propIsEnum$2.call(b, prop))
|
|
1785
|
-
__defNormalProp$2(a, prop, b[prop]);
|
|
1786
|
-
}
|
|
1787
|
-
return a;
|
|
1788
|
-
};
|
|
1789
|
-
var __spreadProps$2 = (a, b) => __defProps$2(a, __getOwnPropDescs$2(b));
|
|
1790
1600
|
const processTables = async (ast, structureToAstCtx, domainsMap, adapter, tables, dbStructure, currentSchema, config, generatorIgnore, verifying) => {
|
|
1791
1601
|
const createTables = collectCreateTables(
|
|
1792
1602
|
tables,
|
|
@@ -1840,8 +1650,7 @@ const processTables = async (ast, structureToAstCtx, domainsMap, adapter, tables
|
|
|
1840
1650
|
};
|
|
1841
1651
|
const collectCreateTables = (tables, dbStructure, currentSchema) => {
|
|
1842
1652
|
return tables.reduce((acc, codeTable) => {
|
|
1843
|
-
|
|
1844
|
-
const tableSchema = (_a = codeTable.q.schema) != null ? _a : currentSchema;
|
|
1653
|
+
const tableSchema = codeTable.q.schema ?? currentSchema;
|
|
1845
1654
|
const hasDbTable = dbStructure.tables.some(
|
|
1846
1655
|
(t) => t.name === codeTable.table && t.schemaName === tableSchema
|
|
1847
1656
|
);
|
|
@@ -1852,25 +1661,21 @@ const collectCreateTables = (tables, dbStructure, currentSchema) => {
|
|
|
1852
1661
|
}, []);
|
|
1853
1662
|
};
|
|
1854
1663
|
const collectChangeAndDropTables = (config, tables, dbStructure, currentSchema, createTables, generatorIgnore) => {
|
|
1855
|
-
var _a;
|
|
1856
1664
|
const changeTables = [];
|
|
1857
1665
|
const changeTableSchemas = [];
|
|
1858
1666
|
const dropTables = [];
|
|
1859
1667
|
const tableShapes = {};
|
|
1860
|
-
const ignoreTables =
|
|
1668
|
+
const ignoreTables = generatorIgnore?.tables?.map((name) => {
|
|
1861
1669
|
const [schema = currentSchema, table] = getSchemaAndTableFromName(name);
|
|
1862
1670
|
return { schema, table };
|
|
1863
1671
|
});
|
|
1864
1672
|
for (const dbTable of dbStructure.tables) {
|
|
1865
|
-
if (dbTable.name === config.migrationsTable ||
|
|
1673
|
+
if (dbTable.name === config.migrationsTable || ignoreTables?.some(
|
|
1866
1674
|
({ schema, table }) => table === dbTable.name && schema === dbTable.schemaName
|
|
1867
|
-
))
|
|
1675
|
+
))
|
|
1868
1676
|
continue;
|
|
1869
1677
|
const codeTable = tables.find(
|
|
1870
|
-
(t) =>
|
|
1871
|
-
var _a2;
|
|
1872
|
-
return t.table === dbTable.name && ((_a2 = t.q.schema) != null ? _a2 : currentSchema) === dbTable.schemaName;
|
|
1873
|
-
}
|
|
1678
|
+
(t) => t.table === dbTable.name && (t.q.schema ?? currentSchema) === dbTable.schemaName
|
|
1874
1679
|
);
|
|
1875
1680
|
if (codeTable) {
|
|
1876
1681
|
addChangeTable(
|
|
@@ -1895,10 +1700,9 @@ const collectChangeAndDropTables = (config, tables, dbStructure, currentSchema,
|
|
|
1895
1700
|
return { changeTables, changeTableSchemas, dropTables, tableShapes };
|
|
1896
1701
|
};
|
|
1897
1702
|
const applyChangeTableSchemas = (changeTableSchemas, currentSchema, ast) => {
|
|
1898
|
-
var _a;
|
|
1899
1703
|
for (const { codeTable, dbTable } of changeTableSchemas) {
|
|
1900
1704
|
const fromSchema = dbTable.schemaName;
|
|
1901
|
-
const toSchema =
|
|
1705
|
+
const toSchema = codeTable.q.schema ?? currentSchema;
|
|
1902
1706
|
ast.push({
|
|
1903
1707
|
type: "renameType",
|
|
1904
1708
|
kind: "TABLE",
|
|
@@ -1910,7 +1714,6 @@ const applyChangeTableSchemas = (changeTableSchemas, currentSchema, ast) => {
|
|
|
1910
1714
|
}
|
|
1911
1715
|
};
|
|
1912
1716
|
const applyChangeTables = async (adapter, changeTables, structureToAstCtx, dbStructure, domainsMap, ast, currentSchema, config, compareSql, tableExpressions, verifying) => {
|
|
1913
|
-
var _a;
|
|
1914
1717
|
const compareExpressions = [];
|
|
1915
1718
|
const typeCastsCache = {};
|
|
1916
1719
|
for (const changeTableData of changeTables) {
|
|
@@ -1935,16 +1738,15 @@ const applyChangeTables = async (adapter, changeTables, structureToAstCtx, dbStr
|
|
|
1935
1738
|
const types = [];
|
|
1936
1739
|
for (const key in codeTable.shape) {
|
|
1937
1740
|
const column = codeTable.shape[key];
|
|
1938
|
-
if (!column.dataType)
|
|
1939
|
-
|
|
1940
|
-
const name = (_a = column.data.name) != null ? _a : key;
|
|
1741
|
+
if (!column.dataType) continue;
|
|
1742
|
+
const name = column.data.name ?? key;
|
|
1941
1743
|
names.push(name);
|
|
1942
1744
|
types.push(getColumnDbType(column, currentSchema));
|
|
1943
1745
|
}
|
|
1944
1746
|
const tableName = codeTable.table;
|
|
1945
1747
|
const source = `(VALUES (${types.map((x) => `NULL::${x}`).join(", ")})) "${tableName}"(${names.map((x) => `"${x}"`).join(", ")})`;
|
|
1946
1748
|
tableExpressions.push(
|
|
1947
|
-
...compareExpressions.map((x) =>
|
|
1749
|
+
...compareExpressions.map((x) => ({ ...x, source }))
|
|
1948
1750
|
);
|
|
1949
1751
|
}
|
|
1950
1752
|
}
|
|
@@ -1965,7 +1767,6 @@ const applyCompareSql = async (compareSql, adapter) => {
|
|
|
1965
1767
|
}
|
|
1966
1768
|
};
|
|
1967
1769
|
const applyCreateOrRenameTables = async (dbStructure, createTables, dropTables, changeTables, tableShapes, currentSchema, ast, verifying) => {
|
|
1968
|
-
var _a;
|
|
1969
1770
|
for (const codeTable of createTables) {
|
|
1970
1771
|
if (dropTables.length) {
|
|
1971
1772
|
const index = await promptCreateOrRename(
|
|
@@ -1982,7 +1783,7 @@ const applyCreateOrRenameTables = async (dbStructure, createTables, dropTables,
|
|
|
1982
1783
|
kind: "TABLE",
|
|
1983
1784
|
fromSchema: dbTable.schemaName,
|
|
1984
1785
|
from: dbTable.name,
|
|
1985
|
-
toSchema:
|
|
1786
|
+
toSchema: codeTable.q.schema ?? currentSchema,
|
|
1986
1787
|
to: codeTable.table
|
|
1987
1788
|
});
|
|
1988
1789
|
addChangeTable(
|
|
@@ -2000,9 +1801,8 @@ const applyCreateOrRenameTables = async (dbStructure, createTables, dropTables,
|
|
|
2000
1801
|
}
|
|
2001
1802
|
};
|
|
2002
1803
|
const addChangeTable = (dbStructure, changeTables, tableShapes, currentSchema, dbTable, codeTable) => {
|
|
2003
|
-
var _a;
|
|
2004
1804
|
const shape = {};
|
|
2005
|
-
const schema =
|
|
1805
|
+
const schema = codeTable.q.schema ?? currentSchema;
|
|
2006
1806
|
changeTables.push({
|
|
2007
1807
|
codeTable,
|
|
2008
1808
|
dbTable,
|
|
@@ -2023,15 +1823,16 @@ const addChangeTable = (dbStructure, changeTables, tableShapes, currentSchema, d
|
|
|
2023
1823
|
tableShapes[`${schema}.${codeTable.table}`] = shape;
|
|
2024
1824
|
};
|
|
2025
1825
|
const createTableAst = (currentSchema, table) => {
|
|
2026
|
-
return
|
|
1826
|
+
return {
|
|
2027
1827
|
type: "table",
|
|
2028
1828
|
action: "create",
|
|
2029
1829
|
schema: table.q.schema === currentSchema ? void 0 : table.q.schema,
|
|
2030
1830
|
comment: table.internal.comment,
|
|
2031
1831
|
name: table.table,
|
|
2032
1832
|
shape: makeTableShape(table),
|
|
2033
|
-
noPrimaryKey: table.internal.noPrimaryKey ? "ignore" : "error"
|
|
2034
|
-
|
|
1833
|
+
noPrimaryKey: table.internal.noPrimaryKey ? "ignore" : "error",
|
|
1834
|
+
...table.internal.tableData
|
|
1835
|
+
};
|
|
2035
1836
|
};
|
|
2036
1837
|
const makeTableShape = (table) => {
|
|
2037
1838
|
const shape = {};
|
|
@@ -2149,9 +1950,7 @@ const verifyMigration = async (adapter, config, migrationCode, generateMigration
|
|
|
2149
1950
|
};
|
|
2150
1951
|
|
|
2151
1952
|
const report = (ast, config, currentSchema) => {
|
|
2152
|
-
|
|
2153
|
-
if (!config.logger)
|
|
2154
|
-
return;
|
|
1953
|
+
if (!config.logger) return;
|
|
2155
1954
|
const code = [];
|
|
2156
1955
|
let green, red, yellow, pale;
|
|
2157
1956
|
if (typeof config.log === "object" && config.log.colors === false) {
|
|
@@ -2165,15 +1964,15 @@ const report = (ast, config, currentSchema) => {
|
|
|
2165
1964
|
let hasPrimaryKey = !!a.primaryKey;
|
|
2166
1965
|
const counters = {
|
|
2167
1966
|
column: 0,
|
|
2168
|
-
index:
|
|
2169
|
-
"foreign key":
|
|
1967
|
+
index: a.indexes?.length ?? 0,
|
|
1968
|
+
"foreign key": a.constraints?.reduce(
|
|
2170
1969
|
(sum, c) => c.references ? sum + 1 : sum,
|
|
2171
1970
|
0
|
|
2172
|
-
)
|
|
2173
|
-
check:
|
|
1971
|
+
) ?? 0,
|
|
1972
|
+
check: a.constraints?.reduce(
|
|
2174
1973
|
(sum, c) => c.check ? sum + 1 : sum,
|
|
2175
1974
|
0
|
|
2176
|
-
)
|
|
1975
|
+
) ?? 0
|
|
2177
1976
|
};
|
|
2178
1977
|
for (const key in a.shape) {
|
|
2179
1978
|
counters.column++;
|
|
@@ -2223,14 +2022,14 @@ const report = (ast, config, currentSchema) => {
|
|
|
2223
2022
|
const column = change.item;
|
|
2224
2023
|
const { primaryKey, indexes, foreignKeys, check } = column.data;
|
|
2225
2024
|
inner.push(
|
|
2226
|
-
`${change.type === "add" ? green("+ add column") : red("- drop column")} ${key} ${
|
|
2025
|
+
`${change.type === "add" ? green("+ add column") : red("- drop column")} ${key} ${column.data.alias ?? getColumnDbType(column, currentSchema)}${column.data.isNullable ? " nullable" : ""}${primaryKey ? " primary key" : ""}${foreignKeys ? ` references ${foreignKeys.map((fk) => {
|
|
2227
2026
|
return `${fnOrTableToString(
|
|
2228
2027
|
fk.fnOrTable
|
|
2229
2028
|
)}(${fk.foreignColumns.join(", ")})`;
|
|
2230
|
-
}).join(", ")}` : ""}${
|
|
2029
|
+
}).join(", ")}` : ""}${indexes?.length ? indexes.length === 1 ? ", has index" : `, has ${indexes.length} indexes` : ""}${check ? `, checks ${check.sql.toSQL({ values: [] })}` : ""}`
|
|
2231
2030
|
);
|
|
2232
2031
|
} else if (change.type === "change") {
|
|
2233
|
-
const name =
|
|
2032
|
+
const name = change.from.column?.data.name ?? key;
|
|
2234
2033
|
const changes2 = [];
|
|
2235
2034
|
inner.push(`${yellow("~ change column")} ${name}:`, changes2);
|
|
2236
2035
|
changes2.push(`${yellow("from")}: `);
|
|
@@ -2270,8 +2069,7 @@ const report = (ast, config, currentSchema) => {
|
|
|
2270
2069
|
}
|
|
2271
2070
|
if (a.drop.constraints) {
|
|
2272
2071
|
for (const { references } of a.drop.constraints) {
|
|
2273
|
-
if (!references)
|
|
2274
|
-
continue;
|
|
2072
|
+
if (!references) continue;
|
|
2275
2073
|
const [schema, name] = getSchemaAndTableFromName(
|
|
2276
2074
|
references.fnOrTable
|
|
2277
2075
|
);
|
|
@@ -2288,8 +2086,7 @@ const report = (ast, config, currentSchema) => {
|
|
|
2288
2086
|
);
|
|
2289
2087
|
}
|
|
2290
2088
|
for (const { check } of a.drop.constraints) {
|
|
2291
|
-
if (!check)
|
|
2292
|
-
continue;
|
|
2089
|
+
if (!check) continue;
|
|
2293
2090
|
inner.push(`${red(`- drop check`)} ${check.toSQL({ values: [] })}`);
|
|
2294
2091
|
}
|
|
2295
2092
|
}
|
|
@@ -2311,8 +2108,7 @@ const report = (ast, config, currentSchema) => {
|
|
|
2311
2108
|
}
|
|
2312
2109
|
if (a.add.constraints) {
|
|
2313
2110
|
for (const { references } of a.add.constraints) {
|
|
2314
|
-
if (!references)
|
|
2315
|
-
continue;
|
|
2111
|
+
if (!references) continue;
|
|
2316
2112
|
inner.push(
|
|
2317
2113
|
`${green(`+ add foreign key`)} on (${references.columns.join(
|
|
2318
2114
|
", "
|
|
@@ -2320,8 +2116,7 @@ const report = (ast, config, currentSchema) => {
|
|
|
2320
2116
|
);
|
|
2321
2117
|
}
|
|
2322
2118
|
for (const { check } of a.add.constraints) {
|
|
2323
|
-
if (!check)
|
|
2324
|
-
continue;
|
|
2119
|
+
if (!check) continue;
|
|
2325
2120
|
inner.push(
|
|
2326
2121
|
`${green(`+ add check`)} ${check.toSQL({ values: [] })}`
|
|
2327
2122
|
);
|
|
@@ -2435,37 +2230,14 @@ const dbItemName = ({ schema, name }, currentSchema) => {
|
|
|
2435
2230
|
return schema && schema !== currentSchema ? `${schema}.${name}` : name;
|
|
2436
2231
|
};
|
|
2437
2232
|
|
|
2438
|
-
var __defProp$1 = Object.defineProperty;
|
|
2439
|
-
var __defProps$1 = Object.defineProperties;
|
|
2440
|
-
var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
|
|
2441
|
-
var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
|
|
2442
|
-
var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
|
|
2443
|
-
var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
|
|
2444
|
-
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
2445
|
-
var __spreadValues$1 = (a, b) => {
|
|
2446
|
-
for (var prop in b || (b = {}))
|
|
2447
|
-
if (__hasOwnProp$1.call(b, prop))
|
|
2448
|
-
__defNormalProp$1(a, prop, b[prop]);
|
|
2449
|
-
if (__getOwnPropSymbols$1)
|
|
2450
|
-
for (var prop of __getOwnPropSymbols$1(b)) {
|
|
2451
|
-
if (__propIsEnum$1.call(b, prop))
|
|
2452
|
-
__defNormalProp$1(a, prop, b[prop]);
|
|
2453
|
-
}
|
|
2454
|
-
return a;
|
|
2455
|
-
};
|
|
2456
|
-
var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
|
|
2457
2233
|
class AbortSignal extends Error {
|
|
2458
2234
|
}
|
|
2459
2235
|
const generate = async (options, config, args, afterPull) => {
|
|
2460
|
-
var _a, _b, _c;
|
|
2461
2236
|
let { dbPath } = config;
|
|
2462
|
-
if (!dbPath || !config.baseTable)
|
|
2463
|
-
|
|
2464
|
-
if (!
|
|
2465
|
-
|
|
2466
|
-
if (!dbPath.endsWith(".ts"))
|
|
2467
|
-
dbPath += ".ts";
|
|
2468
|
-
let migrationName = (_a = args[0]) != null ? _a : "generated";
|
|
2237
|
+
if (!dbPath || !config.baseTable) throw invalidConfig(config);
|
|
2238
|
+
if (!options.length) throw new Error(`Database options must not be empty`);
|
|
2239
|
+
if (!dbPath.endsWith(".ts")) dbPath += ".ts";
|
|
2240
|
+
let migrationName = args[0] ?? "generated";
|
|
2469
2241
|
let up;
|
|
2470
2242
|
if (migrationName === "up") {
|
|
2471
2243
|
up = true;
|
|
@@ -2479,7 +2251,7 @@ const generate = async (options, config, args, afterPull) => {
|
|
|
2479
2251
|
afterPull
|
|
2480
2252
|
);
|
|
2481
2253
|
const [adapter] = adapters;
|
|
2482
|
-
const currentSchema =
|
|
2254
|
+
const currentSchema = adapter.schema ?? "public";
|
|
2483
2255
|
const db = await getDbFromConfig(config, dbPath);
|
|
2484
2256
|
const { columnTypes, internal } = db.$queryBuilder;
|
|
2485
2257
|
const codeItems = await getActualItems(
|
|
@@ -2529,18 +2301,18 @@ ${result}`}`
|
|
|
2529
2301
|
}
|
|
2530
2302
|
}
|
|
2531
2303
|
const { logger } = config;
|
|
2532
|
-
if ((!up || !migrationCode) && !afterPull)
|
|
2533
|
-
await closeAdapters(adapters);
|
|
2304
|
+
if ((!up || !migrationCode) && !afterPull) await closeAdapters(adapters);
|
|
2534
2305
|
if (!migrationCode) {
|
|
2535
|
-
logger
|
|
2306
|
+
logger?.log("No changes were detected");
|
|
2536
2307
|
return;
|
|
2537
2308
|
}
|
|
2538
|
-
const version =
|
|
2309
|
+
const version = afterPull?.version ?? await makeFileVersion({}, config);
|
|
2539
2310
|
const delayLog = [];
|
|
2540
2311
|
await writeMigrationFile(
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2312
|
+
{
|
|
2313
|
+
...config,
|
|
2314
|
+
logger: logger ? { ...logger, log: (msg) => delayLog.push(msg) } : logger
|
|
2315
|
+
},
|
|
2544
2316
|
version,
|
|
2545
2317
|
migrationName,
|
|
2546
2318
|
migrationCode
|
|
@@ -2562,14 +2334,13 @@ const invalidConfig = (config) => new Error(
|
|
|
2562
2334
|
`\`${config.dbPath ? "baseTable" : "dbPath"}\` setting must be set in the migrations config for the generator to work`
|
|
2563
2335
|
);
|
|
2564
2336
|
const getDbFromConfig = async (config, dbPath) => {
|
|
2565
|
-
var _a, _b;
|
|
2566
2337
|
const module = await config.import(
|
|
2567
2338
|
pathToFileURL(path.resolve(config.basePath, dbPath)).toString()
|
|
2568
2339
|
);
|
|
2569
|
-
const db = module[
|
|
2570
|
-
if (!
|
|
2340
|
+
const db = module[config.dbExportedAs ?? "db"];
|
|
2341
|
+
if (!db?.$queryBuilder) {
|
|
2571
2342
|
throw new Error(
|
|
2572
|
-
`Unable to import OrchidORM instance as ${
|
|
2343
|
+
`Unable to import OrchidORM instance as ${config.dbExportedAs ?? "db"} from ${config.dbPath}`
|
|
2573
2344
|
);
|
|
2574
2345
|
}
|
|
2575
2346
|
return db;
|
|
@@ -2644,7 +2415,6 @@ const compareDbStructures = (a, b, i, path2) => {
|
|
|
2644
2415
|
}
|
|
2645
2416
|
};
|
|
2646
2417
|
const getActualItems = async (db, currentSchema, internal, columnTypes) => {
|
|
2647
|
-
var _a;
|
|
2648
2418
|
const tableNames = /* @__PURE__ */ new Set();
|
|
2649
2419
|
const habtmTables = /* @__PURE__ */ new Map();
|
|
2650
2420
|
const codeItems = {
|
|
@@ -2655,8 +2425,7 @@ const getActualItems = async (db, currentSchema, internal, columnTypes) => {
|
|
|
2655
2425
|
};
|
|
2656
2426
|
const domains = /* @__PURE__ */ new Map();
|
|
2657
2427
|
for (const key in db) {
|
|
2658
|
-
if (key[0] === "$")
|
|
2659
|
-
continue;
|
|
2428
|
+
if (key[0] === "$") continue;
|
|
2660
2429
|
const table = db[key];
|
|
2661
2430
|
if (!table.table) {
|
|
2662
2431
|
throw new Error(`Table ${key} is missing table property`);
|
|
@@ -2667,8 +2436,7 @@ const getActualItems = async (db, currentSchema, internal, columnTypes) => {
|
|
|
2667
2436
|
throw new Error(`Table ${name} is defined more than once`);
|
|
2668
2437
|
}
|
|
2669
2438
|
tableNames.add(name);
|
|
2670
|
-
if (schema)
|
|
2671
|
-
codeItems.schemas.add(schema);
|
|
2439
|
+
if (schema) codeItems.schemas.add(schema);
|
|
2672
2440
|
codeItems.tables.push(table);
|
|
2673
2441
|
for (const key2 in table.relations) {
|
|
2674
2442
|
const column = table.shape[key2];
|
|
@@ -2687,7 +2455,7 @@ const getActualItems = async (db, currentSchema, internal, columnTypes) => {
|
|
|
2687
2455
|
domains.set(column.dataType, {
|
|
2688
2456
|
schemaName,
|
|
2689
2457
|
name: name2,
|
|
2690
|
-
column:
|
|
2458
|
+
column: column.data.as ?? new UnknownColumn(defaultSchemaConfig)
|
|
2691
2459
|
});
|
|
2692
2460
|
} else if (column.dataType === "enum") {
|
|
2693
2461
|
processEnumColumn(column, currentSchema, codeItems);
|
|
@@ -2697,8 +2465,7 @@ const getActualItems = async (db, currentSchema, internal, columnTypes) => {
|
|
|
2697
2465
|
if (internal.extensions) {
|
|
2698
2466
|
for (const extension of internal.extensions) {
|
|
2699
2467
|
const [schema] = getSchemaAndTableFromName(extension.name);
|
|
2700
|
-
if (schema)
|
|
2701
|
-
codeItems.schemas.add(schema);
|
|
2468
|
+
if (schema) codeItems.schemas.add(schema);
|
|
2702
2469
|
}
|
|
2703
2470
|
}
|
|
2704
2471
|
if (internal.domains) {
|
|
@@ -2721,22 +2488,20 @@ const getActualItems = async (db, currentSchema, internal, columnTypes) => {
|
|
|
2721
2488
|
const processEnumColumn = (column, currentSchema, codeItems) => {
|
|
2722
2489
|
const { enumName, options } = column;
|
|
2723
2490
|
const [schema, name] = getSchemaAndTableFromName(enumName);
|
|
2724
|
-
const enumSchema = schema
|
|
2491
|
+
const enumSchema = schema ?? currentSchema;
|
|
2725
2492
|
codeItems.enums.set(`${enumSchema}.${name}`, {
|
|
2726
2493
|
schema: enumSchema,
|
|
2727
2494
|
name,
|
|
2728
2495
|
values: options
|
|
2729
2496
|
});
|
|
2730
|
-
if (schema)
|
|
2731
|
-
codeItems.schemas.add(schema);
|
|
2497
|
+
if (schema) codeItems.schemas.add(schema);
|
|
2732
2498
|
};
|
|
2733
2499
|
const processHasAndBelongsToManyColumn = (column, habtmTables, codeItems) => {
|
|
2734
|
-
var _a, _b;
|
|
2735
2500
|
const q = column.joinTable;
|
|
2736
2501
|
const prev = habtmTables.get(q.table);
|
|
2737
2502
|
if (prev) {
|
|
2738
2503
|
for (const key in q.shape) {
|
|
2739
|
-
if (q.shape[key].dataType !==
|
|
2504
|
+
if (q.shape[key].dataType !== prev.shape[key]?.dataType) {
|
|
2740
2505
|
throw new Error(
|
|
2741
2506
|
`Column ${key} in ${q.table} in hasAndBelongsToMany relation does not match with the relation on the other side`
|
|
2742
2507
|
);
|
|
@@ -2749,23 +2514,25 @@ const processHasAndBelongsToManyColumn = (column, habtmTables, codeItems) => {
|
|
|
2749
2514
|
const shape = {};
|
|
2750
2515
|
for (const key in joinTable.shape) {
|
|
2751
2516
|
const column2 = Object.create(joinTable.shape[key]);
|
|
2752
|
-
column2.data =
|
|
2753
|
-
|
|
2517
|
+
column2.data = {
|
|
2518
|
+
...column2.data,
|
|
2519
|
+
name: column2.data.name ?? key,
|
|
2754
2520
|
identity: void 0,
|
|
2755
2521
|
primaryKey: void 0,
|
|
2756
2522
|
default: void 0
|
|
2757
|
-
}
|
|
2523
|
+
};
|
|
2758
2524
|
shape[toCamelCase(key)] = column2;
|
|
2759
2525
|
}
|
|
2760
2526
|
joinTable.shape = shape;
|
|
2761
|
-
joinTable.internal =
|
|
2527
|
+
joinTable.internal = {
|
|
2528
|
+
...joinTable.internal,
|
|
2762
2529
|
tableData: {
|
|
2763
2530
|
primaryKey: {
|
|
2764
2531
|
columns: Object.keys(shape)
|
|
2765
2532
|
}
|
|
2766
2533
|
},
|
|
2767
2534
|
noPrimaryKey: false
|
|
2768
|
-
}
|
|
2535
|
+
};
|
|
2769
2536
|
codeItems.tables.push(joinTable);
|
|
2770
2537
|
return;
|
|
2771
2538
|
};
|
|
@@ -2773,28 +2540,9 @@ const closeAdapters = (adapters) => {
|
|
|
2773
2540
|
return Promise.all(adapters.map((x) => x.close()));
|
|
2774
2541
|
};
|
|
2775
2542
|
|
|
2776
|
-
var __defProp = Object.defineProperty;
|
|
2777
|
-
var __defProps = Object.defineProperties;
|
|
2778
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
2779
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
2780
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
2781
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
2782
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
2783
|
-
var __spreadValues = (a, b) => {
|
|
2784
|
-
for (var prop in b || (b = {}))
|
|
2785
|
-
if (__hasOwnProp.call(b, prop))
|
|
2786
|
-
__defNormalProp(a, prop, b[prop]);
|
|
2787
|
-
if (__getOwnPropSymbols)
|
|
2788
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
2789
|
-
if (__propIsEnum.call(b, prop))
|
|
2790
|
-
__defNormalProp(a, prop, b[prop]);
|
|
2791
|
-
}
|
|
2792
|
-
return a;
|
|
2793
|
-
};
|
|
2794
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
2795
2543
|
const getTableInfosAndFKeys = (asts, config) => {
|
|
2796
|
-
var _a
|
|
2797
|
-
const generateTableTo =
|
|
2544
|
+
var _a;
|
|
2545
|
+
const generateTableTo = config.generateTableTo ?? ((name) => `./tables/${name}.table.ts`);
|
|
2798
2546
|
const tableInfos = {};
|
|
2799
2547
|
const fkeys = {};
|
|
2800
2548
|
for (const ast of asts) {
|
|
@@ -2802,8 +2550,7 @@ const getTableInfosAndFKeys = (asts, config) => {
|
|
|
2802
2550
|
const tableKey = toCamelCase(ast.name);
|
|
2803
2551
|
const dbTableName = ast.schema ? `${ast.schema}.${ast.name}` : ast.name;
|
|
2804
2552
|
let tablePath = path.resolve(config.basePath, generateTableTo(tableKey));
|
|
2805
|
-
if (!tablePath.endsWith(".ts"))
|
|
2806
|
-
tablePath += ".ts";
|
|
2553
|
+
if (!tablePath.endsWith(".ts")) tablePath += ".ts";
|
|
2807
2554
|
const name = toPascalCase(ast.name);
|
|
2808
2555
|
const info = {
|
|
2809
2556
|
dbTableName,
|
|
@@ -2815,9 +2562,8 @@ const getTableInfosAndFKeys = (asts, config) => {
|
|
|
2815
2562
|
tableInfos[dbTableName] = info;
|
|
2816
2563
|
if (ast.constraints) {
|
|
2817
2564
|
for (const { references } of ast.constraints) {
|
|
2818
|
-
if (!references)
|
|
2819
|
-
|
|
2820
|
-
((_c = fkeys[_b = references.fnOrTable]) != null ? _c : fkeys[_b] = []).push({
|
|
2565
|
+
if (!references) continue;
|
|
2566
|
+
(fkeys[_a = references.fnOrTable] ?? (fkeys[_a] = [])).push({
|
|
2821
2567
|
table: info,
|
|
2822
2568
|
references
|
|
2823
2569
|
});
|
|
@@ -2828,7 +2574,6 @@ const getTableInfosAndFKeys = (asts, config) => {
|
|
|
2828
2574
|
return { tableInfos, fkeys };
|
|
2829
2575
|
};
|
|
2830
2576
|
const appCodeGenTable = (tableInfos, fkeys, ast, baseTablePath, baseTableExportedAs) => {
|
|
2831
|
-
var _a, _b;
|
|
2832
2577
|
const tableInfo = tableInfos[ast.schema ? `${ast.schema}.${ast.name}` : ast.name];
|
|
2833
2578
|
const imports = {
|
|
2834
2579
|
"orchid-orm": "Selectable, Insertable, Updatable",
|
|
@@ -2846,7 +2591,7 @@ const appCodeGenTable = (tableInfos, fkeys, ast, baseTablePath, baseTableExporte
|
|
|
2846
2591
|
props.push("noPrimaryKey = true;");
|
|
2847
2592
|
}
|
|
2848
2593
|
const hasTableData = Boolean(
|
|
2849
|
-
ast.primaryKey ||
|
|
2594
|
+
ast.primaryKey || ast.indexes?.length || ast.constraints?.length
|
|
2850
2595
|
);
|
|
2851
2596
|
const shapeCode = columnsShapeToCode({ t: "t", table: ast.name }, ast.shape);
|
|
2852
2597
|
props.push(
|
|
@@ -2875,8 +2620,7 @@ const appCodeGenTable = (tableInfos, fkeys, ast, baseTablePath, baseTableExporte
|
|
|
2875
2620
|
}
|
|
2876
2621
|
if (ast.constraints) {
|
|
2877
2622
|
for (const { references } of ast.constraints) {
|
|
2878
|
-
if (!references)
|
|
2879
|
-
continue;
|
|
2623
|
+
if (!references) continue;
|
|
2880
2624
|
const table = tableInfos[references.fnOrTable];
|
|
2881
2625
|
imports[getImportPath(tableInfo.path, table.path)] = table.className;
|
|
2882
2626
|
relations.push(
|
|
@@ -2903,9 +2647,10 @@ export class ${className} extends ${baseTableExportedAs} {`,
|
|
|
2903
2647
|
props,
|
|
2904
2648
|
"}\n"
|
|
2905
2649
|
];
|
|
2906
|
-
return
|
|
2650
|
+
return {
|
|
2651
|
+
...tableInfo,
|
|
2907
2652
|
content: importsCode + "\n\n" + codeToString(code, "", " ")
|
|
2908
|
-
}
|
|
2653
|
+
};
|
|
2909
2654
|
};
|
|
2910
2655
|
function importsToCode(imports) {
|
|
2911
2656
|
return Object.entries(imports).map(([from, name]) => `import { ${name} } from '${from}';`).join("\n");
|
|
@@ -2924,11 +2669,9 @@ const appCodeGenUpdateDbFile = async (dbPath, tables, extensions, domains) => {
|
|
|
2924
2669
|
let replacedConfig;
|
|
2925
2670
|
if (extensions.length || domains.length) {
|
|
2926
2671
|
let code = content.slice(config.pos, config.end).trim();
|
|
2927
|
-
if (code[0] !== "{")
|
|
2928
|
-
code = `{...${code}}`;
|
|
2672
|
+
if (code[0] !== "{") code = `{...${code}}`;
|
|
2929
2673
|
code = "{\n " + code.slice(1, -1).trim();
|
|
2930
|
-
if (!code.endsWith(","))
|
|
2931
|
-
code += ",";
|
|
2674
|
+
if (!code.endsWith(",")) code += ",";
|
|
2932
2675
|
if (extensions.length) {
|
|
2933
2676
|
code += `
|
|
2934
2677
|
extensions: [${extensions.map(
|
|
@@ -2939,15 +2682,12 @@ const appCodeGenUpdateDbFile = async (dbPath, tables, extensions, domains) => {
|
|
|
2939
2682
|
code += `
|
|
2940
2683
|
domains: {
|
|
2941
2684
|
${domains.sort((a, b) => a.name > b.name ? 1 : -1).map(
|
|
2942
|
-
(ast) => {
|
|
2943
|
-
|
|
2944
|
-
|
|
2945
|
-
|
|
2946
|
-
|
|
2947
|
-
|
|
2948
|
-
(_a = ast.baseType.data.name) != null ? _a : ""
|
|
2949
|
-
)},`;
|
|
2950
|
-
}
|
|
2685
|
+
(ast) => `${quoteObjectKey(
|
|
2686
|
+
ast.schema ? `${ast.schema}.${ast.name}` : ast.name
|
|
2687
|
+
)}: (t) => ${ast.baseType.toCode(
|
|
2688
|
+
{ t: "t", table: ast.name },
|
|
2689
|
+
ast.baseType.data.name ?? ""
|
|
2690
|
+
)},`
|
|
2951
2691
|
).join("\n ")}
|
|
2952
2692
|
},`;
|
|
2953
2693
|
}
|
|
@@ -2985,19 +2725,15 @@ const getTsStatements = (content) => {
|
|
|
2985
2725
|
return createSourceFile("file.ts", content, ScriptTarget.Latest, true).statements;
|
|
2986
2726
|
};
|
|
2987
2727
|
const getOrchidOrmImportName = (statements) => {
|
|
2988
|
-
var _a, _b;
|
|
2989
2728
|
for (const node of statements) {
|
|
2990
|
-
if (node.kind !== SyntaxKind.ImportDeclaration)
|
|
2991
|
-
continue;
|
|
2729
|
+
if (node.kind !== SyntaxKind.ImportDeclaration) continue;
|
|
2992
2730
|
const imp = node;
|
|
2993
2731
|
const source = imp.moduleSpecifier.getText().slice(1, -1);
|
|
2994
|
-
if (source !== "orchid-orm")
|
|
2995
|
-
|
|
2996
|
-
|
|
2997
|
-
continue;
|
|
2998
|
-
const elements = (_a = imp.importClause.namedBindings) == null ? void 0 : _a.elements;
|
|
2732
|
+
if (source !== "orchid-orm") continue;
|
|
2733
|
+
if (!imp.importClause) continue;
|
|
2734
|
+
const elements = imp.importClause.namedBindings?.elements;
|
|
2999
2735
|
for (const element of elements) {
|
|
3000
|
-
if (
|
|
2736
|
+
if (element.propertyName?.escapedText === "orchidORM" || element.name.escapedText === "orchidORM") {
|
|
3001
2737
|
return element.name.escapedText.toString();
|
|
3002
2738
|
}
|
|
3003
2739
|
}
|
|
@@ -3023,8 +2759,7 @@ ${spaces} ${key}: ${className},`;
|
|
|
3023
2759
|
${spaces}`;
|
|
3024
2760
|
}
|
|
3025
2761
|
}
|
|
3026
|
-
if (!imports.length)
|
|
3027
|
-
return;
|
|
2762
|
+
if (!imports.length) return;
|
|
3028
2763
|
let importPos = 0;
|
|
3029
2764
|
for (const node of statements) {
|
|
3030
2765
|
if (node.kind === SyntaxKind.ImportDeclaration) {
|
|
@@ -3037,28 +2772,24 @@ ${spaces}`;
|
|
|
3037
2772
|
};
|
|
3038
2773
|
};
|
|
3039
2774
|
const getTablesListSpaces = (content, object) => {
|
|
3040
|
-
var _a;
|
|
3041
2775
|
const lines = content.slice(0, object.end).split("\n");
|
|
3042
2776
|
const last = lines[lines.length - 1];
|
|
3043
|
-
return
|
|
2777
|
+
return last.match(/^\s+/)?.[0] || "";
|
|
3044
2778
|
};
|
|
3045
2779
|
const getOrchidOrmArgs = (importName, statements) => {
|
|
3046
2780
|
for (const v of statements) {
|
|
3047
|
-
if (v.kind !== SyntaxKind.VariableStatement)
|
|
3048
|
-
continue;
|
|
2781
|
+
if (v.kind !== SyntaxKind.VariableStatement) continue;
|
|
3049
2782
|
for (const node of v.declarationList.declarations) {
|
|
3050
2783
|
const call = node.initializer;
|
|
3051
|
-
if (
|
|
3052
|
-
|
|
3053
|
-
if (call.expression.getText() !== importName)
|
|
3054
|
-
continue;
|
|
2784
|
+
if (call?.kind !== SyntaxKind.CallExpression) continue;
|
|
2785
|
+
if (call.expression.getText() !== importName) continue;
|
|
3055
2786
|
if (call.arguments.length !== 2) {
|
|
3056
2787
|
throw new Error(
|
|
3057
2788
|
"Invalid number of arguments when initializing orchid orm"
|
|
3058
2789
|
);
|
|
3059
2790
|
}
|
|
3060
2791
|
const object = call.arguments[1];
|
|
3061
|
-
if (
|
|
2792
|
+
if (object?.kind !== SyntaxKind.ObjectLiteralExpression) {
|
|
3062
2793
|
throw new Error(
|
|
3063
2794
|
"Second argument of orchidORM must be an object literal"
|
|
3064
2795
|
);
|
|
@@ -3117,8 +2848,7 @@ const pull = async (options, config) => {
|
|
|
3117
2848
|
baseTableExportedAs
|
|
3118
2849
|
);
|
|
3119
2850
|
tables[table.key] = table;
|
|
3120
|
-
if (!firstTable)
|
|
3121
|
-
firstTable = table;
|
|
2851
|
+
if (!firstTable) firstTable = table;
|
|
3122
2852
|
pendingFileWrites.push([
|
|
3123
2853
|
table.path,
|
|
3124
2854
|
table.content,
|
|
@@ -3144,24 +2874,21 @@ const pull = async (options, config) => {
|
|
|
3144
2874
|
return;
|
|
3145
2875
|
}
|
|
3146
2876
|
let dbPath = path.resolve(config.basePath, config.dbPath);
|
|
3147
|
-
if (!dbPath.endsWith(".ts"))
|
|
3148
|
-
dbPath += ".ts";
|
|
2877
|
+
if (!dbPath.endsWith(".ts")) dbPath += ".ts";
|
|
3149
2878
|
const content = await appCodeGenUpdateDbFile(
|
|
3150
2879
|
dbPath,
|
|
3151
2880
|
tables,
|
|
3152
2881
|
extensions,
|
|
3153
2882
|
domains
|
|
3154
2883
|
);
|
|
3155
|
-
if (content)
|
|
3156
|
-
pendingFileWrites.push([dbPath, content]);
|
|
2884
|
+
if (content) pendingFileWrites.push([dbPath, content]);
|
|
3157
2885
|
if (firstTable) {
|
|
3158
2886
|
await fs.mkdir(path.dirname(firstTable.path), { recursive: true });
|
|
3159
2887
|
}
|
|
3160
2888
|
await Promise.all(
|
|
3161
2889
|
pendingFileWrites.map(
|
|
3162
2890
|
([path2, content2, options2]) => fs.writeFile(path2, content2, options2).then(() => {
|
|
3163
|
-
|
|
3164
|
-
(_a = config.logger) == null ? void 0 : _a.log(`Created ${pathToLog(path2)}`);
|
|
2891
|
+
config.logger?.log(`Created ${pathToLog(path2)}`);
|
|
3165
2892
|
})
|
|
3166
2893
|
)
|
|
3167
2894
|
);
|