@mikro-orm/sql 7.0.3-dev.13 → 7.0.3-dev.15
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/AbstractSqlDriver.js +20 -16
- package/package.json +2 -2
- package/query/QueryBuilder.js +20 -0
package/AbstractSqlDriver.js
CHANGED
|
@@ -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
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
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
|
}
|
|
@@ -1521,13 +1528,10 @@ export class AbstractSqlDriver extends DatabaseDriver {
|
|
|
1521
1528
|
childAliases[childMeta.className] = childAlias;
|
|
1522
1529
|
qb.addPropertyJoin(childMeta.tptInverseProp, baseAlias, childAlias, JoinType.leftJoin, `[tpt]${meta.className}`);
|
|
1523
1530
|
// Add fields from this child (only ownProps, skip PKs)
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
fields.push(raw(`${this.platform.quoteIdentifier(field)} as ${this.platform.quoteIdentifier(fieldAlias)}`));
|
|
1529
|
-
}
|
|
1530
|
-
}
|
|
1531
|
+
const schema = childMeta.schema === '*' ? '*' : this.getSchemaName(childMeta);
|
|
1532
|
+
childMeta
|
|
1533
|
+
.ownProps.filter(p => !p.primary && this.platform.shouldHaveColumn(p, []))
|
|
1534
|
+
.forEach(prop => fields.push(...this.mapPropToFieldNames(qb, prop, childAlias, childMeta, schema)));
|
|
1531
1535
|
}
|
|
1532
1536
|
// Add computed discriminator (descendants already sorted by depth)
|
|
1533
1537
|
if (meta.root.tptDiscriminatorColumn) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/sql",
|
|
3
|
-
"version": "7.0.3-dev.
|
|
3
|
+
"version": "7.0.3-dev.15",
|
|
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.
|
|
56
|
+
"@mikro-orm/core": "7.0.3-dev.15"
|
|
57
57
|
},
|
|
58
58
|
"engines": {
|
|
59
59
|
"node": ">= 22.17.0"
|
package/query/QueryBuilder.js
CHANGED
|
@@ -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
|
}
|