metal-orm 1.0.100 → 1.0.102

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.js CHANGED
@@ -4085,6 +4085,37 @@ var collectFromOrderingTerm = (term, collector) => {
4085
4085
  };
4086
4086
 
4087
4087
  // src/query-builder/relation-join-planner.ts
4088
+ var getExposedName = (ts) => {
4089
+ if (ts.type === "Table") return ts.alias ?? ts.name;
4090
+ if (ts.type === "DerivedTable") return ts.alias;
4091
+ if (ts.type === "FunctionTable") return ts.alias ?? ts.name;
4092
+ return null;
4093
+ };
4094
+ var collectExposedNames = (state) => {
4095
+ const used = /* @__PURE__ */ new Set();
4096
+ const fromName = getExposedName(state.ast.from);
4097
+ if (fromName) used.add(fromName);
4098
+ for (const j of state.ast.joins) {
4099
+ const n = getExposedName(j.table);
4100
+ if (n) used.add(n);
4101
+ }
4102
+ return used;
4103
+ };
4104
+ var makeUniqueAlias = (base, used) => {
4105
+ let alias = base;
4106
+ let i = 2;
4107
+ while (used.has(alias)) alias = `${base}_${i++}`;
4108
+ return alias;
4109
+ };
4110
+ var ensureCorrelationName = (state, relationName, ts, extraUsed) => {
4111
+ if (ts.type !== "Table") return ts;
4112
+ if (ts.alias) return ts;
4113
+ const used = collectExposedNames(state);
4114
+ for (const x of extraUsed ?? []) used.add(x);
4115
+ if (!used.has(ts.name)) return ts;
4116
+ const alias = makeUniqueAlias(relationName, used);
4117
+ return { ...ts, alias };
4118
+ };
4088
4119
  var RelationJoinPlanner = class {
4089
4120
  constructor(table, createQueryAstService) {
4090
4121
  this.table = table;
@@ -4093,11 +4124,17 @@ var RelationJoinPlanner = class {
4093
4124
  withJoin(state, relationName, relation, joinKind, extraCondition, tableSource) {
4094
4125
  const rootAlias = state.ast.from.type === "Table" ? state.ast.from.alias : void 0;
4095
4126
  if (relation.type === RelationKinds.BelongsToMany) {
4096
- const targetTableSource = tableSource ?? {
4127
+ let targetTableSource = tableSource ?? {
4097
4128
  type: "Table",
4098
4129
  name: relation.target.name,
4099
4130
  schema: relation.target.schema
4100
4131
  };
4132
+ targetTableSource = ensureCorrelationName(
4133
+ state,
4134
+ relationName,
4135
+ targetTableSource,
4136
+ [relation.pivotTable.name]
4137
+ );
4101
4138
  const targetName2 = this.resolveTargetTableName(targetTableSource, relation);
4102
4139
  const joins = buildBelongsToManyJoins(
4103
4140
  this.table,
@@ -4111,11 +4148,12 @@ var RelationJoinPlanner = class {
4111
4148
  );
4112
4149
  return joins.reduce((current, join) => this.astService(current).withJoin(join), state);
4113
4150
  }
4114
- const targetTable = tableSource ?? {
4151
+ let targetTable = tableSource ?? {
4115
4152
  type: "Table",
4116
4153
  name: relation.target.name,
4117
4154
  schema: relation.target.schema
4118
4155
  };
4156
+ targetTable = ensureCorrelationName(state, relationName, targetTable);
4119
4157
  const targetName = this.resolveTargetTableName(targetTable, relation);
4120
4158
  const condition = buildRelationJoinCondition(
4121
4159
  this.table,
@@ -4191,13 +4229,22 @@ var RelationCteBuilder = class {
4191
4229
  };
4192
4230
 
4193
4231
  // src/query-builder/relation-include-strategies.ts
4194
- var buildTypedSelection = (columns, prefix, keys, missingMsg) => {
4232
+ var getJoinCorrelationName = (state, relationName, fallback) => {
4233
+ const join = state.ast.joins.find((j) => getJoinRelationName(j) === relationName);
4234
+ if (!join) return fallback;
4235
+ const t = join.table;
4236
+ if (t.type === "Table") return t.alias ?? t.name;
4237
+ if (t.type === "DerivedTable") return t.alias;
4238
+ if (t.type === "FunctionTable") return t.alias ?? fallback;
4239
+ return fallback;
4240
+ };
4241
+ var buildTypedSelection = (columns, prefix, keys, missingMsg, tableOverride) => {
4195
4242
  return keys.reduce((acc, key) => {
4196
4243
  const def = columns[key];
4197
4244
  if (!def) {
4198
4245
  throw new Error(missingMsg(key));
4199
4246
  }
4200
- acc[makeRelationAlias(prefix, key)] = def;
4247
+ acc[makeRelationAlias(prefix, key)] = tableOverride ? { ...def, table: tableOverride } : def;
4201
4248
  return acc;
4202
4249
  }, {});
4203
4250
  };
@@ -4234,11 +4281,13 @@ var standardIncludeStrategy = (context) => {
4234
4281
  state = fkSelectionResult.state;
4235
4282
  hydration = fkSelectionResult.hydration;
4236
4283
  const targetColumns = resolveTargetColumns(relation, context.options);
4284
+ const tableOverride = getJoinCorrelationName(state, context.relationName, relation.target.name);
4237
4285
  const targetSelection = buildTypedSelection(
4238
4286
  relation.target.columns,
4239
4287
  context.aliasPrefix,
4240
4288
  targetColumns,
4241
- (key) => `Column '${key}' not found on relation '${context.relationName}'`
4289
+ (key) => `Column '${key}' not found on relation '${context.relationName}'`,
4290
+ tableOverride
4242
4291
  );
4243
4292
  const relationSelectionResult = context.selectColumns(state, hydration, targetSelection);
4244
4293
  state = relationSelectionResult.state;
@@ -4256,11 +4305,13 @@ var belongsToManyStrategy = (context) => {
4256
4305
  const relation = context.relation;
4257
4306
  let { state, hydration } = context;
4258
4307
  const targetColumns = resolveTargetColumns(relation, context.options);
4308
+ const tableOverride = getJoinCorrelationName(state, context.relationName, relation.target.name);
4259
4309
  const targetSelection = buildTypedSelection(
4260
4310
  relation.target.columns,
4261
4311
  context.aliasPrefix,
4262
4312
  targetColumns,
4263
- (key) => `Column '${key}' not found on relation '${context.relationName}'`
4313
+ (key) => `Column '${key}' not found on relation '${context.relationName}'`,
4314
+ tableOverride
4264
4315
  );
4265
4316
  const pivotAliasPrefix = context.options?.pivot?.aliasPrefix ?? `${context.aliasPrefix}_pivot`;
4266
4317
  const pivotPk = relation.pivotPrimaryKey || findPrimaryKey(relation.pivotTable);