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.
@@ -6,6 +6,7 @@ import {
6
6
  messageData,
7
7
  profileData,
8
8
  userData,
9
+ useRelationCallback,
9
10
  useTestDatabase,
10
11
  } from '../test-utils/test-utils';
11
12
  import { RelationQuery } from 'pqb';
@@ -381,6 +382,35 @@ describe('belongsTo', () => {
381
382
  name: 'user 2',
382
383
  });
383
384
  });
385
+
386
+ describe('relation callbacks', () => {
387
+ const { beforeCreate, afterCreate, resetMocks } = useRelationCallback(
388
+ db.message.relations.chat,
389
+ );
390
+
391
+ const data = {
392
+ ...messageData,
393
+ chat: {
394
+ create: chatData,
395
+ },
396
+ };
397
+
398
+ it('should invoke callbacks', async () => {
399
+ await db.message.create(data);
400
+
401
+ expect(beforeCreate).toHaveBeenCalledTimes(1);
402
+ expect(afterCreate).toHaveBeenCalledTimes(1);
403
+ });
404
+
405
+ it('should invoke callbacks in a batch create', async () => {
406
+ resetMocks();
407
+
408
+ await db.message.createMany([data, data]);
409
+
410
+ expect(beforeCreate).toHaveBeenCalledTimes(1);
411
+ expect(afterCreate).toHaveBeenCalledTimes(1);
412
+ });
413
+ });
384
414
  });
385
415
 
386
416
  describe('connect', () => {
@@ -576,6 +606,38 @@ describe('belongsTo', () => {
576
606
  name: 'user 2',
577
607
  });
578
608
  });
609
+
610
+ describe('relation callbacks', () => {
611
+ const { beforeCreate, afterCreate, resetMocks } = useRelationCallback(
612
+ db.message.relations.chat,
613
+ );
614
+
615
+ const data = {
616
+ ...messageData,
617
+ chat: {
618
+ connectOrCreate: {
619
+ where: { title: 'title' },
620
+ create: chatData,
621
+ },
622
+ },
623
+ };
624
+
625
+ it('should invoke callbacks', async () => {
626
+ await db.message.create(data);
627
+
628
+ expect(beforeCreate).toHaveBeenCalledTimes(1);
629
+ expect(afterCreate).toHaveBeenCalledTimes(1);
630
+ });
631
+
632
+ it('should invoke callbacks in a batch create', async () => {
633
+ resetMocks();
634
+
635
+ await db.message.createMany([data, data]);
636
+
637
+ expect(beforeCreate).toHaveBeenCalledTimes(1);
638
+ expect(afterCreate).toHaveBeenCalledTimes(1);
639
+ });
640
+ });
579
641
  });
580
642
  });
581
643
 
@@ -733,6 +795,47 @@ describe('belongsTo', () => {
733
795
  const deletedUser = await db.user.findOptional(user.id);
734
796
  expect(deletedUser).toBe(undefined);
735
797
  });
798
+
799
+ describe('relation callbacks', () => {
800
+ const { beforeDelete, afterDelete, resetMocks } = useRelationCallback(
801
+ db.profile.relations.user,
802
+ );
803
+
804
+ const profileWithUserData = {
805
+ ...profileData,
806
+ user: {
807
+ create: userData,
808
+ },
809
+ };
810
+
811
+ const data = {
812
+ user: {
813
+ delete: true,
814
+ },
815
+ };
816
+
817
+ it('should invoke callbacks', async () => {
818
+ const id = await db.profile.get('id').create(profileWithUserData);
819
+
820
+ await db.profile.find(id).update(data);
821
+
822
+ expect(beforeDelete).toHaveBeenCalledTimes(1);
823
+ expect(afterDelete).toHaveBeenCalledTimes(1);
824
+ });
825
+
826
+ it('should invoke callbacks in a batch delete', async () => {
827
+ resetMocks();
828
+
829
+ const ids = await db.profile
830
+ .pluck('id')
831
+ .createMany([profileWithUserData, profileWithUserData]);
832
+
833
+ await db.profile.where({ id: { in: ids } }).update(data);
834
+
835
+ expect(beforeDelete).toHaveBeenCalledTimes(1);
836
+ expect(afterDelete).toHaveBeenCalledTimes(1);
837
+ });
838
+ });
736
839
  });
737
840
 
738
841
  describe('nested update', () => {
@@ -777,6 +880,49 @@ describe('belongsTo', () => {
777
880
  });
778
881
  expect(updatedNames).toEqual(['new name', 'new name']);
779
882
  });
