orchid-orm 1.3.0 → 1.3.2

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.
@@ -5,6 +5,7 @@ import {
5
5
  expectSql,
6
6
  messageData,
7
7
  userData,
8
+ useRelationCallback,
8
9
  useTestDatabase,
9
10
  } from '../test-utils/test-utils';
10
11
  import { RelationQuery } from 'pqb';
@@ -510,6 +511,59 @@ describe('hasMany', () => {
510
511
 
511
512
  checkUser(user, 'user 1');
512
513
  });
514
+
515
+ describe('relation callbacks', () => {
516
+ const { beforeCreate, afterCreate, resetMocks } = useRelationCallback(
517
+ db.user.relations.messages,
518
+ );
519
+
520
+ it('should invoke callbacks', async () => {
521
+ const chatId = await db.chat.get('id').create(chatData);
522
+
523
+ await db.user.create({
524
+ ...userData,
525
+ messages: {
526
+ create: [
527
+ { ...messageData, chatId },
528
+ { ...messageData, chatId },
529
+ ],
530
+ },
531
+ });
532
+
533
+ expect(beforeCreate).toHaveBeenCalledTimes(1);
534
+ expect(afterCreate).toHaveBeenCalledTimes(1);
535
+ });
536
+
537
+ it('should invoke callbacks in a batch create', async () => {
538
+ resetMocks();
539
+
540
+ const chatId = await db.chat.get('id').create(chatData);
541
+
542
+ await db.user.createMany([
543
+ {
544
+ ...userData,
545
+ messages: {
546
+ create: [
547
+ { ...messageData, chatId },
548
+ { ...messageData, chatId },
549
+ ],
550
+ },
551
+ },
552
+ {
553
+ ...userData,
554
+ messages: {
555
+ create: [
556
+ { ...messageData, chatId },
557
+ { ...messageData, chatId },
558
+ ],
559
+ },
560
+ },
561
+ ]);
562
+
563
+ expect(beforeCreate).toHaveBeenCalledTimes(1);
564
+ expect(afterCreate).toHaveBeenCalledTimes(1);
565
+ });
566
+ });
513
567
  });
514
568
 
515
569
  describe('nested connect', () => {
@@ -649,6 +703,61 @@ describe('hasMany', () => {
649
703
 
650
704
  checkUser(user, 'user 1');
651
705
  });
706
+
707
+ describe('relation callbacks', () => {
708
+ const { beforeUpdate, afterUpdate, resetMocks } = useRelationCallback(
709
+ db.user.relations.messages,
710
+ );
711
+
712
+ it('should invoke callbacks', async () => {
713
+ const chatId = await db.chat.get('id').create(chatData);
714
+ const ids = await db.message.pluck('id').createMany([
715
+ { ...messageData, chatId },
716
+ { ...messageData, chatId },
717
+ ]);
718
+
719
+ await db.user.create({
720
+ ...userData,
721
+ messages: {
722
+ connect: [{ id: ids[0] }, { id: ids[1] }],
723
+ },
724
+ });
725
+
726
+ expect(beforeUpdate).toHaveBeenCalledTimes(1);
727
+ expect(afterUpdate).toHaveBeenCalledTimes(1);
728
+ });
729
+
730
+ it('should invoke callbacks in a batch create', async () => {
731
+ const chatId = await db.chat.get('id').create(chatData);
732
+
733
+ const ids = await db.message.pluck('id').createMany([
734
+ { ...messageData, chatId },
735
+ { ...messageData, chatId },
736
+ { ...messageData, chatId },
737
+ { ...messageData, chatId },
738
+ ]);
739
+
740
+ resetMocks();
741
+
742
+ await db.user.createMany([
743
+ {
744
+ ...userData,
745
+ messages: {
746
+ connect: [{ id: ids[0] }, { id: ids[1] }],
747
+ },
748
+ },
749
+ {
750
+ ...userData,
751
+ messages: {
752
+ connect: [{ id: ids[2] }, { id: ids[3] }],
753
+ },
754
+ },
755
+ ]);
756
+
757
+ expect(beforeUpdate).toHaveBeenCalledTimes(2);
758
+ expect(afterUpdate).toHaveBeenCalledTimes(2);
759
+ });
760
+ });
652
761
  });
