wabe-postgres 0.5.3 → 0.5.5

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/src/index.test.ts DELETED
@@ -1,2183 +0,0 @@
1
- import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'bun:test'
2
- import { fail } from 'node:assert'
3
- import getPort from 'get-port'
4
- import { v4 as uuid } from 'uuid'
5
- import { buildPostgresWhereQueryAndValues, PostgresAdapter } from '.'
6
- import { setupTests, closeTests } from '../utils/testHelper'
7
- import { notEmpty, Wabe, type WabeContext } from 'wabe'
8
-
9
- describe('Postgres adapter', () => {
10
- let postgresAdapter: PostgresAdapter<any>
11
- let wabe: Wabe<any>
12
- let context: WabeContext<any>
13
-
14
- beforeAll(async () => {
15
- const setup = await setupTests()
16
- wabe = setup.wabe
17
-
18
- // @ts-expect-error
19
- postgresAdapter = wabe.controllers.database.adapter
20
-
21
- context = {
22
- isRoot: true,
23
- wabe: {
24
- controllers: { database: wabe.controllers.database },
25
- config: wabe.config,
26
- },
27
- } as WabeContext<any>
28
- })
29
-
30
- afterAll(async () => {
31
- await closeTests(wabe)
32
- })
33
-
34
- beforeEach(async () => {
35
- // Get all tables except Role
36
- const client = await postgresAdapter.pool.connect()
37
- try {
38
- const tablesResult = await client.query(`
39
- SELECT tablename
40
- FROM pg_catalog.pg_tables
41
- WHERE schemaname = 'public' AND tablename != 'Role'
42
- `)
43
-
44
- // Delete all data from each table
45
- await Promise.all(
46
- tablesResult.rows.map((row) => client.query(`TRUNCATE TABLE "${row.tablename}" CASCADE`)),
47
- )
48
- } finally {
49
- client.release()
50
- }
51
- })
52
-
53
- it('should create a row with no values', async () => {
54
- const res = await postgresAdapter.createObject({
55
- className: 'Test',
56
- data: {},
57
- context,
58
- })
59
-
60
- expect(res.id).toBeDefined()
61
-
62
- const res2 = await postgresAdapter.createObjects({
63
- className: 'Test',
64
- data: [{}],
65
- context,
66
- })
67
-
68
- expect(res2[0]?.id).toBeDefined()
69
- })
70
-
71
- it('should create a row with an array field', async () => {
72
- const res = await postgresAdapter.createObject({
73
- className: 'Test',
74
- data: {
75
- array: ['a', 'b', 'c'],
76
- },
77
- context,
78
- })
79
-
80
- expect(res.id).toBeDefined()
81
- })
82
-
83
- it('should create an object with an enum field', async () => {
84
- const res = await postgresAdapter.createObject({
85
- className: 'Test',
86
- data: {
87
- enum: 'emailPassword',
88
- },
89
- context,
90
- })
91
-
92
- expect(res.id).toBeDefined()
93
- })
94
-
95
- it('should update updatedAt on an object (update one and many)', async () => {
96
- const res = await postgresAdapter.createObject({
97
- className: 'Test',
98
- data: {
99
- array: ['a', 'b', 'c'],
100
- },
101
- context,
102
- })
103
-
104
- const res2 = await postgresAdapter.updateObject({
105
- className: 'Test',
106
- data: {
107
- updatedAt: new Date(),
108
- },
109
- context,
110
- id: res.id,
111
- })
112
-
113
- expect(res2.id).toBeDefined()
114
-
115
- const res3 = await postgresAdapter.updateObjects({
116
- className: 'Test',
117
- data: {
118
- updatedAt: new Date(),
119
- },
120
- context,
121
- where: {
122
- id: {
123
- equalTo: res.id,
124
- },
125
- },
126
- })
127
-
128
- expect(res3.length).toEqual(1)
129
- })
130
-
131
- it('should insert date in iso string', async () => {
132
- // Because we store date in iso string in database
133
- const now = new Date()
134
-
135
- const res = await postgresAdapter.createObject({
136
- className: 'Test',
137
- data: {
138
- date: now.toISOString(),
139
- },
140
- context,
141
- })
142
-
143
- const res2 = await postgresAdapter.getObject({
144
- className: 'Test',
145
- id: res.id,
146
- context,
147
- select: { date: true },
148
- })
149
-
150
- expect(res2?.date).toEqual(now.toISOString())
151
- })
152
-
153
- it('should support notEqualTo', async () => {
154
- await postgresAdapter.createObjects({
155
- className: 'User',
156
- data: [
157
- {
158
- name: 'Toto',
159
- },
160
- {
161
- name: 'Toto2',
162
- },
163
- ],
164
- context,
165
- })
166
-
167
- const res2 = await postgresAdapter.getObjects({
168
- className: 'User',
169
- context,
170
- where: {
171
- name: {
172
- notEqualTo: 'Toto',
173
- },
174
- },
175
- })
176
-
177
- expect(res2.length).toEqual(1)
178
- expect(res2[0]?.name).toEqual('Toto2')
179
-
180
- const res3 = await postgresAdapter.getObjects({
181
- className: 'User',
182
- context,
183
- where: {
184
- email: {
185
- notEqualTo: 'toto@gmail.com',
186
- },
187
- },
188
- })
189
-
190
- expect(res3.length).toEqual(2)
191
- })
192
-
193
- it('should query contains on array field', async () => {
194
- const res = await postgresAdapter.createObject({
195
- className: 'Test',
196
- data: {
197
- array: ['a', 'b', 'c'],
198
- },
199
- context,
200
- })
201
-
202
- expect(res.id).toBeDefined()
203
-
204
- const res2 = await postgresAdapter.getObjects({
205
- className: 'Test',
206
- context,
207
- where: {
208
- array: {
209
- contains: 'a',
210
- },
211
- },
212
- select: { array: true },
213
- })
214
-
215
- expect(res2[0]?.array).toEqual(['a', 'b', 'c'])
216
-
217
- const res3 = await postgresAdapter.getObjects({
218
- className: 'Test',
219
- context,
220
- where: {
221
- array: {
222
- notContains: 'd',
223
- },
224
- },
225
- select: { array: true },
226
- })
227
-
228
- expect(res3[0]?.array).toEqual(['a', 'b', 'c'])
229
- })
230
-
231
- it('should query equalTo on array field', async () => {
232
- const res = await postgresAdapter.createObject({
233
- className: 'Test',
234
- data: {
235
- array: ['a', 'b', 'c'],
236
- },
237
- context,
238
- })
239
-
240
- expect(res.id).toBeDefined()
241
-
242
- const res2 = await postgresAdapter.getObjects({
243
- className: 'Test',
244
- context,
245
- where: {
246
- array: {
247
- equalTo: ['a', 'b', 'c'],
248
- },
249
- },
250
- select: { array: true },
251
- })
252
-
253
- expect(res2[0]?.array).toEqual(['a', 'b', 'c'])
254
-
255
- const res3 = await postgresAdapter.getObjects({
256
- className: 'Test',
257
- context,
258
- where: {
259
- array: {
260
- notEqualTo: ['d'],
261
- },
262
- },
263
- select: { array: true },
264
- })
265
-
266
- expect(res3[0]?.array).toEqual(['a', 'b', 'c'])
267
- })
268
-
269
- it('should update with complex where (AND and OR)', async () => {
270
- const createdObject = await postgresAdapter.createObject({
271
- className: 'Test',
272
- data: {
273
- field1: 'test',
274
- int: 10,
275
- },
276
- context,
277
- })
278
-
279
- const res = await postgresAdapter.updateObject({
280
- className: 'Test',
281
- id: createdObject.id,
282
- data: {
283
- field1: 'tata',
284
- },
285
- where: {
286
- OR: [
287
- // @ts-expect-error
288
- {
289
- AND: [
290
- {
291
- field1: { equalTo: 'test' },
292
- },
293
- {
294
- int: { equalTo: 11 },
295
- },
296
- ],
297
- },
298
- // @ts-expect-error
299
- {
300
- AND: [
301
- {
302
- field1: { equalTo: 'test' },
303
- },
304
- {
305
- int: { equalTo: 10 },
306
- },
307
- ],
308
- },
309
- ],
310
- },
311
- context,
312
- })
313
-
314
- expect(res.id).toEqual(createdObject.id)
315
- })
316
-
317
- it('should support where with null value', async () => {
318
- await postgresAdapter.createObject({
319
- className: 'Test',
320
- data: {
321
- int: null,
322
- },
323
- context,
324
- })
325
-
326
- const res = await postgresAdapter.getObjects({
327
- className: 'Test',
328
- // @ts-expect-error
329
- where: {
330
- OR: [{ int: { equalTo: 10 } }, { int: { equalTo: null } }],
331
- },
332
- context,
333
- })
334
-
335
- expect(res.length).toEqual(1)
336
-
337
- const res2 = await postgresAdapter.getObjects({
338
- className: 'Test',
339
- where: {
340
- int: { notEqualTo: null },
341
- },
342
- context,
343
- })
344
-
345
- expect(res2.length).toEqual(0)
346
- })
347
-
348
- it('should be able to interact with element in array in json', async () => {
349
- await postgresAdapter.createObject({
350
- className: 'Test',
351
- data: {
352
- object: { array: [{ string: 'string' }] },
353
- },
354
- context,
355
- })
356
-
357
- await postgresAdapter.createObject({
358
- className: 'Test',
359
- data: {
360
- object: { array: [{ string: 'string2' }] },
361
- },
362
- context,
363
- })
364
-
365
- const res = await postgresAdapter.getObjects({
366
- className: 'Test',
367
- context,
368
- where: {
369
- object: {
370
- // @ts-expect-error
371
- array: {
372
- contains: { string: 'string' },
373
- },
374
- },
375
- },
376
- })
377
-
378
- expect(res.length).toBe(1)
379
-
380
- const res2 = await postgresAdapter.getObjects({
381
- className: 'Test',
382
- context,
383
- where: {
384
- object: {
385
- // @ts-expect-error
386
- array: {
387
- notContains: { string: 'string' },
388
- },
389
- },
390
- },
391
- })
392
-
393
- expect(res2.length).toBe(1)
394
- })
395
-
396
- it('should correctly exclude documents with notContains on array of objects (ACL notContains fix)', async () => {
397
- await postgresAdapter.createObject({
398
- className: 'Test',
399
- data: {
400
- object: { array: [{ string: 'user1' }] },
401
- },
402
- context,
403
- })
404
-
405
- await postgresAdapter.createObject({
406
- className: 'Test',
407
- data: {
408
- object: { array: [{ string: 'user2' }] },
409
- },
410
- context,
411
- })
412
-
413
- const res = await postgresAdapter.getObjects({
414
- className: 'Test',
415
- context,
416
- where: {
417
- object: {
418
- // @ts-expect-error
419
- array: {
420
- notContains: { string: 'user1' },
421
- },
422
- },
423
- },
424
- })
425
-
426
- expect(res.length).toBe(1)
427
- expect(res[0]?.object?.array).toEqual([{ string: 'user2' }])
428
- })
429
-
430
- it('should correctly filter with nested notContains and equalTo without recursion overwrite', async () => {
431
- await postgresAdapter.createObject({
432
- className: 'Test',
433
- data: {
434
- object: { array: [{ string: 'user1' }] },
435
- },
436
- context,
437
- })
438
-
439
- await postgresAdapter.createObject({
440
- className: 'Test',
441
- data: {
442
- object: { array: [{ string: 'user2' }] },
443
- },
444
- context,
445
- })
446
-
447
- const res = await postgresAdapter.getObjects({
448
- className: 'Test',
449
- context,
450
- where: {
451
- object: {
452
- // @ts-expect-error
453
- array: {
454
- notContains: { string: { equalTo: 'user1' } },
455
- },
456
- },
457
- },
458
- })
459
-
460
- expect(res.length).toBe(1)
461
- expect(res[0]?.object?.array).toEqual([{ string: 'user2' }])
462
- })
463
-
464
- it('should create class', async () => {
465
- const client = await postgresAdapter.pool.connect()
466
-
467
- await client.query(`DROP TABLE IF EXISTS "Test" CASCADE`)
468
-
469
- const initialCheck = await client.query(`
470
- SELECT EXISTS (
471
- SELECT FROM information_schema.tables
472
- WHERE table_name = 'Test'
473
- )
474
- `)
475
-
476
- expect(initialCheck.rows[0].exists).toBe(false)
477
-
478
- await postgresAdapter.createClassIfNotExist('Test', context.wabe.config.schema || {})
479
-
480
- const finalCheck = await client.query(`
481
- SELECT EXISTS (
482
- SELECT FROM information_schema.tables
483
- WHERE table_name = 'Test'
484
- )
485
- `)
486
-
487
- expect(finalCheck.rows[0].exists).toBe(true)
488
-
489
- client.release()
490
- })
491
-
492
- it('should update table if a column is added after the first launch', async () => {
493
- const port = await getPort()
494
- const databaseId = uuid()
495
- const wabe = new Wabe<any>({
496
- isProduction: false,
497
- rootKey:
498
- '0uwFvUxM$ceFuF1aEtTtZMa7DUN2NZudqgY5ve5W*QCyb58cwMj9JeoaV@d#%29v&aJzswuudVU1%nAT+rxS0Bh&OkgBYc0PH18*',
499
- database: {
500
- adapter: new PostgresAdapter({
501
- databaseUrl: 'postgresql://wabe:wabe@localhost:5432',
502
- databaseName: databaseId,
503
- }),
504
- },
505
- port,
506
- security: {
507
- disableCSRFProtection: true,
508
- },
509
- schema: {
510
- classes: [
511
- {
512
- name: 'Test',
513
- fields: {
514
- field1: { type: 'String' },
515
- },
516
- },
517
- ],
518
- },
519
- })
520
-
521
- await wabe.start()
522
-
523
- const port2 = await getPort()
524
-
525
- const wabe2 = new Wabe<any>({
526
- isProduction: false,
527
- rootKey:
528
- '0uwFvUxM$ceFuF1aEtTtZMa7DUN2NZudqgY5ve5W*QCyb58cwMj9JeoaV@d#%29v&aJzswuudVU1%nAT+rxS0Bh&OkgBYc0PH18*',
529
- database: {
530
- adapter: new PostgresAdapter({
531
- databaseUrl: 'postgresql://wabe:wabe@localhost:5432',
532
- databaseName: databaseId,
533
- }),
534
- },
535
- security: {
536
- disableCSRFProtection: true,
537
- },
538
- port: port2,
539
- schema: {
540
- classes: [
541
- {
542
- name: 'Test',
543
- fields: {
544
- field1: { type: 'String' },
545
- field2: { type: 'String' },
546
- },
547
- },
548
- ],
549
- },
550
- })
551
-
552
- await wabe2.start()
553
-
554
- await wabe2.controllers.database.createObject({
555
- className: 'Test',
556
- data: {
557
- field1: 'test',
558
- field2: 'test2',
559
- },
560
- context: {
561
- wabe: wabe2,
562
- isRoot: true,
563
- },
564
- })
565
-
566
- const res = await wabe2.controllers.database.getObjects({
567
- className: 'Test',
568
- context: {
569
- wabe: wabe2,
570
- isRoot: true,
571
- },
572
- })
573
-
574
- expect(res.length).toEqual(1)
575
- expect(res[0]).toEqual(expect.objectContaining({ field1: 'test', field2: 'test2' }))
576
-
577
- await wabe2.close()
578
-
579
- await wabe.close()
580
- })
581
-
582
- it('should only return id on createObject if fields is empty', async () => {
583
- const res = await postgresAdapter.createObject({
584
- className: 'User',
585
- data: {
586
- name: 'John',
587
- age: 20,
588
- },
589
- context,
590
- select: {},
591
- })
592
-
593
- expect(res?.id).toBeDefined()
594
- expect(res).toEqual({ id: expect.any(String) })
595
- })
596
-
597
- it('should only return an array of id on createObjects if fields is empty', async () => {
598
- const res = await postgresAdapter.createObjects({
599
- className: 'User',
600
- data: [
601
- {
602
- name: 'John',
603
- age: 20,
604
- },
605
- ],
606
- context,
607
- select: {},
608
- })
609
-
610
- expect(res[0]?.id).toBeDefined()
611
- expect(res).toEqual([{ id: expect.any(String) }])
612
- })
613
-
614
- it('should only return id on updateObject if fields is empty', async () => {
615
- const insertedObject = await postgresAdapter.createObject({
616
- className: 'User',
617
- data: {
618
- name: 'John',
619
- age: 20,
620
- },
621
- context,
622
- })
623
-
624
- const res = await postgresAdapter.updateObject({
625
- className: 'User',
626
- id: insertedObject?.id || '',
627
- data: { name: 'Doe' },
628
- select: {},
629
- context,
630
- })
631
-
632
- expect(res?.id).toBeDefined()
633
- expect(res).toEqual({ id: expect.any(String) })
634
- })
635
-
636
- it('should only return an array of id on updateObjects if fields is empty', async () => {
637
- await postgresAdapter.createObjects({
638
- className: 'User',
639
- data: [
640
- {
641
- name: 'John',
642
- age: 20,
643
- },
644
- ],
645
- context,
646
- })
647
-
648
- const res = await postgresAdapter.updateObjects({
649
- className: 'User',
650
- where: {
651
- name: { equalTo: 'John' },
652
- },
653
- data: { name: 'Doe' },
654
- select: {},
655
- context,
656
- })
657
-
658
- expect(res[0]?.id).toBeDefined()
659
- expect(res).toEqual([{ id: expect.any(String) }])
660
- })
661
-
662
- it("should order the result by the field 'name' in ascending order", async () => {
663
- await postgresAdapter.createObjects({
664
- className: 'User',
665
- data: [
666
- {
667
- name: 'A',
668
- age: 20,
669
- },
670
- {
671
- name: 'B',
672
- age: 18,
673
- },
674
- ],
675
- select: {},
676
- context,
677
- })
678
-
679
- const res = await postgresAdapter.getObjects({
680
- className: 'User',
681
- select: { name: true },
682
- order: {
683
- name: 'ASC',
684
- },
685
- context,
686
- })
687
-
688
- expect(res.map((user) => user?.name)).toEqual(['A', 'B'])
689
- })
690
-
691
- it("should order the result by the field 'name' in descending order", async () => {
692
- await postgresAdapter.createObjects({
693
- className: 'User',
694
- data: [
695
- {
696
- name: 'A',
697
- age: 20,
698
- },
699
- {
700
- name: 'B',
701
- age: 18,
702
- },
703
- ],
704
- select: {},
705
- context,
706
- })
707
-
708
- const res = await postgresAdapter.getObjects({
709
- className: 'User',
710
- select: { name: true },
711
- order: {
712
- name: 'DESC',
713
- },
714
- context,
715
- })
716
-
717
- expect(res.map((user) => user?.name)).toEqual(['B', 'A'])
718
- })
719
-
720
- it("should order the result by the field 'age' and 'name' in descending order", async () => {
721
- await postgresAdapter.createObjects({
722
- className: 'User',
723
- data: [
724
- {
725
- name: 'A',
726
- age: 20,
727
- },
728
- {
729
- name: 'B',
730
- age: 18,
731
- },
732
- ],
733
- select: {},
734
- context,
735
- })
736
-
737
- const res = await postgresAdapter.getObjects({
738
- className: 'User',
739
- select: { name: true },
740
- order: {
741
- age: 'ASC',
742
- name: 'DESC',
743
- },
744
- context,
745
- })
746
-
747
- expect(res.map((user) => user?.name)).toEqual(['B', 'A'])
748
- })
749
-
750
- it('should count all elements corresponds to where condition in collection', async () => {
751
- const res = await postgresAdapter.count({
752
- className: 'User',
753
- context,
754
- })
755
-
756
- expect(res).toEqual(0)
757
-
758
- await postgresAdapter.createObjects({
759
- className: 'User',
760
- data: [
761
- {
762
- name: 'Lucas',
763
- age: 20,
764
- },
765
- {
766
- name: 'LucasBis',
767
- age: 18,
768
- },
769
- ],
770
- select: {},
771
- context,
772
- })
773
-
774
- const res2 = await postgresAdapter.count({
775
- className: 'User',
776
- context,
777
- })
778
-
779
- expect(res2).toEqual(2)
780
-
781
- const res3 = await postgresAdapter.count({
782
- className: 'User',
783
- context,
784
- where: { age: { equalTo: 20 } },
785
- })
786
-
787
- expect(res3).toEqual(1)
788
- })
789
-
790
- it('should clear all database except Role collection', async () => {
791
- await postgresAdapter.createObjects({
792
- className: 'User',
793
- data: [
794
- {
795
- name: 'Lucas',
796
- age: 20,
797
- },
798
- {
799
- name: 'LucasBis',
800
- age: 18,
801
- },
802
- ],
803
- select: {},
804
- context,
805
- })
806
-
807
- await postgresAdapter.createObjects({
808
- className: '_Session',
809
- data: [
810
- {
811
- accessTokenEncrypted: 'accessTokenEncrypted',
812
- refreshTokenEncrypted: 'refreshTokenEncrypted',
813
- accessTokenExpiresAt: new Date(),
814
- refreshTokenExpiresAt: new Date(),
815
- user: 'id',
816
- },
817
- {
818
- accessTokenEncrypted: 'accessTokenEncrypted',
819
- refreshTokenEncrypted: 'refreshTokenEncrypted',
820
- accessTokenExpiresAt: new Date(),
821
- refreshTokenExpiresAt: new Date(),
822
- user: 'id',
823
- },
824
- ],
825
- select: {},
826
- context,
827
- })
828
-
829
- await postgresAdapter.clearDatabase()
830
-
831
- const res = await postgresAdapter.getObjects({
832
- className: 'User',
833
- select: {},
834
- context,
835
- })
836
-
837
- const res2 = await postgresAdapter.getObjects({
838
- className: '_Session',
839
- select: {},
840
- context,
841
- })
842
-
843
- const res3 = await postgresAdapter.getObjects({
844
- className: 'Role',
845
- select: { id: true },
846
- context,
847
- })
848
-
849
- expect(res.length).toEqual(0)
850
- expect(res2.length).toEqual(0)
851
- expect(res3.length).not.toEqual(0)
852
- })
853
-
854
- it('should support where on getObject for additional check (acl for example)', async () => {
855
- const insertedObjects = await postgresAdapter.createObjects({
856
- className: 'User',
857
- data: [
858
- {
859
- name: 'Lucas',
860
- age: 20,
861
- },
862
- {
863
- name: 'LucasBis',
864
- age: 18,
865
- },
866
- ],
867
- select: { id: true },
868
- context,
869
- })
870
-
871
- expect(
872
- postgresAdapter.getObject({
873
- className: 'User',
874
- where: {
875
- name: { equalTo: 'InvalidName' },
876
- },
877
- id: insertedObjects[0]?.id || '',
878
- context,
879
- }),
880
- ).rejects.toThrow('Object not found')
881
-
882
- const res = await postgresAdapter.getObject({
883
- className: 'User',
884
- where: {
885
- name: { equalTo: 'Lucas' },
886
- },
887
- id: insertedObjects[0]?.id || '',
888
- context,
889
- })
890
-
891
- expect(res?.name).toEqual('Lucas')
892
- })
893
-
894
- it('should support where on updateObject for additional check (acl for example)', async () => {
895
- const insertedObjects = await postgresAdapter.createObjects({
896
- className: 'User',
897
- data: [
898
- {
899
- name: 'Lucas',
900
- age: 20,
901
- },
902
- {
903
- name: 'LucasBis',
904
- age: 18,
905
- },
906
- ],
907
- select: { id: true },
908
- context,
909
- })
910
-
911
- expect(
912
- postgresAdapter.updateObject({
913
- className: 'User',
914
- where: {
915
- name: { equalTo: 'InvalidName' },
916
- },
917
- id: insertedObjects[0]?.id || '',
918
- context,
919
- data: { name: 'Lucas2' },
920
- }),
921
- ).rejects.toThrow('Object not found')
922
-
923
- const { id } = await postgresAdapter.updateObject({
924
- className: 'User',
925
- where: {
926
- name: { equalTo: 'Lucas' },
927
- },
928
- id: insertedObjects[0]?.id || '',
929
- context,
930
- data: { name: 'Lucas2' },
931
- })
932
-
933
- const updatedObject = await postgresAdapter.getObject({
934
- className: 'User',
935
- id,
936
- select: { id: true, name: true },
937
- context,
938
- })
939
-
940
- expect(updatedObject?.name).toEqual('Lucas2')
941
- })
942
-
943
- it('should support where on delete for additional check (acl for example)', async () => {
944
- const insertedObjects = await postgresAdapter.createObjects({
945
- className: 'User',
946
- data: [
947
- {
948
- name: 'Lucas',
949
- age: 20,
950
- },
951
- {
952
- name: 'LucasBis',
953
- age: 18,
954
- },
955
- ],
956
- select: { id: true },
957
- context,
958
- })
959
-
960
- expect(
961
- postgresAdapter.deleteObject({
962
- className: 'User',
963
- where: {
964
- name: { equalTo: 'InvalidName' },
965
- },
966
- id: insertedObjects[0]?.id || '',
967
- context,
968
- }),
969
- ).rejects.toThrow('Object not found')
970
-
971
- await postgresAdapter.deleteObject({
972
- className: 'User',
973
- where: {
974
- name: { equalTo: 'Lucas' },
975
- },
976
- id: insertedObjects[0]?.id || '',
977
- context,
978
- })
979
- })
980
-
981
- it('should support notEqualTo on id', async () => {
982
- const insertedObjects = await postgresAdapter.createObjects({
983
- className: 'User',
984
- data: [
985
- {
986
- name: 'Lucas',
987
- age: 20,
988
- },
989
- {
990
- name: 'LucasBis',
991
- age: 18,
992
- },
993
- ],
994
- select: { id: true },
995
- context,
996
- })
997
-
998
- const res = await postgresAdapter.getObjects({
999
- className: 'User',
1000
- where: {
1001
- id: { notEqualTo: insertedObjects[0]?.id },
1002
- },
1003
- context,
1004
- })
1005
-
1006
- expect(res.length).toEqual(1)
1007
- })
1008
-
1009
- it('should support equalTo on id', async () => {
1010
- const insertedObjects = await postgresAdapter.createObjects({
1011
- className: 'User',
1012
- data: [
1013
- {
1014
- name: 'Lucas',
1015
- age: 20,
1016
- },
1017
- {
1018
- name: 'LucasBis',
1019
- age: 18,
1020
- },
1021
- ],
1022
- select: { id: true },
1023
- context,
1024
- })
1025
-
1026
- const res = await postgresAdapter.getObjects({
1027
- className: 'User',
1028
- where: {
1029
- id: { equalTo: insertedObjects[0]?.id },
1030
- },
1031
- context,
1032
- })
1033
-
1034
- expect(res.length).toEqual(1)
1035
- })
1036
-
1037
- it('should support in aggregation on id', async () => {
1038
- const insertedObjects = await postgresAdapter.createObjects({
1039
- className: 'User',
1040
- data: [
1041
- {
1042
- name: 'Lucas',
1043
- age: 20,
1044
- },
1045
- {
1046
- name: 'LucasBis',
1047
- age: 18,
1048
- },
1049
- ],
1050
- select: { id: true },
1051
- context,
1052
- })
1053
-
1054
- const res = await postgresAdapter.getObjects({
1055
- className: 'User',
1056
- where: {
1057
- id: {
1058
- in: insertedObjects.map((obj) => obj?.id).filter(notEmpty),
1059
- },
1060
- },
1061
- context,
1062
- })
1063
-
1064
- expect(res.length).toEqual(2)
1065
- })
1066
-
1067
- it('should support notIn aggregation on id', async () => {
1068
- const insertedObjects = await postgresAdapter.createObjects({
1069
- className: 'User',
1070
- data: [
1071
- {
1072
- name: 'Lucas',
1073
- age: 20,
1074
- },
1075
- {
1076
- name: 'LucasBis',
1077
- age: 18,
1078
- },
1079
- ],
1080
- select: { id: true },
1081
- context,
1082
- })
1083
-
1084
- const res = await postgresAdapter.getObjects({
1085
- className: 'User',
1086
- where: {
1087
- id: {
1088
- notIn: insertedObjects.map((obj) => obj?.id).filter(notEmpty),
1089
- },
1090
- },
1091
- context,
1092
- })
1093
-
1094
- expect(res.length).toEqual(0)
1095
- })
1096
-
1097
- it('should always return id', async () => {
1098
- const insertedObjects = await postgresAdapter.createObjects({
1099
- className: 'User',
1100
- data: [
1101
- {
1102
- name: 'Lucas',
1103
- age: 20,
1104
- },
1105
- ],
1106
- context,
1107
- select: { age: true, id: true },
1108
- })
1109
-
1110
- if (!insertedObjects) fail()
1111
-
1112
- expect(insertedObjects[0]?.id).toBeDefined()
1113
- })
1114
-
1115
- it('should return id if no fields is specified', async () => {
1116
- const insertedObjects = await postgresAdapter.createObjects({
1117
- className: 'User',
1118
- data: [
1119
- {
1120
- name: 'Lucas',
1121
- age: 20,
1122
- },
1123
- ],
1124
- context,
1125
- })
1126
-
1127
- if (!insertedObjects) fail()
1128
-
1129
- expect(insertedObjects[0]?.id).toBeDefined()
1130
- })
1131
-
1132
- it('should getObjects using id', async () => {
1133
- const insertedObjects = await postgresAdapter.createObjects({
1134
- className: 'User',
1135
- data: [
1136
- {
1137
- name: 'Lucas',
1138
- age: 20,
1139
- },
1140
- {
1141
- name: 'Lucas1',
1142
- age: 20,
1143
- },
1144
- ],
1145
- select: { name: true, id: true },
1146
- context,
1147
- })
1148
-
1149
- if (!insertedObjects) fail()
1150
-
1151
- const res = await postgresAdapter.getObjects({
1152
- className: 'User',
1153
- where: {
1154
- id: {
1155
- equalTo: insertedObjects[0]?.id || '',
1156
- },
1157
- },
1158
- context,
1159
- })
1160
-
1161
- expect(res.length).toEqual(1)
1162
- })
1163
-
1164
- it('should getObjects with limit and offset', async () => {
1165
- await postgresAdapter.createObjects({
1166
- className: 'User',
1167
- data: [
1168
- {
1169
- name: 'John',
1170
- age: 20,
1171
- },
1172
- {
1173
- name: 'John1',
1174
- age: 20,
1175
- },
1176
- {
1177
- name: 'John2',
1178
- age: 20,
1179
- },
1180
- {
1181
- name: 'John3',
1182
- age: 20,
1183
- },
1184
- {
1185
- name: 'John4',
1186
- age: 20,
1187
- },
1188
- ],
1189
- select: { name: true, id: true },
1190
- context,
1191
- })
1192
-
1193
- const res = await postgresAdapter.getObjects({
1194
- className: 'User',
1195
- select: { name: true },
1196
- first: 2,
1197
- offset: 2,
1198
- context,
1199
- })
1200
-
1201
- expect(res.length).toEqual(2)
1202
- expect(res[0]?.name).toEqual('John2')
1203
- expect(res[1]?.name).toEqual('John3')
1204
- })
1205
-
1206
- it('should get all the objects without limit and without offset', async () => {
1207
- await postgresAdapter.createObjects({
1208
- className: 'User',
1209
- data: [
1210
- {
1211
- name: 'John',
1212
- age: 20,
1213
- },
1214
- {
1215
- name: 'John1',
1216
- age: 20,
1217
- },
1218
- {
1219
- name: 'John2',
1220
- age: 20,
1221
- },
1222
- {
1223
- name: 'John3',
1224
- age: 20,
1225
- },
1226
- {
1227
- name: 'John4',
1228
- age: 20,
1229
- },
1230
- ],
1231
- select: { name: true, id: true },
1232
- context,
1233
- })
1234
-
1235
- const res = await postgresAdapter.getObjects({
1236
- className: 'User',
1237
- select: { name: true },
1238
- context,
1239
- where: {},
1240
- })
1241
-
1242
- expect(res.length).toEqual(5)
1243
- })
1244
-
1245
- it('should get the id of an object', async () => {
1246
- const insertedObject = await postgresAdapter.createObject({
1247
- className: 'User',
1248
- data: {
1249
- name: 'John',
1250
- age: 20,
1251
- },
1252
- select: { name: true, id: true },
1253
- context,
1254
- })
1255
-
1256
- const res = await postgresAdapter.getObject({
1257
- id: insertedObject?.id.toString() || '',
1258
- className: 'User',
1259
- select: { id: true },
1260
- context,
1261
- })
1262
-
1263
- expect(res?.id).toEqual(insertedObject?.id || '')
1264
- })
1265
-
1266
- it('should get all fields when no select is specified', async () => {
1267
- const insertedObject = await postgresAdapter.createObject({
1268
- className: 'User',
1269
- data: {
1270
- name: 'John',
1271
- age: 20,
1272
- },
1273
- select: { name: true, id: true },
1274
- context,
1275
- })
1276
-
1277
- if (!insertedObject) fail()
1278
-
1279
- const id = insertedObject.id
1280
-
1281
- expect(id).toBeDefined()
1282
-
1283
- const res = await postgresAdapter.getObject({
1284
- className: 'User',
1285
- id: id.toString(),
1286
- context,
1287
- })
1288
-
1289
- expect(res).toEqual(
1290
- expect.objectContaining({
1291
- name: 'John',
1292
- age: 20,
1293
- id: expect.any(String),
1294
- }),
1295
- )
1296
- })
1297
-
1298
- it('should get one object with specific field', async () => {
1299
- const insertedObject = await postgresAdapter.createObject({
1300
- className: 'User',
1301
- data: {
1302
- name: 'John',
1303
- age: 20,
1304
- },
1305
- select: { name: true, id: true },
1306
- context,
1307
- })
1308
-
1309
- if (!insertedObject) fail()
1310
-
1311
- const id = insertedObject.id
1312
-
1313
- expect(id).toBeDefined()
1314
-
1315
- const field = await postgresAdapter.getObject({
1316
- className: 'User',
1317
- id: id.toString(),
1318
- select: { name: true, id: true },
1319
- context,
1320
- })
1321
-
1322
- expect(field).toEqual({
1323
- name: 'John',
1324
- id: expect.any(String),
1325
- })
1326
- })
1327
-
1328
- it('should get all object with specific field', async () => {
1329
- const objects = await postgresAdapter.getObjects({
1330
- className: 'User',
1331
- select: { name: true },
1332
- context,
1333
- })
1334
-
1335
- expect(objects.length).toEqual(0)
1336
-
1337
- await postgresAdapter.createObject({
1338
- className: 'User',
1339
- data: {
1340
- name: 'John1',
1341
- age: 20,
1342
- },
1343
- select: { name: true },
1344
- context,
1345
- })
1346
-
1347
- await postgresAdapter.createObject({
1348
- className: 'User',
1349
- data: {
1350
- name: 'John2',
1351
- age: 20,
1352
- },
1353
- select: { name: true },
1354
- context,
1355
- })
1356
-
1357
- const objects2 = await postgresAdapter.getObjects({
1358
- className: 'User',
1359
- select: { name: true, id: true },
1360
- context,
1361
- })
1362
-
1363
- expect(objects2.length).toEqual(2)
1364
- expect(objects2).toEqual([
1365
- {
1366
- id: expect.any(String),
1367
- name: 'John1',
1368
- },
1369
- {
1370
- id: expect.any(String),
1371
- name: 'John2',
1372
- },
1373
- ])
1374
-
1375
- const objects3 = await postgresAdapter.getObjects({
1376
- className: 'User',
1377
- select: { name: true, id: true, age: true },
1378
- context,
1379
- })
1380
-
1381
- expect(objects3.length).toEqual(2)
1382
- expect(objects3).toEqual([
1383
- {
1384
- id: expect.any(String),
1385
- name: 'John1',
1386
- age: 20,
1387
- },
1388
- {
1389
- id: expect.any(String),
1390
- name: 'John2',
1391
- age: 20,
1392
- },
1393
- ])
1394
- })
1395
-
1396
- it('should get all objects with where filter', async () => {
1397
- await postgresAdapter.createObject({
1398
- className: 'User',
1399
- data: {
1400
- name: 'John1',
1401
- age: 20,
1402
- },
1403
- select: { name: true },
1404
- context,
1405
- })
1406
-
1407
- await postgresAdapter.createObject({
1408
- className: 'User',
1409
- data: {
1410
- name: 'John2',
1411
- age: 20,
1412
- },
1413
- select: { name: true },
1414
- context,
1415
- })
1416
-
1417
- // OR statement
1418
- expect(
1419
- await postgresAdapter.getObjects({
1420
- className: 'User',
1421
- // @ts-expect-error
1422
- where: {
1423
- OR: [
1424
- {
1425
- name: { equalTo: 'John1' },
1426
- },
1427
- {
1428
- name: { equalTo: 'John2' },
1429
- },
1430
- ],
1431
- },
1432
- select: { name: true, id: true, age: true },
1433
- context,
1434
- }),
1435
- ).toEqual([
1436
- {
1437
- id: expect.any(String),
1438
- name: 'John1',
1439
- age: 20,
1440
- },
1441
- {
1442
- id: expect.any(String),
1443
- name: 'John2',
1444
- age: 20,
1445
- },
1446
- ])
1447
-
1448
- expect(
1449
- await postgresAdapter.getObjects({
1450
- className: 'User',
1451
- // @ts-expect-error
1452
- where: {
1453
- OR: [
1454
- {
1455
- name: { equalTo: 'John1' },
1456
- },
1457
- {
1458
- name: { equalTo: 'John3' },
1459
- },
1460
- ],
1461
- },
1462
- select: { name: true, id: true, age: true },
1463
- context,
1464
- }),
1465
- ).toEqual([
1466
- {
1467
- id: expect.any(String),
1468
- name: 'John1',
1469
- age: 20,
1470
- },
1471
- ])
1472
-
1473
- // AND statement
1474
- expect(
1475
- await postgresAdapter.getObjects({
1476
- className: 'User',
1477
- // @ts-expect-error
1478
- where: {
1479
- AND: [
1480
- {
1481
- name: { equalTo: 'John1' },
1482
- },
1483
- {
1484
- age: { equalTo: 20 },
1485
- },
1486
- ],
1487
- },
1488
- select: { name: true, id: true, age: true },
1489
- context,
1490
- }),
1491
- ).toEqual([
1492
- {
1493
- id: expect.any(String),
1494
- name: 'John1',
1495
- age: 20,
1496
- },
1497
- ])
1498
-
1499
- expect(
1500
- await postgresAdapter.getObjects({
1501
- className: 'User',
1502
- // @ts-expect-error
1503
- where: {
1504
- AND: [
1505
- {
1506
- name: { equalTo: 'John1' },
1507
- },
1508
- {
1509
- age: { equalTo: 10 },
1510
- },
1511
- ],
1512
- },
1513
- context,
1514
- }),
1515
- ).toEqual([])
1516
-
1517
- // Equal to statement
1518
- expect(
1519
- await postgresAdapter.getObjects({
1520
- className: 'User',
1521
- where: {
1522
- name: { equalTo: 'John1' },
1523
- },
1524
- select: { name: true, id: true, age: true },
1525
- context,
1526
- }),
1527
- ).toEqual([
1528
- {
1529
- id: expect.any(String),
1530
- name: 'John1',
1531
- age: 20,
1532
- },
1533
- ])
1534
-
1535
- expect(
1536
- await postgresAdapter.getObjects({
1537
- className: 'User',
1538
- where: {
1539
- age: { greaterThan: 21 },
1540
- },
1541
- context,
1542
- }),
1543
- ).toEqual([])
1544
-
1545
- // Not equal to statement
1546
- expect(
1547
- await postgresAdapter.getObjects({
1548
- className: 'User',
1549
- where: {
1550
- name: { notEqualTo: 'John1' },
1551
- },
1552
- select: { name: true, id: true, age: true },
1553
- context,
1554
- }),
1555
- ).toEqual([
1556
- {
1557
- id: expect.any(String),
1558
- name: 'John2',
1559
- age: 20,
1560
- },
1561
- ])
1562
-
1563
- // Less than to statement on number
1564
- expect(
1565
- await postgresAdapter.getObjects({
1566
- className: 'User',
1567
- where: {
1568
- age: { lessThan: 30 },
1569
- },
1570
- select: { name: true, id: true, age: true },
1571
- context,
1572
- }),
1573
- ).toEqual([
1574
- {
1575
- id: expect.any(String),
1576
- name: 'John1',
1577
- age: 20,
1578
- },
1579
- {
1580
- id: expect.any(String),
1581
- name: 'John2',
1582
- age: 20,
1583
- },
1584
- ])
1585
-
1586
- // Equal to statement on number
1587
- expect(
1588
- await postgresAdapter.getObjects({
1589
- className: 'User',
1590
- where: {
1591
- age: { equalTo: 20 },
1592
- },
1593
- select: { name: true, id: true, age: true },
1594
- context,
1595
- }),
1596
- ).toEqual([
1597
- {
1598
- id: expect.any(String),
1599
- name: 'John1',
1600
- age: 20,
1601
- },
1602
- {
1603
- id: expect.any(String),
1604
- name: 'John2',
1605
- age: 20,
1606
- },
1607
- ])
1608
- })
1609
-
1610
- it("should throw object not found if the object doesn't exist", () => {
1611
- expect(
1612
- postgresAdapter.getObject({
1613
- className: 'User',
1614
- id: '4fd2217c-bc08-437f-bf01-d21e8bd7b891',
1615
- select: { name: true },
1616
- context,
1617
- }),
1618
- ).rejects.toThrow('Object not found')
1619
- })
1620
-
1621
- it('should create object and return the created object', async () => {
1622
- const { id } = await postgresAdapter.createObject({
1623
- className: 'User',
1624
- data: {
1625
- name: 'Lucas',
1626
- age: 23,
1627
- },
1628
- select: { age: true, id: true },
1629
- context,
1630
- })
1631
-
1632
- const insertedObject = await postgresAdapter.getObject({
1633
- className: 'User',
1634
- id,
1635
- select: { age: true, id: true },
1636
- context,
1637
- })
1638
-
1639
- expect(insertedObject).toEqual({ age: 23, id: expect.any(String) })
1640
-
1641
- const { id: id2 } = await postgresAdapter.createObject({
1642
- className: 'User',
1643
- data: {
1644
- name: 'Lucas2',
1645
- age: 24,
1646
- },
1647
- select: { name: true, id: true, age: true },
1648
- context,
1649
- })
1650
-
1651
- const insertedObject2 = await postgresAdapter.getObject({
1652
- className: 'User',
1653
- id: id2,
1654
- select: { name: true, id: true, age: true },
1655
- context,
1656
- })
1657
-
1658
- expect(insertedObject2).toEqual({
1659
- age: 24,
1660
- id: expect.any(String),
1661
- name: 'Lucas2',
1662
- })
1663
- })
1664
-
1665
- it('should create multiple objects and return an array of the created object', async () => {
1666
- await postgresAdapter.createObjects({
1667
- className: 'User',
1668
- data: [
1669
- {
1670
- name: 'Lucas3',
1671
- age: 23,
1672
- },
1673
- {
1674
- name: 'Lucas4',
1675
- age: 24,
1676
- },
1677
- ],
1678
- select: { name: true, id: true },
1679
- context,
1680
- })
1681
-
1682
- const insertedObjects = await postgresAdapter.getObjects({
1683
- className: 'User',
1684
- where: {},
1685
- select: { name: true, id: true },
1686
- context,
1687
- })
1688
-
1689
- expect(insertedObjects).toEqual([
1690
- {
1691
- id: expect.any(String),
1692
- name: 'Lucas3',
1693
- },
1694
- {
1695
- id: expect.any(String),
1696
- name: 'Lucas4',
1697
- },
1698
- ])
1699
- })
1700
-
1701
- it('should create multiple objects and return an array of the created object with all fields', async () => {
1702
- await postgresAdapter.createObjects({
1703
- className: 'User',
1704
- data: [
1705
- {
1706
- name: 'Lucas3',
1707
- age: 23,
1708
- },
1709
- {
1710
- name: 'Lucas4',
1711
- age: 24,
1712
- },
1713
- ],
1714
- select: { name: true, id: true, age: true },
1715
- context,
1716
- })
1717
-
1718
- const insertedObjects = await postgresAdapter.getObjects({
1719
- className: 'User',
1720
- where: {},
1721
- select: { name: true, id: true, age: true },
1722
- context,
1723
- })
1724
-
1725
- expect(insertedObjects).toEqual([
1726
- {
1727
- id: expect.any(String),
1728
- name: 'Lucas3',
1729
- age: 23,
1730
- },
1731
- {
1732
- id: expect.any(String),
1733
- name: 'Lucas4',
1734
- age: 24,
1735
- },
1736
- ])
1737
- })
1738
-
1739
- it('should update object', async () => {
1740
- const insertedObject = await postgresAdapter.createObject({
1741
- className: 'User',
1742
- data: {
1743
- name: 'John',
1744
- age: 20,
1745
- },
1746
- context,
1747
- })
1748
-
1749
- if (!insertedObject) fail()
1750
-
1751
- const id = insertedObject.id
1752
-
1753
- await postgresAdapter.updateObject({
1754
- className: 'User',
1755
- id: id.toString(),
1756
- data: { name: 'Doe' },
1757
- select: { name: true, id: true },
1758
- context,
1759
- })
1760
-
1761
- const updatedObject = await postgresAdapter.getObject({
1762
- className: 'User',
1763
- id: id.toString(),
1764
- select: { name: true, id: true },
1765
- context,
1766
- })
1767
-
1768
- expect(updatedObject).toEqual({
1769
- name: 'Doe',
1770
- id: expect.any(String),
1771
- })
1772
-
1773
- await postgresAdapter.updateObject({
1774
- className: 'User',
1775
- id: id.toString(),
1776
- data: { name: 'Doe' },
1777
- select: { name: true, id: true, age: true },
1778
- context,
1779
- })
1780
-
1781
- const updatedObject2 = await postgresAdapter.getObject({
1782
- className: 'User',
1783
- id: id.toString(),
1784
- select: { name: true, age: true, id: true },
1785
- context,
1786
- })
1787
-
1788
- expect(updatedObject2).toEqual({
1789
- id: expect.any(String),
1790
- name: 'Doe',
1791
- age: 20,
1792
- })
1793
- })
1794
-
1795
- it('should update multiple objects', async () => {
1796
- const insertedObjects = await postgresAdapter.createObjects({
1797
- className: 'User',
1798
- data: [
1799
- {
1800
- name: 'Lucas',
1801
- age: 20,
1802
- },
1803
- {
1804
- name: 'Lucas1',
1805
- age: 20,
1806
- },
1807
- ],
1808
- context,
1809
- })
1810
-
1811
- if (!insertedObjects) fail()
1812
-
1813
- await postgresAdapter.updateObjects({
1814
- className: 'User',
1815
- where: {
1816
- name: { equalTo: 'Lucas' },
1817
- },
1818
- data: { name: 'Doe' },
1819
- select: { name: true, id: true },
1820
- context,
1821
- })
1822
-
1823
- const updatedObjects = await postgresAdapter.getObjects({
1824
- className: 'User',
1825
- where: {
1826
- name: {
1827
- equalTo: 'Doe',
1828
- },
1829
- },
1830
- select: { name: true, id: true },
1831
- context,
1832
- })
1833
-
1834
- expect(updatedObjects).toEqual([
1835
- {
1836
- id: expect.any(String),
1837
- name: 'Doe',
1838
- },
1839
- ])
1840
-
1841
- await postgresAdapter.updateObjects({
1842
- className: 'User',
1843
- where: {
1844
- age: { greaterThanOrEqualTo: 20 },
1845
- },
1846
- data: { age: 23 },
1847
- select: { name: true, id: true, age: true },
1848
- context,
1849
- })
1850
-
1851
- const updatedObjects2 = await postgresAdapter.getObjects({
1852
- className: 'User',
1853
- where: {
1854
- age: {
1855
- greaterThanOrEqualTo: 23,
1856
- },
1857
- },
1858
- select: { age: true, name: true, id: true },
1859
- context,
1860
- })
1861
-
1862
- expect(updatedObjects2).toEqual(
1863
- expect.arrayContaining([
1864
- {
1865
- id: expect.any(String),
1866
- name: 'Doe',
1867
- age: 23,
1868
- },
1869
- {
1870
- id: expect.any(String),
1871
- name: 'Lucas1',
1872
- age: 23,
1873
- },
1874
- ]),
1875
- )
1876
- })
1877
-
1878
- it('should update the same field of an object that which we use in the where field', async () => {
1879
- const insertedObject = await postgresAdapter.createObject({
1880
- className: 'User',
1881
- data: {
1882
- name: 'John',
1883
- age: 20,
1884
- },
1885
- context,
1886
- })
1887
-
1888
- if (!insertedObject) fail()
1889
-
1890
- await postgresAdapter.updateObjects({
1891
- className: 'User',
1892
- data: { name: 'Doe' },
1893
- where: {
1894
- name: {
1895
- equalTo: 'John',
1896
- },
1897
- },
1898
- select: {},
1899
- context,
1900
- })
1901
-
1902
- const updatedObjects = await postgresAdapter.getObjects({
1903
- className: 'User',
1904
- where: {
1905
- name: {
1906
- equalTo: 'Doe',
1907
- },
1908
- },
1909
- select: { age: true, id: true },
1910
- context,
1911
- })
1912
-
1913
- expect(updatedObjects).toEqual([
1914
- {
1915
- id: expect.any(String),
1916
- age: 20,
1917
- },
1918
- ])
1919
- })
1920
-
1921
- it('should delete one object', async () => {
1922
- const insertedObject = await postgresAdapter.createObject({
1923
- className: 'User',
1924
- data: {
1925
- name: 'John',
1926
- age: 20,
1927
- },
1928
- context,
1929
- })
1930
-
1931
- if (!insertedObject) fail()
1932
-
1933
- const id = insertedObject.id
1934
-
1935
- await postgresAdapter.deleteObject({
1936
- className: 'User',
1937
- id: id.toString(),
1938
- select: { name: true, id: true, age: true },
1939
- context,
1940
- })
1941
-
1942
- expect(
1943
- postgresAdapter.getObject({
1944
- className: 'User',
1945
- id: id.toString(),
1946
- context,
1947
- }),
1948
- ).rejects.toThrow('Object not found')
1949
- })
1950
-
1951
- it('should delete multiple object', async () => {
1952
- await postgresAdapter.createObject({
1953
- className: 'User',
1954
- data: {
1955
- name: 'John',
1956
- age: 18,
1957
- },
1958
- context,
1959
- })
1960
-
1961
- await postgresAdapter.createObject({
1962
- className: 'User',
1963
- data: {
1964
- name: 'Lucas',
1965
- age: 18,
1966
- },
1967
- context,
1968
- })
1969
-
1970
- await postgresAdapter.deleteObjects({
1971
- className: 'User',
1972
- where: { age: { equalTo: 18 } },
1973
- select: { name: true, id: true, age: true },
1974
- context,
1975
- })
1976
-
1977
- const resAfterDelete = await postgresAdapter.getObjects({
1978
- className: 'User',
1979
- where: { age: { equalTo: 18 } },
1980
- context,
1981
- })
1982
-
1983
- expect(resAfterDelete.length).toEqual(0)
1984
- })
1985
-
1986
- it('should build empty where query for postgres adapter if where is empty', () => {
1987
- const { query, values } = buildPostgresWhereQueryAndValues({})
1988
-
1989
- expect(query).toEqual('')
1990
- expect(values).toEqual([])
1991
- })
1992
-
1993
- it('should request nested objects in object', async () => {
1994
- await postgresAdapter.createObject({
1995
- className: 'User',
1996
- context,
1997
- data: {
1998
- authentication: {
1999
- emailPassword: {
2000
- email: 'email@test.fr',
2001
- password: 'password',
2002
- },
2003
- },
2004
- },
2005
- })
2006
-
2007
- const res = await postgresAdapter.getObjects({
2008
- className: 'User',
2009
- where: {
2010
- authentication: {
2011
- // @ts-expect-error
2012
- emailPassword: {
2013
- email: { equalTo: 'email@test.fr' },
2014
- },
2015
- },
2016
- },
2017
- context,
2018
- select: { authentication: true },
2019
- })
2020
-
2021
- expect(res.length).toEqual(1)
2022
- expect(res[0]?.authentication?.emailPassword?.email).toEqual('email@test.fr')
2023
- expect(res[0]?.authentication?.emailPassword?.password).toEqual('password')
2024
- })
2025
-
2026
- it('should filter documents where field exists (exists: true)', async () => {
2027
- // Create test documents using the adapter
2028
- await postgresAdapter.createObjects({
2029
- className: 'Test',
2030
- data: [
2031
- { field1: 'Document with field1', int: 25 },
2032
- { field1: 'Another document with field1', int: 30 },
2033
- { field1: null, int: 35 }, // field1 is null
2034
- ],
2035
- context,
2036
- })
2037
-
2038
- // Test exists: true using the adapter
2039
- const results = await postgresAdapter.getObjects({
2040
- className: 'Test',
2041
- where: { field1: { exists: true } },
2042
- context,
2043
- })
2044
-
2045
- expect(results.length).toBe(2)
2046
- expect(results.every((row) => row?.field1 !== null)).toBe(true)
2047
- })
2048
-
2049
- it('should filter documents where field does not exist (exists: false)', async () => {
2050
- // Create test documents using the adapter
2051
- await postgresAdapter.createObjects({
2052
- className: 'Test',
2053
- data: [
2054
- { field1: 'Document with field1', int: 25 },
2055
- { field1: null, int: 30 },
2056
- { field1: null, int: 35 },
2057
- ],
2058
- context,
2059
- })
2060
-
2061
- // Test exists: false using the adapter
2062
- const results = await postgresAdapter.getObjects({
2063
- className: 'Test',
2064
- where: { field1: { exists: false } },
2065
- context,
2066
- })
2067
-
2068
- expect(results.length).toBe(2)
2069
- expect(results.every((row) => row?.field1 === null)).toBe(true)
2070
- })
2071
-
2072
- it('should work with complex queries combining exists and other operators', async () => {
2073
- // Create test documents using the adapter
2074
- await postgresAdapter.createObjects({
2075
- className: 'Test',
2076
- data: [
2077
- { field1: 'John', int: 25 },
2078
- { field1: 'Jane', int: 30 },
2079
- { field1: null, int: 35 },
2080
- { field1: 'Bob', int: null },
2081
- ],
2082
- context,
2083
- })
2084
-
2085
- // Test complex query: field1 exists AND int > 25
2086
- const results = await postgresAdapter.getObjects({
2087
- className: 'Test',
2088
- // @ts-expect-error
2089
- where: {
2090
- AND: [{ field1: { exists: true } }, { int: { greaterThan: 25 } }],
2091
- },
2092
- context,
2093
- })
2094
-
2095
- expect(results.length).toBe(1)
2096
- expect(results[0]?.field1).toBe('Jane')
2097
- expect(results[0]?.int).toBe(30)
2098
- })
2099
-
2100
- it('should handle exists with JSON fields', async () => {
2101
- // Create test documents with JSON data using the adapter
2102
- await postgresAdapter.createObjects({
2103
- className: 'Test',
2104
- data: [
2105
- { object: { array: [{ string: 'John' }] }, int: 25 },
2106
- { object: { array: [{ string: 'Jane' }] }, int: 30 },
2107
- { object: null, int: 35 },
2108
- ],
2109
- context,
2110
- })
2111
-
2112
- const results = await postgresAdapter.getObjects({
2113
- className: 'Test',
2114
- // @ts-expect-error
2115
- where: { object: { array: { exists: true } } },
2116
- context,
2117
- })
2118
-
2119
- expect(results.length).toBe(2)
2120
- expect(results.some((row) => row?.int === 25)).toBe(true)
2121
- expect(results.some((row) => row?.int === 30)).toBe(true)
2122
- })
2123
-
2124
- it('should handle exists with JSON fields and null array in object', async () => {
2125
- // Create test documents with JSON data using the adapter
2126
- await postgresAdapter.createObjects({
2127
- className: 'Test',
2128
- data: [
2129
- { object: { array: [{ string: 'John' }] }, int: 25 },
2130
- { object: { array: null }, int: 30 },
2131
- { object: null, int: 35 },
2132
- ],
2133
- context,
2134
- })
2135
-
2136
- const results = await postgresAdapter.getObjects({
2137
- className: 'Test',
2138
- // @ts-expect-error
2139
- where: { object: { array: { exists: true } } },
2140
- context,
2141
- })
2142
-
2143
- expect(results.length).toBe(1)
2144
- expect(results.some((row) => row?.int === 25)).toBe(true)
2145
- })
2146
-
2147
- it('should handle correctly equalTo and notEqualTo undefined', async () => {
2148
- // Create test documents with JSON data using the adapter
2149
- await postgresAdapter.createObjects({
2150
- className: 'Test',
2151
- // @ts-expect-error
2152
- data: [{ object: null, field1: 'John', float: 2.5 }, { int: 35 }],
2153
- context,
2154
- })
2155
-
2156
- const results = await postgresAdapter.getObjects({
2157
- className: 'Test',
2158
- where: { int: { equalTo: undefined } },
2159
- context,
2160
- })
2161
-
2162
- expect(results.length).toBe(1)
2163
- expect(results.some((row) => row?.field1 === 'John')).toBe(true)
2164
-
2165
- const results2 = await postgresAdapter.getObjects({
2166
- className: 'Test',
2167
- where: { int: { notEqualTo: undefined } },
2168
- context,
2169
- })
2170
-
2171
- expect(results2.length).toBe(1)
2172
- expect(results2.some((row) => row?.int === 35)).toBe(true)
2173
-
2174
- const results3 = await postgresAdapter.getObjects({
2175
- className: 'Test',
2176
- where: { int: { equalTo: undefined }, float: { greaterThan: 2 } },
2177
- context,
2178
- })
2179
-
2180
- expect(results3.length).toBe(1)
2181
- expect(results3.some((row) => row?.field1 === 'John')).toBe(true)
2182
- })
2183
- })