@mikro-orm/sql 7.2.10-dev.1 → 7.2.10-dev.2

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.
@@ -51,6 +51,9 @@ export declare abstract class AbstractSqlDriver<Connection extends AbstractSqlCo
51
51
  */
52
52
  private mapTPTColumns;
53
53
  private mapJoinedProps;
54
+ /** Keys are marked consumed, not deleted per row: deletes are slow and shared FK columns must stay readable. */
55
+ private resolveJoinedTargets;
56
+ private aliasedKeys;
54
57
  /**
55
58
  * Maps a single property from a joined result row into the relation pojo.
56
59
  * Handles polymorphic FKs, composite keys, Date parsing, and embedded objects.
@@ -342,11 +342,14 @@ export class AbstractSqlDriver extends DatabaseDriver {
342
342
  if (qb) {
343
343
  // here we map the aliased results (cartesian product) to an object graph
344
344
  this.mapJoinedProps(ret, meta, populate, qb, ret, map);
345
- // the aliased columns were deleted, which left the row in slow dictionary mode
345
+ // copy without the consumed aliased columns, deleting them would leave the row in slow dictionary mode
346
346
  if (populate.length > 0) {
347
347
  const clean = {};
348
+ const consumed = map.consumedKeys;
348
349
  for (const key in ret) {
349
- clean[key] = ret[key];
350
+ if (!consumed?.has(key)) {
351
+ clean[key] = ret[key];
352
+ }
350
353
  }
351
354
  return clean;
352
355
  }
@@ -421,7 +424,7 @@ export class AbstractSqlDriver extends DatabaseDriver {
421
424
  const relationPojo = {};
422
425
  const tz = this.platform.getTimezone();
423
426
  for (const p of targetProps) {
424
- this.mapJoinedProp(relationPojo, p, relationAlias, root, tz, meta2);
427
+ this.mapJoinedProp(relationPojo, p, this.aliasedKeys(p, relationAlias), root, tz, meta2);
425
428
  }
426
429
  // For TPT base targets, map child-specific fields and resolve the
427
430
  // concrete class so the factory creates the correct subtype.
@@ -436,9 +439,10 @@ export class AbstractSqlDriver extends DatabaseDriver {
436
439
  this.mapJoinedProps(relationPojo, meta2, populateChildren, qb, root, map, targetPath);
437
440
  }
438
441
  // Clean up aliased columns for ALL targets (even non-matching ones)
442
+ const consumed = (map.consumedKeys ??= new Set());
439
443
  for (const p of targetProps) {
440
444
  for (const name of p.fieldNames) {
441
- delete root[`${relationAlias}__${name}`];
445
+ consumed.add(`${relationAlias}__${name}`);
442
446
  }
443
447
  }
444
448
  }
@@ -472,8 +476,9 @@ export class AbstractSqlDriver extends DatabaseDriver {
472
476
  const alias = `${relationAlias}__${prop.inverseJoinColumns[0]}`;
473
477
  item = root[alias];
474
478
  }
475
- prop.joinColumns.forEach(name => delete root[`${relationAlias}__${name}`]);
476
- prop.inverseJoinColumns.forEach(name => delete root[`${relationAlias}__${name}`]);
479
+ const consumed = (map.consumedKeys ??= new Set());
480
+ prop.joinColumns.forEach(name => consumed.add(`${relationAlias}__${name}`));
481
+ prop.inverseJoinColumns.forEach(name => consumed.add(`${relationAlias}__${name}`));
477
482
  result[prop.name] ??= [];
478
483
  if (item) {
479
484
  result[prop.name].push(item);
@@ -483,17 +488,12 @@ export class AbstractSqlDriver extends DatabaseDriver {
483
488
  const mapToPk = !hint.dataOnly && !!(ref || prop.mapToPk);
484
489
  // all rows of a result share their columns, so resolve the mapped props once per join path
485
490
  const joinedTargets = (map.joinedTargets ??= {});
486
- const { targetProps, virtualProps } = (joinedTargets[path] ??= {
487
- targetProps: (mapToPk
488
- ? meta2.getPrimaryProps()
489
- : meta2.props.filter(prop => this.platform.shouldHaveColumn(prop, hint.children || []))).filter(prop => prop.fieldNames.some(name => typeof root[`${relationAlias}__${name}`] !== 'undefined')),
490
- virtualProps: meta2.props.filter(prop => !ref && prop.persist === false && prop.fieldNames),
491
- });
491
+ joinedTargets[path] ??= this.resolveJoinedTargets(meta2, hint, mapToPk, !!ref, relationAlias, root, map);
492
+ const targets = joinedTargets[path];
493
+ const { targetProps, targetKeys, pkKeys, virtualProps } = targets;
492
494
  // If the primary key value for the relation is null, we know we haven't joined to anything
493
495
  // and therefore we don't return any record (since all values would be null)
494
- const hasPK = meta2.getPrimaryProps().every(pk => pk.fieldNames.every(name => {
495
- return root[`${relationAlias}__${name}`] != null;
496
- }));
496
+ const hasPK = pkKeys.every(key => root[key] != null);
497
497
  if (!hasPK) {
498
498
  if ([ReferenceKind.MANY_TO_MANY, ReferenceKind.ONE_TO_MANY].includes(prop.kind)) {
499
499
  result[prop.name] = [];
@@ -501,10 +501,10 @@ export class AbstractSqlDriver extends DatabaseDriver {
501
501
  if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
502
502
  result[prop.name] = null;
503
503
  }
504
- for (const prop of targetProps) {
505
- for (const name of prop.fieldNames) {
506
- delete root[`${relationAlias}__${name}`];
507
- }
504
+ // resolve nested join paths once, so their columns are dropped even when no row has this relation
505
+ if (hint.children?.length && !targets.childrenResolved) {
506
+ this.mapJoinedProps({}, meta2, hint.children, qb, root, map, path);
507
+ targets.childrenResolved = true;
508
508
  }
509
509
  return;
510
510
  }
@@ -521,18 +521,11 @@ export class AbstractSqlDriver extends DatabaseDriver {
521
521
  }
522
522
  });
523
523
  const tz = this.platform.getTimezone();
524
- for (const prop of targetProps) {
525
- this.mapJoinedProp(relationPojo, prop, relationAlias, root, tz, meta2);
524
+ for (let i = 0; i < targetProps.length; i++) {
525
+ this.mapJoinedProp(relationPojo, targetProps[i], targetKeys[i], root, tz, meta2);
526
526
  }
527
527
  // Handle TPT polymorphic child fields - map fields from child table aliases
528
528
  this.mapTPTChildFields(relationPojo, meta2, relationAlias, qb, root);
529
- // properties can be mapped to multiple places, e.g. when sharing a column in multiple FKs,
530
- // so we need to delete them after everything is mapped from given level
531
- for (const prop of targetProps) {
532
- for (const name of prop.fieldNames) {
533
- delete root[`${relationAlias}__${name}`];
534
- }
535
- }
536
529
  if (mapToPk) {
537
530
  const tmp = Object.values(relationPojo);
538
531
  /* v8 ignore next */
