@mikro-orm/core 7.2.10-dev.10 → 7.2.10-dev.11

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.
@@ -4,6 +4,7 @@ import { EventType, ReferenceKind } from '../enums.js';
4
4
  import { Reference } from './Reference.js';
5
5
  import { helper } from './wrap.js';
6
6
  import { EntityHelper } from './EntityHelper.js';
7
+ import { PolymorphicRef } from './PolymorphicRef.js';
7
8
  import { JsonType } from '../types/JsonType.js';
8
9
  import { isRaw } from '../utils/RawQueryFragment.js';
9
10
  /** @internal Factory responsible for creating, merging, and hydrating entity instances. */
@@ -237,12 +238,20 @@ export class EntityFactory {
237
238
  const nullVal = this.#config.get('forceUndefined') ? undefined : null;
238
239
  Utils.keys(diff2).forEach(key => {
239
240
  const prop = meta.properties[key];
240
- if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) &&
241
- Utils.isPlainObject(data[prop.name])) {
242
- // oxfmt-ignore
243
- diff2[key] = entity[prop.name] ? helper(entity[prop.name]).getPrimaryKey(options.convertCustomTypes) : null;
241
+ // a joined polymorphic row carries its target class in `constructor`, so it is not a plain object
242
+ const joined = Utils.isPlainObject(data[prop.name]) ||
243
+ (prop.polymorphic && Utils.isObject(data[prop.name]) && !(data[prop.name] instanceof PolymorphicRef));
244
+ if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) && joined) {
245
+ const target = entity[prop.name] ? Reference.unwrapReference(entity[prop.name]) : null;
246
+ // use the actual target's meta, a polymorphic relation has several
247
+ let value = target ? EntityHelper.getForeignKeyValue(target, prop, helper(target).__meta) : null;
248
+ if (target && prop.polymorphic) {
249
+ const discriminator = QueryHelper.findDiscriminatorValue(prop.discriminatorMap, target.constructor);
250
+ value = new PolymorphicRef(discriminator, value);
251
+ }
252
+ diff2[key] = value;
244
253
  }
245
- if (!options.convertCustomTypes &&
254
+ else if (!options.convertCustomTypes &&
246
255
  [ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE, ReferenceKind.SCALAR].includes(prop.kind) &&
247
256
  prop.customType?.ensureComparable(meta, prop) &&
248
257
  diff2[key] != null) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
- "version": "7.2.10-dev.10",
3
+ "version": "7.2.10-dev.11",
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/utils/Utils.js CHANGED
@@ -153,7 +153,7 @@ export function parseJsonSafe(value) {
153
153
  /** Collection of general-purpose utility methods used throughout the ORM. */
154
154
  export class Utils {
155
155
  static PK_SEPARATOR = '~~~';
156
- static #ORM_VERSION = '7.2.10-dev.10';
156
+ static #ORM_VERSION = '7.2.10-dev.11';
157
157
  /** Default session variable name backing an RLS filter argument (`current_setting('mikro.<filter>.<arg>')`). */
158
158
  static getRlsSettingName(filterName, argName) {
159
159
  return `mikro.${filterName}.${argName}`;