metal-orm 1.0.113 → 1.0.114
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 +15 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.js +14 -3
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/dto/openapi/type-mappings.ts +207 -191
package/package.json
CHANGED
|
@@ -1,191 +1,207 @@
|
|
|
1
|
-
import type { ColumnDef } from '../../schema/column-types.js';
|
|
2
|
-
import type { OpenApiType } from './types.js';
|
|
3
|
-
|
|
4
|
-
export interface TypeMappingStrategy {
|
|
5
|
-
supports(columnType: string): boolean;
|
|
6
|
-
getOpenApiType(): OpenApiType;
|
|
7
|
-
getFormat(columnType: string): string | undefined;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export class IntegerTypeStrategy implements TypeMappingStrategy {
|
|
11
|
-
private readonly types = ['INT', 'INTEGER'];
|
|
12
|
-
|
|
13
|
-
supports(type: string): boolean {
|
|
14
|
-
return this.types.includes(type.toUpperCase());
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
getOpenApiType(): OpenApiType {
|
|
18
|
-
return 'integer';
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
getFormat(): string | undefined {
|
|
22
|
-
return 'int32';
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export class BigIntTypeStrategy implements TypeMappingStrategy {
|
|
27
|
-
private readonly types = ['BIGINT'];
|
|
28
|
-
|
|
29
|
-
supports(type: string): boolean {
|
|
30
|
-
return this.types.includes(type.toUpperCase());
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
getOpenApiType(): OpenApiType {
|
|
34
|
-
return 'integer';
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
getFormat(): string | undefined {
|
|
38
|
-
return 'int64';
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export class DecimalTypeStrategy implements TypeMappingStrategy {
|
|
43
|
-
private readonly types = ['DECIMAL', 'FLOAT', 'DOUBLE'];
|
|
44
|
-
|
|
45
|
-
supports(type: string): boolean {
|
|
46
|
-
return this.types.includes(type.toUpperCase());
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
getOpenApiType(): OpenApiType {
|
|
50
|
-
return 'number';
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
getFormat(): string | undefined {
|
|
54
|
-
return 'double';
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export class BooleanTypeStrategy implements TypeMappingStrategy {
|
|
59
|
-
private readonly types = ['BOOLEAN'];
|
|
60
|
-
|
|
61
|
-
supports(type: string): boolean {
|
|
62
|
-
return this.types.includes(type.toUpperCase());
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
getOpenApiType(): OpenApiType {
|
|
66
|
-
return 'boolean';
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
getFormat(): undefined {
|
|
70
|
-
return undefined;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export class UuidTypeStrategy implements TypeMappingStrategy {
|
|
75
|
-
private readonly types = ['UUID'];
|
|
76
|
-
|
|
77
|
-
supports(type: string): boolean {
|
|
78
|
-
return this.types.includes(type.toUpperCase());
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
getOpenApiType(): OpenApiType {
|
|
82
|
-
return 'string';
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
getFormat(): string {
|
|
86
|
-
return 'uuid';
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export class
|
|
91
|
-
private readonly types = ['
|
|
92
|
-
|
|
93
|
-
supports(type: string): boolean {
|
|
94
|
-
return this.types.includes(type.toUpperCase());
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
getOpenApiType(): OpenApiType {
|
|
98
|
-
return 'string';
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
getFormat(
|
|
102
|
-
return
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export class
|
|
107
|
-
private readonly types = [
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
1
|
+
import type { ColumnDef } from '../../schema/column-types.js';
|
|
2
|
+
import type { OpenApiType } from './types.js';
|
|
3
|
+
|
|
4
|
+
export interface TypeMappingStrategy {
|
|
5
|
+
supports(columnType: string): boolean;
|
|
6
|
+
getOpenApiType(): OpenApiType;
|
|
7
|
+
getFormat(columnType: string): string | undefined;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export class IntegerTypeStrategy implements TypeMappingStrategy {
|
|
11
|
+
private readonly types = ['INT', 'INTEGER'];
|
|
12
|
+
|
|
13
|
+
supports(type: string): boolean {
|
|
14
|
+
return this.types.includes(type.toUpperCase());
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
getOpenApiType(): OpenApiType {
|
|
18
|
+
return 'integer';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
getFormat(): string | undefined {
|
|
22
|
+
return 'int32';
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class BigIntTypeStrategy implements TypeMappingStrategy {
|
|
27
|
+
private readonly types = ['BIGINT'];
|
|
28
|
+
|
|
29
|
+
supports(type: string): boolean {
|
|
30
|
+
return this.types.includes(type.toUpperCase());
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
getOpenApiType(): OpenApiType {
|
|
34
|
+
return 'integer';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
getFormat(): string | undefined {
|
|
38
|
+
return 'int64';
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export class DecimalTypeStrategy implements TypeMappingStrategy {
|
|
43
|
+
private readonly types = ['DECIMAL', 'FLOAT', 'DOUBLE'];
|
|
44
|
+
|
|
45
|
+
supports(type: string): boolean {
|
|
46
|
+
return this.types.includes(type.toUpperCase());
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
getOpenApiType(): OpenApiType {
|
|
50
|
+
return 'number';
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
getFormat(): string | undefined {
|
|
54
|
+
return 'double';
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export class BooleanTypeStrategy implements TypeMappingStrategy {
|
|
59
|
+
private readonly types = ['BOOLEAN'];
|
|
60
|
+
|
|
61
|
+
supports(type: string): boolean {
|
|
62
|
+
return this.types.includes(type.toUpperCase());
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
getOpenApiType(): OpenApiType {
|
|
66
|
+
return 'boolean';
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
getFormat(): undefined {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export class UuidTypeStrategy implements TypeMappingStrategy {
|
|
75
|
+
private readonly types = ['UUID'];
|
|
76
|
+
|
|
77
|
+
supports(type: string): boolean {
|
|
78
|
+
return this.types.includes(type.toUpperCase());
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
getOpenApiType(): OpenApiType {
|
|
82
|
+
return 'string';
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
getFormat(): string {
|
|
86
|
+
return 'uuid';
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export class BinaryTypeStrategy implements TypeMappingStrategy {
|
|
91
|
+
private readonly types = ['BLOB', 'BINARY', 'VARBINARY', 'BYTEA'];
|
|
92
|
+
|
|
93
|
+
supports(type: string): boolean {
|
|
94
|
+
return this.types.includes(type.toUpperCase());
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
getOpenApiType(): OpenApiType {
|
|
98
|
+
return 'string';
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
getFormat(): string {
|
|
102
|
+
return 'byte';
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export class DateTimeTypeStrategy implements TypeMappingStrategy {
|
|
107
|
+
private readonly types = ['DATE', 'DATETIME', 'TIMESTAMP', 'TIMESTAMPTZ'];
|
|
108
|
+
|
|
109
|
+
supports(type: string): boolean {
|
|
110
|
+
return this.types.includes(type.toUpperCase());
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
getOpenApiType(): OpenApiType {
|
|
114
|
+
return 'string';
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
getFormat(columnType: string = 'DATETIME'): string {
|
|
118
|
+
return columnType.toUpperCase() === 'DATE' ? 'date' : 'date-time';
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export class StringTypeStrategy implements TypeMappingStrategy {
|
|
123
|
+
private readonly types = [
|
|
124
|
+
'JSON', 'TEXT', 'VARCHAR', 'CHAR', 'ENUM'
|
|
125
|
+
];
|
|
126
|
+
|
|
127
|
+
supports(type: string): boolean {
|
|
128
|
+
return this.types.includes(type.toUpperCase());
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
getOpenApiType(): OpenApiType {
|
|
132
|
+
return 'string';
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
getFormat(): undefined {
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export class DefaultTypeStrategy implements TypeMappingStrategy {
|
|
141
|
+
supports(): boolean {
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
getOpenApiType(): OpenApiType {
|
|
146
|
+
return 'string';
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
getFormat(): undefined {
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export class TypeMappingService {
|
|
155
|
+
private readonly strategies: TypeMappingStrategy[];
|
|
156
|
+
|
|
157
|
+
constructor() {
|
|
158
|
+
this.strategies = [
|
|
159
|
+
new IntegerTypeStrategy(),
|
|
160
|
+
new BigIntTypeStrategy(),
|
|
161
|
+
new DecimalTypeStrategy(),
|
|
162
|
+
new BooleanTypeStrategy(),
|
|
163
|
+
new DateTimeTypeStrategy(),
|
|
164
|
+
new UuidTypeStrategy(),
|
|
165
|
+
new BinaryTypeStrategy(),
|
|
166
|
+
new StringTypeStrategy(),
|
|
167
|
+
new DefaultTypeStrategy(),
|
|
168
|
+
];
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
getOpenApiType(column: ColumnDef): OpenApiType {
|
|
172
|
+
const strategy = this.findStrategy(column.type);
|
|
173
|
+
return strategy.getOpenApiType();
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
getFormat(column: ColumnDef): string | undefined {
|
|
177
|
+
const strategy = this.findStrategy(column.type);
|
|
178
|
+
return strategy.getFormat(column.type);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
private findStrategy(columnType: string): TypeMappingStrategy {
|
|
182
|
+
for (const strategy of this.strategies) {
|
|
183
|
+
if (strategy.supports(columnType)) {
|
|
184
|
+
return strategy;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return this.strategies[this.strategies.length - 1];
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
registerStrategy(strategy: TypeMappingStrategy, index?: number): void {
|
|
191
|
+
if (index !== undefined) {
|
|
192
|
+
this.strategies.splice(index, 0, strategy);
|
|
193
|
+
} else {
|
|
194
|
+
this.strategies.push(strategy);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export const typeMappingService = new TypeMappingService();
|
|
200
|
+
|
|
201
|
+
export function columnTypeToOpenApiType(col: ColumnDef): OpenApiType {
|
|
202
|
+
return typeMappingService.getOpenApiType(col);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export function columnTypeToOpenApiFormat(col: ColumnDef): string | undefined {
|
|
206
|
+
return typeMappingService.getFormat(col);
|
|
207
|
+
}
|