@mikro-orm/sql 7.0.11-dev.13 → 7.0.11-dev.14

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.
@@ -123,7 +123,7 @@ export declare abstract class AbstractSqlDriver<Connection extends AbstractSqlCo
123
123
  * This method reads the discriminator to determine the concrete type and maps child-specific fields.
124
124
  * @internal
125
125
  */
126
- protected mapTPTChildFields<T extends object>(relationPojo: EntityData<T>, meta: EntityMetadata<T>, relationAlias: string, qb: AnyQueryBuilder<T>, root: EntityData<T>): void;
126
+ protected mapTPTChildFields<T extends object>(relationPojo: EntityData<T>, meta: EntityMetadata<T>, relationAlias: string, qb: AnyQueryBuilder<T>, root: EntityData<T>): EntityMetadata | undefined;
127
127
  /**
128
128
  * @internal
129
129
  */
@@ -328,9 +328,11 @@ export class AbstractSqlDriver extends DatabaseDriver {
328
328
  for (const p of targetProps) {
329
329
  this.mapJoinedProp(relationPojo, p, relationAlias, root, tz, meta2);
330
330
  }
331
- // Inject the entity class constructor so that the factory creates the correct type
331
+ // For TPT base targets, map child-specific fields and resolve the
332
+ // concrete class so the factory creates the correct subtype.
333
+ const concreteMeta = this.mapTPTChildFields(relationPojo, meta2, relationAlias, qb, root);
332
334
  Object.defineProperty(relationPojo, 'constructor', {
333
- value: meta2.class,
335
+ value: concreteMeta?.class ?? meta2.class,
334
336
  enumerable: false,
335
337
  configurable: true,
336
338
  });
@@ -1439,6 +1441,12 @@ export class AbstractSqlDriver extends DatabaseDriver {
1439
1441
  const targetPath = `${pathPrefix}${basePath}[${targetMeta.className}]`;
1440
1442
  const schema = targetMeta.schema === '*' ? (options?.schema ?? this.config.get('schema')) : targetMeta.schema;
1441
1443
  qb.addPolymorphicJoin(prop, targetMeta, options.parentTableAlias, tableAlias, JoinType.leftJoin, targetPath, schema);
1444
+ // For polymorphic targets that are TPT base classes, also LEFT JOIN
1445
+ // all descendant tables so child-specific fields can be selected.
1446
+ if (targetMeta.inheritanceType === 'tpt' && targetMeta.tptChildren?.length && !ref) {
1447
+ const tptMeta = this.metadata.get(targetMeta.class);
1448
+ this.addTPTPolymorphicJoinsForRelation(qb, tptMeta, tableAlias, fields);
1449
+ }
1442
1450
  if (ref) {
1443
1451
  // For filter :ref hints, schedule filter check for each target (no field selection)
1444
1452
  qb.scheduleFilterCheck(targetPath);
@@ -1600,29 +1608,24 @@ export class AbstractSqlDriver extends DatabaseDriver {
1600
1608
  * @internal
1601
1609
  */
1602
1610
  mapTPTChildFields(relationPojo, meta, relationAlias, qb, root) {
1603
- // Check if this is a TPT base with polymorphic children
1604
1611
  if (meta.inheritanceType !== 'tpt' || !meta.root.tptDiscriminatorColumn) {
1605
1612
  return;
1606
1613
  }
1607
- // Read the discriminator value
1608
1614
  const discriminatorAlias = `${relationAlias}__${meta.root.tptDiscriminatorColumn}`;
1609
1615
  const discriminatorValue = root[discriminatorAlias];
1610
1616
  if (!discriminatorValue) {
1611
1617
  return;
1612
1618
  }
1613
- // Set the discriminator in the pojo for EntityFactory
1614
1619
  relationPojo[meta.root.tptDiscriminatorColumn] = discriminatorValue;
1615
- // Find the concrete metadata from discriminator map
1616
1620
  const concreteClass = meta.root.discriminatorMap?.[discriminatorValue];
1617
1621
  /* v8 ignore next 3 - defensive check for invalid discriminator values */
1618
1622
  if (!concreteClass) {
1619
1623
  return;
1620
1624
  }
1621
1625
  const concreteMeta = this.metadata.get(concreteClass);
1626
+ delete root[discriminatorAlias];
1622
1627
  if (concreteMeta === meta) {
1623
- // Already the concrete type, no child fields to map
1624
- delete root[discriminatorAlias];
1625
- return;
1628
+ return concreteMeta;
1626
1629
  }
1627
1630
  // Traverse up from concrete type and map fields from each level's table
1628
1631
  const tz = this.platform.getTimezone();
@@ -1639,8 +1642,7 @@ export class AbstractSqlDriver extends DatabaseDriver {
1639
1642
  }
1640
1643
  currentMeta = currentMeta.tptParent;
1641
1644
  }
1642
- // Clean up the discriminator alias
1643
- delete root[discriminatorAlias];
1645
+ return concreteMeta;
1644
1646
  }
1645
1647
  /**
1646
1648
  * @internal
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/sql",
3
- "version": "7.0.11-dev.13",
3
+ "version": "7.0.11-dev.14",
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",
@@ -53,7 +53,7 @@
53
53
  "@mikro-orm/core": "^7.0.10"
54
54
  },
55
55
  "peerDependencies": {
56
- "@mikro-orm/core": "7.0.11-dev.13"
56
+ "@mikro-orm/core": "7.0.11-dev.14"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"