Skip to content

Commit

Permalink
feat(database): Add and to the query syntax (#3021)
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl authored Jan 29, 2023
1 parent dbf514e commit 00cb0d9
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 0 deletions.
17 changes: 17 additions & 0 deletions docs/api/databases/querying.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@ app.service('messages').find({
GET /messages?$or[0][archived][$ne]=true&$or[1][roomId]=2
```

### $and

Find all records that match all of the given criteria.

```js
// Find all messages that are not marked as archived and in room 2
app.service('messages').find({
query: {
$all: [{ archived: { $ne: true } }, { roomId: 2 }]
}
})
```

```
GET /messages?$and[0][archived][$ne]=true&$and[1][roomId]=2
```

## Operators

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.
Expand Down
7 changes: 7 additions & 0 deletions packages/adapter-commons/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ export const FILTERS: FilterSettings = {
}

return or
},
$and: (and: any, { operators }: FilterQueryOptions) => {
if (Array.isArray(and)) {
return and.map((current) => validateQueryProperty(current, operators))
}

return and
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/adapter-tests/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ export type AdapterSyntaxTestName =
| '.find + $ne'
| '.find + $gt + $lt + $sort'
| '.find + $or nested + $sort'
| '.find + $and'
| '.find + $and + $or'
| 'params.adapter + paginate'
| 'params.adapter + multi'
| '.find + paginate'
Expand Down
28 changes: 28 additions & 0 deletions packages/adapter-tests/src/syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,34 @@ export default (test: AdapterSyntaxTest, app: any, _errors: any, serviceName: st
assert.strictEqual(data[1].name, 'Doug')
})

test('.find + $and', async () => {
const params = {
query: {
$and: [{ age: 19 }],
$sort: { name: 1 }
}
}

const data = await service.find(params)

assert.strictEqual(data.length, 1)
assert.strictEqual(data[0].name, 'Alice')
})

test('.find + $and + $or', async () => {
const params = {
query: {
$and: [{ $or: [{ name: 'Alice' }] }],
$sort: { name: 1 }
}
}

const data = await service.find(params)

assert.strictEqual(data.length, 1)
assert.strictEqual(data[0].name, 'Alice')
})

describe('params.adapter', () => {
test('params.adapter + paginate', async () => {
const page = await service.find({
Expand Down
2 changes: 2 additions & 0 deletions packages/knex/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const testSuite = adapterTests([
'.find + $skip',
'.find + $select',
'.find + $or',
'.find + $and',
'.find + $in',
'.find + $nin',
'.find + $lt',
Expand All @@ -74,6 +75,7 @@ const testSuite = adapterTests([
'.find + $ne',
'.find + $gt + $lt + $sort',
'.find + $or nested + $sort',
'.find + $and + $or',
'params.adapter + paginate',
'params.adapter + multi',
'.find + paginate',
Expand Down
2 changes: 2 additions & 0 deletions packages/mongodb/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const testSuite = adapterTests([
'.find + $skip',
'.find + $select',
'.find + $or',
'.find + $and',
'.find + $in',
'.find + $nin',
'.find + $lt',
Expand All @@ -73,6 +74,7 @@ const testSuite = adapterTests([
'.find + $ne',
'.find + $gt + $lt + $sort',
'.find + $or nested + $sort',
'.find + $and + $or',
'.find + paginate',
'.find + paginate + $limit + $skip',
'.find + paginate + $limit 0',
Expand Down

0 comments on commit 00cb0d9

Please # to comment.