orchid-orm 1.20.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.js +158 -91
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +159 -92
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -150,7 +150,8 @@ const hasRelationHandleCreate = (q, ctx, item, rowIndex, key, primaryKeys, neste
|
|
|
150
150
|
q.q.wrapInTransaction = true;
|
|
151
151
|
const relationData = [values];
|
|
152
152
|
store.hasRelation[key] = relationData;
|
|
153
|
-
|
|
153
|
+
pqb._queryHookAfterCreate(
|
|
154
|
+
q,
|
|
154
155
|
primaryKeys,
|
|
155
156
|
(rows, q2) => nestedInsert(
|
|
156
157
|
q2,
|
|
@@ -167,7 +168,7 @@ const hasRelationHandleUpdate = (q, set, key, primaryKeys, nestedUpdate) => {
|
|
|
167
168
|
return;
|
|
168
169
|
selectIfNotSelected(q, primaryKeys);
|
|
169
170
|
q.q.wrapInTransaction = true;
|
|
170
|
-
|
|
171
|
+
pqb._queryHookAfterUpdate(q, q.primaryKeys, (rows, q2) => {
|
|
171
172
|
return nestedUpdate(
|
|
172
173
|
q2,
|
|
173
174
|
rows,
|
|
@@ -359,7 +360,7 @@ const nestedInsert$3 = ({ query, primaryKeys }) => {
|
|
|
359
360
|
for (let i = 0, len = items.length; i < len; i++) {
|
|
360
361
|
items[i] = "create" in items[i] ? items[i].create : items[i].connectOrCreate.create;
|
|
361
362
|
}
|
|
362
|
-
created = await t.select(...primaryKeys)
|
|
363
|
+
created = await pqb._queryCreateMany(t.select(...primaryKeys), items);
|
|
363
364
|
} else {
|
|
364
365
|
created = orchidCore.emptyArray;
|
|
365
366
|
}
|
|
@@ -393,7 +394,7 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
393
394
|
throw new Error("`upsert` option is not allowed in a batch update");
|
|
394
395
|
}
|
|
395
396
|
let idsForDelete;
|
|
396
|
-
|
|
397
|
+
pqb._queryHookBeforeUpdate(q, async (q2) => {
|
|
397
398
|
if (params.disconnect) {
|
|
398
399
|
for (const key of foreignKeys) {
|
|
399
400
|
update[key] = null;
|
|
@@ -411,7 +412,9 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
411
412
|
}
|
|
412
413
|
}
|
|
413
414
|
if (loadPrimaryKeys) {
|
|
414
|
-
const record = await query.select(...loadPrimaryKeys)
|
|
415
|
+
const record = await pqb._queryFindBy(query.select(...loadPrimaryKeys), [
|
|
416
|
+
params.set
|
|
417
|
+
]);
|
|
415
418
|
for (let i = 0, len2 = loadPrimaryKeys.length; i < len2; i++) {
|
|
416
419
|
update[loadForeignKeys[i]] = record[loadPrimaryKeys[i]];
|
|
417
420
|
}
|
|
@@ -419,7 +422,7 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
419
422
|
} else if (params.create) {
|
|
420
423
|
const q3 = query.clone();
|
|
421
424
|
q3.q.select = primaryKeys;
|
|
422
|
-
const record = await
|
|
425
|
+
const record = await pqb._queryCreate(q3, params.create);
|
|
423
426
|
for (let i = 0; i < len; i++) {
|
|
424
427
|
update[foreignKeys[i]] = record[primaryKeys[i]];
|
|
425
428
|
}
|
|
@@ -427,7 +430,7 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
427
430
|
const selectQuery = q2.clone();
|
|
428
431
|
selectQuery.q.type = void 0;
|
|
429
432
|
selectQuery.q.distinct = orchidCore.emptyArray;
|
|
430
|
-
idsForDelete = await
|
|
433
|
+
idsForDelete = await pqb._queryRows(selectQuery);
|
|
431
434
|
for (const foreignKey of foreignKeys) {
|
|
432
435
|
update[foreignKey] = null;
|
|
433
436
|
}
|
|
@@ -451,17 +454,20 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
451
454
|
obj[primaryKeys[i]] = id;
|
|
452
455
|
}
|
|
453
456
|
if (obj) {
|
|
454
|
-
await
|
|
457
|
+
await pqb._queryUpdate(
|
|
458
|
+
query.findBy(obj),
|
|
459
|
+
upsert.update
|
|
460
|
+
);
|
|
455
461
|
} else {
|
|
456
462
|
const data = typeof upsert.create === "function" ? upsert.create() : upsert.create;
|
|
457
|
-
const result = await query.select(...primaryKeys)
|
|
463
|
+
const result = await pqb._queryCreate(query.select(...primaryKeys), data);
|
|
458
464
|
for (let i = 0; i < len; i++) {
|
|
459
465
|
((_a2 = state.updateData) != null ? _a2 : state.updateData = {})[foreignKeys[i]] = result[primaryKeys[i]];
|
|
460
466
|
}
|
|
461
467
|
}
|
|
462
468
|
});
|
|
463
469
|
} else if (params.delete || params.update) {
|
|
464
|
-
|
|
470
|
+
pqb._queryHookAfterUpdate(q, [], async (data) => {
|
|
465
471
|
let ids;
|
|
466
472
|
if (params.delete) {
|
|
467
473
|
ids = idsForDelete;
|
|
@@ -489,11 +495,9 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
489
495
|
ids
|
|
490
496
|
);
|
|
491
497
|
if (params.delete) {
|
|
492
|
-
await
|
|
498
|
+
await pqb._queryDelete(t);
|
|
493
499
|
} else {
|
|
494
|
-
await t.
|
|
495
|
-
params.update
|
|
496
|
-
);
|
|
500
|
+
await pqb._queryUpdate(t, params.update);
|
|
497
501
|
}
|
|
498
502
|
});
|
|
499
503
|
}
|
|
@@ -613,7 +617,7 @@ const makeHasOneMethod = (table, relation, relationName, query) => {
|
|
|
613
617
|
for (let i = 0; i < len; i++) {
|
|
614
618
|
values[foreignKeys[i]] = params[primaryKeys[i]];
|
|
615
619
|
}
|
|
616
|
-
return query.where(values)
|
|
620
|
+
return pqb._queryDefaults(query.where(values), values);
|
|
617
621
|
},
|
|
618
622
|
virtualColumn: new HasOneVirtualColumn(
|
|
619
623
|
pqb.defaultSchemaConfig,
|
|
@@ -653,9 +657,15 @@ const nestedInsert$2 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
653
657
|
for (let i2 = 0; i2 < len; i2++) {
|
|
654
658
|
data2[foreignKeys[i2]] = selfData[primaryKeys[i2]];
|
|
655
659
|
}
|
|
656
|
-
items[i] = "connect" in item ?
|
|
657
|
-
item.
|
|
658
|
-
|
|
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
|
+
);
|
|
659
669
|
}
|
|
660
670
|
connected = await Promise.all(items);
|
|
661
671
|
} else {
|
|
@@ -701,7 +711,8 @@ const nestedUpdate$2 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
701
711
|
ids
|
|
702
712
|
);
|
|
703
713
|
if (params.create || params.disconnect || params.set) {
|
|
704
|
-
await
|
|
714
|
+
await pqb._queryUpdate(
|
|
715
|
+
currentRelationsQuery,
|
|
705
716
|
setNulls
|
|
706
717
|
);
|
|
707
718
|
const record = data[0];
|
|
@@ -717,16 +728,25 @@ const nestedUpdate$2 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
717
728
|
for (let i = 0; i < len; i++) {
|
|
718
729
|
obj[foreignKeys[i]] = record[primaryKeys[i]];
|
|
719
730
|
}
|
|
720
|
-
await
|
|
731
|
+
await pqb._queryUpdate(
|
|
732
|
+
pqb._queryWhere(t, [params.set]),
|
|
733
|
+
obj
|
|
734
|
+
);
|
|
721
735
|
}
|
|
722
736
|
} else if (params.update) {
|
|
723
|
-
await
|
|
737
|
+
await pqb._queryUpdate(
|
|
738
|
+
currentRelationsQuery,
|
|
739
|
+
params.update
|
|
740
|
+
);
|
|
724
741
|
} else if (params.delete) {
|
|
725
|
-
await
|
|
742
|
+
await pqb._queryDelete(currentRelationsQuery);
|
|
726
743
|
} else if (params.upsert) {
|
|
727
744
|
const { update, create } = params.upsert;
|
|
728
745
|
currentRelationsQuery.q.select = foreignKeys;
|
|
729
|
-
const updatedIds = await
|
|
746
|
+
const updatedIds = await pqb._queryUpdate(
|
|
747
|
+
pqb._queryRows(currentRelationsQuery),
|
|
748
|
+
update
|
|
749
|
+
);
|
|
730
750
|
if (updatedIds.length < ids.length) {
|
|
731
751
|
const data2 = typeof create === "function" ? create() : create;
|
|
732
752
|
await t.createMany(
|
|
@@ -865,7 +885,7 @@ const makeHasManyMethod = (table, relation, relationName, query) => {
|
|
|
865
885
|
for (let i = 0; i < len; i++) {
|
|
866
886
|
values[foreignKeys[i]] = params[primaryKeys[i]];
|
|
867
887
|
}
|
|
868
|
-
return query.where(values)
|
|
888
|
+
return pqb._queryDefaults(query.where(values), values);
|
|
869
889
|
},
|
|
870
890
|
virtualColumn: new HasManyVirtualColumn(
|
|
871
891
|
pqb.defaultSchemaConfig,
|
|
@@ -914,7 +934,10 @@ const nestedInsert$1 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
914
934
|
for (let i2 = 0; i2 < len2; i2++) {
|
|
915
935
|
obj[foreignKeys[i2]] = selfData[primaryKeys[i2]];
|
|
916
936
|
}
|
|
917
|
-
items[i] =
|
|
937
|
+
items[i] = pqb._queryUpdateOrThrow(
|
|
938
|
+
t.orWhere(...connect),
|
|
939
|
+
obj
|
|
940
|
+
);
|
|
918
941
|
}
|
|
919
942
|
await Promise.all(items);
|
|
920
943
|
}
|
|
@@ -935,7 +958,10 @@ const nestedInsert$1 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
935
958
|
obj[foreignKeys[i2]] = selfData[primaryKeys[i2]];
|
|
936
959
|
}
|
|
937
960
|
queries.push(
|
|
938
|
-
|
|
961
|
+
pqb._queryUpdate(
|
|
962
|
+
t.where(item.where),
|
|
963
|
+
obj
|
|
964
|
+
)
|
|
939
965
|
);
|
|
940
966
|
}
|
|
941
967
|
}
|
|
@@ -980,7 +1006,7 @@ const nestedInsert$1 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
980
1006
|
}
|
|
981
1007
|
}
|
|
982
1008
|
}
|
|
983
|
-
await
|
|
1009
|
+
await pqb._queryCreateMany(t, records);
|
|
984
1010
|
}
|
|
985
1011
|
};
|
|
986
1012
|
};
|
|
@@ -1004,24 +1030,30 @@ const nestedUpdate$1 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
1004
1030
|
for (const foreignKey of foreignKeys) {
|
|
1005
1031
|
obj[foreignKey] = null;
|
|
1006
1032
|
}
|
|
1007
|
-
await
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1033
|
+
await pqb._queryUpdate(
|
|
1034
|
+
getWhereForNestedUpdate(
|
|
1035
|
+
t,
|
|
1036
|
+
data,
|
|
1037
|
+
params.disconnect,
|
|
1038
|
+
primaryKeys,
|
|
1039
|
+
foreignKeys
|
|
1040
|
+
),
|
|
1041
|
+
obj
|
|
1042
|
+
);
|
|
1014
1043
|
if (params.set) {
|
|
1015
1044
|
delete t.q[pqb.toSQLCacheKey];
|
|
1016
1045
|
const obj2 = {};
|
|
1017
1046
|
for (let i = 0; i < len; i++) {
|
|
1018
1047
|
obj2[foreignKeys[i]] = data[0][primaryKeys[i]];
|
|
1019
1048
|
}
|
|
1020
|
-
await
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1049
|
+
await pqb._queryUpdate(
|
|
1050
|
+
t.where(
|
|
1051
|
+
Array.isArray(params.set) ? {
|
|
1052
|
+
OR: params.set
|
|
1053
|
+
} : params.set
|
|
1054
|
+
),
|
|
1055
|
+
obj2
|
|
1056
|
+
);
|
|
1025
1057
|
}
|
|
1026
1058
|
}
|
|
1027
1059
|
if (params.delete || params.update) {
|
|
@@ -1034,9 +1066,9 @@ const nestedUpdate$1 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
1034
1066
|
foreignKeys
|
|
1035
1067
|
);
|
|
1036
1068
|
if (params.delete) {
|
|
1037
|
-
await
|
|
1069
|
+
await pqb._queryDelete(q);
|
|
1038
1070
|
} else if (params.update) {
|
|
1039
|
-
await
|
|
1071
|
+
await pqb._queryUpdate(q, params.update.data);
|
|
1040
1072
|
}
|
|
1041
1073
|
}
|
|
1042
1074
|
};
|
|
@@ -1160,13 +1192,13 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
1160
1192
|
const joinQuery = (joiningQuery, tableAs, foreignAs) => {
|
|
1161
1193
|
return joiningQuery.whereExists(subQuery, (q) => {
|
|
1162
1194
|
for (let i = 0; i < throughLen; i++) {
|
|
1163
|
-
|
|
1195
|
+
pqb._queryJoinOn(q, [
|
|
1164
1196
|
throughForeignKeysFull[i],
|
|
1165
1197
|
`${foreignAs}.${throughPrimaryKeys[i]}`
|
|
1166
|
-
);
|
|
1198
|
+
]);
|
|
1167
1199
|
}
|
|
1168
1200
|
for (let i = 0; i < len; i++) {
|
|
1169
|
-
|
|
1201
|
+
pqb._queryJoinOn(q, [foreignKeysFull[i], `${tableAs}.${primaryKeys[i]}`]);
|
|
1170
1202
|
}
|
|
1171
1203
|
return q;
|
|
1172
1204
|
});
|
|
@@ -1193,9 +1225,12 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
1193
1225
|
where[foreignKeysFull[i]] = params[primaryKeys[i]];
|
|
1194
1226
|
}
|
|
1195
1227
|
for (let i = 0; i < throughLen; i++) {
|
|
1196
|
-
|
|
1228
|
+
pqb._queryJoinOn(q, [
|
|
1229
|
+
throughForeignKeysFull[i],
|
|
1230
|
+
throughPrimaryKeysFull[i]
|
|
1231
|
+
]);
|
|
1197
1232
|
}
|
|
1198
|
-
return
|
|
1233
|
+
return pqb._queryWhere(q, [where]);
|
|
1199
1234
|
});
|
|
1200
1235
|
},
|
|
1201
1236
|
virtualColumn: new HasAndBelongsToManyVirtualColumn(
|
|
@@ -1217,7 +1252,7 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
1217
1252
|
reverseJoin,
|
|
1218
1253
|
modifyRelatedQuery(relationQuery) {
|
|
1219
1254
|
const ref = {};
|
|
1220
|
-
|
|
1255
|
+
pqb._queryHookAfterCreate(relationQuery, [], async (result) => {
|
|
1221
1256
|
if (result.length > 1) {
|
|
1222
1257
|
throw new pqb.OrchidOrmInternalError(
|
|
1223
1258
|
relationQuery,
|
|
@@ -1230,7 +1265,11 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
1230
1265
|
for (let i = 0; i < throughLen; i++) {
|
|
1231
1266
|
data[throughForeignKeys[i]] = result[0][throughPrimaryKeys[i]];
|
|
1232
1267
|
}
|
|
1233
|
-
const createdCount = await
|
|
1268
|
+
const createdCount = await pqb._queryCreateFrom(
|
|
1269
|
+
subQuery.count(),
|
|
1270
|
+
baseQuery2,
|
|
1271
|
+
data
|
|
1272
|
+
);
|
|
1234
1273
|
if (createdCount === 0) {
|
|
1235
1274
|
throw new pqb.NotFoundError(baseQuery2);
|
|
1236
1275
|
}
|
|
@@ -1249,12 +1288,17 @@ const queryJoinTable = (state, data, conditions) => {
|
|
|
1249
1288
|
}
|
|
1250
1289
|
});
|
|
1251
1290
|
if (conditions) {
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
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
|
+
}
|
|
1256
1300
|
}
|
|
1257
|
-
|
|
1301
|
+
]);
|
|
1258
1302
|
}
|
|
1259
1303
|
return t;
|
|
1260
1304
|
};
|
|
@@ -1301,9 +1345,7 @@ const nestedInsert = ({
|
|
|
1301
1345
|
const queries = [];
|
|
1302
1346
|
for (const [, { connect }] of items) {
|
|
1303
1347
|
for (const item of connect) {
|
|
1304
|
-
queries.push(
|
|
1305
|
-
t.select(...throughPrimaryKeys)._findBy(item)._take()
|
|
1306
|
-
);
|
|
1348
|
+
queries.push(pqb._queryFindBy(t.select(...throughPrimaryKeys), [item]));
|
|
1307
1349
|
}
|
|
1308
1350
|
}
|
|
1309
1351
|
connected = await Promise.all(queries);
|
|
@@ -1322,7 +1364,7 @@ const nestedInsert = ({
|
|
|
1322
1364
|
for (const [, { connectOrCreate }] of items) {
|
|
1323
1365
|
for (const item of connectOrCreate) {
|
|
1324
1366
|
queries.push(
|
|
1325
|
-
t.select(...throughPrimaryKeys)
|
|
1367
|
+
pqb._queryFindByOptional(t.select(...throughPrimaryKeys), [item.where])
|
|
1326
1368
|
);
|
|
1327
1369
|
}
|
|
1328
1370
|
}
|
|
@@ -1362,7 +1404,10 @@ const nestedInsert = ({
|
|
|
1362
1404
|
}
|
|
1363
1405
|
}
|
|
1364
1406
|
}
|
|
1365
|
-
created = await
|
|
1407
|
+
created = await pqb._queryCreateMany(
|
|
1408
|
+
t.select(...throughPrimaryKeys),
|
|
1409
|
+
records2
|
|
1410
|
+
);
|
|
1366
1411
|
} else {
|
|
1367
1412
|
created = [];
|
|
1368
1413
|
}
|
|
@@ -1420,7 +1465,10 @@ const nestedUpdate = (state) => {
|
|
|
1420
1465
|
const throughLen = state.throughPrimaryKeys.length;
|
|
1421
1466
|
return async (_, data, params) => {
|
|
1422
1467
|
if (params.create) {
|
|
1423
|
-
const idsRows = await
|
|
1468
|
+
const idsRows = await pqb._queryCreateMany(
|
|
1469
|
+
pqb._queryRows(state.relatedTableQuery.select(...state.throughPrimaryKeys)),
|
|
1470
|
+
params.create
|
|
1471
|
+
);
|
|
1424
1472
|
const records = [];
|
|
1425
1473
|
for (const item of data) {
|
|
1426
1474
|
const obj = {};
|
|
@@ -1438,41 +1486,58 @@ const nestedUpdate = (state) => {
|
|
|
1438
1486
|
await state.joinTableQuery.createMany(records);
|
|
1439
1487
|
}
|
|
1440
1488
|
if (params.update) {
|
|
1441
|
-
await
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
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
|
+
);
|
|
1457
1513
|
}
|
|
1458
1514
|
if (params.disconnect) {
|
|
1459
|
-
await queryJoinTable(state, data, params.disconnect)
|
|
1515
|
+
await pqb._queryDelete(queryJoinTable(state, data, params.disconnect));
|
|
1460
1516
|
}
|
|
1461
1517
|
if (params.delete) {
|
|
1462
1518
|
const j = queryJoinTable(state, data, params.delete);
|
|
1463
|
-
const idsRows = await
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
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
|
+
);
|
|
1470
1530
|
}
|
|
1471
1531
|
if (params.set) {
|
|
1472
1532
|
const j = queryJoinTable(state, data);
|
|
1473
|
-
await
|
|
1533
|
+
await pqb._queryDelete(j);
|
|
1474
1534
|
delete j.q[pqb.toSQLCacheKey];
|
|
1475
|
-
const idsRows = await
|
|
1535
|
+
const idsRows = await pqb._queryRows(
|
|
1536
|
+
pqb._querySelect(
|
|
1537
|
+
state.relatedTableQuery.where(conditionsToWhereArg(params.set)),
|
|
1538
|
+
state.throughPrimaryKeys
|
|
1539
|
+
)
|
|
1540
|
+
);
|
|
1476
1541
|
await insertToJoinTable(state, j, data, idsRows);
|
|
1477
1542
|
}
|
|
1478
1543
|
};
|
|
@@ -1614,9 +1679,9 @@ const applyRelation = (qb, { relationName, relation, dbTable, otherDbTable }, de
|
|
|
1614
1679
|
}
|
|
1615
1680
|
if (data.returns === "one") {
|
|
1616
1681
|
if (relation.options.required) {
|
|
1617
|
-
|
|
1682
|
+
pqb._queryTake(query);
|
|
1618
1683
|
} else {
|
|
1619
|
-
|
|
1684
|
+
pqb._queryTakeOptional(query);
|
|
1620
1685
|
}
|
|
1621
1686
|
query.q.returnsOne = true;
|
|
1622
1687
|
}
|
|
@@ -1659,11 +1724,13 @@ const makeRelationQuery = (table, relationName, data, q) => {
|
|
|
1659
1724
|
query = toTable;
|
|
1660
1725
|
query.q.isSubQuery = true;
|
|
1661
1726
|
} else {
|
|
1662
|
-
query =
|
|
1663
|
-
|
|
1664
|
-
|
|
1727
|
+
query = pqb._queryWhere(pqb._queryAll(toTable), [
|
|
1728
|
+
{
|
|
1729
|
+
EXISTS: {
|
|
1730
|
+
args: [data.reverseJoin(this, toTable)]
|
|
1731
|
+
}
|
|
1665
1732
|
}
|
|
1666
|
-
|
|
1733
|
+
]);
|
|
1667
1734
|
}
|
|
1668
1735
|
if (this.q.relChain) {
|
|
1669
1736
|
query.q.relChain = [...this.q.relChain, this];
|