@dbsp/core 6.0.0 → 6.1.0

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.
@@ -2673,14 +2673,16 @@ interface QueryBuilder<TResult = unknown> {
2673
2673
  * Add an explicit SQL JOIN clause to the query (non-hydrating, flat result).
2674
2674
  *
2675
2675
  * Two modes based on whether `opts.on` is provided:
2676
- * - **Relation mode** (no `on`): FK auto-resolved from the model, like `include` but flat.
2676
+ * - **Relation mode** (no `on`): FK auto-resolved from the model, like `include` but flat. A dotted relation
2677
+ * path joins one range per relation, each correlated to the previous one on its declared keys.
2677
2678
  * - **Table mode** (`on` required): Explicit table + ON condition. Use `as` for self-joins.
2678
2679
  *
2679
- * @param relationOrTable - Relation name (FK mode) or table name (explicit mode)
2680
+ * @param relationOrTable - Relation name or dotted relation path (FK mode) or table name (explicit mode)
2680
2681
  * @param opts - Optional join configuration
2681
- * @param opts.type - Join type: `'inner'` (default) or `'left'`
2682
+ * @param opts.type - Join type: `'inner'` (default) or `'left'`, applied to every hop of a path
2682
2683
  * @param opts.on - ON condition (required for table mode, absent for relation mode)
2683
- * @param opts.as - Alias for the joined table (required for self-joins)
2684
+ * @param opts.as - Alias for the joined table (required for self-joins). In relation mode, a string names the
2685
+ * last hop and a list names every hop; unnamed hops are aliased by their path prefix joined with `_`.
2684
2686
  * @returns A new QueryBuilder with the join appended
2685
2687
  *
2686
2688
  * @example
@@ -2688,6 +2690,9 @@ interface QueryBuilder<TResult = unknown> {
2688
2690
  * // Relation mode — FK auto-resolved
2689
2691
  * orm.from(calls).join('caller')
2690
2692
  * orm.from(calls).join('callerFile', { type: 'left' })
2693
+ * // Relation path — JOIN symbols AS callee …, JOIN files AS callee_file ON callee.file_id = callee_file.id
2694
+ * orm.from(calls).join('callee.file', { type: 'left' })
2695
+ * orm.from(calls).join('callee.file', { as: ['c', 'cf'] })
2691
2696
  *
2692
2697
  * // Table mode — explicit ON condition
2693
2698
  * orm.from(embeddings).join('embeddings', {
@@ -2697,6 +2702,11 @@ interface QueryBuilder<TResult = unknown> {
2697
2702
  * })
2698
2703
  * ```
2699
2704
  */
2705
+ join(relationPath: string, opts?: {
2706
+ type?: 'inner' | 'left';
2707
+ as?: string | readonly string[];
2708
+ on?: never;
2709
+ }): QueryBuilder<TResult>;
2700
2710
  join(relationOrTable: string, opts?: {
2701
2711
  type?: 'inner' | 'left';
2702
2712
  on?: WhereIntent$1;
@@ -3596,6 +3596,29 @@ import {
3596
3596
  resolveDeclaredRelationPath as resolveDeclaredRelationPath2,
3597
3597
  resolveIncludeRelationName
3598
3598
  } from "@dbsp/types/internal";
3599
+
3600
+ // src/dx/relation-join-path.ts
3601
+ function relationJoinHops(relation, alias) {
3602
+ const segments = relation.split(".");
3603
+ for (const segment of segments) validateIdentifier(segment, "relation");
3604
+ if (alias !== void 0 && typeof alias !== "string") {
3605
+ if (alias.length !== segments.length)
3606
+ throw new Error(
3607
+ `join('${relation}'): 'as' lists ${alias.length} aliases for ${segments.length} hops.`
3608
+ );
3609
+ }
3610
+ const last = segments.length - 1;
3611
+ const aliases = segments.map((_, k) => {
3612
+ if (alias !== void 0 && typeof alias !== "string")
3613
+ return alias[k];
3614
+ if (k === last && typeof alias === "string") return alias;
3615
+ return segments.slice(0, k + 1).join("_");
3616
+ });
3617
+ for (const hopAlias of aliases) validateIdentifier(hopAlias, "alias");
3618
+ return { segments, aliases };
3619
+ }
3620
+
3621
+ // src/resolved-includes.ts
3599
3622
  function assertSupportedIncludeWhere(includes, strategies, parent = "", intentParent = "", parentStrategy) {
3600
3623
  for (const [index, include] of (includes ?? []).entries()) {
3601
3624
  const path = `${parent}include[${index}](${include.relation})`;
@@ -3658,72 +3681,98 @@ function resolveReportIncludes(intent, decisions, model, options = {}) {
3658
3681
  const visibleJoinRanges = [rootRange];
3659
3682
  const firstJoinByQualifier = /* @__PURE__ */ new Map();
3660
3683
  (intent.joins ?? []).forEach((join, index) => {
3661
- const qualifier = join.alias ?? join.relation ?? join.batchValues?.alias ?? join.table;
3662
- if (!firstJoinByQualifier.has(qualifier))
3663
- firstJoinByQualifier.set(qualifier, index);
3684
+ const qualifiers = join.relation !== void 0 ? relationJoinHops(join.relation, join.alias).aliases : [join.alias ?? join.batchValues?.alias ?? join.table];
3685
+ for (const qualifier of qualifiers)
3686
+ if (!firstJoinByQualifier.has(qualifier))
3687
+ firstJoinByQualifier.set(qualifier, index);
3664
3688
  });
3665
- const joins = (intent.joins ?? []).map(
3666
- (join, intentIndex) => {
3667
- const alias = join.alias ?? join.relation ?? join.batchValues?.alias ?? join.table;
3668
- let table = join.table ?? alias;
3669
- let path;
3670
- if (join.relation !== void 0) {
3671
- if (!model)
3672
- throw new Error(
3673
- `join('${join.relation}'): relation-mode join requires a model for FK resolution.`
3674
- );
3675
- const relations = model.getRelationsFrom(intent.from);
3676
- const relation = relations.find((r) => r.name === join.relation);
3677
- if (!relation)
3678
- throw new Error(
3679
- `join('${join.relation}'): relation not found on table '${intent.from}'. Available: ${relations.map((r) => r.name).join(", ")}`
3680
- );
3681
- if (relation.type === "belongsToMany")
3682
- throw new InvalidOperationError(
3683
- "relation",
3684
- belongsToManyJoinIncludeRefusal(`${intent.from}.${relation.name}`)
3685
- );
3686
- const resolved = resolveDeclaredRelationPath2(model, intent.from, [
3687
- join.relation
3688
- ]);
3689
- if (!resolved.ok)
3690
- throw new Error(
3691
- `join('${join.relation}'): relation not found on table '${intent.from}'. Available: ${relations.map((r) => r.name).join(", ")}`
3692
- );
3693
- path = resolved;
3694
- table = resolved.targetTable;
3695
- }
3696
- const range = allocator.bind(table, alias);
3697
- allocator.reserve(range.alias);
3698
- visibleJoinRanges.push(range);
3699
- const defaultFilter = join.batchValues ? void 0 : resolveScanDefaultFilter(options.defaultFilters, range, model);
3700
- const on = resolveConditionContext(
3701
- join.on,
3702
- rootRange,
3703
- [...visibleJoinRanges],
3704
- [[range]],
3705
- allocator,
3706
- model,
3707
- 0,
3708
- [],
3709
- {
3710
- defaultFilters: options.defaultFilters,
3711
- subqueryRefusal: join.batchValues ? "Subquery in BatchValues JOIN ON condition is not supported." : "Subquery in JOIN ON condition is not supported.",
3712
- firstJoinByQualifier,
3713
- intentIndex
3714
- }
3689
+ const resolveRelationJoinHops = (relationPath, alias) => {
3690
+ if (!model)
3691
+ throw new Error(
3692
+ `join('${relationPath}'): relation-mode join requires a model for FK resolution.`
3693
+ );
3694
+ const { segments, aliases } = relationJoinHops(relationPath, alias);
3695
+ const notFound = (segmentIndex, table2) => {
3696
+ const available = model.getRelationsFrom(table2).map((r) => r.name).join(", ");
3697
+ return new Error(
3698
+ segmentIndex === 0 ? `join('${relationPath}'): relation not found on table '${table2}'. Available: ${available}` : `join('${relationPath}'): relation '${segments[segmentIndex]}' not found on table '${table2}'. Available: ${available}`
3699
+ );
3700
+ };
3701
+ let table = intent.from;
3702
+ for (const [segmentIndex, segment] of segments.entries()) {
3703
+ const relation = model.getRelationsFrom(table).find((r) => r.name === segment);
3704
+ if (!relation) throw notFound(segmentIndex, table);
3705
+ if (relation.type === "belongsToMany")
3706
+ throw new InvalidOperationError(
3707
+ "relation",
3708
+ belongsToManyJoinIncludeRefusal(`${table}.${relation.name}`)
3709
+ );
3710
+ table = relation.target;
3711
+ }
3712
+ const resolved = resolveDeclaredRelationPath2(model, intent.from, segments);
3713
+ if (!resolved.ok)
3714
+ throw notFound(resolved.segmentIndex, resolved.sourceTable);
3715
+ if (resolved.hops.length !== segments.length || resolved.hops.some((hop, k) => hop.segmentIndex !== k))
3716
+ throw new Error(
3717
+ `join('${relationPath}'): the resolved relation path does not hold one hop per segment.`
3715
3718
  );
3719
+ return segments.map((_, k) => {
3720
+ const target = resolved.relations[k].target;
3716
3721
  return {
3717
- intentPath: `join[${intentIndex}]`,
3718
- intentIndex,
3719
- kind: join.relation !== void 0 ? "relation" : join.batchValues ? "values" : "table",
3720
- type: join.type,
3721
- range,
3722
- sourceRange: rootRange,
3723
- ...path && { path },
3724
- ...on && { on },
3725
- ...defaultFilter && { defaultFilter }
3722
+ table: target,
3723
+ alias: aliases[k],
3724
+ path: {
3725
+ ok: true,
3726
+ logicalSegments: segments.slice(0, k + 1),
3727
+ relations: resolved.relations.slice(0, k + 1),
3728
+ hops: resolved.hops.slice(0, k + 1),
3729
+ targetTable: target
3730
+ }
3726
3731
  };
3732
+ });
3733
+ };
3734
+ const joins = (intent.joins ?? []).flatMap(
3735
+ (join, intentIndex) => {
3736
+ const hops = join.relation !== void 0 ? resolveRelationJoinHops(join.relation, join.alias) : (() => {
3737
+ const alias = join.alias ?? join.batchValues?.alias ?? join.table;
3738
+ return [{ table: join.table ?? alias, alias, path: void 0 }];
3739
+ })();
3740
+ let sourceRange = rootRange;
3741
+ return hops.map((hop) => {
3742
+ const range = allocator.bind(hop.table, hop.alias);
3743
+ allocator.reserve(range.alias);
3744
+ visibleJoinRanges.push(range);
3745
+ const defaultFilter = join.batchValues ? void 0 : resolveScanDefaultFilter(options.defaultFilters, range, model);
3746
+ const on = resolveConditionContext(
3747
+ join.on,
3748
+ rootRange,
3749
+ [...visibleJoinRanges],
3750
+ [[range]],
3751
+ allocator,
3752
+ model,
3753
+ 0,
3754
+ [],
3755
+ {
3756
+ defaultFilters: options.defaultFilters,
3757
+ subqueryRefusal: join.batchValues ? "Subquery in BatchValues JOIN ON condition is not supported." : "Subquery in JOIN ON condition is not supported.",
3758
+ firstJoinByQualifier,
3759
+ intentIndex
3760
+ }
3761
+ );
3762
+ const resolvedJoin = {
3763
+ intentPath: `join[${intentIndex}]`,
3764
+ intentIndex,
3765
+ kind: join.relation !== void 0 ? "relation" : join.batchValues ? "values" : "table",
3766
+ type: join.type,
3767
+ range,
3768
+ sourceRange,
3769
+ ...hop.path && { path: hop.path },
3770
+ ...on && { on },
3771
+ ...defaultFilter && { defaultFilter }
3772
+ };
3773
+ sourceRange = range;
3774
+ return resolvedJoin;
3775
+ });
3727
3776
  }
3728
3777
  );
3729
3778
  const generatedAliases = /* @__PURE__ */ new Map();
@@ -23616,8 +23665,13 @@ var QueryBuilderImpl = class _QueryBuilderImpl {
23616
23665
  join(relationOrTableOrBatch, opts) {
23617
23666
  const builder = this.clone();
23618
23667
  const type = opts?.type ?? "inner";
23668
+ const alias = opts?.as;
23619
23669
  if (isBatchValuesRef(relationOrTableOrBatch)) {
23620
23670
  const bv = relationOrTableOrBatch;
23671
+ if (alias !== void 0 && typeof alias !== "string")
23672
+ throw new Error(
23673
+ `join('${bv.alias}'): an alias list requires a relation path.`
23674
+ );
23621
23675
  if (!opts?.on) {
23622
23676
  throw new Error(
23623
23677
  "join(batchValuesRef): an `on` condition is required for BatchValues joins"
@@ -23627,8 +23681,8 @@ var QueryBuilderImpl = class _QueryBuilderImpl {
23627
23681
  for (const col2 of bv.columns) {
23628
23682
  validateIdentifier(col2, "column");
23629
23683
  }
23630
- if (opts.as !== void 0) {
23631
- validateIdentifier(opts.as, "alias");
23684
+ if (alias !== void 0) {
23685
+ validateIdentifier(alias, "alias");
23632
23686
  }
23633
23687
  const joinIntent = {
23634
23688
  batchValues: {
@@ -23640,25 +23694,31 @@ var QueryBuilderImpl = class _QueryBuilderImpl {
23640
23694
  },
23641
23695
  on: opts.on,
23642
23696
  type,
23643
- ...opts.as !== void 0 ? { alias: opts.as } : { alias: bv.alias }
23697
+ alias: alias ?? bv.alias
23644
23698
  };
23645
23699
  builder.joinIntents.push(joinIntent);
23646
- } else {
23700
+ } else if (opts?.on) {
23647
23701
  validateIdentifier(relationOrTableOrBatch, "table");
23648
- if (opts?.as !== void 0) {
23649
- validateIdentifier(opts.as, "alias");
23650
- }
23651
- const joinIntent = opts?.on ? {
23702
+ if (alias !== void 0 && typeof alias !== "string")
23703
+ throw new Error(
23704
+ `join('${relationOrTableOrBatch}'): an alias list requires a relation path.`
23705
+ );
23706
+ if (alias !== void 0) validateIdentifier(alias, "alias");
23707
+ builder.joinIntents.push({
23652
23708
  table: relationOrTableOrBatch,
23653
23709
  on: opts.on,
23654
23710
  type,
23655
- ...opts.as !== void 0 && { alias: opts.as }
23656
- } : {
23711
+ ...alias !== void 0 && { alias }
23712
+ });
23713
+ } else {
23714
+ relationJoinHops(relationOrTableOrBatch, alias);
23715
+ builder.joinIntents.push({
23657
23716
  relation: relationOrTableOrBatch,
23658
23717
  type,
23659
- ...opts?.as !== void 0 && { alias: opts.as }
23660
- };
23661
- builder.joinIntents.push(joinIntent);
23718
+ ...alias !== void 0 && {
23719
+ alias: typeof alias === "string" ? alias : Object.freeze([...alias])
23720
+ }
23721
+ });
23662
23722
  }
23663
23723
  return builder;
23664
23724
  }
@@ -26822,4 +26882,4 @@ export {
26822
26882
  runAssertions,
26823
26883
  isOverallSuccess
26824
26884
  };
26825
- //# sourceMappingURL=chunk-UATOOZ2G.js.map
26885
+ //# sourceMappingURL=chunk-SDUWYSS7.js.map