wabe-postgres 0.5.1 → 0.5.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.
- package/README.md +113 -39
- package/package.json +32 -30
- package/src/index.test.ts +2115 -0
- package/src/index.ts +752 -0
- package/tsconfig.json +31 -0
- package/utils/preload.ts +1 -1
- package/utils/testHelper.ts +110 -108
- package/bunfig.toml +0 -4
- package/dist/index.d.ts +0 -74
- package/dist/index.js +0 -5313
|
@@ -0,0 +1,2115 @@
|
|
|
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 create class', async () => {
|
|
397
|
+
const client = await postgresAdapter.pool.connect()
|
|
398
|
+
|
|
399
|
+
await client.query(`DROP TABLE IF EXISTS "Test" CASCADE`)
|
|
400
|
+
|
|
401
|
+
const initialCheck = await client.query(`
|
|
402
|
+
SELECT EXISTS (
|
|
403
|
+
SELECT FROM information_schema.tables
|
|
404
|
+
WHERE table_name = 'Test'
|
|
405
|
+
)
|
|
406
|
+
`)
|
|
407
|
+
|
|
408
|
+
expect(initialCheck.rows[0].exists).toBe(false)
|
|
409
|
+
|
|
410
|
+
await postgresAdapter.createClassIfNotExist('Test', context.wabe.config.schema || {})
|
|
411
|
+
|
|
412
|
+
const finalCheck = await client.query(`
|
|
413
|
+
SELECT EXISTS (
|
|
414
|
+
SELECT FROM information_schema.tables
|
|
415
|
+
WHERE table_name = 'Test'
|
|
416
|
+
)
|
|
417
|
+
`)
|
|
418
|
+
|
|
419
|
+
expect(finalCheck.rows[0].exists).toBe(true)
|
|
420
|
+
|
|
421
|
+
client.release()
|
|
422
|
+
})
|
|
423
|
+
|
|
424
|
+
it('should update table if a column is added after the first launch', async () => {
|
|
425
|
+
const port = await getPort()
|
|
426
|
+
const databaseId = uuid()
|
|
427
|
+
const wabe = new Wabe<any>({
|
|
428
|
+
isProduction: false,
|
|
429
|
+
rootKey:
|
|
430
|
+
'0uwFvUxM$ceFuF1aEtTtZMa7DUN2NZudqgY5ve5W*QCyb58cwMj9JeoaV@d#%29v&aJzswuudVU1%nAT+rxS0Bh&OkgBYc0PH18*',
|
|
431
|
+
database: {
|
|
432
|
+
adapter: new PostgresAdapter({
|
|
433
|
+
databaseUrl: 'postgresql://wabe:wabe@localhost:5432',
|
|
434
|
+
databaseName: databaseId,
|
|
435
|
+
}),
|
|
436
|
+
},
|
|
437
|
+
port,
|
|
438
|
+
security: {
|
|
439
|
+
disableCSRFProtection: true,
|
|
440
|
+
},
|
|
441
|
+
schema: {
|
|
442
|
+
classes: [
|
|
443
|
+
{
|
|
444
|
+
name: 'Test',
|
|
445
|
+
fields: {
|
|
446
|
+
field1: { type: 'String' },
|
|
447
|
+
},
|
|
448
|
+
},
|
|
449
|
+
],
|
|
450
|
+
},
|
|
451
|
+
})
|
|
452
|
+
|
|
453
|
+
await wabe.start()
|
|
454
|
+
|
|
455
|
+
const port2 = await getPort()
|
|
456
|
+
|
|
457
|
+
const wabe2 = new Wabe<any>({
|
|
458
|
+
isProduction: false,
|
|
459
|
+
rootKey:
|
|
460
|
+
'0uwFvUxM$ceFuF1aEtTtZMa7DUN2NZudqgY5ve5W*QCyb58cwMj9JeoaV@d#%29v&aJzswuudVU1%nAT+rxS0Bh&OkgBYc0PH18*',
|
|
461
|
+
database: {
|
|
462
|
+
adapter: new PostgresAdapter({
|
|
463
|
+
databaseUrl: 'postgresql://wabe:wabe@localhost:5432',
|
|
464
|
+
databaseName: databaseId,
|
|
465
|
+
}),
|
|
466
|
+
},
|
|
467
|
+
security: {
|
|
468
|
+
disableCSRFProtection: true,
|
|
469
|
+
},
|
|
470
|
+
port: port2,
|
|
471
|
+
schema: {
|
|
472
|
+
classes: [
|
|
473
|
+
{
|
|
474
|
+
name: 'Test',
|
|
475
|
+
fields: {
|
|
476
|
+
field1: { type: 'String' },
|
|
477
|
+
field2: { type: 'String' },
|
|
478
|
+
},
|
|
479
|
+
},
|
|
480
|
+
],
|
|
481
|
+
},
|
|
482
|
+
})
|
|
483
|
+
|
|
484
|
+
await wabe2.start()
|
|
485
|
+
|
|
486
|
+
await wabe2.controllers.database.createObject({
|
|
487
|
+
className: 'Test',
|
|
488
|
+
data: {
|
|
489
|
+
field1: 'test',
|
|
490
|
+
field2: 'test2',
|
|
491
|
+
},
|
|
492
|
+
context: {
|
|
493
|
+
wabe: wabe2,
|
|
494
|
+
isRoot: true,
|
|
495
|
+
},
|
|
496
|
+
})
|
|
497
|
+
|
|
498
|
+
const res = await wabe2.controllers.database.getObjects({
|
|
499
|
+
className: 'Test',
|
|
500
|
+
context: {
|
|
501
|
+
wabe: wabe2,
|
|
502
|
+
isRoot: true,
|
|
503
|
+
},
|
|
504
|
+
})
|
|
505
|
+
|
|
506
|
+
expect(res.length).toEqual(1)
|
|
507
|
+
expect(res[0]).toEqual(expect.objectContaining({ field1: 'test', field2: 'test2' }))
|
|
508
|
+
|
|
509
|
+
await wabe2.close()
|
|
510
|
+
|
|
511
|
+
await wabe.close()
|
|
512
|
+
})
|
|
513
|
+
|
|
514
|
+
it('should only return id on createObject if fields is empty', async () => {
|
|
515
|
+
const res = await postgresAdapter.createObject({
|
|
516
|
+
className: 'User',
|
|
517
|
+
data: {
|
|
518
|
+
name: 'John',
|
|
519
|
+
age: 20,
|
|
520
|
+
},
|
|
521
|
+
context,
|
|
522
|
+
select: {},
|
|
523
|
+
})
|
|
524
|
+
|
|
525
|
+
expect(res?.id).toBeDefined()
|
|
526
|
+
expect(res).toEqual({ id: expect.any(String) })
|
|
527
|
+
})
|
|
528
|
+
|
|
529
|
+
it('should only return an array of id on createObjects if fields is empty', async () => {
|
|
530
|
+
const res = await postgresAdapter.createObjects({
|
|
531
|
+
className: 'User',
|
|
532
|
+
data: [
|
|
533
|
+
{
|
|
534
|
+
name: 'John',
|
|
535
|
+
age: 20,
|
|
536
|
+
},
|
|
537
|
+
],
|
|
538
|
+
context,
|
|
539
|
+
select: {},
|
|
540
|
+
})
|
|
541
|
+
|
|
542
|
+
expect(res[0]?.id).toBeDefined()
|
|
543
|
+
expect(res).toEqual([{ id: expect.any(String) }])
|
|
544
|
+
})
|
|
545
|
+
|
|
546
|
+
it('should only return id on updateObject if fields is empty', async () => {
|
|
547
|
+
const insertedObject = await postgresAdapter.createObject({
|
|
548
|
+
className: 'User',
|
|
549
|
+
data: {
|
|
550
|
+
name: 'John',
|
|
551
|
+
age: 20,
|
|
552
|
+
},
|
|
553
|
+
context,
|
|
554
|
+
})
|
|
555
|
+
|
|
556
|
+
const res = await postgresAdapter.updateObject({
|
|
557
|
+
className: 'User',
|
|
558
|
+
id: insertedObject?.id || '',
|
|
559
|
+
data: { name: 'Doe' },
|
|
560
|
+
select: {},
|
|
561
|
+
context,
|
|
562
|
+
})
|
|
563
|
+
|
|
564
|
+
expect(res?.id).toBeDefined()
|
|
565
|
+
expect(res).toEqual({ id: expect.any(String) })
|
|
566
|
+
})
|
|
567
|
+
|
|
568
|
+
it('should only return an array of id on updateObjects if fields is empty', async () => {
|
|
569
|
+
await postgresAdapter.createObjects({
|
|
570
|
+
className: 'User',
|
|
571
|
+
data: [
|
|
572
|
+
{
|
|
573
|
+
name: 'John',
|
|
574
|
+
age: 20,
|
|
575
|
+
},
|
|
576
|
+
],
|
|
577
|
+
context,
|
|
578
|
+
})
|
|
579
|
+
|
|
580
|
+
const res = await postgresAdapter.updateObjects({
|
|
581
|
+
className: 'User',
|
|
582
|
+
where: {
|
|
583
|
+
name: { equalTo: 'John' },
|
|
584
|
+
},
|
|
585
|
+
data: { name: 'Doe' },
|
|
586
|
+
select: {},
|
|
587
|
+
context,
|
|
588
|
+
})
|
|
589
|
+
|
|
590
|
+
expect(res[0]?.id).toBeDefined()
|
|
591
|
+
expect(res).toEqual([{ id: expect.any(String) }])
|
|
592
|
+
})
|
|
593
|
+
|
|
594
|
+
it("should order the result by the field 'name' in ascending order", async () => {
|
|
595
|
+
await postgresAdapter.createObjects({
|
|
596
|
+
className: 'User',
|
|
597
|
+
data: [
|
|
598
|
+
{
|
|
599
|
+
name: 'A',
|
|
600
|
+
age: 20,
|
|
601
|
+
},
|
|
602
|
+
{
|
|
603
|
+
name: 'B',
|
|
604
|
+
age: 18,
|
|
605
|
+
},
|
|
606
|
+
],
|
|
607
|
+
select: {},
|
|
608
|
+
context,
|
|
609
|
+
})
|
|
610
|
+
|
|
611
|
+
const res = await postgresAdapter.getObjects({
|
|
612
|
+
className: 'User',
|
|
613
|
+
select: { name: true },
|
|
614
|
+
order: {
|
|
615
|
+
name: 'ASC',
|
|
616
|
+
},
|
|
617
|
+
context,
|
|
618
|
+
})
|
|
619
|
+
|
|
620
|
+
expect(res.map((user) => user?.name)).toEqual(['A', 'B'])
|
|
621
|
+
})
|
|
622
|
+
|
|
623
|
+
it("should order the result by the field 'name' in descending order", async () => {
|
|
624
|
+
await postgresAdapter.createObjects({
|
|
625
|
+
className: 'User',
|
|
626
|
+
data: [
|
|
627
|
+
{
|
|
628
|
+
name: 'A',
|
|
629
|
+
age: 20,
|
|
630
|
+
},
|
|
631
|
+
{
|
|
632
|
+
name: 'B',
|
|
633
|
+
age: 18,
|
|
634
|
+
},
|
|
635
|
+
],
|
|
636
|
+
select: {},
|
|
637
|
+
context,
|
|
638
|
+
})
|
|
639
|
+
|
|
640
|
+
const res = await postgresAdapter.getObjects({
|
|
641
|
+
className: 'User',
|
|
642
|
+
select: { name: true },
|
|
643
|
+
order: {
|
|
644
|
+
name: 'DESC',
|
|
645
|
+
},
|
|
646
|
+
context,
|
|
647
|
+
})
|
|
648
|
+
|
|
649
|
+
expect(res.map((user) => user?.name)).toEqual(['B', 'A'])
|
|
650
|
+
})
|
|
651
|
+
|
|
652
|
+
it("should order the result by the field 'age' and 'name' in descending order", async () => {
|
|
653
|
+
await postgresAdapter.createObjects({
|
|
654
|
+
className: 'User',
|
|
655
|
+
data: [
|
|
656
|
+
{
|
|
657
|
+
name: 'A',
|
|
658
|
+
age: 20,
|
|
659
|
+
},
|
|
660
|
+
{
|
|
661
|
+
name: 'B',
|
|
662
|
+
age: 18,
|
|
663
|
+
},
|
|
664
|
+
],
|
|
665
|
+
select: {},
|
|
666
|
+
context,
|
|
667
|
+
})
|
|
668
|
+
|
|
669
|
+
const res = await postgresAdapter.getObjects({
|
|
670
|
+
className: 'User',
|
|
671
|
+
select: { name: true },
|
|
672
|
+
order: {
|
|
673
|
+
age: 'ASC',
|
|
674
|
+
name: 'DESC',
|
|
675
|
+
},
|
|
676
|
+
context,
|
|
677
|
+
})
|
|
678
|
+
|
|
679
|
+
expect(res.map((user) => user?.name)).toEqual(['B', 'A'])
|
|
680
|
+
})
|
|
681
|
+
|
|
682
|
+
it('should count all elements corresponds to where condition in collection', async () => {
|
|
683
|
+
const res = await postgresAdapter.count({
|
|
684
|
+
className: 'User',
|
|
685
|
+
context,
|
|
686
|
+
})
|
|
687
|
+
|
|
688
|
+
expect(res).toEqual(0)
|
|
689
|
+
|
|
690
|
+
await postgresAdapter.createObjects({
|
|
691
|
+
className: 'User',
|
|
692
|
+
data: [
|
|
693
|
+
{
|
|
694
|
+
name: 'Lucas',
|
|
695
|
+
age: 20,
|
|
696
|
+
},
|
|
697
|
+
{
|
|
698
|
+
name: 'LucasBis',
|
|
699
|
+
age: 18,
|
|
700
|
+
},
|
|
701
|
+
],
|
|
702
|
+
select: {},
|
|
703
|
+
context,
|
|
704
|
+
})
|
|
705
|
+
|
|
706
|
+
const res2 = await postgresAdapter.count({
|
|
707
|
+
className: 'User',
|
|
708
|
+
context,
|
|
709
|
+
})
|
|
710
|
+
|
|
711
|
+
expect(res2).toEqual(2)
|
|
712
|
+
|
|
713
|
+
const res3 = await postgresAdapter.count({
|
|
714
|
+
className: 'User',
|
|
715
|
+
context,
|
|
716
|
+
where: { age: { equalTo: 20 } },
|
|
717
|
+
})
|
|
718
|
+
|
|
719
|
+
expect(res3).toEqual(1)
|
|
720
|
+
})
|
|
721
|
+
|
|
722
|
+
it('should clear all database except Role collection', async () => {
|
|
723
|
+
await postgresAdapter.createObjects({
|
|
724
|
+
className: 'User',
|
|
725
|
+
data: [
|
|
726
|
+
{
|
|
727
|
+
name: 'Lucas',
|
|
728
|
+
age: 20,
|
|
729
|
+
},
|
|
730
|
+
{
|
|
731
|
+
name: 'LucasBis',
|
|
732
|
+
age: 18,
|
|
733
|
+
},
|
|
734
|
+
],
|
|
735
|
+
select: {},
|
|
736
|
+
context,
|
|
737
|
+
})
|
|
738
|
+
|
|
739
|
+
await postgresAdapter.createObjects({
|
|
740
|
+
className: '_Session',
|
|
741
|
+
data: [
|
|
742
|
+
{
|
|
743
|
+
accessTokenEncrypted: 'accessTokenEncrypted',
|
|
744
|
+
refreshTokenEncrypted: 'refreshTokenEncrypted',
|
|
745
|
+
accessTokenExpiresAt: new Date(),
|
|
746
|
+
refreshTokenExpiresAt: new Date(),
|
|
747
|
+
user: 'id',
|
|
748
|
+
},
|
|
749
|
+
{
|
|
750
|
+
accessTokenEncrypted: 'accessTokenEncrypted',
|
|
751
|
+
refreshTokenEncrypted: 'refreshTokenEncrypted',
|
|
752
|
+
accessTokenExpiresAt: new Date(),
|
|
753
|
+
refreshTokenExpiresAt: new Date(),
|
|
754
|
+
user: 'id',
|
|
755
|
+
},
|
|
756
|
+
],
|
|
757
|
+
select: {},
|
|
758
|
+
context,
|
|
759
|
+
})
|
|
760
|
+
|
|
761
|
+
await postgresAdapter.clearDatabase()
|
|
762
|
+
|
|
763
|
+
const res = await postgresAdapter.getObjects({
|
|
764
|
+
className: 'User',
|
|
765
|
+
select: {},
|
|
766
|
+
context,
|
|
767
|
+
})
|
|
768
|
+
|
|
769
|
+
const res2 = await postgresAdapter.getObjects({
|
|
770
|
+
className: '_Session',
|
|
771
|
+
select: {},
|
|
772
|
+
context,
|
|
773
|
+
})
|
|
774
|
+
|
|
775
|
+
const res3 = await postgresAdapter.getObjects({
|
|
776
|
+
className: 'Role',
|
|
777
|
+
select: { id: true },
|
|
778
|
+
context,
|
|
779
|
+
})
|
|
780
|
+
|
|
781
|
+
expect(res.length).toEqual(0)
|
|
782
|
+
expect(res2.length).toEqual(0)
|
|
783
|
+
expect(res3.length).not.toEqual(0)
|
|
784
|
+
})
|
|
785
|
+
|
|
786
|
+
it('should support where on getObject for additional check (acl for example)', async () => {
|
|
787
|
+
const insertedObjects = await postgresAdapter.createObjects({
|
|
788
|
+
className: 'User',
|
|
789
|
+
data: [
|
|
790
|
+
{
|
|
791
|
+
name: 'Lucas',
|
|
792
|
+
age: 20,
|
|
793
|
+
},
|
|
794
|
+
{
|
|
795
|
+
name: 'LucasBis',
|
|
796
|
+
age: 18,
|
|
797
|
+
},
|
|
798
|
+
],
|
|
799
|
+
select: { id: true },
|
|
800
|
+
context,
|
|
801
|
+
})
|
|
802
|
+
|
|
803
|
+
expect(
|
|
804
|
+
postgresAdapter.getObject({
|
|
805
|
+
className: 'User',
|
|
806
|
+
where: {
|
|
807
|
+
name: { equalTo: 'InvalidName' },
|
|
808
|
+
},
|
|
809
|
+
id: insertedObjects[0]?.id || '',
|
|
810
|
+
context,
|
|
811
|
+
}),
|
|
812
|
+
).rejects.toThrow('Object not found')
|
|
813
|
+
|
|
814
|
+
const res = await postgresAdapter.getObject({
|
|
815
|
+
className: 'User',
|
|
816
|
+
where: {
|
|
817
|
+
name: { equalTo: 'Lucas' },
|
|
818
|
+
},
|
|
819
|
+
id: insertedObjects[0]?.id || '',
|
|
820
|
+
context,
|
|
821
|
+
})
|
|
822
|
+
|
|
823
|
+
expect(res?.name).toEqual('Lucas')
|
|
824
|
+
})
|
|
825
|
+
|
|
826
|
+
it('should support where on updateObject for additional check (acl for example)', async () => {
|
|
827
|
+
const insertedObjects = await postgresAdapter.createObjects({
|
|
828
|
+
className: 'User',
|
|
829
|
+
data: [
|
|
830
|
+
{
|
|
831
|
+
name: 'Lucas',
|
|
832
|
+
age: 20,
|
|
833
|
+
},
|
|
834
|
+
{
|
|
835
|
+
name: 'LucasBis',
|
|
836
|
+
age: 18,
|
|
837
|
+
},
|
|
838
|
+
],
|
|
839
|
+
select: { id: true },
|
|
840
|
+
context,
|
|
841
|
+
})
|
|
842
|
+
|
|
843
|
+
expect(
|
|
844
|
+
postgresAdapter.updateObject({
|
|
845
|
+
className: 'User',
|
|
846
|
+
where: {
|
|
847
|
+
name: { equalTo: 'InvalidName' },
|
|
848
|
+
},
|
|
849
|
+
id: insertedObjects[0]?.id || '',
|
|
850
|
+
context,
|
|
851
|
+
data: { name: 'Lucas2' },
|
|
852
|
+
}),
|
|
853
|
+
).rejects.toThrow('Object not found')
|
|
854
|
+
|
|
855
|
+
const { id } = await postgresAdapter.updateObject({
|
|
856
|
+
className: 'User',
|
|
857
|
+
where: {
|
|
858
|
+
name: { equalTo: 'Lucas' },
|
|
859
|
+
},
|
|
860
|
+
id: insertedObjects[0]?.id || '',
|
|
861
|
+
context,
|
|
862
|
+
data: { name: 'Lucas2' },
|
|
863
|
+
})
|
|
864
|
+
|
|
865
|
+
const updatedObject = await postgresAdapter.getObject({
|
|
866
|
+
className: 'User',
|
|
867
|
+
id,
|
|
868
|
+
select: { id: true, name: true },
|
|
869
|
+
context,
|
|
870
|
+
})
|
|
871
|
+
|
|
872
|
+
expect(updatedObject?.name).toEqual('Lucas2')
|
|
873
|
+
})
|
|
874
|
+
|
|
875
|
+
it('should support where on delete for additional check (acl for example)', async () => {
|
|
876
|
+
const insertedObjects = await postgresAdapter.createObjects({
|
|
877
|
+
className: 'User',
|
|
878
|
+
data: [
|
|
879
|
+
{
|
|
880
|
+
name: 'Lucas',
|
|
881
|
+
age: 20,
|
|
882
|
+
},
|
|
883
|
+
{
|
|
884
|
+
name: 'LucasBis',
|
|
885
|
+
age: 18,
|
|
886
|
+
},
|
|
887
|
+
],
|
|
888
|
+
select: { id: true },
|
|
889
|
+
context,
|
|
890
|
+
})
|
|
891
|
+
|
|
892
|
+
expect(
|
|
893
|
+
postgresAdapter.deleteObject({
|
|
894
|
+
className: 'User',
|
|
895
|
+
where: {
|
|
896
|
+
name: { equalTo: 'InvalidName' },
|
|
897
|
+
},
|
|
898
|
+
id: insertedObjects[0]?.id || '',
|
|
899
|
+
context,
|
|
900
|
+
}),
|
|
901
|
+
).rejects.toThrow('Object not found')
|
|
902
|
+
|
|
903
|
+
await postgresAdapter.deleteObject({
|
|
904
|
+
className: 'User',
|
|
905
|
+
where: {
|
|
906
|
+
name: { equalTo: 'Lucas' },
|
|
907
|
+
},
|
|
908
|
+
id: insertedObjects[0]?.id || '',
|
|
909
|
+
context,
|
|
910
|
+
})
|
|
911
|
+
})
|
|
912
|
+
|
|
913
|
+
it('should support notEqualTo on id', async () => {
|
|
914
|
+
const insertedObjects = await postgresAdapter.createObjects({
|
|
915
|
+
className: 'User',
|
|
916
|
+
data: [
|
|
917
|
+
{
|
|
918
|
+
name: 'Lucas',
|
|
919
|
+
age: 20,
|
|
920
|
+
},
|
|
921
|
+
{
|
|
922
|
+
name: 'LucasBis',
|
|
923
|
+
age: 18,
|
|
924
|
+
},
|
|
925
|
+
],
|
|
926
|
+
select: { id: true },
|
|
927
|
+
context,
|
|
928
|
+
})
|
|
929
|
+
|
|
930
|
+
const res = await postgresAdapter.getObjects({
|
|
931
|
+
className: 'User',
|
|
932
|
+
where: {
|
|
933
|
+
id: { notEqualTo: insertedObjects[0]?.id },
|
|
934
|
+
},
|
|
935
|
+
context,
|
|
936
|
+
})
|
|
937
|
+
|
|
938
|
+
expect(res.length).toEqual(1)
|
|
939
|
+
})
|
|
940
|
+
|
|
941
|
+
it('should support equalTo on id', async () => {
|
|
942
|
+
const insertedObjects = await postgresAdapter.createObjects({
|
|
943
|
+
className: 'User',
|
|
944
|
+
data: [
|
|
945
|
+
{
|
|
946
|
+
name: 'Lucas',
|
|
947
|
+
age: 20,
|
|
948
|
+
},
|
|
949
|
+
{
|
|
950
|
+
name: 'LucasBis',
|
|
951
|
+
age: 18,
|
|
952
|
+
},
|
|
953
|
+
],
|
|
954
|
+
select: { id: true },
|
|
955
|
+
context,
|
|
956
|
+
})
|
|
957
|
+
|
|
958
|
+
const res = await postgresAdapter.getObjects({
|
|
959
|
+
className: 'User',
|
|
960
|
+
where: {
|
|
961
|
+
id: { equalTo: insertedObjects[0]?.id },
|
|
962
|
+
},
|
|
963
|
+
context,
|
|
964
|
+
})
|
|
965
|
+
|
|
966
|
+
expect(res.length).toEqual(1)
|
|
967
|
+
})
|
|
968
|
+
|
|
969
|
+
it('should support in aggregation on id', async () => {
|
|
970
|
+
const insertedObjects = await postgresAdapter.createObjects({
|
|
971
|
+
className: 'User',
|
|
972
|
+
data: [
|
|
973
|
+
{
|
|
974
|
+
name: 'Lucas',
|
|
975
|
+
age: 20,
|
|
976
|
+
},
|
|
977
|
+
{
|
|
978
|
+
name: 'LucasBis',
|
|
979
|
+
age: 18,
|
|
980
|
+
},
|
|
981
|
+
],
|
|
982
|
+
select: { id: true },
|
|
983
|
+
context,
|
|
984
|
+
})
|
|
985
|
+
|
|
986
|
+
const res = await postgresAdapter.getObjects({
|
|
987
|
+
className: 'User',
|
|
988
|
+
where: {
|
|
989
|
+
id: {
|
|
990
|
+
in: insertedObjects.map((obj) => obj?.id).filter(notEmpty),
|
|
991
|
+
},
|
|
992
|
+
},
|
|
993
|
+
context,
|
|
994
|
+
})
|
|
995
|
+
|
|
996
|
+
expect(res.length).toEqual(2)
|
|
997
|
+
})
|
|
998
|
+
|
|
999
|
+
it('should support notIn aggregation on id', async () => {
|
|
1000
|
+
const insertedObjects = await postgresAdapter.createObjects({
|
|
1001
|
+
className: 'User',
|
|
1002
|
+
data: [
|
|
1003
|
+
{
|
|
1004
|
+
name: 'Lucas',
|
|
1005
|
+
age: 20,
|
|
1006
|
+
},
|
|
1007
|
+
{
|
|
1008
|
+
name: 'LucasBis',
|
|
1009
|
+
age: 18,
|
|
1010
|
+
},
|
|
1011
|
+
],
|
|
1012
|
+
select: { id: true },
|
|
1013
|
+
context,
|
|
1014
|
+
})
|
|
1015
|
+
|
|
1016
|
+
const res = await postgresAdapter.getObjects({
|
|
1017
|
+
className: 'User',
|
|
1018
|
+
where: {
|
|
1019
|
+
id: {
|
|
1020
|
+
notIn: insertedObjects.map((obj) => obj?.id).filter(notEmpty),
|
|
1021
|
+
},
|
|
1022
|
+
},
|
|
1023
|
+
context,
|
|
1024
|
+
})
|
|
1025
|
+
|
|
1026
|
+
expect(res.length).toEqual(0)
|
|
1027
|
+
})
|
|
1028
|
+
|
|
1029
|
+
it('should always return id', async () => {
|
|
1030
|
+
const insertedObjects = await postgresAdapter.createObjects({
|
|
1031
|
+
className: 'User',
|
|
1032
|
+
data: [
|
|
1033
|
+
{
|
|
1034
|
+
name: 'Lucas',
|
|
1035
|
+
age: 20,
|
|
1036
|
+
},
|
|
1037
|
+
],
|
|
1038
|
+
context,
|
|
1039
|
+
select: { age: true, id: true },
|
|
1040
|
+
})
|
|
1041
|
+
|
|
1042
|
+
if (!insertedObjects) fail()
|
|
1043
|
+
|
|
1044
|
+
expect(insertedObjects[0]?.id).toBeDefined()
|
|
1045
|
+
})
|
|
1046
|
+
|
|
1047
|
+
it('should return id if no fields is specified', async () => {
|
|
1048
|
+
const insertedObjects = await postgresAdapter.createObjects({
|
|
1049
|
+
className: 'User',
|
|
1050
|
+
data: [
|
|
1051
|
+
{
|
|
1052
|
+
name: 'Lucas',
|
|
1053
|
+
age: 20,
|
|
1054
|
+
},
|
|
1055
|
+
],
|
|
1056
|
+
context,
|
|
1057
|
+
})
|
|
1058
|
+
|
|
1059
|
+
if (!insertedObjects) fail()
|
|
1060
|
+
|
|
1061
|
+
expect(insertedObjects[0]?.id).toBeDefined()
|
|
1062
|
+
})
|
|
1063
|
+
|
|
1064
|
+
it('should getObjects using id', async () => {
|
|
1065
|
+
const insertedObjects = await postgresAdapter.createObjects({
|
|
1066
|
+
className: 'User',
|
|
1067
|
+
data: [
|
|
1068
|
+
{
|
|
1069
|
+
name: 'Lucas',
|
|
1070
|
+
age: 20,
|
|
1071
|
+
},
|
|
1072
|
+
{
|
|
1073
|
+
name: 'Lucas1',
|
|
1074
|
+
age: 20,
|
|
1075
|
+
},
|
|
1076
|
+
],
|
|
1077
|
+
select: { name: true, id: true },
|
|
1078
|
+
context,
|
|
1079
|
+
})
|
|
1080
|
+
|
|
1081
|
+
if (!insertedObjects) fail()
|
|
1082
|
+
|
|
1083
|
+
const res = await postgresAdapter.getObjects({
|
|
1084
|
+
className: 'User',
|
|
1085
|
+
where: {
|
|
1086
|
+
id: {
|
|
1087
|
+
equalTo: insertedObjects[0]?.id || '',
|
|
1088
|
+
},
|
|
1089
|
+
},
|
|
1090
|
+
context,
|
|
1091
|
+
})
|
|
1092
|
+
|
|
1093
|
+
expect(res.length).toEqual(1)
|
|
1094
|
+
})
|
|
1095
|
+
|
|
1096
|
+
it('should getObjects with limit and offset', async () => {
|
|
1097
|
+
await postgresAdapter.createObjects({
|
|
1098
|
+
className: 'User',
|
|
1099
|
+
data: [
|
|
1100
|
+
{
|
|
1101
|
+
name: 'John',
|
|
1102
|
+
age: 20,
|
|
1103
|
+
},
|
|
1104
|
+
{
|
|
1105
|
+
name: 'John1',
|
|
1106
|
+
age: 20,
|
|
1107
|
+
},
|
|
1108
|
+
{
|
|
1109
|
+
name: 'John2',
|
|
1110
|
+
age: 20,
|
|
1111
|
+
},
|
|
1112
|
+
{
|
|
1113
|
+
name: 'John3',
|
|
1114
|
+
age: 20,
|
|
1115
|
+
},
|
|
1116
|
+
{
|
|
1117
|
+
name: 'John4',
|
|
1118
|
+
age: 20,
|
|
1119
|
+
},
|
|
1120
|
+
],
|
|
1121
|
+
select: { name: true, id: true },
|
|
1122
|
+
context,
|
|
1123
|
+
})
|
|
1124
|
+
|
|
1125
|
+
const res = await postgresAdapter.getObjects({
|
|
1126
|
+
className: 'User',
|
|
1127
|
+
select: { name: true },
|
|
1128
|
+
first: 2,
|
|
1129
|
+
offset: 2,
|
|
1130
|
+
context,
|
|
1131
|
+
})
|
|
1132
|
+
|
|
1133
|
+
expect(res.length).toEqual(2)
|
|
1134
|
+
expect(res[0]?.name).toEqual('John2')
|
|
1135
|
+
expect(res[1]?.name).toEqual('John3')
|
|
1136
|
+
})
|
|
1137
|
+
|
|
1138
|
+
it('should get all the objects without limit and without offset', async () => {
|
|
1139
|
+
await postgresAdapter.createObjects({
|
|
1140
|
+
className: 'User',
|
|
1141
|
+
data: [
|
|
1142
|
+
{
|
|
1143
|
+
name: 'John',
|
|
1144
|
+
age: 20,
|
|
1145
|
+
},
|
|
1146
|
+
{
|
|
1147
|
+
name: 'John1',
|
|
1148
|
+
age: 20,
|
|
1149
|
+
},
|
|
1150
|
+
{
|
|
1151
|
+
name: 'John2',
|
|
1152
|
+
age: 20,
|
|
1153
|
+
},
|
|
1154
|
+
{
|
|
1155
|
+
name: 'John3',
|
|
1156
|
+
age: 20,
|
|
1157
|
+
},
|
|
1158
|
+
{
|
|
1159
|
+
name: 'John4',
|
|
1160
|
+
age: 20,
|
|
1161
|
+
},
|
|
1162
|
+
],
|
|
1163
|
+
select: { name: true, id: true },
|
|
1164
|
+
context,
|
|
1165
|
+
})
|
|
1166
|
+
|
|
1167
|
+
const res = await postgresAdapter.getObjects({
|
|
1168
|
+
className: 'User',
|
|
1169
|
+
select: { name: true },
|
|
1170
|
+
context,
|
|
1171
|
+
where: {},
|
|
1172
|
+
})
|
|
1173
|
+
|
|
1174
|
+
expect(res.length).toEqual(5)
|
|
1175
|
+
})
|
|
1176
|
+
|
|
1177
|
+
it('should get the id of an object', async () => {
|
|
1178
|
+
const insertedObject = await postgresAdapter.createObject({
|
|
1179
|
+
className: 'User',
|
|
1180
|
+
data: {
|
|
1181
|
+
name: 'John',
|
|
1182
|
+
age: 20,
|
|
1183
|
+
},
|
|
1184
|
+
select: { name: true, id: true },
|
|
1185
|
+
context,
|
|
1186
|
+
})
|
|
1187
|
+
|
|
1188
|
+
const res = await postgresAdapter.getObject({
|
|
1189
|
+
id: insertedObject?.id.toString() || '',
|
|
1190
|
+
className: 'User',
|
|
1191
|
+
select: { id: true },
|
|
1192
|
+
context,
|
|
1193
|
+
})
|
|
1194
|
+
|
|
1195
|
+
expect(res?.id).toEqual(insertedObject?.id || '')
|
|
1196
|
+
})
|
|
1197
|
+
|
|
1198
|
+
it('should get all fields when no select is specified', async () => {
|
|
1199
|
+
const insertedObject = await postgresAdapter.createObject({
|
|
1200
|
+
className: 'User',
|
|
1201
|
+
data: {
|
|
1202
|
+
name: 'John',
|
|
1203
|
+
age: 20,
|
|
1204
|
+
},
|
|
1205
|
+
select: { name: true, id: true },
|
|
1206
|
+
context,
|
|
1207
|
+
})
|
|
1208
|
+
|
|
1209
|
+
if (!insertedObject) fail()
|
|
1210
|
+
|
|
1211
|
+
const id = insertedObject.id
|
|
1212
|
+
|
|
1213
|
+
expect(id).toBeDefined()
|
|
1214
|
+
|
|
1215
|
+
const res = await postgresAdapter.getObject({
|
|
1216
|
+
className: 'User',
|
|
1217
|
+
id: id.toString(),
|
|
1218
|
+
context,
|
|
1219
|
+
})
|
|
1220
|
+
|
|
1221
|
+
expect(res).toEqual(
|
|
1222
|
+
expect.objectContaining({
|
|
1223
|
+
name: 'John',
|
|
1224
|
+
age: 20,
|
|
1225
|
+
id: expect.any(String),
|
|
1226
|
+
}),
|
|
1227
|
+
)
|
|
1228
|
+
})
|
|
1229
|
+
|
|
1230
|
+
it('should get one object with specific field', async () => {
|
|
1231
|
+
const insertedObject = await postgresAdapter.createObject({
|
|
1232
|
+
className: 'User',
|
|
1233
|
+
data: {
|
|
1234
|
+
name: 'John',
|
|
1235
|
+
age: 20,
|
|
1236
|
+
},
|
|
1237
|
+
select: { name: true, id: true },
|
|
1238
|
+
context,
|
|
1239
|
+
})
|
|
1240
|
+
|
|
1241
|
+
if (!insertedObject) fail()
|
|
1242
|
+
|
|
1243
|
+
const id = insertedObject.id
|
|
1244
|
+
|
|
1245
|
+
expect(id).toBeDefined()
|
|
1246
|
+
|
|
1247
|
+
const field = await postgresAdapter.getObject({
|
|
1248
|
+
className: 'User',
|
|
1249
|
+
id: id.toString(),
|
|
1250
|
+
select: { name: true, id: true },
|
|
1251
|
+
context,
|
|
1252
|
+
})
|
|
1253
|
+
|
|
1254
|
+
expect(field).toEqual({
|
|
1255
|
+
name: 'John',
|
|
1256
|
+
id: expect.any(String),
|
|
1257
|
+
})
|
|
1258
|
+
})
|
|
1259
|
+
|
|
1260
|
+
it('should get all object with specific field', async () => {
|
|
1261
|
+
const objects = await postgresAdapter.getObjects({
|
|
1262
|
+
className: 'User',
|
|
1263
|
+
select: { name: true },
|
|
1264
|
+
context,
|
|
1265
|
+
})
|
|
1266
|
+
|
|
1267
|
+
expect(objects.length).toEqual(0)
|
|
1268
|
+
|
|
1269
|
+
await postgresAdapter.createObject({
|
|
1270
|
+
className: 'User',
|
|
1271
|
+
data: {
|
|
1272
|
+
name: 'John1',
|
|
1273
|
+
age: 20,
|
|
1274
|
+
},
|
|
1275
|
+
select: { name: true },
|
|
1276
|
+
context,
|
|
1277
|
+
})
|
|
1278
|
+
|
|
1279
|
+
await postgresAdapter.createObject({
|
|
1280
|
+
className: 'User',
|
|
1281
|
+
data: {
|
|
1282
|
+
name: 'John2',
|
|
1283
|
+
age: 20,
|
|
1284
|
+
},
|
|
1285
|
+
select: { name: true },
|
|
1286
|
+
context,
|
|
1287
|
+
})
|
|
1288
|
+
|
|
1289
|
+
const objects2 = await postgresAdapter.getObjects({
|
|
1290
|
+
className: 'User',
|
|
1291
|
+
select: { name: true, id: true },
|
|
1292
|
+
context,
|
|
1293
|
+
})
|
|
1294
|
+
|
|
1295
|
+
expect(objects2.length).toEqual(2)
|
|
1296
|
+
expect(objects2).toEqual([
|
|
1297
|
+
{
|
|
1298
|
+
id: expect.any(String),
|
|
1299
|
+
name: 'John1',
|
|
1300
|
+
},
|
|
1301
|
+
{
|
|
1302
|
+
id: expect.any(String),
|
|
1303
|
+
name: 'John2',
|
|
1304
|
+
},
|
|
1305
|
+
])
|
|
1306
|
+
|
|
1307
|
+
const objects3 = await postgresAdapter.getObjects({
|
|
1308
|
+
className: 'User',
|
|
1309
|
+
select: { name: true, id: true, age: true },
|
|
1310
|
+
context,
|
|
1311
|
+
})
|
|
1312
|
+
|
|
1313
|
+
expect(objects3.length).toEqual(2)
|
|
1314
|
+
expect(objects3).toEqual([
|
|
1315
|
+
{
|
|
1316
|
+
id: expect.any(String),
|
|
1317
|
+
name: 'John1',
|
|
1318
|
+
age: 20,
|
|
1319
|
+
},
|
|
1320
|
+
{
|
|
1321
|
+
id: expect.any(String),
|
|
1322
|
+
name: 'John2',
|
|
1323
|
+
age: 20,
|
|
1324
|
+
},
|
|
1325
|
+
])
|
|
1326
|
+
})
|
|
1327
|
+
|
|
1328
|
+
it('should get all objects with where filter', async () => {
|
|
1329
|
+
await postgresAdapter.createObject({
|
|
1330
|
+
className: 'User',
|
|
1331
|
+
data: {
|
|
1332
|
+
name: 'John1',
|
|
1333
|
+
age: 20,
|
|
1334
|
+
},
|
|
1335
|
+
select: { name: true },
|
|
1336
|
+
context,
|
|
1337
|
+
})
|
|
1338
|
+
|
|
1339
|
+
await postgresAdapter.createObject({
|
|
1340
|
+
className: 'User',
|
|
1341
|
+
data: {
|
|
1342
|
+
name: 'John2',
|
|
1343
|
+
age: 20,
|
|
1344
|
+
},
|
|
1345
|
+
select: { name: true },
|
|
1346
|
+
context,
|
|
1347
|
+
})
|
|
1348
|
+
|
|
1349
|
+
// OR statement
|
|
1350
|
+
expect(
|
|
1351
|
+
await postgresAdapter.getObjects({
|
|
1352
|
+
className: 'User',
|
|
1353
|
+
// @ts-expect-error
|
|
1354
|
+
where: {
|
|
1355
|
+
OR: [
|
|
1356
|
+
{
|
|
1357
|
+
name: { equalTo: 'John1' },
|
|
1358
|
+
},
|
|
1359
|
+
{
|
|
1360
|
+
name: { equalTo: 'John2' },
|
|
1361
|
+
},
|
|
1362
|
+
],
|
|
1363
|
+
},
|
|
1364
|
+
select: { name: true, id: true, age: true },
|
|
1365
|
+
context,
|
|
1366
|
+
}),
|
|
1367
|
+
).toEqual([
|
|
1368
|
+
{
|
|
1369
|
+
id: expect.any(String),
|
|
1370
|
+
name: 'John1',
|
|
1371
|
+
age: 20,
|
|
1372
|
+
},
|
|
1373
|
+
{
|
|
1374
|
+
id: expect.any(String),
|
|
1375
|
+
name: 'John2',
|
|
1376
|
+
age: 20,
|
|
1377
|
+
},
|
|
1378
|
+
])
|
|
1379
|
+
|
|
1380
|
+
expect(
|
|
1381
|
+
await postgresAdapter.getObjects({
|
|
1382
|
+
className: 'User',
|
|
1383
|
+
// @ts-expect-error
|
|
1384
|
+
where: {
|
|
1385
|
+
OR: [
|
|
1386
|
+
{
|
|
1387
|
+
name: { equalTo: 'John1' },
|
|
1388
|
+
},
|
|
1389
|
+
{
|
|
1390
|
+
name: { equalTo: 'John3' },
|
|
1391
|
+
},
|
|
1392
|
+
],
|
|
1393
|
+
},
|
|
1394
|
+
select: { name: true, id: true, age: true },
|
|
1395
|
+
context,
|
|
1396
|
+
}),
|
|
1397
|
+
).toEqual([
|
|
1398
|
+
{
|
|
1399
|
+
id: expect.any(String),
|
|
1400
|
+
name: 'John1',
|
|
1401
|
+
age: 20,
|
|
1402
|
+
},
|
|
1403
|
+
])
|
|
1404
|
+
|
|
1405
|
+
// AND statement
|
|
1406
|
+
expect(
|
|
1407
|
+
await postgresAdapter.getObjects({
|
|
1408
|
+
className: 'User',
|
|
1409
|
+
// @ts-expect-error
|
|
1410
|
+
where: {
|
|
1411
|
+
AND: [
|
|
1412
|
+
{
|
|
1413
|
+
name: { equalTo: 'John1' },
|
|
1414
|
+
},
|
|
1415
|
+
{
|
|
1416
|
+
age: { equalTo: 20 },
|
|
1417
|
+
},
|
|
1418
|
+
],
|
|
1419
|
+
},
|
|
1420
|
+
select: { name: true, id: true, age: true },
|
|
1421
|
+
context,
|
|
1422
|
+
}),
|
|
1423
|
+
).toEqual([
|
|
1424
|
+
{
|
|
1425
|
+
id: expect.any(String),
|
|
1426
|
+
name: 'John1',
|
|
1427
|
+
age: 20,
|
|
1428
|
+
},
|
|
1429
|
+
])
|
|
1430
|
+
|
|
1431
|
+
expect(
|
|
1432
|
+
await postgresAdapter.getObjects({
|
|
1433
|
+
className: 'User',
|
|
1434
|
+
// @ts-expect-error
|
|
1435
|
+
where: {
|
|
1436
|
+
AND: [
|
|
1437
|
+
{
|
|
1438
|
+
name: { equalTo: 'John1' },
|
|
1439
|
+
},
|
|
1440
|
+
{
|
|
1441
|
+
age: { equalTo: 10 },
|
|
1442
|
+
},
|
|
1443
|
+
],
|
|
1444
|
+
},
|
|
1445
|
+
context,
|
|
1446
|
+
}),
|
|
1447
|
+
).toEqual([])
|
|
1448
|
+
|
|
1449
|
+
// Equal to statement
|
|
1450
|
+
expect(
|
|
1451
|
+
await postgresAdapter.getObjects({
|
|
1452
|
+
className: 'User',
|
|
1453
|
+
where: {
|
|
1454
|
+
name: { equalTo: 'John1' },
|
|
1455
|
+
},
|
|
1456
|
+
select: { name: true, id: true, age: true },
|
|
1457
|
+
context,
|
|
1458
|
+
}),
|
|
1459
|
+
).toEqual([
|
|
1460
|
+
{
|
|
1461
|
+
id: expect.any(String),
|
|
1462
|
+
name: 'John1',
|
|
1463
|
+
age: 20,
|
|
1464
|
+
},
|
|
1465
|
+
])
|
|
1466
|
+
|
|
1467
|
+
expect(
|
|
1468
|
+
await postgresAdapter.getObjects({
|
|
1469
|
+
className: 'User',
|
|
1470
|
+
where: {
|
|
1471
|
+
age: { greaterThan: 21 },
|
|
1472
|
+
},
|
|
1473
|
+
context,
|
|
1474
|
+
}),
|
|
1475
|
+
).toEqual([])
|
|
1476
|
+
|
|
1477
|
+
// Not equal to statement
|
|
1478
|
+
expect(
|
|
1479
|
+
await postgresAdapter.getObjects({
|
|
1480
|
+
className: 'User',
|
|
1481
|
+
where: {
|
|
1482
|
+
name: { notEqualTo: 'John1' },
|
|
1483
|
+
},
|
|
1484
|
+
select: { name: true, id: true, age: true },
|
|
1485
|
+
context,
|
|
1486
|
+
}),
|
|
1487
|
+
).toEqual([
|
|
1488
|
+
{
|
|
1489
|
+
id: expect.any(String),
|
|
1490
|
+
name: 'John2',
|
|
1491
|
+
age: 20,
|
|
1492
|
+
},
|
|
1493
|
+
])
|
|
1494
|
+
|
|
1495
|
+
// Less than to statement on number
|
|
1496
|
+
expect(
|
|
1497
|
+
await postgresAdapter.getObjects({
|
|
1498
|
+
className: 'User',
|
|
1499
|
+
where: {
|
|
1500
|
+
age: { lessThan: 30 },
|
|
1501
|
+
},
|
|
1502
|
+
select: { name: true, id: true, age: true },
|
|
1503
|
+
context,
|
|
1504
|
+
}),
|
|
1505
|
+
).toEqual([
|
|
1506
|
+
{
|
|
1507
|
+
id: expect.any(String),
|
|
1508
|
+
name: 'John1',
|
|
1509
|
+
age: 20,
|
|
1510
|
+
},
|
|
1511
|
+
{
|
|
1512
|
+
id: expect.any(String),
|
|
1513
|
+
name: 'John2',
|
|
1514
|
+
age: 20,
|
|
1515
|
+
},
|
|
1516
|
+
])
|
|
1517
|
+
|
|
1518
|
+
// Equal to statement on number
|
|
1519
|
+
expect(
|
|
1520
|
+
await postgresAdapter.getObjects({
|
|
1521
|
+
className: 'User',
|
|
1522
|
+
where: {
|
|
1523
|
+
age: { equalTo: 20 },
|
|
1524
|
+
},
|
|
1525
|
+
select: { name: true, id: true, age: true },
|
|
1526
|
+
context,
|
|
1527
|
+
}),
|
|
1528
|
+
).toEqual([
|
|
1529
|
+
{
|
|
1530
|
+
id: expect.any(String),
|
|
1531
|
+
name: 'John1',
|
|
1532
|
+
age: 20,
|
|
1533
|
+
},
|
|
1534
|
+
{
|
|
1535
|
+
id: expect.any(String),
|
|
1536
|
+
name: 'John2',
|
|
1537
|
+
age: 20,
|
|
1538
|
+
},
|
|
1539
|
+
])
|
|
1540
|
+
})
|
|
1541
|
+
|
|
1542
|
+
it("should throw object not found if the object doesn't exist", () => {
|
|
1543
|
+
expect(
|
|
1544
|
+
postgresAdapter.getObject({
|
|
1545
|
+
className: 'User',
|
|
1546
|
+
id: '4fd2217c-bc08-437f-bf01-d21e8bd7b891',
|
|
1547
|
+
select: { name: true },
|
|
1548
|
+
context,
|
|
1549
|
+
}),
|
|
1550
|
+
).rejects.toThrow('Object not found')
|
|
1551
|
+
})
|
|
1552
|
+
|
|
1553
|
+
it('should create object and return the created object', async () => {
|
|
1554
|
+
const { id } = await postgresAdapter.createObject({
|
|
1555
|
+
className: 'User',
|
|
1556
|
+
data: {
|
|
1557
|
+
name: 'Lucas',
|
|
1558
|
+
age: 23,
|
|
1559
|
+
},
|
|
1560
|
+
select: { age: true, id: true },
|
|
1561
|
+
context,
|
|
1562
|
+
})
|
|
1563
|
+
|
|
1564
|
+
const insertedObject = await postgresAdapter.getObject({
|
|
1565
|
+
className: 'User',
|
|
1566
|
+
id,
|
|
1567
|
+
select: { age: true, id: true },
|
|
1568
|
+
context,
|
|
1569
|
+
})
|
|
1570
|
+
|
|
1571
|
+
expect(insertedObject).toEqual({ age: 23, id: expect.any(String) })
|
|
1572
|
+
|
|
1573
|
+
const { id: id2 } = await postgresAdapter.createObject({
|
|
1574
|
+
className: 'User',
|
|
1575
|
+
data: {
|
|
1576
|
+
name: 'Lucas2',
|
|
1577
|
+
age: 24,
|
|
1578
|
+
},
|
|
1579
|
+
select: { name: true, id: true, age: true },
|
|
1580
|
+
context,
|
|
1581
|
+
})
|
|
1582
|
+
|
|
1583
|
+
const insertedObject2 = await postgresAdapter.getObject({
|
|
1584
|
+
className: 'User',
|
|
1585
|
+
id: id2,
|
|
1586
|
+
select: { name: true, id: true, age: true },
|
|
1587
|
+
context,
|
|
1588
|
+
})
|
|
1589
|
+
|
|
1590
|
+
expect(insertedObject2).toEqual({
|
|
1591
|
+
age: 24,
|
|
1592
|
+
id: expect.any(String),
|
|
1593
|
+
name: 'Lucas2',
|
|
1594
|
+
})
|
|
1595
|
+
})
|
|
1596
|
+
|
|
1597
|
+
it('should create multiple objects and return an array of the created object', async () => {
|
|
1598
|
+
await postgresAdapter.createObjects({
|
|
1599
|
+
className: 'User',
|
|
1600
|
+
data: [
|
|
1601
|
+
{
|
|
1602
|
+
name: 'Lucas3',
|
|
1603
|
+
age: 23,
|
|
1604
|
+
},
|
|
1605
|
+
{
|
|
1606
|
+
name: 'Lucas4',
|
|
1607
|
+
age: 24,
|
|
1608
|
+
},
|
|
1609
|
+
],
|
|
1610
|
+
select: { name: true, id: true },
|
|
1611
|
+
context,
|
|
1612
|
+
})
|
|
1613
|
+
|
|
1614
|
+
const insertedObjects = await postgresAdapter.getObjects({
|
|
1615
|
+
className: 'User',
|
|
1616
|
+
where: {},
|
|
1617
|
+
select: { name: true, id: true },
|
|
1618
|
+
context,
|
|
1619
|
+
})
|
|
1620
|
+
|
|
1621
|
+
expect(insertedObjects).toEqual([
|
|
1622
|
+
{
|
|
1623
|
+
id: expect.any(String),
|
|
1624
|
+
name: 'Lucas3',
|
|
1625
|
+
},
|
|
1626
|
+
{
|
|
1627
|
+
id: expect.any(String),
|
|
1628
|
+
name: 'Lucas4',
|
|
1629
|
+
},
|
|
1630
|
+
])
|
|
1631
|
+
})
|
|
1632
|
+
|
|
1633
|
+
it('should create multiple objects and return an array of the created object with all fields', async () => {
|
|
1634
|
+
await postgresAdapter.createObjects({
|
|
1635
|
+
className: 'User',
|
|
1636
|
+
data: [
|
|
1637
|
+
{
|
|
1638
|
+
name: 'Lucas3',
|
|
1639
|
+
age: 23,
|
|
1640
|
+
},
|
|
1641
|
+
{
|
|
1642
|
+
name: 'Lucas4',
|
|
1643
|
+
age: 24,
|
|
1644
|
+
},
|
|
1645
|
+
],
|
|
1646
|
+
select: { name: true, id: true, age: true },
|
|
1647
|
+
context,
|
|
1648
|
+
})
|
|
1649
|
+
|
|
1650
|
+
const insertedObjects = await postgresAdapter.getObjects({
|
|
1651
|
+
className: 'User',
|
|
1652
|
+
where: {},
|
|
1653
|
+
select: { name: true, id: true, age: true },
|
|
1654
|
+
context,
|
|
1655
|
+
})
|
|
1656
|
+
|
|
1657
|
+
expect(insertedObjects).toEqual([
|
|
1658
|
+
{
|
|
1659
|
+
id: expect.any(String),
|
|
1660
|
+
name: 'Lucas3',
|
|
1661
|
+
age: 23,
|
|
1662
|
+
},
|
|
1663
|
+
{
|
|
1664
|
+
id: expect.any(String),
|
|
1665
|
+
name: 'Lucas4',
|
|
1666
|
+
age: 24,
|
|
1667
|
+
},
|
|
1668
|
+
])
|
|
1669
|
+
})
|
|
1670
|
+
|
|
1671
|
+
it('should update object', async () => {
|
|
1672
|
+
const insertedObject = await postgresAdapter.createObject({
|
|
1673
|
+
className: 'User',
|
|
1674
|
+
data: {
|
|
1675
|
+
name: 'John',
|
|
1676
|
+
age: 20,
|
|
1677
|
+
},
|
|
1678
|
+
context,
|
|
1679
|
+
})
|
|
1680
|
+
|
|
1681
|
+
if (!insertedObject) fail()
|
|
1682
|
+
|
|
1683
|
+
const id = insertedObject.id
|
|
1684
|
+
|
|
1685
|
+
await postgresAdapter.updateObject({
|
|
1686
|
+
className: 'User',
|
|
1687
|
+
id: id.toString(),
|
|
1688
|
+
data: { name: 'Doe' },
|
|
1689
|
+
select: { name: true, id: true },
|
|
1690
|
+
context,
|
|
1691
|
+
})
|
|
1692
|
+
|
|
1693
|
+
const updatedObject = await postgresAdapter.getObject({
|
|
1694
|
+
className: 'User',
|
|
1695
|
+
id: id.toString(),
|
|
1696
|
+
select: { name: true, id: true },
|
|
1697
|
+
context,
|
|
1698
|
+
})
|
|
1699
|
+
|
|
1700
|
+
expect(updatedObject).toEqual({
|
|
1701
|
+
name: 'Doe',
|
|
1702
|
+
id: expect.any(String),
|
|
1703
|
+
})
|
|
1704
|
+
|
|
1705
|
+
await postgresAdapter.updateObject({
|
|
1706
|
+
className: 'User',
|
|
1707
|
+
id: id.toString(),
|
|
1708
|
+
data: { name: 'Doe' },
|
|
1709
|
+
select: { name: true, id: true, age: true },
|
|
1710
|
+
context,
|
|
1711
|
+
})
|
|
1712
|
+
|
|
1713
|
+
const updatedObject2 = await postgresAdapter.getObject({
|
|
1714
|
+
className: 'User',
|
|
1715
|
+
id: id.toString(),
|
|
1716
|
+
select: { name: true, age: true, id: true },
|
|
1717
|
+
context,
|
|
1718
|
+
})
|
|
1719
|
+
|
|
1720
|
+
expect(updatedObject2).toEqual({
|
|
1721
|
+
id: expect.any(String),
|
|
1722
|
+
name: 'Doe',
|
|
1723
|
+
age: 20,
|
|
1724
|
+
})
|
|
1725
|
+
})
|
|
1726
|
+
|
|
1727
|
+
it('should update multiple objects', async () => {
|
|
1728
|
+
const insertedObjects = await postgresAdapter.createObjects({
|
|
1729
|
+
className: 'User',
|
|
1730
|
+
data: [
|
|
1731
|
+
{
|
|
1732
|
+
name: 'Lucas',
|
|
1733
|
+
age: 20,
|
|
1734
|
+
},
|
|
1735
|
+
{
|
|
1736
|
+
name: 'Lucas1',
|
|
1737
|
+
age: 20,
|
|
1738
|
+
},
|
|
1739
|
+
],
|
|
1740
|
+
context,
|
|
1741
|
+
})
|
|
1742
|
+
|
|
1743
|
+
if (!insertedObjects) fail()
|
|
1744
|
+
|
|
1745
|
+
await postgresAdapter.updateObjects({
|
|
1746
|
+
className: 'User',
|
|
1747
|
+
where: {
|
|
1748
|
+
name: { equalTo: 'Lucas' },
|
|
1749
|
+
},
|
|
1750
|
+
data: { name: 'Doe' },
|
|
1751
|
+
select: { name: true, id: true },
|
|
1752
|
+
context,
|
|
1753
|
+
})
|
|
1754
|
+
|
|
1755
|
+
const updatedObjects = await postgresAdapter.getObjects({
|
|
1756
|
+
className: 'User',
|
|
1757
|
+
where: {
|
|
1758
|
+
name: {
|
|
1759
|
+
equalTo: 'Doe',
|
|
1760
|
+
},
|
|
1761
|
+
},
|
|
1762
|
+
select: { name: true, id: true },
|
|
1763
|
+
context,
|
|
1764
|
+
})
|
|
1765
|
+
|
|
1766
|
+
expect(updatedObjects).toEqual([
|
|
1767
|
+
{
|
|
1768
|
+
id: expect.any(String),
|
|
1769
|
+
name: 'Doe',
|
|
1770
|
+
},
|
|
1771
|
+
])
|
|
1772
|
+
|
|
1773
|
+
await postgresAdapter.updateObjects({
|
|
1774
|
+
className: 'User',
|
|
1775
|
+
where: {
|
|
1776
|
+
age: { greaterThanOrEqualTo: 20 },
|
|
1777
|
+
},
|
|
1778
|
+
data: { age: 23 },
|
|
1779
|
+
select: { name: true, id: true, age: true },
|
|
1780
|
+
context,
|
|
1781
|
+
})
|
|
1782
|
+
|
|
1783
|
+
const updatedObjects2 = await postgresAdapter.getObjects({
|
|
1784
|
+
className: 'User',
|
|
1785
|
+
where: {
|
|
1786
|
+
age: {
|
|
1787
|
+
greaterThanOrEqualTo: 23,
|
|
1788
|
+
},
|
|
1789
|
+
},
|
|
1790
|
+
select: { age: true, name: true, id: true },
|
|
1791
|
+
context,
|
|
1792
|
+
})
|
|
1793
|
+
|
|
1794
|
+
expect(updatedObjects2).toEqual(
|
|
1795
|
+
expect.arrayContaining([
|
|
1796
|
+
{
|
|
1797
|
+
id: expect.any(String),
|
|
1798
|
+
name: 'Doe',
|
|
1799
|
+
age: 23,
|
|
1800
|
+
},
|
|
1801
|
+
{
|
|
1802
|
+
id: expect.any(String),
|
|
1803
|
+
name: 'Lucas1',
|
|
1804
|
+
age: 23,
|
|
1805
|
+
},
|
|
1806
|
+
]),
|
|
1807
|
+
)
|
|
1808
|
+
})
|
|
1809
|
+
|
|
1810
|
+
it('should update the same field of an object that which we use in the where field', async () => {
|
|
1811
|
+
const insertedObject = await postgresAdapter.createObject({
|
|
1812
|
+
className: 'User',
|
|
1813
|
+
data: {
|
|
1814
|
+
name: 'John',
|
|
1815
|
+
age: 20,
|
|
1816
|
+
},
|
|
1817
|
+
context,
|
|
1818
|
+
})
|
|
1819
|
+
|
|
1820
|
+
if (!insertedObject) fail()
|
|
1821
|
+
|
|
1822
|
+
await postgresAdapter.updateObjects({
|
|
1823
|
+
className: 'User',
|
|
1824
|
+
data: { name: 'Doe' },
|
|
1825
|
+
where: {
|
|
1826
|
+
name: {
|
|
1827
|
+
equalTo: 'John',
|
|
1828
|
+
},
|
|
1829
|
+
},
|
|
1830
|
+
select: {},
|
|
1831
|
+
context,
|
|
1832
|
+
})
|
|
1833
|
+
|
|
1834
|
+
const updatedObjects = await postgresAdapter.getObjects({
|
|
1835
|
+
className: 'User',
|
|
1836
|
+
where: {
|
|
1837
|
+
name: {
|
|
1838
|
+
equalTo: 'Doe',
|
|
1839
|
+
},
|
|
1840
|
+
},
|
|
1841
|
+
select: { age: true, id: true },
|
|
1842
|
+
context,
|
|
1843
|
+
})
|
|
1844
|
+
|
|
1845
|
+
expect(updatedObjects).toEqual([
|
|
1846
|
+
{
|
|
1847
|
+
id: expect.any(String),
|
|
1848
|
+
age: 20,
|
|
1849
|
+
},
|
|
1850
|
+
])
|
|
1851
|
+
})
|
|
1852
|
+
|
|
1853
|
+
it('should delete one object', async () => {
|
|
1854
|
+
const insertedObject = await postgresAdapter.createObject({
|
|
1855
|
+
className: 'User',
|
|
1856
|
+
data: {
|
|
1857
|
+
name: 'John',
|
|
1858
|
+
age: 20,
|
|
1859
|
+
},
|
|
1860
|
+
context,
|
|
1861
|
+
})
|
|
1862
|
+
|
|
1863
|
+
if (!insertedObject) fail()
|
|
1864
|
+
|
|
1865
|
+
const id = insertedObject.id
|
|
1866
|
+
|
|
1867
|
+
await postgresAdapter.deleteObject({
|
|
1868
|
+
className: 'User',
|
|
1869
|
+
id: id.toString(),
|
|
1870
|
+
select: { name: true, id: true, age: true },
|
|
1871
|
+
context,
|
|
1872
|
+
})
|
|
1873
|
+
|
|
1874
|
+
expect(
|
|
1875
|
+
postgresAdapter.getObject({
|
|
1876
|
+
className: 'User',
|
|
1877
|
+
id: id.toString(),
|
|
1878
|
+
context,
|
|
1879
|
+
}),
|
|
1880
|
+
).rejects.toThrow('Object not found')
|
|
1881
|
+
})
|
|
1882
|
+
|
|
1883
|
+
it('should delete multiple object', async () => {
|
|
1884
|
+
await postgresAdapter.createObject({
|
|
1885
|
+
className: 'User',
|
|
1886
|
+
data: {
|
|
1887
|
+
name: 'John',
|
|
1888
|
+
age: 18,
|
|
1889
|
+
},
|
|
1890
|
+
context,
|
|
1891
|
+
})
|
|
1892
|
+
|
|
1893
|
+
await postgresAdapter.createObject({
|
|
1894
|
+
className: 'User',
|
|
1895
|
+
data: {
|
|
1896
|
+
name: 'Lucas',
|
|
1897
|
+
age: 18,
|
|
1898
|
+
},
|
|
1899
|
+
context,
|
|
1900
|
+
})
|
|
1901
|
+
|
|
1902
|
+
await postgresAdapter.deleteObjects({
|
|
1903
|
+
className: 'User',
|
|
1904
|
+
where: { age: { equalTo: 18 } },
|
|
1905
|
+
select: { name: true, id: true, age: true },
|
|
1906
|
+
context,
|
|
1907
|
+
})
|
|
1908
|
+
|
|
1909
|
+
const resAfterDelete = await postgresAdapter.getObjects({
|
|
1910
|
+
className: 'User',
|
|
1911
|
+
where: { age: { equalTo: 18 } },
|
|
1912
|
+
context,
|
|
1913
|
+
})
|
|
1914
|
+
|
|
1915
|
+
expect(resAfterDelete.length).toEqual(0)
|
|
1916
|
+
})
|
|
1917
|
+
|
|
1918
|
+
it('should build empty where query for postgres adapter if where is empty', () => {
|
|
1919
|
+
const { query, values } = buildPostgresWhereQueryAndValues({})
|
|
1920
|
+
|
|
1921
|
+
expect(query).toEqual('')
|
|
1922
|
+
expect(values).toEqual([])
|
|
1923
|
+
})
|
|
1924
|
+
|
|
1925
|
+
it('should request nested objects in object', async () => {
|
|
1926
|
+
await postgresAdapter.createObject({
|
|
1927
|
+
className: 'User',
|
|
1928
|
+
context,
|
|
1929
|
+
data: {
|
|
1930
|
+
authentication: {
|
|
1931
|
+
emailPassword: {
|
|
1932
|
+
email: 'email@test.fr',
|
|
1933
|
+
password: 'password',
|
|
1934
|
+
},
|
|
1935
|
+
},
|
|
1936
|
+
},
|
|
1937
|
+
})
|
|
1938
|
+
|
|
1939
|
+
const res = await postgresAdapter.getObjects({
|
|
1940
|
+
className: 'User',
|
|
1941
|
+
where: {
|
|
1942
|
+
authentication: {
|
|
1943
|
+
// @ts-expect-error
|
|
1944
|
+
emailPassword: {
|
|
1945
|
+
email: { equalTo: 'email@test.fr' },
|
|
1946
|
+
},
|
|
1947
|
+
},
|
|
1948
|
+
},
|
|
1949
|
+
context,
|
|
1950
|
+
select: { authentication: true },
|
|
1951
|
+
})
|
|
1952
|
+
|
|
1953
|
+
expect(res.length).toEqual(1)
|
|
1954
|
+
expect(res[0]?.authentication?.emailPassword?.email).toEqual('email@test.fr')
|
|
1955
|
+
expect(res[0]?.authentication?.emailPassword?.password).toEqual('password')
|
|
1956
|
+
})
|
|
1957
|
+
|
|
1958
|
+
it('should filter documents where field exists (exists: true)', async () => {
|
|
1959
|
+
// Create test documents using the adapter
|
|
1960
|
+
await postgresAdapter.createObjects({
|
|
1961
|
+
className: 'Test',
|
|
1962
|
+
data: [
|
|
1963
|
+
{ field1: 'Document with field1', int: 25 },
|
|
1964
|
+
{ field1: 'Another document with field1', int: 30 },
|
|
1965
|
+
{ field1: null, int: 35 }, // field1 is null
|
|
1966
|
+
],
|
|
1967
|
+
context,
|
|
1968
|
+
})
|
|
1969
|
+
|
|
1970
|
+
// Test exists: true using the adapter
|
|
1971
|
+
const results = await postgresAdapter.getObjects({
|
|
1972
|
+
className: 'Test',
|
|
1973
|
+
where: { field1: { exists: true } },
|
|
1974
|
+
context,
|
|
1975
|
+
})
|
|
1976
|
+
|
|
1977
|
+
expect(results.length).toBe(2)
|
|
1978
|
+
expect(results.every((row) => row?.field1 !== null)).toBe(true)
|
|
1979
|
+
})
|
|
1980
|
+
|
|
1981
|
+
it('should filter documents where field does not exist (exists: false)', async () => {
|
|
1982
|
+
// Create test documents using the adapter
|
|
1983
|
+
await postgresAdapter.createObjects({
|
|
1984
|
+
className: 'Test',
|
|
1985
|
+
data: [
|
|
1986
|
+
{ field1: 'Document with field1', int: 25 },
|
|
1987
|
+
{ field1: null, int: 30 },
|
|
1988
|
+
{ field1: null, int: 35 },
|
|
1989
|
+
],
|
|
1990
|
+
context,
|
|
1991
|
+
})
|
|
1992
|
+
|
|
1993
|
+
// Test exists: false using the adapter
|
|
1994
|
+
const results = await postgresAdapter.getObjects({
|
|
1995
|
+
className: 'Test',
|
|
1996
|
+
where: { field1: { exists: false } },
|
|
1997
|
+
context,
|
|
1998
|
+
})
|
|
1999
|
+
|
|
2000
|
+
expect(results.length).toBe(2)
|
|
2001
|
+
expect(results.every((row) => row?.field1 === null)).toBe(true)
|
|
2002
|
+
})
|
|
2003
|
+
|
|
2004
|
+
it('should work with complex queries combining exists and other operators', async () => {
|
|
2005
|
+
// Create test documents using the adapter
|
|
2006
|
+
await postgresAdapter.createObjects({
|
|
2007
|
+
className: 'Test',
|
|
2008
|
+
data: [
|
|
2009
|
+
{ field1: 'John', int: 25 },
|
|
2010
|
+
{ field1: 'Jane', int: 30 },
|
|
2011
|
+
{ field1: null, int: 35 },
|
|
2012
|
+
{ field1: 'Bob', int: null },
|
|
2013
|
+
],
|
|
2014
|
+
context,
|
|
2015
|
+
})
|
|
2016
|
+
|
|
2017
|
+
// Test complex query: field1 exists AND int > 25
|
|
2018
|
+
const results = await postgresAdapter.getObjects({
|
|
2019
|
+
className: 'Test',
|
|
2020
|
+
// @ts-expect-error
|
|
2021
|
+
where: {
|
|
2022
|
+
AND: [{ field1: { exists: true } }, { int: { greaterThan: 25 } }],
|
|
2023
|
+
},
|
|
2024
|
+
context,
|
|
2025
|
+
})
|
|
2026
|
+
|
|
2027
|
+
expect(results.length).toBe(1)
|
|
2028
|
+
expect(results[0]?.field1).toBe('Jane')
|
|
2029
|
+
expect(results[0]?.int).toBe(30)
|
|
2030
|
+
})
|
|
2031
|
+
|
|
2032
|
+
it('should handle exists with JSON fields', async () => {
|
|
2033
|
+
// Create test documents with JSON data using the adapter
|
|
2034
|
+
await postgresAdapter.createObjects({
|
|
2035
|
+
className: 'Test',
|
|
2036
|
+
data: [
|
|
2037
|
+
{ object: { array: [{ string: 'John' }] }, int: 25 },
|
|
2038
|
+
{ object: { array: [{ string: 'Jane' }] }, int: 30 },
|
|
2039
|
+
{ object: null, int: 35 },
|
|
2040
|
+
],
|
|
2041
|
+
context,
|
|
2042
|
+
})
|
|
2043
|
+
|
|
2044
|
+
const results = await postgresAdapter.getObjects({
|
|
2045
|
+
className: 'Test',
|
|
2046
|
+
// @ts-expect-error
|
|
2047
|
+
where: { object: { array: { exists: true } } },
|
|
2048
|
+
context,
|
|
2049
|
+
})
|
|
2050
|
+
|
|
2051
|
+
expect(results.length).toBe(2)
|
|
2052
|
+
expect(results.some((row) => row?.int === 25)).toBe(true)
|
|
2053
|
+
expect(results.some((row) => row?.int === 30)).toBe(true)
|
|
2054
|
+
})
|
|
2055
|
+
|
|
2056
|
+
it('should handle exists with JSON fields and null array in object', async () => {
|
|
2057
|
+
// Create test documents with JSON data using the adapter
|
|
2058
|
+
await postgresAdapter.createObjects({
|
|
2059
|
+
className: 'Test',
|
|
2060
|
+
data: [
|
|
2061
|
+
{ object: { array: [{ string: 'John' }] }, int: 25 },
|
|
2062
|
+
{ object: { array: null }, int: 30 },
|
|
2063
|
+
{ object: null, int: 35 },
|
|
2064
|
+
],
|
|
2065
|
+
context,
|
|
2066
|
+
})
|
|
2067
|
+
|
|
2068
|
+
const results = await postgresAdapter.getObjects({
|
|
2069
|
+
className: 'Test',
|
|
2070
|
+
// @ts-expect-error
|
|
2071
|
+
where: { object: { array: { exists: true } } },
|
|
2072
|
+
context,
|
|
2073
|
+
})
|
|
2074
|
+
|
|
2075
|
+
expect(results.length).toBe(1)
|
|
2076
|
+
expect(results.some((row) => row?.int === 25)).toBe(true)
|
|
2077
|
+
})
|
|
2078
|
+
|
|
2079
|
+
it('should handle correctly equalTo and notEqualTo undefined', async () => {
|
|
2080
|
+
// Create test documents with JSON data using the adapter
|
|
2081
|
+
await postgresAdapter.createObjects({
|
|
2082
|
+
className: 'Test',
|
|
2083
|
+
// @ts-expect-error
|
|
2084
|
+
data: [{ object: null, field1: 'John', float: 2.5 }, { int: 35 }],
|
|
2085
|
+
context,
|
|
2086
|
+
})
|
|
2087
|
+
|
|
2088
|
+
const results = await postgresAdapter.getObjects({
|
|
2089
|
+
className: 'Test',
|
|
2090
|
+
where: { int: { equalTo: undefined } },
|
|
2091
|
+
context,
|
|
2092
|
+
})
|
|
2093
|
+
|
|
2094
|
+
expect(results.length).toBe(1)
|
|
2095
|
+
expect(results.some((row) => row?.field1 === 'John')).toBe(true)
|
|
2096
|
+
|
|
2097
|
+
const results2 = await postgresAdapter.getObjects({
|
|
2098
|
+
className: 'Test',
|
|
2099
|
+
where: { int: { notEqualTo: undefined } },
|
|
2100
|
+
context,
|
|
2101
|
+
})
|
|
2102
|
+
|
|
2103
|
+
expect(results2.length).toBe(1)
|
|
2104
|
+
expect(results2.some((row) => row?.int === 35)).toBe(true)
|
|
2105
|
+
|
|
2106
|
+
const results3 = await postgresAdapter.getObjects({
|
|
2107
|
+
className: 'Test',
|
|
2108
|
+
where: { int: { equalTo: undefined }, float: { greaterThan: 2 } },
|
|
2109
|
+
context,
|
|
2110
|
+
})
|
|
2111
|
+
|
|
2112
|
+
expect(results3.length).toBe(1)
|
|
2113
|
+
expect(results3.some((row) => row?.field1 === 'John')).toBe(true)
|
|
2114
|
+
})
|
|
2115
|
+
})
|