Skip to content

Commit

Permalink
feat(auto rest): simple sort
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Sep 18, 2024
1 parent ef26e06 commit a44ba1b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ export default defineConfig({
With this configuration, moquerie will automatically create RESTful endpoints for each resource types found with:

- `GET /resourceType`: list all instances
- You can filter the results with query parameters, for example `GET /resourceType?name=foo`
- You can paginate with `__page` (first page is `0`) and `__pageSize` (default `10`) query parameters: `GET /resourceType?__page=1&__pageSize=10`
- Filter the results with query parameters, for example `GET /resourceType?name=foo`
- Paginate with `__page` (first page is `0`) and `__pageSize` (default `10`) query parameters: `GET /resourceType?__page=1&__pageSize=10`
- Sort with `__sort` query parameter with the syntax `<field>:asc` or `<field>:desc`: `GET /resourceType?__sort=name:asc`
- `POST /resourceType`: create a new instance
- `GET /resourceType/:id`: get an instance
- `PUT /resourceType/:id`: update an instance
Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/rest/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,19 @@ export async function setupRestApi(mq: MoquerieInstance, expressApp: Application

data = await (ctx.db as UntypedQueryManagerProxy)[resourceType.name].findMany(predicate)

// Sort
if (query.__sort != null) {
const [key, order] = query.__sort.split(':')
data = data.sort((a: any, b: any) => {
const valueA = String(get(a, key))
const valueB = String(get(b, key))
if (order === 'asc') {
return valueA.localeCompare(valueB)
}
return valueB.localeCompare(valueA)
})
}

// Pagination
if (query.__page != null || query.__pageSize != null) {
let page = 0
Expand Down

0 comments on commit a44ba1b

Please sign in to comment.