orchid-orm 1.4.17 → 1.4.20
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/CHANGELOG.md +15 -0
- package/dist/index.d.ts +6 -2
- package/dist/index.esm.js +925 -86
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +948 -84
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/appCodeUpdater/appCodeUpdater.test.ts +42 -0
- package/src/appCodeUpdater/appCodeUpdater.ts +17 -6
- package/src/appCodeUpdater/testUtils.ts +26 -2
- package/src/appCodeUpdater/tsUtils.ts +63 -19
- package/src/appCodeUpdater/updateMainFile.test.ts +18 -44
- package/src/appCodeUpdater/updateMainFile.ts +5 -16
- package/src/appCodeUpdater/updateTableFile/changeTable.test.ts +647 -0
- package/src/appCodeUpdater/updateTableFile/changeTable.ts +512 -0
- package/src/appCodeUpdater/updateTableFile/createTable.test.ts +86 -0
- package/src/appCodeUpdater/updateTableFile/createTable.ts +34 -0
- package/src/appCodeUpdater/updateTableFile/renameTable.test.ts +43 -0
- package/src/appCodeUpdater/updateTableFile/renameTable.ts +40 -0
- package/src/appCodeUpdater/updateTableFile/updateTableFile.ts +22 -0
- package/src/appCodeUpdater/utils.ts +10 -0
- package/src/index.ts +1 -4
- package/src/table.test.ts +3 -0
- package/src/appCodeUpdater/updateTableFile.ts +0 -19
package/dist/index.js
CHANGED
|
@@ -3,6 +3,33 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var pqb = require('pqb');
|
|
6
|
+
var path = require('path');
|
|
7
|
+
var fs = require('fs/promises');
|
|
8
|
+
var typescript = require('typescript');
|
|
9
|
+
|
|
10
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
11
|
+
|
|
12
|
+
function _interopNamespace(e) {
|
|
13
|
+
if (e && e.__esModule) return e;
|
|
14
|
+
var n = Object.create(null);
|
|
15
|
+
if (e) {
|
|
16
|
+
Object.keys(e).forEach(function (k) {
|
|
17
|
+
if (k !== 'default') {
|
|
18
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
19
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
get: function () { return e[k]; }
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
n["default"] = e;
|
|
27
|
+
return Object.freeze(n);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
|
|
31
|
+
var path__namespace = /*#__PURE__*/_interopNamespace(path);
|
|
32
|
+
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
|
|
6
33
|
|
|
7
34
|
const createBaseTable = (options) => {
|
|
8
35
|
return class BaseTable {
|
|
@@ -296,25 +323,25 @@ const hasRelationHandleUpdate = (q, ctx, set, key, primaryKey, nestedUpdate) =>
|
|
|
296
323
|
});
|
|
297
324
|
};
|
|
298
325
|
|
|
299
|
-
var __defProp$
|
|
300
|
-
var __defProps$
|
|
301
|
-
var __getOwnPropDescs$
|
|
302
|
-
var __getOwnPropSymbols$
|
|
303
|
-
var __hasOwnProp$
|
|
304
|
-
var __propIsEnum$
|
|
305
|
-
var __defNormalProp$
|
|
306
|
-
var __spreadValues$
|
|
326
|
+
var __defProp$6 = Object.defineProperty;
|
|
327
|
+
var __defProps$4 = Object.defineProperties;
|
|
328
|
+
var __getOwnPropDescs$4 = Object.getOwnPropertyDescriptors;
|
|
329
|
+
var __getOwnPropSymbols$9 = Object.getOwnPropertySymbols;
|
|
330
|
+
var __hasOwnProp$9 = Object.prototype.hasOwnProperty;
|
|
331
|
+
var __propIsEnum$9 = Object.prototype.propertyIsEnumerable;
|
|
332
|
+
var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
333
|
+
var __spreadValues$6 = (a, b) => {
|
|
307
334
|
for (var prop in b || (b = {}))
|
|
308
|
-
if (__hasOwnProp$
|
|
309
|
-
__defNormalProp$
|
|
310
|
-
if (__getOwnPropSymbols$
|
|
311
|
-
for (var prop of __getOwnPropSymbols$
|
|
312
|
-
if (__propIsEnum$
|
|
313
|
-
__defNormalProp$
|
|
335
|
+
if (__hasOwnProp$9.call(b, prop))
|
|
336
|
+
__defNormalProp$6(a, prop, b[prop]);
|
|
337
|
+
if (__getOwnPropSymbols$9)
|
|
338
|
+
for (var prop of __getOwnPropSymbols$9(b)) {
|
|
339
|
+
if (__propIsEnum$9.call(b, prop))
|
|
340
|
+
__defNormalProp$6(a, prop, b[prop]);
|
|
314
341
|
}
|
|
315
342
|
return a;
|
|
316
343
|
};
|
|
317
|
-
var __spreadProps$
|
|
344
|
+
var __spreadProps$4 = (a, b) => __defProps$4(a, __getOwnPropDescs$4(b));
|
|
318
345
|
class HasOneVirtualColumn extends pqb.VirtualColumn {
|
|
319
346
|
constructor(key, state) {
|
|
320
347
|
super();
|
|
@@ -447,7 +474,7 @@ const nestedInsert$2 = ({ query, primaryKey, foreignKey }) => {
|
|
|
447
474
|
);
|
|
448
475
|
if (create.length) {
|
|
449
476
|
await t._count()._createMany(
|
|
450
|
-
create.map(([selfData, item]) => __spreadValues$
|
|
477
|
+
create.map(([selfData, item]) => __spreadValues$6({
|
|
451
478
|
[foreignKey]: selfData[primaryKey]
|
|
452
479
|
}, "create" in item ? item.create : item.connectOrCreate.create))
|
|
453
480
|
);
|
|
@@ -468,7 +495,7 @@ const nestedUpdate$2 = ({ query, primaryKey, foreignKey }) => {
|
|
|
468
495
|
if (params.create || params.disconnect || params.set) {
|
|
469
496
|
await currentRelationsQuery._update({ [foreignKey]: null });
|
|
470
497
|
if (params.create) {
|
|
471
|
-
await t._count()._create(__spreadProps$
|
|
498
|
+
await t._count()._create(__spreadProps$4(__spreadValues$6({}, params.create), {
|
|
472
499
|
[foreignKey]: data[0][primaryKey]
|
|
473
500
|
}));
|
|
474
501
|
}
|
|
@@ -484,7 +511,7 @@ const nestedUpdate$2 = ({ query, primaryKey, foreignKey }) => {
|
|
|
484
511
|
const updatedIds = await currentRelationsQuery._pluck(foreignKey)._update(update);
|
|
485
512
|
if (updatedIds.length < ids.length) {
|
|
486
513
|
await t.createMany(
|
|
487
|
-
ids.filter((id) => !updatedIds.includes(id)).map((id) => __spreadProps$
|
|
514
|
+
ids.filter((id) => !updatedIds.includes(id)).map((id) => __spreadProps$4(__spreadValues$6({}, create), {
|
|
488
515
|
[foreignKey]: id
|
|
489
516
|
}))
|
|
490
517
|
);
|
|
@@ -493,25 +520,25 @@ const nestedUpdate$2 = ({ query, primaryKey, foreignKey }) => {
|
|
|
493
520
|
};
|
|
494
521
|
};
|
|
495
522
|
|
|
496
|
-
var __defProp$
|
|
497
|
-
var __defProps$
|
|
498
|
-
var __getOwnPropDescs$
|
|
499
|
-
var __getOwnPropSymbols$
|
|
500
|
-
var __hasOwnProp$
|
|
501
|
-
var __propIsEnum$
|
|
502
|
-
var __defNormalProp$
|
|
503
|
-
var __spreadValues$
|
|
523
|
+
var __defProp$5 = Object.defineProperty;
|
|
524
|
+
var __defProps$3 = Object.defineProperties;
|
|
525
|
+
var __getOwnPropDescs$3 = Object.getOwnPropertyDescriptors;
|
|
526
|
+
var __getOwnPropSymbols$8 = Object.getOwnPropertySymbols;
|
|
527
|
+
var __hasOwnProp$8 = Object.prototype.hasOwnProperty;
|
|
528
|
+
var __propIsEnum$8 = Object.prototype.propertyIsEnumerable;
|
|
529
|
+
var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
530
|
+
var __spreadValues$5 = (a, b) => {
|
|
504
531
|
for (var prop in b || (b = {}))
|
|
505
|
-
if (__hasOwnProp$
|
|
506
|
-
__defNormalProp$
|
|
507
|
-
if (__getOwnPropSymbols$
|
|
508
|
-
for (var prop of __getOwnPropSymbols$
|
|
509
|
-
if (__propIsEnum$
|
|
510
|
-
__defNormalProp$
|
|
532
|
+
if (__hasOwnProp$8.call(b, prop))
|
|
533
|
+
__defNormalProp$5(a, prop, b[prop]);
|
|
534
|
+
if (__getOwnPropSymbols$8)
|
|
535
|
+
for (var prop of __getOwnPropSymbols$8(b)) {
|
|
536
|
+
if (__propIsEnum$8.call(b, prop))
|
|
537
|
+
__defNormalProp$5(a, prop, b[prop]);
|
|
511
538
|
}
|
|
512
539
|
return a;
|
|
513
540
|
};
|
|
514
|
-
var __spreadProps$
|
|
541
|
+
var __spreadProps$3 = (a, b) => __defProps$3(a, __getOwnPropDescs$3(b));
|
|
515
542
|
class HasManyVirtualColumn extends pqb.VirtualColumn {
|
|
516
543
|
constructor(key, state) {
|
|
517
544
|
super();
|
|
@@ -677,10 +704,10 @@ const nestedInsert$1 = ({ query, primaryKey, foreignKey }) => {
|
|
|
677
704
|
await t._createMany(
|
|
678
705
|
create.flatMap(([selfData, { create: create2 = [], connectOrCreate: connectOrCreate2 = [] }]) => {
|
|
679
706
|
return [
|
|
680
|
-
...create2.map((item) => __spreadValues$
|
|
707
|
+
...create2.map((item) => __spreadValues$5({
|
|
681
708
|
[foreignKey]: selfData[primaryKey]
|
|
682
709
|
}, item)),
|
|
683
|
-
...connectOrCreate2.filter(() => connected[connectedI++] === 0).map((item) => __spreadValues$
|
|
710
|
+
...connectOrCreate2.filter(() => connected[connectedI++] === 0).map((item) => __spreadValues$5({
|
|
684
711
|
[foreignKey]: selfData[primaryKey]
|
|
685
712
|
}, item.create))
|
|
686
713
|
];
|
|
@@ -699,7 +726,7 @@ const nestedUpdate$1 = ({ query, primaryKey, foreignKey }) => {
|
|
|
699
726
|
const t = query.transacting(q);
|
|
700
727
|
if (params.create) {
|
|
701
728
|
await t._count()._createMany(
|
|
702
|
-
params.create.map((create) => __spreadProps$
|
|
729
|
+
params.create.map((create) => __spreadProps$3(__spreadValues$5({}, create), {
|
|
703
730
|
[foreignKey]: data[0][primaryKey]
|
|
704
731
|
}))
|
|
705
732
|
);
|
|
@@ -742,19 +769,19 @@ const nestedUpdate$1 = ({ query, primaryKey, foreignKey }) => {
|
|
|
742
769
|
};
|
|
743
770
|
};
|
|
744
771
|
|
|
745
|
-
var __defProp$
|
|
746
|
-
var __getOwnPropSymbols$
|
|
747
|
-
var __hasOwnProp$
|
|
748
|
-
var __propIsEnum$
|
|
749
|
-
var __defNormalProp$
|
|
750
|
-
var __spreadValues$
|
|
772
|
+
var __defProp$4 = Object.defineProperty;
|
|
773
|
+
var __getOwnPropSymbols$7 = Object.getOwnPropertySymbols;
|
|
774
|
+
var __hasOwnProp$7 = Object.prototype.hasOwnProperty;
|
|
775
|
+
var __propIsEnum$7 = Object.prototype.propertyIsEnumerable;
|
|
776
|
+
var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
777
|
+
var __spreadValues$4 = (a, b) => {
|
|
751
778
|
for (var prop in b || (b = {}))
|
|
752
|
-
if (__hasOwnProp$
|
|
753
|
-
__defNormalProp$
|
|
754
|
-
if (__getOwnPropSymbols$
|
|
755
|
-
for (var prop of __getOwnPropSymbols$
|
|
756
|
-
if (__propIsEnum$
|
|
757
|
-
__defNormalProp$
|
|
779
|
+
if (__hasOwnProp$7.call(b, prop))
|
|
780
|
+
__defNormalProp$4(a, prop, b[prop]);
|
|
781
|
+
if (__getOwnPropSymbols$7)
|
|
782
|
+
for (var prop of __getOwnPropSymbols$7(b)) {
|
|
783
|
+
if (__propIsEnum$7.call(b, prop))
|
|
784
|
+
__defNormalProp$4(a, prop, b[prop]);
|
|
758
785
|
}
|
|
759
786
|
return a;
|
|
760
787
|
};
|
|
@@ -807,7 +834,7 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
807
834
|
[afk]: query.shape[apk]
|
|
808
835
|
};
|
|
809
836
|
const subQuery = Object.create(__table);
|
|
810
|
-
subQuery.query = __spreadValues$
|
|
837
|
+
subQuery.query = __spreadValues$4({}, subQuery.query);
|
|
811
838
|
const state = {
|
|
812
839
|
relatedTableQuery: query,
|
|
813
840
|
joinTableQuery: subQuery,
|
|
@@ -1241,33 +1268,33 @@ function transaction(fn) {
|
|
|
1241
1268
|
});
|
|
1242
1269
|
}
|
|
1243
1270
|
|
|
1244
|
-
var __defProp$
|
|
1245
|
-
var __defProps = Object.defineProperties;
|
|
1246
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
1247
|
-
var __getOwnPropSymbols$
|
|
1248
|
-
var __hasOwnProp$
|
|
1249
|
-
var __propIsEnum$
|
|
1250
|
-
var __defNormalProp$
|
|
1251
|
-
var __spreadValues$
|
|
1271
|
+
var __defProp$3 = Object.defineProperty;
|
|
1272
|
+
var __defProps$2 = Object.defineProperties;
|
|
1273
|
+
var __getOwnPropDescs$2 = Object.getOwnPropertyDescriptors;
|
|
1274
|
+
var __getOwnPropSymbols$6 = Object.getOwnPropertySymbols;
|
|
1275
|
+
var __hasOwnProp$6 = Object.prototype.hasOwnProperty;
|
|
1276
|
+
var __propIsEnum$6 = Object.prototype.propertyIsEnumerable;
|
|
1277
|
+
var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1278
|
+
var __spreadValues$3 = (a, b) => {
|
|
1252
1279
|
for (var prop in b || (b = {}))
|
|
1253
|
-
if (__hasOwnProp$
|
|
1254
|
-
__defNormalProp$
|
|
1255
|
-
if (__getOwnPropSymbols$
|
|
1256
|
-
for (var prop of __getOwnPropSymbols$
|
|
1257
|
-
if (__propIsEnum$
|
|
1258
|
-
__defNormalProp$
|
|
1280
|
+
if (__hasOwnProp$6.call(b, prop))
|
|
1281
|
+
__defNormalProp$3(a, prop, b[prop]);
|
|
1282
|
+
if (__getOwnPropSymbols$6)
|
|
1283
|
+
for (var prop of __getOwnPropSymbols$6(b)) {
|
|
1284
|
+
if (__propIsEnum$6.call(b, prop))
|
|
1285
|
+
__defNormalProp$3(a, prop, b[prop]);
|
|
1259
1286
|
}
|
|
1260
1287
|
return a;
|
|
1261
1288
|
};
|
|
1262
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
1263
|
-
var __objRest = (source, exclude) => {
|
|
1289
|
+
var __spreadProps$2 = (a, b) => __defProps$2(a, __getOwnPropDescs$2(b));
|
|
1290
|
+
var __objRest$3 = (source, exclude) => {
|
|
1264
1291
|
var target = {};
|
|
1265
1292
|
for (var prop in source)
|
|
1266
|
-
if (__hasOwnProp$
|
|
1293
|
+
if (__hasOwnProp$6.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
1267
1294
|
target[prop] = source[prop];
|
|
1268
|
-
if (source != null && __getOwnPropSymbols$
|
|
1269
|
-
for (var prop of __getOwnPropSymbols$
|
|
1270
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum$
|
|
1295
|
+
if (source != null && __getOwnPropSymbols$6)
|
|
1296
|
+
for (var prop of __getOwnPropSymbols$6(source)) {
|
|
1297
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum$6.call(source, prop))
|
|
1271
1298
|
target[prop] = source[prop];
|
|
1272
1299
|
}
|
|
1273
1300
|
return target;
|
|
@@ -1278,7 +1305,7 @@ const orchidORM = (_a, tables) => {
|
|
|
1278
1305
|
logger,
|
|
1279
1306
|
autoPreparedStatements,
|
|
1280
1307
|
noPrimaryKey = "error"
|
|
1281
|
-
} = _b, options = __objRest(_b, [
|
|
1308
|
+
} = _b, options = __objRest$3(_b, [
|
|
1282
1309
|
"log",
|
|
1283
1310
|
"logger",
|
|
1284
1311
|
"autoPreparedStatements",
|
|
@@ -1313,7 +1340,7 @@ const orchidORM = (_a, tables) => {
|
|
|
1313
1340
|
}
|
|
1314
1341
|
const table = new tables[key]();
|
|
1315
1342
|
tableInstances[key] = table;
|
|
1316
|
-
const options2 = __spreadProps(__spreadValues$
|
|
1343
|
+
const options2 = __spreadProps$2(__spreadValues$3({}, commonOptions), {
|
|
1317
1344
|
schema: table.schema
|
|
1318
1345
|
});
|
|
1319
1346
|
if (table.noPrimaryKey)
|
|
@@ -1334,24 +1361,24 @@ const orchidORM = (_a, tables) => {
|
|
|
1334
1361
|
return result;
|
|
1335
1362
|
};
|
|
1336
1363
|
|
|
1337
|
-
var __defProp = Object.defineProperty;
|
|
1338
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
1339
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
1340
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
1341
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1342
|
-
var __spreadValues = (a, b) => {
|
|
1364
|
+
var __defProp$2 = Object.defineProperty;
|
|
1365
|
+
var __getOwnPropSymbols$5 = Object.getOwnPropertySymbols;
|
|
1366
|
+
var __hasOwnProp$5 = Object.prototype.hasOwnProperty;
|
|
1367
|
+
var __propIsEnum$5 = Object.prototype.propertyIsEnumerable;
|
|
1368
|
+
var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1369
|
+
var __spreadValues$2 = (a, b) => {
|
|
1343
1370
|
for (var prop in b || (b = {}))
|
|
1344
|
-
if (__hasOwnProp.call(b, prop))
|
|
1345
|
-
__defNormalProp(a, prop, b[prop]);
|
|
1346
|
-
if (__getOwnPropSymbols)
|
|
1347
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
1348
|
-
if (__propIsEnum.call(b, prop))
|
|
1349
|
-
__defNormalProp(a, prop, b[prop]);
|
|
1371
|
+
if (__hasOwnProp$5.call(b, prop))
|
|
1372
|
+
__defNormalProp$2(a, prop, b[prop]);
|
|
1373
|
+
if (__getOwnPropSymbols$5)
|
|
1374
|
+
for (var prop of __getOwnPropSymbols$5(b)) {
|
|
1375
|
+
if (__propIsEnum$5.call(b, prop))
|
|
1376
|
+
__defNormalProp$2(a, prop, b[prop]);
|
|
1350
1377
|
}
|
|
1351
1378
|
return a;
|
|
1352
1379
|
};
|
|
1353
1380
|
const createRepo = (table, methods) => {
|
|
1354
|
-
const queryMethods = __spreadValues(__spreadValues(__spreadValues(__spreadValues({}, methods.queryMethods), methods.queryOneMethods), methods.queryWithWhereMethods), methods.queryOneWithWhereMethods);
|
|
1381
|
+
const queryMethods = __spreadValues$2(__spreadValues$2(__spreadValues$2(__spreadValues$2({}, methods.queryMethods), methods.queryOneMethods), methods.queryWithWhereMethods), methods.queryOneWithWhereMethods);
|
|
1355
1382
|
const plainMethods = methods.methods;
|
|
1356
1383
|
const repo = (q2) => {
|
|
1357
1384
|
const proto = Object.create(q2.__table);
|
|
@@ -1377,6 +1404,843 @@ const createRepo = (table, methods) => {
|
|
|
1377
1404
|
});
|
|
1378
1405
|
};
|
|
1379
1406
|
|
|
1407
|
+
const toPascalCase = (s) => {
|
|
1408
|
+
const words = s.match(/(\w)(\w*)/g) || [];
|
|
1409
|
+
return words.map((word) => word[0].toUpperCase() + word.slice(1)).join("");
|
|
1410
|
+
};
|
|
1411
|
+
const toCamelCase = (s) => {
|
|
1412
|
+
const pascal = toPascalCase(s);
|
|
1413
|
+
return pascal[0].toLowerCase() + pascal.slice(1);
|
|
1414
|
+
};
|
|
1415
|
+
|
|
1416
|
+
class FileChanges {
|
|
1417
|
+
constructor(content) {
|
|
1418
|
+
this.content = content;
|
|
1419
|
+
this.ranges = [];
|
|
1420
|
+
}
|
|
1421
|
+
add(at, text, end = at) {
|
|
1422
|
+
if (this.ranges.length === 0) {
|
|
1423
|
+
this.ranges.push([0, at], text, [end, this.content.length]);
|
|
1424
|
+
} else {
|
|
1425
|
+
const last = this.ranges[this.ranges.length - 1];
|
|
1426
|
+
last[1] = at;
|
|
1427
|
+
this.ranges.push(text, [end, this.content.length]);
|
|
1428
|
+
}
|
|
1429
|
+
}
|
|
1430
|
+
replace(from, to, text) {
|
|
1431
|
+
this.add(from, text, to);
|
|
1432
|
+
}
|
|
1433
|
+
remove(from, to) {
|
|
1434
|
+
if (this.ranges.length === 0) {
|
|
1435
|
+
this.ranges.push([0, from], [to, this.content.length]);
|
|
1436
|
+
} else {
|
|
1437
|
+
const last = this.ranges[this.ranges.length - 1];
|
|
1438
|
+
last[1] = from;
|
|
1439
|
+
this.ranges.push([to, this.content.length]);
|
|
1440
|
+
}
|
|
1441
|
+
}
|
|
1442
|
+
apply() {
|
|
1443
|
+
return this.ranges.length ? this.ranges.map(
|
|
1444
|
+
(item) => typeof item === "string" ? item : this.content.slice(item[0], item[1])
|
|
1445
|
+
).join("") : this.content;
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
const iterate = (kind) => {
|
|
1450
|
+
return function* (statements) {
|
|
1451
|
+
for (const node of statements) {
|
|
1452
|
+
if (node.kind === kind) {
|
|
1453
|
+
yield node;
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
1456
|
+
};
|
|
1457
|
+
};
|
|
1458
|
+
const isNode = (kind) => {
|
|
1459
|
+
return (node) => {
|
|
1460
|
+
return (node == null ? void 0 : node.kind) === kind;
|
|
1461
|
+
};
|
|
1462
|
+
};
|
|
1463
|
+
const ts = {
|
|
1464
|
+
getStatements(content) {
|
|
1465
|
+
const { statements } = typescript.createSourceFile(
|
|
1466
|
+
"file.ts",
|
|
1467
|
+
content,
|
|
1468
|
+
typescript.ScriptTarget.Latest,
|
|
1469
|
+
true
|
|
1470
|
+
);
|
|
1471
|
+
return statements;
|
|
1472
|
+
},
|
|
1473
|
+
is: {
|
|
1474
|
+
call: isNode(typescript.SyntaxKind.CallExpression),
|
|
1475
|
+
objectLiteral: isNode(
|
|
1476
|
+
typescript.SyntaxKind.ObjectLiteralExpression
|
|
1477
|
+
),
|
|
1478
|
+
propertyAssignment: isNode(
|
|
1479
|
+
typescript.SyntaxKind.PropertyAssignment
|
|
1480
|
+
),
|
|
1481
|
+
shorthandPropertyAssignment: isNode(
|
|
1482
|
+
typescript.SyntaxKind.ShorthandPropertyAssignment
|
|
1483
|
+
),
|
|
1484
|
+
identifier: isNode(typescript.SyntaxKind.Identifier),
|
|
1485
|
+
stringLiteral: isNode(typescript.SyntaxKind.StringLiteral),
|
|
1486
|
+
arrayLiteral: isNode(
|
|
1487
|
+
typescript.SyntaxKind.ArrayLiteralExpression
|
|
1488
|
+
),
|
|
1489
|
+
numericLiteral: isNode(typescript.SyntaxKind.NumericLiteral),
|
|
1490
|
+
computedPropertyName: isNode(
|
|
1491
|
+
typescript.SyntaxKind.ComputedPropertyName
|
|
1492
|
+
),
|
|
1493
|
+
privateIdentifier: isNode(typescript.SyntaxKind.PrivateIdentifier),
|
|
1494
|
+
this: isNode(typescript.SyntaxKind.ThisKeyword),
|
|
1495
|
+
propertyAccess: isNode(
|
|
1496
|
+
typescript.SyntaxKind.PropertyAccessExpression
|
|
1497
|
+
),
|
|
1498
|
+
arrowFunction: isNode(typescript.SyntaxKind.ArrowFunction),
|
|
1499
|
+
parenthesizedExpression: isNode(
|
|
1500
|
+
typescript.SyntaxKind.ParenthesizedExpression
|
|
1501
|
+
),
|
|
1502
|
+
spreadAssignment: isNode(typescript.SyntaxKind.SpreadAssignment)
|
|
1503
|
+
},
|
|
1504
|
+
import: {
|
|
1505
|
+
iterate: iterate(typescript.SyntaxKind.ImportDeclaration),
|
|
1506
|
+
*iterateWithSource(statements, path) {
|
|
1507
|
+
for (const node of ts.import.iterate(statements)) {
|
|
1508
|
+
if (ts.import.getSource(node) !== path)
|
|
1509
|
+
continue;
|
|
1510
|
+
yield node;
|
|
1511
|
+
}
|
|
1512
|
+
},
|
|
1513
|
+
getSource(node) {
|
|
1514
|
+
return node.moduleSpecifier.getText().slice(1, -1);
|
|
1515
|
+
},
|
|
1516
|
+
getEndPos(statements) {
|
|
1517
|
+
let end = 0;
|
|
1518
|
+
for (const node of ts.import.iterate(statements)) {
|
|
1519
|
+
end = node.end;
|
|
1520
|
+
}
|
|
1521
|
+
return end;
|
|
1522
|
+
},
|
|
1523
|
+
getStatementsImportedName(statements, path, key) {
|
|
1524
|
+
for (const node of ts.import.iterateWithSource(statements, path)) {
|
|
1525
|
+
const name = ts.import.getImportName(node, key);
|
|
1526
|
+
if (name)
|
|
1527
|
+
return name;
|
|
1528
|
+
}
|
|
1529
|
+
return;
|
|
1530
|
+
},
|
|
1531
|
+
getImportName(node, key) {
|
|
1532
|
+
var _a, _b;
|
|
1533
|
+
if (!node.importClause)
|
|
1534
|
+
return;
|
|
1535
|
+
const elements = (_a = node.importClause.namedBindings) == null ? void 0 : _a.elements;
|
|
1536
|
+
if (!elements)
|
|
1537
|
+
return;
|
|
1538
|
+
for (const element of elements) {
|
|
1539
|
+
if (((_b = element.propertyName) == null ? void 0 : _b.escapedText) === key || element.name.escapedText === key) {
|
|
1540
|
+
return element.name.escapedText.toString();
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
return;
|
|
1544
|
+
}
|
|
1545
|
+
},
|
|
1546
|
+
variable: {
|
|
1547
|
+
iterate: iterate(typescript.SyntaxKind.VariableStatement),
|
|
1548
|
+
*iterateDeclarations(statements) {
|
|
1549
|
+
for (const node of ts.variable.iterate(statements)) {
|
|
1550
|
+
for (const dec of node.declarationList.declarations) {
|
|
1551
|
+
yield dec;
|
|
1552
|
+
}
|
|
1553
|
+
}
|
|
1554
|
+
}
|
|
1555
|
+
},
|
|
1556
|
+
class: {
|
|
1557
|
+
iterate: iterate(typescript.SyntaxKind.ClassDeclaration)
|
|
1558
|
+
},
|
|
1559
|
+
prop: {
|
|
1560
|
+
getName({ name }) {
|
|
1561
|
+
if (ts.is.identifier(name)) {
|
|
1562
|
+
return name.escapedText;
|
|
1563
|
+
} else {
|
|
1564
|
+
return name == null ? void 0 : name.getText();
|
|
1565
|
+
}
|
|
1566
|
+
},
|
|
1567
|
+
getValue(prop) {
|
|
1568
|
+
if (ts.is.propertyAssignment(prop)) {
|
|
1569
|
+
return prop.initializer.getText();
|
|
1570
|
+
} else if (ts.is.shorthandPropertyAssignment(prop)) {
|
|
1571
|
+
return prop.name.escapedText.toString();
|
|
1572
|
+
} else {
|
|
1573
|
+
return;
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
},
|
|
1577
|
+
spaces: {
|
|
1578
|
+
getAtLine(content, pos) {
|
|
1579
|
+
var _a;
|
|
1580
|
+
const lines = content.slice(0, pos).split("\n");
|
|
1581
|
+
const last = lines[lines.length - 1];
|
|
1582
|
+
return ((_a = last.match(/^\s+/)) == null ? void 0 : _a[0]) || "";
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
};
|
|
1586
|
+
|
|
1587
|
+
const getRelativePath = (from, to) => {
|
|
1588
|
+
const rel = path__default["default"].relative(path__default["default"].dirname(from), to);
|
|
1589
|
+
return rel.startsWith("./") || rel.startsWith("../") ? rel : `./${rel}`;
|
|
1590
|
+
};
|
|
1591
|
+
const getImportPath = (from, to) => {
|
|
1592
|
+
return getRelativePath(from, to).replace(/\.[tj]s$/, "");
|
|
1593
|
+
};
|
|
1594
|
+
|
|
1595
|
+
const libraryName = "orchid-orm";
|
|
1596
|
+
const importKey = "orchidORM";
|
|
1597
|
+
const updateMainFile = async (path, tablePath, ast) => {
|
|
1598
|
+
const content = await fs__default["default"].readFile(path, "utf-8");
|
|
1599
|
+
const statements = ts.getStatements(content);
|
|
1600
|
+
const importName = ts.import.getStatementsImportedName(
|
|
1601
|
+
statements,
|
|
1602
|
+
libraryName,
|
|
1603
|
+
importKey
|
|
1604
|
+
);
|
|
1605
|
+
if (!importName) {
|
|
1606
|
+
throw new AppCodeUpdaterError(
|
|
1607
|
+
`Main file does not contain import of orchid-orm`
|
|
1608
|
+
);
|
|
1609
|
+
}
|
|
1610
|
+
const object = getTablesListObject(importName, statements);
|
|
1611
|
+
if (!object) {
|
|
1612
|
+
throw new Error("List of tables is not found in main file");
|
|
1613
|
+
}
|
|
1614
|
+
const spaces = ts.spaces.getAtLine(content, object.end);
|
|
1615
|
+
const context = {
|
|
1616
|
+
path,
|
|
1617
|
+
tablePath,
|
|
1618
|
+
statements,
|
|
1619
|
+
object,
|
|
1620
|
+
content,
|
|
1621
|
+
spaces
|
|
1622
|
+
};
|
|
1623
|
+
if (ast.type === "table") {
|
|
1624
|
+
if (ast.action === "create") {
|
|
1625
|
+
return fs__default["default"].writeFile(path, createTable$1(context, ast));
|
|
1626
|
+
} else {
|
|
1627
|
+
return fs__default["default"].writeFile(path, dropTable(context, ast));
|
|
1628
|
+
}
|
|
1629
|
+
}
|
|
1630
|
+
};
|
|
1631
|
+
const createTable$1 = ({ path, tablePath, statements, object, content, spaces }, ast) => {
|
|
1632
|
+
const key = toCamelCase(ast.name);
|
|
1633
|
+
const value = toPascalCase(ast.name);
|
|
1634
|
+
const changes = new FileChanges(content);
|
|
1635
|
+
const importPath = getImportPath(path, tablePath(ast.name));
|
|
1636
|
+
const importPos = ts.import.getEndPos(statements);
|
|
1637
|
+
changes.add(
|
|
1638
|
+
importPos,
|
|
1639
|
+
`${importPos === 0 ? "" : "\n"}import { ${value} } from '${importPath}';`
|
|
1640
|
+
);
|
|
1641
|
+
let insert = `
|
|
1642
|
+
${spaces} ${key}: ${value},`;
|
|
1643
|
+
if (object.properties.length && !object.properties.hasTrailingComma) {
|
|
1644
|
+
insert = `,${insert}`;
|
|
1645
|
+
}
|
|
1646
|
+
if (!content.slice(object.properties.end, object.end).includes("\n")) {
|
|
1647
|
+
insert += `
|
|
1648
|
+
${spaces}`;
|
|
1649
|
+
}
|
|
1650
|
+
changes.add(object.properties.end, insert);
|
|
1651
|
+
return changes.apply();
|
|
1652
|
+
};
|
|
1653
|
+
const dropTable = ({ path, tablePath, statements, object, content }, ast) => {
|
|
1654
|
+
const changes = new FileChanges(content);
|
|
1655
|
+
const importPath = getImportPath(path, tablePath(ast.name));
|
|
1656
|
+
const tableClassName = toPascalCase(ast.name);
|
|
1657
|
+
const importNames = [];
|
|
1658
|
+
for (const node of ts.import.iterateWithSource(statements, importPath)) {
|
|
1659
|
+
changes.remove(node.pos, node.end);
|
|
1660
|
+
const name = ts.import.getImportName(node, tableClassName);
|
|
1661
|
+
if (name && !importNames.includes(name)) {
|
|
1662
|
+
importNames.push(name);
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1665
|
+
for (const prop of object.properties) {
|
|
1666
|
+
const name = ts.prop.getValue(prop);
|
|
1667
|
+
if (!name || !importNames.includes(name))
|
|
1668
|
+
continue;
|
|
1669
|
+
let { end } = prop;
|
|
1670
|
+
if (content[end] === ",")
|
|
1671
|
+
end++;
|
|
1672
|
+
changes.remove(prop.pos, end);
|
|
1673
|
+
}
|
|
1674
|
+
return changes.apply();
|
|
1675
|
+
};
|
|
1676
|
+
const getTablesListObject = (importName, statements) => {
|
|
1677
|
+
for (const node of ts.variable.iterateDeclarations(statements)) {
|
|
1678
|
+
const call = node.initializer;
|
|
1679
|
+
if (!ts.is.call(call))
|
|
1680
|
+
continue;
|
|
1681
|
+
if (call.expression.getText() !== importName)
|
|
1682
|
+
continue;
|
|
1683
|
+
if (call.arguments.length !== 2) {
|
|
1684
|
+
throw new Error(
|
|
1685
|
+
"Invalid number of arguments when initializing orchid orm"
|
|
1686
|
+
);
|
|
1687
|
+
}
|
|
1688
|
+
const object = call.arguments[1];
|
|
1689
|
+
if (!ts.is.objectLiteral(object)) {
|
|
1690
|
+
throw new Error("Second argument of orchidORM must be an object literal");
|
|
1691
|
+
}
|
|
1692
|
+
return object;
|
|
1693
|
+
}
|
|
1694
|
+
return;
|
|
1695
|
+
};
|
|
1696
|
+
|
|
1697
|
+
var __getOwnPropSymbols$4 = Object.getOwnPropertySymbols;
|
|
1698
|
+
var __hasOwnProp$4 = Object.prototype.hasOwnProperty;
|
|
1699
|
+
var __propIsEnum$4 = Object.prototype.propertyIsEnumerable;
|
|
1700
|
+
var __objRest$2 = (source, exclude) => {
|
|
1701
|
+
var target = {};
|
|
1702
|
+
for (var prop in source)
|
|
1703
|
+
if (__hasOwnProp$4.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
1704
|
+
target[prop] = source[prop];
|
|
1705
|
+
if (source != null && __getOwnPropSymbols$4)
|
|
1706
|
+
for (var prop of __getOwnPropSymbols$4(source)) {
|
|
1707
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum$4.call(source, prop))
|
|
1708
|
+
target[prop] = source[prop];
|
|
1709
|
+
}
|
|
1710
|
+
return target;
|
|
1711
|
+
};
|
|
1712
|
+
const createTable = async (_a) => {
|
|
1713
|
+
var _b = _a, {
|
|
1714
|
+
ast
|
|
1715
|
+
} = _b, params = __objRest$2(_b, [
|
|
1716
|
+
"ast"
|
|
1717
|
+
]);
|
|
1718
|
+
const tablePath = params.tablePath(ast.name);
|
|
1719
|
+
const baseTablePath = getImportPath(tablePath, params.baseTablePath);
|
|
1720
|
+
const props = [`table = ${pqb.singleQuote(ast.name)};`];
|
|
1721
|
+
if (ast.noPrimaryKey === "ignore") {
|
|
1722
|
+
props.push("noPrimaryKey = true;");
|
|
1723
|
+
}
|
|
1724
|
+
props.push(
|
|
1725
|
+
"columns = this.setColumns((t) => ({",
|
|
1726
|
+
pqb.columnsShapeToCode(ast.shape, ast, "t"),
|
|
1727
|
+
"}));"
|
|
1728
|
+
);
|
|
1729
|
+
const code = [
|
|
1730
|
+
`import { ${params.baseTableName} } from '${baseTablePath}';
|
|
1731
|
+
`,
|
|
1732
|
+
`export class ${toPascalCase(ast.name)} extends ${params.baseTableName} {`,
|
|
1733
|
+
props,
|
|
1734
|
+
"}"
|
|
1735
|
+
];
|
|
1736
|
+
await fs__default["default"].writeFile(tablePath, pqb.codeToString(code, "", " "));
|
|
1737
|
+
};
|
|
1738
|
+
|
|
1739
|
+
var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
|
|
1740
|
+
var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
|
|
1741
|
+
var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
|
|
1742
|
+
var __objRest$1 = (source, exclude) => {
|
|
1743
|
+
var target = {};
|
|
1744
|
+
for (var prop in source)
|
|
1745
|
+
if (__hasOwnProp$3.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
1746
|
+
target[prop] = source[prop];
|
|
1747
|
+
if (source != null && __getOwnPropSymbols$3)
|
|
1748
|
+
for (var prop of __getOwnPropSymbols$3(source)) {
|
|
1749
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum$3.call(source, prop))
|
|
1750
|
+
target[prop] = source[prop];
|
|
1751
|
+
}
|
|
1752
|
+
return target;
|
|
1753
|
+
};
|
|
1754
|
+
const changeTable = async (_a) => {
|
|
1755
|
+
var _b = _a, {
|
|
1756
|
+
ast
|
|
1757
|
+
} = _b, params = __objRest$1(_b, [
|
|
1758
|
+
"ast"
|
|
1759
|
+
]);
|
|
1760
|
+
const tablePath = params.tablePath(ast.name);
|
|
1761
|
+
const content = await fs__default["default"].readFile(tablePath, "utf-8").catch(() => void 0);
|
|
1762
|
+
if (!content)
|
|
1763
|
+
return;
|
|
1764
|
+
const changes = new FileChanges(content);
|
|
1765
|
+
const statements = ts.getStatements(content);
|
|
1766
|
+
const className = toPascalCase(ast.name);
|
|
1767
|
+
for (const { t, object } of iterateColumnsShapes(statements, className)) {
|
|
1768
|
+
const context = makeChangeContext(changes, ast, content, object, t);
|
|
1769
|
+
prependSpaces(context);
|
|
1770
|
+
applySchemaChanges(context);
|
|
1771
|
+
appendTrailingComma(context);
|
|
1772
|
+
addColumns(context);
|
|
1773
|
+
addTableData(context);
|
|
1774
|
+
}
|
|
1775
|
+
await fs__default["default"].writeFile(tablePath, changes.apply());
|
|
1776
|
+
};
|
|
1777
|
+
function* iterateColumnsShapes(statements, className) {
|
|
1778
|
+
var _a, _b;
|
|
1779
|
+
for (const node of ts.class.iterate(statements)) {
|
|
1780
|
+
if (((_a = node.name) == null ? void 0 : _a.escapedText) !== className)
|
|
1781
|
+
continue;
|
|
1782
|
+
for (const member of node.members) {
|
|
1783
|
+
const name = ts.prop.getName(member);
|
|
1784
|
+
const { initializer: call } = member;
|
|
1785
|
+
if (name !== "columns" || !call || !ts.is.call(call))
|
|
1786
|
+
continue;
|
|
1787
|
+
const { expression } = call;
|
|
1788
|
+
if (!ts.is.propertyAccess(expression) || !ts.is.this(expression.expression) || expression.name.escapedText !== "setColumns")
|
|
1789
|
+
continue;
|
|
1790
|
+
const [arg] = call.arguments;
|
|
1791
|
+
if (!ts.is.arrowFunction(arg))
|
|
1792
|
+
continue;
|
|
1793
|
+
const { parameters, body } = arg;
|
|
1794
|
+
const param = (_b = parameters[0]) == null ? void 0 : _b.name;
|
|
1795
|
+
if (!ts.is.identifier(param) || !ts.is.parenthesizedExpression(body))
|
|
1796
|
+
continue;
|
|
1797
|
+
const { expression: object } = body;
|
|
1798
|
+
if (!ts.is.objectLiteral(object))
|
|
1799
|
+
continue;
|
|
1800
|
+
yield { t: param.escapedText.toString(), object };
|
|
1801
|
+
}
|
|
1802
|
+
}
|
|
1803
|
+
}
|
|
1804
|
+
const makeChangeContext = (changes, ast, content, object, t) => {
|
|
1805
|
+
const add = {};
|
|
1806
|
+
const drop = {};
|
|
1807
|
+
const change = {};
|
|
1808
|
+
const { properties: props } = object;
|
|
1809
|
+
const existingColumns = getExistingColumns(props);
|
|
1810
|
+
for (const key in ast.shape) {
|
|
1811
|
+
const item = ast.shape[key];
|
|
1812
|
+
if (item.type === "add" && !existingColumns[key]) {
|
|
1813
|
+
add[key] = item.item;
|
|
1814
|
+
}
|
|
1815
|
+
if (!existingColumns[key])
|
|
1816
|
+
continue;
|
|
1817
|
+
if (item.type === "drop" && existingColumns[key]) {
|
|
1818
|
+
drop[key] = true;
|
|
1819
|
+
} else if (item.type === "change" && existingColumns[key]) {
|
|
1820
|
+
change[key] = item;
|
|
1821
|
+
}
|
|
1822
|
+
}
|
|
1823
|
+
const spaces = ts.spaces.getAtLine(content, object.end);
|
|
1824
|
+
const shape = { add, drop, change };
|
|
1825
|
+
return {
|
|
1826
|
+
changes,
|
|
1827
|
+
props,
|
|
1828
|
+
shape,
|
|
1829
|
+
spaces,
|
|
1830
|
+
t,
|
|
1831
|
+
object,
|
|
1832
|
+
add: ast.add,
|
|
1833
|
+
drop: ast.drop
|
|
1834
|
+
};
|
|
1835
|
+
};
|
|
1836
|
+
const getExistingColumns = (props) => {
|
|
1837
|
+
const existingColumns = {};
|
|
1838
|
+
props.map((prop) => ts.is.propertyAssignment(prop) && ts.prop.getName(prop)).filter((name) => !!name).forEach((name) => existingColumns[name] = true);
|
|
1839
|
+
for (const prop of props) {
|
|
1840
|
+
if (!ts.is.propertyAssignment(prop))
|
|
1841
|
+
continue;
|
|
1842
|
+
const name = ts.prop.getName(prop);
|
|
1843
|
+
if (name)
|
|
1844
|
+
existingColumns[name] = true;
|
|
1845
|
+
}
|
|
1846
|
+
return existingColumns;
|
|
1847
|
+
};
|
|
1848
|
+
const prependSpaces = ({
|
|
1849
|
+
props,
|
|
1850
|
+
shape: { add },
|
|
1851
|
+
changes,
|
|
1852
|
+
spaces
|
|
1853
|
+
}) => {
|
|
1854
|
+
if (Object.keys(add).length && props.pos === props.end) {
|
|
1855
|
+
changes.add(props.pos, `
|
|
1856
|
+
${spaces}`);
|
|
1857
|
+
}
|
|
1858
|
+
};
|
|
1859
|
+
const applySchemaChanges = (context) => {
|
|
1860
|
+
const {
|
|
1861
|
+
props,
|
|
1862
|
+
shape: { drop: dropColumns, change: changeColumns },
|
|
1863
|
+
add,
|
|
1864
|
+
drop
|
|
1865
|
+
} = context;
|
|
1866
|
+
props.forEach((prop, i) => {
|
|
1867
|
+
if (ts.is.spreadAssignment(prop)) {
|
|
1868
|
+
const call = prop.expression;
|
|
1869
|
+
if (!ts.is.call(call))
|
|
1870
|
+
return;
|
|
1871
|
+
const access = call.expression;
|
|
1872
|
+
if (!ts.is.propertyAccess(access))
|
|
1873
|
+
return;
|
|
1874
|
+
const name = access.name.escapedText.toString();
|
|
1875
|
+
if (name === "primaryKey") {
|
|
1876
|
+
if (drop.primaryKey || add.primaryKey) {
|
|
1877
|
+
removeProp(context, prop, i);
|
|
1878
|
+
}
|
|
1879
|
+
} else if (name === "index") {
|
|
1880
|
+
dropMatchingIndexes(context, prop, i, call, drop.indexes);
|
|
1881
|
+
} else if (name === "foreignKey") {
|
|
1882
|
+
dropMatchingForeignKey(context, prop, i, call, drop.foreignKeys);
|
|
1883
|
+
}
|
|
1884
|
+
} else if (ts.is.propertyAssignment(prop)) {
|
|
1885
|
+
const name = ts.prop.getName(prop);
|
|
1886
|
+
if (!name)
|
|
1887
|
+
return;
|
|
1888
|
+
if (dropColumns[name]) {
|
|
1889
|
+
removeProp(context, prop, i);
|
|
1890
|
+
} else {
|
|
1891
|
+
const changeItem = changeColumns[name];
|
|
1892
|
+
if (changeItem) {
|
|
1893
|
+
changeColumn(context, changeItem, prop);
|
|
1894
|
+
}
|
|
1895
|
+
}
|
|
1896
|
+
}
|
|
1897
|
+
});
|
|
1898
|
+
};
|
|
1899
|
+
const removeProp = ({ props, changes }, prop, i) => {
|
|
1900
|
+
var _a;
|
|
1901
|
+
const end = ((_a = props[i + 1]) == null ? void 0 : _a.pos) || props.end;
|
|
1902
|
+
changes.remove(prop.pos, end);
|
|
1903
|
+
};
|
|
1904
|
+
const changeColumn = ({ changes, t, spaces }, changeItem, prop) => {
|
|
1905
|
+
const { from, to } = changeItem;
|
|
1906
|
+
if (from.type !== to.type && to.column) {
|
|
1907
|
+
changes.replace(
|
|
1908
|
+
prop.initializer.pos,
|
|
1909
|
+
prop.end,
|
|
1910
|
+
` ${pqb.codeToString(to.column.toCode(t), spaces + " ", " ").trim()}`
|
|
1911
|
+
);
|
|
1912
|
+
return;
|
|
1913
|
+
}
|
|
1914
|
+
const items = [];
|
|
1915
|
+
let chain = prop.initializer;
|
|
1916
|
+
while (ts.is.call(chain) && ts.is.propertyAccess(chain.expression)) {
|
|
1917
|
+
items.push(chain);
|
|
1918
|
+
chain = chain.expression.expression;
|
|
1919
|
+
}
|
|
1920
|
+
const propsToChange = {};
|
|
1921
|
+
for (const key in from) {
|
|
1922
|
+
if (to[key] !== from[key]) {
|
|
1923
|
+
propsToChange[key] = true;
|
|
1924
|
+
}
|
|
1925
|
+
}
|
|
1926
|
+
for (const key in to) {
|
|
1927
|
+
if (to[key] !== from[key]) {
|
|
1928
|
+
propsToChange[key] = true;
|
|
1929
|
+
}
|
|
1930
|
+
}
|
|
1931
|
+
const changedProps = {};
|
|
1932
|
+
for (const item of items.reverse()) {
|
|
1933
|
+
if (!ts.is.propertyAccess(item.expression))
|
|
1934
|
+
continue;
|
|
1935
|
+
const { name } = item.expression;
|
|
1936
|
+
const key = name.escapedText.toString();
|
|
1937
|
+
if (!propsToChange[key])
|
|
1938
|
+
continue;
|
|
1939
|
+
const value = getColumnMethodArgs(to, key);
|
|
1940
|
+
if (value) {
|
|
1941
|
+
const code = [`${key}(`];
|
|
1942
|
+
pqb.addCode(code, value);
|
|
1943
|
+
pqb.addCode(code, ")");
|
|
1944
|
+
changes.replace(name.pos, item.end, pqb.codeToString(code, "", " ").trim());
|
|
1945
|
+
} else {
|
|
1946
|
+
changes.remove(item.expression.expression.end, item.end);
|
|
1947
|
+
}
|
|
1948
|
+
changedProps[key] = true;
|
|
1949
|
+
}
|
|
1950
|
+
let append = "";
|
|
1951
|
+
for (const key in propsToChange) {
|
|
1952
|
+
if (changedProps[key])
|
|
1953
|
+
continue;
|
|
1954
|
+
const value = getColumnMethodArgs(to, key);
|
|
1955
|
+
if (value !== void 0) {
|
|
1956
|
+
const code = [`.${key}(`];
|
|
1957
|
+
pqb.addCode(code, value);
|
|
1958
|
+
pqb.addCode(code, ")");
|
|
1959
|
+
append += pqb.codeToString(code, "", " ").trim();
|
|
1960
|
+
}
|
|
1961
|
+
}
|
|
1962
|
+
if (append) {
|
|
1963
|
+
changes.add(prop.end, append);
|
|
1964
|
+
}
|
|
1965
|
+
};
|
|
1966
|
+
const appendTrailingComma = ({ props, changes }) => {
|
|
1967
|
+
if (!props.hasTrailingComma) {
|
|
1968
|
+
const last = props[props.length - 1];
|
|
1969
|
+
if (last) {
|
|
1970
|
+
changes.add(last.end, ",");
|
|
1971
|
+
}
|
|
1972
|
+
}
|
|
1973
|
+
};
|
|
1974
|
+
const addColumns = ({
|
|
1975
|
+
shape: { add },
|
|
1976
|
+
changes,
|
|
1977
|
+
object,
|
|
1978
|
+
t,
|
|
1979
|
+
spaces
|
|
1980
|
+
}) => {
|
|
1981
|
+
const end = object.end - 1;
|
|
1982
|
+
for (const key in add) {
|
|
1983
|
+
const code = pqb.codeToString(add[key].toCode(t), spaces + " ", " ");
|
|
1984
|
+
changes.add(end, ` ${pqb.quoteObjectKey(key)}: ${code.trim()},
|
|
1985
|
+
${spaces}`);
|
|
1986
|
+
}
|
|
1987
|
+
};
|
|
1988
|
+
const addTableData = ({ add, changes, object, t, spaces }) => {
|
|
1989
|
+
const end = object.end - 1;
|
|
1990
|
+
if (add.primaryKey) {
|
|
1991
|
+
const code = pqb.codeToString(
|
|
1992
|
+
pqb.primaryKeyToCode(add.primaryKey, t),
|
|
1993
|
+
spaces,
|
|
1994
|
+
" "
|
|
1995
|
+
);
|
|
1996
|
+
changes.add(end, ` ${code.trim()}
|
|
1997
|
+
${spaces}`);
|
|
1998
|
+
}
|
|
1999
|
+
for (const item of add.indexes) {
|
|
2000
|
+
const code = pqb.codeToString(pqb.indexToCode(item, t), spaces + " ", " ");
|
|
2001
|
+
changes.add(end, ` ${code.trim()}
|
|
2002
|
+
${spaces}`);
|
|
2003
|
+
}
|
|
2004
|
+
for (const item of add.foreignKeys) {
|
|
2005
|
+
const code = pqb.codeToString(pqb.foreignKeyToCode(item, t), spaces + " ", " ");
|
|
2006
|
+
changes.add(end, ` ${code.trim()}
|
|
2007
|
+
${spaces}`);
|
|
2008
|
+
}
|
|
2009
|
+
};
|
|
2010
|
+
const getColumnMethodArgs = (to, key) => {
|
|
2011
|
+
const value = to[key];
|
|
2012
|
+
if (!value)
|
|
2013
|
+
return;
|
|
2014
|
+
if (key === "collate" || key === "compression") {
|
|
2015
|
+
return pqb.singleQuote(value);
|
|
2016
|
+
} else if (key === "default") {
|
|
2017
|
+
return pqb.columnDefaultArgumentToCode(value);
|
|
2018
|
+
} else if (key === "nullable" || key === "primaryKey") {
|
|
2019
|
+
return "";
|
|
2020
|
+
} else {
|
|
2021
|
+
return;
|
|
2022
|
+
}
|
|
2023
|
+
};
|
|
2024
|
+
const dropMatchingIndexes = (context, prop, i, call, items) => {
|
|
2025
|
+
if (!items.length)
|
|
2026
|
+
return;
|
|
2027
|
+
const [columnsNode, optionsNode] = call.arguments;
|
|
2028
|
+
const columns = [];
|
|
2029
|
+
if (ts.is.stringLiteral(columnsNode)) {
|
|
2030
|
+
columns.push({ column: columnsNode.text });
|
|
2031
|
+
} else if (ts.is.arrayLiteral(columnsNode)) {
|
|
2032
|
+
for (const node of columnsNode.elements) {
|
|
2033
|
+
if (ts.is.stringLiteral(node)) {
|
|
2034
|
+
columns.push({ column: node.text });
|
|
2035
|
+
} else if (ts.is.objectLiteral(node)) {
|
|
2036
|
+
const object = collectObjectFromCode(node);
|
|
2037
|
+
if (!object)
|
|
2038
|
+
return;
|
|
2039
|
+
columns.push(object);
|
|
2040
|
+
}
|
|
2041
|
+
}
|
|
2042
|
+
} else {
|
|
2043
|
+
return;
|
|
2044
|
+
}
|
|
2045
|
+
const options = ts.is.objectLiteral(optionsNode) && collectObjectFromCode(optionsNode) || {};
|
|
2046
|
+
for (const item of items) {
|
|
2047
|
+
if (deepCompare(columns, item.columns) && deepCompare(options, item.options)) {
|
|
2048
|
+
removeProp(context, prop, i);
|
|
2049
|
+
}
|
|
2050
|
+
}
|
|
2051
|
+
};
|
|
2052
|
+
const dropMatchingForeignKey = (context, prop, i, call, items) => {
|
|
2053
|
+
if (!items.length)
|
|
2054
|
+
return;
|
|
2055
|
+
const { arguments: args } = call;
|
|
2056
|
+
const columns = collectStringArrayFromCode(args[0]);
|
|
2057
|
+
if (!columns)
|
|
2058
|
+
return;
|
|
2059
|
+
const fnOrTableNode = args[1];
|
|
2060
|
+
let fnOrTable;
|
|
2061
|
+
if (ts.is.stringLiteral(fnOrTableNode)) {
|
|
2062
|
+
fnOrTable = fnOrTableNode.text;
|
|
2063
|
+
} else if (ts.is.arrowFunction(fnOrTableNode)) {
|
|
2064
|
+
fnOrTable = context.changes.content.slice(fnOrTableNode.pos, fnOrTableNode.end).replaceAll(/\s/g, "");
|
|
2065
|
+
} else {
|
|
2066
|
+
return;
|
|
2067
|
+
}
|
|
2068
|
+
const foreignColumns = collectStringArrayFromCode(args[2]);
|
|
2069
|
+
if (!foreignColumns)
|
|
2070
|
+
return;
|
|
2071
|
+
const options = ts.is.objectLiteral(args[3]) && collectObjectFromCode(args[3]) || {};
|
|
2072
|
+
for (const item of items) {
|
|
2073
|
+
const itemOptions = item.options;
|
|
2074
|
+
delete itemOptions.dropMode;
|
|
2075
|
+
if (deepCompare(columns, item.columns) && deepCompare(fnOrTable, item.fnOrTable.toString()) && deepCompare(foreignColumns, item.foreignColumns) && deepCompare(options, itemOptions)) {
|
|
2076
|
+
removeProp(context, prop, i);
|
|
2077
|
+
}
|
|
2078
|
+
}
|
|
2079
|
+
};
|
|
2080
|
+
const collectStringArrayFromCode = (node) => {
|
|
2081
|
+
if (!ts.is.arrayLiteral(node))
|
|
2082
|
+
return;
|
|
2083
|
+
const result = node.elements.filter(ts.is.stringLiteral).map((item) => item.text);
|
|
2084
|
+
return result.length === node.elements.length ? result : void 0;
|
|
2085
|
+
};
|
|
2086
|
+
const collectObjectFromCode = (node) => {
|
|
2087
|
+
const object = {};
|
|
2088
|
+
for (const prop of node.properties) {
|
|
2089
|
+
if (!ts.is.propertyAssignment(prop))
|
|
2090
|
+
return;
|
|
2091
|
+
const name = ts.prop.getName(prop);
|
|
2092
|
+
if (!name)
|
|
2093
|
+
return;
|
|
2094
|
+
const init = prop.initializer;
|
|
2095
|
+
if (ts.is.stringLiteral(init)) {
|
|
2096
|
+
object[name] = init.text;
|
|
2097
|
+
} else if (ts.is.numericLiteral(init)) {
|
|
2098
|
+
object[name] = parseFloat(init.text);
|
|
2099
|
+
} else {
|
|
2100
|
+
return;
|
|
2101
|
+
}
|
|
2102
|
+
}
|
|
2103
|
+
return object;
|
|
2104
|
+
};
|
|
2105
|
+
const deepCompare = (a, b) => {
|
|
2106
|
+
if (typeof a !== typeof b)
|
|
2107
|
+
return false;
|
|
2108
|
+
if (a === b)
|
|
2109
|
+
return true;
|
|
2110
|
+
if (typeof a === "object") {
|
|
2111
|
+
if (a === null)
|
|
2112
|
+
return b === null;
|
|
2113
|
+
if (Array.isArray(a)) {
|
|
2114
|
+
if (!Array.isArray(b) || a.length !== b.length)
|
|
2115
|
+
return false;
|
|
2116
|
+
return a.every((item, i) => deepCompare(item, b[i]));
|
|
2117
|
+
}
|
|
2118
|
+
for (const key in a) {
|
|
2119
|
+
if (!deepCompare(
|
|
2120
|
+
a[key],
|
|
2121
|
+
b[key]
|
|
2122
|
+
))
|
|
2123
|
+
return false;
|
|
2124
|
+
}
|
|
2125
|
+
for (const key in b) {
|
|
2126
|
+
if (!(key in a))
|
|
2127
|
+
return false;
|
|
2128
|
+
}
|
|
2129
|
+
}
|
|
2130
|
+
return true;
|
|
2131
|
+
};
|
|
2132
|
+
|
|
2133
|
+
var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
|
|
2134
|
+
var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
|
|
2135
|
+
var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
|
|
2136
|
+
var __objRest = (source, exclude) => {
|
|
2137
|
+
var target = {};
|
|
2138
|
+
for (var prop in source)
|
|
2139
|
+
if (__hasOwnProp$2.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
2140
|
+
target[prop] = source[prop];
|
|
2141
|
+
if (source != null && __getOwnPropSymbols$2)
|
|
2142
|
+
for (var prop of __getOwnPropSymbols$2(source)) {
|
|
2143
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum$2.call(source, prop))
|
|
2144
|
+
target[prop] = source[prop];
|
|
2145
|
+
}
|
|
2146
|
+
return target;
|
|
2147
|
+
};
|
|
2148
|
+
const renameTable = async (_a) => {
|
|
2149
|
+
var _b = _a, {
|
|
2150
|
+
ast
|
|
2151
|
+
} = _b, params = __objRest(_b, [
|
|
2152
|
+
"ast"
|
|
2153
|
+
]);
|
|
2154
|
+
var _a2;
|
|
2155
|
+
const tablePath = params.tablePath(ast.from);
|
|
2156
|
+
const content = await fs__default["default"].readFile(tablePath, "utf-8").catch(() => void 0);
|
|
2157
|
+
if (!content)
|
|
2158
|
+
return;
|
|
2159
|
+
const changes = new FileChanges(content);
|
|
2160
|
+
const statements = ts.getStatements(content);
|
|
2161
|
+
const className = toPascalCase(ast.from);
|
|
2162
|
+
for (const node of ts.class.iterate(statements)) {
|
|
2163
|
+
if (((_a2 = node.name) == null ? void 0 : _a2.escapedText) !== className)
|
|
2164
|
+
continue;
|
|
2165
|
+
for (const member of node.members) {
|
|
2166
|
+
const name = ts.prop.getName(member);
|
|
2167
|
+
if (name !== "table")
|
|
2168
|
+
continue;
|
|
2169
|
+
const { initializer: value } = member;
|
|
2170
|
+
if (!value)
|
|
2171
|
+
continue;
|
|
2172
|
+
changes.replace(value.pos, value.end, ` ${pqb.singleQuote(ast.to)}`);
|
|
2173
|
+
}
|
|
2174
|
+
}
|
|
2175
|
+
await fs__default["default"].writeFile(params.tablePath(ast.to), changes.apply());
|
|
2176
|
+
};
|
|
2177
|
+
|
|
2178
|
+
var __defProp$1 = Object.defineProperty;
|
|
2179
|
+
var __defProps$1 = Object.defineProperties;
|
|
2180
|
+
var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
|
|
2181
|
+
var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
|
|
2182
|
+
var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
|
|
2183
|
+
var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
|
|
2184
|
+
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
2185
|
+
var __spreadValues$1 = (a, b) => {
|
|
2186
|
+
for (var prop in b || (b = {}))
|
|
2187
|
+
if (__hasOwnProp$1.call(b, prop))
|
|
2188
|
+
__defNormalProp$1(a, prop, b[prop]);
|
|
2189
|
+
if (__getOwnPropSymbols$1)
|
|
2190
|
+
for (var prop of __getOwnPropSymbols$1(b)) {
|
|
2191
|
+
if (__propIsEnum$1.call(b, prop))
|
|
2192
|
+
__defNormalProp$1(a, prop, b[prop]);
|
|
2193
|
+
}
|
|
2194
|
+
return a;
|
|
2195
|
+
};
|
|
2196
|
+
var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
|
|
2197
|
+
const updateTableFile = async (params) => {
|
|
2198
|
+
const { ast } = params;
|
|
2199
|
+
if (ast.type === "table" && ast.action === "create") {
|
|
2200
|
+
await createTable(__spreadProps$1(__spreadValues$1({}, params), { ast }));
|
|
2201
|
+
} else if (ast.type === "changeTable") {
|
|
2202
|
+
await changeTable(__spreadProps$1(__spreadValues$1({}, params), { ast }));
|
|
2203
|
+
} else if (ast.type === "renameTable") {
|
|
2204
|
+
await renameTable(__spreadProps$1(__spreadValues$1({}, params), { ast }));
|
|
2205
|
+
}
|
|
2206
|
+
};
|
|
2207
|
+
|
|
2208
|
+
var __defProp = Object.defineProperty;
|
|
2209
|
+
var __defProps = Object.defineProperties;
|
|
2210
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
2211
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
2212
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
2213
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
2214
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
2215
|
+
var __spreadValues = (a, b) => {
|
|
2216
|
+
for (var prop in b || (b = {}))
|
|
2217
|
+
if (__hasOwnProp.call(b, prop))
|
|
2218
|
+
__defNormalProp(a, prop, b[prop]);
|
|
2219
|
+
if (__getOwnPropSymbols)
|
|
2220
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
2221
|
+
if (__propIsEnum.call(b, prop))
|
|
2222
|
+
__defNormalProp(a, prop, b[prop]);
|
|
2223
|
+
}
|
|
2224
|
+
return a;
|
|
2225
|
+
};
|
|
2226
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
2227
|
+
class AppCodeUpdaterError extends Error {
|
|
2228
|
+
}
|
|
2229
|
+
const appCodeUpdater = (config) => {
|
|
2230
|
+
const params = __spreadProps(__spreadValues({}, config), {
|
|
2231
|
+
tablePath: (name) => path__namespace.resolve(config.tablePath(name)),
|
|
2232
|
+
mainFilePath: path__namespace.resolve(config.mainFilePath)
|
|
2233
|
+
});
|
|
2234
|
+
return async (ast) => {
|
|
2235
|
+
await Promise.all([
|
|
2236
|
+
updateMainFile(params.mainFilePath, params.tablePath, ast),
|
|
2237
|
+
updateTableFile(__spreadProps(__spreadValues({}, params), { ast }))
|
|
2238
|
+
]);
|
|
2239
|
+
};
|
|
2240
|
+
};
|
|
2241
|
+
|
|
2242
|
+
exports.AppCodeUpdaterError = AppCodeUpdaterError;
|
|
2243
|
+
exports.appCodeUpdater = appCodeUpdater;
|
|
1380
2244
|
exports.createBaseTable = createBaseTable;
|
|
1381
2245
|
exports.createRepo = createRepo;
|
|
1382
2246
|
exports.orchidORM = orchidORM;
|