orchid-orm 1.19.0 → 1.20.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +10 -9
- package/dist/index.js +210 -117
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +215 -122
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -6
package/dist/index.js
CHANGED
|
@@ -5,25 +5,18 @@ var orchidCore = require('orchid-core');
|
|
|
5
5
|
var node_async_hooks = require('node:async_hooks');
|
|
6
6
|
|
|
7
7
|
function createBaseTable({
|
|
8
|
+
schemaConfig = pqb.defaultSchemaConfig,
|
|
8
9
|
columnTypes: columnTypesArg,
|
|
9
10
|
snakeCase,
|
|
10
11
|
filePath: filePathArg,
|
|
11
12
|
nowSQL,
|
|
12
13
|
exportAs = "BaseTable",
|
|
13
|
-
language
|
|
14
|
-
schemaProvider: schemaProviderArg
|
|
14
|
+
language
|
|
15
15
|
} = {}) {
|
|
16
16
|
var _a;
|
|
17
|
-
const columnTypes = typeof columnTypesArg === "function" ? columnTypesArg(
|
|
18
|
-
pqb.columnTypes
|
|
19
|
-
) : columnTypesArg || pqb.columnTypes;
|
|
17
|
+
const columnTypes = typeof columnTypesArg === "function" ? columnTypesArg(pqb.makeColumnTypes(schemaConfig)) : columnTypesArg || pqb.makeColumnTypes(pqb.defaultSchemaConfig);
|
|
20
18
|
const filePathOrStack = filePathArg || orchidCore.getStackTrace();
|
|
21
19
|
let filePath;
|
|
22
|
-
function schemaProvider() {
|
|
23
|
-
const schema = schemaProviderArg.call(this);
|
|
24
|
-
this.schema = () => schema;
|
|
25
|
-
return schema;
|
|
26
|
-
}
|
|
27
20
|
const base = (_a = class {
|
|
28
21
|
constructor() {
|
|
29
22
|
this.snakeCase = snakeCase;
|
|
@@ -31,6 +24,21 @@ function createBaseTable({
|
|
|
31
24
|
this.q = {};
|
|
32
25
|
this.language = language;
|
|
33
26
|
}
|
|
27
|
+
static inputSchema() {
|
|
28
|
+
return this._inputSchema === void 0 ? this._inputSchema = schemaConfig.inputSchema.call(this) : this._inputSchema;
|
|
29
|
+
}
|
|
30
|
+
static outputSchema() {
|
|
31
|
+
return this._outputSchema === void 0 ? this._outputSchema = schemaConfig.outputSchema.call(this) : this._outputSchema;
|
|
32
|
+
}
|
|
33
|
+
static querySchema() {
|
|
34
|
+
return this._querySchema === void 0 ? this._querySchema = schemaConfig.querySchema.call(this) : this._querySchema;
|
|
35
|
+
}
|
|
36
|
+
static updateSchema() {
|
|
37
|
+
return this._updateSchema === void 0 ? this._updateSchema = schemaConfig.updateSchema.call(this) : this._updateSchema;
|
|
38
|
+
}
|
|
39
|
+
static pkeySchema() {
|
|
40
|
+
return this._pkeySchema === void 0 ? this._pkeySchema = schemaConfig.pkeySchema.call(this) : this._pkeySchema;
|
|
41
|
+
}
|
|
34
42
|
static getFilePath() {
|
|
35
43
|
if (filePath)
|
|
36
44
|
return filePath;
|
|
@@ -77,7 +85,7 @@ function createBaseTable({
|
|
|
77
85
|
}
|
|
78
86
|
}
|
|
79
87
|
}
|
|
80
|
-
return shape;
|
|
88
|
+
return this.constructor.prototype.columns = shape;
|
|
81
89
|
}
|
|
82
90
|
setComputed(computed) {
|
|
83
91
|
return computed;
|
|
@@ -113,7 +121,7 @@ function createBaseTable({
|
|
|
113
121
|
options
|
|
114
122
|
};
|
|
115
123
|
}
|
|
116
|
-
}, _a.nowSQL = nowSQL, _a.exportAs = exportAs, _a
|
|
124
|
+
}, _a.nowSQL = nowSQL, _a.exportAs = exportAs, _a);
|
|
117
125
|
orchidCore.applyMixins(base, [pqb.QueryHooks]);
|
|
118
126
|
base.prototype.types = columnTypes;
|
|
119
127
|
return base;
|
|
@@ -142,7 +150,8 @@ const hasRelationHandleCreate = (q, ctx, item, rowIndex, key, primaryKeys, neste
|
|
|
142
150
|
q.q.wrapInTransaction = true;
|
|
143
151
|
const relationData = [values];
|
|
144
152
|
store.hasRelation[key] = relationData;
|
|
145
|
-
|
|
153
|
+
pqb._queryHookAfterCreate(
|
|
154
|
+
q,
|
|
146
155
|
primaryKeys,
|
|
147
156
|
(rows, q2) => nestedInsert(
|
|
148
157
|
q2,
|
|
@@ -159,7 +168,7 @@ const hasRelationHandleUpdate = (q, set, key, primaryKeys, nestedUpdate) => {
|
|
|
159
168
|
return;
|
|
160
169
|
selectIfNotSelected(q, primaryKeys);
|
|
161
170
|
q.q.wrapInTransaction = true;
|
|
162
|
-
|
|
171
|
+
pqb._queryHookAfterUpdate(q, q.primaryKeys, (rows, q2) => {
|
|
163
172
|
return nestedUpdate(
|
|
164
173
|
q2,
|
|
165
174
|
rows,
|
|
@@ -226,8 +235,8 @@ const joinQueryChainingHOF = (reverseJoin, joinQuery) => (joiningQuery, baseQuer
|
|
|
226
235
|
};
|
|
227
236
|
|
|
228
237
|
class BelongsToVirtualColumn extends pqb.VirtualColumn {
|
|
229
|
-
constructor(key, state) {
|
|
230
|
-
super();
|
|
238
|
+
constructor(schema, key, state) {
|
|
239
|
+
super(schema);
|
|
231
240
|
this.key = key;
|
|
232
241
|
this.state = state;
|
|
233
242
|
this.nestedInsert = nestedInsert$3(this.state);
|
|
@@ -304,7 +313,11 @@ const makeBelongsToMethod = (relation, relationName, query) => {
|
|
|
304
313
|
method(params) {
|
|
305
314
|
return query.where(makeWhere(params));
|
|
306
315
|
},
|
|
307
|
-
virtualColumn: new BelongsToVirtualColumn(
|
|
316
|
+
virtualColumn: new BelongsToVirtualColumn(
|
|
317
|
+
pqb.defaultSchemaConfig,
|
|
318
|
+
relationName,
|
|
319
|
+
state
|
|
320
|
+
),
|
|
308
321
|
joinQuery: joinQueryChainingHOF(
|
|
309
322
|
reverseJoin,
|
|
310
323
|
(joiningQuery, baseQuery) => join(baseQuery, joiningQuery, primaryKeys, foreignKeys)
|
|
@@ -347,7 +360,7 @@ const nestedInsert$3 = ({ query, primaryKeys }) => {
|
|
|
347
360
|
for (let i = 0, len = items.length; i < len; i++) {
|
|
348
361
|
items[i] = "create" in items[i] ? items[i].create : items[i].connectOrCreate.create;
|
|
349
362
|
}
|
|
350
|
-
created = await t.select(...primaryKeys)
|
|
363
|
+
created = await pqb._queryCreateMany(t.select(...primaryKeys), items);
|
|
351
364
|
} else {
|
|
352
365
|
created = orchidCore.emptyArray;
|
|
353
366
|
}
|
|
@@ -381,7 +394,7 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
381
394
|
throw new Error("`upsert` option is not allowed in a batch update");
|
|
382
395
|
}
|
|
383
396
|
let idsForDelete;
|
|
384
|
-
|
|
397
|
+
pqb._queryHookBeforeUpdate(q, async (q2) => {
|
|
385
398
|
if (params.disconnect) {
|
|
386
399
|
for (const key of foreignKeys) {
|
|
387
400
|
update[key] = null;
|
|
@@ -399,7 +412,9 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
399
412
|
}
|
|
400
413
|
}
|
|
401
414
|
if (loadPrimaryKeys) {
|
|
402
|
-
const record = await query.select(...loadPrimaryKeys)
|
|
415
|
+
const record = await pqb._queryFindBy(query.select(...loadPrimaryKeys), [
|
|
416
|
+
params.set
|
|
417
|
+
]);
|
|
403
418
|
for (let i = 0, len2 = loadPrimaryKeys.length; i < len2; i++) {
|
|
404
419
|
update[loadForeignKeys[i]] = record[loadPrimaryKeys[i]];
|
|
405
420
|
}
|
|
@@ -407,7 +422,7 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
407
422
|
} else if (params.create) {
|
|
408
423
|
const q3 = query.clone();
|
|
409
424
|
q3.q.select = primaryKeys;
|
|
410
|
-
const record = await
|
|
425
|
+
const record = await pqb._queryCreate(q3, params.create);
|
|
411
426
|
for (let i = 0; i < len; i++) {
|
|
412
427
|
update[foreignKeys[i]] = record[primaryKeys[i]];
|
|
413
428
|
}
|
|
@@ -415,7 +430,7 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
415
430
|
const selectQuery = q2.clone();
|
|
416
431
|
selectQuery.q.type = void 0;
|
|
417
432
|
selectQuery.q.distinct = orchidCore.emptyArray;
|
|
418
|
-
idsForDelete = await
|
|
433
|
+
idsForDelete = await pqb._queryRows(selectQuery);
|
|
419
434
|
for (const foreignKey of foreignKeys) {
|
|
420
435
|
update[foreignKey] = null;
|
|
421
436
|
}
|
|
@@ -439,17 +454,20 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
439
454
|
obj[primaryKeys[i]] = id;
|
|
440
455
|
}
|
|
441
456
|
if (obj) {
|
|
442
|
-
await
|
|
457
|
+
await pqb._queryUpdate(
|
|
458
|
+
query.findBy(obj),
|
|
459
|
+
upsert.update
|
|
460
|
+
);
|
|
443
461
|
} else {
|
|
444
462
|
const data = typeof upsert.create === "function" ? upsert.create() : upsert.create;
|
|
445
|
-
const result = await query.select(...primaryKeys)
|
|
463
|
+
const result = await pqb._queryCreate(query.select(...primaryKeys), data);
|
|
446
464
|
for (let i = 0; i < len; i++) {
|
|
447
465
|
((_a2 = state.updateData) != null ? _a2 : state.updateData = {})[foreignKeys[i]] = result[primaryKeys[i]];
|
|
448
466
|
}
|
|
449
467
|
}
|
|
450
468
|
});
|
|
451
469
|
} else if (params.delete || params.update) {
|
|
452
|
-
|
|
470
|
+
pqb._queryHookAfterUpdate(q, [], async (data) => {
|
|
453
471
|
let ids;
|
|
454
472
|
if (params.delete) {
|
|
455
473
|
ids = idsForDelete;
|
|
@@ -477,11 +495,9 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
477
495
|
ids
|
|
478
496
|
);
|
|
479
497
|
if (params.delete) {
|
|
480
|
-
await
|
|
498
|
+
await pqb._queryDelete(t);
|
|
481
499
|
} else {
|
|
482
|
-
await t.
|
|
483
|
-
params.update
|
|
484
|
-
);
|
|
500
|
+
await pqb._queryUpdate(t, params.update);
|
|
485
501
|
}
|
|
486
502
|
});
|
|
487
503
|
}
|
|
@@ -505,8 +521,8 @@ var __spreadValues$5 = (a, b) => {
|
|
|
505
521
|
return a;
|
|
506
522
|
};
|
|
507
523
|
class HasOneVirtualColumn extends pqb.VirtualColumn {
|
|
508
|
-
constructor(key, state) {
|
|
509
|
-
super();
|
|
524
|
+
constructor(schema, key, state) {
|
|
525
|
+
super(schema);
|
|
510
526
|
this.key = key;
|
|
511
527
|
this.state = state;
|
|
512
528
|
this.nestedInsert = nestedInsert$2(state);
|
|
@@ -601,9 +617,13 @@ const makeHasOneMethod = (table, relation, relationName, query) => {
|
|
|
601
617
|
for (let i = 0; i < len; i++) {
|
|
602
618
|
values[foreignKeys[i]] = params[primaryKeys[i]];
|
|
603
619
|
}
|
|
604
|
-
return query.where(values)
|
|
620
|
+
return pqb._queryDefaults(query.where(values), values);
|
|
605
621
|
},
|
|
606
|
-
virtualColumn: new HasOneVirtualColumn(
|
|
622
|
+
virtualColumn: new HasOneVirtualColumn(
|
|
623
|
+
pqb.defaultSchemaConfig,
|
|
624
|
+
relationName,
|
|
625
|
+
state
|
|
626
|
+
),
|
|
607
627
|
joinQuery: joinQueryChainingHOF(
|
|
608
628
|
reverseJoin,
|
|
609
629
|
(joiningQuery, baseQuery) => joinHasRelation(baseQuery, joiningQuery, primaryKeys, foreignKeys, len)
|
|
@@ -637,9 +657,15 @@ const nestedInsert$2 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
637
657
|
for (let i2 = 0; i2 < len; i2++) {
|
|
638
658
|
data2[foreignKeys[i2]] = selfData[primaryKeys[i2]];
|
|
639
659
|
}
|
|
640
|
-
items[i] = "connect" in item ?
|
|
641
|
-
item.
|
|
642
|
-
|
|
660
|
+
items[i] = "connect" in item ? pqb._queryUpdateOrThrow(
|
|
661
|
+
t.where(item.connect),
|
|
662
|
+
data2
|
|
663
|
+
) : pqb._queryUpdate(
|
|
664
|
+
t.where(
|
|
665
|
+
item.connectOrCreate.where
|
|
666
|
+
),
|
|
667
|
+
data2
|
|
668
|
+
);
|
|
643
669
|
}
|
|
644
670
|
connected = await Promise.all(items);
|
|
645
671
|
} else {
|
|
@@ -685,7 +711,8 @@ const nestedUpdate$2 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
685
711
|
ids
|
|
686
712
|
);
|
|
687
713
|
if (params.create || params.disconnect || params.set) {
|
|
688
|
-
await
|
|
714
|
+
await pqb._queryUpdate(
|
|
715
|
+
currentRelationsQuery,
|
|
689
716
|
setNulls
|
|
690
717
|
);
|
|
691
718
|
const record = data[0];
|
|
@@ -701,16 +728,25 @@ const nestedUpdate$2 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
701
728
|
for (let i = 0; i < len; i++) {
|
|
702
729
|
obj[foreignKeys[i]] = record[primaryKeys[i]];
|
|
703
730
|
}
|
|
704
|
-
await
|
|
731
|
+
await pqb._queryUpdate(
|
|
732
|
+
pqb._queryWhere(t, [params.set]),
|
|
733
|
+
obj
|
|
734
|
+
);
|
|
705
735
|
}
|
|
706
736
|
} else if (params.update) {
|
|
707
|
-
await
|
|
737
|
+
await pqb._queryUpdate(
|
|
738
|
+
currentRelationsQuery,
|
|
739
|
+
params.update
|
|
740
|
+
);
|
|
708
741
|
} else if (params.delete) {
|
|
709
|
-
await
|
|
742
|
+
await pqb._queryDelete(currentRelationsQuery);
|
|
710
743
|
} else if (params.upsert) {
|
|
711
744
|
const { update, create } = params.upsert;
|
|
712
745
|
currentRelationsQuery.q.select = foreignKeys;
|
|
713
|
-
const updatedIds = await
|
|
746
|
+
const updatedIds = await pqb._queryUpdate(
|
|
747
|
+
pqb._queryRows(currentRelationsQuery),
|
|
748
|
+
update
|
|
749
|
+
);
|
|
714
750
|
if (updatedIds.length < ids.length) {
|
|
715
751
|
const data2 = typeof create === "function" ? create() : create;
|
|
716
752
|
await t.createMany(
|
|
@@ -749,8 +785,8 @@ var __spreadValues$4 = (a, b) => {
|
|
|
749
785
|
return a;
|
|
750
786
|
};
|
|
751
787
|
class HasManyVirtualColumn extends pqb.VirtualColumn {
|
|
752
|
-
constructor(key, state) {
|
|
753
|
-
super();
|
|
788
|
+
constructor(schema, key, state) {
|
|
789
|
+
super(schema);
|
|
754
790
|
this.key = key;
|
|
755
791
|
this.state = state;
|
|
756
792
|
this.nestedInsert = nestedInsert$1(state);
|
|
@@ -849,9 +885,13 @@ const makeHasManyMethod = (table, relation, relationName, query) => {
|
|
|
849
885
|
for (let i = 0; i < len; i++) {
|
|
850
886
|
values[foreignKeys[i]] = params[primaryKeys[i]];
|
|
851
887
|
}
|
|
852
|
-
return query.where(values)
|
|
888
|
+
return pqb._queryDefaults(query.where(values), values);
|
|
853
889
|
},
|
|
854
|
-
virtualColumn: new HasManyVirtualColumn(
|
|
890
|
+
virtualColumn: new HasManyVirtualColumn(
|
|
891
|
+
pqb.defaultSchemaConfig,
|
|
892
|
+
relationName,
|
|
893
|
+
state
|
|
894
|
+
),
|
|
855
895
|
joinQuery: joinQueryChainingHOF(
|
|
856
896
|
reverseJoin,
|
|
857
897
|
(joiningQuery, baseQuery) => joinHasRelation(baseQuery, joiningQuery, primaryKeys, foreignKeys, len)
|
|
@@ -894,7 +934,10 @@ const nestedInsert$1 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
894
934
|
for (let i2 = 0; i2 < len2; i2++) {
|
|
895
935
|
obj[foreignKeys[i2]] = selfData[primaryKeys[i2]];
|
|
896
936
|
}
|
|
897
|
-
items[i] =
|
|
937
|
+
items[i] = pqb._queryUpdateOrThrow(
|
|
938
|
+
t.orWhere(...connect),
|
|
939
|
+
obj
|
|
940
|
+
);
|
|
898
941
|
}
|
|
899
942
|
await Promise.all(items);
|
|
900
943
|
}
|
|
@@ -915,7 +958,10 @@ const nestedInsert$1 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
915
958
|
obj[foreignKeys[i2]] = selfData[primaryKeys[i2]];
|
|
916
959
|
}
|
|
917
960
|
queries.push(
|
|
918
|
-
|
|
961
|
+
pqb._queryUpdate(
|
|
962
|
+
t.where(item.where),
|
|
963
|
+
obj
|
|
964
|
+
)
|
|
919
965
|
);
|
|
920
966
|
}
|
|
921
967
|
}
|
|
@@ -960,7 +1006,7 @@ const nestedInsert$1 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
960
1006
|
}
|
|
961
1007
|
}
|
|
962
1008
|
}
|
|
963
|
-
await
|
|
1009
|
+
await pqb._queryCreateMany(t, records);
|
|
964
1010
|
}
|
|
965
1011
|
};
|
|
966
1012
|
};
|
|
@@ -984,24 +1030,30 @@ const nestedUpdate$1 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
984
1030
|
for (const foreignKey of foreignKeys) {
|
|
985
1031
|
obj[foreignKey] = null;
|
|
986
1032
|
}
|
|
987
|
-
await
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
1033
|
+
await pqb._queryUpdate(
|
|
1034
|
+
getWhereForNestedUpdate(
|
|
1035
|
+
t,
|
|
1036
|
+
data,
|
|
1037
|
+
params.disconnect,
|
|
1038
|
+
primaryKeys,
|
|
1039
|
+
foreignKeys
|
|
1040
|
+
),
|
|
1041
|
+
obj
|
|
1042
|
+
);
|
|
994
1043
|
if (params.set) {
|
|
995
1044
|
delete t.q[pqb.toSQLCacheKey];
|
|
996
1045
|
const obj2 = {};
|
|
997
1046
|
for (let i = 0; i < len; i++) {
|
|
998
1047
|
obj2[foreignKeys[i]] = data[0][primaryKeys[i]];
|
|
999
1048
|
}
|
|
1000
|
-
await
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1049
|
+
await pqb._queryUpdate(
|
|
1050
|
+
t.where(
|
|
1051
|
+
Array.isArray(params.set) ? {
|
|
1052
|
+
OR: params.set
|
|
1053
|
+
} : params.set
|
|
1054
|
+
),
|
|
1055
|
+
obj2
|
|
1056
|
+
);
|
|
1005
1057
|
}
|
|
1006
1058
|
}
|
|
1007
1059
|
if (params.delete || params.update) {
|
|
@@ -1014,9 +1066,9 @@ const nestedUpdate$1 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
1014
1066
|
foreignKeys
|
|
1015
1067
|
);
|
|
1016
1068
|
if (params.delete) {
|
|
1017
|
-
await
|
|
1069
|
+
await pqb._queryDelete(q);
|
|
1018
1070
|
} else if (params.update) {
|
|
1019
|
-
await
|
|
1071
|
+
await pqb._queryUpdate(q, params.update.data);
|
|
1020
1072
|
}
|
|
1021
1073
|
}
|
|
1022
1074
|
};
|
|
@@ -1042,8 +1094,8 @@ var __spreadValues$3 = (a, b) => {
|
|
|
1042
1094
|
};
|
|
1043
1095
|
var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
|
|
1044
1096
|
class HasAndBelongsToManyVirtualColumn extends pqb.VirtualColumn {
|
|
1045
|
-
constructor(key, state) {
|
|
1046
|
-
super();
|
|
1097
|
+
constructor(schema, key, state) {
|
|
1098
|
+
super(schema);
|
|
1047
1099
|
this.key = key;
|
|
1048
1100
|
this.state = state;
|
|
1049
1101
|
this.nestedInsert = nestedInsert(state);
|
|
@@ -1112,7 +1164,9 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
1112
1164
|
baseQuery.table = joinTable;
|
|
1113
1165
|
const shape = {};
|
|
1114
1166
|
for (let i = 0; i < len; i++) {
|
|
1115
|
-
shape[foreignKeys[i]] = removeColumnName(
|
|
1167
|
+
shape[foreignKeys[i]] = removeColumnName(
|
|
1168
|
+
table.shape[primaryKeys[i]]
|
|
1169
|
+
);
|
|
1116
1170
|
}
|
|
1117
1171
|
for (let i = 0; i < throughLen; i++) {
|
|
1118
1172
|
shape[throughForeignKeys[i]] = removeColumnName(
|
|
@@ -1138,13 +1192,13 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
1138
1192
|
const joinQuery = (joiningQuery, tableAs, foreignAs) => {
|
|
1139
1193
|
return joiningQuery.whereExists(subQuery, (q) => {
|
|
1140
1194
|
for (let i = 0; i < throughLen; i++) {
|
|
1141
|
-
|
|
1195
|
+
pqb._queryJoinOn(q, [
|
|
1142
1196
|
throughForeignKeysFull[i],
|
|
1143
1197
|
`${foreignAs}.${throughPrimaryKeys[i]}`
|
|
1144
|
-
);
|
|
1198
|
+
]);
|
|
1145
1199
|
}
|
|
1146
1200
|
for (let i = 0; i < len; i++) {
|
|
1147
|
-
|
|
1201
|
+
pqb._queryJoinOn(q, [foreignKeysFull[i], `${tableAs}.${primaryKeys[i]}`]);
|
|
1148
1202
|
}
|
|
1149
1203
|
return q;
|
|
1150
1204
|
});
|
|
@@ -1171,12 +1225,19 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
1171
1225
|
where[foreignKeysFull[i]] = params[primaryKeys[i]];
|
|
1172
1226
|
}
|
|
1173
1227
|
for (let i = 0; i < throughLen; i++) {
|
|
1174
|
-
|
|
1228
|
+
pqb._queryJoinOn(q, [
|
|
1229
|
+
throughForeignKeysFull[i],
|
|
1230
|
+
throughPrimaryKeysFull[i]
|
|
1231
|
+
]);
|
|
1175
1232
|
}
|
|
1176
|
-
return
|
|
1233
|
+
return pqb._queryWhere(q, [where]);
|
|
1177
1234
|
});
|
|
1178
1235
|
},
|
|
1179
|
-
virtualColumn: new HasAndBelongsToManyVirtualColumn(
|
|
1236
|
+
virtualColumn: new HasAndBelongsToManyVirtualColumn(
|
|
1237
|
+
pqb.defaultSchemaConfig,
|
|
1238
|
+
relationName,
|
|
1239
|
+
state
|
|
1240
|
+
),
|
|
1180
1241
|
joinQuery: joinQueryChainingHOF(reverseJoin, (joiningQuery, baseQuery2) => {
|
|
1181
1242
|
const joined = joinQuery(
|
|
1182
1243
|
joiningQuery,
|
|
@@ -1191,7 +1252,7 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
1191
1252
|
reverseJoin,
|
|
1192
1253
|
modifyRelatedQuery(relationQuery) {
|
|
1193
1254
|
const ref = {};
|
|
1194
|
-
|
|
1255
|
+
pqb._queryHookAfterCreate(relationQuery, [], async (result) => {
|
|
1195
1256
|
if (result.length > 1) {
|
|
1196
1257
|
throw new pqb.OrchidOrmInternalError(
|
|
1197
1258
|
relationQuery,
|
|
@@ -1204,7 +1265,11 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
1204
1265
|
for (let i = 0; i < throughLen; i++) {
|
|
1205
1266
|
data[throughForeignKeys[i]] = result[0][throughPrimaryKeys[i]];
|
|
1206
1267
|
}
|
|
1207
|
-
const createdCount = await
|
|
1268
|
+
const createdCount = await pqb._queryCreateFrom(
|
|
1269
|
+
subQuery.count(),
|
|
1270
|
+
baseQuery2,
|
|
1271
|
+
data
|
|
1272
|
+
);
|
|
1208
1273
|
if (createdCount === 0) {
|
|
1209
1274
|
throw new pqb.NotFoundError(baseQuery2);
|
|
1210
1275
|
}
|
|
@@ -1223,12 +1288,17 @@ const queryJoinTable = (state, data, conditions) => {
|
|
|
1223
1288
|
}
|
|
1224
1289
|
});
|
|
1225
1290
|
if (conditions) {
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1291
|
+
pqb._queryWhere(t, [
|
|
1292
|
+
{
|
|
1293
|
+
IN: {
|
|
1294
|
+
columns: state.throughForeignKeys,
|
|
1295
|
+
values: pqb._querySelect(
|
|
1296
|
+
state.relatedTableQuery.where(conditionsToWhereArg(conditions)),
|
|
1297
|
+
state.throughPrimaryKeys
|
|
1298
|
+
)
|
|
1299
|
+
}
|
|
1230
1300
|
}
|
|
1231
|
-
|
|
1301
|
+
]);
|
|
1232
1302
|
}
|
|
1233
1303
|
return t;
|
|
1234
1304
|
};
|
|
@@ -1275,9 +1345,7 @@ const nestedInsert = ({
|
|
|
1275
1345
|
const queries = [];
|
|
1276
1346
|
for (const [, { connect }] of items) {
|
|
1277
1347
|
for (const item of connect) {
|
|
1278
|
-
queries.push(
|
|
1279
|
-
t.select(...throughPrimaryKeys)._findBy(item)._take()
|
|
1280
|
-
);
|
|
1348
|
+
queries.push(pqb._queryFindBy(t.select(...throughPrimaryKeys), [item]));
|
|
1281
1349
|
}
|
|
1282
1350
|
}
|
|
1283
1351
|
connected = await Promise.all(queries);
|
|
@@ -1296,7 +1364,7 @@ const nestedInsert = ({
|
|
|
1296
1364
|
for (const [, { connectOrCreate }] of items) {
|
|
1297
1365
|
for (const item of connectOrCreate) {
|
|
1298
1366
|
queries.push(
|
|
1299
|
-
t.select(...throughPrimaryKeys)
|
|
1367
|
+
pqb._queryFindByOptional(t.select(...throughPrimaryKeys), [item.where])
|
|
1300
1368
|
);
|
|
1301
1369
|
}
|
|
1302
1370
|
}
|
|
@@ -1336,7 +1404,10 @@ const nestedInsert = ({
|
|
|
1336
1404
|
}
|
|
1337
1405
|
}
|
|
1338
1406
|
}
|
|
1339
|
-
created = await
|
|
1407
|
+
created = await pqb._queryCreateMany(
|
|
1408
|
+
t.select(...throughPrimaryKeys),
|
|
1409
|
+
records2
|
|
1410
|
+
);
|
|
1340
1411
|
} else {
|
|
1341
1412
|
created = [];
|
|
1342
1413
|
}
|
|
@@ -1394,7 +1465,10 @@ const nestedUpdate = (state) => {
|
|
|
1394
1465
|
const throughLen = state.throughPrimaryKeys.length;
|
|
1395
1466
|
return async (_, data, params) => {
|
|
1396
1467
|
if (params.create) {
|
|
1397
|
-
const idsRows = await
|
|
1468
|
+
const idsRows = await pqb._queryCreateMany(
|
|
1469
|
+
pqb._queryRows(state.relatedTableQuery.select(...state.throughPrimaryKeys)),
|
|
1470
|
+
params.create
|
|
1471
|
+
);
|
|
1398
1472
|
const records = [];
|
|
1399
1473
|
for (const item of data) {
|
|
1400
1474
|
const obj = {};
|
|
@@ -1412,41 +1486,58 @@ const nestedUpdate = (state) => {
|
|
|
1412
1486
|
await state.joinTableQuery.createMany(records);
|
|
1413
1487
|
}
|
|
1414
1488
|
if (params.update) {
|
|
1415
|
-
await
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1489
|
+
await pqb._queryUpdate(
|
|
1490
|
+
pqb._queryWhere(
|
|
1491
|
+
state.relatedTableQuery.whereExists(state.joinTableQuery, (q) => {
|
|
1492
|
+
for (let i = 0; i < throughLen; i++) {
|
|
1493
|
+
pqb._queryJoinOn(q, [
|
|
1494
|
+
state.throughForeignKeysFull[i],
|
|
1495
|
+
state.throughPrimaryKeysFull[i]
|
|
1496
|
+
]);
|
|
1497
|
+
}
|
|
1498
|
+
return pqb._queryWhere(q, [
|
|
1499
|
+
{
|
|
1500
|
+
IN: {
|
|
1501
|
+
columns: state.foreignKeysFull,
|
|
1502
|
+
values: data.map(
|
|
1503
|
+
(item) => state.primaryKeys.map((key) => item[key])
|
|
1504
|
+
)
|
|
1505
|
+
}
|
|
1506
|
+
}
|
|
1507
|
+
]);
|
|
1508
|
+
}),
|
|
1509
|
+
[conditionsToWhereArg(params.update.where)]
|
|
1510
|
+
),
|
|
1511
|
+
params.update.data
|
|
1512
|
+
);
|
|
1431
1513
|
}
|
|
1432
1514
|
if (params.disconnect) {
|
|
1433
|
-
await queryJoinTable(state, data, params.disconnect)
|
|
1515
|
+
await pqb._queryDelete(queryJoinTable(state, data, params.disconnect));
|
|
1434
1516
|
}
|
|
1435
1517
|
if (params.delete) {
|
|
1436
1518
|
const j = queryJoinTable(state, data, params.delete);
|
|
1437
|
-
const idsRows = await
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1519
|
+
const idsRows = await pqb._queryDelete(
|
|
1520
|
+
pqb._queryRows(pqb._querySelect(j, state.throughForeignKeys))
|
|
1521
|
+
);
|
|
1522
|
+
await pqb._queryDelete(
|
|
1523
|
+
state.relatedTableQuery.where({
|
|
1524
|
+
IN: {
|
|
1525
|
+
columns: state.throughPrimaryKeys,
|
|
1526
|
+
values: idsRows
|
|
1527
|
+
}
|
|
1528
|
+
})
|
|
1529
|
+
);
|
|
1444
1530
|
}
|
|
1445
1531
|
if (params.set) {
|
|
1446
1532
|
const j = queryJoinTable(state, data);
|
|
1447
|
-
await
|
|
1533
|
+
await pqb._queryDelete(j);
|
|
1448
1534
|
delete j.q[pqb.toSQLCacheKey];
|
|
1449
|
-
const idsRows = await
|
|
1535
|
+
const idsRows = await pqb._queryRows(
|
|
1536
|
+
pqb._querySelect(
|
|
1537
|
+
state.relatedTableQuery.where(conditionsToWhereArg(params.set)),
|
|
1538
|
+
state.throughPrimaryKeys
|
|
1539
|
+
)
|
|
1540
|
+
);
|
|
1450
1541
|
await insertToJoinTable(state, j, data, idsRows);
|
|
1451
1542
|
}
|
|
1452
1543
|
};
|
|
@@ -1588,9 +1679,9 @@ const applyRelation = (qb, { relationName, relation, dbTable, otherDbTable }, de
|
|
|
1588
1679
|
}
|
|
1589
1680
|
if (data.returns === "one") {
|
|
1590
1681
|
if (relation.options.required) {
|
|
1591
|
-
|
|
1682
|
+
pqb._queryTake(query);
|
|
1592
1683
|
} else {
|
|
1593
|
-
|
|
1684
|
+
pqb._queryTakeOptional(query);
|
|
1594
1685
|
}
|
|
1595
1686
|
query.q.returnsOne = true;
|
|
1596
1687
|
}
|
|
@@ -1633,11 +1724,13 @@ const makeRelationQuery = (table, relationName, data, q) => {
|
|
|
1633
1724
|
query = toTable;
|
|
1634
1725
|
query.q.isSubQuery = true;
|
|
1635
1726
|
} else {
|
|
1636
|
-
query =
|
|
1637
|
-
|
|
1638
|
-
|
|
1727
|
+
query = pqb._queryWhere(pqb._queryAll(toTable), [
|
|
1728
|
+
{
|
|
1729
|
+
EXISTS: {
|
|
1730
|
+
args: [data.reverseJoin(this, toTable)]
|
|
1731
|
+
}
|
|
1639
1732
|
}
|
|
1640
|
-
|
|
1733
|
+
]);
|
|
1641
1734
|
}
|
|
1642
1735
|
if (this.q.relChain) {
|
|
1643
1736
|
query.q.relChain = [...this.q.relChain, this];
|
|
@@ -1735,7 +1828,7 @@ const orchidORM = (_a, tables) => {
|
|
|
1735
1828
|
void 0,
|
|
1736
1829
|
void 0,
|
|
1737
1830
|
pqb.anyShape,
|
|
1738
|
-
pqb.
|
|
1831
|
+
pqb.makeColumnTypes(pqb.defaultSchemaConfig),
|
|
1739
1832
|
transactionStorage,
|
|
1740
1833
|
commonOptions
|
|
1741
1834
|
);
|