883
+
884
+ describe('relation callbacks', () => {
885
+ const { beforeUpdate, afterUpdate, resetMocks } = useRelationCallback(
886
+ db.profile.relations.user,
887
+ );
888
+
889
+ const profileWithUserData = {
890
+ ...profileData,
891
+ user: {
892
+ create: userData,
893
+ },
894
+ };
895
+
896
+ const data = {
897
+ user: {
898
+ update: {
899
+ name: 'new name',
900
+ },
901
+ },
902
+ };
903
+
904
+ it('should invoke callbacks', async () => {
905
+ const id = await db.profile.get('id').create(profileWithUserData);
906
+
907
+ await db.profile.find(id).update(data);
908
+
909
+ expect(beforeUpdate).toHaveBeenCalledTimes(1);
910
+ expect(afterUpdate).toHaveBeenCalledTimes(1);
911
+ });
912
+
913
+ it('should invoke callbacks in a batch create', async () => {
914
+ resetMocks();
915
+
916
+ const ids = await db.profile
917
+ .pluck('id')
918
+ .createMany([profileWithUserData, profileWithUserData]);
919
+
920
+ await db.profile.where({ id: { in: ids } }).update(data);
921
+
922
+ expect(beforeUpdate).toHaveBeenCalledTimes(1);
923
+ expect(afterUpdate).toHaveBeenCalledTimes(1);
924
+ });
925
+ });
780
926
  });
781
927
 
782
928
  describe('nested upsert', () => {
@@ -845,6 +991,58 @@ describe('belongsTo', () => {
845
991
  }),
846
992
  ).toThrow();
847
993
  });
994
+
995
+ describe('relation callbacks', () => {
996
+ const {
997
+ beforeUpdate,
998
+ afterUpdate,
999
+ beforeCreate,
1000
+ afterCreate,
1001
+ resetMocks,
1002
+ } = useRelationCallback(db.profile.relations.user);
1003
+
1004
+ const data = {
1005
+ user: {
1006
+ upsert: {
1007
+ update: {
1008
+ name: 'new name',
1009
+ },
1010
+ create: userData,
1011
+ },
1012
+ },
1013
+ };
1014
+
1015
+ it('should invoke update callbacks when updating', async () => {
1016
+ const id = await db.profile.get('id').create({
1017
+ ...profileData,
1018
+ user: {
1019
+ create: userData,
1020
+ },
1021
+ });
1022
+
1023
+ resetMocks();
1024
+
1025
+ await db.profile.find(id).update(data);
1026
+
1027
+ expect(beforeUpdate).toHaveBeenCalledTimes(1);
1028
+ expect(afterUpdate).toHaveBeenCalledTimes(1);
1029
+ expect(beforeCreate).not.toBeCalled();
1030
+ expect(afterCreate).not.toBeCalled();
1031
+ });
1032
+
1033
+ it('should invoke create callbacks when creating', async () => {
1034
+ resetMocks();
1035
+
1036
+ const id = await db.profile.get('id').create(profileData);
1037
+
1038
+ await db.profile.find(id).update(data);
1039
+
1040
+ expect(beforeUpdate).not.toBeCalled();
1041
+ expect(afterUpdate).not.toBeCalled();
1042
+ expect(beforeCreate).toHaveBeenCalledTimes(1);
1043
+ expect(afterCreate).toHaveBeenCalledTimes(1);
1044
+ });
1045
+ });
848
1046
  });
849
1047
 
850
1048
  describe('nested create', () => {
@@ -885,6 +1083,40 @@ describe('belongsTo', () => {
885
1083
  const user = await db.user.find(updatedUserIds[0] as number);
886
1084
  expect(user.name).toBe('created');
887
1085
  });
1086
+
1087
+ describe('relation callbacks', () => {
1088
+ const { beforeCreate, afterCreate, resetMocks } = useRelationCallback(
1089
+ db.profile.relations.user,
1090
+ );
1091
+
1092
+ const data = {
1093
+ user: {
1094
+ create: userData,
1095
+ },
1096
+ };
1097
+
1098
+ it('should invoke callbacks', async () => {
1099
+ const id = await db.profile.get('id').create(profileData);
1100
+
1101
+ await db.profile.find(id).update(data);
1102
+
1103
+ expect(beforeCreate).toHaveBeenCalledTimes(1);
1104
+ expect(afterCreate).toHaveBeenCalledTimes(1);
1105
+ });
1106
+
1107
+ it('should invoke callbacks in a batch update', async () => {
1108
+ const ids = await db.profile
1109
+ .pluck('id')
1110
+ .createMany([profileData, profileData]);
1111
+
1112
+ resetMocks();
1113
+
1114
+ await db.profile.where({ id: { in: ids } }).update(data);
1115
+
1116
+ expect(beforeCreate).toHaveBeenCalledTimes(1);
1117
+ expect(afterCreate).toHaveBeenCalledTimes(1);
1118
+ });
1119
+ });
888
1120
  });
889
1121
  });
890
1122
  });
