| 106 | * The FROM clause may refer directly to a collection or indirectly to a subquery. |
| 107 | */ |
| 108 | export function extractCollectionFromSource( |
| 109 | query: any, |
| 110 | ): Collection<any, any, any> { |
| 111 | const from = query.from |
| 112 | |
| 113 | if (from.type === `collectionRef`) { |
| 114 | return from.collection |
| 115 | } else if (from.type === `queryRef`) { |
| 116 | // Recursively extract from subquery |
| 117 | return extractCollectionFromSource(from.query) |
| 118 | } else if (from.type === `unionFrom`) { |
| 119 | return extractCollectionFromSource({ from: from.sources[0] }) |
| 120 | } else if (from.type === `unionAll`) { |
| 121 | return extractCollectionFromSource(from.queries[0]) |
| 122 | } |
| 123 | |
| 124 | throw new Error( |
| 125 | `Failed to extract collection. Invalid FROM clause: ${JSON.stringify(query)}`, |
| 126 | ) |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Extracts all aliases used for each collection across the entire query tree. |