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