@mikro-orm/knex 6.6.18-dev.1 → 6.6.18-dev.3

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.
@@ -670,7 +670,10 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
670
670
  sql = sql.substring(0, sql.length - 2) + ' where ';
671
671
  const pkProps = meta.primaryKeys.concat(...meta.concurrencyCheckKeys);
672
672
  const pks = core_1.Utils.flatten(pkProps.map(pk => meta.properties[pk].fieldNames));
673
- sql += pks.length > 1 ? `(${pks.map(pk => `${this.platform.quoteIdentifier(pk)}`).join(', ')})` : this.platform.quoteIdentifier(pks[0]);
673
+ const useTupleIn = pks.length <= 1 || this.platform.allowsComparingTuples();
674
+ const condTemplate = useTupleIn
675
+ ? `(${pks.map(() => '?').join(', ')})`
676
+ : `(${pks.map(pk => `${this.platform.quoteIdentifier(pk)} = ?`).join(' and ')})`;
674
677
  const conds = where.map(cond => {
675
678
  if (core_1.Utils.isPlainObject(cond) && core_1.Utils.getObjectKeysSize(cond) === 1) {
676
679
  cond = Object.values(cond)[0];
@@ -684,12 +687,18 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
684
687
  params.push(cond[pk]);
685
688
  }
686
689
  });
687
- return `(${Array.from({ length: pks.length }).fill('?').join(', ')})`;
690
+ return condTemplate;
688
691
  }
689
692
  params.push(cond);
690
693
  return '?';
691
694
  });
692
- sql += ` in (${conds.join(', ')})`;
695
+ if (useTupleIn) {
696
+ sql += pks.length > 1 ? `(${pks.map(pk => `${this.platform.quoteIdentifier(pk)}`).join(', ')})` : this.platform.quoteIdentifier(pks[0]);
697
+ sql += ` in (${conds.join(', ')})`;
698
+ }
699
+ else {
700
+ sql += conds.join(' or ');
701
+ }
693
702
  if (this.platform.usesReturningStatement() && returning.size > 0) {
694
703
  const returningFields = core_1.Utils.flatten([...returning].map(prop => meta.properties[prop].fieldNames));
695
704
  /* istanbul ignore next */
@@ -842,7 +851,11 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
842
851
  map[key] = [];
843
852
  }
844
853
  for (const item of res) {
845
- const key = core_1.Utils.getPrimaryKeyHash(core_1.Utils.asArray(item[pivotProp2.name]));
854
+ const fk = item[pivotProp2.name];
855
+ // the owner PKs are always flat, while the pivot FK follows the owner PK structure,
856
+ // so a PK built from a relation to another composite PK entity needs flattening too
857
+ const pks = fk != null ? core_1.Utils.getOrderedPrimaryKeys(fk, ownerMeta) : core_1.Utils.asArray(fk);
858
+ const key = core_1.Utils.getPrimaryKeyHash(pks);
846
859
  map[key].push(item[pivotProp1.name]);
847
860
  }
848
861
  return map;
@@ -917,8 +930,24 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
917
930
  const [propName, ref] = hint.field.split(':', 2);
918
931
  return { propName, ref, children: hint.children };
919
932
  });
933
+ // with `fixedOrder` the pivot PK is the order column, which is not guaranteed to be unique when the
934
+ // pivot table is managed externally, so we disambiguate the rows by their FKs on top of the PK
935
+ const pivotRelations = meta.pivotTable && !meta.compositePK
936
+ ? meta.relations.filter(p => p.kind === core_1.ReferenceKind.MANY_TO_ONE && p.persist !== false)
937
+ : [];
920
938
  for (const item of rawResults) {
921
- const pk = core_1.Utils.getCompositeKeyHash(item, meta);
939
+ // flat hash, so nested composite PK values keep their own separators and cannot collide
940
+ let pk = core_1.Utils.getCompositeKeyHash(item, meta, false, undefined, true);
941
+ if (pivotRelations.length > 0) {
942
+ pk = core_1.Utils.getPrimaryKeyHash([
943
+ pk,
944
+ ...pivotRelations.flatMap(p => {
945
+ const value = item[p.name];
946
+ // composite FKs are mapped to an array of values, which `extractPK` does not accept
947
+ return (Array.isArray(value) ? core_1.Utils.flatten(value, true) : core_1.Utils.extractPK(value, p.targetMeta));
948
+ }),
949
+ ]);
950
+ }
922
951
  if (map[pk]) {
923
952
  for (const { propName } of hints) {
924
953
  if (!item[propName]) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "6.6.18-dev.1",
3
+ "version": "6.6.18-dev.3",
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
  "main": "index.js",
6
6
  "module": "index.mjs",
@@ -66,7 +66,7 @@
66
66
  "@mikro-orm/core": "^6.6.17"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "6.6.18-dev.1",
69
+ "@mikro-orm/core": "6.6.18-dev.3",
70
70
  "better-sqlite3": "*",
71
71
  "libsql": "*",
72
72
  "mariadb": "*"