metal-orm 1.0.89 → 1.0.90

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.
Files changed (49) hide show
  1. package/dist/index.cjs +2968 -2983
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.cts +765 -246
  4. package/dist/index.d.ts +765 -246
  5. package/dist/index.js +2913 -2975
  6. package/dist/index.js.map +1 -1
  7. package/package.json +3 -2
  8. package/src/codegen/typescript.ts +29 -40
  9. package/src/core/ast/expression-builders.ts +34 -53
  10. package/src/core/ast/expression-nodes.ts +51 -72
  11. package/src/core/ast/expression-visitor.ts +219 -252
  12. package/src/core/ast/expression.ts +20 -21
  13. package/src/core/dialect/abstract.ts +55 -81
  14. package/src/core/execution/db-executor.ts +4 -5
  15. package/src/core/execution/executors/mysql-executor.ts +7 -9
  16. package/src/decorators/bootstrap.ts +11 -8
  17. package/src/dto/apply-filter.ts +281 -0
  18. package/src/dto/dto-types.ts +229 -0
  19. package/src/dto/filter-types.ts +193 -0
  20. package/src/dto/index.ts +97 -0
  21. package/src/dto/openapi/generators/base.ts +29 -0
  22. package/src/dto/openapi/generators/column.ts +34 -0
  23. package/src/dto/openapi/generators/dto.ts +94 -0
  24. package/src/dto/openapi/generators/filter.ts +74 -0
  25. package/src/dto/openapi/generators/nested-dto.ts +532 -0
  26. package/src/dto/openapi/generators/pagination.ts +111 -0
  27. package/src/dto/openapi/generators/relation-filter.ts +210 -0
  28. package/src/dto/openapi/index.ts +17 -0
  29. package/src/dto/openapi/type-mappings.ts +191 -0
  30. package/src/dto/openapi/types.ts +83 -0
  31. package/src/dto/openapi/utilities.ts +45 -0
  32. package/src/dto/pagination-utils.ts +150 -0
  33. package/src/dto/transform.ts +193 -0
  34. package/src/index.ts +67 -65
  35. package/src/orm/unit-of-work.ts +13 -25
  36. package/src/query-builder/query-ast-service.ts +287 -300
  37. package/src/query-builder/relation-filter-utils.ts +159 -160
  38. package/src/query-builder/select.ts +137 -192
  39. package/src/core/ast/ast-validation.ts +0 -19
  40. package/src/core/ast/param-proxy.ts +0 -47
  41. package/src/core/ast/query-visitor.ts +0 -273
  42. package/src/openapi/index.ts +0 -4
  43. package/src/openapi/query-parameters.ts +0 -207
  44. package/src/openapi/schema-extractor-input.ts +0 -193
  45. package/src/openapi/schema-extractor-output.ts +0 -427
  46. package/src/openapi/schema-extractor-utils.ts +0 -110
  47. package/src/openapi/schema-extractor.ts +0 -120
  48. package/src/openapi/schema-types.ts +0 -187
  49. package/src/openapi/type-mappers.ts +0 -227
