( aliases: Array<string>, )
| 99 | * Used in callbacks like where, select, etc. to create type-safe references |
| 100 | */ |
| 101 | export function createRefProxy<T extends Record<string, any>>( |
| 102 | aliases: Array<string>, |
| 103 | ): RefProxy<T> & T { |
| 104 | const cache = new Map<string, any>() |
| 105 | let accessId = 0 // Monotonic counter to record evaluation order |
| 106 | |
| 107 | function createProxy(path: Array<string>): any { |
| 108 | const pathKey = path.join(`.`) |
| 109 | if (cache.has(pathKey)) { |
| 110 | return cache.get(pathKey) |
| 111 | } |
| 112 | |
| 113 | const proxy = new Proxy({} as any, { |
| 114 | get(target, prop, receiver) { |
| 115 | if (prop === `__refProxy`) return true |
| 116 | if (prop === `__path`) return path |
| 117 | if (prop === `__type`) return undefined // Type is only for TypeScript inference |
| 118 | if (typeof prop === `symbol`) return Reflect.get(target, prop, receiver) |
| 119 | |
| 120 | const newPath = [...path, String(prop)] |
| 121 | return createProxy(newPath) |
| 122 | }, |
| 123 | |
| 124 | has(target, prop) { |
| 125 | if (prop === `__refProxy` || prop === `__path` || prop === `__type`) |
| 126 | return true |
| 127 | return Reflect.has(target, prop) |
| 128 | }, |
| 129 | |
| 130 | ownKeys(target) { |
| 131 | const id = ++accessId |
| 132 | const sentinelKey = `__SPREAD_SENTINEL__${path.join(`.`)}__${id}` |
| 133 | if (!Object.prototype.hasOwnProperty.call(target, sentinelKey)) { |
| 134 | Object.defineProperty(target, sentinelKey, { |
| 135 | enumerable: true, |
| 136 | configurable: true, |
| 137 | value: true, |
| 138 | }) |
| 139 | } |
| 140 | return Reflect.ownKeys(target) |
| 141 | }, |
| 142 | |
| 143 | getOwnPropertyDescriptor(target, prop) { |
| 144 | if (prop === `__refProxy` || prop === `__path` || prop === `__type`) { |
| 145 | return { enumerable: false, configurable: true } |
| 146 | } |
| 147 | return Reflect.getOwnPropertyDescriptor(target, prop) |
| 148 | }, |
| 149 | }) |
| 150 | |
| 151 | cache.set(pathKey, proxy) |
| 152 | return proxy |
| 153 | } |
| 154 | |
| 155 | // Create the root proxy with all aliases as top-level properties |
| 156 | const rootProxy = new Proxy({} as any, { |
| 157 | get(target, prop, receiver) { |
| 158 | if (prop === `__refProxy`) return true |
no outgoing calls
no test coverage detected
searching dependent graphs…