@mikro-orm/core 7.0.7-dev.9 → 7.0.8-dev.0

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
@@ -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
  }
@@ -1,6 +1,6 @@
1
1
  import type { EntityManager } from '../EntityManager.js';
2
2
  import type { ColumnType, PropertyOptions, ReferenceOptions, EnumOptions, EmbeddedOptions, ManyToOneOptions, OneToManyOptions, OneToOneOptions, ManyToManyOptions, IndexColumnOptions } from '../metadata/types.js';
3
- import type { AnyString, GeneratedColumnCallback, Constructor, CheckCallback, FilterQuery, EntityName, Dictionary, EntityMetadata, PrimaryKeyProp, EntityRepositoryType, Hidden, Opt, Primary, EntityClass, EntitySchemaWithMeta, InferEntity, MaybeReturnType, Ref, IndexCallback, FormulaCallback, EntityCtor, IsNever, IWrappedEntity, DefineConfig, Config } from '../typings.js';
3
+ import type { AnyString, GeneratedColumnCallback, Constructor, CheckCallback, FilterQuery, EntityName, Dictionary, EntityMetadata, PrimaryKeyProp, EntityRepositoryType, Hidden, Opt, Primary, EntityClass, EntitySchemaWithMeta, InferEntity, MaybeReturnType, Ref, IndexCallback, FormulaCallback, EntityCtor, IsNever, IWrappedEntity, DefineConfig, Config, MaybePromise } from '../typings.js';
4
4
  import type { Raw } from '../utils/RawQueryFragment.js';
5
5
  import type { ScalarReference } from './Reference.js';
6
6
  import type { SerializeOptions } from '../serialization/EntitySerializer.js';
@@ -9,7 +9,7 @@ import type { EventSubscriber } from '../events/EventSubscriber.js';
9
9
  import type { IType, Type } from '../types/Type.js';
10
10
  import { types } from '../types/index.js';
11
11
  import type { Collection } from './Collection.js';
12
- import type { FilterOptions } from '../drivers/IDatabaseDriver.js';
12
+ import type { FilterOptions, FindOptions, FindOneOptions } from '../drivers/IDatabaseDriver.js';
13
13
  /** Union of all option keys supported across all property definition types (scalar, enum, embedded, relations). */
14
14
  export type UniversalPropertyKeys = keyof PropertyOptions<any> | keyof EnumOptions<any> | keyof EmbeddedOptions<any, any> | keyof ReferenceOptions<any, any> | keyof ManyToOneOptions<any, any> | keyof OneToManyOptions<any, any> | keyof OneToOneOptions<any, any> | keyof ManyToManyOptions<any, any>;
15
15
  type BuilderExtraKeys = '~options' | '~type' | '$type' | 'strictNullable';
