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 standard datagrid #89

Merged
merged 7 commits into from
Mar 15, 2024
Merged

Add standard datagrid #89

merged 7 commits into from
Mar 15, 2024

Conversation

pawelTshDev
Copy link
Contributor

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" } ] }

}

const [data, total] = await this.dependencies.userRepository.findAndCount(findOptions);
const totalPages = limit ? Math.ceil(total / Math.max(limit, 1)) : null;
Copy link
Collaborator

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;
Copy link
Collaborator

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 {
Copy link
Collaborator

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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better PaginationResult ?

@pawelTshDev pawelTshDev force-pushed the feat/standard-datagrid branch from 758df89 to 39d810d Compare March 14, 2024 12:21
@pawelTshDev pawelTshDev merged commit 1817b49 into main Mar 15, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants