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.
Files changed (37) hide show
  1. package/dist/index.cjs +214 -118
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.cts +71 -32
  4. package/dist/index.d.ts +71 -32
  5. package/dist/index.js +206 -118
  6. package/dist/index.js.map +1 -1
  7. package/package.json +4 -2
  8. package/scripts/generate-entities/render.mjs +16 -3
  9. package/src/core/ddl/introspect/utils.ts +45 -45
  10. package/src/decorators/bootstrap.ts +37 -37
  11. package/src/decorators/column-decorator.ts +3 -1
  12. package/src/dto/apply-filter.ts +279 -281
  13. package/src/dto/dto-types.ts +229 -229
  14. package/src/dto/filter-types.ts +193 -193
  15. package/src/dto/index.ts +97 -97
  16. package/src/dto/openapi/generators/base.ts +29 -29
  17. package/src/dto/openapi/generators/column.ts +37 -34
  18. package/src/dto/openapi/generators/dto.ts +94 -94
  19. package/src/dto/openapi/generators/filter.ts +75 -74
  20. package/src/dto/openapi/generators/nested-dto.ts +618 -532
  21. package/src/dto/openapi/generators/pagination.ts +111 -111
  22. package/src/dto/openapi/generators/relation-filter.ts +228 -210
  23. package/src/dto/openapi/index.ts +17 -17
  24. package/src/dto/openapi/type-mappings.ts +191 -191
  25. package/src/dto/openapi/types.ts +101 -83
  26. package/src/dto/openapi/utilities.ts +90 -45
  27. package/src/dto/pagination-utils.ts +150 -150
  28. package/src/dto/transform.ts +197 -193
  29. package/src/index.ts +69 -69
  30. package/src/orm/entity-context.ts +9 -9
  31. package/src/orm/entity-metadata.ts +14 -14
  32. package/src/orm/entity.ts +74 -74
  33. package/src/orm/orm-session.ts +159 -159
  34. package/src/orm/relation-change-processor.ts +3 -3
  35. package/src/orm/runtime-types.ts +5 -5
  36. package/src/schema/column-types.ts +4 -4
  37. 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,228 @@
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, OpenApiDialect } from '../types.js';
12
+ import { columnToFilterSchema } from './filter.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
+ dialect: OpenApiDialect = 'openapi-3.1',
22
+ depth: number = 0
23
+ ): OpenApiSchema {
24
+ if (relation.type === RelationKinds.BelongsTo || relation.type === RelationKinds.HasOne) {
25
+ return singleRelationFilterToOpenApiSchema(
26
+ (relation as BelongsToRelation | HasOneRelation).target,
27
+ options,
28
+ dialect,
29
+ depth
30
+ );
31
+ }
32
+
33
+ if (relation.type === RelationKinds.HasMany || relation.type === RelationKinds.BelongsToMany) {
34
+ return manyRelationFilterToOpenApiSchema(
35
+ (relation as HasManyRelation | BelongsToManyRelation).target,
36
+ options,
37
+ dialect,
38
+ depth
39
+ );
40
+ }
41
+
42
+ return { type: 'object', properties: {} };
43
+ }
44
+
45
+ function singleRelationFilterToOpenApiSchema(
46
+ target: TableDef | EntityConstructor,
47
+ options?: { exclude?: string[]; include?: string[] },
48
+ dialect: OpenApiDialect = 'openapi-3.1',
49
+ depth: number = 0
50
+ ): OpenApiSchema {
51
+ const columns = getColumnMap(target);
52
+ const properties: Record<string, OpenApiSchema> = {};
53
+ const required: string[] = [];
54
+
55
+ for (const [key, col] of Object.entries(columns)) {
56
+ if (options?.exclude?.includes(key)) {
57
+ continue;
58
+ }
59
+
60
+ if (options?.include && !options.include.includes(key)) {
61
+ continue;
62
+ }
63
+
64
+ properties[key] = columnToFilterSchema(col, dialect);
65
+
66
+ if (col.notNull || col.primary) {
67
+ required.push(key);
68
+ }
69
+ }
70
+
71
+ if (depth > 0) {
72
+ const tableDef = target as TableDef;
73
+ if (tableDef.relations) {
74
+ for (const [relationName, relation] of Object.entries(tableDef.relations)) {
75
+ properties[relationName] = relationFilterToOpenApiSchema(relation, undefined, dialect, depth - 1);
76
+ }
77
+ }
78
+ }
79
+
80
+ return {
81
+ type: 'object',
82
+ properties,
83
+ ...(required.length > 0 && { required }),
84
+ };
85
+ }
86
+
87
+ function manyRelationFilterToOpenApiSchema(
88
+ target: TableDef | EntityConstructor,
89
+ options?: { exclude?: string[]; include?: string[] },
90
+ dialect: OpenApiDialect = 'openapi-3.1',
91
+ depth: number = 0
92
+ ): OpenApiSchema {
93
+ const nestedSchema: OpenApiSchema = depth > 0
94
+ ? nestedWhereInputToOpenApiSchema(target, depth, dialect)
95
+ : {
96
+ type: 'object',
97
+ properties: generateNestedProperties(target, options, dialect),
98
+ };
99
+
100
+ return {
101
+ type: 'object',
102
+ properties: {
103
+ some: {
104
+ ...nestedSchema,
105
+ description: 'Filter related records that match all conditions',
106
+ },
107
+ every: {
108
+ ...nestedSchema,
109
+ description: 'Filter related records where all match conditions',
110
+ },
111
+ none: {
112
+ ...nestedSchema,
113
+ description: 'Filter where no related records match',
114
+ },
115
+ isEmpty: {
116
+ type: 'boolean',
117
+ description: 'Filter where relation has no related records',
118
+ },
119
+ isNotEmpty: {
120
+ type: 'boolean',
121
+ description: 'Filter where relation has related records',
122
+ },
123
+ },
124
+ };
125
+ }
126
+
127
+ function generateNestedProperties(
128
+ target: TableDef | EntityConstructor,
129
+ options?: { exclude?: string[]; include?: string[] },
130
+ dialect: OpenApiDialect = 'openapi-3.1'
131
+ ): Record<string, OpenApiSchema> {
132
+ const columns = getColumnMap(target);
133
+ const properties: Record<string, OpenApiSchema> = {};
134
+
135
+ for (const [key, col] of Object.entries(columns)) {
136
+ if (options?.exclude?.includes(key)) {
137
+ continue;
138
+ }
139
+
140
+ if (options?.include && !options.include.includes(key)) {
141
+ continue;
142
+ }
143
+
144
+ properties[key] = columnToFilterSchema(col, dialect);
145
+ }
146
+
147
+ return properties;
148
+ }
149
+
150
+ export function whereInputWithRelationsToOpenApiSchema<T extends TableDef | EntityConstructor>(
151
+ target: T,
152
+ options?: {
153
+ columnExclude?: string[];
154
+ columnInclude?: string[];
155
+ relationExclude?: string[];
156
+ relationInclude?: string[];
157
+ maxDepth?: number;
158
+ },
159
+ dialect: OpenApiDialect = 'openapi-3.1'
160
+ ): OpenApiSchema {
161
+ const columns = getColumnMap(target);
162
+ const properties: Record<string, OpenApiSchema> = {};
163
+ const depth = options?.maxDepth ?? 3;
164
+
165
+ for (const [key, col] of Object.entries(columns)) {
166
+ if (options?.columnExclude?.includes(key)) {
167
+ continue;
168
+ }
169
+
170
+ if (options?.columnInclude && !options.columnInclude.includes(key)) {
171
+ continue;
172
+ }
173
+
174
+ properties[key] = columnToFilterSchema(col, dialect);
175
+ }
176
+
177
+ const tableDef = target as TableDef;
178
+ if (tableDef.relations && depth > 0) {
179
+ for (const [relationName, relation] of Object.entries(tableDef.relations)) {
180
+ if (options?.relationExclude?.includes(relationName)) {
181
+ continue;
182
+ }
183
+
184
+ if (options?.relationInclude && !options.relationInclude.includes(relationName)) {
185
+ continue;
186
+ }
187
+
188
+ properties[relationName] = relationFilterToOpenApiSchema(relation, {
189
+ exclude: options?.columnExclude,
190
+ include: options?.columnInclude,
191
+ }, dialect);
192
+ }
193
+ }
194
+
195
+ return {
196
+ type: 'object',
197
+ properties,
198
+ };
199
+ }
200
+
201
+ export function nestedWhereInputToOpenApiSchema<T extends TableDef | EntityConstructor>(
202
+ target: T,
203
+ depth: number = 2,
204
+ dialect: OpenApiDialect = 'openapi-3.1'
205
+ ): OpenApiSchema {
206
+ if (depth <= 0) {
207
+ return { type: 'object', properties: {} };
208
+ }
209
+
210
+ const columns = getColumnMap(target);
211
+ const properties: Record<string, OpenApiSchema> = {};
212
+
213
+ for (const [key, col] of Object.entries(columns)) {
214
+ properties[key] = columnToFilterSchema(col, dialect);
215
+ }
216
+
217
+ const tableDef = target as TableDef;
218
+ if (tableDef.relations) {
219
+ for (const [relationName, relation] of Object.entries(tableDef.relations)) {
220
+ properties[relationName] = relationFilterToOpenApiSchema(relation, undefined, dialect, depth - 1);
221
+ }
222
+ }
223
+
224
+ return {
225
+ type: 'object',
226
+ properties,
227
+ };
228
+ }