UNPKG

@tanstack/db

Version:

A reactive client store for building super fast apps on sync

230 lines (229 loc) 7.06 kB
import { map, filter, distinct } from "@tanstack/db-ivm"; import { optimizeQuery } from "../optimizer.js"; import { DistinctRequiresSelectError, HavingRequiresGroupByError, LimitOffsetRequireOrderByError, UnsupportedFromTypeError, CollectionInputNotFoundError } from "../../errors.js"; import { compileExpression } from "./evaluators.js"; import { processJoins } from "./joins.js"; import { processGroupBy } from "./group-by.js"; import { processOrderBy } from "./order-by.js"; import { processSelectToResults } from "./select.js"; function compileQuery(rawQuery, inputs, cache = /* @__PURE__ */ new WeakMap(), queryMapping = /* @__PURE__ */ new WeakMap()) { const cachedResult = cache.get(rawQuery); if (cachedResult) { return cachedResult; } const { optimizedQuery: query, collectionWhereClauses } = optimizeQuery(rawQuery); queryMapping.set(query, rawQuery); mapNestedQueries(query, rawQuery, queryMapping); const allInputs = { ...inputs }; const tables = {}; const { alias: mainTableAlias, input: mainInput } = processFrom( query.from, allInputs, cache, queryMapping ); tables[mainTableAlias] = mainInput; let pipeline = mainInput.pipe( map(([key, row]) => { const ret = [key, { [mainTableAlias]: row }]; return ret; }) ); if (query.join && query.join.length > 0) { pipeline = processJoins( pipeline, query.join, tables, mainTableAlias, allInputs, cache, queryMapping ); } if (query.where && query.where.length > 0) { for (const where of query.where) { const compiledWhere = compileExpression(where); pipeline = pipeline.pipe( filter(([_key, namespacedRow]) => { return compiledWhere(namespacedRow); }) ); } } if (query.fnWhere && query.fnWhere.length > 0) { for (const fnWhere of query.fnWhere) { pipeline = pipeline.pipe( filter(([_key, namespacedRow]) => { return fnWhere(namespacedRow); }) ); } } if (query.distinct && !query.fnSelect && !query.select) { throw new DistinctRequiresSelectError(); } if (query.fnSelect) { pipeline = pipeline.pipe( map(([key, namespacedRow]) => { const selectResults = query.fnSelect(namespacedRow); return [ key, { ...namespacedRow, __select_results: selectResults } ]; }) ); } else if (query.select) { pipeline = processSelectToResults(pipeline, query.select); } else { pipeline = pipeline.pipe( map(([key, namespacedRow]) => { const selectResults = !query.join && !query.groupBy ? namespacedRow[mainTableAlias] : namespacedRow; return [ key, { ...namespacedRow, __select_results: selectResults } ]; }) ); } if (query.groupBy && query.groupBy.length > 0) { pipeline = processGroupBy( pipeline, query.groupBy, query.having, query.select, query.fnHaving ); } else if (query.select) { const hasAggregates = Object.values(query.select).some( (expr) => expr.type === `agg` ); if (hasAggregates) { pipeline = processGroupBy( pipeline, [], // Empty group by means single group query.having, query.select, query.fnHaving ); } } if (query.having && (!query.groupBy || query.groupBy.length === 0)) { const hasAggregates = query.select ? Object.values(query.select).some((expr) => expr.type === `agg`) : false; if (!hasAggregates) { throw new HavingRequiresGroupByError(); } } if (query.fnHaving && query.fnHaving.length > 0 && (!query.groupBy || query.groupBy.length === 0)) { for (const fnHaving of query.fnHaving) { pipeline = pipeline.pipe( filter(([_key, namespacedRow]) => { return fnHaving(namespacedRow); }) ); } } if (query.distinct) { pipeline = pipeline.pipe(distinct(([_key, row]) => row.__select_results)); } if (query.orderBy && query.orderBy.length > 0) { const orderedPipeline = processOrderBy( pipeline, query.orderBy, query.limit, query.offset ); const resultPipeline2 = orderedPipeline.pipe( map(([key, [row, orderByIndex]]) => { const finalResults = row.__select_results; return [key, [finalResults, orderByIndex]]; }) ); const result2 = resultPipeline2; const compilationResult2 = { pipeline: result2, collectionWhereClauses }; cache.set(rawQuery, compilationResult2); return compilationResult2; } else if (query.limit !== void 0 || query.offset !== void 0) { throw new LimitOffsetRequireOrderByError(); } const resultPipeline = pipeline.pipe( map(([key, row]) => { const finalResults = row.__select_results; return [key, [finalResults, void 0]]; }) ); const result = resultPipeline; const compilationResult = { pipeline: result, collectionWhereClauses }; cache.set(rawQuery, compilationResult); return compilationResult; } function processFrom(from, allInputs, cache, queryMapping) { switch (from.type) { case `collectionRef`: { const input = allInputs[from.collection.id]; if (!input) { throw new CollectionInputNotFoundError(from.collection.id); } return { alias: from.alias, input }; } case `queryRef`: { const originalQuery = queryMapping.get(from.query) || from.query; const subQueryResult = compileQuery( originalQuery, allInputs, cache, queryMapping ); const subQueryInput = subQueryResult.pipeline; const extractedInput = subQueryInput.pipe( map((data) => { const [key, [value, _orderByIndex]] = data; return [key, value]; }) ); return { alias: from.alias, input: extractedInput }; } default: throw new UnsupportedFromTypeError(from.type); } } function mapNestedQueries(optimizedQuery, originalQuery, queryMapping) { if (optimizedQuery.from.type === `queryRef` && originalQuery.from.type === `queryRef`) { queryMapping.set(optimizedQuery.from.query, originalQuery.from.query); mapNestedQueries( optimizedQuery.from.query, originalQuery.from.query, queryMapping ); } if (optimizedQuery.join && originalQuery.join) { for (let i = 0; i < optimizedQuery.join.length && i < originalQuery.join.length; i++) { const optimizedJoin = optimizedQuery.join[i]; const originalJoin = originalQuery.join[i]; if (optimizedJoin.from.type === `queryRef` && originalJoin.from.type === `queryRef`) { queryMapping.set(optimizedJoin.from.query, originalJoin.from.query); mapNestedQueries( optimizedJoin.from.query, originalJoin.from.query, queryMapping ); } } } } export { compileQuery }; //# sourceMappingURL=index.js.map