1 import { CrossModuleJoinSpec, RemoteJoinerQuery } from "@medusajs/types"
2 import { GraphCatalog } from "../catalog"
3 import {
4 InternalJoinerServiceConfig,
5 ResidualCrossModuleFilter,
6 ResidualHiddenProperty,
7 ResidualOrderBy,
8 } from "../types"
9 import {
10 consumeResidualFilters,
11 createConsumeContext,
12 } from "./residual-filters"
13 import { consumeResidualOrderBy } from "./residual-order"
14 import {
15 rewriteExpandFilters,
16 rewriteRootFilters,
17 } from "./rewrite-filters"
18 import { rewriteRootOrder } from "./rewrite-order"
19 import { SpecRegistry } from "./types"
20
21 /**
22 * Stage-1 cross-module filtering/sorting (SQL pushdown).
23 *
24 * Rewrites eligible cross-module filter/sort paths on the joiner query into
25 * {@link CrossModuleJoinSpec}s that the root module's DAL turns into
26 * correlated EXISTS/scalar subqueries (see
27 * `augmentFindOptionsWithCrossModuleJoins`). Pushed-down filters restrict the
28 * ROOT rows — matching `query.index` semantics — and are pruned from the
29 * query. Expand nodes that existed only to carry those filters are dropped so
30 * they never trigger a fetch.
31 *
32 * A path is eligible when every hop from the root goes through a link module
33 * (link table -> target entity pairs), a read-only link FK, or a
34 * module-internal DML relation, all parties live in the same database, and
35 * every filtered/sorted field is `crossjoinable` (a non-computed DML column)
36 * on its target entity. Anything else — computed fields, unsupported
37 * operators, missing metadata, schema-remapped relations — is reported as
38 * residual and left untouched on the query. Compile then hands
39 * `residualCrossModuleFilters` to the in-memory stage (stage 2, see
40 * `residual-filters.ts`), which strips them from the query and completes
41 * them after the fetch.
42 *
43 * Pipeline per filter/sort location:
44 *
45 * resolveChain (path -> hops -> spec chain)
46 * -> collectCandidates (filters object -> per-level candidates)
47 * -> registerCandidates (dedupe + unique-target-table constraint)
48 * -> prune the pushed filters from the query
49 */
50 export function extractCrossModuleJoins(
51 {
52 query,
53 serviceConfig,
54 }: {
55 query: RemoteJoinerQuery
56 serviceConfig: InternalJoinerServiceConfig
57 },
58 catalog: GraphCatalog
59 ): {
60 crossModuleJoins: CrossModuleJoinSpec[]
61 residualCrossModuleFilters: ResidualCrossModuleFilter[]
62 residualOrderBy: ResidualOrderBy[]
63 } {
64 const registry: SpecRegistry = {}
65 const residual: ResidualCrossModuleFilter[] = []
66
67 rewriteRootFilters({ query, serviceConfig, catalog, registry, residual })
68 rewriteExpandFilters({ query, serviceConfig, catalog, registry, residual })
69 const residualOrderBy = rewriteRootOrder({
70 query,
71 serviceConfig,
72 catalog,
73 registry,
74 })
75
76 return {
77 crossModuleJoins: Object.values(registry),
78 residualCrossModuleFilters: residual,
79 residualOrderBy,
80 }
81 }
82
83 /**
84 * Stage-2 compile step: consumes the residual filters and order keys reported
85 * by {@link extractCrossModuleJoins}. Strips the filters from the query,
86 * loads the data both need through injected expands (see `residual-filters.ts`
87 * and `residual-order.ts`), and returns the properties added only for
88 * in-memory evaluation so executePlan can hide them.
89 */
90 export function consumeResiduals(
91 params: {
92 query: RemoteJoinerQuery
93 serviceConfig: InternalJoinerServiceConfig
94 residualFilters: ResidualCrossModuleFilter[]
95 residualOrderBy: ResidualOrderBy[]
96 },
97 catalog: GraphCatalog
98 ): ResidualHiddenProperty[] {
99 const context = createConsumeContext(
100 { query: params.query, serviceConfig: params.serviceConfig },
101 catalog
102 )
103
104 consumeResidualFilters(context, params.residualFilters)
105 consumeResidualOrderBy(context, params.residualOrderBy)
106
107 return context.hidden
108 }