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

Function createRefProxyWithSelected

packages/db/src/query/builder/ref-proxy.ts:206–284  ·  view source on GitHub ↗
(
  aliases: Array<string>,
)

Source from the content-addressed store, hash-verified

204 * @returns A ref proxy with table aliases and $selected namespace
205 */
206export function createRefProxyWithSelected<T extends Record<string, any>>(
207 aliases: Array<string>,
208): RefProxy<T> & T & { $selected: SingleRowRefProxy<any> } {
209 const baseProxy = createRefProxy(aliases)
210
211 // Create a proxy for $selected that prefixes all paths with '$selected'
212 const cache = new Map<string, any>()
213
214 function createSelectedProxy(path: Array<string>): any {
215 const pathKey = path.join(`.`)
216 if (cache.has(pathKey)) {
217 return cache.get(pathKey)
218 }
219
220 const proxy = new Proxy({} as any, {
221 get(target, prop, receiver) {
222 if (prop === `__refProxy`) return true
223 if (prop === `__path`) return [`$selected`, ...path]
224 if (prop === `__type`) return undefined
225 if (typeof prop === `symbol`) return Reflect.get(target, prop, receiver)
226
227 const newPath = [...path, String(prop)]
228 return createSelectedProxy(newPath)
229 },
230
231 has(target, prop) {
232 if (prop === `__refProxy` || prop === `__path` || prop === `__type`)
233 return true
234 return Reflect.has(target, prop)
235 },
236
237 ownKeys(target) {
238 return Reflect.ownKeys(target)
239 },
240
241 getOwnPropertyDescriptor(target, prop) {
242 if (prop === `__refProxy` || prop === `__path` || prop === `__type`) {
243 return { enumerable: false, configurable: true }
244 }
245 return Reflect.getOwnPropertyDescriptor(target, prop)
246 },
247 })
248
249 cache.set(pathKey, proxy)
250 return proxy
251 }
252
253 const wrappedSelectedProxy = createSelectedProxy([])
254
255 // Wrap the base proxy to also handle $selected access
256 return new Proxy(baseProxy, {
257 get(target, prop, receiver) {
258 if (prop === `$selected`) {
259 return wrappedSelectedProxy
260 }
261 return Reflect.get(target, prop, receiver)
262 },
263

Callers 2

havingMethod · 0.85
orderByMethod · 0.85

Calls 2

createRefProxyFunction · 0.85
createSelectedProxyFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…