@@ -547,14 +540,34 @@ export class AbstractSqlDriver extends DatabaseDriver {
547
540
  }
548
541
  const populateChildren = hint.children || [];
549
542
  this.mapJoinedProps(relationPojo, meta2, populateChildren, qb, root, map, path);
543
+ targets.childrenResolved = true;
550
544
  });
551
545
  }
546
+ /** Keys are marked consumed, not deleted per row: deletes are slow and shared FK columns must stay readable. */
547
+ resolveJoinedTargets(meta, hint, mapToPk, ref, relationAlias, root, map) {
548
+ const targetProps = (mapToPk
549
+ ? meta.getPrimaryProps()
550
+ : meta.props.filter(prop => this.platform.shouldHaveColumn(prop, hint.children || []))).filter(prop => prop.fieldNames.some(name => typeof root[`${relationAlias}__${name}`] !== 'undefined'));
551
+ const targetKeys = targetProps.map(prop => this.aliasedKeys(prop, relationAlias));
552
+ const consumed = (map.consumedKeys ??= new Set());
553
+ targetKeys.forEach(keys => keys.forEach(key => consumed.add(key)));
554
+ return {
555
+ targetProps,
556
+ targetKeys,
557
+ pkKeys: meta.getPrimaryProps().flatMap(pk => this.aliasedKeys(pk, relationAlias)),
558
+ virtualProps: meta.props.filter(prop => !ref && prop.persist === false && prop.fieldNames),
559
+ };
560
+ }
561
+ aliasedKeys(prop, relationAlias) {
562
+ return prop.fieldNames.map(name => `${relationAlias}__${name}`);
563
+ }
552
564
  /**
553
565
  * Maps a single property from a joined result row into the relation pojo.
554
566
  * Handles polymorphic FKs, composite keys, Date parsing, and embedded objects.
555
567
  */