@@ -556,16 +556,24 @@ export type PropertyBuilders = {
556
556
  /** Own keys + base entity keys (when TBase is not `never`). Guards against `keyof never = string | number | symbol`. */
557
557
  type AllKeys<TProperties, TBase> = keyof TProperties | (IsNever<TBase> extends true ? never : keyof TBase);
558
558
  /** Metadata descriptor for `defineEntity()`, combining entity options with property definitions. */
559
- export interface EntityMetadataWithProperties<TName extends string, TTableName extends string, TProperties extends Record<string, any>, TPK extends (keyof TProperties)[] | undefined = undefined, TBase = never, TRepository = never, TForceObject extends boolean = false> extends Omit<Partial<EntityMetadata<InferEntityFromProperties<TProperties, TPK, TBase, TRepository>>>, 'properties' | 'extends' | 'primaryKeys' | 'hooks' | 'discriminatorColumn' | 'versionProperty' | 'concurrencyCheckKeys' | 'serializedPrimaryKey' | 'indexes' | 'uniques' | 'repository' | 'orderBy'> {
559
+ export interface EntityMetadataWithProperties<TName extends string, TTableName extends string, TProperties extends Record<string, any>, TPK extends (keyof TProperties)[] | undefined = undefined, TBase = never, TRepository = never, TForceObject extends boolean = false> extends Omit<Partial<EntityMetadata<InferEntityFromProperties<TProperties, TPK, TBase, TRepository>>>, 'properties' | 'extends' | 'primaryKeys' | 'hooks' | 'discriminatorColumn' | 'versionProperty' | 'concurrencyCheckKeys' | 'serializedPrimaryKey' | 'indexes' | 'uniques' | 'repository' | 'filters' | 'orderBy'> {
560
560
  name: TName;
561
561
  tableName?: TTableName;
562
562
  extends?: {
563
563
  '~entity': TBase;
564
564
  } | EntityCtor<TBase>;
565
565
  properties: TProperties | ((properties: PropertyBuilders) => TProperties);
566
- primaryKeys?: TPK & InferPrimaryKey<TProperties>[];
566
+ primaryKeys?: TPK & InferPrimaryKeyConstraint<TProperties>[];
567
567
  hooks?: DefineEntityHooks;
568
568
  repository?: () => TRepository;
569
+ filters?: Dictionary<{
570
+ name: string;
571
+ cond: Dictionary | ((args: Dictionary, type: 'read' | 'update' | 'delete', em: any, options?: FindOptions<any, any, any, any> | FindOneOptions<any, any, any, any>, entityName?: string) => MaybePromise<FilterQuery<any>>);
572
+ default?: boolean;
573
+ entity?: EntityName<any> | EntityName<any>[];
574
+ args?: boolean;
575
+ strict?: boolean;
576
+ }>;
569
577
  forceObject?: TForceObject;
570
578
  inheritance?: 'tpt';
571
579
  orderBy?: {
@@ -671,6 +679,14 @@ export type InferPrimaryKey<Properties extends Record<string, any>> = {
671
679
  };
672
680
  } ? K : never;
673
681
  }[keyof Properties];
682
+ /** Like InferPrimaryKey, but skips evaluating function return types to prevent circular inference (GH #7445). */
683
+ export type InferPrimaryKeyConstraint<Properties extends Record<string, any>> = {
684
+ [K in keyof Properties]: Properties[K] extends (...args: any) => any ? K : Properties[K] extends {
685
+ '~options': {
686
+ primary: true;
687
+ };
688
+ } ? K : never;
689
+ }[keyof Properties];
674
690
  type InferBuilderValue<Builder> = Builder extends {
675
691
  '~type'?: {
676
692
  value: infer Value;
@@ -706,6 +722,9 @@ type MaybeScalarRef<Value, Options> = Options extends {
706
722
  } ? Value : Options extends {
707
723
  ref: true;
708
724
  } ? ScalarReference<Value> : Value;
725
+ type IsAllPropsOpt<T> = [Exclude<keyof T, symbol>] extends [never] ? false : {
726
+ [K in Exclude<keyof T, symbol>]-?: T[K] extends Opt ? never : K;
727
+ }[Exclude<keyof T, symbol>] extends never ? true : false;
709
728
  type MaybeOpt<Value, Options> = Options extends {
710
729
  mapToPk: true;
711
730
  } ? Value extends Opt<infer OriginalValue> ? OriginalValue : Value : Options extends {
@@ -722,7 +741,9 @@ type MaybeOpt<Value, Options> = Options extends {
722
741
  version: true;
723
742
  } | {
724
743
  formula: string | ((...args: any[]) => any);
725
- } ? Opt<NonNullable<Value>> | Extract<Value, null | undefined> : Value;
744
+ } ? Opt<NonNullable<Value>> | Extract<Value, null | undefined> : Options extends {
745
+ kind: 'embedded';
746
+ } ? IsAllPropsOpt<Value> extends true ? Opt<NonNullable<Value>> | Extract<Value, null | undefined> : Value : Value;
726
747
  type MaybeHidden<Value, Options> = Options extends {
727
748
  hidden: true;
728
749
  } ? 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.9",
3
+ "version": "7.0.8-dev.0",
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.9';
135
+ static #ORM_VERSION = '7.0.8-dev.0';
136
136
  /**
137
137
  * Checks if the argument is instance of `Object`. Returns false for arrays.
138
138
  */