@mikro-orm/reflection 7.0.9-dev.8 → 7.0.9
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/README.md +1 -1
- package/TsMorphMetadataProvider.d.ts +18 -18
- package/TsMorphMetadataProvider.js +241 -225
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -133,7 +133,7 @@ const author = await em.findOneOrFail(Author, 1, {
|
|
|
133
133
|
populate: ['books'],
|
|
134
134
|
});
|
|
135
135
|
author.name = 'Jon Snow II';
|
|
136
|
-
author.books.getItems().forEach(book => book.title += ' (2nd ed.)');
|
|
136
|
+
author.books.getItems().forEach(book => (book.title += ' (2nd ed.)'));
|
|
137
137
|
author.books.add(orm.em.create(Book, { title: 'New Book', author }));
|
|
138
138
|
|
|
139
139
|
// Flush computes change sets and executes them in a single transaction
|
|
@@ -2,22 +2,22 @@ import { type SourceFile } from 'ts-morph';
|
|
|
2
2
|
import { type EntityMetadata, MetadataProvider } from '@mikro-orm/core';
|
|
3
3
|
/** Metadata provider that uses ts-morph to infer property types from TypeScript source files or declaration files. */
|
|
4
4
|
export declare class TsMorphMetadataProvider extends MetadataProvider {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
5
|
+
private project;
|
|
6
|
+
private sources;
|
|
7
|
+
static useCache(): boolean;
|
|
8
|
+
useCache(): boolean;
|
|
9
|
+
loadEntityMetadata(meta: EntityMetadata): void;
|
|
10
|
+
getExistingSourceFile(path: string, ext?: string, validate?: boolean): SourceFile;
|
|
11
|
+
protected initProperties(meta: EntityMetadata): void;
|
|
12
|
+
private extractType;
|
|
13
|
+
private cleanUpTypeTags;
|
|
14
|
+
private initPropertyType;
|
|
15
|
+
private readTypeFromSource;
|
|
16
|
+
private getSourceFile;
|
|
17
|
+
private stripRelativePath;
|
|
18
|
+
private processWrapper;
|
|
19
|
+
private initProject;
|
|
20
|
+
private initSourceFiles;
|
|
21
|
+
saveToCache(meta: EntityMetadata): void;
|
|
22
|
+
getCacheKey(meta: Pick<EntityMetadata, 'className' | 'path'>): string;
|
|
23
23
|
}
|
|
@@ -1,244 +1,260 @@
|
|
|
1
1
|
import { extname } from 'node:path';
|
|
2
2
|
import { ModuleKind, Project } from 'ts-morph';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
EntitySchema,
|
|
5
|
+
MetadataError,
|
|
6
|
+
MetadataProvider,
|
|
7
|
+
MetadataStorage,
|
|
8
|
+
RawQueryFragment,
|
|
9
|
+
ReferenceKind,
|
|
10
|
+
Type,
|
|
11
|
+
Utils,
|
|
12
|
+
} from '@mikro-orm/core';
|
|
4
13
|
import { fs } from '@mikro-orm/core/fs-utils';
|
|
5
14
|
/** Metadata provider that uses ts-morph to infer property types from TypeScript source files or declaration files. */
|
|
6
15
|
export class TsMorphMetadataProvider extends MetadataProvider {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
16
|
+
project;
|
|
17
|
+
sources;
|
|
18
|
+
static useCache() {
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
useCache() {
|
|
22
|
+
return this.config.get('metadataCache').enabled ?? TsMorphMetadataProvider.useCache();
|
|
23
|
+
}
|
|
24
|
+
loadEntityMetadata(meta) {
|
|
25
|
+
if (!meta.path) {
|
|
26
|
+
return;
|
|
11
27
|
}
|
|
12
|
-
|
|
13
|
-
|
|
28
|
+
this.initProperties(meta);
|
|
29
|
+
}
|
|
30
|
+
getExistingSourceFile(path, ext, validate = true) {
|
|
31
|
+
if (!ext) {
|
|
32
|
+
return this.getExistingSourceFile(path, '.d.ts', false) || this.getExistingSourceFile(path, '.ts');
|
|
14
33
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
34
|
+
const tsPath = /.*\/[^/]+$/.exec(path)[0].replace(/\.js$/, ext);
|
|
35
|
+
return this.getSourceFile(tsPath, validate);
|
|
36
|
+
}
|
|
37
|
+
initProperties(meta) {
|
|
38
|
+
meta.path = fs.normalizePath(meta.path);
|
|
39
|
+
// load types and column names
|
|
40
|
+
for (const prop of Object.values(meta.properties)) {
|
|
41
|
+
const { type, target } = this.extractType(meta, prop);
|
|
42
|
+
this.initPropertyType(meta, prop);
|
|
43
|
+
prop.type = type ?? prop.type;
|
|
44
|
+
prop.target = target;
|
|
20
45
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
initProperties(meta) {
|
|
29
|
-
meta.path = fs.normalizePath(meta.path);
|
|
30
|
-
// load types and column names
|
|
31
|
-
for (const prop of Object.values(meta.properties)) {
|
|
32
|
-
const { type, target } = this.extractType(meta, prop);
|
|
33
|
-
this.initPropertyType(meta, prop);
|
|
34
|
-
prop.type = type ?? prop.type;
|
|
35
|
-
prop.target = target;
|
|
36
|
-
}
|
|
46
|
+
}
|
|
47
|
+
extractType(meta, prop) {
|
|
48
|
+
/* v8 ignore next */
|
|
49
|
+
if (typeof prop.entity === 'string') {
|
|
50
|
+
throw new Error(
|
|
51
|
+
`Relation target needs to be an entity class or EntitySchema instance, '${prop.entity}' given instead for ${meta.className}.${prop.name}.`,
|
|
52
|
+
);
|
|
37
53
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
if (typeof prop.entity === 'string') {
|
|
41
|
-
throw new Error(`Relation target needs to be an entity class or EntitySchema instance, '${prop.entity}' given instead for ${meta.className}.${prop.name}.`);
|
|
42
|
-
}
|
|
43
|
-
if (!prop.entity) {
|
|
44
|
-
return { type: prop.type };
|
|
45
|
-
}
|
|
46
|
-
const tmp = prop.entity();
|
|
47
|
-
const target = EntitySchema.is(tmp) ? tmp.meta.class : tmp;
|
|
48
|
-
return { type: Utils.className(target), target };
|
|
49
|
-
}
|
|
50
|
-
cleanUpTypeTags(type) {
|
|
51
|
-
const genericTags = [/Opt<(.*?)>/, /Hidden<(.*?)>/, /RequiredNullable<(.*?)>/];
|
|
52
|
-
const intersectionTags = ['Opt.Brand', 'Hidden.Brand', 'RequiredNullable.Brand'];
|
|
53
|
-
for (const tag of genericTags) {
|
|
54
|
-
type = type.replace(tag, '$1');
|
|
55
|
-
}
|
|
56
|
-
for (const tag of intersectionTags) {
|
|
57
|
-
type = type.replace(' & ' + tag, '');
|
|
58
|
-
type = type.replace(tag + ' & ', '');
|
|
59
|
-
}
|
|
60
|
-
return type;
|
|
54
|
+
if (!prop.entity) {
|
|
55
|
+
return { type: prop.type };
|
|
61
56
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
this.processWrapper(prop, 'Reference');
|
|
72
|
-
this.processWrapper(prop, 'EntityRef');
|
|
73
|
-
this.processWrapper(prop, 'ScalarRef');
|
|
74
|
-
this.processWrapper(prop, 'ScalarReference');
|
|
75
|
-
this.processWrapper(prop, 'Collection');
|
|
76
|
-
prop.runtimeType ??= prop.type;
|
|
77
|
-
if (/^(Dictionary|Record)<.*>$/.exec(prop.type.replace(/import\(.*\)\./g, ''))) {
|
|
78
|
-
prop.type = 'json';
|
|
79
|
-
}
|
|
57
|
+
const tmp = prop.entity();
|
|
58
|
+
const target = EntitySchema.is(tmp) ? tmp.meta.class : tmp;
|
|
59
|
+
return { type: Utils.className(target), target };
|
|
60
|
+
}
|
|
61
|
+
cleanUpTypeTags(type) {
|
|
62
|
+
const genericTags = [/Opt<(.*?)>/, /Hidden<(.*?)>/, /RequiredNullable<(.*?)>/];
|
|
63
|
+
const intersectionTags = ['Opt.Brand', 'Hidden.Brand', 'RequiredNullable.Brand'];
|
|
64
|
+
for (const tag of genericTags) {
|
|
65
|
+
type = type.replace(tag, '$1');
|
|
80
66
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
/* v8 ignore next */
|
|
85
|
-
if (!cls) {
|
|
86
|
-
throw new MetadataError(`Source class for entity ${meta.className} not found. Verify you have 'compilerOptions.declaration' enabled in your 'tsconfig.json'. If you are using webpack, see https://bit.ly/35pPDNn`);
|
|
87
|
-
}
|
|
88
|
-
const property = (cls.getInstanceProperty(prop.name) ??
|
|
89
|
-
// fallback to the type checker for inherited properties (e.g. with TC39/ES decorators)
|
|
90
|
-
cls.getType().getProperty(prop.name)?.getDeclarations()?.[0]);
|
|
91
|
-
if (!property) {
|
|
92
|
-
return { type: prop.type, optional: prop.nullable };
|
|
93
|
-
}
|
|
94
|
-
const tsType = property.getType();
|
|
95
|
-
const typeName = tsType.getText(property);
|
|
96
|
-
if (prop.enum && tsType.isEnum()) {
|
|
97
|
-
prop.items = tsType.getUnionTypes().map(t => t.getLiteralValueOrThrow());
|
|
98
|
-
}
|
|
99
|
-
if (tsType.isArray()) {
|
|
100
|
-
prop.array = true;
|
|
101
|
-
/* v8 ignore next */
|
|
102
|
-
if (tsType.getArrayElementType().isEnum()) {
|
|
103
|
-
prop.items = tsType
|
|
104
|
-
.getArrayElementType()
|
|
105
|
-
.getUnionTypes()
|
|
106
|
-
.map(t => t.getLiteralValueOrThrow());
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
let type = typeName;
|
|
110
|
-
const union = type.split(' | ');
|
|
111
|
-
const optional = property.hasQuestionToken?.() || union.includes('null') || union.includes('undefined') || tsType.isNullable();
|
|
112
|
-
type = union.filter(t => !['null', 'undefined'].includes(t)).join(' | ');
|
|
113
|
-
prop.array ??= type.endsWith('[]') || !!/Array<(.*)>/.exec(type);
|
|
114
|
-
if (prop.array && prop.enum) {
|
|
115
|
-
prop.enum = false;
|
|
116
|
-
}
|
|
117
|
-
type = type
|
|
118
|
-
.replace(/Array<(.*)>/, '$1') // unwrap array
|
|
119
|
-
.replace(/\[]$/, '') // remove array suffix
|
|
120
|
-
.replace(/\((.*)\)/, '$1'); // unwrap union types
|
|
121
|
-
// keep the array suffix in the type, it is needed in few places in discovery and comparator (`prop.array` is used only for enum arrays)
|
|
122
|
-
if (prop.array && !type.includes(' | ') && prop.kind === ReferenceKind.SCALAR) {
|
|
123
|
-
type += '[]';
|
|
124
|
-
}
|
|
125
|
-
return { type, optional };
|
|
67
|
+
for (const tag of intersectionTags) {
|
|
68
|
+
type = type.replace(' & ' + tag, '');
|
|
69
|
+
type = type.replace(tag + ' & ', '');
|
|
126
70
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
let path = tsPath;
|
|
134
|
-
/* v8 ignore next */
|
|
135
|
-
if (outDir != null) {
|
|
136
|
-
const outDirRelative = fs.relativePath(outDir, baseDir);
|
|
137
|
-
path = path.replace(new RegExp(`^${outDirRelative}`), '');
|
|
138
|
-
}
|
|
139
|
-
path = this.stripRelativePath(path);
|
|
140
|
-
const source = this.sources.find(s => s.getFilePath().endsWith(path));
|
|
141
|
-
if (!source && validate) {
|
|
142
|
-
throw new MetadataError(`Source file '${fs.relativePath(tsPath, baseDir)}' not found. Check your 'entitiesTs' option and verify you have 'compilerOptions.declaration' enabled in your 'tsconfig.json'. If you are using webpack, see https://bit.ly/35pPDNn`);
|
|
143
|
-
}
|
|
144
|
-
return source;
|
|
145
|
-
}
|
|
146
|
-
stripRelativePath(str) {
|
|
147
|
-
return str.replace(/^(?:\.\.\/|\.\/)+/, '/');
|
|
148
|
-
}
|
|
149
|
-
processWrapper(prop, wrapper) {
|
|
150
|
-
// type can be sometimes in form of:
|
|
151
|
-
// `'({ object?: Entity | undefined; } & import("...").Reference<Entity>)'`
|
|
152
|
-
// `{ object?: import("...").Entity | undefined; } & import("...").Reference<Entity>`
|
|
153
|
-
// `{ node?: ({ id?: number | undefined; } & import("...").Reference<import("...").Entity>) | undefined; } & import("...").Reference<Entity>`
|
|
154
|
-
// the regexp is looking for the `wrapper`, possible prefixed with `.` or wrapped in parens.
|
|
155
|
-
const type = prop.type.replace(/import\(.*\)\./g, '').replace(/\{ .* } & ([\w &]+)/g, '$1');
|
|
156
|
-
const m = new RegExp(`(?:^|[.( ])${wrapper}<(\\w+),?.*>(?:$|[) ])`).exec(type);
|
|
157
|
-
if (!m) {
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
160
|
-
prop.type = m[1];
|
|
161
|
-
if (['Ref', 'Reference', 'EntityRef', 'ScalarRef', 'ScalarReference'].includes(wrapper)) {
|
|
162
|
-
prop.ref = true;
|
|
163
|
-
}
|
|
71
|
+
return type;
|
|
72
|
+
}
|
|
73
|
+
initPropertyType(meta, prop) {
|
|
74
|
+
const { type: typeRaw, optional } = this.readTypeFromSource(meta, prop);
|
|
75
|
+
if (typeRaw != null) {
|
|
76
|
+
prop.type = this.cleanUpTypeTags(typeRaw);
|
|
164
77
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
const tsConfigFilePath = this.config.get('discovery').tsConfigPath ?? './tsconfig.json';
|
|
168
|
-
try {
|
|
169
|
-
this.project = new Project({
|
|
170
|
-
tsConfigFilePath: fs.normalizePath(process.cwd(), tsConfigFilePath),
|
|
171
|
-
skipAddingFilesFromTsConfig: true,
|
|
172
|
-
compilerOptions: {
|
|
173
|
-
strictNullChecks: true,
|
|
174
|
-
module: ModuleKind.Node20,
|
|
175
|
-
},
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
catch (e) {
|
|
179
|
-
this.config.getLogger().warn('discovery', e.message);
|
|
180
|
-
this.project = new Project({
|
|
181
|
-
compilerOptions: {
|
|
182
|
-
strictNullChecks: true,
|
|
183
|
-
module: ModuleKind.Node20,
|
|
184
|
-
},
|
|
185
|
-
});
|
|
186
|
-
}
|
|
78
|
+
if (optional) {
|
|
79
|
+
prop.optional = true;
|
|
187
80
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
/* v8 ignore next */
|
|
198
|
-
const path = /\.[jt]s$/.exec(metaPath) ? metaPath.replace(/\.js$/, '.d.ts') : `${metaPath}.d.ts`; // when entities are bundled, their paths are just their names
|
|
199
|
-
const sourceFile = this.project.addSourceFileAtPathIfExists(path);
|
|
200
|
-
if (sourceFile) {
|
|
201
|
-
this.sources.push(sourceFile);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
81
|
+
this.processWrapper(prop, 'Ref');
|
|
82
|
+
this.processWrapper(prop, 'Reference');
|
|
83
|
+
this.processWrapper(prop, 'EntityRef');
|
|
84
|
+
this.processWrapper(prop, 'ScalarRef');
|
|
85
|
+
this.processWrapper(prop, 'ScalarReference');
|
|
86
|
+
this.processWrapper(prop, 'Collection');
|
|
87
|
+
prop.runtimeType ??= prop.type;
|
|
88
|
+
if (/^(Dictionary|Record)<.*>$/.exec(prop.type.replace(/import\(.*\)\./g, ''))) {
|
|
89
|
+
prop.type = 'json';
|
|
204
90
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
91
|
+
}
|
|
92
|
+
readTypeFromSource(meta, prop) {
|
|
93
|
+
const source = this.getExistingSourceFile(meta.path);
|
|
94
|
+
const cls = source.getClass(meta.className);
|
|
95
|
+
/* v8 ignore next */
|
|
96
|
+
if (!cls) {
|
|
97
|
+
throw new MetadataError(
|
|
98
|
+
`Source class for entity ${meta.className} not found. Verify you have 'compilerOptions.declaration' enabled in your 'tsconfig.json'. If you are using webpack, see https://bit.ly/35pPDNn`,
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
const property =
|
|
102
|
+
cls.getInstanceProperty(prop.name) ??
|
|
103
|
+
// fallback to the type checker for inherited properties (e.g. with TC39/ES decorators)
|
|
104
|
+
cls.getType().getProperty(prop.name)?.getDeclarations()?.[0];
|
|
105
|
+
if (!property) {
|
|
106
|
+
return { type: prop.type, optional: prop.nullable };
|
|
107
|
+
}
|
|
108
|
+
const tsType = property.getType();
|
|
109
|
+
const typeName = tsType.getText(property);
|
|
110
|
+
if (prop.enum && tsType.isEnum()) {
|
|
111
|
+
prop.items = tsType.getUnionTypes().map(t => t.getLiteralValueOrThrow());
|
|
112
|
+
}
|
|
113
|
+
if (tsType.isArray()) {
|
|
114
|
+
prop.array = true;
|
|
115
|
+
/* v8 ignore next */
|
|
116
|
+
if (tsType.getArrayElementType().isEnum()) {
|
|
117
|
+
prop.items = tsType
|
|
118
|
+
.getArrayElementType()
|
|
119
|
+
.getUnionTypes()
|
|
120
|
+
.map(t => t.getLiteralValueOrThrow());
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
let type = typeName;
|
|
124
|
+
const union = type.split(' | ');
|
|
125
|
+
const optional =
|
|
126
|
+
property.hasQuestionToken?.() || union.includes('null') || union.includes('undefined') || tsType.isNullable();
|
|
127
|
+
type = union.filter(t => !['null', 'undefined'].includes(t)).join(' | ');
|
|
128
|
+
prop.array ??= type.endsWith('[]') || !!/Array<(.*)>/.exec(type);
|
|
129
|
+
if (prop.array && prop.enum) {
|
|
130
|
+
prop.enum = false;
|
|
131
|
+
}
|
|
132
|
+
type = type
|
|
133
|
+
.replace(/Array<(.*)>/, '$1') // unwrap array
|
|
134
|
+
.replace(/\[]$/, '') // remove array suffix
|
|
135
|
+
.replace(/\((.*)\)/, '$1'); // unwrap union types
|
|
136
|
+
// keep the array suffix in the type, it is needed in few places in discovery and comparator (`prop.array` is used only for enum arrays)
|
|
137
|
+
if (prop.array && !type.includes(' | ') && prop.kind === ReferenceKind.SCALAR) {
|
|
138
|
+
type += '[]';
|
|
139
|
+
}
|
|
140
|
+
return { type, optional };
|
|
141
|
+
}
|
|
142
|
+
getSourceFile(tsPath, validate) {
|
|
143
|
+
if (!this.sources) {
|
|
144
|
+
this.initSourceFiles();
|
|
145
|
+
}
|
|
146
|
+
const baseDir = this.config.get('baseDir');
|
|
147
|
+
const outDir = this.project.getCompilerOptions().outDir;
|
|
148
|
+
let path = tsPath;
|
|
149
|
+
/* v8 ignore next */
|
|
150
|
+
if (outDir != null) {
|
|
151
|
+
const outDirRelative = fs.relativePath(outDir, baseDir);
|
|
152
|
+
path = path.replace(new RegExp(`^${outDirRelative}`), '');
|
|
153
|
+
}
|
|
154
|
+
path = this.stripRelativePath(path);
|
|
155
|
+
const source = this.sources.find(s => s.getFilePath().endsWith(path));
|
|
156
|
+
if (!source && validate) {
|
|
157
|
+
throw new MetadataError(
|
|
158
|
+
`Source file '${fs.relativePath(tsPath, baseDir)}' not found. Check your 'entitiesTs' option and verify you have 'compilerOptions.declaration' enabled in your 'tsconfig.json'. If you are using webpack, see https://bit.ly/35pPDNn`,
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
return source;
|
|
162
|
+
}
|
|
163
|
+
stripRelativePath(str) {
|
|
164
|
+
return str.replace(/^(?:\.\.\/|\.\/)+/, '/');
|
|
165
|
+
}
|
|
166
|
+
processWrapper(prop, wrapper) {
|
|
167
|
+
// type can be sometimes in form of:
|
|
168
|
+
// `'({ object?: Entity | undefined; } & import("...").Reference<Entity>)'`
|
|
169
|
+
// `{ object?: import("...").Entity | undefined; } & import("...").Reference<Entity>`
|
|
170
|
+
// `{ node?: ({ id?: number | undefined; } & import("...").Reference<import("...").Entity>) | undefined; } & import("...").Reference<Entity>`
|
|
171
|
+
// the regexp is looking for the `wrapper`, possible prefixed with `.` or wrapped in parens.
|
|
172
|
+
const type = prop.type.replace(/import\(.*\)\./g, '').replace(/\{ .* } & ([\w &]+)/g, '$1');
|
|
173
|
+
const m = new RegExp(`(?:^|[.( ])${wrapper}<(\\w+),?.*>(?:$|[) ])`).exec(type);
|
|
174
|
+
if (!m) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
prop.type = m[1];
|
|
178
|
+
if (['Ref', 'Reference', 'EntityRef', 'ScalarRef', 'ScalarReference'].includes(wrapper)) {
|
|
179
|
+
prop.ref = true;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
initProject() {
|
|
183
|
+
/* v8 ignore next */
|
|
184
|
+
const tsConfigFilePath = this.config.get('discovery').tsConfigPath ?? './tsconfig.json';
|
|
185
|
+
try {
|
|
186
|
+
this.project = new Project({
|
|
187
|
+
tsConfigFilePath: fs.normalizePath(process.cwd(), tsConfigFilePath),
|
|
188
|
+
skipAddingFilesFromTsConfig: true,
|
|
189
|
+
compilerOptions: {
|
|
190
|
+
strictNullChecks: true,
|
|
191
|
+
module: ModuleKind.Node20,
|
|
192
|
+
},
|
|
193
|
+
});
|
|
194
|
+
} catch (e) {
|
|
195
|
+
this.config.getLogger().warn('discovery', e.message);
|
|
196
|
+
this.project = new Project({
|
|
197
|
+
compilerOptions: {
|
|
198
|
+
strictNullChecks: true,
|
|
199
|
+
module: ModuleKind.Node20,
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
initSourceFiles() {
|
|
205
|
+
this.initProject();
|
|
206
|
+
this.sources = [];
|
|
207
|
+
// All entity files are first required during the discovery, before we reach here, so it is safe to get the parts from the global
|
|
208
|
+
// metadata storage. We know the path thanks to the decorators being executed. In case we are running the TS code, the extension
|
|
209
|
+
// will be already `.ts`, so no change is needed. `.js` files will get renamed to `.d.ts` files as they will be used as a source for
|
|
210
|
+
// the ts-morph reflection.
|
|
211
|
+
for (const meta of Utils.values(MetadataStorage.getMetadata())) {
|
|
212
|
+
const metaPath = fs.normalizePath(meta.path);
|
|
213
|
+
/* v8 ignore next */
|
|
214
|
+
const path = /\.[jt]s$/.exec(metaPath) ? metaPath.replace(/\.js$/, '.d.ts') : `${metaPath}.d.ts`; // when entities are bundled, their paths are just their names
|
|
215
|
+
const sourceFile = this.project.addSourceFileAtPathIfExists(path);
|
|
216
|
+
if (sourceFile) {
|
|
217
|
+
this.sources.push(sourceFile);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
saveToCache(meta) {
|
|
222
|
+
if (!this.useCache()) {
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
Reflect.deleteProperty(meta, 'root'); // to allow caching (as root can contain cycles)
|
|
226
|
+
const copy = Utils.copy(meta, false);
|
|
227
|
+
for (const prop of copy.props) {
|
|
228
|
+
if (Type.isMappedType(prop.type)) {
|
|
229
|
+
Reflect.deleteProperty(prop, 'type');
|
|
230
|
+
Reflect.deleteProperty(prop, 'customType');
|
|
231
|
+
}
|
|
232
|
+
if (prop.default) {
|
|
233
|
+
const raw = RawQueryFragment.getKnownFragment(prop.default);
|
|
234
|
+
if (raw) {
|
|
235
|
+
prop.defaultRaw ??= this.config.getPlatform().formatQuery(raw.sql, raw.params);
|
|
236
|
+
Reflect.deleteProperty(prop, 'default');
|
|
238
237
|
}
|
|
238
|
+
}
|
|
239
|
+
Reflect.deleteProperty(prop, 'targetMeta');
|
|
239
240
|
}
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
241
|
+
[
|
|
242
|
+
'prototype',
|
|
243
|
+
'props',
|
|
244
|
+
'referencingProperties',
|
|
245
|
+
'propertyOrder',
|
|
246
|
+
'relations',
|
|
247
|
+
'concurrencyCheckKeys',
|
|
248
|
+
'checks',
|
|
249
|
+
].forEach(key => delete copy[key]);
|
|
250
|
+
// base entity without properties might not have path, but nothing to cache there
|
|
251
|
+
if (meta.path) {
|
|
252
|
+
meta.path = fs.relativePath(meta.path, this.config.get('baseDir'));
|
|
253
|
+
this.config.getMetadataCacheAdapter().set(this.getCacheKey(meta), copy, meta.path);
|
|
243
254
|
}
|
|
255
|
+
}
|
|
256
|
+
getCacheKey(meta) {
|
|
257
|
+
/* v8 ignore next */
|
|
258
|
+
return meta.className + (meta.path ? extname(meta.path) : '');
|
|
259
|
+
}
|
|
244
260
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/reflection",
|
|
3
|
-
"version": "7.0.9
|
|
3
|
+
"version": "7.0.9",
|
|
4
4
|
"description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"data-mapper",
|
|
@@ -50,10 +50,10 @@
|
|
|
50
50
|
"ts-morph": "27.0.2"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"@mikro-orm/core": "^7.0.
|
|
53
|
+
"@mikro-orm/core": "^7.0.9"
|
|
54
54
|
},
|
|
55
55
|
"peerDependencies": {
|
|
56
|
-
"@mikro-orm/core": "7.0.9
|
|
56
|
+
"@mikro-orm/core": "7.0.9"
|
|
57
57
|
},
|
|
58
58
|
"engines": {
|
|
59
59
|
"node": ">= 22.17.0"
|