@@ -6,6 +6,7 @@ import {
6
6
  useTestDatabase,
7
7
  now,
8
8
  assertType,
9
+ useRelationCallback,
9
10
  } from '../test-utils/test-utils';
10
11
  import { RelationQuery, Sql, TransactionAdapter } from 'pqb';
11
12
  import { Chat, User } from '../test-utils/test-models';
@@ -107,11 +108,10 @@ describe('hasAndBelongsToMany', () => {
107
108
  });
108
109
 
109
110
  it('should throw when the main query returns many records', async () => {
110
- await expect(
111
- async () =>
112
- await db.user.chats.create({
113
- title: 'title',
114
- }),
111
+ await expect(() =>
112
+ db.user.chats.create({
113
+ title: 'title',
114
+ }),
115
115
  ).rejects.toThrow(
116
116
  'Cannot create based on a query which returns multiple records',
117
117
  );
@@ -584,6 +584,35 @@ describe('hasAndBelongsToMany', () => {
584
584
  },
585
585
  });
586
586
  });
587
+
588
+ describe('relation callbacks', () => {
589
+ const { beforeCreate, afterCreate, resetMocks } = useRelationCallback(
590
+ db.user.relations.chats,
591
+ );
592
+
593
+ const data = {
594
+ ...userData,
595
+ chats: {
596
+ create: [chatData, chatData],
597
+ },
598
+ };
599
+
600
+ it('should invoke callbacks', async () => {
601
+ await db.user.create(data);
602
+
603
+ expect(beforeCreate).toHaveBeenCalledTimes(1);
604
+ expect(afterCreate).toHaveBeenCalledTimes(1);
605
+ });
606
+
607
+ it('should invoke callbacks in a batch create', async () => {
608
+ resetMocks();
609
+
610
+ await db.user.createMany([data, data]);
611
+
612
+ expect(beforeCreate).toHaveBeenCalledTimes(1);
613
+ expect(afterCreate).toHaveBeenCalledTimes(1);
614
+ });
615
+ });
587
616
  });
588
617
 
589
618
  describe('nested connect', () => {
@@ -872,6 +901,44 @@ describe('hasAndBelongsToMany', () => {
872
901
  },
873
902
  });
874
903
  });
904
+
905
+ describe('relation callbacks', () => {
906
+ const { beforeCreate, afterCreate, resetMocks } = useRelationCallback(
907
+ db.user.relations.chats,
908
+ );
909
+
910
+ const data = {
911
+ ...userData,
912
+ chats: {
913
+ connectOrCreate: [
914
+ {
915
+ where: { title: 'one' },
916
+ create: chatData,
917
+ },
918
+ {
919
+ where: { title: 'two' },
920
+ create: chatData,
921
+ },
922
+ ],
923
+ },
924
+ };
925
+
926
+ it('should invoke callbacks', async () => {
927
+ await db.user.create(data);
928
+
929
+ expect(beforeCreate).toHaveBeenCalledTimes(1);
930
+ expect(afterCreate).toHaveBeenCalledTimes(1);
931
+ });
932
+
933
+ it('should invoke callbacks in a batch create', async () => {
934
+ resetMocks();
935
+
936
+ await db.user.createMany([data, data]);
937
+
938
+ expect(beforeCreate).toHaveBeenCalledTimes(1);
939
+ expect(afterCreate).toHaveBeenCalledTimes(1);
940
+ });
941
+ });
875
942
  });
876
943
  });
877
944
 
@@ -1000,6 +1067,65 @@ describe('hasAndBelongsToMany', () => {
1000
1067
  const chats = await db.user.chats({ id }).pluck('title');
1001
1068
  expect(chats).toEqual(['chat 1']);
1002
1069
  });
1070
+
1071
+ describe('relation callbacks', () => {
1072
+ const { beforeDelete, afterDelete, resetMocks } = useRelationCallback(
1073
+ db.user.relations.chats,
1074
+ );
1075
+
1076
+ const data = {
1077
+ chats: {
1078
+ delete: [{ title: 'chat 1' }, { title: 'chat 2' }],
1079
+ },
1080
+ };
1081
+
1082
+ it('should invoke callbacks', async () => {
1083
+ const id = await db.user.get('id').create({
1084
+ ...userData,
1085
+ chats: {
1086
+ create: [
1087
+ { ...chatData, title: 'chat 1' },
1088
+ { ...chatData, title: 'chat 2' },
1089
+ ],
1090
+ },
1091
+ });
1092
+
1093
+ await db.user.find(id).update(data);
1094
+
1095
+ expect(beforeDelete).toHaveBeenCalledTimes(1);
1096
+ expect(afterDelete).toHaveBeenCalledTimes(1);
1097
+ });
1098
+
1099
+ it('should invoke callbacks in a batch update', async () => {
1100
+ resetMocks();
1101
+
1102
+ const ids = await db.user.pluck('id').createMany([
1103
+ {
1104
+ ...userData,
1105
+ chats: {
1106
+ create: [
1107
+ { ...chatData, title: 'chat 1' },
1108
+ { ...chatData, title: 'chat 3' },
1109
+ ],
1110
+ },
1111
+ },
1112
+ {
1113
+ ...userData,
1114
+ chats: {
1115
+ create: [
1116
+ { ...chatData, title: 'chat 2' },
1117
+ { ...chatData, title: 'chat 4' },
1118
+ ],
1119
+ },
1120
+ },
1121
+ ]);
1122
+
1123
+ await db.user.where({ id: { in: ids } }).update(data);
1124
+
1125
+ expect(beforeDelete).toHaveBeenCalledTimes(1);
1126
+ expect(afterDelete).toHaveBeenCalledTimes(1);
1127
+ });
1128
+ });
1003
1129
  });
