@mikro-orm/core 7.2.5-dev.1 → 7.2.5-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.
package/EntityManager.js CHANGED
@@ -781,14 +781,36 @@ export class EntityManager {
781
781
  em.#unitOfWork.unsetIdentity(entity);
782
782
  return null;
783
783
  }
784
+ // `serialize()` applies `Type.toJSON()` and `serializedName`, take only relations and embeddables from it, scalars from the entity
785
+ const getData = (e, data = helper(e).serialize({
786
+ ignoreSerializers: true,
787
+ includeHidden: true,
788
+ convertCustomTypes: false,
789
+ })) => {
790
+ for (const prop of helper(e).__meta.props) {
791
+ if (prop.kind === ReferenceKind.SCALAR && !prop.ref) {
792
+ data[prop.name] = e[prop.name];
793
+ continue;
794
+ }
795
+ if (prop.serializedName && prop.serializedName in data) {
796
+ data[prop.name] = data[prop.serializedName];
797
+ }
798
+ if (prop.kind === ReferenceKind.EMBEDDED && e[prop.name] != null) {
799
+ Utils.asArray(e[prop.name]).forEach((item, i) => getData(item, prop.array ? data[prop.name][i] : data[prop.name]));
800
+ }
801
+ }
802
+ return data;
803
+ };
804
+ const hydrate = (target, source) => {
805
+ em.config
806
+ .getHydrator(this.metadata)
807
+ .hydrate(target, helper(target).__meta, getData(source), em.#entityFactory, 'full', false, false);
808
+ Utils.merge(helper(target).__originalEntityData, this.#comparator.prepareEntity(source));
809
+ };
784
810
  let found = false;
785
811
  for (const e of fork.#unitOfWork.getIdentityMap()) {
786
812
  const ref = em.getReference(e.constructor, helper(e).getPrimaryKey());
787
- const data = helper(e).serialize({ ignoreSerializers: true, includeHidden: true, convertCustomTypes: false });
788
- em.config
789
- .getHydrator(this.metadata)
790
- .hydrate(ref, helper(ref).__meta, data, em.#entityFactory, 'full', false, false);
791
- Utils.merge(helper(ref).__originalEntityData, this.#comparator.prepareEntity(e));
813
+ hydrate(ref, e);
792
814
  // index a refreshed `targetKey` value, `getByKey` ignores the old one as stale
793
815
  if (helper(ref).__meta.root.targetKeys) {
794
816
  em.#unitOfWork.getIdentityMap().store(ref);
@@ -796,15 +818,7 @@ export class EntityManager {
796
818
  found ||= ref === entity;
797
819
  }
798
820
  if (!found) {
799
- const data = helper(reloaded).serialize({
800
- ignoreSerializers: true,
801
- includeHidden: true,
802
- convertCustomTypes: false,
803
- });
804
- em.config
805
- .getHydrator(this.metadata)
806
- .hydrate(entity, wrapped.__meta, data, em.#entityFactory, 'full', false, false);
807
- Utils.merge(wrapped.__originalEntityData, this.#comparator.prepareEntity(reloaded));
821
+ hydrate(entity, reloaded);
808
822
  }
809
823
  return entity;
810
824
  }
@@ -1506,6 +1506,9 @@ export class MetadataDiscovery {
1506
1506
  if (isInverseSideRelation && rootProp && rootProp.type !== prop.type) {
1507
1507
  return;
1508
1508
  }
1509
+ // a sibling matching the root property must not drop the column variants of already renamed siblings
1510
+ newProp.stiFieldNames ??= rootProp?.stiFieldNames;
1511
+ newProp.stiFieldNameMap ??= rootProp?.stiFieldNameMap;
1509
1512
  meta.root.addProperty(newProp);
1510
1513
  });
1511
1514
  meta.tableName = meta.root.tableName;
@@ -1626,7 +1629,7 @@ export class MetadataDiscovery {
1626
1629
  if (meta.inheritanceType !== 'tpt') {
1627
1630
  return;
1628
1631
  }
1629
- const belongsToTable = (prop) => prop.persist !== false || prop.primary;
1632
+ const belongsToTable = (prop) => prop.persist !== false || prop.primary || !!prop.formula;
1630
1633
  // Use meta.properties (object) since meta.props (array) may not be populated yet
1631
1634
  const allProps = Object.values(meta.properties);
1632
1635
  if (!meta.tptParent) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
- "version": "7.2.5-dev.1",
3
+ "version": "7.2.5-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",
@@ -44,7 +44,8 @@ export class CommitOrderCalculator {
44
44
  if (!toOneOwner && !toManyOwner) {
45
45
  return;
46
46
  }
47
- const propertyType = prop.targetMeta?.root._id;
47
+ // TPT tables are separate nodes and the FK references the target's own table, not the root
48
+ const propertyType = (prop.targetMeta?.inheritanceType === 'tpt' ? prop.targetMeta : prop.targetMeta?.root)?._id;
48
49
  if (propertyType == null || !this.hasNode(propertyType)) {
49
50
  return;
50
51
  }
@@ -1283,6 +1283,13 @@ export class UnitOfWork {
1283
1283
  calc.discoverProperty({ ...prop, targetMeta }, meta._id);
1284
1284
  }
1285
1285
  }
1286
+ else if (prop.targetMeta?.inheritanceType === 'tpt') {
1287
+ calc.discoverProperty(prop, meta._id);
1288
+ // a TPT subclass delete has only a leaf change set that also removes the target row; weak edge so it can't break insert order
1289
+ for (const targetMeta of this.#metadata.find(prop.targetMeta.class).allTPTDescendants ?? []) {
1290
+ calc.discoverProperty({ ...prop, targetMeta, nullable: true }, meta._id);
1291
+ }
1292
+ }
1286
1293
  else {
1287
1294
  calc.discoverProperty(prop, meta._id);
1288
1295
  }
@@ -366,8 +366,16 @@ export class EntityComparator {
366
366
  if (prop.renamedFrom) {
367
367
  lines.push(`${padding} ${this.propName(prop.fieldNames[0], 'mapped')} = true;`);
368
368
  defined = `${this.propName(prop.fieldNames[0])} != null`;
369
- // a column shared with a differently typed subtype is left for each subtype's hydrator to convert
370
- if (meta.props.some(p => p !== prop && p.name === prop.name && p.fieldNames?.[0] === prop.fieldNames[0])) {
369
+ const sharesColumn = meta.props.some(p => p !== prop && p.name === prop.name && p.fieldNames?.[0] === prop.fieldNames[0]);
370
+ // hydrators expect mapped embeddable keys, so a shared object embeddable is mapped only for its own subtypes
371
+ if (sharesColumn && prop.kind === ReferenceKind.EMBEDDED && prop.object && meta.discriminatorMap) {
372
+ const owners = Object.entries(meta.discriminatorMap)
373
+ .filter(([, cls]) => this.#metadata.find(cls)?.properties[prop.name]?.targetMeta === prop.targetMeta)
374
+ .map(([value]) => `${this.propName(meta.discriminatorColumn)} == ${JSON.stringify(value)}`);
375
+ defined += ` && (${owners.join(' || ')})`;
376
+ }
377
+ else if (sharesColumn) {
378
+ // a column shared with a differently typed subtype is left for each subtype's hydrator to convert
371
379
  lines.push(`${padding} if (${defined}) {`);
372
380
  lines.push(`${padding} ret${this.wrap(prop.name)} = ${this.propName(prop.fieldNames[0])};`);
373
381
  lines.push(`${padding} }`);
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.5-dev.1';
156
+ static #ORM_VERSION = '7.2.5-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}`;