metal-orm 1.0.90 → 1.0.91

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,532 +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
- }
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
+ }