metal-orm 1.0.90 → 1.0.91

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,150 +1,150 @@
1
- /**
2
- * Pagination utility functions for DTO responses.
3
- * Converts basic PaginatedResult to enhanced PagedResponse with computed metadata.
4
- */
5
-
6
- import type { PaginatedResult } from '../query-builder/select.js';
7
- import type { PagedResponse } from './dto-types.js';
8
-
9
- /**
10
- * Converts PaginatedResult to PagedResponse with computed metadata.
11
- *
12
- * @param result - The basic paginated result from executePaged()
13
- * @returns Enhanced paginated response with totalPages, hasNextPage, hasPrevPage
14
- *
15
- * @example
16
- * ```ts
17
- * // In your controller
18
- * const basic = await qb.executePaged(session, { page: 2, pageSize: 20 });
19
- * const response = toPagedResponse(basic);
20
- * return res.json(response);
21
- * // → { items: [...], totalItems: 150, page: 2, pageSize: 20,
22
- * // totalPages: 8, hasNextPage: true, hasPrevPage: true }
23
- * ```
24
- */
25
- export function toPagedResponse<T>(
26
- result: PaginatedResult<T>
27
- ): PagedResponse<T> {
28
- const { items, totalItems, page, pageSize } = result;
29
-
30
- const totalPages = calculateTotalPages(totalItems, pageSize);
31
- const next = hasNextPage(page, totalPages);
32
- const prev = hasPrevPage(page);
33
-
34
- return {
35
- items,
36
- totalItems,
37
- page,
38
- pageSize,
39
- totalPages,
40
- hasNextPage: next,
41
- hasPrevPage: prev,
42
- };
43
- }
44
-
45
- /**
46
- * Creates a reusable toPagedResponse function with fixed pageSize.
47
- * Useful when your API uses a consistent page size across all endpoints.
48
- *
49
- * @param fixedPageSize - The fixed page size to use
50
- * @returns A function that converts PaginatedResult to PagedResponse
51
- *
52
- * @example
53
- * ```ts
54
- * const toUserPagedResponse = toPagedResponseBuilder<UserResponse>(20);
55
- *
56
- * app.get('/users', async (req, res) => {
57
- * const basic = await qb.executePaged(session, { page: req.query.page || 1, pageSize: 20 });
58
- * const response = toUserPagedResponse(basic);
59
- * res.json(response);
60
- * });
61
- * ```
62
- */
63
- export function toPagedResponseBuilder<T>(
64
- fixedPageSize: number
65
- ): (result: Omit<PaginatedResult<T>, 'pageSize'> & { pageSize?: number }) => PagedResponse<T> {
66
- return (result) => toPagedResponse({
67
- ...result,
68
- pageSize: fixedPageSize,
69
- });
70
- }
71
-
72
- /**
73
- * Calculates total pages from total items and page size.
74
- *
75
- * @param totalItems - Total number of items
76
- * @param pageSize - Number of items per page
77
- * @returns Total number of pages (minimum 1)
78
- *
79
- * @example
80
- * ```ts
81
- * const totalPages = calculateTotalPages(150, 20); // → 8
82
- * const totalPages = calculateTotalPages(150, 50); // → 3
83
- * ```
84
- */
85
- export function calculateTotalPages(totalItems: number, pageSize: number): number {
86
- if (pageSize <= 0) {
87
- throw new Error('pageSize must be greater than 0');
88
- }
89
- return Math.max(1, Math.ceil(totalItems / pageSize));
90
- }
91
-
92
- /**
93
- * Checks if there is a next page.
94
- *
95
- * @param currentPage - Current page number (1-based)
96
- * @param totalPages - Total number of pages
97
- * @returns true if there is a next page
98
- *
99
- * @example
100
- * ```ts
101
- * const hasNext = hasNextPage(2, 8); // → true
102
- * const hasNext = hasNextPage(8, 8); // → false
103
- * ```
104
- */
105
- export function hasNextPage(currentPage: number, totalPages: number): boolean {
106
- return currentPage < totalPages;
107
- }
108
-
109
- /**
110
- * Checks if there is a previous page.
111
- *
112
- * @param currentPage - Current page number (1-based)
113
- * @returns true if there is a previous page
114
- *
115
- * @example
116
- * ```ts
117
- * const hasPrev = hasPrevPage(2); // → true
118
- * const hasPrev = hasPrevPage(1); // → false
119
- * ```
120
- */
121
- export function hasPrevPage(currentPage: number): boolean {
122
- return currentPage > 1;
123
- }
124
-
125
- /**
126
- * Computes all pagination metadata from basic pagination info.
127
- *
128
- * @param totalItems - Total number of items
129
- * @param page - Current page number (1-based)
130
- * @param pageSize - Number of items per page
131
- * @returns Object with totalPages, hasNextPage, hasPrevPage
132
- *
133
- * @example
134
- * ```ts
135
- * const meta = computePaginationMetadata(150, 2, 20);
136
- * // → { totalPages: 8, hasNextPage: true, hasPrevPage: true }
137
- * ```
138
- */
139
- export function computePaginationMetadata(
140
- totalItems: number,
141
- page: number,
142
- pageSize: number
143
- ): Pick<PagedResponse<unknown>, 'totalPages' | 'hasNextPage' | 'hasPrevPage'> {
144
- const totalPages = calculateTotalPages(totalItems, pageSize);
145
- return {
146
- totalPages,
147
- hasNextPage: hasNextPage(page, totalPages),
148
- hasPrevPage: hasPrevPage(page),
149
- };
150
- }
1
+ /**
2
+ * Pagination utility functions for DTO responses.
3
+ * Converts basic PaginatedResult to enhanced PagedResponse with computed metadata.
4
+ */
5
+
6
+ import type { PaginatedResult } from '../query-builder/select.js';
7
+ import type { PagedResponse } from './dto-types.js';
8
+
9
+ /**
10
+ * Converts PaginatedResult to PagedResponse with computed metadata.
11
+ *
12
+ * @param result - The basic paginated result from executePaged()
13
+ * @returns Enhanced paginated response with totalPages, hasNextPage, hasPrevPage
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * // In your controller
18
+ * const basic = await qb.executePaged(session, { page: 2, pageSize: 20 });
19
+ * const response = toPagedResponse(basic);
20
+ * return res.json(response);
21
+ * // → { items: [...], totalItems: 150, page: 2, pageSize: 20,
22
+ * // totalPages: 8, hasNextPage: true, hasPrevPage: true }
23
+ * ```
24
+ */
25
+ export function toPagedResponse<T>(
26
+ result: PaginatedResult<T>
27
+ ): PagedResponse<T> {
28
+ const { items, totalItems, page, pageSize } = result;
29
+
30
+ const totalPages = calculateTotalPages(totalItems, pageSize);
31
+ const next = hasNextPage(page, totalPages);
32
+ const prev = hasPrevPage(page);
33
+
34
+ return {
35
+ items,
36
+ totalItems,
37
+ page,
38
+ pageSize,
39
+ totalPages,
40
+ hasNextPage: next,
41
+ hasPrevPage: prev,
42
+ };
43
+ }
44
+
45
+ /**
46
+ * Creates a reusable toPagedResponse function with fixed pageSize.
47
+ * Useful when your API uses a consistent page size across all endpoints.
48
+ *
49
+ * @param fixedPageSize - The fixed page size to use
50
+ * @returns A function that converts PaginatedResult to PagedResponse
51
+ *
52
+ * @example
53
+ * ```ts
54
+ * const toUserPagedResponse = toPagedResponseBuilder<UserResponse>(20);
55
+ *
56
+ * app.get('/users', async (req, res) => {
57
+ * const basic = await qb.executePaged(session, { page: req.query.page || 1, pageSize: 20 });
58
+ * const response = toUserPagedResponse(basic);
59
+ * res.json(response);
60
+ * });
61
+ * ```
62
+ */
63
+ export function toPagedResponseBuilder<T>(
64
+ fixedPageSize: number
65
+ ): (result: Omit<PaginatedResult<T>, 'pageSize'> & { pageSize?: number }) => PagedResponse<T> {
66
+ return (result) => toPagedResponse({
67
+ ...result,
68
+ pageSize: fixedPageSize,
69
+ });
70
+ }
71
+
72
+ /**
73
+ * Calculates total pages from total items and page size.
74
+ *
75
+ * @param totalItems - Total number of items
76
+ * @param pageSize - Number of items per page
77
+ * @returns Total number of pages (minimum 1)
78
+ *
79
+ * @example
80
+ * ```ts
81
+ * const totalPages = calculateTotalPages(150, 20); // → 8
82
+ * const totalPages = calculateTotalPages(150, 50); // → 3
83
+ * ```
84
+ */
85
+ export function calculateTotalPages(totalItems: number, pageSize: number): number {
86
+ if (pageSize <= 0) {
87
+ throw new Error('pageSize must be greater than 0');
88
+ }
89
+ return Math.max(1, Math.ceil(totalItems / pageSize));
90
+ }
91
+
92
+ /**
93
+ * Checks if there is a next page.
94
+ *
95
+ * @param currentPage - Current page number (1-based)
96
+ * @param totalPages - Total number of pages
97
+ * @returns true if there is a next page
98
+ *
99
+ * @example
100
+ * ```ts
101
+ * const hasNext = hasNextPage(2, 8); // → true
102
+ * const hasNext = hasNextPage(8, 8); // → false
103
+ * ```
104
+ */
105
+ export function hasNextPage(currentPage: number, totalPages: number): boolean {
106
+ return currentPage < totalPages;
107
+ }
108
+
109
+ /**
110
+ * Checks if there is a previous page.
111
+ *
112
+ * @param currentPage - Current page number (1-based)
113
+ * @returns true if there is a previous page
114
+ *
115
+ * @example
116
+ * ```ts
117
+ * const hasPrev = hasPrevPage(2); // → true
118
+ * const hasPrev = hasPrevPage(1); // → false
119
+ * ```
120
+ */
121
+ export function hasPrevPage(currentPage: number): boolean {
122
+ return currentPage > 1;
123
+ }
124
+
125
+ /**
126
+ * Computes all pagination metadata from basic pagination info.
127
+ *
128
+ * @param totalItems - Total number of items
129
+ * @param page - Current page number (1-based)
130
+ * @param pageSize - Number of items per page
131
+ * @returns Object with totalPages, hasNextPage, hasPrevPage
132
+ *
133
+ * @example
134
+ * ```ts
135
+ * const meta = computePaginationMetadata(150, 2, 20);
136
+ * // → { totalPages: 8, hasNextPage: true, hasPrevPage: true }
137
+ * ```
138
+ */
139
+ export function computePaginationMetadata(
140
+ totalItems: number,
141
+ page: number,
142
+ pageSize: number
143
+ ): Pick<PagedResponse<unknown>, 'totalPages' | 'hasNextPage' | 'hasPrevPage'> {
144
+ const totalPages = calculateTotalPages(totalItems, pageSize);
145
+ return {
146
+ totalPages,
147
+ hasNextPage: hasNextPage(page, totalPages),
148
+ hasPrevPage: hasPrevPage(page),
149
+ };
150
+ }