metal-orm 1.0.90 → 1.0.92
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.
- package/dist/index.cjs +214 -118
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +71 -32
- package/dist/index.d.ts +71 -32
- package/dist/index.js +206 -118
- package/dist/index.js.map +1 -1
- package/package.json +4 -2
- package/scripts/generate-entities/render.mjs +16 -3
- package/src/core/ddl/introspect/utils.ts +45 -45
- package/src/decorators/bootstrap.ts +37 -37
- package/src/decorators/column-decorator.ts +3 -1
- package/src/dto/apply-filter.ts +279 -281
- package/src/dto/dto-types.ts +229 -229
- package/src/dto/filter-types.ts +193 -193
- package/src/dto/index.ts +97 -97
- package/src/dto/openapi/generators/base.ts +29 -29
- package/src/dto/openapi/generators/column.ts +37 -34
- package/src/dto/openapi/generators/dto.ts +94 -94
- package/src/dto/openapi/generators/filter.ts +75 -74
- package/src/dto/openapi/generators/nested-dto.ts +618 -532
- package/src/dto/openapi/generators/pagination.ts +111 -111
- package/src/dto/openapi/generators/relation-filter.ts +228 -210
- package/src/dto/openapi/index.ts +17 -17
- package/src/dto/openapi/type-mappings.ts +191 -191
- package/src/dto/openapi/types.ts +101 -83
- package/src/dto/openapi/utilities.ts +90 -45
- package/src/dto/pagination-utils.ts +150 -150
- package/src/dto/transform.ts +197 -193
- package/src/index.ts +69 -69
- package/src/orm/entity-context.ts +9 -9
- package/src/orm/entity-metadata.ts +14 -14
- package/src/orm/entity.ts +74 -74
- package/src/orm/orm-session.ts +159 -159
- package/src/orm/relation-change-processor.ts +3 -3
- package/src/orm/runtime-types.ts +5 -5
- package/src/schema/column-types.ts +4 -4
- package/src/schema/types.ts +5 -1
|
@@ -1,45 +1,90 @@
|
|
|
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
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
1
|
+
import type { OpenApiSchema, OpenApiOperation, OpenApiDocumentInfo, ApiRouteDefinition, OpenApiDocument, OpenApiDialect, OpenApiType } 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 applyNullability(
|
|
24
|
+
schema: OpenApiSchema,
|
|
25
|
+
isNullable: boolean,
|
|
26
|
+
dialect: OpenApiDialect
|
|
27
|
+
): OpenApiSchema {
|
|
28
|
+
if (!isNullable) {
|
|
29
|
+
const { nullable: _, ...clean } = schema;
|
|
30
|
+
return clean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (dialect === 'openapi-3.0') {
|
|
34
|
+
return { ...schema, nullable: true };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const type = schema.type;
|
|
38
|
+
if (Array.isArray(type)) {
|
|
39
|
+
if (!type.includes('null')) {
|
|
40
|
+
const { nullable: _, ...clean } = schema;
|
|
41
|
+
return { ...clean, type: [...type, 'null'] as OpenApiType[] };
|
|
42
|
+
}
|
|
43
|
+
} else if (type) {
|
|
44
|
+
const { nullable: _, ...clean } = schema;
|
|
45
|
+
return { ...clean, type: [type, 'null'] as OpenApiType[] };
|
|
46
|
+
} else {
|
|
47
|
+
const { nullable: _, ...clean } = schema;
|
|
48
|
+
return { ...clean, type: ['null'] as OpenApiType[] };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const { nullable: _, ...clean } = schema;
|
|
52
|
+
return clean;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function isNullableColumn(col: { notNull?: boolean }): boolean {
|
|
56
|
+
return !col.notNull;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function getOpenApiVersionForDialect(dialect: OpenApiDialect): string {
|
|
60
|
+
return dialect === 'openapi-3.0' ? '3.0.3' : '3.1.0';
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function generateOpenApiDocument(
|
|
64
|
+
info: OpenApiDocumentInfo,
|
|
65
|
+
routes: ApiRouteDefinition[],
|
|
66
|
+
options?: {
|
|
67
|
+
dialect?: OpenApiDialect;
|
|
68
|
+
allowScalarEquals?: boolean;
|
|
69
|
+
}
|
|
70
|
+
): OpenApiDocument {
|
|
71
|
+
const dialect: OpenApiDialect = options?.dialect ?? 'openapi-3.1';
|
|
72
|
+
const paths: Record<string, Record<string, OpenApiOperation>> = {};
|
|
73
|
+
|
|
74
|
+
for (const route of routes) {
|
|
75
|
+
if (!paths[route.path]) {
|
|
76
|
+
paths[route.path] = {};
|
|
77
|
+
}
|
|
78
|
+
paths[route.path][route.method] = route.operation;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
openapi: getOpenApiVersionForDialect(dialect),
|
|
83
|
+
info: {
|
|
84
|
+
title: info.title,
|
|
85
|
+
version: info.version,
|
|
86
|
+
description: info.description,
|
|
87
|
+
},
|
|
88
|
+
paths,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
@@ -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
|
+
}
|