orchid-orm 1.20.0 → 1.21.0
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 +46 -38
- package/dist/index.js +161 -90
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +162 -91
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -133,7 +133,7 @@ const getThroughRelation = (table, through) => {
|
|
|
133
133
|
};
|
|
134
134
|
const getSourceRelation = (throughRelation, source) => {
|
|
135
135
|
var _a;
|
|
136
|
-
return (_a = throughRelation.
|
|
136
|
+
return (_a = throughRelation.query.relations[source]) == null ? void 0 : _a.relationConfig;
|
|
137
137
|
};
|
|
138
138
|
const hasRelationHandleCreate = (q, ctx, item, rowIndex, key, primaryKeys, nestedInsert) => {
|
|
139
139
|
const value = item[key];
|
|
@@ -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
|
};
|
|
@@ -1302,7 +1346,7 @@ const nestedInsert = ({
|
|
|
1302
1346
|
for (const [, { connect }] of items) {
|
|
1303
1347
|
for (const item of connect) {
|
|
1304
1348
|
queries.push(
|
|
1305
|
-
t.select(...throughPrimaryKeys)
|
|
1349
|
+
pqb._queryFindBy(t.select(...throughPrimaryKeys), [item])
|
|
1306
1350
|
);
|
|
1307
1351
|
}
|
|
1308
1352
|
}
|
|
@@ -1322,7 +1366,9 @@ const nestedInsert = ({
|
|
|
1322
1366
|
for (const [, { connectOrCreate }] of items) {
|
|
1323
1367
|
for (const item of connectOrCreate) {
|
|
1324
1368
|
queries.push(
|
|
1325
|
-
t.select(...throughPrimaryKeys)
|
|
1369
|
+
pqb._queryFindByOptional(t.select(...throughPrimaryKeys), [
|
|
1370
|
+
item.where
|
|
1371
|
+
])
|
|
1326
1372
|
);
|
|
1327
1373
|
}
|
|
1328
1374
|
}
|
|
@@ -1362,7 +1408,10 @@ const nestedInsert = ({
|
|
|
1362
1408
|
}
|
|
1363
1409
|
}
|
|
1364
1410
|
}
|
|
1365
|
-
created = await
|
|
1411
|
+
created = await pqb._queryCreateMany(
|
|
1412
|
+
t.select(...throughPrimaryKeys),
|
|
1413
|
+
records2
|
|
1414
|
+
);
|
|
1366
1415
|
} else {
|
|
1367
1416
|
created = [];
|
|
1368
1417
|
}
|
|
@@ -1420,7 +1469,10 @@ const nestedUpdate = (state) => {
|
|
|
1420
1469
|
const throughLen = state.throughPrimaryKeys.length;
|
|
1421
1470
|
return async (_, data, params) => {
|
|
1422
1471
|
if (params.create) {
|
|
1423
|
-
const idsRows = await
|
|
1472
|
+
const idsRows = await pqb._queryCreateMany(
|
|
1473
|
+
pqb._queryRows(state.relatedTableQuery.select(...state.throughPrimaryKeys)),
|
|
1474
|
+
params.create
|
|
1475
|
+
);
|
|
1424
1476
|
const records = [];
|
|
1425
1477
|
for (const item of data) {
|
|
1426
1478
|
const obj = {};
|
|
@@ -1438,41 +1490,58 @@ const nestedUpdate = (state) => {
|
|
|
1438
1490
|
await state.joinTableQuery.createMany(records);
|
|
1439
1491
|
}
|
|
1440
1492
|
if (params.update) {
|
|
1441
|
-
await
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1493
|
+
await pqb._queryUpdate(
|
|
1494
|
+
pqb._queryWhere(
|
|
1495
|
+
state.relatedTableQuery.whereExists(state.joinTableQuery, (q) => {
|
|
1496
|
+
for (let i = 0; i < throughLen; i++) {
|
|
1497
|
+
pqb._queryJoinOn(q, [
|
|
1498
|
+
state.throughForeignKeysFull[i],
|
|
1499
|
+
state.throughPrimaryKeysFull[i]
|
|
1500
|
+
]);
|
|
1501
|
+
}
|
|
1502
|
+
return pqb._queryWhere(q, [
|
|
1503
|
+
{
|
|
1504
|
+
IN: {
|
|
1505
|
+
columns: state.foreignKeysFull,
|
|
1506
|
+
values: data.map(
|
|
1507
|
+
(item) => state.primaryKeys.map((key) => item[key])
|
|
1508
|
+
)
|
|
1509
|
+
}
|
|
1510
|
+
}
|
|
1511
|
+
]);
|
|
1512
|
+
}),
|
|
1513
|
+
[conditionsToWhereArg(params.update.where)]
|
|
1514
|
+
),
|
|
1515
|
+
params.update.data
|
|
1516
|
+
);
|
|
1457
1517
|
}
|
|
1458
1518
|
if (params.disconnect) {
|
|
1459
|
-
await queryJoinTable(state, data, params.disconnect)
|
|
1519
|
+
await pqb._queryDelete(queryJoinTable(state, data, params.disconnect));
|
|
1460
1520
|
}
|
|
1461
1521
|
if (params.delete) {
|
|
1462
1522
|
const j = queryJoinTable(state, data, params.delete);
|
|
1463
|
-
const idsRows = await
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1523
|
+
const idsRows = await pqb._queryDelete(
|
|
1524
|
+
pqb._queryRows(pqb._querySelect(j, state.throughForeignKeys))
|
|
1525
|
+
);
|
|
1526
|
+
await pqb._queryDelete(
|
|
1527
|
+
state.relatedTableQuery.where({
|
|
1528
|
+
IN: {
|
|
1529
|
+
columns: state.throughPrimaryKeys,
|
|
1530
|
+
values: idsRows
|
|
1531
|
+
}
|
|
1532
|
+
})
|
|
1533
|
+
);
|
|
1470
1534
|
}
|
|
1471
1535
|
if (params.set) {
|
|
1472
1536
|
const j = queryJoinTable(state, data);
|
|
1473
|
-
await
|
|
1537
|
+
await pqb._queryDelete(j);
|
|
1474
1538
|
delete j.q[pqb.toSQLCacheKey];
|
|
1475
|
-
const idsRows = await
|
|
1539
|
+
const idsRows = await pqb._queryRows(
|
|
1540
|
+
pqb._querySelect(
|
|
1541
|
+
state.relatedTableQuery.where(conditionsToWhereArg(params.set)),
|
|
1542
|
+
state.throughPrimaryKeys
|
|
1543
|
+
)
|
|
1544
|
+
);
|
|
1476
1545
|
await insertToJoinTable(state, j, data, idsRows);
|
|
1477
1546
|
}
|
|
1478
1547
|
};
|
|
@@ -1614,9 +1683,9 @@ const applyRelation = (qb, { relationName, relation, dbTable, otherDbTable }, de
|
|
|
1614
1683
|
}
|
|
1615
1684
|
if (data.returns === "one") {
|
|
1616
1685
|
if (relation.options.required) {
|
|
1617
|
-
|
|
1686
|
+
pqb._queryTake(query);
|
|
1618
1687
|
} else {
|
|
1619
|
-
|
|
1688
|
+
pqb._queryTakeOptional(query);
|
|
1620
1689
|
}
|
|
1621
1690
|
query.q.returnsOne = true;
|
|
1622
1691
|
}
|
|
@@ -1659,11 +1728,13 @@ const makeRelationQuery = (table, relationName, data, q) => {
|
|
|
1659
1728
|
query = toTable;
|
|
1660
1729
|
query.q.isSubQuery = true;
|
|
1661
1730
|
} else {
|
|
1662
|
-
query =
|
|
1663
|
-
|
|
1664
|
-
|
|
1731
|
+
query = pqb._queryWhere(pqb._queryAll(toTable), [
|
|
1732
|
+
{
|
|
1733
|
+
EXISTS: {
|
|
1734
|
+
args: [data.reverseJoin(this, toTable)]
|
|
1735
|
+
}
|
|
1665
1736
|
}
|
|
1666
|
-
|
|
1737
|
+
]);
|
|
1667
1738
|
}
|
|
1668
1739
|
if (this.q.relChain) {
|
|
1669
1740
|
query.q.relChain = [...this.q.relChain, this];
|