Skip to content
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

add filter for getting first result #1386

Open
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 19 additions & 3 deletions README.md
Expand Up @@ -77,6 +77,7 @@ __Please help me build OSS__ 👉 [GitHub Sponsors](https://github.com/sponsors/
* [Paginate](#paginate)
* [Sort](#sort)
* [Slice](#slice)
* [First](#first)
* [Operators](#operators)
* [Full-text search](#full-text-search)
* [Relationships](#relationships)
Expand Down Expand Up @@ -106,12 +107,11 @@ __Please help me build OSS__ 👉 [GitHub Sponsors](https://github.com/sponsors/
* [Articles](#articles)
* [Third-party tools](#third-party-tools)
- [License](#license)

<!-- tocstop -->

## Getting started

Install JSON Server
Install JSON Server

```
npm install -g json-server
Expand Down Expand Up @@ -148,7 +148,7 @@ Also when doing requests, it's good to know that:
- If you make POST, PUT, PATCH or DELETE requests, changes will be automatically and safely saved to `db.json` using [lowdb](https://github.com/typicode/lowdb).
- Your request body JSON should be object enclosed, just like the GET output. (for example `{"name": "Foobar"}`)
- Id values are not mutable. Any `id` value in the body of your PUT or PATCH request will be ignored. Only a value set in a POST request will be respected, but only if not already taken.
- A POST, PUT or PATCH request should include a `Content-Type: application/json` header to use the JSON in the request body. Otherwise it will return a 2XX status code, but without changes being made to the data.
- A POST, PUT or PATCH request should include a `Content-Type: application/json` header to use the JSON in the request body. Otherwise it will return a 2XX status code, but without changes being made to the data.

## Routes

Expand Down Expand Up @@ -225,6 +225,22 @@ GET /posts/1/comments?_start=20&_limit=10

_Works exactly as [Array.slice](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) (i.e. `_start` is inclusive and `_end` exclusive)_

### First

Add `_first=true`

```
GET /posts?_first=true
```

more useful when combined with other filters.

Example: get the first post by the author

```
GET /posts?author=typicode&_sort=title&_first=true
```

### Operators

Add `_gte` or `_lte` for getting a range
Expand Down
10 changes: 10 additions & 0 deletions __tests__/server/plural.js
Expand Up @@ -208,6 +208,16 @@ describe('Server', () => {
.expect(200, db.comments.slice(0, 2)))
})

describe('GET /:resource?_first=true', () => {
test('should respond with the first matching resource', () => {
const buyer = db.buyers[3]
return request(server)
.get('/buyers?country=Belize&_sort=total&_order=desc,desc&_first=true')
.expect('Content-Type', /json/)
.expect(200, buyer)
})
})

describe('GET /:resource?_sort=', () => {
test('should respond with json and sort on a field', () =>
request(server)
Expand Down
6 changes: 6 additions & 0 deletions src/server/router/plural.js
Expand Up @@ -58,6 +58,7 @@ module.exports = (db, name, opts) => {
let _start = req.query._start
let _end = req.query._end
let _page = req.query._page
const _first = req.query._first
const _sort = req.query._sort
const _order = req.query._order
let _limit = req.query._limit
Expand All @@ -71,6 +72,7 @@ module.exports = (db, name, opts) => {
delete req.query._limit
delete req.query._embed
delete req.query._expand
delete req.query._first

// Automatically delete query parameters that can't be found
// in the database
Expand Down Expand Up @@ -223,6 +225,10 @@ module.exports = (db, name, opts) => {
expand(element, _expand)
})

if (_first) {
chain = chain.first()
}

res.locals.data = chain.value()
next()
}
Expand Down