@mikro-orm/sql 7.0.3-dev.14 → 7.0.3-dev.16

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.
@@ -1412,10 +1412,11 @@ export class AbstractSqlDriver extends DatabaseDriver {
1412
1412
  const [propName, ref] = hint.field.split(':', 2);
1413
1413
  const prop = meta.properties[propName];
1414
1414
  // Polymorphic to-one: create a LEFT JOIN per target type
1415
- // Skip :ref hints — polymorphic to-one already has FK + discriminator in the row
1415
+ // Skip regular :ref hints — polymorphic to-one already has FK + discriminator in the row
1416
+ // But allow filter :ref hints through to create per-target LEFT JOINs with filter checks
1416
1417
  if (prop.polymorphic &&
1417
1418
  prop.polymorphTargets?.length &&
1418
- !ref &&
1419
+ (!ref || hint.filter) &&
1419
1420
  [ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
1420
1421
  const basePath = options.parentJoinPath
1421
1422
  ? `${options.parentJoinPath}.${prop.name}`
@@ -1426,13 +1427,19 @@ export class AbstractSqlDriver extends DatabaseDriver {
1426
1427
  const targetPath = `${pathPrefix}${basePath}[${targetMeta.className}]`;
1427
1428
  const schema = targetMeta.schema === '*' ? (options?.schema ?? this.config.get('schema')) : targetMeta.schema;
1428
1429
  qb.addPolymorphicJoin(prop, targetMeta, options.parentTableAlias, tableAlias, JoinType.leftJoin, targetPath, schema);
1429
- // Select fields from each target table
1430
- fields.push(...this.getFieldsForJoinedLoad(qb, targetMeta, {
1431
- ...options,
1432
- populate: hint.children,
1433
- parentTableAlias: tableAlias,
1434
- parentJoinPath: targetPath,
1435
- }));
1430
+ if (ref) {
1431
+ // For filter :ref hints, schedule filter check for each target (no field selection)
1432
+ qb.scheduleFilterCheck(targetPath);
1433
+ }
1434
+ else {
1435
+ // Select fields from each target table
1436
+ fields.push(...this.getFieldsForJoinedLoad(qb, targetMeta, {
1437
+ ...options,
1438
+ populate: hint.children,
1439
+ parentTableAlias: tableAlias,
1440
+ parentJoinPath: targetPath,
1441
+ }));
1442
+ }
1436
1443
  }
1437
1444
  continue;
1438
1445
  }
package/README.md CHANGED
@@ -64,7 +64,7 @@ export class Book extends BookSchema.class {}
64
64
  BookSchema.setClass(Book);
65
65
  ```
66
66
 
67
- You can also define entities using [decorators](https://mikro-orm.io/docs/defining-entities) or [`EntitySchema`](https://mikro-orm.io/docs/entity-schema). See the [defining entities guide](https://mikro-orm.io/docs/defining-entities) for all options.
67
+ You can also define entities using [decorators](https://mikro-orm.io/docs/using-decorators) or [`EntitySchema`](https://mikro-orm.io/docs/define-entity#entityschema-low-level-api). See the [defining entities guide](https://mikro-orm.io/docs/defining-entities) for all options.
68
68
 
69
69
  ### Initialize and Use
70
70
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/sql",
3
- "version": "7.0.3-dev.14",
3
+ "version": "7.0.3-dev.16",
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.2"
54
54
  },
55
55
  "peerDependencies": {
56
- "@mikro-orm/core": "7.0.3-dev.14"
56
+ "@mikro-orm/core": "7.0.3-dev.16"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"
@@ -391,6 +391,26 @@ export class QueryBuilder {
391
391
  else {
392
392
  join.cond = { ...cond };
393
393
  }
394
+ // For polymorphic LEFT JOIN filters, add a WHERE condition to enforce the filter
395
+ // only for rows matching this target's discriminator value. This ensures rows pointing
396
+ // to other polymorphic targets are not excluded.
397
+ if (join.prop.polymorphic &&
398
+ join.prop.targetMeta &&
399
+ join.type === JoinType.leftJoin &&
400
+ join.prop.discriminatorColumn &&
401
+ join.prop.discriminatorMap) {
402
+ const discriminatorValue = QueryHelper.findDiscriminatorValue(join.prop.discriminatorMap, join.prop.targetMeta.class);
403
+ if (discriminatorValue) {
404
+ const pks = join.prop.targetMeta.primaryKeys;
405
+ this.andWhere({
406
+ $or: [
407
+ { [`${join.ownerAlias}.${join.prop.discriminatorColumn}`]: null },
408
+ { [`${join.ownerAlias}.${join.prop.discriminatorColumn}`]: { $ne: discriminatorValue } },
409
+ { [`${join.alias}.${Utils.getPrimaryKeyHash(pks)}`]: { $ne: null } },
410
+ ],
411
+ });
412
+ }
413
+ }
394
414
  }
395
415
  }
396
416
  }