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

Function extractCollectionsFromQuery

packages/db/src/query/live/utils.ts:19–102  ·  view source on GitHub ↗
(
  query: any,
)

Source from the content-addressed store, hash-verified

17 * Maps collections by their ID (not alias) as expected by the compiler.
18 */
19export function extractCollectionsFromQuery(
20 query: any,
21): Record<string, Collection<any, any, any>> {
22 const collections: Record<string, any> = {}
23
24 // Helper function to recursively extract collections from a query or source
25 function extractFromSource(source: any) {
26 if (source.type === `collectionRef`) {
27 collections[source.collection.id] = source.collection
28 } else if (source.type === `queryRef`) {
29 // Recursively extract from subquery
30 extractFromQuery(source.query)
31 } else if (source.type === `unionFrom`) {
32 for (const childSource of source.sources) {
33 extractFromSource(childSource)
34 }
35 } else if (source.type === `unionAll`) {
36 for (const branch of source.queries) {
37 extractFromQuery(branch)
38 }
39 }
40 }
41
42 // Helper function to recursively extract collections from a query
43 function extractFromQuery(q: any) {
44 // Extract from FROM clause
45 if (q.from) {
46 extractFromSource(q.from)
47 }
48
49 // Extract from JOIN clauses
50 if (q.join && Array.isArray(q.join)) {
51 for (const joinClause of q.join) {
52 if (joinClause.from) {
53 extractFromSource(joinClause.from)
54 }
55 }
56 }
57
58 // Extract from SELECT (for IncludesSubquery)
59 if (q.select) {
60 extractFromSelect(q.select)
61 }
62 }
63
64 function extractFromSelect(select: any) {
65 for (const [key, value] of Object.entries(select)) {
66 if (typeof key === `string` && key.startsWith(`__SPREAD_SENTINEL__`)) {
67 continue
68 }
69 if (value instanceof IncludesSubquery) {
70 extractFromQuery(value.query)
71 } else if (value instanceof ConditionalSelect) {
72 extractFromConditionalSelect(value)
73 } else if (isNestedSelectObject(value)) {
74 extractFromSelect(value)
75 }
76 }

Callers 2

constructorMethod · 0.85
constructorMethod · 0.85

Calls 1

extractFromQueryFunction · 0.85

Tested by

no test coverage detected