653
762
 
654
763
  describe('connectOrCreate', () => {
@@ -781,6 +890,116 @@ describe('hasMany', () => {
781
890
 
782
891
  checkUser(user, 'user 1');
783
892
  });
893
+
894
+ describe('relation callbacks', () => {
895
+ const {
896
+ beforeCreate,
897
+ afterCreate,
898
+ beforeUpdate,
899
+ afterUpdate,
900
+ resetMocks,
901
+ } = useRelationCallback(db.user.relations.messages);
902
+
903
+ it('should invoke callbacks when connecting', async () => {
904
+ const chatId = await db.chat.get('id').create(chatData);
905
+ const ids = await db.message.pluck('id').createMany([
906
+ { ...messageData, chatId },
907
+ { ...messageData, chatId },
908
+ ]);
909
+
910
+ await db.user.create({
911
+ ...userData,
912
+ messages: {
913
+ connectOrCreate: [
914
+ {
915
+ where: { id: ids[0] },
916
+ create: messageData,
917
+ },
918
+ {
919
+ where: { id: ids[1] },
920
+ create: messageData,
921
+ },
922
+ ],
923
+ },
924
+ });
925
+
926
+ expect(beforeUpdate).toHaveBeenCalledTimes(2);
927
+ expect(afterUpdate).toHaveBeenCalledTimes(2);
928
+ });
929
+
930
+ it('should invoke callbacks when creating', async () => {
931
+ const chatId = await db.chat.get('id').create(chatData);
932
+
933
+ resetMocks();
934
+
935
+ await db.user.create({
936
+ ...userData,
937
+ messages: {
938
+ connectOrCreate: [
939
+ {
940
+ where: { id: 0 },
941
+ create: { ...messageData, chatId },
942
+ },
943
+ {
944
+ where: { id: 0 },
945
+ create: { ...messageData, chatId },
946
+ },
947
+ ],
948
+ },
949
+ });
950
+
951
+ expect(beforeCreate).toHaveBeenCalledTimes(1);
952
+ expect(afterCreate).toHaveBeenCalledTimes(1);
953
+ });
954
+
955
+ it('should invoke callbacks in a batch create', async () => {
956
+ const chatId = await db.chat.get('id').create(chatData);
957
+ const ids = await db.message.pluck('id').createMany([
958
+ { ...messageData, chatId },
959
+ { ...messageData, chatId },
960
+ ]);
961
+
962
+ resetMocks();
963
+
964
+ await db.user.createMany([
965
+ {
966
+ ...userData,
967
+ messages: {
968
+ connectOrCreate: [
969
+ {
970
+ where: { id: ids[0] },
971
+ create: { ...messageData, chatId },
972
+ },
973
+ {
974
+ where: { id: 0 },
975
+ create: { ...messageData, chatId },
976
+ },
977
+ ],
978
+ },
979
+ },
980
+ {
981
+ ...userData,
982
+ messages: {
983
+ connectOrCreate: [
984
+ {
985
+ where: { id: ids[1] },
986
+ create: { ...messageData, chatId },
987
+ },
988
+ {
989
+ where: { id: 0 },
990
+ create: { ...messageData, chatId },
991
+ },
992
+ ],
993
+ },
994
+ },
995
+ ]);
996
+
997
+ expect(beforeUpdate).toHaveBeenCalledTimes(4);
998
+ expect(afterUpdate).toHaveBeenCalledTimes(4);
999
+ expect(beforeCreate).toHaveBeenCalledTimes(1);
1000
+ expect(afterCreate).toHaveBeenCalledTimes(1);
1001
+ });
1002
+ });
784
1003
  });
785
1004
  });
786
1005
 
@@ -858,6 +1077,93 @@ describe('hasMany', () => {
858
1077
  },
859
1078
  });
860
1079
  });