@@ -0,0 +1,210 @@
1
+ import type { TableDef } from '../../../schema/table.js';
2
+ import type { EntityConstructor } from '../../../orm/entity-metadata.js';
3
+ import {
4
+ RelationDef,
5
+ BelongsToRelation,
6
+ HasManyRelation,
7
+ HasOneRelation,
8
+ BelongsToManyRelation,
9
+ RelationKinds
10
+ } from '../../../schema/relation.js';
11
+ import type { OpenApiSchema } from '../types.js';
12
+ import { columnToOpenApiSchema } from './column.js';
13
+ import { getColumnMap } from './base.js';
14
+
15
+ export function relationFilterToOpenApiSchema(
16
+ relation: RelationDef,
17
+ options?: {
18
+ exclude?: string[];
19
+ include?: string[];
20
+ }
21
+ ): OpenApiSchema {
22
+ if (relation.type === RelationKinds.BelongsTo || relation.type === RelationKinds.HasOne) {
23
+ return singleRelationFilterToOpenApiSchema((relation as BelongsToRelation | HasOneRelation).target, options);
24
+ }
25
+
26
+ if (relation.type === RelationKinds.HasMany || relation.type === RelationKinds.BelongsToMany) {
27
+ return manyRelationFilterToOpenApiSchema((relation as HasManyRelation | BelongsToManyRelation).target, options);
28
+ }
29
+
30
+ return { type: 'object', properties: {} };
31
+ }
32
+
33
+ function singleRelationFilterToOpenApiSchema(
34
+ target: TableDef | EntityConstructor,
35
+ options?: { exclude?: string[]; include?: string[] }
36
+ ): OpenApiSchema {
37
+ const columns = getColumnMap(target);
38
+ const properties: Record<string, OpenApiSchema> = {};
39
+ const required: string[] = [];
40
+
41
+ for (const [key, col] of Object.entries(columns)) {
42
+ if (options?.exclude?.includes(key)) {
43
+ continue;
44
+ }
45
+
46
+ if (options?.include && !options.include.includes(key)) {
47
+ continue;
48
+ }
49
+
50
+ properties[key] = columnToOpenApiSchema(col);
51
+
52
+ if (col.notNull || col.primary) {
53
+ required.push(key);
54
+ }
55
+ }
56
+
57
+ return {
58
+ type: 'object',
59
+ properties,
60
+ ...(required.length > 0 && { required }),
61
+ };
62
+ }
63
+
64
+ function manyRelationFilterToOpenApiSchema(
65
+ target: TableDef | EntityConstructor,
66
+ options?: { exclude?: string[]; include?: string[] }
67
+ ): OpenApiSchema {
68
+ return {
69
+ type: 'object',
70
+ properties: {
71
+ some: {
72
+ type: 'object',
73
+ description: 'Filter related records that match all conditions',
74
+ properties: generateNestedProperties(target, options),
75
+ },
76
+ every: {
77
+ type: 'object',
78
+ description: 'Filter related records where all match conditions',
79
+ properties: generateNestedProperties(target, options),
80
+ },
81
+ none: {
82
+ type: 'object',
83
+ description: 'Filter where no related records match',
84
+ properties: generateNestedProperties(target, options),
85
+ },
86
+ isEmpty: {
87
+ type: 'boolean',
88
+ description: 'Filter where relation has no related records',
89
+ },
90
+ isNotEmpty: {
91
+ type: 'boolean',
92
+ description: 'Filter where relation has related records',
93
+ },
94
+ },
95
+ };
96
+ }
97
+
98
+ function generateNestedProperties(
99
+ target: TableDef | EntityConstructor,
100
+ options?: { exclude?: string[]; include?: string[] }
101
+ ): Record<string, OpenApiSchema> {
102
+ const columns = getColumnMap(target);
103
+ const properties: Record<string, OpenApiSchema> = {};
104
+
105
+ for (const [key, col] of Object.entries(columns)) {
106
+ if (options?.exclude?.includes(key)) {
107
+ continue;
108
+ }
109
+
110
+ if (options?.include && !options.include.includes(key)) {
111
+ continue;
112
+ }
113
+
114
+ properties[key] = columnToOpenApiSchema(col);
115
+ }
116
+
117
+ return properties;
118
+ }
119
+
120
+ export function whereInputWithRelationsToOpenApiSchema<T extends TableDef | EntityConstructor>(
121
+ target: T,
122
+ options?: {
123
+ columnExclude?: string[];
124
+ columnInclude?: string[];
125
+ relationExclude?: string[];
126
+ relationInclude?: string[];
127
+ maxDepth?: number;
128
+ }
129
+ ): OpenApiSchema {
130
+ const columns = getColumnMap(target);
131
+ const properties: Record<string, OpenApiSchema> = {};
132
+ const depth = options?.maxDepth ?? 3;
133
+
134
+ for (const [key, col] of Object.entries(columns)) {
135
+ if (options?.columnExclude?.includes(key)) {
136
+ continue;
137
+ }
138
+
139
+ if (options?.columnInclude && !options.columnInclude.includes(key)) {
140
+ continue;
141
+ }
142
+
143
+ properties[key] = columnToOpenApiSchema(col);
144
+ }
145
+
146
+ const tableDef = target as TableDef;
147
+ if (tableDef.relations && depth > 0) {
148
+ for (const [relationName, relation] of Object.entries(tableDef.relations)) {
149
+ if (options?.relationExclude?.includes(relationName)) {
150
+ continue;
151
+ }
152
+
153
+ if (options?.relationInclude && !options.relationInclude.includes(relationName)) {
154
+ continue;
155
+ }
156
+
157
+ properties[relationName] = relationFilterToOpenApiSchema(relation, {
158
+ exclude: options?.columnExclude,
159
+ include: options?.columnInclude,
160
+ });
161
+ }
162
+ }
163
+
164
+ return {
165
+ type: 'object',
166
+ properties,
167
+ };
168
+ }
169
+
170
+ export function nestedWhereInputToOpenApiSchema<T extends TableDef | EntityConstructor>(
171
+ target: T,
172
+ depth: number = 2
173
+ ): OpenApiSchema {
174
+ if (depth <= 0) {
175
+ return { type: 'object', properties: {} };
176
+ }
177
+
178
+ const columns = getColumnMap(target);
179
+ const properties: Record<string, OpenApiSchema> = {};
180
+
181
+ for (const [key, col] of Object.entries(columns)) {
182
+ properties[key] = columnToOpenApiSchema(col);
183
+ }
184
+
185
+ const tableDef = target as TableDef;
186
+ if (tableDef.relations) {
187
+ for (const [relationName, relation] of Object.entries(tableDef.relations)) {
188
+ properties[relationName] = relationFilterToOpenApiSchema(relation);
189
+
190
+ if (depth > 1) {
191
+ if (relation.type === RelationKinds.BelongsTo || relation.type === RelationKinds.HasOne) {
192
+ properties[relationName] = singleRelationFilterToOpenApiSchema((relation as BelongsToRelation | HasOneRelation).target);
193
+ } else if (relation.type === RelationKinds.HasMany || relation.type === RelationKinds.BelongsToMany) {
194
+ properties[relationName] = {
195
+ type: 'object',
196
+ properties: {
197
+ some: nestedWhereInputToOpenApiSchema((relation as HasManyRelation | BelongsToManyRelation).target, depth - 1),
198
+ every: nestedWhereInputToOpenApiSchema((relation as HasManyRelation | BelongsToManyRelation).target, depth - 1),
199
+ },
200
+ };
201
+ }
202
+ }
203
+ }
204
+ }
205
+
206
+ return {
207
+ type: 'object',
208
+ properties,
209
+ };
210
+ }
@@ -0,0 +1,17 @@
1
+ export * from './types.js';
2
+ export * from './type-mappings.js';
3
+ export * from './utilities.js';
4
+ export * from './generators/base.js';
5
+ export * from './generators/column.js';
6
+ export * from './generators/dto.js';
7
+ export * from './generators/filter.js';
8
+ export * from './generators/relation-filter.js';
9
+ export * from './generators/nested-dto.js';
10
+ export type {
11
+ PaginationParams,
12
+ } from './generators/pagination.js';
13
+ export {
14
+ paginationParamsSchema,
15
+ toPaginationParams,
16
+ pagedResponseToOpenApiSchema
17
+ } from './generators/pagination.js';
@@ -0,0 +1,191 @@
1
+ import type { ColumnDef } from '../../schema/column-types.js';
2
+ import type { OpenApiType } from './types.js';
3
+
4
+ export interface TypeMappingStrategy {
5
+ supports(columnType: string): boolean;
6
+ getOpenApiType(): OpenApiType;
7
+ getFormat(columnType: string): string | undefined;
8
+ }
9
+
10
+ export class IntegerTypeStrategy implements TypeMappingStrategy {
11
+ private readonly types = ['INT', 'INTEGER'];
12
+
13
+ supports(type: string): boolean {
14
+ return this.types.includes(type.toUpperCase());
15
+ }
16
+
17
+ getOpenApiType(): OpenApiType {
18
+ return 'integer';
19
+ }
20
+
21
+ getFormat(): string | undefined {
22
+ return 'int32';
23
+ }
24
+ }
25
+
26
+ export class BigIntTypeStrategy implements TypeMappingStrategy {
27
+ private readonly types = ['BIGINT'];
28
+
29
+ supports(type: string): boolean {
30
+ return this.types.includes(type.toUpperCase());
31
+ }
32
+
33
+ getOpenApiType(): OpenApiType {
34
+ return 'integer';
35
+ }
36
+
37
+ getFormat(): string | undefined {
38
+ return 'int64';
39
+ }
40
+ }
41
+
42
+ export class DecimalTypeStrategy implements TypeMappingStrategy {
43
+ private readonly types = ['DECIMAL', 'FLOAT', 'DOUBLE'];
44
+
45
+ supports(type: string): boolean {
46
+ return this.types.includes(type.toUpperCase());
47
+ }
48
+
49
+ getOpenApiType(): OpenApiType {
50
+ return 'number';
51
+ }
52
+
53
+ getFormat(): string | undefined {
54
+ return 'double';
55
+ }
56
+ }
57
+
58
+ export class BooleanTypeStrategy implements TypeMappingStrategy {
59
+ private readonly types = ['BOOLEAN'];
60
+
61
+ supports(type: string): boolean {
62
+ return this.types.includes(type.toUpperCase());
63
+ }
64
+
65
+ getOpenApiType(): OpenApiType {
66
+ return 'boolean';
67
+ }
68
+
69
+ getFormat(): undefined {
70
+ return undefined;
71
+ }
72
+ }
73
+
74
+ export class UuidTypeStrategy implements TypeMappingStrategy {
75
+ private readonly types = ['UUID'];
76
+
77
+ supports(type: string): boolean {
78
+ return this.types.includes(type.toUpperCase());
79
+ }
80
+
81
+ getOpenApiType(): OpenApiType {
82
+ return 'string';
83
+ }
84
+
85
+ getFormat(): string {
86
+ return 'uuid';
87
+ }
88
+ }
89
+
90
+ export class DateTimeTypeStrategy implements TypeMappingStrategy {
91
+ private readonly types = ['DATE', 'DATETIME', 'TIMESTAMP', 'TIMESTAMPTZ'];
92
+
93
+ supports(type: string): boolean {
94
+ return this.types.includes(type.toUpperCase());
95
+ }
96
+
97
+ getOpenApiType(): OpenApiType {
98
+ return 'string';
99
+ }
100
+
101
+ getFormat(columnType: string = 'DATETIME'): string {
102
+ return columnType.toUpperCase() === 'DATE' ? 'date' : 'date-time';
103
+ }
104
+ }
105
+
106
+ export class StringTypeStrategy implements TypeMappingStrategy {
107
+ private readonly types = [
108
+ 'JSON', 'TEXT', 'VARCHAR', 'CHAR', 'BINARY',
109
+ 'VARBINARY', 'BLOB', 'ENUM'
110
+ ];
111
+
112
+ supports(type: string): boolean {
113
+ return this.types.includes(type.toUpperCase());
114
+ }
115
+
116
+ getOpenApiType(): OpenApiType {
117
+ return 'string';
118
+ }
119
+
120
+ getFormat(): undefined {
121
+ return undefined;
122
+ }
123
+ }
124
+
125
+ export class DefaultTypeStrategy implements TypeMappingStrategy {
126
+ supports(): boolean {
127
+ return true;
128
+ }
129
+
130
+ getOpenApiType(): OpenApiType {
131
+ return 'string';
132
+ }
133
+
134
+ getFormat(): undefined {
135
+ return undefined;
136
+ }
137
+ }
138
+
139
+ export class TypeMappingService {
140
+ private readonly strategies: TypeMappingStrategy[];
141
+
142
+ constructor() {
143
+ this.strategies = [
144
+ new IntegerTypeStrategy(),
145
+ new BigIntTypeStrategy(),
146
+ new DecimalTypeStrategy(),
147
+ new BooleanTypeStrategy(),
148
+ new DateTimeTypeStrategy(),
149
+ new UuidTypeStrategy(),
150
+ new StringTypeStrategy(),
151
+ new DefaultTypeStrategy(),
152
+ ];
153
+ }
154
+
155
+ getOpenApiType(column: ColumnDef): OpenApiType {
156
+ const strategy = this.findStrategy(column.type);
157
+ return strategy.getOpenApiType();
158
+ }
159
+
160
+ getFormat(column: ColumnDef): string | undefined {
161
+ const strategy = this.findStrategy(column.type);
162
+ return strategy.getFormat(column.type);
163
+ }
164
+
165
+ private findStrategy(columnType: string): TypeMappingStrategy {
166
+ for (const strategy of this.strategies) {
167
+ if (strategy.supports(columnType)) {
168
+ return strategy;
169
+ }
170
+ }
171
+ return this.strategies[this.strategies.length - 1];
172
+ }
173
+
174
+ registerStrategy(strategy: TypeMappingStrategy, index?: number): void {
175
+ if (index !== undefined) {
176
+ this.strategies.splice(index, 0, strategy);
177
+ } else {
178
+ this.strategies.push(strategy);
179
+ }
180
+ }
181
+ }
182
+
183
+ export const typeMappingService = new TypeMappingService();
184
+
185
+ export function columnTypeToOpenApiType(col: ColumnDef): OpenApiType {
186
+ return typeMappingService.getOpenApiType(col);
187
+ }
188
+
189
+ export function columnTypeToOpenApiFormat(col: ColumnDef): string | undefined {
190
+ return typeMappingService.getFormat(col);
191
+ }
@@ -0,0 +1,83 @@
1
+ export type OpenApiType =
2
+ | 'string'
3
+ | 'number'
4
+ | 'integer'
5
+ | 'boolean'
6
+ | 'array'
7
+ | 'object'
8
+ | 'null';
9
+
10
+ export interface OpenApiSchema {
11
+ type?: OpenApiType | OpenApiType[];
12
+ properties?: Record<string, OpenApiSchema>;
13
+ items?: OpenApiSchema;
14
+ required?: string[];
15
+ enum?: unknown[];
16
+ format?: string;
17
+ description?: string;
18
+ example?: unknown;
19
+ nullable?: boolean;
20
+ minimum?: number;
21
+ maximum?: number;
22
+ default?: unknown;
23
+ $ref?: string;
24
+ allOf?: OpenApiSchema[];
25
+ oneOf?: OpenApiSchema[];
26
+ }
27
+
28
+ export interface OpenApiParameter {
29
+ name: string;
30
+ in: 'query' | 'path' | 'header' | 'cookie';
31
+ required?: boolean;
32
+ schema?: OpenApiSchema;
33
+ description?: string;
34
+ }
35
+
36
+ export interface OpenApiOperation {
37
+ summary?: string;
38
+ description?: string;
39
+ parameters?: OpenApiParameter[];
40
+ requestBody?: {
41
+ description?: string;
42
+ required?: boolean;
43
+ content: {
44
+ 'application/json': {
45
+ schema: OpenApiSchema;
46
+ };
47
+ };
48
+ };
49
+ responses?: Record<string, {
50
+ description: string;
51
+ content?: {
52
+ 'application/json': {
53
+ schema: OpenApiSchema;
54
+ };
55
+ };
56
+ }>;
57
+ }
58
+
59
+ export interface OpenApiDocumentInfo {
60
+ title: string;
61
+ version: string;
62
+ description?: string;
63
+ }
64
+
65
+ export interface ApiRouteDefinition {
66
+ path: string;
67
+ method: 'get' | 'post' | 'put' | 'patch' | 'delete';
68
+ operation: OpenApiOperation;
69
+ }
70
+
71
+ export interface PaginationParams {
72
+ page?: number;
73
+ pageSize?: number;
74
+ sortBy?: string;
75
+ sortOrder?: 'asc' | 'desc';
76
+ }
77
+
78
+ export interface OpenApiComponent {
79
+ schemas?: Record<string, OpenApiSchema>;
80
+ parameters?: Record<string, OpenApiSchema>;
81
+ responses?: Record<string, OpenApiSchema>;
82
+ securitySchemes?: Record<string, unknown>;
83
+ }
@@ -0,0 +1,45 @@
1
+ import type { OpenApiSchema, OpenApiOperation, OpenApiDocumentInfo, ApiRouteDefinition } from './types.js';
2
+
3
+ export function schemaToJson(schema: OpenApiSchema): string {
4
+ return JSON.stringify(schema, null, 2);
5
+ }
6
+
7
+ export function deepCloneSchema(schema: OpenApiSchema): OpenApiSchema {
8
+ return JSON.parse(JSON.stringify(schema));
9
+ }
10
+
11
+ export function mergeSchemas(base: OpenApiSchema, override: Partial<OpenApiSchema>): OpenApiSchema {
12
+ return {
13
+ ...base,
14
+ ...override,
15
+ properties: {
16
+ ...base.properties,
17
+ ...(override.properties || {}),
18
+ },
19
+ required: override.required ?? base.required,
20
+ };
21
+ }
22
+
23
+ export function generateOpenApiDocument(
24
+ info: OpenApiDocumentInfo,
25
+ routes: ApiRouteDefinition[]
26
+ ): Record<string, unknown> {
27
+ const paths: Record<string, Record<string, OpenApiOperation>> = {};
28
+
29
+ for (const route of routes) {
30
+ if (!paths[route.path]) {
31
+ paths[route.path] = {};
32
+ }
33
+ paths[route.path][route.method] = route.operation;
34
+ }
35
+
36
+ return {
37
+ openapi: '3.1.0',
38
+ info: {
39
+ title: info.title,
40
+ version: info.version,
41
+ description: info.description,
42
+ },
43
+ paths,
44
+ };
45
+ }
@@ -0,0 +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
+ }