-
Notifications
You must be signed in to change notification settings - Fork 74
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 standard datagrid #89
Conversation
} | ||
|
||
const [data, total] = await this.dependencies.userRepository.findAndCount(findOptions); | ||
const totalPages = limit ? Math.ceil(total / Math.max(limit, 1)) : null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should do this more generic. For example, to have some function like:
makePaginationMeta(filters, total, data);
This function will always return structured data:
{ meta: { page: 1, total: 100, limit: 25, totalPages: 4}, data: [{ ... }] }
The main goal is to use this function in other places without thinking about proper object structure.
} | ||
|
||
if (filter) { | ||
findOptions.where = filter; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we assign to where property any filter values from GET, it will be unsafe and the findAndCount
method probably will fail with an error. We should control filters that are passed to TypeOrm query. If some filter name is not supported we can't assign it to the query.
* | ||
*/ | ||
export function makePaginationMeta(size: number, page: number, total: number): PaginationMeta { | ||
export function makePaginationMeta(data: any, total: number, limit?: number, page?: number): PaginationMeta { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A better name for this method will be something like makePaginationResult
because we doesn't return only metadata but also data
.
@@ -1,34 +1,38 @@ | |||
import { Repository } from "typeorm"; | |||
import { AppError } from "../../errors/app.error"; | |||
|
|||
export interface PaginationMeta { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better PaginationResult
?
758df89
to
39d810d
Compare
Add standard datagrid.
Example query string: page=1&limit=100&sort[XYZ]=ASC&filter[XYZ][]
Example response:
{ "meta": { "page": 1, "total": 2, "limit": 1, "totalPages": 1 }, "data": [ { "id": "c8cf550b-9d3d-428d-ba1d-d26433d64956", "firstName": "John", "lastName": "Doe", "email": "john@doe.com" } ] }