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.
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +24 -10
- package/dist/index.d.ts +24 -10
- package/dist/index.js.map +1 -1
- package/package.json +4 -2
- package/src/core/ddl/introspect/utils.ts +45 -45
- package/src/decorators/bootstrap.ts +37 -37
- 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 +34 -34
- package/src/dto/openapi/generators/dto.ts +94 -94
- package/src/dto/openapi/generators/filter.ts +74 -74
- package/src/dto/openapi/generators/nested-dto.ts +532 -532
- package/src/dto/openapi/generators/pagination.ts +111 -111
- package/src/dto/openapi/generators/relation-filter.ts +210 -210
- package/src/dto/openapi/index.ts +17 -17
- package/src/dto/openapi/type-mappings.ts +191 -191
- package/src/dto/openapi/types.ts +90 -83
- package/src/dto/openapi/utilities.ts +45 -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.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,111 +1,111 @@
|
|
|
1
|
-
import type { OpenApiSchema, OpenApiParameter } from '../types.js';
|
|
2
|
-
|
|
3
|
-
export interface PaginationParams {
|
|
4
|
-
page?: number;
|
|
5
|
-
pageSize?: number;
|
|
6
|
-
sortBy?: string;
|
|
7
|
-
sortOrder?: 'asc' | 'desc';
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export const paginationParamsSchema: OpenApiSchema = {
|
|
11
|
-
type: 'object',
|
|
12
|
-
properties: {
|
|
13
|
-
page: {
|
|
14
|
-
type: 'integer',
|
|
15
|
-
format: 'int32',
|
|
16
|
-
minimum: 1,
|
|
17
|
-
default: 1,
|
|
18
|
-
description: 'Page number (1-based)',
|
|
19
|
-
},
|
|
20
|
-
pageSize: {
|
|
21
|
-
type: 'integer',
|
|
22
|
-
format: 'int32',
|
|
23
|
-
minimum: 1,
|
|
24
|
-
maximum: 100,
|
|
25
|
-
default: 20,
|
|
26
|
-
description: 'Number of items per page',
|
|
27
|
-
},
|
|
28
|
-
sortBy: {
|
|
29
|
-
type: 'string',
|
|
30
|
-
description: 'Field name to sort by',
|
|
31
|
-
},
|
|
32
|
-
sortOrder: {
|
|
33
|
-
type: 'string',
|
|
34
|
-
enum: ['asc', 'desc'],
|
|
35
|
-
default: 'asc',
|
|
36
|
-
description: 'Sort order',
|
|
37
|
-
},
|
|
38
|
-
},
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
export function toPaginationParams(): OpenApiParameter[] {
|
|
42
|
-
return [
|
|
43
|
-
{
|
|
44
|
-
name: 'page',
|
|
45
|
-
in: 'query',
|
|
46
|
-
schema: paginationParamsSchema.properties?.page as OpenApiSchema,
|
|
47
|
-
description: 'Page number (1-based)',
|
|
48
|
-
},
|
|
49
|
-
{
|
|
50
|
-
name: 'pageSize',
|
|
51
|
-
in: 'query',
|
|
52
|
-
schema: paginationParamsSchema.properties?.pageSize as OpenApiSchema,
|
|
53
|
-
description: 'Number of items per page (max 100)',
|
|
54
|
-
},
|
|
55
|
-
{
|
|
56
|
-
name: 'sortBy',
|
|
57
|
-
in: 'query',
|
|
58
|
-
schema: paginationParamsSchema.properties?.sortBy as OpenApiSchema,
|
|
59
|
-
description: 'Field name to sort by',
|
|
60
|
-
},
|
|
61
|
-
{
|
|
62
|
-
name: 'sortOrder',
|
|
63
|
-
in: 'query',
|
|
64
|
-
schema: paginationParamsSchema.properties?.sortOrder as OpenApiSchema,
|
|
65
|
-
description: 'Sort order (asc or desc)',
|
|
66
|
-
},
|
|
67
|
-
];
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export function pagedResponseToOpenApiSchema<T extends OpenApiSchema>(
|
|
71
|
-
itemSchema: T
|
|
72
|
-
): OpenApiSchema {
|
|
73
|
-
return {
|
|
74
|
-
type: 'object',
|
|
75
|
-
properties: {
|
|
76
|
-
items: {
|
|
77
|
-
type: 'array',
|
|
78
|
-
items: itemSchema,
|
|
79
|
-
},
|
|
80
|
-
totalItems: {
|
|
81
|
-
type: 'integer',
|
|
82
|
-
format: 'int64',
|
|
83
|
-
description: 'Total number of items matching the query',
|
|
84
|
-
},
|
|
85
|
-
page: {
|
|
86
|
-
type: 'integer',
|
|
87
|
-
format: 'int32',
|
|
88
|
-
description: 'Current page number (1-based)',
|
|
89
|
-
},
|
|
90
|
-
pageSize: {
|
|
91
|
-
type: 'integer',
|
|
92
|
-
format: 'int32',
|
|
93
|
-
description: 'Number of items per page',
|
|
94
|
-
},
|
|
95
|
-
totalPages: {
|
|
96
|
-
type: 'integer',
|
|
97
|
-
format: 'int32',
|
|
98
|
-
description: 'Total number of pages',
|
|
99
|
-
},
|
|
100
|
-
hasNextPage: {
|
|
101
|
-
type: 'boolean',
|
|
102
|
-
description: 'Whether there are more pages after the current one',
|
|
103
|
-
},
|
|
104
|
-
hasPrevPage: {
|
|
105
|
-
type: 'boolean',
|
|
106
|
-
description: 'Whether there are pages before the current one',
|
|
107
|
-
},
|
|
108
|
-
},
|
|
109
|
-
required: ['items', 'totalItems', 'page', 'pageSize', 'totalPages', 'hasNextPage', 'hasPrevPage'],
|
|
110
|
-
};
|
|
111
|
-
}
|
|
1
|
+
import type { OpenApiSchema, OpenApiParameter } from '../types.js';
|
|
2
|
+
|
|
3
|
+
export interface PaginationParams {
|
|
4
|
+
page?: number;
|
|
5
|
+
pageSize?: number;
|
|
6
|
+
sortBy?: string;
|
|
7
|
+
sortOrder?: 'asc' | 'desc';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const paginationParamsSchema: OpenApiSchema = {
|
|
11
|
+
type: 'object',
|
|
12
|
+
properties: {
|
|
13
|
+
page: {
|
|
14
|
+
type: 'integer',
|
|
15
|
+
format: 'int32',
|
|
16
|
+
minimum: 1,
|
|
17
|
+
default: 1,
|
|
18
|
+
description: 'Page number (1-based)',
|
|
19
|
+
},
|
|
20
|
+
pageSize: {
|
|
21
|
+
type: 'integer',
|
|
22
|
+
format: 'int32',
|
|
23
|
+
minimum: 1,
|
|
24
|
+
maximum: 100,
|
|
25
|
+
default: 20,
|
|
26
|
+
description: 'Number of items per page',
|
|
27
|
+
},
|
|
28
|
+
sortBy: {
|
|
29
|
+
type: 'string',
|
|
30
|
+
description: 'Field name to sort by',
|
|
31
|
+
},
|
|
32
|
+
sortOrder: {
|
|
33
|
+
type: 'string',
|
|
34
|
+
enum: ['asc', 'desc'],
|
|
35
|
+
default: 'asc',
|
|
36
|
+
description: 'Sort order',
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export function toPaginationParams(): OpenApiParameter[] {
|
|
42
|
+
return [
|
|
43
|
+
{
|
|
44
|
+
name: 'page',
|
|
45
|
+
in: 'query',
|
|
46
|
+
schema: paginationParamsSchema.properties?.page as OpenApiSchema,
|
|
47
|
+
description: 'Page number (1-based)',
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
name: 'pageSize',
|
|
51
|
+
in: 'query',
|
|
52
|
+
schema: paginationParamsSchema.properties?.pageSize as OpenApiSchema,
|
|
53
|
+
description: 'Number of items per page (max 100)',
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
name: 'sortBy',
|
|
57
|
+
in: 'query',
|
|
58
|
+
schema: paginationParamsSchema.properties?.sortBy as OpenApiSchema,
|
|
59
|
+
description: 'Field name to sort by',
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
name: 'sortOrder',
|
|
63
|
+
in: 'query',
|
|
64
|
+
schema: paginationParamsSchema.properties?.sortOrder as OpenApiSchema,
|
|
65
|
+
description: 'Sort order (asc or desc)',
|
|
66
|
+
},
|
|
67
|
+
];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function pagedResponseToOpenApiSchema<T extends OpenApiSchema>(
|
|
71
|
+
itemSchema: T
|
|
72
|
+
): OpenApiSchema {
|
|
73
|
+
return {
|
|
74
|
+
type: 'object',
|
|
75
|
+
properties: {
|
|
76
|
+
items: {
|
|
77
|
+
type: 'array',
|
|
78
|
+
items: itemSchema,
|
|
79
|
+
},
|
|
80
|
+
totalItems: {
|
|
81
|
+
type: 'integer',
|
|
82
|
+
format: 'int64',
|
|
83
|
+
description: 'Total number of items matching the query',
|
|
84
|
+
},
|
|
85
|
+
page: {
|
|
86
|
+
type: 'integer',
|
|
87
|
+
format: 'int32',
|
|
88
|
+
description: 'Current page number (1-based)',
|
|
89
|
+
},
|
|
90
|
+
pageSize: {
|
|
91
|
+
type: 'integer',
|
|
92
|
+
format: 'int32',
|
|
93
|
+
description: 'Number of items per page',
|
|
94
|
+
},
|
|
95
|
+
totalPages: {
|
|
96
|
+
type: 'integer',
|
|
97
|
+
format: 'int32',
|
|
98
|
+
description: 'Total number of pages',
|
|
99
|
+
},
|
|
100
|
+
hasNextPage: {
|
|
101
|
+
type: 'boolean',
|
|
102
|
+
description: 'Whether there are more pages after the current one',
|
|
103
|
+
},
|
|
104
|
+
hasPrevPage: {
|
|
105
|
+
type: 'boolean',
|
|
106
|
+
description: 'Whether there are pages before the current one',
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
required: ['items', 'totalItems', 'page', 'pageSize', 'totalPages', 'hasNextPage', 'hasPrevPage'],
|
|
110
|
+
};
|
|
111
|
+
}
|
|
@@ -1,210 +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
|
-
}
|
|
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
|
+
}
|
package/src/dto/openapi/index.ts
CHANGED
|
@@ -1,17 +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';
|
|
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';
|