wabe-postgres 0.5.2 → 0.5.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wabe-postgres",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
4
4
  "description": "PostgreSQL adapter for Wabe (official)",
5
5
  "keywords": [
6
6
  "adapter",
@@ -28,7 +28,7 @@
28
28
  "@types/pg": "8.11.11",
29
29
  "get-port": "7.1.0",
30
30
  "uuid": "13.0.0",
31
- "wabe": "0.6.11",
31
+ "wabe": "0.6.12",
32
32
  "wabe-postgres-launcher": "0.5.0"
33
33
  }
34
34
  }
package/src/index.test.ts CHANGED
@@ -393,6 +393,74 @@ describe('Postgres adapter', () => {
393
393
  expect(res2.length).toBe(1)
394
394
  })
395
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
+
396
464
  it('should create class', async () => {
397
465
  const client = await postgresAdapter.pool.connect()
398
466
 
package/src/index.ts CHANGED
@@ -52,6 +52,19 @@ const getSQLColumnCreateTableFromType = <T extends WabeTypes>(type: TypeField<T>
52
52
  }
53
53
  }
54
54
 
55
+ /** Resolve where operators (e.g. { string: { equalTo: 'user1' } }) to plain values for JSON. */
56
+ const resolveWhereToPlain = (obj: unknown): unknown => {
57
+ if (!obj || typeof obj !== 'object' || Array.isArray(obj)) return obj
58
+ return Object.fromEntries(
59
+ Object.entries(obj).map(([key, value]) => [
60
+ key,
61
+ value && typeof value === 'object' && !Array.isArray(value) && 'equalTo' in value
62
+ ? (value as { equalTo: unknown }).equalTo
63
+ : resolveWhereToPlain(value),
64
+ ]),
65
+ )
66
+ }
67
+
55
68
  export const buildPostgresOrderQuery = <
56
69
  T extends WabeTypes,
57
70
  K extends keyof T['types'],
@@ -207,10 +220,11 @@ export const buildPostgresWhereQueryAndValues = <T extends WabeTypes, K extends
207
220
  if (value?.notContains) {
208
221
  // Simple access on json field because contains is use for array or object column
209
222
  acc.conditions.push(`NOT (${simpleFullKey} @> $${acc.paramIndex})`)
223
+ const toJson = (v: unknown) => (Array.isArray(v) ? JSON.stringify(v) : JSON.stringify([v]))
210
224
  acc.values.push(
211
- Array.isArray(value.notContains)
212
- ? JSON.stringify(value.notContains)
213
- : JSON.stringify([value.notContains]),
225
+ typeof value.notContains === 'object' && !Array.isArray(value.notContains)
226
+ ? toJson(resolveWhereToPlain(value.notContains))
227
+ : toJson(value.notContains),
214
228
  )
215
229
  acc.paramIndex++
216
230
  return acc