1080
+
1081
+ describe('relation callbacks', () => {
1082
+ const { beforeUpdate, afterUpdate, resetMocks } = useRelationCallback(
1083
+ db.user.relations.messages,
1084
+ );
1085
+
1086
+ it('should invoke callbacks', async () => {
1087
+ const chatId = await db.chat.get('id').create(chatData);
1088
+ const userId = await db.user.get('id').create({
1089
+ ...userData,
1090
+ messages: {
1091
+ create: [
1092
+ {
1093
+ ...messageData,
1094
+ chatId,
1095
+ text: 'message 1',
1096
+ },
1097
+ {
1098
+ ...messageData,
1099
+ chatId,
1100
+ text: 'message 2',
1101
+ },
1102
+ ],
1103
+ },
1104
+ });
1105
+
1106
+ await db.user.find(userId).update({
1107
+ messages: {
1108
+ disconnect: [{ text: 'message 1' }, { text: 'message 2' }],
1109
+ },
1110
+ });
1111
+
1112
+ expect(beforeUpdate).toHaveBeenCalledTimes(1);
1113
+ expect(afterUpdate).toHaveBeenCalledTimes(1);
1114
+ });
1115
+
1116
+ it('should invoke callbacks in a batch update', async () => {
1117
+ resetMocks();
1118
+
1119
+ const chatId = await db.chat.get('id').create(chatData);
1120
+ const ids = await db.user.pluck('id').createMany([
1121
+ {
1122
+ ...userData,
1123
+ messages: {
1124
+ create: [
1125
+ {
1126
+ ...messageData,
1127
+ chatId,
1128
+ text: 'message 1',
1129
+ },
1130
+ {
1131
+ ...messageData,
1132
+ chatId,
1133
+ text: 'message 1',
1134
+ },
1135
+ ],
1136
+ },
1137
+ },
1138
+ {
1139
+ ...userData,
1140
+ messages: {
1141
+ create: [
1142
+ {
1143
+ ...messageData,
1144
+ chatId,
1145
+ text: 'message 3',
1146
+ },
1147
+ {
1148
+ ...messageData,
1149
+ chatId,
1150
+ text: 'message 4',
1151
+ },
1152
+ ],
1153
+ },
1154
+ },
1155
+ ]);
1156
+
1157
+ await db.user.where({ id: { in: ids } }).update({
1158
+ messages: {
1159
+ disconnect: [{ text: 'message 1' }, { text: 'message 3' }],
1160
+ },
1161
+ });
1162
+
1163
+ expect(beforeUpdate).toHaveBeenCalledTimes(1);
1164
+ expect(afterUpdate).toHaveBeenCalledTimes(1);
1165
+ });
1166
+ });
861
1167
  });
862
1168
 
863
1169
  describe('set', () => {
@@ -900,6 +1206,40 @@ describe('hasMany', () => {
900
1206
 
901
1207
  await expect(query).rejects.toThrow();
902
1208
  });
1209
+
1210
+ describe('relation callbacks', () => {
1211
+ const { beforeUpdate, afterUpdate } = useRelationCallback(
1212
+ db.user.relations.messages,
1213
+ );
1214
+
1215
+ it('should invoke callbacks', async () => {
1216
+ const chatId = await db.chat.get('id').create(chatData);
1217
+ const id = await db.user.get('id').create({
1218
+ ...userData,
1219
+ messages: {
1220
+ create: [
1221
+ { ...messageData, chatId, text: 'message 1' },
1222
+ { ...messageData, chatId, text: 'message 2' },
1223
+ ],
1224
+ },
1225
+ });
1226
+
1227
+ await db.message.create({
1228
+ ...messageData,
1229
+ chatId,
1230
+ text: 'message 3',
1231
+ });
1232
+
1233
+ await db.user.find(id).update({
1234
+ messages: {
1235
+ set: { text: { in: ['message 2', 'message 3'] } },
1236
+ },
1237
+ });
1238
+
1239
+ expect(beforeUpdate).toHaveBeenCalledTimes(2);
1240
+ expect(afterUpdate).toHaveBeenCalledTimes(2);
1241
+ });
1242
+ });
903
1243
  });