1004
1130
 
1005
1131
  describe('nested update', () => {
@@ -1061,6 +1187,59 @@ describe('hasAndBelongsToMany', () => {
1061
1187
  const chats = await db.user.chats({ id }).pluck('title');
1062
1188
  expect(chats).toEqual(['chat 1']);
1063
1189
  });
1190
+
1191
+ describe('relation callbacks', () => {
1192
+ const { beforeUpdate, afterUpdate, resetMocks } = useRelationCallback(
1193
+ db.user.relations.chats,
1194
+ );
1195
+
1196
+ const data = {
1197
+ chats: {
1198
+ update: {
1199
+ where: [{ title: 'chat 1' }, { title: 'chat 2' }],
1200
+ data: { title: 'new title' },
1201
+ },
1202
+ },
1203
+ };
1204
+
1205
+ it('should invoke callbacks', async () => {
1206
+ const id = await db.user.get('id').create({
1207
+ ...userData,
1208
+ chats: {
1209
+ create: [{ ...chatData, title: 'chat 1' }],
1210
+ },
1211
+ });
1212
+
1213
+ await db.user.find(id).update(data);
1214
+
1215
+ expect(beforeUpdate).toHaveBeenCalledTimes(1);
1216
+ expect(afterUpdate).toHaveBeenCalledTimes(1);
1217
+ });
1218
+
1219
+ it('should invoke callbacks in a batch update', async () => {
1220
+ const id = await db.user.get('id').createMany([
1221
+ {
1222
+ ...userData,
1223
+ chats: {
1224
+ create: [{ ...chatData, title: 'chat 1' }],
1225
+ },
1226
+ },
1227
+ {
1228
+ ...userData,
1229
+ chats: {
1230
+ create: [{ ...chatData, title: 'chat 2' }],
1231
+ },
1232
+ },
1233
+ ]);
1234
+
1235
+ resetMocks();
1236
+
1237
+ await db.user.find(id).update(data);
1238
+
1239
+ expect(beforeUpdate).toHaveBeenCalledTimes(1);
1240
+ expect(afterUpdate).toHaveBeenCalledTimes(1);
1241
+ });
1242
+ });
1064
1243
  });
1065
1244
 
1066
1245
  describe('nested create', () => {
@@ -1117,6 +1296,40 @@ describe('hasAndBelongsToMany', () => {
1117
1296
  const chats = await db.user.chats({ id });
1118
1297
  expect(chats).toEqual([]);
1119
1298
  });
1299
+
1300
+ describe('relation callbacks', () => {
1301
+ const { beforeCreate, afterCreate, resetMocks } = useRelationCallback(
1302
+ db.user.relations.chats,
1303
+ );
1304
+
1305
+ const data = {
1306
+ chats: {
1307
+ create: [chatData, chatData],
1308
+ },
1309
+ };
1310
+
1311
+ it('should invoke callbacks', async () => {
1312
+ const id = await db.user.get('id').create(userData);
1313
+
1314
+ await db.user.find(id).update(data);
1315
+
1316
+ expect(beforeCreate).toHaveBeenCalledTimes(1);
1317
+ expect(afterCreate).toHaveBeenCalledTimes(1);
1318
+ });
1319
+
1320
+ it('should invoke callbacks in a batch update', async () => {
1321
+ const ids = await db.user
1322
+ .pluck('id')
1323
+ .createMany([userData, userData]);
1324
+
1325
+ resetMocks();
1326
+
1327
+ await db.user.where({ id: { in: ids } }).update(data);
1328
+
1329
+ expect(beforeCreate).toHaveBeenCalledTimes(1);
1330
+ expect(afterCreate).toHaveBeenCalledTimes(1);
1331
+ });
1332
+ });
1120
1333
  });
1121
1334
  });
1122
1335
  });