metal-orm 1.0.101 → 1.0.103

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/dist/index.cjs CHANGED
@@ -4444,6 +4444,37 @@ var collectFromOrderingTerm = (term, collector) => {
4444
4444
  };
4445
4445
 
4446
4446
  // src/query-builder/relation-join-planner.ts
4447
+ var getExposedName = (ts) => {
4448
+ if (ts.type === "Table") return ts.alias ?? ts.name;
4449
+ if (ts.type === "DerivedTable") return ts.alias;
4450
+ if (ts.type === "FunctionTable") return ts.alias ?? ts.name;
4451
+ return null;
4452
+ };
4453
+ var collectExposedNames = (state) => {
4454
+ const used = /* @__PURE__ */ new Set();
4455
+ const fromName = getExposedName(state.ast.from);
4456
+ if (fromName) used.add(fromName);
4457
+ for (const j of state.ast.joins) {
4458
+ const n = getExposedName(j.table);
4459
+ if (n) used.add(n);
4460
+ }
4461
+ return used;
4462
+ };
4463
+ var makeUniqueAlias = (base, used) => {
4464
+ let alias = base;
4465
+ let i = 2;
4466
+ while (used.has(alias)) alias = `${base}_${i++}`;
4467
+ return alias;
4468
+ };
4469
+ var ensureCorrelationName = (state, relationName, ts, extraUsed) => {
4470
+ if (ts.type !== "Table") return ts;
4471
+ if (ts.alias) return ts;
4472
+ const used = collectExposedNames(state);
4473
+ for (const x of extraUsed ?? []) used.add(x);
4474
+ if (!used.has(ts.name)) return ts;
4475
+ const alias = makeUniqueAlias(relationName, used);
4476
+ return { ...ts, alias };
4477
+ };
4447
4478
  var RelationJoinPlanner = class {
4448
4479
  constructor(table, createQueryAstService) {
4449
4480
  this.table = table;
@@ -4452,11 +4483,17 @@ var RelationJoinPlanner = class {
4452
4483
  withJoin(state, relationName, relation, joinKind, extraCondition, tableSource) {
4453
4484
  const rootAlias = state.ast.from.type === "Table" ? state.ast.from.alias : void 0;
4454
4485
  if (relation.type === RelationKinds.BelongsToMany) {
4455
- const targetTableSource = tableSource ?? {
4486
+ let targetTableSource = tableSource ?? {
4456
4487
  type: "Table",
4457
4488
  name: relation.target.name,
4458
4489
  schema: relation.target.schema
4459
4490
  };
4491
+ targetTableSource = ensureCorrelationName(
4492
+ state,
4493
+ relationName,
4494
+ targetTableSource,
4495
+ [relation.pivotTable.name]
4496
+ );
4460
4497
  const targetName2 = this.resolveTargetTableName(targetTableSource, relation);
4461
4498
  const joins = buildBelongsToManyJoins(
4462
4499
  this.table,
@@ -4470,11 +4507,12 @@ var RelationJoinPlanner = class {
4470
4507
  );
4471
4508
  return joins.reduce((current, join) => this.astService(current).withJoin(join), state);
4472
4509
  }
4473
- const targetTable = tableSource ?? {
4510
+ let targetTable = tableSource ?? {
4474
4511
  type: "Table",
4475
4512
  name: relation.target.name,
4476
4513
  schema: relation.target.schema
4477
4514
  };
4515
+ targetTable = ensureCorrelationName(state, relationName, targetTable);
4478
4516
  const targetName = this.resolveTargetTableName(targetTable, relation);
4479
4517
  const condition = buildRelationJoinCondition(
4480
4518
  this.table,
@@ -4550,13 +4588,22 @@ var RelationCteBuilder = class {
4550
4588
  };
4551
4589
 
4552
4590
  // src/query-builder/relation-include-strategies.ts
4553
- var buildTypedSelection = (columns, prefix, keys, missingMsg) => {
4591
+ var getJoinCorrelationName = (state, relationName, fallback) => {
4592
+ const join = state.ast.joins.find((j) => getJoinRelationName(j) === relationName);
4593
+ if (!join) return fallback;
4594
+ const t = join.table;
4595
+ if (t.type === "Table") return t.alias ?? t.name;
4596
+ if (t.type === "DerivedTable") return t.alias;
4597
+ if (t.type === "FunctionTable") return t.alias ?? fallback;
4598
+ return fallback;
4599
+ };
4600
+ var buildTypedSelection = (columns, prefix, keys, missingMsg, tableOverride) => {
4554
4601
  return keys.reduce((acc, key) => {
4555
4602
  const def = columns[key];
4556
4603
  if (!def) {
4557
4604
  throw new Error(missingMsg(key));
4558
4605
  }
4559
- acc[makeRelationAlias(prefix, key)] = def;
4606
+ acc[makeRelationAlias(prefix, key)] = tableOverride ? { ...def, table: tableOverride } : def;
4560
4607
  return acc;
4561
4608
  }, {});
4562
4609
  };
@@ -4593,11 +4640,13 @@ var standardIncludeStrategy = (context) => {
4593
4640
  state = fkSelectionResult.state;
4594
4641
  hydration = fkSelectionResult.hydration;
4595
4642
  const targetColumns = resolveTargetColumns(relation, context.options);
4643
+ const tableOverride = getJoinCorrelationName(state, context.relationName, relation.target.name);
4596
4644
  const targetSelection = buildTypedSelection(
4597
4645
  relation.target.columns,
4598
4646
  context.aliasPrefix,
4599
4647
  targetColumns,
4600
- (key) => `Column '${key}' not found on relation '${context.relationName}'`
4648
+ (key) => `Column '${key}' not found on relation '${context.relationName}'`,
4649
+ tableOverride
4601
4650
  );
4602
4651
  const relationSelectionResult = context.selectColumns(state, hydration, targetSelection);
4603
4652
  state = relationSelectionResult.state;
@@ -4615,11 +4664,13 @@ var belongsToManyStrategy = (context) => {
4615
4664
  const relation = context.relation;
4616
4665
  let { state, hydration } = context;
4617
4666
  const targetColumns = resolveTargetColumns(relation, context.options);
4667
+ const tableOverride = getJoinCorrelationName(state, context.relationName, relation.target.name);
4618
4668
  const targetSelection = buildTypedSelection(
4619
4669
  relation.target.columns,
4620
4670
  context.aliasPrefix,
4621
4671
  targetColumns,
4622
- (key) => `Column '${key}' not found on relation '${context.relationName}'`
4672
+ (key) => `Column '${key}' not found on relation '${context.relationName}'`,
4673
+ tableOverride
4623
4674
  );
4624
4675
  const pivotAliasPrefix = context.options?.pivot?.aliasPrefix ?? `${context.aliasPrefix}_pivot`;
4625
4676
  const pivotPk = relation.pivotPrimaryKey || findPrimaryKey(relation.pivotTable);