@mikro-orm/core 7.0.7-dev.2 → 7.0.7-dev.20

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.
@@ -47,11 +47,32 @@ export class EntityFactory {
47
47
  const exists = this.findEntity(data, meta2, options);
48
48
  let wrapped = exists && helper(exists);
49
49
  if (wrapped && !options.refresh) {
50
+ const wasInitialized = wrapped.isInitialized();
50
51
  wrapped.__processing = true;
51
52
  Utils.dropUndefinedProperties(data);
52
53
  this.mergeData(meta2, exists, data, options);
53
54
  wrapped.__processing = false;
55
+ wrapped.__initialized ||= !!options.initialized;
54
56
  if (wrapped.isInitialized()) {
57
+ if (!wasInitialized) {
58
+ if (meta.root.inheritanceType && !(exists instanceof meta2.class)) {
59
+ Object.setPrototypeOf(exists, meta2.prototype);
60
+ }
61
+ if (options.merge && wrapped.hasPrimaryKey()) {
62
+ this.unitOfWork.register(exists, data, { loaded: options.initialized });
63
+ // ensure all data keys are tracked as loaded for shouldRefresh checks,
64
+ // but don't overwrite __originalEntityData — mergeData already set it
65
+ // with DB values for non-user-modified keys, leaving user changes detectable
66
+ for (const key of Utils.keys(data)) {
67
+ if (meta2.properties[key]) {
68
+ wrapped.__loadedProperties.add(key);
69
+ }
70
+ }
71
+ }
72
+ if (this.#eventManager.hasListeners(EventType.onInit, meta2)) {
73
+ this.#eventManager.dispatchEvent(EventType.onInit, { entity: exists, meta: meta2, em: this.#em });
74
+ }
75
+ }
55
76
  return exists;
56
77
  }
57
78
  }
@@ -125,8 +146,18 @@ export class EntityFactory {
125
146
  diff[meta.versionProperty] = data[meta.versionProperty];
126
147
  }
127
148
  const diff2 = this.#comparator.diffEntities(meta.class, existsData, data, { includeInverseSides: true });
128
- // do not override values changed by user
129
- Utils.keys(diff).forEach(key => delete diff2[key]);
149
+ // do not override values changed by user; for uninitialized entities,
150
+ // the diff may include stale snapshot entries (value is undefined) — skip those
151
+ if (helper(entity).__initialized) {
152
+ Utils.keys(diff).forEach(key => delete diff2[key]);
153
+ }
154
+ else {
155
+ Utils.keys(diff).forEach(key => {
156
+ if (diff[key] !== undefined) {
157
+ delete diff2[key];
158
+ }
159
+ });
160
+ }
130
161
  Utils.keys(diff2)
131
162
  .filter(key => {
132
163
  // ignore null values if there is already present non-null value
@@ -289,7 +320,8 @@ export class EntityFactory {
289
320
  if (prop.kind === ReferenceKind.EMBEDDED && entity[prop.name]) {
290
321
  const items = prop.array ? entity[prop.name] : [entity[prop.name]];
291
322
  for (const item of items) {
292
- this.assignDefaultValues(item, prop.targetMeta, onCreateOnly);
323
+ // Embedded sub-properties need all defaults since the DB can't apply them within JSON columns.
324
+ this.assignDefaultValues(item, prop.targetMeta);
293
325
  }
294
326
  }
295
327
  }
@@ -223,10 +223,20 @@ export class EntityHelper {
223
223
  if (entity && (!prop.owner || helper(entity).__initialized)) {
224
224
  EntityHelper.propagateOneToOne(entity, owner, prop, prop2, value, old);
225
225
  }
226
- if (old && prop.orphanRemoval) {
227
- helper(old).__em?.getUnitOfWork().scheduleOrphanRemoval(old);
226
+ }
227
+ else if (old && old !== value) {
228
+ // Inverse already points to owner — propagation is not needed,
229
+ // but we still need to clean up old's inverse side.
230
+ helper(old).__pk ??= helper(old).getPrimaryKey();
231
+ if (old[prop2.name] != null) {
232
+ delete helper(old).__data[prop2.name];
233
+ old[prop2.name] = null;
228
234
  }
229
235
  }
236
+ if (old && old !== value && prop.orphanRemoval) {
237
+ helper(old).__pk ??= helper(old).getPrimaryKey();
238
+ helper(old).__em?.getUnitOfWork().scheduleOrphanRemoval(old);
239
+ }
230
240
  }
231
241
  }
232
242
  }
@@ -399,10 +399,17 @@ export class EntityLoader {
399
399
  for (const child of children) {
400
400
  childrenMap.add(getKey(child));
401
401
  }
402
+ const isInverseOneToOne = prop.kind === ReferenceKind.ONE_TO_ONE && !prop.owner;
402
403
  for (const entity of entities) {
403
- const ref = entity[prop.name] ?? {};
404
- const key = helper(ref) ? getKey(ref) : undefined;
405
- if (key && childrenMap.has(key) && !itemsMap.has(key)) {
404
+ const ref = entity[prop.name];
405
+ if (ref == null) {
406
+ continue;
407
+ }
408
+ const refKey = getKey(ref);
409
+ // For 1:1 inverse, `children` contains parent entities, so `childrenMap`
410
+ // has parent PKs — match against the entity's own PK, not the referenced entity's PK.
411
+ const childKey = isInverseOneToOne ? getKey(entity) : refKey;
412
+ if (childrenMap.has(childKey) && !itemsMap.has(refKey)) {
406
413
  entity[prop.name] = nullVal;
407
414
  helper(entity).__originalEntityData[prop.name] = null;
408
415
  }
@@ -706,6 +706,9 @@ type MaybeScalarRef<Value, Options> = Options extends {
706
706
  } ? Value : Options extends {
707
707
  ref: true;
708
708
  } ? ScalarReference<Value> : Value;
709
+ type IsAllPropsOpt<T> = [Exclude<keyof T, symbol>] extends [never] ? false : {
710
+ [K in Exclude<keyof T, symbol>]-?: T[K] extends Opt ? never : K;
711
+ }[Exclude<keyof T, symbol>] extends never ? true : false;
709
712
  type MaybeOpt<Value, Options> = Options extends {
710
713
  mapToPk: true;
711
714
  } ? Value extends Opt<infer OriginalValue> ? OriginalValue : Value : Options extends {
@@ -722,7 +725,9 @@ type MaybeOpt<Value, Options> = Options extends {
722
725
  version: true;
723
726
  } | {
724
727
  formula: string | ((...args: any[]) => any);
725
- } ? Opt<NonNullable<Value>> | Extract<Value, null | undefined> : Value;
728
+ } ? Opt<NonNullable<Value>> | Extract<Value, null | undefined> : Options extends {
729
+ kind: 'embedded';
730
+ } ? IsAllPropsOpt<Value> extends true ? Opt<NonNullable<Value>> | Extract<Value, null | undefined> : Value : Value;
726
731
  type MaybeHidden<Value, Options> = Options extends {
727
732
  hidden: true;
728
733
  } ? Hidden<NonNullable<Value>> | Extract<Value, null | undefined> : Value;
@@ -428,13 +428,13 @@ export class MetadataDiscovery {
428
428
  prop.fieldNames = this.initManyToManyFieldName(prop, prop.name);
429
429
  }
430
430
  }
431
- initManyToOneFieldName(prop, name, tableName) {
431
+ initManyToOneFieldName(prop, name) {
432
432
  const meta2 = prop.targetMeta;
433
433
  const ret = [];
434
434
  for (const primaryKey of meta2.primaryKeys) {
435
435
  this.initFieldName(meta2.properties[primaryKey]);
436
436
  for (const fieldName of meta2.properties[primaryKey].fieldNames) {
437
- ret.push(this.#namingStrategy.joinKeyColumnName(name, fieldName, meta2.compositePK, tableName));
437
+ ret.push(this.#namingStrategy.joinKeyColumnName(name, fieldName, meta2.compositePK));
438
438
  }
439
439
  }
440
440
  return ret;
@@ -487,11 +487,9 @@ export class MetadataDiscovery {
487
487
  prop.joinColumns ??= prop.referencedColumnNames.map(referencedColumnName => this.#namingStrategy.joinKeyColumnName(prop.discriminator, referencedColumnName, prop.referencedColumnNames.length > 1));
488
488
  }
489
489
  else {
490
- const ownerTableName = this.isExplicitTableName(meta.root) ? meta.root.tableName : undefined;
491
- prop.joinColumns ??= prop.referencedColumnNames.map(referencedColumnName => this.#namingStrategy.joinKeyColumnName(meta.root.className, referencedColumnName, meta.compositePK, ownerTableName));
490
+ prop.joinColumns ??= prop.referencedColumnNames.map(referencedColumnName => this.#namingStrategy.joinKeyColumnName(meta.root.className, referencedColumnName, meta.compositePK));
492
491
  }
493
- const inverseTableName = this.isExplicitTableName(meta2.root) ? meta2.root.tableName : undefined;
494
- prop.inverseJoinColumns ??= this.initManyToOneFieldName(prop, meta2.root.className, inverseTableName);
492
+ prop.inverseJoinColumns ??= this.initManyToOneFieldName(prop, meta2.root.className);
495
493
  }
496
494
  isExplicitTableName(meta) {
497
495
  return meta.tableName !== this.#namingStrategy.classToTableName(meta.className);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
- "version": "7.0.7-dev.2",
3
+ "version": "7.0.7-dev.20",
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",
package/typings.d.ts CHANGED
@@ -1300,6 +1300,10 @@ export interface EntitySchemaWithMeta<TName extends string = string, TTableName
1300
1300
  readonly tableName: TTableName;
1301
1301
  /** @internal Direct entity type access - avoids expensive pattern matching */
1302
1302
  readonly '~entity': TEntity;
1303
+ /** @internal */
1304
+ readonly class: TClass & {
1305
+ '~entityName'?: TName;
1306
+ };
1303
1307
  }
1304
1308
  /**
1305
1309
  * Extracts the entity type from an `EntitySchema`, `EntitySchemaWithMeta`, or entity class.
@@ -101,6 +101,8 @@ export class ChangeSetPersister {
101
101
  !prop.generated &&
102
102
  !prop.embedded &&
103
103
  ![ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(prop.kind) &&
104
+ !(prop.kind === ReferenceKind.EMBEDDED &&
105
+ prop.targetMeta?.props.every(p => p.formula || p.persist === false || p.primary)) &&
104
106
  prop.name !== wrapped.__meta.root.discriminatorColumn &&
105
107
  prop.type !== 'ObjectId' &&
106
108
  prop.persist !== false &&
package/utils/Utils.js CHANGED
@@ -132,7 +132,7 @@ export function parseJsonSafe(value) {
132
132
  /** Collection of general-purpose utility methods used throughout the ORM. */
133
133
  export class Utils {
134
134
  static PK_SEPARATOR = '~~~';
135
- static #ORM_VERSION = '7.0.7-dev.2';
135
+ static #ORM_VERSION = '7.0.7-dev.20';
136
136
  /**
137
137
  * Checks if the argument is instance of `Object`. Returns false for arrays.
138
138
  */