@mikro-orm/core 7.1.4-dev.0 → 7.1.4-dev.10

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.
@@ -70,6 +70,8 @@ export declare class Collection<T extends object, O extends object = object> {
70
70
  /** Initializes the collection by loading its items from the database. */
71
71
  init<TT extends T, P extends string = never>(options?: InitCollectionOptions<TT, P>): Promise<LoadedCollection<Loaded<TT, P>>>;
72
72
  private getEntityManager;
73
+ /** The owning-side property this collection is mapped by (the inverse of `mappedBy`); `undefined` for the owning side of m:n. */
74
+ private get mappedByProp();
73
75
  private createCondition;
74
76
  private createManyToManyCondition;
75
77
  private createLoadCountCondition;
@@ -320,9 +320,14 @@ export class Collection {
320
320
  }
321
321
  return em;
322
322
  }
323
+ /** The owning-side property this collection is mapped by (the inverse of `mappedBy`); `undefined` for the owning side of m:n. */
324
+ get mappedByProp() {
325
+ return this.property.mappedBy ? this.property.targetMeta.properties[this.property.mappedBy] : undefined;
326
+ }
323
327
  createCondition(cond = {}) {
324
328
  if (this.property.kind === ReferenceKind.ONE_TO_MANY) {
325
- cond[this.property.mappedBy] = helper(this.owner).getPrimaryKey();
329
+ // the owning FK may reference a non-PK column (`targetKey`), so match on that value when set
330
+ cond[this.property.mappedBy] = helper(this.owner).getTargetKeyValue(this.mappedByProp?.targetKey);
326
331
  }
327
332
  else {
328
333
  // MANY_TO_MANY
@@ -344,7 +349,12 @@ export class Collection {
344
349
  }
345
350
  createLoadCountCondition(cond) {
346
351
  const wrapped = helper(this.owner);
347
- const val = wrapped.__meta.compositePK ? { $in: wrapped.__primaryKeys } : wrapped.getPrimaryKey();
352
+ const ownerProp = this.mappedByProp;
353
+ const val = ownerProp?.targetKey
354
+ ? wrapped.getTargetKeyValue(ownerProp.targetKey)
355
+ : wrapped.__meta.compositePK
356
+ ? { $in: wrapped.__primaryKeys }
357
+ : wrapped.getPrimaryKey();
348
358
  const dict = cond;
349
359
  if (this.property.kind === ReferenceKind.ONE_TO_MANY) {
350
360
  dict[this.property.mappedBy] = val;
@@ -281,22 +281,30 @@ export class EntityLoader {
281
281
  }
282
282
  }
283
283
  initializeOneToMany(filtered, children, prop, field, partial, readonly) {
284
- const mapToPk = prop.targetMeta.properties[prop.mappedBy].mapToPk;
284
+ const ownerProp = prop.targetMeta.properties[prop.mappedBy];
285
+ const targetKey = ownerProp.targetKey;
285
286
  const map = {};
287
+ // when the owning side targets a non-PK column, group by that value on both sides
288
+ const parentKey = (entity) => helper(entity).getSerializedTargetKey(targetKey);
286
289
  for (const entity of filtered) {
287
- const key = helper(entity).getSerializedPrimaryKey();
288
- map[key] = [];
290
+ map[parentKey(entity)] = [];
289
291
  }
290
292
  for (const child of children) {
291
- const pk = child.__helper.__data[prop.mappedBy] ?? child[prop.mappedBy];
292
- if (pk) {
293
- const key = helper(mapToPk ? this.#em.getReference(prop.targetMeta.class, pk) : pk).getSerializedPrimaryKey();
293
+ const fk = child.__helper.__data[prop.mappedBy] ?? child[prop.mappedBy];
294
+ if (fk) {
295
+ let key;
296
+ if (targetKey) {
297
+ // `fk` is the owner reference (resolve its targetKey) unless the relation maps to the raw PK value
298
+ key = Utils.isEntity(fk, true) ? helper(fk).getSerializedTargetKey(targetKey) : '' + fk;
299
+ }
300
+ else {
301
+ key = helper(ownerProp.mapToPk ? this.#em.getReference(prop.targetMeta.class, fk) : fk).getSerializedPrimaryKey();
302
+ }
294
303
  map[key]?.push(child);
295
304
  }
296
305
  }
297
306
  for (const entity of filtered) {
298
- const key = helper(entity).getSerializedPrimaryKey();
299
- entity[field].hydrate(map[key], undefined, partial, readonly);
307
+ entity[field].hydrate(map[parentKey(entity)], undefined, partial, readonly);
300
308
  }
301
309
  }
302
310
  initializeManyToMany(filtered, children, prop, field, customOrder, partial, readonly) {
@@ -326,8 +334,10 @@ export class EntityLoader {
326
334
  let schema = options.schema;
327
335
  const partial = !Utils.isEmpty(prop.where) || !Utils.isEmpty(options.where);
328
336
  let polymorphicOwnerProp;
329
- if (prop.kind === ReferenceKind.ONE_TO_MANY || (prop.kind === ReferenceKind.MANY_TO_MANY && !prop.owner)) {
330
- const ownerProp = meta.properties[prop.mappedBy];
337
+ const ownerProp = prop.kind === ReferenceKind.ONE_TO_MANY || (prop.kind === ReferenceKind.MANY_TO_MANY && !prop.owner)
338
+ ? meta.properties[prop.mappedBy]
339
+ : undefined;
340
+ if (ownerProp) {
331
341
  if (ownerProp.polymorphic && ownerProp.fieldNames.length >= 2) {
332
342
  const idColumns = ownerProp.fieldNames.slice(1);
333
343
  fk = idColumns.length === 1 ? idColumns[0] : idColumns;
@@ -348,7 +358,9 @@ export class EntityLoader {
348
358
  if (!schema && [ReferenceKind.ONE_TO_ONE, ReferenceKind.MANY_TO_ONE].includes(prop.kind)) {
349
359
  schema = children.find(e => e.__helper.__schema)?.__helper.__schema;
350
360
  }
351
- const ids = Utils.unique(children.map(e => (prop.targetKey ? e[prop.targetKey] : e.__helper.getPrimaryKey())));
361
+ // for inverse sides the `targetKey` lives on the owning property, otherwise on the prop itself
362
+ const targetKey = prop.targetKey ?? ownerProp?.targetKey;
363
+ const ids = Utils.unique(children.map(e => e.__helper.getTargetKeyValue(targetKey)));
352
364
  let where;
353
365
  if (polymorphicOwnerProp && Array.isArray(fk)) {
354
366
  const conditions = ids.map(id => {
@@ -417,7 +429,7 @@ export class EntityLoader {
417
429
  if (prop.targetKey && [ReferenceKind.ONE_TO_ONE, ReferenceKind.MANY_TO_ONE].includes(prop.kind)) {
418
430
  const itemsByKey = new Map();
419
431
  for (const item of items) {
420
- itemsByKey.set('' + item[prop.targetKey], item);
432
+ itemsByKey.set(helper(item).getSerializedTargetKey(prop.targetKey), item);
421
433
  }
422
434
  for (const entity of entities) {
423
435
  const ref = entity[prop.name];
@@ -425,8 +437,7 @@ export class EntityLoader {
425
437
  if (!ref) {
426
438
  continue;
427
439
  }
428
- // oxfmt-ignore
429
- const keyValue = '' + (Reference.isReference(ref) ? ref.unwrap()[prop.targetKey] : ref[prop.targetKey]);
440
+ const keyValue = helper(ref).getSerializedTargetKey(prop.targetKey);
430
441
  const loadedItem = itemsByKey.get(keyValue);
431
442
  if (loadedItem) {
432
443
  entity[prop.name] = (Reference.isReference(ref) ? Reference.create(loadedItem) : loadedItem);
@@ -437,7 +448,8 @@ export class EntityLoader {
437
448
  const nullVal = this.#em.config.get('forceUndefined') ? undefined : null;
438
449
  const itemsMap = new Set();
439
450
  const childrenMap = new Set();
440
- // Use targetKey value if set, otherwise use serialized PK
451
+ // `e` may be an unresolved reference here, so read the targetKey value directly instead of
452
+ // resolving it through the entity — that keeps orphaned references (missing target row) intact
441
453
  const getKey = (e) => (prop.targetKey ? '' + e[prop.targetKey] : helper(e).getSerializedPrimaryKey());
442
454
  for (const item of items) {
443
455
  /* v8 ignore next */
@@ -79,6 +79,10 @@ export declare class WrappedEntity<Entity extends object> {
79
79
  setPrimaryKey(id: Primary<Entity> | null): void;
80
80
  /** Returns the primary key serialized as a string suitable for identity map lookups. */
81
81
  getSerializedPrimaryKey(): string;
82
+ /** Returns the value a relation references on this entity — the `targetKey` column when set, otherwise the primary key. */
83
+ getTargetKeyValue(targetKey?: string): Primary<Entity> | null;
84
+ /** Serialized counterpart of `getTargetKeyValue`, suitable for identity map / grouping keys. */
85
+ getSerializedTargetKey(targetKey?: string): string;
82
86
  get __meta(): EntityMetadata<Entity>;
83
87
  get __platform(): Platform;
84
88
  get __config(): Configuration;
@@ -159,6 +159,14 @@ export class WrappedEntity {
159
159
  getSerializedPrimaryKey() {
160
160
  return this.pkSerializer(this.entity);
161
161
  }
162
+ /** Returns the value a relation references on this entity — the `targetKey` column when set, otherwise the primary key. */
163
+ getTargetKeyValue(targetKey) {
164
+ return targetKey ? this.entity[targetKey] : this.getPrimaryKey();
165
+ }
166
+ /** Serialized counterpart of `getTargetKeyValue`, suitable for identity map / grouping keys. */
167
+ getSerializedTargetKey(targetKey) {
168
+ return targetKey ? '' + this.entity[targetKey] : this.getSerializedPrimaryKey();
169
+ }
162
170
  get __meta() {
163
171
  return this.entity.__meta;
164
172
  }
@@ -1391,7 +1391,9 @@ export class MetadataDiscovery {
1391
1391
  meta.root.uniques.push({ properties });
1392
1392
  }
1393
1393
  }
1394
- newProp.nullable = true;
1394
+ // The primary key column is shared by every subtype, so it stays NOT NULL —
1395
+ // only subtype-specific columns become nullable for the rows of other subtypes.
1396
+ newProp.nullable = !newProp.primary;
1395
1397
  newProp.inherited = !rootProp;
1396
1398
  // For narrowed relation overrides, keep the root's declaration intact so
1397
1399
  // the full target union (e.g. `Food`) is preserved for populates from the
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
- "version": "7.1.4-dev.0",
3
+ "version": "7.1.4-dev.10",
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
@@ -405,6 +405,8 @@ export interface IWrappedEntityInternal<Entity extends object> extends IWrappedE
405
405
  getPrimaryKeys(convertCustomTypes?: boolean): Primary<Entity>[] | null;
406
406
  setPrimaryKey(val: Primary<Entity>): void;
407
407
  getSerializedPrimaryKey(): string & keyof Entity;
408
+ getTargetKeyValue(targetKey?: string): Primary<Entity> | null;
409
+ getSerializedTargetKey(targetKey?: string): string;
408
410
  __meta: EntityMetadata<Entity>;
409
411
  __data: Dictionary;
410
412
  __em?: EntityManager;
@@ -393,7 +393,25 @@ export class EntityComparator {
393
393
  else {
394
394
  mapEntityProperties(meta);
395
395
  }
396
- lines.push(` for (let k in result) { if (Object.hasOwn(result, k) && !mapped[k] && ret[k] === undefined) ret[k] = result[k]; }`);
396
+ // pass through any unmapped columns (e.g. extra selections or virtual props). for embeddables we restrict
397
+ // this to declared keys, otherwise JSON keys not part of the schema leak into the original data while
398
+ // hydration drops them from the entity, diverging the snapshot and triggering spurious updates.
399
+ let knownKeysGuard = '';
400
+ if (meta.embeddable) {
401
+ const knownKeys = new Set();
402
+ for (const m of meta.polymorphs?.length ? meta.polymorphs : [meta]) {
403
+ for (const prop of m.props) {
404
+ knownKeys.add(prop.name);
405
+ if (prop.embedded) {
406
+ knownKeys.add(prop.embedded[1]);
407
+ }
408
+ prop.fieldNames?.forEach(field => knownKeys.add(field));
409
+ }
410
+ }
411
+ context.set('knownKeys', knownKeys);
412
+ knownKeysGuard = ' && knownKeys.has(k)';
413
+ }
414
+ lines.push(` for (let k in result) { if (Object.hasOwn(result, k) && !mapped[k] && ret[k] === undefined${knownKeysGuard}) ret[k] = result[k]; }`);
397
415
  const code = `// compiled mapper for entity ${meta.className}\n` +
398
416
  `return function(result) {\n const ret = {};\n${lines.join('\n')}\n return ret;\n}`;
399
417
  const fnKey = `resultMapper-${meta.uniqueName}`;
package/utils/Utils.js CHANGED
@@ -141,7 +141,7 @@ export function parseJsonSafe(value) {
141
141
  /** Collection of general-purpose utility methods used throughout the ORM. */
142
142
  export class Utils {
143
143
  static PK_SEPARATOR = '~~~';
144
- static #ORM_VERSION = '7.1.4-dev.0';
144
+ static #ORM_VERSION = '7.1.4-dev.10';
145
145
  /**
146
146
  * Checks if the argument is instance of `Object`. Returns false for arrays.
147
147
  */