556
- mapJoinedProp(relationPojo, prop, relationAlias, root, tz, meta, options) {
557
- if (prop.fieldNames.every(name => typeof root[`${relationAlias}__${name}`] === 'undefined')) {
568
+ mapJoinedProp(relationPojo, prop, keys, root, tz, meta, options) {
569
+ const row = root;
570
+ if (keys.every(key => typeof row[key] === 'undefined')) {
558
571
  return;
559
572
  }
560
573
  // inline embeddables are mapped via their flattened child props; mapping the embedded root prop
@@ -563,10 +576,8 @@ export class AbstractSqlDriver extends DatabaseDriver {
563
576
  return;
564
577
  }
565
578
  if (prop.polymorphic && prop.kind !== ReferenceKind.EMBEDDED) {
566
- const discriminatorAlias = `${relationAlias}__${prop.fieldNames[0]}`;
567
- const discriminatorValue = root[discriminatorAlias];
568
- const pkFieldNames = prop.fieldNames.slice(1);
569
- const pkValues = pkFieldNames.map(name => root[`${relationAlias}__${name}`]);
579
+ const discriminatorValue = row[keys[0]];
580
+ const pkValues = keys.slice(1).map(key => row[key]);
570
581
  const pkValue = pkValues.length === 1 ? pkValues[0] : pkValues;
571
582
  if (discriminatorValue != null && pkValue != null) {
572
583
  relationPojo[prop.name] = new PolymorphicRef(discriminatorValue, pkValue);
@@ -577,7 +588,7 @@ export class AbstractSqlDriver extends DatabaseDriver {
577
588
  }
578
589
  else if (prop.fieldNames.length > 1) {
579
590
  // composite keys
580
- const fk = prop.fieldNames.map(name => root[`${relationAlias}__${name}`]);
591
+ const fk = keys.map(key => row[key]);
581
592
  // `mapFlatCompositePrimaryKey` collapses to a scalar when the target PK is a single prop
582
593
  // (e.g. a relation referencing a composite unique key on a single-PK target).
583
594
  const pk = Utils.mapFlatCompositePrimaryKey(fk, prop);
@@ -585,8 +596,7 @@ export class AbstractSqlDriver extends DatabaseDriver {
585
596
  relationPojo[prop.name] = valid ? pk : null;
586
597
  }
587
598
  else if (prop.runtimeType === 'Date') {
588
- const alias = `${relationAlias}__${prop.fieldNames[0]}`;
589
- const value = root[alias];
599
+ const value = row[keys[0]];
590
600
  if (tz &&
591
601
  tz !== 'local' &&
592
602
  typeof value === 'string' &&
@@ -603,8 +613,7 @@ export class AbstractSqlDriver extends DatabaseDriver {
603
613
  }
604
614
  }
605
615
  else {
606
- const alias = `${relationAlias}__${prop.fieldNames[0]}`;
607
- relationPojo[prop.name] = root[alias];
616
+ relationPojo[prop.name] = row[keys[0]];
608
617
  if (prop.kind === ReferenceKind.EMBEDDED && (prop.object || meta.embeddable)) {
609
618
  const item = parseJsonSafe(relationPojo[prop.name]);
610
619
  if (Array.isArray(item)) {
@@ -617,8 +626,8 @@ export class AbstractSqlDriver extends DatabaseDriver {
617
626
  }
618
627
  }
619
628
  if (options?.deleteFromRoot) {
620
- for (const name of prop.fieldNames) {
621
- delete root[`${relationAlias}__${name}`];
629
+ for (const key of keys) {
630
+ delete row[key];
622
631
  }
623
632
  }
624
633
  }
@@ -2109,7 +2118,7 @@ export class AbstractSqlDriver extends DatabaseDriver {
2109
2118
  if (childAlias) {
2110
2119
  // Map fields using same filtering as joined loading, plus skip PKs
2111
2120
  for (const prop of currentMeta.ownProps.filter(p => !p.primary && this.platform.shouldHaveColumn(p, []))) {
2112
- this.mapJoinedProp(relationPojo, prop, childAlias, root, tz, currentMeta, {
2121
+ this.mapJoinedProp(relationPojo, prop, this.aliasedKeys(prop, childAlias), root, tz, currentMeta, {
2113
2122
  deleteFromRoot: true,
2114
2123
  });
2115
2124
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/sql",
3
- "version": "7.2.10-dev.1",
3
+ "version": "7.2.10-dev.2",
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.2.9"
54
54
  },
55
55
  "peerDependencies": {
56
- "@mikro-orm/core": "7.2.10-dev.1"
56
+ "@mikro-orm/core": "7.2.10-dev.2"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"
@@ -1,5 +1,5 @@
1
1
  var _a;
2
- import { EntityMetadata, helper, inspect, isRaw, LoadStrategy, LockMode, PopulateHint, QueryFlag, QueryHelper, raw, RawQueryFragment, Reference, ReferenceKind, serialize, SUBQUERY_BRAND, Utils, ValidationError, } from '@mikro-orm/core';
2
+ import { EntityMetadata, helper, inspect, isRaw, LoadStrategy, LockMode, PopulateHint, QueryFlag, QueryHelper, raw, RawQueryFragment, Reference, ReferenceKind, serialize, SUBQUERY_BRAND, Utils, validatePaginationValue, ValidationError, } from '@mikro-orm/core';
3
3
  import { JoinType, QueryType } from './enums.js';
4
4
  import { QueryBuilderHelper } from './QueryBuilderHelper.js';
5
5
  import { CriteriaNodeFactory } from './CriteriaNodeFactory.js';
@@ -737,7 +737,7 @@ export class QueryBuilder {
737
737
  */
738
738
  limit(limit, offset = 0) {
739
739
  this.ensureNotFinalized();
740
- this.#state.limit = limit;
740
+ this.#state.limit = validatePaginationValue(limit, 'limit');
741
741
  if (offset) {
742
742
  this.offset(offset);
743
743
  }
@@ -753,7 +753,7 @@ export class QueryBuilder {
753
753
  */
754
754
  offset(offset) {
755
755
  this.ensureNotFinalized();
756
- this.#state.offset = offset;
756
+ this.#state.offset = validatePaginationValue(offset, 'offset');
757
757
  return this;
758
758
  }
759
759
  withSchema(schema) {