core-services-sdk 1.3.24 → 1.3.25
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 +1 -1
- package/src/core/index.js +1 -0
- package/src/core/normalize-array-operators.js +24 -0
- package/src/mongodb/dsl-to-mongo.js +99 -0
- package/src/mongodb/index.js +1 -0
- package/tests/core/normalize-array-operators.integration.test.js +64 -0
- package/tests/core/normalize-array-operators.unit.test.js +98 -0
- package/tests/mongodb/dsl-to-mongo.integration.test.js +88 -0
- package/tests/mongodb/dsl-to-mongo.unit.test.js +106 -0
- package/tests/mongodb/dsl-to-mongo2.unit.test.js +115 -0
- package/tests/resources/server-to-mongo.js +27 -0
- package/tests/resources/server.js +23 -0
- package/types/core/index.d.ts +1 -0
- package/types/core/normalize-array-operators.d.ts +1 -0
- package/types/mongodb/dsl-to-mongo.d.ts +7 -0
- package/types/mongodb/index.d.ts +1 -0
package/package.json
CHANGED
package/src/core/index.js
CHANGED
|
@@ -5,4 +5,5 @@ export * from './normalize-min-max.js'
|
|
|
5
5
|
export * from './normalize-to-array.js'
|
|
6
6
|
export * from './combine-unique-arrays.js'
|
|
7
7
|
export * from './normalize-phone-number.js'
|
|
8
|
+
export * from './normalize-array-operators.js'
|
|
8
9
|
export * from './normalize-premitives-types-or-default.js'
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export const normalizeOperators = (obj) => {
|
|
2
|
+
const arrayOps = ['in', 'nin', 'or', 'and']
|
|
3
|
+
|
|
4
|
+
function normalize(value) {
|
|
5
|
+
if (Array.isArray(value)) {
|
|
6
|
+
return value.map(normalize)
|
|
7
|
+
} else if (typeof value === 'object' && value !== null) {
|
|
8
|
+
// Handle objects with numeric keys (e.g. {0: {...}, 1: {...}})
|
|
9
|
+
const keys = Object.keys(value)
|
|
10
|
+
if (keys.every((k) => /^\d+$/.test(k))) {
|
|
11
|
+
return keys.sort().map((k) => normalize(value[k]))
|
|
12
|
+
}
|
|
13
|
+
for (const [k, v] of Object.entries(value)) {
|
|
14
|
+
value[k] = normalize(v)
|
|
15
|
+
if (arrayOps.includes(k)) {
|
|
16
|
+
value[k] = Array.isArray(value[k]) ? value[k] : [value[k]]
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return value
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return normalize(obj)
|
|
24
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a normalized query object into a MongoDB query object.
|
|
3
|
+
*
|
|
4
|
+
* Supported operators: eq, in, nin, gt, gte, lt, lte, and, or.
|
|
5
|
+
*
|
|
6
|
+
* Example input:
|
|
7
|
+
* {
|
|
8
|
+
* userId: { in: ['123','456'] },
|
|
9
|
+
* status: { eq: 'active' },
|
|
10
|
+
* age: { gte: 18 },
|
|
11
|
+
* or: [
|
|
12
|
+
* { role: { eq: 'admin' } },
|
|
13
|
+
* { status: { eq: 'active' } }
|
|
14
|
+
* ]
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* Example output:
|
|
18
|
+
* {
|
|
19
|
+
* userId: { $in: ['123','456'] },
|
|
20
|
+
* status: { $eq: 'active' },
|
|
21
|
+
* age: { $gte: 18 },
|
|
22
|
+
* $or: [
|
|
23
|
+
* { role: { $eq: 'admin' } },
|
|
24
|
+
* { status: { $eq: 'active' } }
|
|
25
|
+
* ]
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* @param {Record<string, any>} query - The normalized query object
|
|
29
|
+
* @returns {Record<string, any>} - A MongoDB query object
|
|
30
|
+
*/
|
|
31
|
+
/**
|
|
32
|
+
* Map DSL logical keywords to Mongo operators.
|
|
33
|
+
*/
|
|
34
|
+
const LOGICAL_OPS = new Map([
|
|
35
|
+
['or', '$or'],
|
|
36
|
+
['and', '$and'],
|
|
37
|
+
['nor', '$nor'],
|
|
38
|
+
['not', '$not'],
|
|
39
|
+
])
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Convert a condition object (e.g. { gt: 18, lt: 65 }) into Mongo condition.
|
|
43
|
+
* Recursively applies to nested objects.
|
|
44
|
+
*/
|
|
45
|
+
function convertCondition(condition) {
|
|
46
|
+
if (
|
|
47
|
+
condition === null ||
|
|
48
|
+
typeof condition !== 'object' ||
|
|
49
|
+
Array.isArray(condition)
|
|
50
|
+
) {
|
|
51
|
+
return condition // primitive or array stays as is
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return Object.fromEntries(
|
|
55
|
+
Object.entries(condition).map(([op, value]) => {
|
|
56
|
+
if (op.startsWith('$')) {
|
|
57
|
+
return [op, value] // already Mongo operator
|
|
58
|
+
}
|
|
59
|
+
const mongoOp = `$${op}`
|
|
60
|
+
return [mongoOp, convertCondition(value)]
|
|
61
|
+
}),
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Convert a normalized query object into a MongoDB query object.
|
|
67
|
+
*
|
|
68
|
+
* @param {Record<string, any>} query - normalized DSL query
|
|
69
|
+
* @returns {Record<string, any>} - MongoDB query object
|
|
70
|
+
*/
|
|
71
|
+
export function toMongo(query = {}) {
|
|
72
|
+
return Object.fromEntries(
|
|
73
|
+
Object.entries(query).map(([field, condition]) => {
|
|
74
|
+
// logical operator
|
|
75
|
+
if (LOGICAL_OPS.has(field)) {
|
|
76
|
+
const mongoOp = LOGICAL_OPS.get(field)
|
|
77
|
+
if (Array.isArray(condition)) {
|
|
78
|
+
return [mongoOp, condition.map(toMongo)]
|
|
79
|
+
}
|
|
80
|
+
if (condition && typeof condition === 'object') {
|
|
81
|
+
return [mongoOp, toMongo(condition)]
|
|
82
|
+
}
|
|
83
|
+
return [mongoOp, condition]
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// operator object
|
|
87
|
+
if (
|
|
88
|
+
condition !== null &&
|
|
89
|
+
typeof condition === 'object' &&
|
|
90
|
+
!Array.isArray(condition)
|
|
91
|
+
) {
|
|
92
|
+
return [field, convertCondition(condition)]
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// plain value or null → $eq
|
|
96
|
+
return [field, { $eq: condition }]
|
|
97
|
+
}),
|
|
98
|
+
)
|
|
99
|
+
}
|
package/src/mongodb/index.js
CHANGED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { buildServer } from '../resources/server.js'
|
|
4
|
+
|
|
5
|
+
describe('Integration: Fastify + normalizeOperators', () => {
|
|
6
|
+
let app
|
|
7
|
+
|
|
8
|
+
beforeAll(async () => {
|
|
9
|
+
app = buildServer()
|
|
10
|
+
await app.listen({ port: 0 })
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
afterAll(async () => {
|
|
14
|
+
await app.close()
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it('GET with userId[in]=123,456 should normalize to array', async () => {
|
|
18
|
+
const res = await app.inject('/users?userId[in]=123,456&status[eq]=active')
|
|
19
|
+
const body = res.json()
|
|
20
|
+
|
|
21
|
+
expect(body.dsl).toEqual({
|
|
22
|
+
userId: { in: ['123', '456'] },
|
|
23
|
+
status: { eq: 'active' },
|
|
24
|
+
})
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('GET with userId[in]=123 should wrap in array', async () => {
|
|
28
|
+
const res = await app.inject('/users?userId[in]=123')
|
|
29
|
+
const body = res.json()
|
|
30
|
+
|
|
31
|
+
expect(body.dsl).toEqual({ userId: { in: ['123'] } })
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('GET with OR query should normalize to array', async () => {
|
|
35
|
+
const res = await app.inject(
|
|
36
|
+
'/users?or[0][status][eq]=active&or[1][role][eq]=admin',
|
|
37
|
+
)
|
|
38
|
+
const body = res.json()
|
|
39
|
+
|
|
40
|
+
expect(body.dsl).toEqual({
|
|
41
|
+
or: [{ status: { eq: 'active' } }, { role: { eq: 'admin' } }],
|
|
42
|
+
})
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('POST with where body should normalize', async () => {
|
|
46
|
+
const res = await app.inject({
|
|
47
|
+
method: 'POST',
|
|
48
|
+
url: '/users/search',
|
|
49
|
+
payload: {
|
|
50
|
+
where: {
|
|
51
|
+
userId: { in: '123' },
|
|
52
|
+
age: { gte: 18 },
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
const body = res.json()
|
|
58
|
+
|
|
59
|
+
expect(body.dsl).toEqual({
|
|
60
|
+
userId: { in: ['123'] },
|
|
61
|
+
age: { gte: 18 },
|
|
62
|
+
})
|
|
63
|
+
})
|
|
64
|
+
})
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { normalizeOperators } from '../../src/core/normalize-array-operators.js'
|
|
4
|
+
|
|
5
|
+
describe('normalizeOperators', () => {
|
|
6
|
+
it('should wrap single value in `in` into an array', () => {
|
|
7
|
+
const input = { userId: { in: '123' } }
|
|
8
|
+
const output = normalizeOperators(input)
|
|
9
|
+
expect(output).toEqual({ userId: { in: ['123'] } })
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
it('should keep array value in `in` as is', () => {
|
|
13
|
+
const input = { userId: { in: ['123', '456'] } }
|
|
14
|
+
const output = normalizeOperators(input)
|
|
15
|
+
expect(output).toEqual({ userId: { in: ['123', '456'] } })
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('should wrap single value in `nin` into an array', () => {
|
|
19
|
+
const input = { userId: { nin: '789' } }
|
|
20
|
+
const output = normalizeOperators(input)
|
|
21
|
+
expect(output).toEqual({ userId: { nin: ['789'] } })
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('should normalize object with numeric keys into array (or)', () => {
|
|
25
|
+
const input = {
|
|
26
|
+
or: { 0: { status: { eq: 'active' } }, 1: { role: { eq: 'admin' } } },
|
|
27
|
+
}
|
|
28
|
+
const output = normalizeOperators(input)
|
|
29
|
+
expect(output).toEqual({
|
|
30
|
+
or: [{ status: { eq: 'active' } }, { role: { eq: 'admin' } }],
|
|
31
|
+
})
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('should wrap single object in `or` into array', () => {
|
|
35
|
+
const input = { or: { status: { eq: 'active' } } }
|
|
36
|
+
const output = normalizeOperators(input)
|
|
37
|
+
expect(output).toEqual({
|
|
38
|
+
or: [{ status: { eq: 'active' } }],
|
|
39
|
+
})
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it('should handle nested in + or together', () => {
|
|
43
|
+
const input = {
|
|
44
|
+
or: {
|
|
45
|
+
0: { userId: { in: '123' } },
|
|
46
|
+
1: { userId: { in: ['456', '789'] } },
|
|
47
|
+
},
|
|
48
|
+
}
|
|
49
|
+
const output = normalizeOperators(input)
|
|
50
|
+
expect(output).toEqual({
|
|
51
|
+
or: [{ userId: { in: ['123'] } }, { userId: { in: ['456', '789'] } }],
|
|
52
|
+
})
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it('should handle `and` with mixed single and multiple values', () => {
|
|
56
|
+
const input = {
|
|
57
|
+
and: {
|
|
58
|
+
0: { status: { eq: 'active' } },
|
|
59
|
+
1: { userId: { in: '123' } },
|
|
60
|
+
},
|
|
61
|
+
}
|
|
62
|
+
const output = normalizeOperators(input)
|
|
63
|
+
expect(output).toEqual({
|
|
64
|
+
and: [{ status: { eq: 'active' } }, { userId: { in: ['123'] } }],
|
|
65
|
+
})
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('should do nothing for fields without array operators', () => {
|
|
69
|
+
const input = { age: { gte: 18 } }
|
|
70
|
+
const output = normalizeOperators(input)
|
|
71
|
+
expect(output).toEqual({ age: { gte: 18 } })
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it('should work with deeply nested structures', () => {
|
|
75
|
+
const input = {
|
|
76
|
+
or: {
|
|
77
|
+
0: {
|
|
78
|
+
and: {
|
|
79
|
+
0: { userId: { in: '123' } },
|
|
80
|
+
1: { status: { eq: 'active' } },
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
1: { role: { nin: 'guest' } },
|
|
84
|
+
},
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const output = normalizeOperators(input)
|
|
88
|
+
|
|
89
|
+
expect(output).toEqual({
|
|
90
|
+
or: [
|
|
91
|
+
{
|
|
92
|
+
and: [{ userId: { in: ['123'] } }, { status: { eq: 'active' } }],
|
|
93
|
+
},
|
|
94
|
+
{ role: { nin: ['guest'] } },
|
|
95
|
+
],
|
|
96
|
+
})
|
|
97
|
+
})
|
|
98
|
+
})
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { buildServer } from '../resources/server-to-mongo.js'
|
|
4
|
+
|
|
5
|
+
describe('Integration E2E: Fastify + normalizeOperators + toMongo', () => {
|
|
6
|
+
let app
|
|
7
|
+
|
|
8
|
+
beforeAll(async () => {
|
|
9
|
+
app = buildServer()
|
|
10
|
+
await app.listen({ port: 0 })
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
afterAll(async () => {
|
|
14
|
+
await app.close()
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it('GET with simple comparison and AND logic', async () => {
|
|
18
|
+
const res = await app.inject(
|
|
19
|
+
'/search?and[0][age][gte]=18&and[1][age][lt]=65&status[eq]=active',
|
|
20
|
+
)
|
|
21
|
+
const body = res.json()
|
|
22
|
+
|
|
23
|
+
expect(body.dsl).toEqual({
|
|
24
|
+
and: [{ age: { gte: '18' } }, { age: { lt: '65' } }],
|
|
25
|
+
status: { eq: 'active' },
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
expect(body.mongo).toEqual({
|
|
29
|
+
$and: [{ age: { $gte: '18' } }, { age: { $lt: '65' } }],
|
|
30
|
+
status: { $eq: 'active' },
|
|
31
|
+
})
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('GET with OR and IN', async () => {
|
|
35
|
+
const res = await app.inject(
|
|
36
|
+
'/search?or[0][role][eq]=admin&or[1][role][eq]=user&userId[in]=123,456',
|
|
37
|
+
)
|
|
38
|
+
const body = res.json()
|
|
39
|
+
|
|
40
|
+
expect(body.dsl).toEqual({
|
|
41
|
+
or: [{ role: { eq: 'admin' } }, { role: { eq: 'user' } }],
|
|
42
|
+
userId: { in: ['123', '456'] },
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
expect(body.mongo).toEqual({
|
|
46
|
+
$or: [{ role: { $eq: 'admin' } }, { role: { $eq: 'user' } }],
|
|
47
|
+
userId: { $in: ['123', '456'] },
|
|
48
|
+
})
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it('POST with nested AND/OR, gt/lt, exists', async () => {
|
|
52
|
+
const res = await app.inject({
|
|
53
|
+
method: 'POST',
|
|
54
|
+
url: '/search',
|
|
55
|
+
payload: {
|
|
56
|
+
where: {
|
|
57
|
+
and: [
|
|
58
|
+
{ age: { gt: 18, lt: 65 } },
|
|
59
|
+
{
|
|
60
|
+
or: [{ status: { eq: 'active' } }, { status: { eq: 'pending' } }],
|
|
61
|
+
},
|
|
62
|
+
],
|
|
63
|
+
deletedAt: { exists: false },
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
const body = res.json()
|
|
69
|
+
|
|
70
|
+
expect(body.dsl).toEqual({
|
|
71
|
+
and: [
|
|
72
|
+
{ age: { gt: 18, lt: 65 } },
|
|
73
|
+
{ or: [{ status: { eq: 'active' } }, { status: { eq: 'pending' } }] },
|
|
74
|
+
],
|
|
75
|
+
deletedAt: { exists: false },
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
expect(body.mongo).toEqual({
|
|
79
|
+
$and: [
|
|
80
|
+
{ age: { $gt: 18, $lt: 65 } },
|
|
81
|
+
{
|
|
82
|
+
$or: [{ status: { $eq: 'active' } }, { status: { $eq: 'pending' } }],
|
|
83
|
+
},
|
|
84
|
+
],
|
|
85
|
+
deletedAt: { $exists: false },
|
|
86
|
+
})
|
|
87
|
+
})
|
|
88
|
+
})
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { toMongo } from '../../src/mongodb/dsl-to-mongo.js'
|
|
4
|
+
|
|
5
|
+
describe('toMongo', () => {
|
|
6
|
+
it('should convert plain values to $eq', () => {
|
|
7
|
+
const input = { status: 'active', role: 'admin' }
|
|
8
|
+
const output = toMongo(input)
|
|
9
|
+
expect(output).toEqual({
|
|
10
|
+
status: { $eq: 'active' },
|
|
11
|
+
role: { $eq: 'admin' },
|
|
12
|
+
})
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('should convert eq operator to $eq', () => {
|
|
16
|
+
const input = { status: { eq: 'active' } }
|
|
17
|
+
const output = toMongo(input)
|
|
18
|
+
expect(output).toEqual({ status: { $eq: 'active' } })
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it('should convert in operator to $in', () => {
|
|
22
|
+
const input = { userId: { in: ['123', '456'] } }
|
|
23
|
+
const output = toMongo(input)
|
|
24
|
+
expect(output).toEqual({ userId: { $in: ['123', '456'] } })
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('should convert nin operator to $nin', () => {
|
|
28
|
+
const input = { userId: { nin: ['123'] } }
|
|
29
|
+
const output = toMongo(input)
|
|
30
|
+
expect(output).toEqual({ userId: { $nin: ['123'] } })
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('should convert comparison operators correctly', () => {
|
|
34
|
+
const input = {
|
|
35
|
+
age: { gt: 18, lte: 65 },
|
|
36
|
+
}
|
|
37
|
+
const output = toMongo(input)
|
|
38
|
+
expect(output).toEqual({
|
|
39
|
+
age: { $gt: 18, $lte: 65 },
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('should handle $or with multiple conditions', () => {
|
|
44
|
+
const input = {
|
|
45
|
+
or: [{ status: { eq: 'active' } }, { role: { eq: 'admin' } }],
|
|
46
|
+
}
|
|
47
|
+
const output = toMongo(input)
|
|
48
|
+
expect(output).toEqual({
|
|
49
|
+
$or: [{ status: { $eq: 'active' } }, { role: { $eq: 'admin' } }],
|
|
50
|
+
})
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('should handle $and with multiple conditions', () => {
|
|
54
|
+
const input = {
|
|
55
|
+
and: [{ status: { eq: 'active' } }, { age: { gte: 18 } }],
|
|
56
|
+
}
|
|
57
|
+
const output = toMongo(input)
|
|
58
|
+
expect(output).toEqual({
|
|
59
|
+
$and: [{ status: { $eq: 'active' } }, { age: { $gte: 18 } }],
|
|
60
|
+
})
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('should handle nested logical operators', () => {
|
|
64
|
+
const input = {
|
|
65
|
+
or: [
|
|
66
|
+
{ status: { eq: 'active' } },
|
|
67
|
+
{
|
|
68
|
+
and: [{ age: { gte: 18 } }, { age: { lte: 65 } }],
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
}
|
|
72
|
+
const output = toMongo(input)
|
|
73
|
+
expect(output).toEqual({
|
|
74
|
+
$or: [
|
|
75
|
+
{ status: { $eq: 'active' } },
|
|
76
|
+
{
|
|
77
|
+
$and: [{ age: { $gte: 18 } }, { age: { $lte: 65 } }],
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
})
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
it('should handle multiple fields mixed with operators', () => {
|
|
84
|
+
const input = {
|
|
85
|
+
userId: { in: ['123', '456'] },
|
|
86
|
+
status: 'active',
|
|
87
|
+
or: [{ role: { eq: 'admin' } }, { age: { gt: 30 } }],
|
|
88
|
+
}
|
|
89
|
+
const output = toMongo(input)
|
|
90
|
+
expect(output).toEqual({
|
|
91
|
+
userId: { $in: ['123', '456'] },
|
|
92
|
+
status: { $eq: 'active' },
|
|
93
|
+
$or: [{ role: { $eq: 'admin' } }, { age: { $gt: 30 } }],
|
|
94
|
+
})
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it('should return empty object if input is empty', () => {
|
|
98
|
+
expect(toMongo({})).toEqual({})
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('should handle null values as $eq null', () => {
|
|
102
|
+
const input = { deletedAt: null }
|
|
103
|
+
const output = toMongo(input)
|
|
104
|
+
expect(output).toEqual({ deletedAt: { $eq: null } })
|
|
105
|
+
})
|
|
106
|
+
})
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { toMongo } from '../../src/mongodb/dsl-to-mongo.js'
|
|
4
|
+
|
|
5
|
+
describe('toMongo - extended operators', () => {
|
|
6
|
+
it('should support all comparison operators', () => {
|
|
7
|
+
const input = {
|
|
8
|
+
age: { eq: 30, ne: 40, gt: 10, gte: 20, lt: 100, lte: 90 },
|
|
9
|
+
userId: { in: ['u1', 'u2'], nin: ['u3'] },
|
|
10
|
+
}
|
|
11
|
+
const output = toMongo(input)
|
|
12
|
+
|
|
13
|
+
expect(output).toEqual({
|
|
14
|
+
age: { $eq: 30, $ne: 40, $gt: 10, $gte: 20, $lt: 100, $lte: 90 },
|
|
15
|
+
userId: { $in: ['u1', 'u2'], $nin: ['u3'] },
|
|
16
|
+
})
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('should support logical operators', () => {
|
|
20
|
+
const input = {
|
|
21
|
+
and: [{ age: { gt: 18 } }, { status: { eq: 'active' } }],
|
|
22
|
+
or: [{ role: { eq: 'admin' } }, { role: { eq: 'user' } }],
|
|
23
|
+
nor: [{ country: { eq: 'US' } }],
|
|
24
|
+
}
|
|
25
|
+
const output = toMongo(input)
|
|
26
|
+
|
|
27
|
+
expect(output).toEqual({
|
|
28
|
+
$and: [{ age: { $gt: 18 } }, { status: { $eq: 'active' } }],
|
|
29
|
+
$or: [{ role: { $eq: 'admin' } }, { role: { $eq: 'user' } }],
|
|
30
|
+
$nor: [{ country: { $eq: 'US' } }],
|
|
31
|
+
})
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('should support element operators', () => {
|
|
35
|
+
const input = {
|
|
36
|
+
deletedAt: { exists: false },
|
|
37
|
+
status: { type: 'string' },
|
|
38
|
+
}
|
|
39
|
+
const output = toMongo(input)
|
|
40
|
+
|
|
41
|
+
expect(output).toEqual({
|
|
42
|
+
deletedAt: { $exists: false },
|
|
43
|
+
status: { $type: 'string' },
|
|
44
|
+
})
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('should support evaluation operators', () => {
|
|
48
|
+
const input = {
|
|
49
|
+
name: { regex: '^haim', options: 'i' },
|
|
50
|
+
score: { mod: [4, 0] },
|
|
51
|
+
description: { text: 'fast search' },
|
|
52
|
+
}
|
|
53
|
+
const output = toMongo(input)
|
|
54
|
+
|
|
55
|
+
expect(output).toEqual({
|
|
56
|
+
name: { $regex: '^haim', $options: 'i' },
|
|
57
|
+
score: { $mod: [4, 0] },
|
|
58
|
+
description: { $text: 'fast search' },
|
|
59
|
+
})
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it('should support array operators', () => {
|
|
63
|
+
const input = {
|
|
64
|
+
tags: { all: ['tech', 'ai'], size: 3 },
|
|
65
|
+
scores: { elemMatch: { gt: 90 } },
|
|
66
|
+
}
|
|
67
|
+
const output = toMongo(input)
|
|
68
|
+
|
|
69
|
+
expect(output).toEqual({
|
|
70
|
+
tags: { $all: ['tech', 'ai'], $size: 3 },
|
|
71
|
+
scores: { $elemMatch: { $gt: 90 } },
|
|
72
|
+
})
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('should not transform unknown operators (false positive check)', () => {
|
|
76
|
+
const input = {
|
|
77
|
+
age: { weirdOp: 123 },
|
|
78
|
+
}
|
|
79
|
+
const output = toMongo(input)
|
|
80
|
+
|
|
81
|
+
expect(output).toEqual({
|
|
82
|
+
age: { $weirdOp: 123 },
|
|
83
|
+
})
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it('should not wrap already Mongo operators with $$ (false positive)', () => {
|
|
87
|
+
const input = {
|
|
88
|
+
age: { $gte: 18 },
|
|
89
|
+
}
|
|
90
|
+
const output = toMongo(input)
|
|
91
|
+
|
|
92
|
+
expect(output).toEqual({
|
|
93
|
+
age: { $gte: 18 },
|
|
94
|
+
})
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it('should handle nested structures with mix of operators', () => {
|
|
98
|
+
const input = {
|
|
99
|
+
or: [
|
|
100
|
+
{ and: [{ age: { gte: 18 } }, { age: { lte: 65 } }] },
|
|
101
|
+
{ status: { ne: 'banned' } },
|
|
102
|
+
],
|
|
103
|
+
tags: { all: ['mongo', 'node'] },
|
|
104
|
+
}
|
|
105
|
+
const output = toMongo(input)
|
|
106
|
+
|
|
107
|
+
expect(output).toEqual({
|
|
108
|
+
$or: [
|
|
109
|
+
{ $and: [{ age: { $gte: 18 } }, { age: { $lte: 65 } }] },
|
|
110
|
+
{ status: { $ne: 'banned' } },
|
|
111
|
+
],
|
|
112
|
+
tags: { $all: ['mongo', 'node'] },
|
|
113
|
+
})
|
|
114
|
+
})
|
|
115
|
+
})
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import qs from 'qs'
|
|
2
|
+
import Fastify from 'fastify'
|
|
3
|
+
|
|
4
|
+
import { toMongo } from '../../src/mongodb/dsl-to-mongo.js'
|
|
5
|
+
import { normalizeOperators } from '../../src/core/normalize-array-operators.js'
|
|
6
|
+
|
|
7
|
+
export function buildServer() {
|
|
8
|
+
const app = Fastify({
|
|
9
|
+
querystringParser: (str) => qs.parse(str, { comma: true, depth: 10 }),
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
// GET /search?...
|
|
13
|
+
app.get('/search', async (req) => {
|
|
14
|
+
const dsl = normalizeOperators(req.query)
|
|
15
|
+
const mongo = toMongo(dsl)
|
|
16
|
+
return { dsl, mongo }
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
// POST /search with body { where: {...} }
|
|
20
|
+
app.post('/search', async (req) => {
|
|
21
|
+
const dsl = normalizeOperators(req.body?.where || {})
|
|
22
|
+
const mongo = toMongo(dsl)
|
|
23
|
+
return { dsl, mongo }
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
return app
|
|
27
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import Fastify from 'fastify'
|
|
2
|
+
import qs from 'qs'
|
|
3
|
+
import { normalizeOperators } from '../../src/core/normalize-array-operators.js'
|
|
4
|
+
|
|
5
|
+
export function buildServer() {
|
|
6
|
+
const app = Fastify({
|
|
7
|
+
querystringParser: (str) => qs.parse(str, { comma: true, depth: 10 }),
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
// GET /users
|
|
11
|
+
app.get('/users', async (req) => {
|
|
12
|
+
const dsl = normalizeOperators(req.query)
|
|
13
|
+
return { dsl }
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
// POST /users/search
|
|
17
|
+
app.post('/users/search', async (req) => {
|
|
18
|
+
const dsl = normalizeOperators(req.body?.where || {})
|
|
19
|
+
return { dsl }
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
return app
|
|
23
|
+
}
|
package/types/core/index.d.ts
CHANGED
|
@@ -5,4 +5,5 @@ export * from "./normalize-min-max.js";
|
|
|
5
5
|
export * from "./normalize-to-array.js";
|
|
6
6
|
export * from "./combine-unique-arrays.js";
|
|
7
7
|
export * from "./normalize-phone-number.js";
|
|
8
|
+
export * from "./normalize-array-operators.js";
|
|
8
9
|
export * from "./normalize-premitives-types-or-default.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function normalizeOperators(obj: any): any;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a normalized query object into a MongoDB query object.
|
|
3
|
+
*
|
|
4
|
+
* @param {Record<string, any>} query - normalized DSL query
|
|
5
|
+
* @returns {Record<string, any>} - MongoDB query object
|
|
6
|
+
*/
|
|
7
|
+
export function toMongo(query?: Record<string, any>): Record<string, any>;
|
package/types/mongodb/index.d.ts
CHANGED