MCPcopy Index your code
hub / github.com/TanStack/db / compileRef

Function compileRef

packages/db/src/query/compiler/evaluators.ts:139–211  ·  view source on GitHub ↗

* Compiles a reference expression into an optimized evaluator

(ref: PropRef)

Source from the content-addressed store, hash-verified

137 * Compiles a reference expression into an optimized evaluator
138 */
139function compileRef(ref: PropRef): CompiledExpression {
140 const [namespace, ...propertyPath] = ref.path
141
142 if (!namespace) {
143 throw new EmptyReferencePathError()
144 }
145
146 // Handle $selected namespace - references SELECT result fields
147 if (namespace === `$selected`) {
148 // Access $selected directly
149 if (propertyPath.length === 0) {
150 // Just $selected - return entire $selected object
151 return (namespacedRow) => (namespacedRow as any).$selected
152 } else if (propertyPath.length === 1) {
153 // Single property access - most common case
154 const prop = propertyPath[0]!
155 return (namespacedRow) => {
156 const selectResults = (namespacedRow as any).$selected
157 return selectResults?.[prop]
158 }
159 } else {
160 // Multiple property navigation (nested SELECT fields)
161 return (namespacedRow) => {
162 const selectResults = (namespacedRow as any).$selected
163 if (selectResults === undefined) {
164 return undefined
165 }
166
167 let value: any = selectResults
168 for (const prop of propertyPath) {
169 if (value == null) {
170 return value
171 }
172 value = value[prop]
173 }
174 return value
175 }
176 }
177 }
178
179 // Handle table alias namespace (existing logic)
180 const tableAlias = namespace
181
182 // Pre-compile the property path navigation
183 if (propertyPath.length === 0) {
184 // Simple table reference
185 return (namespacedRow) => namespacedRow[tableAlias]
186 } else if (propertyPath.length === 1) {
187 // Single property access - most common case
188 const prop = propertyPath[0]!
189 return (namespacedRow) => {
190 const tableData = namespacedRow[tableAlias]
191 return tableData?.[prop]
192 }
193 } else {
194 // Multiple property navigation
195 return (namespacedRow) => {
196 const tableData = namespacedRow[tableAlias]

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected