@mikro-orm/core 7.0.0-dev.114 → 7.0.0-dev.115

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 (54) hide show
  1. package/EntityManager.d.ts +8 -8
  2. package/EntityManager.js +40 -60
  3. package/MikroORM.d.ts +1 -1
  4. package/MikroORM.js +2 -3
  5. package/drivers/DatabaseDriver.d.ts +11 -11
  6. package/drivers/DatabaseDriver.js +7 -8
  7. package/drivers/IDatabaseDriver.d.ts +10 -10
  8. package/entity/Collection.js +5 -5
  9. package/entity/EntityAssigner.js +9 -9
  10. package/entity/EntityFactory.js +14 -17
  11. package/entity/EntityHelper.d.ts +2 -2
  12. package/entity/EntityHelper.js +2 -2
  13. package/entity/EntityLoader.d.ts +3 -3
  14. package/entity/EntityLoader.js +17 -16
  15. package/entity/WrappedEntity.js +1 -1
  16. package/entity/defineEntity.d.ts +11 -11
  17. package/errors.d.ts +8 -8
  18. package/errors.js +14 -13
  19. package/hydration/ObjectHydrator.js +23 -16
  20. package/metadata/EntitySchema.d.ts +5 -5
  21. package/metadata/EntitySchema.js +23 -21
  22. package/metadata/MetadataDiscovery.d.ts +2 -3
  23. package/metadata/MetadataDiscovery.js +117 -90
  24. package/metadata/MetadataProvider.js +2 -0
  25. package/metadata/MetadataStorage.d.ts +13 -6
  26. package/metadata/MetadataStorage.js +64 -19
  27. package/metadata/MetadataValidator.d.ts +2 -2
  28. package/metadata/MetadataValidator.js +22 -28
  29. package/metadata/types.d.ts +3 -3
  30. package/package.json +1 -1
  31. package/serialization/EntitySerializer.js +2 -2
  32. package/serialization/EntityTransformer.js +6 -6
  33. package/serialization/SerializationContext.d.ts +6 -6
  34. package/typings.d.ts +16 -14
  35. package/typings.js +15 -10
  36. package/unit-of-work/ChangeSet.d.ts +2 -3
  37. package/unit-of-work/ChangeSet.js +2 -3
  38. package/unit-of-work/ChangeSetComputer.js +3 -3
  39. package/unit-of-work/ChangeSetPersister.js +14 -14
  40. package/unit-of-work/CommitOrderCalculator.d.ts +12 -10
  41. package/unit-of-work/CommitOrderCalculator.js +13 -13
  42. package/unit-of-work/UnitOfWork.d.ts +3 -3
  43. package/unit-of-work/UnitOfWork.js +46 -45
  44. package/utils/AbstractSchemaGenerator.js +7 -7
  45. package/utils/Configuration.d.ts +0 -5
  46. package/utils/DataloaderUtils.js +13 -11
  47. package/utils/EntityComparator.d.ts +6 -6
  48. package/utils/EntityComparator.js +22 -24
  49. package/utils/QueryHelper.d.ts +5 -5
  50. package/utils/QueryHelper.js +7 -7
  51. package/utils/TransactionManager.js +1 -1
  52. package/utils/Utils.d.ts +1 -1
  53. package/utils/Utils.js +1 -2
  54. package/utils/env-vars.js +0 -1
