orchid-orm 0.0.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.
@@ -0,0 +1,890 @@
1
+ import { db } from '../test-utils/test-db';
2
+ import {
3
+ assertType,
4
+ chatData,
5
+ expectSql,
6
+ messageData,
7
+ profileData,
8
+ userData,
9
+ useTestDatabase,
10
+ } from '../test-utils/test-utils';
11
+ import { RelationQuery } from 'pqb';
12
+ import { User } from '../test-utils/test-models';
13
+
14
+ describe('belongsTo', () => {
15
+ useTestDatabase();
16
+
17
+ describe('querying', () => {
18
+ it('should have method to query related data', async () => {
19
+ const userQuery = db.user.take();
20
+ type UserQuery = typeof userQuery;
21
+
22
+ assertType<
23
+ typeof db.profile.user,
24
+ RelationQuery<'user', { userId: number | null }, never, UserQuery, true>
25
+ >();
26
+
27
+ const userId = await db.user.get('id').create(userData);
28
+ const profileId = await db.profile
29
+ .get('id')
30
+ .create({ ...profileData, userId });
31
+
32
+ const profile = await db.profile.find(profileId);
33
+ const query = db.profile.user(profile);
34
+
35
+ expectSql(
36
+ query.toSql(),
37
+ `
38
+ SELECT * FROM "user"
39
+ WHERE "user"."id" = $1
40
+ LIMIT $2
41
+ `,
42
+ [userId, 1],
43
+ );
44
+
45
+ const user = await query;
46
+
47
+ expect(user).toMatchObject(userData);
48
+ });
49
+
50
+ it('should handle chained query', () => {
51
+ const query = db.profile
52
+ .where({ bio: 'bio' })
53
+ .user.where({ name: 'name' });
54
+
55
+ expectSql(
56
+ query.toSql(),
57
+ `
58
+ SELECT * FROM "user"
59
+ WHERE EXISTS (
60
+ SELECT 1 FROM "profile"
61
+ WHERE "profile"."bio" = $1
62
+ AND "profile"."userId" = "user"."id"
63
+ LIMIT 1
64
+ )
65
+ AND "user"."name" = $2
66
+ LIMIT $3
67
+ `,
68
+ ['bio', 'name', 1],
69
+ );
70
+ });
71
+
72
+ it('should have disabled create and delete method', () => {
73
+ // @ts-expect-error belongsTo should not have chained create
74
+ db.profile.user.create(userData);
75
+
76
+ // @ts-expect-error belongsTo should not have chained create
77
+ db.profile.user.find(1).delete();
78
+ });
79
+
80
+ it('should have proper joinQuery', () => {
81
+ expectSql(
82
+ db.profile.relations.user
83
+ .joinQuery(db.profile.as('p'), db.user.as('u'))
84
+ .toSql(),
85
+ `
86
+ SELECT * FROM "user" AS "u"
87
+ WHERE "u"."id" = "p"."userId"
88
+ `,
89
+ );
90
+ });
91
+
92
+ it('should be supported in whereExists', () => {
93
+ expectSql(
94
+ db.profile.whereExists('user').toSql(),
95
+ `
96
+ SELECT * FROM "profile"
97
+ WHERE EXISTS (
98
+ SELECT 1 FROM "user"
99
+ WHERE "user"."id" = "profile"."userId"
100
+ LIMIT 1
101
+ )
102
+ `,
103
+ );
104
+
105
+ expectSql(
106
+ db.profile
107
+ .as('p')
108
+ .whereExists('user', (q) => q.where({ name: 'name' }))
109
+ .toSql(),
110
+ `
111
+ SELECT * FROM "profile" AS "p"
112
+ WHERE EXISTS (
113
+ SELECT 1 FROM "user"
114
+ WHERE "user"."id" = "p"."userId"
115
+ AND "user"."name" = $1
116
+ LIMIT 1
117
+ )
118
+ `,
119
+ ['name'],
120
+ );
121
+ });
122
+
123
+ it('should support nested whereExists', () => {
124
+ expectSql(
125
+ db.message
126
+ .as('m')
127
+ .whereExists('user', (q) =>
128
+ q.whereExists('profile', (q) => q.where({ bio: 'bio' })),
129
+ )
130
+ .toSql(),
131
+ `
132
+ SELECT * FROM "message" AS "m"
133
+ WHERE EXISTS (
134
+ SELECT 1 FROM "user"
135
+ WHERE "user"."id" = "m"."authorId"
136
+ AND EXISTS (
137
+ SELECT 1 FROM "profile"
138
+ WHERE "profile"."userId" = "user"."id"
139
+ AND "profile"."bio" = $1
140
+ LIMIT 1
141
+ )
142
+ LIMIT 1
143
+ )
144
+ `,
145
+ ['bio'],
146
+ );
147
+ });
148
+
149
+ it('should be supported in join', () => {
150
+ const query = db.profile
151
+ .as('p')
152
+ .join('user', (q) => q.where({ name: 'name' }))
153
+ .select('bio', 'user.name');
154
+
155
+ assertType<
156
+ Awaited<typeof query>,
157
+ { bio: string | null; name: string }[]
158
+ >();
159
+
160
+ expectSql(
161
+ query.toSql(),
162
+ `
163
+ SELECT "p"."bio", "user"."name"
164
+ FROM "profile" AS "p"
165
+ JOIN "user" ON "user"."id" = "p"."userId" AND "user"."name" = $1
166
+ `,
167
+ ['name'],
168
+ );
169
+ });
170
+
171
+ describe('select', () => {
172
+ it('should be selectable', async () => {
173
+ const query = db.profile.as('p').select('id', {
174
+ user: (q) => q.user.select('id', 'name').where({ name: 'name' }),
175
+ });
176
+
177
+ assertType<
178
+ Awaited<typeof query>,
179
+ { id: number; user: { id: number; name: string } }[]
180
+ >();
181
+
182
+ expectSql(
183
+ query.toSql(),
184
+ `
185
+ SELECT
186
+ "p"."id",
187
+ (
188
+ SELECT row_to_json("t".*)
189
+ FROM (
190
+ SELECT "user"."id", "user"."name" FROM "user"
191
+ WHERE "user"."name" = $1
192
+ AND "user"."id" = "p"."userId"
193
+ LIMIT $2
194
+ ) AS "t"
195
+ ) AS "user"
196
+ FROM "profile" AS "p"
197
+ `,
198
+ ['name', 1],
199
+ );
200
+ });
201
+
202
+ it('should be selectable by relation name', async () => {
203
+ const query = db.profile.select('id', 'user');
204
+
205
+ assertType<Awaited<typeof query>, { id: number; user: User }[]>();
206
+
207
+ expectSql(
208
+ query.toSql(),
209
+ `
210
+ SELECT
211
+ "profile"."id",
212
+ (
213
+ SELECT row_to_json("t".*)
214
+ FROM (
215
+ SELECT * FROM "user"
216
+ WHERE "user"."id" = "profile"."userId"
217
+ LIMIT $1
218
+ ) AS "t"
219
+ ) AS "user"
220
+ FROM "profile"
221
+ `,
222
+ [1],
223
+ );
224
+ });
225
+
226
+ it('should handle exists sub query', async () => {
227
+ const query = db.profile.as('p').select('id', {
228
+ hasUser: (q) => q.user.exists(),
229
+ });
230
+
231
+ assertType<Awaited<typeof query>, { id: number; hasUser: boolean }[]>();
232
+
233
+ expectSql(
234
+ query.toSql(),
235
+ `
236
+ SELECT
237
+ "p"."id",
238
+ COALESCE((
239
+ SELECT true
240
+ FROM "user"
241
+ WHERE "user"."id" = "p"."userId"
242
+ ), false) AS "hasUser"
243
+ FROM "profile" AS "p"
244
+ `,
245
+ );
246
+ });
247
+ });
248
+ });
249
+
250
+ describe('create', () => {
251
+ const checkCreatedResults = async ({
252
+ messageId,
253
+ chatId,
254
+ authorId,
255
+ text,
256
+ title,
257
+ name,
258
+ }: {
259
+ messageId: number;
260
+ chatId: number;
261
+ authorId: number | null;
262
+ text: string;
263
+ title: string;
264
+ name: string;
265
+ }) => {
266
+ const message = await db.message.find(messageId);
267
+ expect(message).toEqual({
268
+ ...message,
269
+ ...messageData,
270
+ chatId,
271
+ authorId,
272
+ text,
273
+ });
274
+
275
+ const chat = await db.chat.find(chatId);
276
+ expect(chat).toEqual({
277
+ ...chat,
278
+ ...chatData,
279
+ title,
280
+ });
281
+
282
+ if (!authorId) return;
283
+ const user = await db.user.find(authorId);
284
+ expect(user).toEqual({
285
+ ...user,
286
+ ...userData,
287
+ active: null,
288
+ age: null,
289
+ data: null,
290
+ picture: null,
291
+ name,
292
+ });
293
+ };
294
+
295
+ describe('nested create', () => {
296
+ it('should support create', async () => {
297
+ const {
298
+ id: messageId,
299
+ chatId,
300
+ authorId,
301
+ } = await db.message.select('id', 'chatId', 'authorId').create({
302
+ ...messageData,
303
+ text: 'message',
304
+ chat: {
305
+ create: {
306
+ ...chatData,
307
+ title: 'chat',
308
+ },
309
+ },
310
+ user: {
311
+ create: {
312
+ ...userData,
313
+ name: 'user',
314
+ },
315
+ },
316
+ });
317
+
318
+ await checkCreatedResults({
319
+ messageId,
320
+ chatId,
321
+ authorId,
322
+ text: 'message',
323
+ title: 'chat',
324
+ name: 'user',
325
+ });
326
+ });
327
+
328
+ it('should support create in batch create', async () => {
329
+ const query = db.message.select('id', 'chatId', 'authorId').createMany([
330
+ {
331
+ ...messageData,
332
+ text: 'message 1',
333
+ chat: {
334
+ create: {
335
+ ...chatData,
336
+ title: 'chat 1',
337
+ },
338
+ },
339
+ user: {
340
+ create: {
341
+ ...userData,
342
+ name: 'user 1',
343
+ },
344
+ },
345
+ },
346
+ {
347
+ ...messageData,
348
+ text: 'message 2',
349
+ chat: {
350
+ create: {
351
+ ...chatData,
352
+ title: 'chat 2',
353
+ },
354
+ },
355
+ user: {
356
+ create: {
357
+ ...userData,
358
+ name: 'user 2',
359
+ },
360
+ },
361
+ },
362
+ ]);
363
+
364
+ const [first, second] = await query;
365
+
366
+ await checkCreatedResults({
367
+ messageId: first.id,
368
+ chatId: first.chatId,
369
+ authorId: first.authorId,
370
+ text: 'message 1',
371
+ title: 'chat 1',
372
+ name: 'user 1',
373
+ });
374
+
375
+ await checkCreatedResults({
376
+ messageId: second.id,
377
+ chatId: second.chatId,
378
+ authorId: second.authorId,
379
+ text: 'message 2',
380
+ title: 'chat 2',
381
+ name: 'user 2',
382
+ });
383
+ });
384
+ });
385
+
386
+ describe('connect', () => {
387
+ it('should support connect', async () => {
388
+ await db.chat.create({ ...chatData, title: 'chat' });
389
+ await db.user.create({ ...userData, name: 'user' });
390
+
391
+ const query = db.message.select('id', 'chatId', 'authorId').create({
392
+ ...messageData,
393
+ text: 'message',
394
+ chat: {
395
+ connect: { title: 'chat' },
396
+ },
397
+ user: {
398
+ connect: { name: 'user' },
399
+ },
400
+ });
401
+
402
+ const { id: messageId, chatId, authorId } = await query;
403
+
404
+ await checkCreatedResults({
405
+ messageId,
406
+ chatId,
407
+ authorId,
408
+ text: 'message',
409
+ title: 'chat',
410
+ name: 'user',
411
+ });
412
+ });
413
+
414
+ it('should support connect in batch create', async () => {
415
+ await db.chat.createMany([
416
+ { ...chatData, title: 'chat 1' },
417
+ { ...chatData, title: 'chat 2' },
418
+ ]);
419
+ await db.user.createMany([
420
+ { ...userData, name: 'user 1' },
421
+ { ...userData, name: 'user 2' },
422
+ ]);
423
+
424
+ const query = db.message.select('id', 'chatId', 'authorId').createMany([
425
+ {
426
+ ...messageData,
427
+ text: 'message 1',
428
+ chat: {
429
+ connect: { title: 'chat 1' },
430
+ },
431
+ user: {
432
+ connect: { name: 'user 1' },
433
+ },
434
+ },
435
+ {
436
+ ...messageData,
437
+ text: 'message 2',
438
+ chat: {
439
+ connect: { title: 'chat 2' },
440
+ },
441
+ user: {
442
+ connect: { name: 'user 2' },
443
+ },
444
+ },
445
+ ]);
446
+
447
+ const [first, second] = await query;
448
+
449
+ await checkCreatedResults({
450
+ messageId: first.id,
451
+ chatId: first.chatId,
452
+ authorId: first.authorId,
453
+ text: 'message 1',
454
+ title: 'chat 1',
455
+ name: 'user 1',
456
+ });
457
+
458
+ await checkCreatedResults({
459
+ messageId: second.id,
460
+ chatId: second.chatId,
461
+ authorId: second.authorId,
462
+ text: 'message 2',
463
+ title: 'chat 2',
464
+ name: 'user 2',
465
+ });
466
+ });
467
+ });
468
+
469
+ describe('connectOrCreate', () => {
470
+ it('should support connect or create', async () => {
471
+ const chat = await db.chat.select('id').create({
472
+ ...chatData,
473
+ title: 'chat',
474
+ });
475
+
476
+ const query = await db.message
477
+ .select('id', 'chatId', 'authorId')
478
+ .create({
479
+ ...messageData,
480
+ text: 'message',
481
+ chat: {
482
+ connectOrCreate: {
483
+ where: { title: 'chat' },
484
+ create: { ...chatData, title: 'chat' },
485
+ },
486
+ },
487
+ user: {
488
+ connectOrCreate: {
489
+ where: { name: 'user' },
490
+ create: { ...userData, name: 'user' },
491
+ },
492
+ },
493
+ });
494
+
495
+ const { id: messageId, chatId, authorId } = await query;
496
+
497
+ expect(chatId).toBe(chat.id);
498
+
499
+ await checkCreatedResults({
500
+ messageId,
501
+ chatId,
502
+ authorId,
503
+ text: 'message',
504
+ title: 'chat',
505
+ name: 'user',
506
+ });
507
+ });
508
+
509
+ it('should support connect or create in batch create', async () => {
510
+ const chat = await db.chat.select('id').create({
511
+ ...chatData,
512
+ title: 'chat 1',
513
+ });
514
+ const user = await db.user.select('id').create({
515
+ ...userData,
516
+ name: 'user 2',
517
+ });
518
+
519
+ const query = await db.message
520
+ .select('id', 'chatId', 'authorId')
521
+ .createMany([
522
+ {
523
+ ...messageData,
524
+ text: 'message 1',
525
+ chat: {
526
+ connectOrCreate: {
527
+ where: { title: 'chat 1' },
528
+ create: { ...chatData, title: 'chat 1' },
529
+ },
530
+ },
531
+ user: {
532
+ connectOrCreate: {
533
+ where: { name: 'user 1' },
534
+ create: { ...userData, name: 'user 1' },
535
+ },
536
+ },
537
+ },
538
+ {
539
+ ...messageData,
540
+ text: 'message 2',
541
+ chat: {
542
+ connectOrCreate: {
543
+ where: { title: 'chat 2' },
544
+ create: { ...chatData, title: 'chat 2' },
545
+ },
546
+ },
547
+ user: {
548
+ connectOrCreate: {
549
+ where: { name: 'user 2' },
550
+ create: { ...userData, name: 'user 2' },
551
+ },
552
+ },
553
+ },
554
+ ]);
555
+
556
+ const [first, second] = await query;
557
+
558
+ expect(first.chatId).toBe(chat.id);
559
+ expect(second.authorId).toBe(user.id);
560
+
561
+ await checkCreatedResults({
562
+ messageId: first.id,
563
+ chatId: first.chatId,
564
+ authorId: first.authorId,
565
+ text: 'message 1',
566
+ title: 'chat 1',
567
+ name: 'user 1',
568
+ });
569
+
570
+ await checkCreatedResults({
571
+ messageId: second.id,
572
+ chatId: second.chatId,
573
+ authorId: second.authorId,
574
+ text: 'message 2',
575
+ title: 'chat 2',
576
+ name: 'user 2',
577
+ });
578
+ });
579
+ });
580
+ });
581
+
582
+ describe('update', () => {
583
+ describe('disconnect', () => {
584
+ it('should nullify foreignKey', async () => {
585
+ const id = await db.profile
586
+ .get('id')
587
+ .create({ ...profileData, user: { create: userData } });
588
+
589
+ const profile = await db.profile
590
+ .select('userId')
591
+ .find(id)
592
+ .update({
593
+ bio: 'string',
594
+ user: { disconnect: true },
595
+ });
596
+
597
+ expect(profile.userId).toBe(null);
598
+ });
599
+
600
+ it('should nullify foreignKey in batch update', async () => {
601
+ const ids = await db.profile.pluck('id').createMany([
602
+ { ...profileData, user: { create: userData } },
603
+ { ...profileData, user: { create: userData } },
604
+ ]);
605
+
606
+ const userIds = await db.profile
607
+ .pluck('userId')
608
+ .where({ id: { in: ids } })
609
+ .update({
610
+ bio: 'string',
611
+ user: { disconnect: true },
612
+ });
613
+
614
+ expect(userIds).toEqual([null, null]);
615
+ });
616
+ });
617
+
618
+ describe('set', () => {
619
+ it('should set foreignKey of current record with provided primaryKey', async () => {
620
+ const id = await db.profile.get('id').create(profileData);
621
+ const user = await db.user.select('id').create(userData);
622
+
623
+ const profile = await db.profile
624
+ .selectAll()
625
+ .find(id)
626
+ .update({
627
+ user: {
628
+ set: user,
629
+ },
630
+ });
631
+
632
+ expect(profile.userId).toBe(user.id);
633
+ });
634
+
635
+ it('should set foreignKey of current record from found related record', async () => {
636
+ const id = await db.profile.get('id').create(profileData);
637
+ const user = await db.user.select('id').create({
638
+ ...userData,
639
+ name: 'user',
640
+ });
641
+
642
+ const profile = await db.profile
643
+ .selectAll()
644
+ .find(id)
645
+ .update({
646
+ user: {
647
+ set: { name: 'user' },
648
+ },
649
+ });
650
+
651
+ expect(profile.userId).toBe(user.id);
652
+ });
653
+
654
+ it('should set foreignKey of current record with provided primaryKey in batch update', async () => {
655
+ const profileIds = await db.profile
656
+ .pluck('id')
657
+ .createMany([profileData, profileData]);
658
+ const user = await db.user.select('id').create(userData);
659
+
660
+ const updatedUserIds = await db.profile
661
+ .pluck('userId')
662
+ .where({ id: { in: profileIds } })
663
+ .update({
664
+ user: {
665
+ set: user,
666
+ },
667
+ });
668
+
669
+ expect(updatedUserIds).toEqual([user.id, user.id]);
670
+ });
671
+
672
+ it('should set foreignKey of current record from found related record in batch update', async () => {
673
+ const profileIds = await db.profile
674
+ .pluck('id')
675
+ .createMany([profileData, profileData]);
676
+ const user = await db.user.select('id').create({
677
+ ...userData,
678
+ name: 'user',
679
+ });
680
+
681
+ const updatedUserIds = await db.profile
682
+ .pluck('userId')
683
+ .where({ id: { in: profileIds } })
684
+ .update({
685
+ user: {
686
+ set: { name: 'user' },
687
+ },
688
+ });
689
+
690
+ expect(updatedUserIds).toEqual([user.id, user.id]);
691
+ });
692
+ });
693
+
694
+ describe('delete', () => {
695
+ it('should nullify foreignKey and delete related record', async () => {
696
+ const { id, userId } = await db.profile
697
+ .select('id', 'userId')
698
+ .create({ ...profileData, user: { create: userData } });
699
+
700
+ const profile = await db.profile
701
+ .select('userId')
702
+ .find(id)
703
+ .update({
704
+ user: {
705
+ delete: true,
706
+ },
707
+ });
708
+
709
+ expect(profile.userId).toBe(null);
710
+
711
+ const user = await db.user.findByOptional({ id: userId });
712
+ expect(user).toBe(undefined);
713
+ });
714
+
715
+ it('should nullify foreignKey and delete related record in batch update', async () => {
716
+ const user = await db.user.selectAll().create(userData);
717
+ const profileIds = await db.profile.pluck('id').createMany([
718
+ { ...profileData, userId: user.id },
719
+ { ...profileData, userId: user.id },
720
+ ]);
721
+
722
+ const updatedUserIds = await db.profile
723
+ .pluck('userId')
724
+ .where({ id: { in: profileIds } })
725
+ .update({
726
+ user: {
727
+ delete: true,
728
+ },
729
+ });
730
+
731
+ expect(updatedUserIds).toEqual([null, null]);
732
+
733
+ const deletedUser = await db.user.findOptional(user.id);
734
+ expect(deletedUser).toBe(undefined);
735
+ });
736
+ });
737
+
738
+ describe('nested update', () => {
739
+ it('should update related record', async () => {
740
+ const { id, userId } = await db.profile
741
+ .select('id', 'userId')
742
+ .create({ ...profileData, user: { create: userData } });
743
+
744
+ await db.profile
745
+ .select('userId')
746
+ .find(id)
747
+ .update({
748
+ user: {
749
+ update: {
750
+ name: 'new name',
751
+ },
752
+ },
753
+ });
754
+
755
+ const user = await db.user.findBy({ id: userId });
756
+ expect(user.name).toBe('new name');
757
+ });
758
+
759
+ it('should update related records in batch update', async () => {
760
+ const profiles = await db.profile.select('id', 'userId').createMany([
761
+ { ...profileData, user: { create: userData } },
762
+ { ...profileData, user: { create: userData } },
763
+ ]);
764
+
765
+ await db.profile
766
+ .where({ id: { in: profiles.map((profile) => profile.id) } })
767
+ .update({
768
+ user: {
769
+ update: {
770
+ name: 'new name',
771
+ },
772
+ },
773
+ });
774
+
775
+ const updatedNames = await db.user.pluck('name').where({
776
+ id: { in: profiles.map((profile) => profile.userId as number) },
777
+ });
778
+ expect(updatedNames).toEqual(['new name', 'new name']);
779
+ });
780
+ });
781
+
782
+ describe('nested upsert', () => {
783
+ it('should update related record if it exists', async () => {
784
+ const profile = await db.profile.create({
785
+ ...profileData,
786
+ user: {
787
+ create: userData,
788
+ },
789
+ });
790
+
791
+ await db.profile.find(profile.id).update({
792
+ user: {
793
+ upsert: {
794
+ update: {
795
+ name: 'updated',
796
+ },
797
+ create: userData,
798
+ },
799
+ },
800
+ });
801
+
802
+ const user = await db.profile.user(profile);
803
+ expect(user.name).toBe('updated');
804
+ });
805
+
806
+ it('should create related record if it does not exist', async () => {
807
+ const profile = await db.profile.create(profileData);
808
+
809
+ const updated = await db.profile
810
+ .selectAll()
811
+ .find(profile.id)
812
+ .update({
813
+ user: {
814
+ upsert: {
815
+ update: {
816
+ name: 'updated',
817
+ },
818
+ create: {
819
+ ...userData,
820
+ name: 'created',
821
+ },
822
+ },
823
+ },
824
+ });
825
+
826
+ const user = await db.profile.user(updated);
827
+ expect(user.name).toBe('created');
828
+ });
829
+
830
+ it('should throw in batch update', () => {
831
+ expect(() =>
832
+ db.profile.where({ id: 1 }).update({
833
+ user: {
834
+ // @ts-expect-error not allows in batch update
835
+ upsert: {
836
+ update: {
837
+ name: 'updated',
838
+ },
839
+ create: {
840
+ ...userData,
841
+ name: 'created',
842
+ },
843
+ },
844
+ },
845
+ }),
846
+ ).toThrow();
847
+ });
848
+ });
849
+
850
+ describe('nested create', () => {
851
+ it('should create new related record and update foreignKey', async () => {
852
+ const profileId = await db.profile
853
+ .get('id')
854
+ .create({ ...profileData, user: { create: userData } });
855
+
856
+ const updated = await db.profile
857
+ .selectAll()
858
+ .find(profileId)
859
+ .update({
860
+ user: {
861
+ create: { ...userData, name: 'created' },
862
+ },
863
+ });
864
+
865
+ const user = await db.profile.user(updated);
866
+ expect(user.name).toBe('created');
867
+ });
868
+
869
+ it('should create new related record and update foreignKey in batch update', async () => {
870
+ const profileIds = await db.profile
871
+ .pluck('id')
872
+ .createMany([profileData, profileData]);
873
+
874
+ const updatedUserIds = await db.profile
875
+ .pluck('userId')
876
+ .where({ id: { in: profileIds } })
877
+ .update({
878
+ user: {
879
+ create: { ...userData, name: 'created' },
880
+ },
881
+ });
882
+
883
+ expect(updatedUserIds[0]).toBe(updatedUserIds[1]);
884
+
885
+ const user = await db.user.find(updatedUserIds[0] as number);
886
+ expect(user.name).toBe('created');
887
+ });
888
+ });
889
+ });
890
+ });