metal-orm 1.0.89 → 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 +2968 -2983
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +784 -251
- package/dist/index.d.ts +784 -251
- package/dist/index.js +2913 -2975
- package/dist/index.js.map +1 -1
- package/package.json +6 -3
- package/src/codegen/typescript.ts +29 -40
- package/src/core/ast/expression-builders.ts +34 -53
- package/src/core/ast/expression-nodes.ts +51 -72
- package/src/core/ast/expression-visitor.ts +219 -252
- package/src/core/ast/expression.ts +20 -21
- package/src/core/ddl/introspect/utils.ts +45 -45
- package/src/core/dialect/abstract.ts +55 -81
- package/src/core/execution/db-executor.ts +4 -5
- package/src/core/execution/executors/mysql-executor.ts +7 -9
- package/src/decorators/bootstrap.ts +29 -26
- package/src/dto/apply-filter.ts +279 -0
- package/src/dto/dto-types.ts +229 -0
- package/src/dto/filter-types.ts +193 -0
- package/src/dto/index.ts +97 -0
- package/src/dto/openapi/generators/base.ts +29 -0
- package/src/dto/openapi/generators/column.ts +34 -0
- package/src/dto/openapi/generators/dto.ts +94 -0
- package/src/dto/openapi/generators/filter.ts +74 -0
- package/src/dto/openapi/generators/nested-dto.ts +532 -0
- package/src/dto/openapi/generators/pagination.ts +111 -0
- package/src/dto/openapi/generators/relation-filter.ts +210 -0
- package/src/dto/openapi/index.ts +17 -0
- package/src/dto/openapi/type-mappings.ts +191 -0
- package/src/dto/openapi/types.ts +90 -0
- package/src/dto/openapi/utilities.ts +45 -0
- package/src/dto/pagination-utils.ts +150 -0
- package/src/dto/transform.ts +197 -0
- package/src/index.ts +5 -3
- 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/orm/unit-of-work.ts +13 -25
- package/src/query-builder/query-ast-service.ts +287 -300
- package/src/query-builder/relation-filter-utils.ts +159 -160
- package/src/query-builder/select.ts +137 -192
- package/src/schema/column-types.ts +4 -4
- package/src/schema/types.ts +5 -1
- package/src/core/ast/ast-validation.ts +0 -19
- package/src/core/ast/param-proxy.ts +0 -47
- package/src/core/ast/query-visitor.ts +0 -273
- package/src/openapi/index.ts +0 -4
- package/src/openapi/query-parameters.ts +0 -207
- package/src/openapi/schema-extractor-input.ts +0 -193
- package/src/openapi/schema-extractor-output.ts +0 -427
- package/src/openapi/schema-extractor-utils.ts +0 -110
- package/src/openapi/schema-extractor.ts +0 -120
- package/src/openapi/schema-types.ts +0 -187
- package/src/openapi/type-mappers.ts +0 -227
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { TableDef } from '../../../schema/table.js';
|
|
2
|
+
import type { ColumnDef } from '../../../schema/column-types.js';
|
|
3
|
+
import type { EntityConstructor } from '../../../orm/entity-metadata.js';
|
|
4
|
+
import type { OpenApiSchema } from '../types.js';
|
|
5
|
+
import { columnTypeToOpenApiType } from '../type-mappings.js';
|
|
6
|
+
import { getColumnMap } from './base.js';
|
|
7
|
+
|
|
8
|
+
function filterFieldToOpenApiSchema(col: ColumnDef): OpenApiSchema {
|
|
9
|
+
const normalizedType = col.type.toUpperCase();
|
|
10
|
+
columnTypeToOpenApiType(col);
|
|
11
|
+
|
|
12
|
+
let filterProperties: Record<string, OpenApiSchema> = {};
|
|
13
|
+
|
|
14
|
+
if (['INT', 'INTEGER', 'BIGINT', 'DECIMAL', 'FLOAT', 'DOUBLE'].includes(normalizedType)) {
|
|
15
|
+
filterProperties = {
|
|
16
|
+
equals: { type: 'number' },
|
|
17
|
+
not: { type: 'number' },
|
|
18
|
+
in: { type: 'array', items: { type: 'number' } },
|
|
19
|
+
notIn: { type: 'array', items: { type: 'number' } },
|
|
20
|
+
lt: { type: 'number' },
|
|
21
|
+
lte: { type: 'number' },
|
|
22
|
+
gt: { type: 'number' },
|
|
23
|
+
gte: { type: 'number' },
|
|
24
|
+
};
|
|
25
|
+
} else if (['BOOLEAN'].includes(normalizedType)) {
|
|
26
|
+
filterProperties = {
|
|
27
|
+
equals: { type: 'boolean' },
|
|
28
|
+
not: { type: 'boolean' },
|
|
29
|
+
};
|
|
30
|
+
} else if (['DATE', 'DATETIME', 'TIMESTAMP', 'TIMESTAMPTZ'].includes(normalizedType)) {
|
|
31
|
+
filterProperties = {
|
|
32
|
+
equals: { type: 'string', format: 'date-time' },
|
|
33
|
+
not: { type: 'string', format: 'date-time' },
|
|
34
|
+
in: { type: 'array', items: { type: 'string', format: 'date-time' } },
|
|
35
|
+
notIn: { type: 'array', items: { type: 'string', format: 'date-time' } },
|
|
36
|
+
lt: { type: 'string', format: 'date-time' },
|
|
37
|
+
lte: { type: 'string', format: 'date-time' },
|
|
38
|
+
gt: { type: 'string', format: 'date-time' },
|
|
39
|
+
gte: { type: 'string', format: 'date-time' },
|
|
40
|
+
};
|
|
41
|
+
} else {
|
|
42
|
+
filterProperties = {
|
|
43
|
+
equals: { type: 'string' },
|
|
44
|
+
not: { type: 'string' },
|
|
45
|
+
in: { type: 'array', items: { type: 'string' } },
|
|
46
|
+
notIn: { type: 'array', items: { type: 'string' } },
|
|
47
|
+
contains: { type: 'string' },
|
|
48
|
+
startsWith: { type: 'string' },
|
|
49
|
+
endsWith: { type: 'string' },
|
|
50
|
+
mode: { type: 'string', enum: ['default', 'insensitive'] },
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
type: 'object',
|
|
56
|
+
properties: filterProperties,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function whereInputToOpenApiSchema<T extends TableDef | EntityConstructor>(
|
|
61
|
+
target: T
|
|
62
|
+
): OpenApiSchema {
|
|
63
|
+
const columns = getColumnMap(target);
|
|
64
|
+
const properties: Record<string, OpenApiSchema> = {};
|
|
65
|
+
|
|
66
|
+
for (const [key, col] of Object.entries(columns)) {
|
|
67
|
+
properties[key] = filterFieldToOpenApiSchema(col);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
type: 'object',
|
|
72
|
+
properties,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
@@ -0,0 +1,532 @@
|
|
|
1
|
+
import type { TableDef } from '../../../schema/table.js';
|
|
2
|
+
import type { EntityConstructor } from '../../../orm/entity-metadata.js';
|
|
3
|
+
import type {
|
|
4
|
+
RelationDef,
|
|
5
|
+
BelongsToRelation,
|
|
6
|
+
HasManyRelation,
|
|
7
|
+
HasOneRelation,
|
|
8
|
+
BelongsToManyRelation
|
|
9
|
+
} from '../../../schema/relation.js';
|
|
10
|
+
import type { OpenApiSchema, OpenApiComponent } from '../types.js';
|
|
11
|
+
import { columnToOpenApiSchema } from './column.js';
|
|
12
|
+
import { getColumnMap } from './base.js';
|
|
13
|
+
import { RelationKinds } from '../../../schema/relation.js';
|
|
14
|
+
|
|
15
|
+
export interface ComponentOptions {
|
|
16
|
+
prefix?: string;
|
|
17
|
+
exclude?: string[];
|
|
18
|
+
include?: string[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface NestedDtoOptions {
|
|
22
|
+
maxDepth?: number;
|
|
23
|
+
includeRelations?: boolean;
|
|
24
|
+
componentOptions?: ComponentOptions;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface ComponentReference {
|
|
28
|
+
$ref: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function isComponentReference(schema: OpenApiSchema): schema is ComponentReference {
|
|
32
|
+
return '$ref' in schema;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function nestedDtoToOpenApiSchema<T extends TableDef | EntityConstructor>(
|
|
36
|
+
target: T,
|
|
37
|
+
options?: NestedDtoOptions
|
|
38
|
+
): OpenApiSchema {
|
|
39
|
+
const depth = options?.maxDepth ?? 2;
|
|
40
|
+
const includeRelations = options?.includeRelations ?? true;
|
|
41
|
+
|
|
42
|
+
return nestedDtoSchema(target, depth, includeRelations, options?.componentOptions);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function nestedDtoSchema(
|
|
46
|
+
target: TableDef | EntityConstructor,
|
|
47
|
+
depth: number,
|
|
48
|
+
includeRelations: boolean,
|
|
49
|
+
componentOptions?: ComponentOptions
|
|
50
|
+
): OpenApiSchema {
|
|
51
|
+
if (depth <= 0) {
|
|
52
|
+
return { type: 'object', properties: {} };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const columns = getColumnMap(target);
|
|
56
|
+
const properties: Record<string, OpenApiSchema> = {};
|
|
57
|
+
|
|
58
|
+
for (const [key, col] of Object.entries(columns)) {
|
|
59
|
+
if (componentOptions?.exclude?.includes(key)) {
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (componentOptions?.include && !componentOptions.include.includes(key)) {
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
properties[key] = columnToOpenApiSchema(col);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const tableDef = target as TableDef;
|
|
71
|
+
if (includeRelations && tableDef.relations) {
|
|
72
|
+
for (const [relationName, relation] of Object.entries(tableDef.relations)) {
|
|
73
|
+
if (componentOptions?.exclude?.includes(relationName)) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (componentOptions?.include && !componentOptions.include.includes(relationName)) {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
properties[relationName] = nestedRelationSchema(relation, depth - 1, componentOptions);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
type: 'object',
|
|
87
|
+
properties,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function nestedRelationSchema(
|
|
92
|
+
relation: RelationDef,
|
|
93
|
+
depth: number,
|
|
94
|
+
componentOptions?: ComponentOptions
|
|
95
|
+
): OpenApiSchema {
|
|
96
|
+
if (depth <= 0) {
|
|
97
|
+
return { type: 'object', properties: {} };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (relation.type === RelationKinds.BelongsTo || relation.type === RelationKinds.HasOne) {
|
|
101
|
+
const target = (relation as BelongsToRelation | HasOneRelation).target;
|
|
102
|
+
return nestedDtoSchema(target, depth, true, componentOptions);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (relation.type === RelationKinds.HasMany || relation.type === RelationKinds.BelongsToMany) {
|
|
106
|
+
const target = (relation as HasManyRelation | BelongsToManyRelation).target;
|
|
107
|
+
return {
|
|
108
|
+
type: 'array',
|
|
109
|
+
items: nestedDtoSchema(target, depth - 1, true, componentOptions),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return { type: 'object', properties: {} };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function updateDtoWithRelationsToOpenApiSchema<T extends TableDef | EntityConstructor>(
|
|
117
|
+
target: T,
|
|
118
|
+
_options?: NestedDtoOptions
|
|
119
|
+
): OpenApiSchema {
|
|
120
|
+
const columns = getColumnMap(target);
|
|
121
|
+
const properties: Record<string, OpenApiSchema> = {};
|
|
122
|
+
|
|
123
|
+
for (const [key, col] of Object.entries(columns)) {
|
|
124
|
+
if (col.autoIncrement || col.generated) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
properties[key] = {
|
|
129
|
+
...columnToOpenApiSchema(col),
|
|
130
|
+
nullable: true,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const tableDef = target as TableDef;
|
|
135
|
+
if (_options?.includeRelations !== false && tableDef.relations) {
|
|
136
|
+
for (const [relationName, relation] of Object.entries(tableDef.relations)) {
|
|
137
|
+
if (relation.type === RelationKinds.BelongsTo || relation.type === RelationKinds.HasOne) {
|
|
138
|
+
properties[relationName] = updateDtoToOpenApiSchemaForComponent(
|
|
139
|
+
(relation as BelongsToRelation | HasOneRelation).target
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return {
|
|
146
|
+
type: 'object',
|
|
147
|
+
properties,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function updateDtoToOpenApiSchemaForComponent(
|
|
152
|
+
target: TableDef | EntityConstructor
|
|
153
|
+
): OpenApiSchema {
|
|
154
|
+
const columns = getColumnMap(target);
|
|
155
|
+
const properties: Record<string, OpenApiSchema> = {};
|
|
156
|
+
|
|
157
|
+
for (const [key, col] of Object.entries(columns)) {
|
|
158
|
+
if (col.autoIncrement || col.generated) {
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
properties[key] = {
|
|
163
|
+
...columnToOpenApiSchema(col),
|
|
164
|
+
nullable: true,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return {
|
|
169
|
+
type: 'object',
|
|
170
|
+
properties,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function generateComponentSchemas(
|
|
175
|
+
targets: Array<{ name: string; table: TableDef | EntityConstructor }>,
|
|
176
|
+
options?: ComponentOptions
|
|
177
|
+
): Record<string, OpenApiSchema> {
|
|
178
|
+
const components: Record<string, OpenApiSchema> = {};
|
|
179
|
+
const prefix = options?.prefix ?? '';
|
|
180
|
+
|
|
181
|
+
for (const target of targets) {
|
|
182
|
+
const componentName = `${prefix}${target.name}`;
|
|
183
|
+
components[componentName] = dtoToOpenApiSchemaForComponent(
|
|
184
|
+
target.table,
|
|
185
|
+
options
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return components;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function dtoToOpenApiSchemaForComponent(
|
|
193
|
+
target: TableDef | EntityConstructor,
|
|
194
|
+
options?: ComponentOptions
|
|
195
|
+
): OpenApiSchema {
|
|
196
|
+
const columns = getColumnMap(target);
|
|
197
|
+
const properties: Record<string, OpenApiSchema> = {};
|
|
198
|
+
const required: string[] = [];
|
|
199
|
+
|
|
200
|
+
for (const [key, col] of Object.entries(columns)) {
|
|
201
|
+
if (options?.exclude?.includes(key)) {
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (options?.include && !options.include.includes(key)) {
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
properties[key] = columnToOpenApiSchema(col);
|
|
210
|
+
|
|
211
|
+
if (col.notNull || col.primary) {
|
|
212
|
+
required.push(key);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return {
|
|
217
|
+
type: 'object',
|
|
218
|
+
properties,
|
|
219
|
+
...(required.length > 0 && { required }),
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export function generateRelationComponents(
|
|
224
|
+
tables: Array<{ name: string; table: TableDef }>,
|
|
225
|
+
options?: ComponentOptions
|
|
226
|
+
): Record<string, OpenApiSchema> {
|
|
227
|
+
const components: Record<string, OpenApiSchema> = {};
|
|
228
|
+
const prefix = options?.prefix ?? '';
|
|
229
|
+
|
|
230
|
+
for (const { name, table } of tables) {
|
|
231
|
+
const baseName = `${prefix}${name}`;
|
|
232
|
+
components[`${baseName}Create`] = createDtoToOpenApiSchemaForComponent(table);
|
|
233
|
+
components[`${baseName}Update`] = updateDtoToOpenApiSchemaForComponent(table);
|
|
234
|
+
components[`${baseName}Filter`] = whereInputWithRelationsToOpenApiSchema(table, {
|
|
235
|
+
columnExclude: options?.exclude,
|
|
236
|
+
columnInclude: options?.include,
|
|
237
|
+
maxDepth: 2,
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return components;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function createDtoToOpenApiSchemaForComponent(
|
|
245
|
+
target: TableDef | EntityConstructor
|
|
246
|
+
): OpenApiSchema {
|
|
247
|
+
const columns = getColumnMap(target);
|
|
248
|
+
const properties: Record<string, OpenApiSchema> = {};
|
|
249
|
+
|
|
250
|
+
for (const [key, col] of Object.entries(columns)) {
|
|
251
|
+
if (col.autoIncrement || col.generated) {
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
properties[key] = columnToOpenApiSchema(col);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return {
|
|
259
|
+
type: 'object',
|
|
260
|
+
properties,
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function whereInputWithRelationsToOpenApiSchema(
|
|
265
|
+
target: TableDef | EntityConstructor,
|
|
266
|
+
options?: {
|
|
267
|
+
columnExclude?: string[];
|
|
268
|
+
columnInclude?: string[];
|
|
269
|
+
relationExclude?: string[];
|
|
270
|
+
relationInclude?: string[];
|
|
271
|
+
maxDepth?: number;
|
|
272
|
+
prefix?: string;
|
|
273
|
+
}
|
|
274
|
+
): OpenApiSchema {
|
|
275
|
+
const columns = getColumnMap(target);
|
|
276
|
+
const properties: Record<string, OpenApiSchema> = {};
|
|
277
|
+
const depth = options?.maxDepth ?? 3;
|
|
278
|
+
|
|
279
|
+
for (const [key, col] of Object.entries(columns)) {
|
|
280
|
+
if (options?.columnExclude?.includes(key)) {
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (options?.columnInclude && !options.columnInclude.includes(key)) {
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
properties[key] = columnToOpenApiSchema(col);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const tableDef = target as TableDef;
|
|
292
|
+
if (tableDef.relations && depth > 0) {
|
|
293
|
+
for (const [relationName, relation] of Object.entries(tableDef.relations)) {
|
|
294
|
+
if (options?.relationExclude?.includes(relationName)) {
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (options?.relationInclude && !options.relationInclude.includes(relationName)) {
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
properties[relationName] = relationFilterToOpenApiSchema(relation, {
|
|
303
|
+
exclude: options?.columnExclude,
|
|
304
|
+
include: options?.columnInclude,
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
return {
|
|
310
|
+
type: 'object',
|
|
311
|
+
properties,
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function relationFilterToOpenApiSchema(
|
|
316
|
+
relation: RelationDef,
|
|
317
|
+
options?: {
|
|
318
|
+
exclude?: string[];
|
|
319
|
+
include?: string[];
|
|
320
|
+
}
|
|
321
|
+
): OpenApiSchema {
|
|
322
|
+
if (relation.type === RelationKinds.BelongsTo || relation.type === RelationKinds.HasOne) {
|
|
323
|
+
return singleRelationFilterToOpenApiSchema((relation as BelongsToRelation | HasOneRelation).target, options);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (relation.type === RelationKinds.HasMany || relation.type === RelationKinds.BelongsToMany) {
|
|
327
|
+
return manyRelationFilterToOpenApiSchema((relation as HasManyRelation | BelongsToManyRelation).target);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
return { type: 'object', properties: {} };
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function singleRelationFilterToOpenApiSchema(
|
|
334
|
+
target: TableDef | EntityConstructor,
|
|
335
|
+
options?: { exclude?: string[]; include?: string[] }
|
|
336
|
+
): OpenApiSchema {
|
|
337
|
+
const columns = getColumnMap(target);
|
|
338
|
+
const properties: Record<string, OpenApiSchema> = {};
|
|
339
|
+
|
|
340
|
+
for (const [key, col] of Object.entries(columns)) {
|
|
341
|
+
if (options?.exclude?.includes(key)) {
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
if (options?.include && !options.include.includes(key)) {
|
|
346
|
+
continue;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
properties[key] = columnToOpenApiSchema(col);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
return {
|
|
353
|
+
type: 'object',
|
|
354
|
+
properties,
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function manyRelationFilterToOpenApiSchema(
|
|
359
|
+
target: TableDef | EntityConstructor
|
|
360
|
+
): OpenApiSchema {
|
|
361
|
+
return {
|
|
362
|
+
type: 'object',
|
|
363
|
+
properties: {
|
|
364
|
+
some: {
|
|
365
|
+
type: 'object',
|
|
366
|
+
description: 'Filter related records that match all conditions',
|
|
367
|
+
properties: generateNestedProperties(target),
|
|
368
|
+
},
|
|
369
|
+
every: {
|
|
370
|
+
type: 'object',
|
|
371
|
+
description: 'Filter related records where all match conditions',
|
|
372
|
+
properties: generateNestedProperties(target),
|
|
373
|
+
},
|
|
374
|
+
none: {
|
|
375
|
+
type: 'object',
|
|
376
|
+
description: 'Filter where no related records match',
|
|
377
|
+
properties: generateNestedProperties(target),
|
|
378
|
+
},
|
|
379
|
+
isEmpty: {
|
|
380
|
+
type: 'boolean',
|
|
381
|
+
description: 'Filter where relation has no related records',
|
|
382
|
+
},
|
|
383
|
+
isNotEmpty: {
|
|
384
|
+
type: 'boolean',
|
|
385
|
+
description: 'Filter where relation has related records',
|
|
386
|
+
},
|
|
387
|
+
},
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function generateNestedProperties(
|
|
392
|
+
target: TableDef | EntityConstructor
|
|
393
|
+
): Record<string, OpenApiSchema> {
|
|
394
|
+
const columns = getColumnMap(target);
|
|
395
|
+
const properties: Record<string, OpenApiSchema> = {};
|
|
396
|
+
|
|
397
|
+
for (const [key, col] of Object.entries(columns)) {
|
|
398
|
+
properties[key] = columnToOpenApiSchema(col);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
return properties;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
export function createApiComponentsSection(
|
|
405
|
+
schemas: Record<string, OpenApiSchema>,
|
|
406
|
+
parameters?: Record<string, OpenApiSchema>,
|
|
407
|
+
responses?: Record<string, OpenApiSchema>
|
|
408
|
+
): OpenApiComponent {
|
|
409
|
+
const component: OpenApiComponent = {};
|
|
410
|
+
|
|
411
|
+
if (Object.keys(schemas).length > 0) {
|
|
412
|
+
component.schemas = schemas;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
if (parameters && Object.keys(parameters).length > 0) {
|
|
416
|
+
component.parameters = parameters;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
if (responses && Object.keys(responses).length > 0) {
|
|
420
|
+
component.responses = responses;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
return component;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
export function createRef(path: string): ComponentReference {
|
|
427
|
+
return { $ref: `#/components/${path}` };
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
export function schemaToRef(schemaName: string): ComponentReference {
|
|
431
|
+
return createRef(`schemas/${schemaName}`);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
export function parameterToRef(paramName: string): ComponentReference {
|
|
435
|
+
return createRef(`parameters/${paramName}`);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
export function responseToRef(responseName: string): ComponentReference {
|
|
439
|
+
return createRef(`responses/${responseName}`);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
export function replaceWithRefs(
|
|
443
|
+
schema: OpenApiSchema,
|
|
444
|
+
schemaMap: Record<string, OpenApiSchema>,
|
|
445
|
+
path: string = 'components/schemas'
|
|
446
|
+
): OpenApiSchema {
|
|
447
|
+
if (typeof schema === 'object' && schema !== null) {
|
|
448
|
+
if ('$ref' in schema) {
|
|
449
|
+
return schema;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const schemaJson = JSON.stringify(schema);
|
|
453
|
+
for (const [name, mapSchema] of Object.entries(schemaMap)) {
|
|
454
|
+
if (JSON.stringify(mapSchema) === schemaJson) {
|
|
455
|
+
return { $ref: `#/${path}/${name}` };
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
if ('type' in schema && schema.type === 'object' && 'properties' in schema) {
|
|
460
|
+
const newProperties: Record<string, OpenApiSchema> = {};
|
|
461
|
+
|
|
462
|
+
for (const [key, value] of Object.entries(schema.properties || {})) {
|
|
463
|
+
newProperties[key] = replaceWithRefs(value as OpenApiSchema, schemaMap, path);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
return {
|
|
467
|
+
...schema,
|
|
468
|
+
properties: newProperties,
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
if ('items' in schema && typeof schema.items === 'object') {
|
|
473
|
+
return {
|
|
474
|
+
...schema,
|
|
475
|
+
items: replaceWithRefs(schema.items, schemaMap, path),
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
if ('allOf' in schema && Array.isArray(schema.allOf)) {
|
|
480
|
+
return {
|
|
481
|
+
...schema,
|
|
482
|
+
allOf: schema.allOf.map(item => replaceWithRefs(item, schemaMap, path)),
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
if ('oneOf' in schema && Array.isArray(schema.oneOf)) {
|
|
487
|
+
return {
|
|
488
|
+
...schema,
|
|
489
|
+
oneOf: schema.oneOf.map(item => replaceWithRefs(item, schemaMap, path)),
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
return schema;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
export function extractReusableSchemas(
|
|
498
|
+
schema: OpenApiSchema,
|
|
499
|
+
existing: Record<string, OpenApiSchema> = {},
|
|
500
|
+
prefix: string = ''
|
|
501
|
+
): Record<string, OpenApiSchema> {
|
|
502
|
+
if (typeof schema !== 'object' || schema === null) {
|
|
503
|
+
return existing;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
if ('type' in schema && schema.type === 'object' && 'properties' in schema) {
|
|
507
|
+
for (const [key, value] of Object.entries(schema.properties || {})) {
|
|
508
|
+
extractReusableSchemas(value as OpenApiSchema, existing, `${prefix}${key.charAt(0).toUpperCase() + key.slice(1)}`);
|
|
509
|
+
}
|
|
510
|
+
} else if ('items' in schema && typeof schema.items === 'object') {
|
|
511
|
+
extractReusableSchemas(schema.items as OpenApiSchema, existing, prefix);
|
|
512
|
+
} else if ('allOf' in schema && Array.isArray(schema.allOf)) {
|
|
513
|
+
for (const item of schema.allOf) {
|
|
514
|
+
extractReusableSchemas(item, existing, prefix);
|
|
515
|
+
}
|
|
516
|
+
} else if ('oneOf' in schema && Array.isArray(schema.oneOf)) {
|
|
517
|
+
for (const item of schema.oneOf) {
|
|
518
|
+
extractReusableSchemas(item, existing, prefix);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
if (!('$ref' in schema)) {
|
|
523
|
+
const name = prefix;
|
|
524
|
+
if (name && 'type' in schema && schema.type === 'object' && 'properties' in schema) {
|
|
525
|
+
if (!(name in existing) && Object.keys(schema.properties || {}).length > 0) {
|
|
526
|
+
existing[name] = { ...schema };
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
return existing;
|
|
532
|
+
}
|
|
@@ -0,0 +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
|
+
}
|