@@ -20,19 +20,19 @@ export declare class EntityComparator {
20
20
  /**
21
21
  * Computes difference between two entities.
22
22
  */
23
- diffEntities<T>(entityName: string, a: EntityData<T>, b: EntityData<T>, options?: {
23
+ diffEntities<T extends object>(entityName: EntityName<T>, a: EntityData<T>, b: EntityData<T>, options?: {
24
24
  includeInverseSides?: boolean;
25
25
  }): EntityData<T>;
26
- matching<T>(entityName: string, a: EntityData<T>, b: EntityData<T>): boolean;
26
+ matching<T extends object>(entityName: EntityName<T>, a: EntityData<T>, b: EntityData<T>): boolean;
27
27
  /**
28
28
  * Removes ORM specific code from entities and prepares it for serializing. Used before change set computation.
29
29
  * References will be mapped to primary keys, collections to arrays of primary keys.
30
30
  */
31
- prepareEntity<T>(entity: T): EntityData<T>;
31
+ prepareEntity<T extends object>(entity: T): EntityData<T>;
32
32
  /**
33
33
  * Maps database columns to properties.
34
34
  */
35
- mapResult<T>(entityName: string, result: EntityDictionary<T>): EntityData<T>;
35
+ mapResult<T>(meta: EntityMetadata<T>, result: EntityDictionary<T>): EntityData<T>;
36
36
  /**
37
37
  * @internal Highly performance-sensitive method.
38
38
  */
@@ -64,7 +64,7 @@ export declare class EntityComparator {
64
64
  /**
65
65
  * @internal Highly performance-sensitive method.
66
66
  */
67
- getResultMapper<T>(entityName: string): ResultMapper<T>;
67
+ getResultMapper<T>(meta: EntityMetadata<T>): ResultMapper<T>;
68
68
  private getPropertyCondition;
69
69
  private getEmbeddedArrayPropertySnapshot;
70
70
  /**
@@ -78,7 +78,7 @@ export declare class EntityComparator {
78
78
  /**
79
79
  * @internal Highly performance-sensitive method.
80
80
  */
81
- getEntityComparator<T extends object>(entityName: string): Comparator<T>;
81
+ getEntityComparator<T extends object>(entityName: EntityName<T>): Comparator<T>;
82
82
  private getGenericComparator;
83
83
  private getPropertyComparator;
84
84
  private wrap;
@@ -33,21 +33,21 @@ export class EntityComparator {
33
33
  * References will be mapped to primary keys, collections to arrays of primary keys.
34
34
  */
35
35
  prepareEntity(entity) {
36
- const generator = this.getSnapshotGenerator(entity.constructor.name);
36
+ const generator = this.getSnapshotGenerator(entity.constructor);
37
37
  return Utils.callCompiledFunction(generator, entity);
38
38
  }
39
39
  /**
40
40
  * Maps database columns to properties.
41
41
  */
42
- mapResult(entityName, result) {
43
- const mapper = this.getResultMapper(entityName);
42
+ mapResult(meta, result) {
43
+ const mapper = this.getResultMapper(meta);
44
44
  return Utils.callCompiledFunction(mapper, result);
45
45
  }
46
46
  /**
47
47
  * @internal Highly performance-sensitive method.
48
48
  */
49
49
  getPkGetter(meta) {
50
- const exists = this.pkGetters.get(meta.className);
50
+ const exists = this.pkGetters.get(meta);
51
51
  /* v8 ignore next */
52
52
  if (exists) {
53
53
  return exists;
@@ -90,14 +90,14 @@ export class EntityComparator {
90
90
  const code = `// compiled pk serializer for entity ${meta.className}\n`
91
91
  + `return function(entity) {\n${lines.join('\n')}\n}`;
92
92
  const pkSerializer = Utils.createFunction(context, code);
93
- this.pkGetters.set(meta.className, pkSerializer);
93
+ this.pkGetters.set(meta, pkSerializer);
94
94
  return pkSerializer;
95
95
  }
96
96
  /**
97
97
  * @internal Highly performance-sensitive method.
98
98
  */
99
99
  getPkGetterConverted(meta) {
100
- const exists = this.pkGettersConverted.get(meta.className);
100
+ const exists = this.pkGettersConverted.get(meta);
101
101
  /* v8 ignore next */
102
102
  if (exists) {
103
103
  return exists;
@@ -140,14 +140,14 @@ export class EntityComparator {
140
140
  const code = `// compiled pk getter (with converted custom types) for entity ${meta.className}\n`
141
141
  + `return function(entity) {\n${lines.join('\n')}\n}`;
142
142
  const pkSerializer = Utils.createFunction(context, code);
143
- this.pkGettersConverted.set(meta.className, pkSerializer);
143
+ this.pkGettersConverted.set(meta, pkSerializer);
144
144
  return pkSerializer;
145
145
  }
146
146
  /**
147
147
  * @internal Highly performance-sensitive method.
148
148
  */
149
149
  getPkSerializer(meta) {
150
- const exists = this.pkSerializers.get(meta.className);
150
+ const exists = this.pkSerializers.get(meta);
151
151
  /* v8 ignore next */
152
152
  if (exists) {
153
153
  return exists;
@@ -192,19 +192,18 @@ export class EntityComparator {
192
192
  const code = `// compiled pk serializer for entity ${meta.className}\n`
193
193
  + `return function(entity) {\n${lines.join('\n')}\n}`;
194
194
  const pkSerializer = Utils.createFunction(context, code);
195
- this.pkSerializers.set(meta.className, pkSerializer);
195
+ this.pkSerializers.set(meta, pkSerializer);
196
196
  return pkSerializer;
197
197
  }
198
198
  /**
199
199
  * @internal Highly performance-sensitive method.
200
200
  */
201
201
  getSnapshotGenerator(entityName) {
202
- entityName = Utils.className(entityName);
203
- const exists = this.snapshotGenerators.get(entityName);
202
+ const meta = this.metadata.find(entityName);
203
+ const exists = this.snapshotGenerators.get(meta);
204
204
  if (exists) {
205
205
  return exists;
206
206
  }
207
- const meta = this.metadata.find(entityName);
208
207
  const lines = [];
209
208
  const context = new Map();
210
209
  context.set('clone', clone);
@@ -222,7 +221,7 @@ export class EntityComparator {
222
221
  .forEach(prop => lines.push(this.getPropertySnapshot(meta, prop, context, this.wrap(prop.name), this.wrap(prop.name), [prop.name])));
223
222
  const code = `return function(entity) {\n const ret = {};\n${lines.join('\n')}\n return ret;\n}`;
224
223
  const snapshotGenerator = Utils.createFunction(context, code);
225
- this.snapshotGenerators.set(entityName, snapshotGenerator);
224
+ this.snapshotGenerators.set(meta, snapshotGenerator);
226
225
  return snapshotGenerator;
227
226
  }
228
227
  /**
@@ -272,12 +271,11 @@ export class EntityComparator {
272
271
  /**
273
272
  * @internal Highly performance-sensitive method.
274
273
  */
275
- getResultMapper(entityName) {
276
- const exists = this.mappers.get(entityName);
274
+ getResultMapper(meta) {
275
+ const exists = this.mappers.get(meta);
277
276
  if (exists) {
278
277
  return exists;
279
278
  }
280
- const meta = this.metadata.get(entityName);
281
279
  const lines = [];
282
280
  const context = new Map();
283
281
  const tz = this.platform.getTimezone();
@@ -336,9 +334,9 @@ export class EntityComparator {
336
334
  context.set(`mapEmbeddedResult_${idx}`, (data) => {
337
335
  const item = parseJsonSafe(data);
338
336
  if (Array.isArray(item)) {
339
- return item.map(row => row == null ? row : this.getResultMapper(prop.type)(row));
337
+ return item.map(row => row == null ? row : this.getResultMapper(prop.targetMeta)(row));
340
338
  }
341
- return item == null ? item : this.getResultMapper(prop.type)(item);
339
+ return item == null ? item : this.getResultMapper(prop.targetMeta)(item);
342
340
  });
343
341
  lines.push(`${padding} if (typeof ${this.propName(prop.fieldNames[0])} !== 'undefined') {`);
344
342
  lines.push(`${padding} ret${this.wrap(prop.name)} = ${this.propName(prop.fieldNames[0])} == null ? ${this.propName(prop.fieldNames[0])} : mapEmbeddedResult_${idx}(${this.propName(prop.fieldNames[0])});`);
@@ -371,7 +369,7 @@ export class EntityComparator {
371
369
  const code = `// compiled mapper for entity ${meta.className}\n`
372
370
  + `return function(result) {\n const ret = {};\n${lines.join('\n')}\n return ret;\n}`;
373
371
  const resultMapper = Utils.createFunction(context, code);
374
- this.mappers.set(entityName, resultMapper);
372
+ this.mappers.set(meta, resultMapper);
375
373
  return resultMapper;
376
374
  }
377
375
  getPropertyCondition(path) {
@@ -544,11 +542,11 @@ export class EntityComparator {
544
542
  * @internal Highly performance-sensitive method.
545
543
  */
546
544
  getEntityComparator(entityName) {
547
- const exists = this.comparators.get(entityName);
545
+ const meta = this.metadata.find(entityName);
546
+ const exists = this.comparators.get(meta);
548
547
  if (exists) {
549
548
  return exists;
550
549
  }
551
- const meta = this.metadata.find(entityName);
552
550
  const lines = [];
553
551
  const context = new Map();
554
552
  context.set('compareArrays', compareArrays);
@@ -570,7 +568,7 @@ export class EntityComparator {
570
568
  const code = `// compiled comparator for entity ${meta.className}\n`
571
569
  + `return function(last, current, options) {\n const diff = {};\n${lines.join('\n')}\n return diff;\n}`;
572
570
  const comparator = Utils.createFunction(context, code);
573
- this.comparators.set(entityName, comparator);
571
+ this.comparators.set(meta, comparator);
574
572
  return comparator;
575
573
  }
576
574
  getGenericComparator(prop, cond) {
@@ -586,12 +584,12 @@ export class EntityComparator {
586
584
  getPropertyComparator(prop, context) {
587
585
  let type = prop.type.toLowerCase();
588
586
  if (prop.kind !== ReferenceKind.SCALAR && prop.kind !== ReferenceKind.EMBEDDED) {
589
- const meta2 = this.metadata.find(prop.type);
587
+ const meta2 = prop.targetMeta;
590
588
  if (meta2.primaryKeys.length > 1) {
591
589
  type = 'array';
592
590
  }
593
591
  else {
594
- type = meta2.properties[meta2.primaryKeys[0]].type.toLowerCase();
592
+ type = meta2.getPrimaryProp().type.toLowerCase();
595
593
  }
596
594
  }
597
595
  if (prop.customType) {
@@ -1,4 +1,4 @@
1
- import type { Dictionary, EntityMetadata, EntityProperty, FilterDef, FilterQuery } from '../typings.js';
1
+ import type { Dictionary, EntityMetadata, EntityName, EntityProperty, FilterDef, FilterQuery } from '../typings.js';
2
2
  import type { Platform } from '../platforms/Platform.js';
3
3
  import type { MetadataStorage } from '../metadata/MetadataStorage.js';
4
4
  import type { FilterOptions } from '../drivers/IDatabaseDriver.js';
@@ -14,9 +14,9 @@ export declare class QueryHelper {
14
14
  static liftGroupOperators<T extends object>(where: Dictionary, meta: EntityMetadata<T>, metadata: MetadataStorage, key?: string): string | undefined;
15
15
  static inlinePrimaryKeyObjects<T extends object>(where: Dictionary, meta: EntityMetadata<T>, metadata: MetadataStorage, key?: string): boolean;
16
16
  static processWhere<T extends object>(options: ProcessWhereOptions<T>): FilterQuery<T>;
17
- static getActiveFilters(entityName: string, options: FilterOptions | undefined, filters: Dictionary<FilterDef>): FilterDef[];
17
+ static getActiveFilters<T>(meta: EntityMetadata<T>, options: FilterOptions | undefined, filters: Dictionary<FilterDef>): FilterDef[];
18
18
  static mergePropertyFilters(propFilters: FilterOptions | undefined, options: FilterOptions | undefined): FilterOptions | undefined;
19
- static isFilterActive(entityName: string, filterName: string, filter: FilterDef, options: Dictionary<boolean | Dictionary>): boolean;
19
+ static isFilterActive<T>(meta: EntityMetadata<T>, filterName: string, filter: FilterDef, options: Dictionary<boolean | Dictionary>): boolean;
20
20
  static processCustomType<T extends object>(prop: EntityProperty<T>, cond: FilterQuery<T>, platform: Platform, key?: string, fromQuery?: boolean): FilterQuery<T>;
21
21
  private static isSupportedOperator;
22
22
  private static processJsonCondition;
@@ -25,11 +25,11 @@ export declare class QueryHelper {
25
25
  }
26
26
  interface ProcessWhereOptions<T> {
27
27
  where: FilterQuery<T>;
28
- entityName: string;
28
+ entityName: EntityName<T>;
29
29
  metadata: MetadataStorage;
30
30
  platform: Platform;
31
31
  aliased?: boolean;
32
- aliasMap?: Dictionary<string>;
32
+ aliasMap?: Dictionary<EntityName>;
33
33
  convertCustomTypes?: boolean;
34
34
  root?: boolean;
35
35
  type?: 'where' | 'orderBy';
@@ -98,7 +98,7 @@ export class QueryHelper {
98
98
  }));
99
99
  }
100
100
  Object.keys(where).forEach(k => {
101
- const meta2 = metadata.find(meta.properties[k]?.type) || meta;
101
+ const meta2 = metadata.find(meta.properties[k]?.targetMeta?.class) || meta;
102
102
  if (this.inlinePrimaryKeyObjects(where[k], meta2, metadata, k)) {
103
103
  where[k] = Utils.getPrimaryKeyValues(where[k], meta2, true);
104
104
  }
@@ -126,7 +126,7 @@ export class QueryHelper {
126
126
  where = { [Utils.getPrimaryKeyHash(meta.primaryKeys)]: where };
127
127
  }
128
128
  if (Array.isArray(where) && root) {
129
- const rootPrimaryKey = meta ? Utils.getPrimaryKeyHash(meta.primaryKeys) : entityName;
129
+ const rootPrimaryKey = meta ? Utils.getPrimaryKeyHash(meta.primaryKeys) : Utils.className(entityName);
130
130
  let cond = { [rootPrimaryKey]: { $in: where } };
131
131
  // @ts-ignore
132
132
  // detect tuple comparison, use `$or` in case the number of constituents don't match
@@ -175,7 +175,7 @@ export class QueryHelper {
175
175
  o[key] = QueryHelper.processWhere({
176
176
  ...options,
177
177
  where: value,
178
- entityName: prop?.type ?? entityName,
178
+ entityName: prop?.targetMeta?.class ?? entityName,
179
179
  root: false,
180
180
  });
181
181
  }
@@ -185,7 +185,7 @@ export class QueryHelper {
185
185
  return o;
186
186
  }, {});
187
187
  }
188
- static getActiveFilters(entityName, options, filters) {
188
+ static getActiveFilters(meta, options, filters) {
189
189
  if (options === false) {
190
190
  return [];
191
191
  }
@@ -197,7 +197,7 @@ export class QueryHelper {
197
197
  Object.keys(options).forEach(filter => opts[filter] = options[filter]);
198
198
  }
199
199
  return Object.keys(filters)
200
- .filter(f => QueryHelper.isFilterActive(entityName, f, filters[f], opts))
200
+ .filter(f => QueryHelper.isFilterActive(meta, f, filters[f], opts))
201
201
  .map(f => {
202
202
  filters[f].name = f;
203
203
  return filters[f];
@@ -221,8 +221,8 @@ export class QueryHelper {
221
221
  }
222
222
  return Utils.mergeConfig({}, propFilters, options);
223
223
  }
224
- static isFilterActive(entityName, filterName, filter, options) {
225
- if (filter.entity && !filter.entity.includes(entityName)) {
224
+ static isFilterActive(meta, filterName, filter, options) {
225
+ if (filter.entity && !filter.entity.includes(meta.className)) {
226
226
  return false;
227
227
  }
228
228
  if (options[filterName] === false) {
@@ -161,7 +161,7 @@ export class TransactionManager {
161
161
  const wrapped = helper(entity);
162
162
  const meta = wrapped.__meta;
163
163
  // eslint-disable-next-line dot-notation
164
- const parentEntity = parentUoW.getById(meta.className, wrapped.getPrimaryKey(), parent['_schema'], true);
164
+ const parentEntity = parentUoW.getById(meta.class, wrapped.getPrimaryKey(), parent['_schema'], true);
165
165
  if (parentEntity && parentEntity !== entity) {
166
166
  const parentWrapped = helper(parentEntity);
167
167
  parentWrapped.__data = wrapped.__data;
package/utils/Utils.d.ts CHANGED
@@ -110,7 +110,7 @@ export declare class Utils {
110
110
  /**
111
111
  * Gets string name of given class.
112
112
  */
113
- static className<T>(classOrName: EntityName<T>): string;
113
+ static className<T>(classOrName: string | EntityName<T>): string;
114
114
  static extractChildElements(items: string[], prefix: string, allSymbol?: string): string[];
115
115
  /**
116
116
  * Tries to detect TypeScript support.
package/utils/Utils.js CHANGED
@@ -35,7 +35,6 @@ export function compareObjects(a, b) {
35
35
  }
36
36
  /* v8 ignore next */
37
37
  if ((typeof a === 'function' && typeof b === 'function') ||
38
- (typeof a === 'object' && a.client && ['Ref', 'Raw'].includes(a.constructor.name) && typeof b === 'object' && b.client && ['Ref', 'Raw'].includes(b.constructor.name)) || // knex qb
39
38
  (a instanceof RegExp && b instanceof RegExp) ||
40
39
  (a instanceof String && b instanceof String) ||
41
40
  (a instanceof Number && b instanceof Number)) {
@@ -124,7 +123,7 @@ export function parseJsonSafe(value) {
124
123
  }
125
124
  export class Utils {
126
125
  static PK_SEPARATOR = '~~~';
127
- static #ORM_VERSION = '7.0.0-dev.114';
126
+ static #ORM_VERSION = '7.0.0-dev.115';
128
127
  /**
129
128
  * Checks if the argument is instance of `Object`. Returns false for arrays.
130
129
  */
package/utils/env-vars.js CHANGED
@@ -60,7 +60,6 @@ export function loadEnvironmentVars() {
60
60
  read1('warnWhenNoEntities', bool);
61
61
  read1('checkDuplicateTableNames', bool);
62
62
  read1('checkDuplicateFieldNames', bool);
63
- read1('checkDuplicateEntities', bool);
64
63
  read1('checkNonPersistentCompositeProps', bool);
65
64
  read1('inferDefaultValues', bool);
66
65
  read1('tsConfigPath');