@mikro-orm/reflection 7.0.0-dev.77 → 7.0.0-dev.78

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.
@@ -3,6 +3,7 @@ import { type EntityMetadata, MetadataProvider } from '@mikro-orm/core';
3
3
  export declare class TsMorphMetadataProvider extends MetadataProvider {
4
4
  private project;
5
5
  private sources;
6
+ static useCache(): boolean;
6
7
  useCache(): boolean;
7
8
  loadEntityMetadata(meta: EntityMetadata): void;
8
9
  getExistingSourceFile(path: string, ext?: string, validate?: boolean): SourceFile;
@@ -16,4 +17,7 @@ export declare class TsMorphMetadataProvider extends MetadataProvider {
16
17
  private processWrapper;
17
18
  private initProject;
18
19
  private initSourceFiles;
20
+ loadFromCache(meta: EntityMetadata, cache: EntityMetadata): void;
21
+ saveToCache(meta: EntityMetadata): void;
22
+ getCacheKey(meta: Pick<EntityMetadata, 'className' | 'path'>): string;
19
23
  }
@@ -1,10 +1,14 @@
1
+ import { extname } from 'node:path';
1
2
  import { ComputedPropertyName, ModuleKind, NoSubstitutionTemplateLiteral, Project, StringLiteral } from 'ts-morph';
2
- import { MetadataError, MetadataProvider, MetadataStorage, ReferenceKind, Utils, } from '@mikro-orm/core';
3
+ import { MetadataError, MetadataProvider, MetadataStorage, RawQueryFragment, ReferenceKind, Type, Utils, } from '@mikro-orm/core';
3
4
  export class TsMorphMetadataProvider extends MetadataProvider {
4
5
  project;
5
6
  sources;
7
+ static useCache() {
8
+ return true;
9
+ }
6
10
  useCache() {
7
- return this.config.get('metadataCache').enabled ?? true;
11
+ return this.config.get('metadataCache').enabled ?? TsMorphMetadataProvider.useCache();
8
12
  }
9
13
  loadEntityMetadata(meta) {
10
14
  if (!meta.path) {
@@ -24,7 +28,7 @@ export class TsMorphMetadataProvider extends MetadataProvider {
24
28
  for (const prop of Object.values(meta.properties)) {
25
29
  const type = this.extractType(prop);
26
30
  this.initPropertyType(meta, prop);
27
- prop.type = type || prop.type;
31
+ prop.type = type ?? prop.type;
28
32
  }
29
33
  }
30
34
  extractType(prop) {
@@ -174,7 +178,7 @@ export class TsMorphMetadataProvider extends MetadataProvider {
174
178
  skipAddingFilesFromTsConfig: true,
175
179
  compilerOptions: {
176
180
  strictNullChecks: true,
177
- module: ModuleKind.Node16,
181
+ module: ModuleKind.Node20,
178
182
  },
179
183
  });
180
184
  }
@@ -183,15 +187,13 @@ export class TsMorphMetadataProvider extends MetadataProvider {
183
187
  this.project = new Project({
184
188
  compilerOptions: {
185
189
  strictNullChecks: true,
186
- module: ModuleKind.Node16,
190
+ module: ModuleKind.Node20,
187
191
  },
188
192
  });
189
193
  }
190
194
  }
191
195
  initSourceFiles() {
192
- if (!this.project) {
193
- this.initProject();
194
- }
196
+ this.initProject();
195
197
  this.sources = [];
196
198
  // All entity files are first required during the discovery, before we reach here, so it is safe to get the parts from the global
197
199
  // metadata storage. We know the path thanks to the decorators being executed. In case we are running the TS code, the extension
@@ -208,4 +210,47 @@ export class TsMorphMetadataProvider extends MetadataProvider {
208
210
  }
209
211
  }
210
212
  }
213
+ loadFromCache(meta, cache) {
214
+ Object.values(cache.properties).forEach(prop => {
215
+ const metaProp = meta.properties[prop.name];
216
+ /* v8 ignore next 3 */
217
+ if (metaProp?.enum && Array.isArray(metaProp.items)) {
218
+ delete prop.items;
219
+ }
220
+ });
221
+ Utils.mergeConfig(meta, cache);
222
+ }
223
+ saveToCache(meta) {
224
+ if (!this.useCache()) {
225
+ return;
226
+ }
227
+ Reflect.deleteProperty(meta, 'root'); // to allow caching (as root can contain cycles)
228
+ const copy = Utils.copy(meta, false);
229
+ for (const prop of copy.props) {
230
+ if (Type.isMappedType(prop.type)) {
231
+ Reflect.deleteProperty(prop, 'type');
232
+ Reflect.deleteProperty(prop, 'customType');
233
+ }
234
+ if (prop.default) {
235
+ const raw = RawQueryFragment.getKnownFragment(prop.default);
236
+ if (raw) {
237
+ prop.defaultRaw ??= this.config.getPlatform().formatQuery(raw.sql, raw.params);
238
+ Reflect.deleteProperty(prop, 'default');
239
+ }
240
+ }
241
+ Reflect.deleteProperty(prop, 'targetMeta');
242
+ }
243
+ [
244
+ 'prototype', 'props', 'referencingProperties', 'propertyOrder', 'relations',
245
+ 'concurrencyCheckKeys', 'checks',
246
+ ].forEach(key => delete copy[key]);
247
+ // base entity without properties might not have path, but nothing to cache there
248
+ if (meta.path) {
249
+ this.config.getMetadataCacheAdapter().set(this.getCacheKey(meta), copy, meta.path);
250
+ }
251
+ }
252
+ getCacheKey(meta) {
253
+ /* v8 ignore next */
254
+ return meta.className + (meta.path ? extname(meta.path) : '');
255
+ }
211
256
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mikro-orm/reflection",
3
3
  "type": "module",
4
- "version": "7.0.0-dev.77",
4
+ "version": "7.0.0-dev.78",
5
5
  "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.",
6
6
  "exports": {
7
7
  "./package.json": "./package.json",
@@ -56,6 +56,6 @@
56
56
  "@mikro-orm/core": "^6.6.1"
57
57
  },
58
58
  "peerDependencies": {
59
- "@mikro-orm/core": "7.0.0-dev.77"
59
+ "@mikro-orm/core": "7.0.0-dev.78"
60
60
  }
61
61
  }