@mikro-orm/sql 7.0.3-dev.2 → 7.0.3-dev.20

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.
@@ -1,4 +1,4 @@
1
- import { type AnyEntity, type Collection, type Configuration, type ConnectionType, type Constructor, type CountOptions, DatabaseDriver, type DeleteOptions, type Dictionary, type DriverMethodOptions, type EntityData, type EntityDictionary, type EntityField, EntityManagerType, type EntityMetadata, type EntityName, type EntityProperty, type FilterQuery, type FindOneOptions, type FindOptions, type FormulaTable, type LockOptions, type LoggingOptions, type NativeInsertUpdateManyOptions, type NativeInsertUpdateOptions, type ObjectQuery, type Options, type OrderDefinition, type PopulateOptions, type PopulatePath, type Primary, type QueryOrderMap, type QueryResult, type Raw, RawQueryFragment, type StreamOptions, type Transaction, type UpsertManyOptions, type UpsertOptions } from '@mikro-orm/core';
1
+ import { type AnyEntity, type Collection, type Configuration, type ConnectionType, type Constructor, type CountOptions, DatabaseDriver, type DeleteOptions, type Dictionary, type DriverMethodOptions, type EntityData, type EntityDictionary, type EntityField, EntityManagerType, type EntityMetadata, type EntityName, type EntityProperty, type FilterQuery, type FindOneOptions, type FindOptions, type FormulaTable, type LockOptions, type LoggingOptions, type NativeInsertUpdateManyOptions, type NativeInsertUpdateOptions, type ObjectQuery, type Options, type OrderDefinition, type PopulateOptions, type Primary, type QueryOrderMap, type QueryResult, type Raw, RawQueryFragment, type StreamOptions, type Transaction, type UpsertManyOptions, type UpsertOptions } from '@mikro-orm/core';
2
2
  import type { AbstractSqlConnection } from './AbstractSqlConnection.js';
3
3
  import type { AbstractSqlPlatform } from './AbstractSqlPlatform.js';
4
4
  import { type AnyQueryBuilder } from './query/QueryBuilder.js';
@@ -23,8 +23,8 @@ export declare abstract class AbstractSqlDriver<Connection extends AbstractSqlCo
23
23
  private validateSqlOptions;
24
24
  createEntityManager(useContext?: boolean): this[typeof EntityManagerType];
25
25
  private createQueryBuilderFromOptions;
26
- find<T extends object, P extends string = never, F extends string = PopulatePath.ALL, E extends string = never>(entityName: EntityName<T>, where: ObjectQuery<T>, options?: FindOptions<T, P, F, E>): Promise<EntityData<T>[]>;
27
- findOne<T extends object, P extends string = never, F extends string = PopulatePath.ALL, E extends string = never>(entityName: EntityName<T>, where: ObjectQuery<T>, options?: FindOneOptions<T, P, F, E>): Promise<EntityData<T> | null>;
26
+ find<T extends object, P extends string = never, F extends string = never, E extends string = never>(entityName: EntityName<T>, where: ObjectQuery<T>, options?: FindOptions<T, P, F, E>): Promise<EntityData<T>[]>;
27
+ findOne<T extends object, P extends string = never, F extends string = never, E extends string = never>(entityName: EntityName<T>, where: ObjectQuery<T>, options?: FindOneOptions<T, P, F, E>): Promise<EntityData<T> | null>;
28
28
  protected hasToManyJoins<T extends object>(hint: PopulateOptions<T>, meta: EntityMetadata<T>): boolean;
29
29
  findVirtual<T extends object>(entityName: EntityName<T>, where: ObjectQuery<T>, options: FindOptions<T, any, any, any>): Promise<EntityData<T>[]>;
30
30
  countVirtual<T extends object>(entityName: EntityName<T>, where: ObjectQuery<T>, options: CountOptions<T, any>): Promise<number>;
@@ -845,7 +845,7 @@ export class AbstractSqlDriver extends DatabaseDriver {
845
845
  value = this.mapDataToFieldNames(value, false, prop.embeddedProps, options.convertCustomTypes);
846
846
  }
847
847
  }
848
- params.push(value);
848
+ params.push(value ?? null);
849
849
  };
850
850
  for (const key of keys) {
851
851
  const prop = meta.properties[key] ?? meta.root.properties[key];
@@ -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
  }
@@ -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
- for (const prop of childMeta.ownProps.filter(p => !p.primary && this.platform.shouldHaveColumn(p, []))) {
1525
- for (const fieldName of prop.fieldNames) {
1526
- const field = `${childAlias}.${fieldName}`;
1527
- const fieldAlias = `${childAlias}__${fieldName}`;
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/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
 
@@ -311,10 +311,32 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
311
311
  : undefined,
312
312
  comment: col.column_comment,
313
313
  };
314
- if (nativeEnums?.[column.type]) {
314
+ let enumKey = column.type;
315
+ let enumEntry = nativeEnums?.[enumKey];
316
+ // for array enum columns, strip the [] suffix and try the base type,
317
+ // try schema-qualified key first for non-default schemas to avoid
318
+ // ambiguity when multiple schemas have enums with the same name
319
+ if (!enumEntry && enumKey.endsWith('[]')) {
320
+ const baseType = enumKey.slice(0, -2);
321
+ if (col.udt_schema && col.udt_schema !== this.platform.getDefaultSchemaName()) {
322
+ const schemaKey = `${col.udt_schema}.${baseType}`;
323
+ enumEntry = nativeEnums?.[schemaKey];
324
+ if (enumEntry) {
325
+ enumKey = schemaKey;
326
+ column.type = `${schemaKey}[]`;
327
+ }
328
+ }
329
+ if (!enumEntry) {
330
+ enumEntry = nativeEnums?.[baseType];
331
+ if (enumEntry) {
332
+ enumKey = baseType;
333
+ }
334
+ }
335
+ }
336
+ if (enumEntry) {
315
337
  column.mappedType = Type.getType(EnumType);
316
- column.nativeEnumName = column.type;
317
- column.enumItems = nativeEnums[column.type]?.items;
338
+ column.nativeEnumName = enumKey;
339
+ column.enumItems = enumEntry.items;
318
340
  }
319
341
  ret[key].push(column);
320
342
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/sql",
3
- "version": "7.0.3-dev.2",
3
+ "version": "7.0.3-dev.20",
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.2"
56
+ "@mikro-orm/core": "7.0.3-dev.20"
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
  }