@mikro-orm/reflection 7.0.10 → 7.0.11-dev.1
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 +225 -241
- package/package.json +2 -2
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 =>
|
|
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,260 +1,244 @@
|
|
|
1
1
|
import { extname } from 'node:path';
|
|
2
2
|
import { ModuleKind, Project } from 'ts-morph';
|
|
3
|
-
import {
|
|
4
|
-
EntitySchema,
|
|
5
|
-
MetadataError,
|
|
6
|
-
MetadataProvider,
|
|
7
|
-
MetadataStorage,
|
|
8
|
-
RawQueryFragment,
|
|
9
|
-
ReferenceKind,
|
|
10
|
-
Type,
|
|
11
|
-
Utils,
|
|
12
|
-
} from '@mikro-orm/core';
|
|
3
|
+
import { EntitySchema, MetadataError, MetadataProvider, MetadataStorage, RawQueryFragment, ReferenceKind, Type, Utils, } from '@mikro-orm/core';
|
|
13
4
|
import { fs } from '@mikro-orm/core/fs-utils';
|
|
14
5
|
/** Metadata provider that uses ts-morph to infer property types from TypeScript source files or declaration files. */
|
|
15
6
|
export class TsMorphMetadataProvider extends MetadataProvider {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
useCache() {
|
|
22
|
-
return this.config.get('metadataCache').enabled ?? TsMorphMetadataProvider.useCache();
|
|
23
|
-
}
|
|
24
|
-
loadEntityMetadata(meta) {
|
|
25
|
-
if (!meta.path) {
|
|
26
|
-
return;
|
|
7
|
+
project;
|
|
8
|
+
sources;
|
|
9
|
+
static useCache() {
|
|
10
|
+
return true;
|
|
27
11
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
getExistingSourceFile(path, ext, validate = true) {
|
|
31
|
-
if (!ext) {
|
|
32
|
-
return this.getExistingSourceFile(path, '.d.ts', false) || this.getExistingSourceFile(path, '.ts');
|
|
12
|
+
useCache() {
|
|
13
|
+
return this.config.get('metadataCache').enabled ?? TsMorphMetadataProvider.useCache();
|
|
33
14
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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;
|
|
45
|
-
}
|
|
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
|
-
);
|
|
53
|
-
}
|
|
54
|
-
if (!prop.entity) {
|
|
55
|
-
return { type: prop.type };
|
|
56
|
-
}
|
|
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');
|
|
66
|
-
}
|
|
67
|
-
for (const tag of intersectionTags) {
|
|
68
|
-
type = type.replace(' & ' + tag, '');
|
|
69
|
-
type = type.replace(tag + ' & ', '');
|
|
70
|
-
}
|
|
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);
|
|
77
|
-
}
|
|
78
|
-
if (optional) {
|
|
79
|
-
prop.optional = true;
|
|
80
|
-
}
|
|
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';
|
|
90
|
-
}
|
|
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();
|
|
15
|
+
loadEntityMetadata(meta) {
|
|
16
|
+
if (!meta.path) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
this.initProperties(meta);
|
|
145
20
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
21
|
+
getExistingSourceFile(path, ext, validate = true) {
|
|
22
|
+
if (!ext) {
|
|
23
|
+
return this.getExistingSourceFile(path, '.d.ts', false) || this.getExistingSourceFile(path, '.ts');
|
|
24
|
+
}
|
|
25
|
+
const tsPath = /.*\/[^/]+$/.exec(path)[0].replace(/\.js$/, ext);
|
|
26
|
+
return this.getSourceFile(tsPath, validate);
|
|
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
|
+
}
|
|
153
37
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
38
|
+
extractType(meta, prop) {
|
|
39
|
+
/* v8 ignore next */
|
|
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;
|
|
160
61
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
62
|
+
initPropertyType(meta, prop) {
|
|
63
|
+
const { type: typeRaw, optional } = this.readTypeFromSource(meta, prop);
|
|
64
|
+
if (typeRaw != null) {
|
|
65
|
+
prop.type = this.cleanUpTypeTags(typeRaw);
|
|
66
|
+
}
|
|
67
|
+
if (optional) {
|
|
68
|
+
prop.optional = true;
|
|
69
|
+
}
|
|
70
|
+
this.processWrapper(prop, 'Ref');
|
|
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
|
+
}
|
|
176
80
|
}
|
|
177
|
-
prop
|
|
178
|
-
|
|
179
|
-
|
|
81
|
+
readTypeFromSource(meta, prop) {
|
|
82
|
+
const source = this.getExistingSourceFile(meta.path);
|
|
83
|
+
const cls = source.getClass(meta.className);
|
|
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 };
|
|
180
126
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
127
|
+
getSourceFile(tsPath, validate) {
|
|
128
|
+
if (!this.sources) {
|
|
129
|
+
this.initSourceFiles();
|
|
130
|
+
}
|
|
131
|
+
const baseDir = this.config.get('baseDir');
|
|
132
|
+
const outDir = this.project.getCompilerOptions().outDir;
|
|
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
|
+
}
|
|
202
164
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
165
|
+
initProject() {
|
|
166
|
+
/* v8 ignore next */
|
|
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
|
+
}
|
|
219
187
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
188
|
+
initSourceFiles() {
|
|
189
|
+
this.initProject();
|
|
190
|
+
this.sources = [];
|
|
191
|
+
// All entity files are first required during the discovery, before we reach here, so it is safe to get the parts from the global
|
|
192
|
+
// metadata storage. We know the path thanks to the decorators being executed. In case we are running the TS code, the extension
|
|
193
|
+
// 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
|
|
194
|
+
// the ts-morph reflection.
|
|
195
|
+
for (const meta of Utils.values(MetadataStorage.getMetadata())) {
|
|
196
|
+
const metaPath = fs.normalizePath(meta.path);
|
|
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
|
+
}
|
|
224
204
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
Reflect.deleteProperty(
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
205
|
+
saveToCache(meta) {
|
|
206
|
+
if (!this.useCache()) {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
Reflect.deleteProperty(meta, 'root'); // to allow caching (as root can contain cycles)
|
|
210
|
+
const copy = Utils.copy(meta, false);
|
|
211
|
+
for (const prop of copy.props) {
|
|
212
|
+
if (Type.isMappedType(prop.type)) {
|
|
213
|
+
Reflect.deleteProperty(prop, 'type');
|
|
214
|
+
Reflect.deleteProperty(prop, 'customType');
|
|
215
|
+
}
|
|
216
|
+
if (prop.default) {
|
|
217
|
+
const raw = RawQueryFragment.getKnownFragment(prop.default);
|
|
218
|
+
if (raw) {
|
|
219
|
+
prop.defaultRaw ??= this.config.getPlatform().formatQuery(raw.sql, raw.params);
|
|
220
|
+
Reflect.deleteProperty(prop, 'default');
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
Reflect.deleteProperty(prop, 'targetMeta');
|
|
224
|
+
}
|
|
225
|
+
[
|
|
226
|
+
'prototype',
|
|
227
|
+
'props',
|
|
228
|
+
'referencingProperties',
|
|
229
|
+
'propertyOrder',
|
|
230
|
+
'relations',
|
|
231
|
+
'concurrencyCheckKeys',
|
|
232
|
+
'checks',
|
|
233
|
+
].forEach(key => delete copy[key]);
|
|
234
|
+
// base entity without properties might not have path, but nothing to cache there
|
|
235
|
+
if (meta.path) {
|
|
236
|
+
meta.path = fs.relativePath(meta.path, this.config.get('baseDir'));
|
|
237
|
+
this.config.getMetadataCacheAdapter().set(this.getCacheKey(meta), copy, meta.path);
|
|
237
238
|
}
|
|
238
|
-
}
|
|
239
|
-
Reflect.deleteProperty(prop, 'targetMeta');
|
|
240
239
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
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);
|
|
240
|
+
getCacheKey(meta) {
|
|
241
|
+
/* v8 ignore next */
|
|
242
|
+
return meta.className + (meta.path ? extname(meta.path) : '');
|
|
254
243
|
}
|
|
255
|
-
}
|
|
256
|
-
getCacheKey(meta) {
|
|
257
|
-
/* v8 ignore next */
|
|
258
|
-
return meta.className + (meta.path ? extname(meta.path) : '');
|
|
259
|
-
}
|
|
260
244
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/reflection",
|
|
3
|
-
"version": "7.0.
|
|
3
|
+
"version": "7.0.11-dev.1",
|
|
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",
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"@mikro-orm/core": "^7.0.10"
|
|
54
54
|
},
|
|
55
55
|
"peerDependencies": {
|
|
56
|
-
"@mikro-orm/core": "7.0.
|
|
56
|
+
"@mikro-orm/core": "7.0.11-dev.1"
|
|
57
57
|
},
|
|
58
58
|
"engines": {
|
|
59
59
|
"node": ">= 22.17.0"
|