wabe-mongodb 0.5.3 → 0.5.4

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-mongodb",
3
- "version": "0.5.3",
3
+ "version": "0.5.4",
4
4
  "description": "MongoDB adapter for Wabe (official)",
5
5
  "keywords": [
6
6
  "baas",
@@ -34,7 +34,7 @@
34
34
  "devDependencies": {
35
35
  "get-port": "7.1.0",
36
36
  "uuid": "13.0.0",
37
- "wabe": "0.6.11",
37
+ "wabe": "0.6.12",
38
38
  "wabe-build": "0.5.0",
39
39
  "wabe-mongodb-launcher": "0.5.2"
40
40
  }
package/src/index.test.ts CHANGED
@@ -384,6 +384,74 @@ describe('Mongo adapter', () => {
384
384
  expect(res2.length).toBe(1)
385
385
  })
386
386
 
387
+ it('should correctly exclude documents with notContains on array of objects (ACL notContains fix)', async () => {
388
+ await mongoAdapter.createObject({
389
+ className: 'Test',
390
+ data: {
391
+ object: { array: [{ string: 'user1' }] },
392
+ },
393
+ context,
394
+ })
395
+
396
+ await mongoAdapter.createObject({
397
+ className: 'Test',
398
+ data: {
399
+ object: { array: [{ string: 'user2' }] },
400
+ },
401
+ context,
402
+ })
403
+
404
+ const res = await mongoAdapter.getObjects({
405
+ className: 'Test',
406
+ context,
407
+ where: {
408
+ object: {
409
+ // @ts-expect-error
410
+ array: {
411
+ notContains: { string: 'user1' },
412
+ },
413
+ },
414
+ },
415
+ })
416
+
417
+ expect(res.length).toBe(1)
418
+ expect(res[0]?.object?.array).toEqual([{ string: 'user2' }])
419
+ })
420
+
421
+ it('should correctly filter with nested notContains and equalTo without recursion overwrite', async () => {
422
+ await mongoAdapter.createObject({
423
+ className: 'Test',
424
+ data: {
425
+ object: { array: [{ string: 'user1' }] },
426
+ },
427
+ context,
428
+ })
429
+
430
+ await mongoAdapter.createObject({
431
+ className: 'Test',
432
+ data: {
433
+ object: { array: [{ string: 'user2' }] },
434
+ },
435
+ context,
436
+ })
437
+
438
+ const res = await mongoAdapter.getObjects({
439
+ className: 'Test',
440
+ context,
441
+ where: {
442
+ object: {
443
+ // @ts-expect-error
444
+ array: {
445
+ notContains: { string: { equalTo: 'user1' } },
446
+ },
447
+ },
448
+ },
449
+ })
450
+
451
+ expect(res.length).toBe(1)
452
+ expect(res[0]?.object?.array).toEqual([{ string: 'user2' }])
453
+ })
454
+
387
455
  it('should retry on connection error', async () => {
388
456
  const spyMongoClientConnect = spyOn(mongoAdapter.client, 'connect').mockImplementationOnce(
389
457
  () => {
@@ -1904,6 +1972,23 @@ describe('Mongo adapter', () => {
1904
1972
  expect(where).toEqual({})
1905
1973
  })
1906
1974
 
1975
+ it('should build nested notContains with equalTo for MongoDB $elemMatch', () => {
1976
+ const where = buildMongoWhereQuery({
1977
+ data: {
1978
+ // @ts-expect-error
1979
+ array: {
1980
+ notContains: { string: { equalTo: 'user1' } },
1981
+ },
1982
+ },
1983
+ })
1984
+
1985
+ expect(where).toEqual({
1986
+ 'data.array': {
1987
+ $not: { $elemMatch: { string: 'user1' } },
1988
+ },
1989
+ })
1990
+ })
1991
+
1907
1992
  it('should request sub object in object', async () => {
1908
1993
  await mongoAdapter.createObject({
1909
1994
  className: 'User',
package/src/index.ts CHANGED
@@ -19,6 +19,10 @@ import type {
19
19
  SchemaInterface,
20
20
  } from 'wabe'
21
21
 
22
+ /** Use built MongoDB query if non-empty, otherwise fallback to original (e.g. direct value { string: 'user1' }). */
23
+ const builtOrOriginal = (built: Record<string, unknown>, original: unknown) =>
24
+ Object.keys(built).length ? built : original
25
+
22
26
  export const buildMongoOrderQuery = <
23
27
  T extends WabeTypes,
24
28
  K extends keyof T['types'],
@@ -63,8 +67,20 @@ export const buildMongoWhereQuery = <T extends WabeTypes, K extends keyof T['typ
63
67
  : {
64
68
  $all: Array.isArray(value.contains) ? value.contains : [value.contains],
65
69
  }
66
- if (value?.notContains || value?.notContains === null)
67
- acc[keyToWrite] = { $ne: value.notContains }
70
+ if (value?.notContains || value?.notContains === null) {
71
+ acc[keyToWrite] =
72
+ typeof value.notContains === 'object' && !Array.isArray(value.notContains)
73
+ ? {
74
+ $not: {
75
+ $elemMatch: builtOrOriginal(
76
+ buildMongoWhereQuery(value.notContains as WhereType<T, K>),
77
+ value.notContains,
78
+ ),
79
+ },
80
+ }
81
+ : { $nin: Array.isArray(value.notContains) ? value.notContains : [value.notContains] }
82
+ return acc
83
+ }
68
84
  if (value?.exists === true) acc[keyToWrite] = { $exists: true, $ne: null }
69
85
  if (value?.exists === false) acc[keyToWrite] = { $eq: null }
70
86
 
@@ -151,6 +167,8 @@ export const buildMongoWhereQuery = <T extends WabeTypes, K extends keyof T['typ
151
167
  return acc
152
168
  }
153
169
 
170
+ if (acc[keyToWrite] !== undefined) return acc
171
+
154
172
  if (typeof value === 'object') {
155
173
  const where = buildMongoWhereQuery(value as WhereType<T, K>)
156
174
  const entries = Object.entries(where)