Skip to content

Commit 00cb0d9

Browse files
authored
feat(database): Add and to the query syntax (#3021)
1 parent dbf514e commit 00cb0d9

6 files changed

Lines changed: 58 additions & 0 deletions

File tree

docs/api/databases/querying.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,23 @@ app.service('messages').find({
121121
GET /messages?$or[0][archived][$ne]=true&$or[1][roomId]=2
122122
```
123123

124+
### $and
125+
126+
Find all records that match all of the given criteria.
127+
128+
```js
129+
// Find all messages that are not marked as archived and in room 2
130+
app.service('messages').find({
131+
query: {
132+
$all: [{ archived: { $ne: true } }, { roomId: 2 }]
133+
}
134+
})
135+
```
136+
137+
```
138+
GET /messages?$and[0][archived][$ne]=true&$and[1][roomId]=2
139+
```
140+
124141
## Operators
125142

126143
Operators either query a property for a specific value or determine nested special properties (starting with a `$`) that allow querying the property for certain conditions. When multiple operators are set, all conditions have to apply for a property to match.

packages/adapter-commons/src/query.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ export const FILTERS: FilterSettings = {
113113
}
114114

115115
return or
116+
},
117+
$and: (and: any, { operators }: FilterQueryOptions) => {
118+
if (Array.isArray(and)) {
119+
return and.map((current) => validateQueryProperty(current, operators))
120+
}
121+
122+
return and
116123
}
117124
}
118125

packages/adapter-tests/src/declarations.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ export type AdapterSyntaxTestName =
8282
| '.find + $ne'
8383
| '.find + $gt + $lt + $sort'
8484
| '.find + $or nested + $sort'
85+
| '.find + $and'
86+
| '.find + $and + $or'
8587
| 'params.adapter + paginate'
8688
| 'params.adapter + multi'
8789
| '.find + paginate'

packages/adapter-tests/src/syntax.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,34 @@ export default (test: AdapterSyntaxTest, app: any, _errors: any, serviceName: st
274274
assert.strictEqual(data[1].name, 'Doug')
275275
})
276276

277+
test('.find + $and', async () => {
278+
const params = {
279+
query: {
280+
$and: [{ age: 19 }],
281+
$sort: { name: 1 }
282+
}
283+
}
284+
285+
const data = await service.find(params)
286+
287+
assert.strictEqual(data.length, 1)
288+
assert.strictEqual(data[0].name, 'Alice')
289+
})
290+
291+
test('.find + $and + $or', async () => {
292+
const params = {
293+
query: {
294+
$and: [{ $or: [{ name: 'Alice' }] }],
295+
$sort: { name: 1 }
296+
}
297+
}
298+
299+
const data = await service.find(params)
300+
301+
assert.strictEqual(data.length, 1)
302+
assert.strictEqual(data[0].name, 'Alice')
303+
})
304+
277305
describe('params.adapter', () => {
278306
test('params.adapter + paginate', async () => {
279307
const page = await service.find({

packages/knex/test/index.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const testSuite = adapterTests([
6565
'.find + $skip',
6666
'.find + $select',
6767
'.find + $or',
68+
'.find + $and',
6869
'.find + $in',
6970
'.find + $nin',
7071
'.find + $lt',
@@ -74,6 +75,7 @@ const testSuite = adapterTests([
7475
'.find + $ne',
7576
'.find + $gt + $lt + $sort',
7677
'.find + $or nested + $sort',
78+
'.find + $and + $or',
7779
'params.adapter + paginate',
7880
'params.adapter + multi',
7981
'.find + paginate',

packages/mongodb/test/index.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ const testSuite = adapterTests([
6464
'.find + $skip',
6565
'.find + $select',
6666
'.find + $or',
67+
'.find + $and',
6768
'.find + $in',
6869
'.find + $nin',
6970
'.find + $lt',
@@ -73,6 +74,7 @@ const testSuite = adapterTests([
7374
'.find + $ne',
7475
'.find + $gt + $lt + $sort',
7576
'.find + $or nested + $sort',
77+
'.find + $and + $or',
7678
'.find + paginate',
7779
'.find + paginate + $limit + $skip',
7880
'.find + paginate + $limit 0',

0 commit comments

Comments
 (0)