904
1244
 
905
1245
  describe('delete', () => {
@@ -985,6 +1325,77 @@ describe('hasMany', () => {
985
1325
  const messages = await db.user.messages({ id }).pluck('text');
986
1326
  expect(messages).toEqual(['message 1']);
987
1327
  });
1328
+
1329
+ describe('relation callbacks', () => {
1330
+ const { beforeDelete, afterDelete, resetMocks } = useRelationCallback(
1331
+ db.user.relations.messages,
1332
+ );
1333
+
1334
+ it('should invoke callbacks', async () => {
1335
+ const chatId = await db.chat.get('id').create(chatData);
1336
+ const id = await db.user.get('id').create({
1337
+ ...userData,
1338
+ messages: {
1339
+ create: [
1340
+ { ...messageData, chatId, text: 'message 1' },
1341
+ { ...messageData, chatId, text: 'message 2' },
1342
+ { ...messageData, chatId, text: 'message 3' },
1343
+ ],
1344
+ },
1345
+ });
1346
+
1347
+ await db.user.find(id).update({
1348
+ messages: {
1349
+ delete: [{ text: 'message 1' }, { text: 'message 2' }],
1350
+ },
1351
+ });
1352
+
1353
+ expect(beforeDelete).toHaveBeenCalledTimes(1);
1354
+ expect(afterDelete).toHaveBeenCalledTimes(1);
1355
+ });
1356
+
1357
+ it('should invoke callbacks in a batch delete', async () => {
1358
+ resetMocks();
1359
+
1360
+ const chatId = await db.chat.get('id').create(chatData);
1361
+ const ids = await db.user.pluck('id').createMany([
1362
+ {
1363
+ ...userData,
1364
+ messages: {
1365
+ create: [
1366
+ { ...messageData, chatId, text: 'message 1' },
1367
+ { ...messageData, chatId, text: 'message 2' },
1368
+ { ...messageData, chatId, text: 'message 3' },
1369
+ ],
1370
+ },
1371
+ },
1372
+ {
1373
+ ...userData,
1374
+ messages: {
1375
+ create: [
1376
+ { ...messageData, chatId, text: 'message 4' },
1377
+ { ...messageData, chatId, text: 'message 5' },
1378
+ { ...messageData, chatId, text: 'message 6' },
1379
+ ],
1380
+ },
1381
+ },
1382
+ ]);
1383
+
1384
+ await db.user.where({ id: { in: ids } }).update({
1385
+ messages: {
1386
+ delete: [
1387
+ { text: 'message 1' },
1388
+ { text: 'message 2' },
1389
+ { text: 'message 4' },
1390
+ { text: 'message 5' },
1391
+ ],
1392
+ },
1393
+ });
1394
+
1395
+ expect(beforeDelete).toHaveBeenCalledTimes(1);
1396
+ expect(afterDelete).toHaveBeenCalledTimes(1);
1397
+ });
1398
+ });
988
1399
  });
989
1400
 
