Skip to content

Feature/add contains filter #691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,41 @@ Add `_like` to filter (RegExp supported)
GET /posts?title_like=server
```

Add `_contains` to filter arrays attributes (case sensitive)

```json
{
"posts": [
{ "id": 1, "title": "json-server", "author": "typicode", "tags": ["technology", "beginner"] },
{ "id": 2, "title": "advanced json-server", "author": "typicode", "tags": ["technology-ai", "advanced"] }
],
"comments": [
{ "id": 1, "body": "some comment", "postId": 1 }
],
"profile": { "name": "typicode" }
}
```

```
GET /posts?tags_contains=beginner
```
_returns [{ "id": 1, "title": "json-server", "author": "typicode", "tags": ["technology", "beginner"] }]_

difference between __contains_ and __like_
```
GET /posts?tags_contains=technology
```
_returns [{ "id": 1, "title": "json-server", "author": "typicode", "tags": ["technology", "beginner"] }]_

while __like_ returns both (_like does substring matching)

_returns
[
{ "id": 1, "title": "json-server", "author": "typicode", "tags": ["technology", "beginner"] },
{ "id": 2, "title": "advanced json-server", "author": "typicode", "tags": ["technology-ai", "advanced"] }
]
_

### Full-text search

Add `q`
Expand Down
24 changes: 18 additions & 6 deletions src/server/router/plural.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ module.exports = (db, name, opts) => {
/_lte$/.test(query) ||
/_gte$/.test(query) ||
/_ne$/.test(query) ||
/_like$/.test(query)
/_like$/.test(query) ||
/_contains$/.test(query)
)
return
}
Expand Down Expand Up @@ -122,7 +123,8 @@ module.exports = (db, name, opts) => {
const isDifferent = /_ne$/.test(key)
const isRange = /_lte$/.test(key) || /_gte$/.test(key)
const isLike = /_like$/.test(key)
const path = key.replace(/(_lte|_gte|_ne|_like)$/, '')
const isContains = /_contains$/.test(key)
const path = key.replace(/(_lte|_gte|_ne|_like|_contains)$/, '')
// get item value based on path
// i.e post.title -> 'foo'
const elementValue = _.get(element, path)
Expand All @@ -138,13 +140,23 @@ module.exports = (db, name, opts) => {
return isLowerThan
? value <= elementValue
: value >= elementValue
} else if (isDifferent) {
}

if (isDifferent) {
return value !== elementValue.toString()
} else if (isLike) {
}

if (isContains) {
let requested = _.filter((value || '').split(','), _.identity)
let found = _.intersection(elementValue, requested)
return _.isEqual(found, requested)
}

if (isLike) {
return new RegExp(value, 'i').test(elementValue.toString())
} else {
return value === elementValue.toString()
}

return value === elementValue.toString()
})
.reduce((a, b) => a || b)
})
Expand Down
23 changes: 23 additions & 0 deletions test/server/plural.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ describe('Server', () => {
{ id: 15 }
]

db.products = [
{ name: 'first product', bought_by: ['firstCustomer', 'secondCustomer'] },
{ name: 'second product', bought_by: ['firstCustomer'] },
{ name: 'third product', bought_by: ['secondCustomer'] },
{ name: 'fourth product' }
]

server = jsonServer.create()
router = jsonServer.router(db)
server.use(jsonServer.defaults())
Expand Down Expand Up @@ -359,6 +366,22 @@ describe('Server', () => {
.expect(200))
})

describe('GET /:resource?attr_contains=', () => {
it('should respond with an array that matches the contains operator (case sensitive)', () =>
request(server)
.get('/products?bought_by_contains=firstCustomer')
.expect('Content-Type', /json/)
.expect([db.products[0], db.products[1]])
.expect(200))

it('should respond with an array that matches the contains operator when multiple contains (case sensitive)', () =>
request(server)
.get('/products?bought_by_contains=firstCustomer,secondCustomer')
.expect('Content-Type', /json/)
.expect([db.products[0]])
.expect(200))
})

describe('GET /:parent/:parentId/:resource', () => {
it('should respond with json and corresponding nested resources', () =>
request(server)
Expand Down