990
1401
  describe('nested update', () => {
@@ -1084,6 +1495,87 @@ describe('hasMany', () => {
1084
1495
  const messages = await db.user.messages({ id }).pluck('text');
1085
1496
  expect(messages).toEqual(['message 1']);
1086
1497
  });
1498
+
1499
+ describe('relation callbacks', () => {
1500
+ const { beforeUpdate, afterUpdate, resetMocks } = useRelationCallback(
1501
+ db.user.relations.messages,
1502
+ );
1503
+
1504
+ it('should invoke callbacks', async () => {
1505
+ const chatId = await db.chat.get('id').create(chatData);
1506
+ const id = await db.user.get('id').create({
1507
+ ...userData,
1508
+ messages: {
1509
+ create: [
1510
+ { ...messageData, chatId, text: 'message 1' },
1511
+ { ...messageData, chatId, text: 'message 2' },
1512
+ { ...messageData, chatId, text: 'message 3' },
1513
+ ],
1514
+ },
1515
+ });
1516
+
1517
+ await db.user.find(id).update({
1518
+ messages: {
1519
+ update: {
1520
+ where: [{ text: 'message 1' }, { text: 'message 2' }],
1521
+ data: {
1522
+ text: 'updated',
1523
+ },
1524
+ },
1525
+ },
1526
+ });
1527
+
1528
+ expect(beforeUpdate).toHaveBeenCalledTimes(1);
1529
+ expect(afterUpdate).toHaveBeenCalledTimes(1);
1530
+ });
1531
+
1532
+ it('should invoke callbacks in a batch update', async () => {
1533
+ resetMocks();
1534
+
1535
+ const chatId = await db.chat.get('id').create(chatData);
1536
+ const ids = await db.user.pluck('id').createMany([
1537
+ {
1538
+ ...userData,
1539
+ messages: {
1540
+ create: [
1541
+ { ...messageData, chatId, text: 'message 1' },
1542
+ { ...messageData, chatId, text: 'message 2' },
1543
+ { ...messageData, chatId, text: 'message 3' },
1544
+ ],
1545
+ },
1546
+ },
1547
+ {
1548
+ ...userData,
1549
+ messages: {
1550
+ create: [
1551
+ { ...messageData, chatId, text: 'message 1' },
1552
+ { ...messageData, chatId, text: 'message 2' },
1553
+ { ...messageData, chatId, text: 'message 3' },
1554
+ ],
1555
+ },
1556
+ },
1557
+ ]);
1558
+
1559
+ await db.user.where({ id: { in: ids } }).update({
1560
+ messages: {
1561
+ update: {
1562
+ where: [
1563
+ { text: 'message 1' },
1564
+ { text: 'message 2' },
1565
+ { text: 'message 3' },
1566
+ { text: 'message 4' },
1567
+ ],
1568
+ data: {
1569
+ text: 'updated',
1570
+ },
1571
+ },
1572
+ },
1573
+ });
1574
+
1575
+ expect(beforeUpdate).toHaveBeenCalledTimes(1);
1576
+ expect(afterUpdate).toHaveBeenCalledTimes(1);
1577
+ });
1578
+ });
1087
1579
  });
1088
1580
 
1089
1581
  describe('nested create', () => {
@@ -1133,6 +1625,29 @@ describe('hasMany', () => {
1133
1625
  const messages = await db.user.messages({ id });
1134
1626
  expect(messages.length).toEqual(0);
1135
1627
  });
1628
+
1629
+ describe('relation callbacks', () => {
1630
+ const { beforeCreate, afterCreate } = useRelationCallback(
1631
+ db.user.relations.messages,
1632
+ );
1633
+
1634
+ it('should invoke callbacks', async () => {
1635
+ const chatId = await db.chat.get('id').create(chatData);
1636
+ const id = await db.user.get('id').create({ ...userData, age: 1 });
1637
+
1638
+ await db.user.find(id).update({
1639
+ messages: {
1640
+ create: [
1641
+ { ...messageData, chatId, text: 'created 1' },
1642
+ { ...messageData, chatId, text: 'created 2' },
1643
+ ],
1644
+ },
1645
+ });
1646
+
1647
+ expect(beforeCreate).toHaveBeenCalledTimes(1);
1648
+ expect(afterCreate).toHaveBeenCalledTimes(1);
1649
+ });
1650
+ });
1136
1651
  });
1137
1652
  });
1138
1653
  });
@@ -147,11 +147,9 @@ export const makeHasManyMethod = (
147
147
  if (connect.length) {
148
148
  await Promise.all(
149
149
  connect.flatMap(([selfData, { connect }]) =>
150
- connect.map((item) =>
151
- t
152
- .where<Query>(item)
153
- ._updateOrThrow({ [foreignKey]: selfData[primaryKey] }),
154
- ),
150
+ t
151
+ .or<Query>(...connect)
152
+ ._updateOrThrow({ [foreignKey]: selfData[primaryKey] }),
155
153
  ),
156
154
  );
157
155
  }