@tanstack/db 0.4.14 → 0.4.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/dist/cjs/index.cjs +8 -0
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs/index.d.cts +2 -0
  4. package/dist/cjs/indexes/auto-index.cjs +17 -8
  5. package/dist/cjs/indexes/auto-index.cjs.map +1 -1
  6. package/dist/cjs/local-storage.cjs +21 -18
  7. package/dist/cjs/local-storage.cjs.map +1 -1
  8. package/dist/cjs/local-storage.d.cts +9 -0
  9. package/dist/cjs/paced-mutations.cjs +52 -0
  10. package/dist/cjs/paced-mutations.cjs.map +1 -0
  11. package/dist/cjs/paced-mutations.d.cts +81 -0
  12. package/dist/cjs/query/optimizer.cjs +17 -2
  13. package/dist/cjs/query/optimizer.cjs.map +1 -1
  14. package/dist/cjs/strategies/debounceStrategy.cjs +21 -0
  15. package/dist/cjs/strategies/debounceStrategy.cjs.map +1 -0
  16. package/dist/cjs/strategies/debounceStrategy.d.cts +22 -0
  17. package/dist/cjs/strategies/index.d.cts +4 -0
  18. package/dist/cjs/strategies/queueStrategy.cjs +33 -0
  19. package/dist/cjs/strategies/queueStrategy.cjs.map +1 -0
  20. package/dist/cjs/strategies/queueStrategy.d.cts +43 -0
  21. package/dist/cjs/strategies/throttleStrategy.cjs +21 -0
  22. package/dist/cjs/strategies/throttleStrategy.cjs.map +1 -0
  23. package/dist/cjs/strategies/throttleStrategy.d.cts +39 -0
  24. package/dist/cjs/strategies/types.d.cts +103 -0
  25. package/dist/esm/index.d.ts +2 -0
  26. package/dist/esm/index.js +8 -0
  27. package/dist/esm/index.js.map +1 -1
  28. package/dist/esm/indexes/auto-index.js +17 -8
  29. package/dist/esm/indexes/auto-index.js.map +1 -1
  30. package/dist/esm/local-storage.d.ts +9 -0
  31. package/dist/esm/local-storage.js +21 -18
  32. package/dist/esm/local-storage.js.map +1 -1
  33. package/dist/esm/paced-mutations.d.ts +81 -0
  34. package/dist/esm/paced-mutations.js +52 -0
  35. package/dist/esm/paced-mutations.js.map +1 -0
  36. package/dist/esm/query/optimizer.js +17 -2
  37. package/dist/esm/query/optimizer.js.map +1 -1
  38. package/dist/esm/strategies/debounceStrategy.d.ts +22 -0
  39. package/dist/esm/strategies/debounceStrategy.js +21 -0
  40. package/dist/esm/strategies/debounceStrategy.js.map +1 -0
  41. package/dist/esm/strategies/index.d.ts +4 -0
  42. package/dist/esm/strategies/queueStrategy.d.ts +43 -0
  43. package/dist/esm/strategies/queueStrategy.js +33 -0
  44. package/dist/esm/strategies/queueStrategy.js.map +1 -0
  45. package/dist/esm/strategies/throttleStrategy.d.ts +39 -0
  46. package/dist/esm/strategies/throttleStrategy.js +21 -0
  47. package/dist/esm/strategies/throttleStrategy.js.map +1 -0
  48. package/dist/esm/strategies/types.d.ts +103 -0
  49. package/package.json +4 -1
  50. package/src/index.ts +2 -0
  51. package/src/indexes/auto-index.ts +23 -10
  52. package/src/local-storage.ts +41 -17
  53. package/src/paced-mutations.ts +169 -0
  54. package/src/query/optimizer.ts +31 -4
  55. package/src/strategies/debounceStrategy.ts +45 -0
  56. package/src/strategies/index.ts +17 -0
  57. package/src/strategies/queueStrategy.ts +75 -0
  58. package/src/strategies/throttleStrategy.ts +62 -0
  59. package/src/strategies/types.ts +130 -0
@@ -75,6 +75,14 @@ function applySingleLevelOptimization(query) {
75
75
  return query;
76
76
  }
77
77
  if (!query.join || query.join.length === 0) {
78
+ if (query.where.length > 1) {
79
+ const splitWhereClauses2 = splitAndClauses(query.where);
80
+ const combinedWhere = combineWithAnd(splitWhereClauses2);
81
+ return {
82
+ ...query,
83
+ where: [combinedWhere]
84
+ };
85
+ }
78
86
  return query;
79
87
  }
80
88
  const nonResidualWhereClauses = query.where.filter(
@@ -234,6 +242,13 @@ function applyOptimizations(query, groupedClauses) {
234
242
  remainingWhereClauses.push(ir.createResidualWhere(clause));
235
243
  }
236
244
  }
245
+ const finalWhere = remainingWhereClauses.length > 1 ? [
246
+ combineWithAnd(
247
+ remainingWhereClauses.flatMap(
248
+ (clause) => splitAndClausesRecursive(ir.getWhereExpression(clause))
249
+ )
250
+ )
251
+ ] : remainingWhereClauses;
237
252
  const optimizedQuery = {
238
253
  // Copy all non-optimized fields as-is
239
254
  select: query.select,
@@ -249,8 +264,8 @@ function applyOptimizations(query, groupedClauses) {
249
264
  // Use the optimized FROM and JOIN clauses
250
265
  from: optimizedFrom,
251
266
  join: optimizedJoins,
252
- // Only include WHERE clauses that weren't successfully optimized
253
- where: remainingWhereClauses.length > 0 ? remainingWhereClauses : []
267
+ // Include combined WHERE clauses
268
+ where: finalWhere.length > 0 ? finalWhere : []
254
269
  };
255
270
  return optimizedQuery;
256
271
  }
@@ -1 +1 @@
1
- {"version":3,"file":"optimizer.cjs","sources":["../../../src/query/optimizer.ts"],"sourcesContent":["/**\n * # Query Optimizer\n *\n * The query optimizer improves query performance by implementing predicate pushdown optimization.\n * It rewrites the intermediate representation (IR) to push WHERE clauses as close to the data\n * source as possible, reducing the amount of data processed during joins.\n *\n * ## How It Works\n *\n * The optimizer follows a 4-step process:\n *\n * ### 1. AND Clause Splitting\n * Splits AND clauses at the root level into separate WHERE clauses for granular optimization.\n * ```javascript\n * // Before: WHERE and(eq(users.department_id, 1), gt(users.age, 25))\n * // After: WHERE eq(users.department_id, 1) + WHERE gt(users.age, 25)\n * ```\n *\n * ### 2. Source Analysis\n * Analyzes each WHERE clause to determine which table sources it references:\n * - Single-source clauses: Touch only one table (e.g., `users.department_id = 1`)\n * - Multi-source clauses: Touch multiple tables (e.g., `users.id = posts.user_id`)\n *\n * ### 3. Clause Grouping\n * Groups WHERE clauses by the sources they touch:\n * - Single-source clauses are grouped by their respective table\n * - Multi-source clauses are combined for the main query\n *\n * ### 4. Subquery Creation\n * Lifts single-source WHERE clauses into subqueries that wrap the original table references.\n *\n * ## Safety & Edge Cases\n *\n * The optimizer includes targeted safety checks to prevent predicate pushdown when it could\n * break query semantics:\n *\n * ### Always Safe Operations\n * - **Creating new subqueries**: Wrapping collection references in subqueries with WHERE clauses\n * - **Main query optimizations**: Moving single-source WHERE clauses from main query to subqueries\n * - **Queries with aggregates/ORDER BY/HAVING**: Can still create new filtered subqueries\n *\n * ### Unsafe Operations (blocked by safety checks)\n * Pushing WHERE clauses **into existing subqueries** that have:\n * - **Aggregates**: GROUP BY, HAVING, or aggregate functions in SELECT (would change aggregation)\n * - **Ordering + Limits**: ORDER BY combined with LIMIT/OFFSET (would change result set)\n * - **Functional Operations**: fnSelect, fnWhere, fnHaving (potential side effects)\n *\n * ### Residual WHERE Clauses\n * For outer joins (LEFT, RIGHT, FULL), WHERE clauses are copied to subqueries for optimization\n * but also kept as \"residual\" clauses in the main query to preserve semantics. This ensures\n * that NULL values from outer joins are properly filtered according to SQL standards.\n *\n * The optimizer tracks which clauses were actually optimized and only removes those from the\n * main query. Subquery reuse is handled safely through immutable query copies.\n *\n * ## Example Optimizations\n *\n * ### Basic Query with Joins\n * **Original Query:**\n * ```javascript\n * query\n * .from({ users: usersCollection })\n * .join({ posts: postsCollection }, ({users, posts}) => eq(users.id, posts.user_id))\n * .where(({users}) => eq(users.department_id, 1))\n * .where(({posts}) => gt(posts.views, 100))\n * .where(({users, posts}) => eq(users.id, posts.author_id))\n * ```\n *\n * **Optimized Query:**\n * ```javascript\n * query\n * .from({\n * users: subquery\n * .from({ users: usersCollection })\n * .where(({users}) => eq(users.department_id, 1))\n * })\n * .join({\n * posts: subquery\n * .from({ posts: postsCollection })\n * .where(({posts}) => gt(posts.views, 100))\n * }, ({users, posts}) => eq(users.id, posts.user_id))\n * .where(({users, posts}) => eq(users.id, posts.author_id))\n * ```\n *\n * ### Query with Aggregates (Now Optimizable!)\n * **Original Query:**\n * ```javascript\n * query\n * .from({ users: usersCollection })\n * .join({ posts: postsCollection }, ({users, posts}) => eq(users.id, posts.user_id))\n * .where(({users}) => eq(users.department_id, 1))\n * .groupBy(['users.department_id'])\n * .select({ count: agg('count', '*') })\n * ```\n *\n * **Optimized Query:**\n * ```javascript\n * query\n * .from({\n * users: subquery\n * .from({ users: usersCollection })\n * .where(({users}) => eq(users.department_id, 1))\n * })\n * .join({ posts: postsCollection }, ({users, posts}) => eq(users.id, posts.user_id))\n * .groupBy(['users.department_id'])\n * .select({ count: agg('count', '*') })\n * ```\n *\n * ## Benefits\n *\n * - **Reduced Data Processing**: Filters applied before joins reduce intermediate result size\n * - **Better Performance**: Smaller datasets lead to faster query execution\n * - **Automatic Optimization**: No manual query rewriting required\n * - **Preserves Semantics**: Optimized queries return identical results\n * - **Safe by Design**: Comprehensive checks prevent semantic-breaking optimizations\n *\n * ## Integration\n *\n * The optimizer is automatically called during query compilation before the IR is\n * transformed into a D2Mini pipeline.\n */\n\nimport { deepEquals } from \"../utils.js\"\nimport { CannotCombineEmptyExpressionListError } from \"../errors.js\"\nimport {\n CollectionRef as CollectionRefClass,\n Func,\n PropRef,\n QueryRef as QueryRefClass,\n createResidualWhere,\n getWhereExpression,\n isResidualWhere,\n} from \"./ir.js\"\nimport { isConvertibleToCollectionFilter } from \"./compiler/expressions.js\"\nimport type { BasicExpression, From, QueryIR, Select, Where } from \"./ir.js\"\n\n/**\n * Represents a WHERE clause after source analysis\n */\nexport interface AnalyzedWhereClause {\n /** The WHERE expression */\n expression: BasicExpression<boolean>\n /** Set of table/source aliases that this WHERE clause touches */\n touchedSources: Set<string>\n /** Whether this clause contains namespace-only references that prevent pushdown */\n hasNamespaceOnlyRef: boolean\n}\n\n/**\n * Represents WHERE clauses grouped by the sources they touch\n */\nexport interface GroupedWhereClauses {\n /** WHERE clauses that touch only a single source, grouped by source alias */\n singleSource: Map<string, BasicExpression<boolean>>\n /** WHERE clauses that touch multiple sources, combined into one expression */\n multiSource?: BasicExpression<boolean>\n}\n\n/**\n * Result of query optimization including both the optimized query and collection-specific WHERE clauses\n */\nexport interface OptimizationResult {\n /** The optimized query with WHERE clauses potentially moved to subqueries */\n optimizedQuery: QueryIR\n /** Map of source aliases to their extracted WHERE clauses for index optimization */\n sourceWhereClauses: Map<string, BasicExpression<boolean>>\n}\n\n/**\n * Main query optimizer entry point that lifts WHERE clauses into subqueries.\n *\n * This function implements multi-level predicate pushdown optimization by recursively\n * moving WHERE clauses through nested subqueries to get them as close to the data\n * sources as possible, then removing redundant subqueries.\n *\n * @param query - The QueryIR to optimize\n * @returns An OptimizationResult with the optimized query and collection WHERE clause mapping\n *\n * @example\n * ```typescript\n * const originalQuery = {\n * from: new CollectionRef(users, 'u'),\n * join: [{ from: new CollectionRef(posts, 'p'), ... }],\n * where: [eq(u.dept_id, 1), gt(p.views, 100)]\n * }\n *\n * const { optimizedQuery, sourceWhereClauses } = optimizeQuery(originalQuery)\n * // Result: Single-source clauses moved to deepest possible subqueries\n * // sourceWhereClauses: Map { 'u' => eq(u.dept_id, 1), 'p' => gt(p.views, 100) }\n * ```\n */\nexport function optimizeQuery(query: QueryIR): OptimizationResult {\n // First, extract source WHERE clauses before optimization\n const sourceWhereClauses = extractSourceWhereClauses(query)\n\n // Apply multi-level predicate pushdown with iterative convergence\n let optimized = query\n let previousOptimized: QueryIR | undefined\n let iterations = 0\n const maxIterations = 10 // Prevent infinite loops\n\n // Keep optimizing until no more changes occur or max iterations reached\n while (\n iterations < maxIterations &&\n !deepEquals(optimized, previousOptimized)\n ) {\n previousOptimized = optimized\n optimized = applyRecursiveOptimization(optimized)\n iterations++\n }\n\n // Remove redundant subqueries\n const cleaned = removeRedundantSubqueries(optimized)\n\n return {\n optimizedQuery: cleaned,\n sourceWhereClauses,\n }\n}\n\n/**\n * Extracts collection-specific WHERE clauses from a query for index optimization.\n * This analyzes the original query to identify WHERE clauses that can be pushed down\n * to specific collections, but only for simple queries without joins.\n *\n * @param query - The original QueryIR to analyze\n * @returns Map of source aliases to their WHERE clauses\n */\nfunction extractSourceWhereClauses(\n query: QueryIR\n): Map<string, BasicExpression<boolean>> {\n const sourceWhereClauses = new Map<string, BasicExpression<boolean>>()\n\n // Only analyze queries that have WHERE clauses\n if (!query.where || query.where.length === 0) {\n return sourceWhereClauses\n }\n\n // Split all AND clauses at the root level for granular analysis\n const splitWhereClauses = splitAndClauses(query.where)\n\n // Analyze each WHERE clause to determine which sources it touches\n const analyzedClauses = splitWhereClauses.map((clause) =>\n analyzeWhereClause(clause)\n )\n\n // Group clauses by single-source vs multi-source\n const groupedClauses = groupWhereClauses(analyzedClauses)\n\n // Only include single-source clauses that reference collections directly\n // and can be converted to BasicExpression format for collection indexes\n for (const [sourceAlias, whereClause] of groupedClauses.singleSource) {\n // Check if this source alias corresponds to a collection reference\n if (isCollectionReference(query, sourceAlias)) {\n // Check if the WHERE clause can be converted to collection-compatible format\n if (isConvertibleToCollectionFilter(whereClause)) {\n sourceWhereClauses.set(sourceAlias, whereClause)\n }\n }\n }\n\n return sourceWhereClauses\n}\n\n/**\n * Determines if a source alias refers to a collection reference (not a subquery).\n * This is used to identify WHERE clauses that can be pushed down to collection subscriptions.\n *\n * @param query - The query to analyze\n * @param sourceAlias - The source alias to check\n * @returns True if the alias refers to a collection reference\n */\nfunction isCollectionReference(query: QueryIR, sourceAlias: string): boolean {\n // Check the FROM clause\n if (query.from.alias === sourceAlias) {\n return query.from.type === `collectionRef`\n }\n\n // Check JOIN clauses\n if (query.join) {\n for (const joinClause of query.join) {\n if (joinClause.from.alias === sourceAlias) {\n return joinClause.from.type === `collectionRef`\n }\n }\n }\n\n return false\n}\n\n/**\n * Applies recursive predicate pushdown optimization.\n *\n * @param query - The QueryIR to optimize\n * @returns A new QueryIR with optimizations applied\n */\nfunction applyRecursiveOptimization(query: QueryIR): QueryIR {\n // First, recursively optimize any existing subqueries\n const subqueriesOptimized = {\n ...query,\n from:\n query.from.type === `queryRef`\n ? new QueryRefClass(\n applyRecursiveOptimization(query.from.query),\n query.from.alias\n )\n : query.from,\n join: query.join?.map((joinClause) => ({\n ...joinClause,\n from:\n joinClause.from.type === `queryRef`\n ? new QueryRefClass(\n applyRecursiveOptimization(joinClause.from.query),\n joinClause.from.alias\n )\n : joinClause.from,\n })),\n }\n\n // Then apply single-level optimization to this query\n return applySingleLevelOptimization(subqueriesOptimized)\n}\n\n/**\n * Applies single-level predicate pushdown optimization (existing logic)\n */\nfunction applySingleLevelOptimization(query: QueryIR): QueryIR {\n // Skip optimization if no WHERE clauses exist\n if (!query.where || query.where.length === 0) {\n return query\n }\n\n // Skip optimization if there are no joins - predicate pushdown only benefits joins\n // Single-table queries don't benefit from this optimization\n if (!query.join || query.join.length === 0) {\n return query\n }\n\n // Filter out residual WHERE clauses to prevent them from being optimized again\n const nonResidualWhereClauses = query.where.filter(\n (where) => !isResidualWhere(where)\n )\n\n // Step 1: Split all AND clauses at the root level for granular optimization\n const splitWhereClauses = splitAndClauses(nonResidualWhereClauses)\n\n // Step 2: Analyze each WHERE clause to determine which sources it touches\n const analyzedClauses = splitWhereClauses.map((clause) =>\n analyzeWhereClause(clause)\n )\n\n // Step 3: Group clauses by single-source vs multi-source\n const groupedClauses = groupWhereClauses(analyzedClauses)\n\n // Step 4: Apply optimizations by lifting single-source clauses into subqueries\n const optimizedQuery = applyOptimizations(query, groupedClauses)\n\n // Add back any residual WHERE clauses that were filtered out\n const residualWhereClauses = query.where.filter((where) =>\n isResidualWhere(where)\n )\n if (residualWhereClauses.length > 0) {\n optimizedQuery.where = [\n ...(optimizedQuery.where || []),\n ...residualWhereClauses,\n ]\n }\n\n return optimizedQuery\n}\n\n/**\n * Removes redundant subqueries that don't add value.\n * A subquery is redundant if it only wraps another query without adding\n * WHERE, SELECT, GROUP BY, HAVING, ORDER BY, or LIMIT/OFFSET clauses.\n *\n * @param query - The QueryIR to process\n * @returns A new QueryIR with redundant subqueries removed\n */\nfunction removeRedundantSubqueries(query: QueryIR): QueryIR {\n return {\n ...query,\n from: removeRedundantFromClause(query.from),\n join: query.join?.map((joinClause) => ({\n ...joinClause,\n from: removeRedundantFromClause(joinClause.from),\n })),\n }\n}\n\n/**\n * Removes redundant subqueries from a FROM clause.\n *\n * @param from - The FROM clause to process\n * @returns A FROM clause with redundant subqueries removed\n */\nfunction removeRedundantFromClause(from: From): From {\n if (from.type === `collectionRef`) {\n return from\n }\n\n const processedQuery = removeRedundantSubqueries(from.query)\n\n // Check if this subquery is redundant\n if (isRedundantSubquery(processedQuery)) {\n // Return the inner query's FROM clause with this alias\n const innerFrom = removeRedundantFromClause(processedQuery.from)\n if (innerFrom.type === `collectionRef`) {\n return new CollectionRefClass(innerFrom.collection, from.alias)\n } else {\n return new QueryRefClass(innerFrom.query, from.alias)\n }\n }\n\n return new QueryRefClass(processedQuery, from.alias)\n}\n\n/**\n * Determines if a subquery is redundant (adds no value).\n *\n * @param query - The query to check\n * @returns True if the query is redundant and can be removed\n */\nfunction isRedundantSubquery(query: QueryIR): boolean {\n return (\n (!query.where || query.where.length === 0) &&\n !query.select &&\n (!query.groupBy || query.groupBy.length === 0) &&\n (!query.having || query.having.length === 0) &&\n (!query.orderBy || query.orderBy.length === 0) &&\n (!query.join || query.join.length === 0) &&\n query.limit === undefined &&\n query.offset === undefined &&\n !query.fnSelect &&\n (!query.fnWhere || query.fnWhere.length === 0) &&\n (!query.fnHaving || query.fnHaving.length === 0)\n )\n}\n\n/**\n * Step 1: Split all AND clauses recursively into separate WHERE clauses.\n *\n * This enables more granular optimization by treating each condition independently.\n * OR clauses are preserved as they cannot be split without changing query semantics.\n *\n * @param whereClauses - Array of WHERE expressions to split\n * @returns Flattened array with AND clauses split into separate expressions\n *\n * @example\n * ```typescript\n * // Input: [and(eq(a, 1), gt(b, 2)), eq(c, 3)]\n * // Output: [eq(a, 1), gt(b, 2), eq(c, 3)]\n * ```\n */\nfunction splitAndClauses(\n whereClauses: Array<Where>\n): Array<BasicExpression<boolean>> {\n const result: Array<BasicExpression<boolean>> = []\n\n for (const whereClause of whereClauses) {\n const clause = getWhereExpression(whereClause)\n result.push(...splitAndClausesRecursive(clause))\n }\n\n return result\n}\n\n// Helper function for recursive splitting of BasicExpression arrays\nfunction splitAndClausesRecursive(\n clause: BasicExpression<boolean>\n): Array<BasicExpression<boolean>> {\n if (clause.type === `func` && clause.name === `and`) {\n // Recursively split nested AND clauses to handle complex expressions\n const result: Array<BasicExpression<boolean>> = []\n for (const arg of clause.args as Array<BasicExpression<boolean>>) {\n result.push(...splitAndClausesRecursive(arg))\n }\n return result\n } else {\n // Preserve non-AND clauses as-is (including OR clauses)\n return [clause]\n }\n}\n\n/**\n * Step 2: Analyze which table sources a WHERE clause touches.\n *\n * This determines whether a clause can be pushed down to a specific table\n * or must remain in the main query (for multi-source clauses like join conditions).\n *\n * Special handling for namespace-only references in outer joins:\n * WHERE clauses that reference only a table namespace (e.g., isUndefined(special), eq(special, value))\n * rather than specific properties (e.g., isUndefined(special.id), eq(special.id, value)) are treated as\n * multi-source to prevent incorrect predicate pushdown that would change join semantics.\n *\n * @param clause - The WHERE expression to analyze\n * @returns Analysis result with the expression and touched source aliases\n *\n * @example\n * ```typescript\n * // eq(users.department_id, 1) -> touches ['users'], hasNamespaceOnlyRef: false\n * // eq(users.id, posts.user_id) -> touches ['users', 'posts'], hasNamespaceOnlyRef: false\n * // isUndefined(special) -> touches ['special'], hasNamespaceOnlyRef: true (prevents pushdown)\n * // eq(special, someValue) -> touches ['special'], hasNamespaceOnlyRef: true (prevents pushdown)\n * // isUndefined(special.id) -> touches ['special'], hasNamespaceOnlyRef: false (allows pushdown)\n * // eq(special.id, 5) -> touches ['special'], hasNamespaceOnlyRef: false (allows pushdown)\n * ```\n */\nfunction analyzeWhereClause(\n clause: BasicExpression<boolean>\n): AnalyzedWhereClause {\n // Track which table aliases this WHERE clause touches\n const touchedSources = new Set<string>()\n // Track whether this clause contains namespace-only references that prevent pushdown\n let hasNamespaceOnlyRef = false\n\n /**\n * Recursively collect all table aliases referenced in an expression\n */\n function collectSources(expr: BasicExpression | any): void {\n switch (expr.type) {\n case `ref`:\n // PropRef path has the table alias as the first element\n if (expr.path && expr.path.length > 0) {\n const firstElement = expr.path[0]\n if (firstElement) {\n touchedSources.add(firstElement)\n\n // If the path has only one element (just the namespace),\n // this is a namespace-only reference that should not be pushed down\n // This applies to ANY function, not just existence-checking functions\n if (expr.path.length === 1) {\n hasNamespaceOnlyRef = true\n }\n }\n }\n break\n case `func`:\n // Recursively analyze function arguments (e.g., eq, gt, and, or)\n if (expr.args) {\n expr.args.forEach(collectSources)\n }\n break\n case `val`:\n // Values don't reference any sources\n break\n case `agg`:\n // Aggregates can reference sources in their arguments\n if (expr.args) {\n expr.args.forEach(collectSources)\n }\n break\n }\n }\n\n collectSources(clause)\n\n return {\n expression: clause,\n touchedSources,\n hasNamespaceOnlyRef,\n }\n}\n\n/**\n * Step 3: Group WHERE clauses by the sources they touch.\n *\n * Single-source clauses can be pushed down to subqueries for optimization.\n * Multi-source clauses must remain in the main query to preserve join semantics.\n *\n * @param analyzedClauses - Array of analyzed WHERE clauses\n * @returns Grouped clauses ready for optimization\n */\nfunction groupWhereClauses(\n analyzedClauses: Array<AnalyzedWhereClause>\n): GroupedWhereClauses {\n const singleSource = new Map<string, Array<BasicExpression<boolean>>>()\n const multiSource: Array<BasicExpression<boolean>> = []\n\n // Categorize each clause based on how many sources it touches\n for (const clause of analyzedClauses) {\n if (clause.touchedSources.size === 1 && !clause.hasNamespaceOnlyRef) {\n // Single source clause without namespace-only references - can be optimized\n const source = Array.from(clause.touchedSources)[0]!\n if (!singleSource.has(source)) {\n singleSource.set(source, [])\n }\n singleSource.get(source)!.push(clause.expression)\n } else if (clause.touchedSources.size > 1 || clause.hasNamespaceOnlyRef) {\n // Multi-source clause or namespace-only reference - must stay in main query\n multiSource.push(clause.expression)\n }\n // Skip clauses that touch no sources (constants) - they don't need optimization\n }\n\n // Combine multiple clauses for each source with AND\n const combinedSingleSource = new Map<string, BasicExpression<boolean>>()\n for (const [source, clauses] of singleSource) {\n combinedSingleSource.set(source, combineWithAnd(clauses))\n }\n\n // Combine multi-source clauses with AND\n const combinedMultiSource =\n multiSource.length > 0 ? combineWithAnd(multiSource) : undefined\n\n return {\n singleSource: combinedSingleSource,\n multiSource: combinedMultiSource,\n }\n}\n\n/**\n * Step 4: Apply optimizations by lifting single-source clauses into subqueries.\n *\n * Creates a new QueryIR with single-source WHERE clauses moved to subqueries\n * that wrap the original table references. This ensures immutability and prevents\n * infinite recursion issues.\n *\n * @param query - Original QueryIR to optimize\n * @param groupedClauses - WHERE clauses grouped by optimization strategy\n * @returns New QueryIR with optimizations applied\n */\nfunction applyOptimizations(\n query: QueryIR,\n groupedClauses: GroupedWhereClauses\n): QueryIR {\n // Track which single-source clauses were actually optimized\n const actuallyOptimized = new Set<string>()\n\n // Optimize the main FROM clause and track what was optimized\n const optimizedFrom = optimizeFromWithTracking(\n query.from,\n groupedClauses.singleSource,\n actuallyOptimized\n )\n\n // Optimize JOIN clauses and track what was optimized\n const optimizedJoins = query.join\n ? query.join.map((joinClause) => ({\n ...joinClause,\n from: optimizeFromWithTracking(\n joinClause.from,\n groupedClauses.singleSource,\n actuallyOptimized\n ),\n }))\n : undefined\n\n // Build the remaining WHERE clauses: multi-source + residual single-source clauses\n const remainingWhereClauses: Array<Where> = []\n\n // Add multi-source clauses\n if (groupedClauses.multiSource) {\n remainingWhereClauses.push(groupedClauses.multiSource)\n }\n\n // Determine if we need residual clauses (when query has outer JOINs)\n const hasOuterJoins =\n query.join &&\n query.join.some(\n (join) =>\n join.type === `left` || join.type === `right` || join.type === `full`\n )\n\n // Add single-source clauses\n for (const [source, clause] of groupedClauses.singleSource) {\n if (!actuallyOptimized.has(source)) {\n // Wasn't optimized at all - keep as regular WHERE clause\n remainingWhereClauses.push(clause)\n } else if (hasOuterJoins) {\n // Was optimized AND query has outer JOINs - keep as residual WHERE clause\n remainingWhereClauses.push(createResidualWhere(clause))\n }\n // If optimized and no outer JOINs - don't keep (original behavior)\n }\n\n // Create a completely new query object to ensure immutability\n const optimizedQuery: QueryIR = {\n // Copy all non-optimized fields as-is\n select: query.select,\n groupBy: query.groupBy ? [...query.groupBy] : undefined,\n having: query.having ? [...query.having] : undefined,\n orderBy: query.orderBy ? [...query.orderBy] : undefined,\n limit: query.limit,\n offset: query.offset,\n distinct: query.distinct,\n fnSelect: query.fnSelect,\n fnWhere: query.fnWhere ? [...query.fnWhere] : undefined,\n fnHaving: query.fnHaving ? [...query.fnHaving] : undefined,\n\n // Use the optimized FROM and JOIN clauses\n from: optimizedFrom,\n join: optimizedJoins,\n\n // Only include WHERE clauses that weren't successfully optimized\n where: remainingWhereClauses.length > 0 ? remainingWhereClauses : [],\n }\n\n return optimizedQuery\n}\n\n/**\n * Helper function to create a deep copy of a QueryIR object for immutability.\n *\n * This ensures that all optimizations create new objects rather than modifying\n * existing ones, preventing infinite recursion and shared reference issues.\n *\n * @param query - QueryIR to deep copy\n * @returns New QueryIR object with all nested objects copied\n */\nfunction deepCopyQuery(query: QueryIR): QueryIR {\n return {\n // Recursively copy the FROM clause\n from:\n query.from.type === `collectionRef`\n ? new CollectionRefClass(query.from.collection, query.from.alias)\n : new QueryRefClass(deepCopyQuery(query.from.query), query.from.alias),\n\n // Copy all other fields, creating new arrays where necessary\n select: query.select,\n join: query.join\n ? query.join.map((joinClause) => ({\n type: joinClause.type,\n left: joinClause.left,\n right: joinClause.right,\n from:\n joinClause.from.type === `collectionRef`\n ? new CollectionRefClass(\n joinClause.from.collection,\n joinClause.from.alias\n )\n : new QueryRefClass(\n deepCopyQuery(joinClause.from.query),\n joinClause.from.alias\n ),\n }))\n : undefined,\n where: query.where ? [...query.where] : undefined,\n groupBy: query.groupBy ? [...query.groupBy] : undefined,\n having: query.having ? [...query.having] : undefined,\n orderBy: query.orderBy ? [...query.orderBy] : undefined,\n limit: query.limit,\n offset: query.offset,\n fnSelect: query.fnSelect,\n fnWhere: query.fnWhere ? [...query.fnWhere] : undefined,\n fnHaving: query.fnHaving ? [...query.fnHaving] : undefined,\n }\n}\n\n/**\n * Helper function to optimize a FROM clause while tracking what was actually optimized.\n *\n * @param from - FROM clause to optimize\n * @param singleSourceClauses - Map of source aliases to their WHERE clauses\n * @param actuallyOptimized - Set to track which sources were actually optimized\n * @returns New FROM clause, potentially wrapped in a subquery\n */\nfunction optimizeFromWithTracking(\n from: From,\n singleSourceClauses: Map<string, BasicExpression<boolean>>,\n actuallyOptimized: Set<string>\n): From {\n const whereClause = singleSourceClauses.get(from.alias)\n\n if (!whereClause) {\n // No optimization needed, but return a copy to maintain immutability\n if (from.type === `collectionRef`) {\n return new CollectionRefClass(from.collection, from.alias)\n }\n // Must be queryRef due to type system\n return new QueryRefClass(deepCopyQuery(from.query), from.alias)\n }\n\n if (from.type === `collectionRef`) {\n // Create a new subquery with the WHERE clause for the collection\n // This is always safe since we're creating a new subquery\n const subQuery: QueryIR = {\n from: new CollectionRefClass(from.collection, from.alias),\n where: [whereClause],\n }\n actuallyOptimized.add(from.alias) // Mark as successfully optimized\n return new QueryRefClass(subQuery, from.alias)\n }\n\n // SAFETY CHECK: Only check safety when pushing WHERE clauses into existing subqueries\n // We need to be careful about pushing WHERE clauses into subqueries that already have\n // aggregates, HAVING, or ORDER BY + LIMIT since that could change their semantics\n if (!isSafeToPushIntoExistingSubquery(from.query, whereClause, from.alias)) {\n // Return a copy without optimization to maintain immutability\n // Do NOT mark as optimized since we didn't actually optimize it\n return new QueryRefClass(deepCopyQuery(from.query), from.alias)\n }\n\n // Skip pushdown when a clause references a field that only exists via a renamed\n // projection inside the subquery; leaving it outside preserves the alias mapping.\n if (referencesAliasWithRemappedSelect(from.query, whereClause, from.alias)) {\n return new QueryRefClass(deepCopyQuery(from.query), from.alias)\n }\n\n // Add the WHERE clause to the existing subquery\n // Create a deep copy to ensure immutability\n const existingWhere = from.query.where || []\n const optimizedSubQuery: QueryIR = {\n ...deepCopyQuery(from.query),\n where: [...existingWhere, whereClause],\n }\n actuallyOptimized.add(from.alias) // Mark as successfully optimized\n return new QueryRefClass(optimizedSubQuery, from.alias)\n}\n\nfunction unsafeSelect(\n query: QueryIR,\n whereClause: BasicExpression<boolean>,\n outerAlias: string\n): boolean {\n if (!query.select) return false\n\n return (\n selectHasAggregates(query.select) ||\n whereReferencesComputedSelectFields(query.select, whereClause, outerAlias)\n )\n}\n\nfunction unsafeGroupBy(query: QueryIR) {\n return query.groupBy && query.groupBy.length > 0\n}\n\nfunction unsafeHaving(query: QueryIR) {\n return query.having && query.having.length > 0\n}\n\nfunction unsafeOrderBy(query: QueryIR) {\n return (\n query.orderBy &&\n query.orderBy.length > 0 &&\n (query.limit !== undefined || query.offset !== undefined)\n )\n}\n\nfunction unsafeFnSelect(query: QueryIR) {\n return (\n query.fnSelect ||\n (query.fnWhere && query.fnWhere.length > 0) ||\n (query.fnHaving && query.fnHaving.length > 0)\n )\n}\n\nfunction isSafeToPushIntoExistingSubquery(\n query: QueryIR,\n whereClause: BasicExpression<boolean>,\n outerAlias: string\n): boolean {\n return !(\n unsafeSelect(query, whereClause, outerAlias) ||\n unsafeGroupBy(query) ||\n unsafeHaving(query) ||\n unsafeOrderBy(query) ||\n unsafeFnSelect(query)\n )\n}\n\n/**\n * Detects whether a SELECT projection contains any aggregate expressions.\n * Recursively traverses nested select objects.\n *\n * @param select - The SELECT object from the IR\n * @returns True if any field is an aggregate, false otherwise\n */\nfunction selectHasAggregates(select: Select): boolean {\n for (const value of Object.values(select)) {\n if (typeof value === `object`) {\n const v: any = value\n if (v.type === `agg`) return true\n if (!(`type` in v)) {\n if (selectHasAggregates(v as unknown as Select)) return true\n }\n }\n }\n return false\n}\n\n/**\n * Recursively collects all PropRef references from an expression.\n *\n * @param expr - The expression to traverse\n * @returns Array of PropRef references found in the expression\n */\nfunction collectRefs(expr: any): Array<PropRef> {\n const refs: Array<PropRef> = []\n\n if (expr == null || typeof expr !== `object`) return refs\n\n switch (expr.type) {\n case `ref`:\n refs.push(expr as PropRef)\n break\n case `func`:\n case `agg`:\n for (const arg of expr.args ?? []) {\n refs.push(...collectRefs(arg))\n }\n break\n default:\n break\n }\n\n return refs\n}\n\n/**\n * Determines whether the provided WHERE clause references fields that are\n * computed by a subquery SELECT rather than pass-through properties.\n *\n * If true, pushing the WHERE clause into the subquery could change semantics\n * (since computed fields do not necessarily exist at the subquery input level),\n * so predicate pushdown must be avoided.\n *\n * @param select - The subquery SELECT map\n * @param whereClause - The WHERE expression to analyze\n * @param outerAlias - The alias of the subquery in the outer query\n * @returns True if WHERE references computed fields, otherwise false\n */\nfunction whereReferencesComputedSelectFields(\n select: Select,\n whereClause: BasicExpression<boolean>,\n outerAlias: string\n): boolean {\n // Build a set of computed field names at the top-level of the subquery output\n const computed = new Set<string>()\n for (const [key, value] of Object.entries(select)) {\n if (key.startsWith(`__SPREAD_SENTINEL__`)) continue\n if (value instanceof PropRef) continue\n // Nested object or non-PropRef expression counts as computed\n computed.add(key)\n }\n\n const refs = collectRefs(whereClause)\n\n for (const ref of refs) {\n const path = (ref as any).path as Array<string>\n if (!Array.isArray(path) || path.length < 2) continue\n const alias = path[0]\n const field = path[1] as string\n if (alias !== outerAlias) continue\n if (computed.has(field)) return true\n }\n return false\n}\n\n/**\n * Detects whether a WHERE clause references the subquery alias through fields that\n * are re-exposed under different names (renamed SELECT projections or fnSelect output).\n * In those cases we keep the clause at the outer level to avoid alias remapping bugs.\n * TODO: in future we should handle this by rewriting the clause to use the subquery's\n * internal field references, but it likely needs a wider refactor to do cleanly.\n */\nfunction referencesAliasWithRemappedSelect(\n subquery: QueryIR,\n whereClause: BasicExpression<boolean>,\n outerAlias: string\n): boolean {\n const refs = collectRefs(whereClause)\n // Only care about clauses that actually reference the outer alias.\n if (refs.every((ref) => ref.path[0] !== outerAlias)) {\n return false\n }\n\n // fnSelect always rewrites the row shape, so alias-safe pushdown is impossible.\n if (subquery.fnSelect) {\n return true\n }\n\n const select = subquery.select\n // Without an explicit SELECT the clause still refers to the original collection.\n if (!select) {\n return false\n }\n\n for (const ref of refs) {\n const path = ref.path\n // Need at least alias + field to matter.\n if (path.length < 2) continue\n if (path[0] !== outerAlias) continue\n\n const projected = select[path[1]!]\n // Unselected fields can't be remapped, so skip - only care about fields in the SELECT.\n if (!projected) continue\n\n // Non-PropRef projections are computed values; cannot push down.\n if (!(projected instanceof PropRef)) {\n return true\n }\n\n // If the projection is just the alias (whole row) without a specific field,\n // we can't verify whether the field we're referencing is being preserved or remapped.\n if (projected.path.length < 2) {\n return true\n }\n\n const [innerAlias, innerField] = projected.path\n\n // Safe only when the projection points straight back to the same alias or the\n // underlying source alias and preserves the field name.\n if (innerAlias !== outerAlias && innerAlias !== subquery.from.alias) {\n return true\n }\n\n if (innerField !== path[1]) {\n return true\n }\n }\n\n return false\n}\n\n/**\n * Helper function to combine multiple expressions with AND.\n *\n * If there's only one expression, it's returned as-is.\n * If there are multiple expressions, they're combined with an AND function.\n *\n * @param expressions - Array of expressions to combine\n * @returns Single expression representing the AND combination\n * @throws Error if the expressions array is empty\n */\nfunction combineWithAnd(\n expressions: Array<BasicExpression<boolean>>\n): BasicExpression<boolean> {\n if (expressions.length === 0) {\n throw new CannotCombineEmptyExpressionListError()\n }\n\n if (expressions.length === 1) {\n return expressions[0]!\n }\n\n // Create an AND function with all expressions as arguments\n return new Func(`and`, expressions)\n}\n"],"names":["deepEquals","isConvertibleToCollectionFilter","QueryRefClass","isResidualWhere","CollectionRefClass","getWhereExpression","createResidualWhere","PropRef","expressions","CannotCombineEmptyExpressionListError","Func"],"mappings":";;;;;;AA+LO,SAAS,cAAc,OAAoC;AAEhE,QAAM,qBAAqB,0BAA0B,KAAK;AAG1D,MAAI,YAAY;AAChB,MAAI;AACJ,MAAI,aAAa;AACjB,QAAM,gBAAgB;AAGtB,SACE,aAAa,iBACb,CAACA,MAAAA,WAAW,WAAW,iBAAiB,GACxC;AACA,wBAAoB;AACpB,gBAAY,2BAA2B,SAAS;AAChD;AAAA,EACF;AAGA,QAAM,UAAU,0BAA0B,SAAS;AAEnD,SAAO;AAAA,IACL,gBAAgB;AAAA,IAChB;AAAA,EAAA;AAEJ;AAUA,SAAS,0BACP,OACuC;AACvC,QAAM,yCAAyB,IAAA;AAG/B,MAAI,CAAC,MAAM,SAAS,MAAM,MAAM,WAAW,GAAG;AA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,EACtB;AAGA,SAAO,IAAIE,GAAAA,KAAK,OAAOF,YAAW;AACpC;;"}
1
+ {"version":3,"file":"optimizer.cjs","sources":["../../../src/query/optimizer.ts"],"sourcesContent":["/**\n * # Query Optimizer\n *\n * The query optimizer improves query performance by implementing predicate pushdown optimization.\n * It rewrites the intermediate representation (IR) to push WHERE clauses as close to the data\n * source as possible, reducing the amount of data processed during joins.\n *\n * ## How It Works\n *\n * The optimizer follows a 4-step process:\n *\n * ### 1. AND Clause Splitting\n * Splits AND clauses at the root level into separate WHERE clauses for granular optimization.\n * ```javascript\n * // Before: WHERE and(eq(users.department_id, 1), gt(users.age, 25))\n * // After: WHERE eq(users.department_id, 1) + WHERE gt(users.age, 25)\n * ```\n *\n * ### 2. Source Analysis\n * Analyzes each WHERE clause to determine which table sources it references:\n * - Single-source clauses: Touch only one table (e.g., `users.department_id = 1`)\n * - Multi-source clauses: Touch multiple tables (e.g., `users.id = posts.user_id`)\n *\n * ### 3. Clause Grouping\n * Groups WHERE clauses by the sources they touch:\n * - Single-source clauses are grouped by their respective table\n * - Multi-source clauses are combined for the main query\n *\n * ### 4. Subquery Creation\n * Lifts single-source WHERE clauses into subqueries that wrap the original table references.\n *\n * ## Safety & Edge Cases\n *\n * The optimizer includes targeted safety checks to prevent predicate pushdown when it could\n * break query semantics:\n *\n * ### Always Safe Operations\n * - **Creating new subqueries**: Wrapping collection references in subqueries with WHERE clauses\n * - **Main query optimizations**: Moving single-source WHERE clauses from main query to subqueries\n * - **Queries with aggregates/ORDER BY/HAVING**: Can still create new filtered subqueries\n *\n * ### Unsafe Operations (blocked by safety checks)\n * Pushing WHERE clauses **into existing subqueries** that have:\n * - **Aggregates**: GROUP BY, HAVING, or aggregate functions in SELECT (would change aggregation)\n * - **Ordering + Limits**: ORDER BY combined with LIMIT/OFFSET (would change result set)\n * - **Functional Operations**: fnSelect, fnWhere, fnHaving (potential side effects)\n *\n * ### Residual WHERE Clauses\n * For outer joins (LEFT, RIGHT, FULL), WHERE clauses are copied to subqueries for optimization\n * but also kept as \"residual\" clauses in the main query to preserve semantics. This ensures\n * that NULL values from outer joins are properly filtered according to SQL standards.\n *\n * The optimizer tracks which clauses were actually optimized and only removes those from the\n * main query. Subquery reuse is handled safely through immutable query copies.\n *\n * ## Example Optimizations\n *\n * ### Basic Query with Joins\n * **Original Query:**\n * ```javascript\n * query\n * .from({ users: usersCollection })\n * .join({ posts: postsCollection }, ({users, posts}) => eq(users.id, posts.user_id))\n * .where(({users}) => eq(users.department_id, 1))\n * .where(({posts}) => gt(posts.views, 100))\n * .where(({users, posts}) => eq(users.id, posts.author_id))\n * ```\n *\n * **Optimized Query:**\n * ```javascript\n * query\n * .from({\n * users: subquery\n * .from({ users: usersCollection })\n * .where(({users}) => eq(users.department_id, 1))\n * })\n * .join({\n * posts: subquery\n * .from({ posts: postsCollection })\n * .where(({posts}) => gt(posts.views, 100))\n * }, ({users, posts}) => eq(users.id, posts.user_id))\n * .where(({users, posts}) => eq(users.id, posts.author_id))\n * ```\n *\n * ### Query with Aggregates (Now Optimizable!)\n * **Original Query:**\n * ```javascript\n * query\n * .from({ users: usersCollection })\n * .join({ posts: postsCollection }, ({users, posts}) => eq(users.id, posts.user_id))\n * .where(({users}) => eq(users.department_id, 1))\n * .groupBy(['users.department_id'])\n * .select({ count: agg('count', '*') })\n * ```\n *\n * **Optimized Query:**\n * ```javascript\n * query\n * .from({\n * users: subquery\n * .from({ users: usersCollection })\n * .where(({users}) => eq(users.department_id, 1))\n * })\n * .join({ posts: postsCollection }, ({users, posts}) => eq(users.id, posts.user_id))\n * .groupBy(['users.department_id'])\n * .select({ count: agg('count', '*') })\n * ```\n *\n * ## Benefits\n *\n * - **Reduced Data Processing**: Filters applied before joins reduce intermediate result size\n * - **Better Performance**: Smaller datasets lead to faster query execution\n * - **Automatic Optimization**: No manual query rewriting required\n * - **Preserves Semantics**: Optimized queries return identical results\n * - **Safe by Design**: Comprehensive checks prevent semantic-breaking optimizations\n *\n * ## Integration\n *\n * The optimizer is automatically called during query compilation before the IR is\n * transformed into a D2Mini pipeline.\n */\n\nimport { deepEquals } from \"../utils.js\"\nimport { CannotCombineEmptyExpressionListError } from \"../errors.js\"\nimport {\n CollectionRef as CollectionRefClass,\n Func,\n PropRef,\n QueryRef as QueryRefClass,\n createResidualWhere,\n getWhereExpression,\n isResidualWhere,\n} from \"./ir.js\"\nimport { isConvertibleToCollectionFilter } from \"./compiler/expressions.js\"\nimport type { BasicExpression, From, QueryIR, Select, Where } from \"./ir.js\"\n\n/**\n * Represents a WHERE clause after source analysis\n */\nexport interface AnalyzedWhereClause {\n /** The WHERE expression */\n expression: BasicExpression<boolean>\n /** Set of table/source aliases that this WHERE clause touches */\n touchedSources: Set<string>\n /** Whether this clause contains namespace-only references that prevent pushdown */\n hasNamespaceOnlyRef: boolean\n}\n\n/**\n * Represents WHERE clauses grouped by the sources they touch\n */\nexport interface GroupedWhereClauses {\n /** WHERE clauses that touch only a single source, grouped by source alias */\n singleSource: Map<string, BasicExpression<boolean>>\n /** WHERE clauses that touch multiple sources, combined into one expression */\n multiSource?: BasicExpression<boolean>\n}\n\n/**\n * Result of query optimization including both the optimized query and collection-specific WHERE clauses\n */\nexport interface OptimizationResult {\n /** The optimized query with WHERE clauses potentially moved to subqueries */\n optimizedQuery: QueryIR\n /** Map of source aliases to their extracted WHERE clauses for index optimization */\n sourceWhereClauses: Map<string, BasicExpression<boolean>>\n}\n\n/**\n * Main query optimizer entry point that lifts WHERE clauses into subqueries.\n *\n * This function implements multi-level predicate pushdown optimization by recursively\n * moving WHERE clauses through nested subqueries to get them as close to the data\n * sources as possible, then removing redundant subqueries.\n *\n * @param query - The QueryIR to optimize\n * @returns An OptimizationResult with the optimized query and collection WHERE clause mapping\n *\n * @example\n * ```typescript\n * const originalQuery = {\n * from: new CollectionRef(users, 'u'),\n * join: [{ from: new CollectionRef(posts, 'p'), ... }],\n * where: [eq(u.dept_id, 1), gt(p.views, 100)]\n * }\n *\n * const { optimizedQuery, sourceWhereClauses } = optimizeQuery(originalQuery)\n * // Result: Single-source clauses moved to deepest possible subqueries\n * // sourceWhereClauses: Map { 'u' => eq(u.dept_id, 1), 'p' => gt(p.views, 100) }\n * ```\n */\nexport function optimizeQuery(query: QueryIR): OptimizationResult {\n // First, extract source WHERE clauses before optimization\n const sourceWhereClauses = extractSourceWhereClauses(query)\n\n // Apply multi-level predicate pushdown with iterative convergence\n let optimized = query\n let previousOptimized: QueryIR | undefined\n let iterations = 0\n const maxIterations = 10 // Prevent infinite loops\n\n // Keep optimizing until no more changes occur or max iterations reached\n while (\n iterations < maxIterations &&\n !deepEquals(optimized, previousOptimized)\n ) {\n previousOptimized = optimized\n optimized = applyRecursiveOptimization(optimized)\n iterations++\n }\n\n // Remove redundant subqueries\n const cleaned = removeRedundantSubqueries(optimized)\n\n return {\n optimizedQuery: cleaned,\n sourceWhereClauses,\n }\n}\n\n/**\n * Extracts collection-specific WHERE clauses from a query for index optimization.\n * This analyzes the original query to identify WHERE clauses that can be pushed down\n * to specific collections, but only for simple queries without joins.\n *\n * @param query - The original QueryIR to analyze\n * @returns Map of source aliases to their WHERE clauses\n */\nfunction extractSourceWhereClauses(\n query: QueryIR\n): Map<string, BasicExpression<boolean>> {\n const sourceWhereClauses = new Map<string, BasicExpression<boolean>>()\n\n // Only analyze queries that have WHERE clauses\n if (!query.where || query.where.length === 0) {\n return sourceWhereClauses\n }\n\n // Split all AND clauses at the root level for granular analysis\n const splitWhereClauses = splitAndClauses(query.where)\n\n // Analyze each WHERE clause to determine which sources it touches\n const analyzedClauses = splitWhereClauses.map((clause) =>\n analyzeWhereClause(clause)\n )\n\n // Group clauses by single-source vs multi-source\n const groupedClauses = groupWhereClauses(analyzedClauses)\n\n // Only include single-source clauses that reference collections directly\n // and can be converted to BasicExpression format for collection indexes\n for (const [sourceAlias, whereClause] of groupedClauses.singleSource) {\n // Check if this source alias corresponds to a collection reference\n if (isCollectionReference(query, sourceAlias)) {\n // Check if the WHERE clause can be converted to collection-compatible format\n if (isConvertibleToCollectionFilter(whereClause)) {\n sourceWhereClauses.set(sourceAlias, whereClause)\n }\n }\n }\n\n return sourceWhereClauses\n}\n\n/**\n * Determines if a source alias refers to a collection reference (not a subquery).\n * This is used to identify WHERE clauses that can be pushed down to collection subscriptions.\n *\n * @param query - The query to analyze\n * @param sourceAlias - The source alias to check\n * @returns True if the alias refers to a collection reference\n */\nfunction isCollectionReference(query: QueryIR, sourceAlias: string): boolean {\n // Check the FROM clause\n if (query.from.alias === sourceAlias) {\n return query.from.type === `collectionRef`\n }\n\n // Check JOIN clauses\n if (query.join) {\n for (const joinClause of query.join) {\n if (joinClause.from.alias === sourceAlias) {\n return joinClause.from.type === `collectionRef`\n }\n }\n }\n\n return false\n}\n\n/**\n * Applies recursive predicate pushdown optimization.\n *\n * @param query - The QueryIR to optimize\n * @returns A new QueryIR with optimizations applied\n */\nfunction applyRecursiveOptimization(query: QueryIR): QueryIR {\n // First, recursively optimize any existing subqueries\n const subqueriesOptimized = {\n ...query,\n from:\n query.from.type === `queryRef`\n ? new QueryRefClass(\n applyRecursiveOptimization(query.from.query),\n query.from.alias\n )\n : query.from,\n join: query.join?.map((joinClause) => ({\n ...joinClause,\n from:\n joinClause.from.type === `queryRef`\n ? new QueryRefClass(\n applyRecursiveOptimization(joinClause.from.query),\n joinClause.from.alias\n )\n : joinClause.from,\n })),\n }\n\n // Then apply single-level optimization to this query\n return applySingleLevelOptimization(subqueriesOptimized)\n}\n\n/**\n * Applies single-level predicate pushdown optimization (existing logic)\n */\nfunction applySingleLevelOptimization(query: QueryIR): QueryIR {\n // Skip optimization if no WHERE clauses exist\n if (!query.where || query.where.length === 0) {\n return query\n }\n\n // For queries without joins, combine multiple WHERE clauses into a single clause\n // to avoid creating multiple filter operators in the pipeline\n if (!query.join || query.join.length === 0) {\n // Only optimize if there are multiple WHERE clauses to combine\n if (query.where.length > 1) {\n // Combine multiple WHERE clauses into a single AND expression\n const splitWhereClauses = splitAndClauses(query.where)\n const combinedWhere = combineWithAnd(splitWhereClauses)\n\n return {\n ...query,\n where: [combinedWhere],\n }\n }\n\n // For single WHERE clauses, no optimization needed\n return query\n }\n\n // Filter out residual WHERE clauses to prevent them from being optimized again\n const nonResidualWhereClauses = query.where.filter(\n (where) => !isResidualWhere(where)\n )\n\n // Step 1: Split all AND clauses at the root level for granular optimization\n const splitWhereClauses = splitAndClauses(nonResidualWhereClauses)\n\n // Step 2: Analyze each WHERE clause to determine which sources it touches\n const analyzedClauses = splitWhereClauses.map((clause) =>\n analyzeWhereClause(clause)\n )\n\n // Step 3: Group clauses by single-source vs multi-source\n const groupedClauses = groupWhereClauses(analyzedClauses)\n\n // Step 4: Apply optimizations by lifting single-source clauses into subqueries\n const optimizedQuery = applyOptimizations(query, groupedClauses)\n\n // Add back any residual WHERE clauses that were filtered out\n const residualWhereClauses = query.where.filter((where) =>\n isResidualWhere(where)\n )\n if (residualWhereClauses.length > 0) {\n optimizedQuery.where = [\n ...(optimizedQuery.where || []),\n ...residualWhereClauses,\n ]\n }\n\n return optimizedQuery\n}\n\n/**\n * Removes redundant subqueries that don't add value.\n * A subquery is redundant if it only wraps another query without adding\n * WHERE, SELECT, GROUP BY, HAVING, ORDER BY, or LIMIT/OFFSET clauses.\n *\n * @param query - The QueryIR to process\n * @returns A new QueryIR with redundant subqueries removed\n */\nfunction removeRedundantSubqueries(query: QueryIR): QueryIR {\n return {\n ...query,\n from: removeRedundantFromClause(query.from),\n join: query.join?.map((joinClause) => ({\n ...joinClause,\n from: removeRedundantFromClause(joinClause.from),\n })),\n }\n}\n\n/**\n * Removes redundant subqueries from a FROM clause.\n *\n * @param from - The FROM clause to process\n * @returns A FROM clause with redundant subqueries removed\n */\nfunction removeRedundantFromClause(from: From): From {\n if (from.type === `collectionRef`) {\n return from\n }\n\n const processedQuery = removeRedundantSubqueries(from.query)\n\n // Check if this subquery is redundant\n if (isRedundantSubquery(processedQuery)) {\n // Return the inner query's FROM clause with this alias\n const innerFrom = removeRedundantFromClause(processedQuery.from)\n if (innerFrom.type === `collectionRef`) {\n return new CollectionRefClass(innerFrom.collection, from.alias)\n } else {\n return new QueryRefClass(innerFrom.query, from.alias)\n }\n }\n\n return new QueryRefClass(processedQuery, from.alias)\n}\n\n/**\n * Determines if a subquery is redundant (adds no value).\n *\n * @param query - The query to check\n * @returns True if the query is redundant and can be removed\n */\nfunction isRedundantSubquery(query: QueryIR): boolean {\n return (\n (!query.where || query.where.length === 0) &&\n !query.select &&\n (!query.groupBy || query.groupBy.length === 0) &&\n (!query.having || query.having.length === 0) &&\n (!query.orderBy || query.orderBy.length === 0) &&\n (!query.join || query.join.length === 0) &&\n query.limit === undefined &&\n query.offset === undefined &&\n !query.fnSelect &&\n (!query.fnWhere || query.fnWhere.length === 0) &&\n (!query.fnHaving || query.fnHaving.length === 0)\n )\n}\n\n/**\n * Step 1: Split all AND clauses recursively into separate WHERE clauses.\n *\n * This enables more granular optimization by treating each condition independently.\n * OR clauses are preserved as they cannot be split without changing query semantics.\n *\n * @param whereClauses - Array of WHERE expressions to split\n * @returns Flattened array with AND clauses split into separate expressions\n *\n * @example\n * ```typescript\n * // Input: [and(eq(a, 1), gt(b, 2)), eq(c, 3)]\n * // Output: [eq(a, 1), gt(b, 2), eq(c, 3)]\n * ```\n */\nfunction splitAndClauses(\n whereClauses: Array<Where>\n): Array<BasicExpression<boolean>> {\n const result: Array<BasicExpression<boolean>> = []\n\n for (const whereClause of whereClauses) {\n const clause = getWhereExpression(whereClause)\n result.push(...splitAndClausesRecursive(clause))\n }\n\n return result\n}\n\n// Helper function for recursive splitting of BasicExpression arrays\nfunction splitAndClausesRecursive(\n clause: BasicExpression<boolean>\n): Array<BasicExpression<boolean>> {\n if (clause.type === `func` && clause.name === `and`) {\n // Recursively split nested AND clauses to handle complex expressions\n const result: Array<BasicExpression<boolean>> = []\n for (const arg of clause.args as Array<BasicExpression<boolean>>) {\n result.push(...splitAndClausesRecursive(arg))\n }\n return result\n } else {\n // Preserve non-AND clauses as-is (including OR clauses)\n return [clause]\n }\n}\n\n/**\n * Step 2: Analyze which table sources a WHERE clause touches.\n *\n * This determines whether a clause can be pushed down to a specific table\n * or must remain in the main query (for multi-source clauses like join conditions).\n *\n * Special handling for namespace-only references in outer joins:\n * WHERE clauses that reference only a table namespace (e.g., isUndefined(special), eq(special, value))\n * rather than specific properties (e.g., isUndefined(special.id), eq(special.id, value)) are treated as\n * multi-source to prevent incorrect predicate pushdown that would change join semantics.\n *\n * @param clause - The WHERE expression to analyze\n * @returns Analysis result with the expression and touched source aliases\n *\n * @example\n * ```typescript\n * // eq(users.department_id, 1) -> touches ['users'], hasNamespaceOnlyRef: false\n * // eq(users.id, posts.user_id) -> touches ['users', 'posts'], hasNamespaceOnlyRef: false\n * // isUndefined(special) -> touches ['special'], hasNamespaceOnlyRef: true (prevents pushdown)\n * // eq(special, someValue) -> touches ['special'], hasNamespaceOnlyRef: true (prevents pushdown)\n * // isUndefined(special.id) -> touches ['special'], hasNamespaceOnlyRef: false (allows pushdown)\n * // eq(special.id, 5) -> touches ['special'], hasNamespaceOnlyRef: false (allows pushdown)\n * ```\n */\nfunction analyzeWhereClause(\n clause: BasicExpression<boolean>\n): AnalyzedWhereClause {\n // Track which table aliases this WHERE clause touches\n const touchedSources = new Set<string>()\n // Track whether this clause contains namespace-only references that prevent pushdown\n let hasNamespaceOnlyRef = false\n\n /**\n * Recursively collect all table aliases referenced in an expression\n */\n function collectSources(expr: BasicExpression | any): void {\n switch (expr.type) {\n case `ref`:\n // PropRef path has the table alias as the first element\n if (expr.path && expr.path.length > 0) {\n const firstElement = expr.path[0]\n if (firstElement) {\n touchedSources.add(firstElement)\n\n // If the path has only one element (just the namespace),\n // this is a namespace-only reference that should not be pushed down\n // This applies to ANY function, not just existence-checking functions\n if (expr.path.length === 1) {\n hasNamespaceOnlyRef = true\n }\n }\n }\n break\n case `func`:\n // Recursively analyze function arguments (e.g., eq, gt, and, or)\n if (expr.args) {\n expr.args.forEach(collectSources)\n }\n break\n case `val`:\n // Values don't reference any sources\n break\n case `agg`:\n // Aggregates can reference sources in their arguments\n if (expr.args) {\n expr.args.forEach(collectSources)\n }\n break\n }\n }\n\n collectSources(clause)\n\n return {\n expression: clause,\n touchedSources,\n hasNamespaceOnlyRef,\n }\n}\n\n/**\n * Step 3: Group WHERE clauses by the sources they touch.\n *\n * Single-source clauses can be pushed down to subqueries for optimization.\n * Multi-source clauses must remain in the main query to preserve join semantics.\n *\n * @param analyzedClauses - Array of analyzed WHERE clauses\n * @returns Grouped clauses ready for optimization\n */\nfunction groupWhereClauses(\n analyzedClauses: Array<AnalyzedWhereClause>\n): GroupedWhereClauses {\n const singleSource = new Map<string, Array<BasicExpression<boolean>>>()\n const multiSource: Array<BasicExpression<boolean>> = []\n\n // Categorize each clause based on how many sources it touches\n for (const clause of analyzedClauses) {\n if (clause.touchedSources.size === 1 && !clause.hasNamespaceOnlyRef) {\n // Single source clause without namespace-only references - can be optimized\n const source = Array.from(clause.touchedSources)[0]!\n if (!singleSource.has(source)) {\n singleSource.set(source, [])\n }\n singleSource.get(source)!.push(clause.expression)\n } else if (clause.touchedSources.size > 1 || clause.hasNamespaceOnlyRef) {\n // Multi-source clause or namespace-only reference - must stay in main query\n multiSource.push(clause.expression)\n }\n // Skip clauses that touch no sources (constants) - they don't need optimization\n }\n\n // Combine multiple clauses for each source with AND\n const combinedSingleSource = new Map<string, BasicExpression<boolean>>()\n for (const [source, clauses] of singleSource) {\n combinedSingleSource.set(source, combineWithAnd(clauses))\n }\n\n // Combine multi-source clauses with AND\n const combinedMultiSource =\n multiSource.length > 0 ? combineWithAnd(multiSource) : undefined\n\n return {\n singleSource: combinedSingleSource,\n multiSource: combinedMultiSource,\n }\n}\n\n/**\n * Step 4: Apply optimizations by lifting single-source clauses into subqueries.\n *\n * Creates a new QueryIR with single-source WHERE clauses moved to subqueries\n * that wrap the original table references. This ensures immutability and prevents\n * infinite recursion issues.\n *\n * @param query - Original QueryIR to optimize\n * @param groupedClauses - WHERE clauses grouped by optimization strategy\n * @returns New QueryIR with optimizations applied\n */\nfunction applyOptimizations(\n query: QueryIR,\n groupedClauses: GroupedWhereClauses\n): QueryIR {\n // Track which single-source clauses were actually optimized\n const actuallyOptimized = new Set<string>()\n\n // Optimize the main FROM clause and track what was optimized\n const optimizedFrom = optimizeFromWithTracking(\n query.from,\n groupedClauses.singleSource,\n actuallyOptimized\n )\n\n // Optimize JOIN clauses and track what was optimized\n const optimizedJoins = query.join\n ? query.join.map((joinClause) => ({\n ...joinClause,\n from: optimizeFromWithTracking(\n joinClause.from,\n groupedClauses.singleSource,\n actuallyOptimized\n ),\n }))\n : undefined\n\n // Build the remaining WHERE clauses: multi-source + residual single-source clauses\n const remainingWhereClauses: Array<Where> = []\n\n // Add multi-source clauses\n if (groupedClauses.multiSource) {\n remainingWhereClauses.push(groupedClauses.multiSource)\n }\n\n // Determine if we need residual clauses (when query has outer JOINs)\n const hasOuterJoins =\n query.join &&\n query.join.some(\n (join) =>\n join.type === `left` || join.type === `right` || join.type === `full`\n )\n\n // Add single-source clauses\n for (const [source, clause] of groupedClauses.singleSource) {\n if (!actuallyOptimized.has(source)) {\n // Wasn't optimized at all - keep as regular WHERE clause\n remainingWhereClauses.push(clause)\n } else if (hasOuterJoins) {\n // Was optimized AND query has outer JOINs - keep as residual WHERE clause\n remainingWhereClauses.push(createResidualWhere(clause))\n }\n // If optimized and no outer JOINs - don't keep (original behavior)\n }\n\n // Combine multiple remaining WHERE clauses into a single clause to avoid\n // multiple filter operations in the pipeline (performance optimization)\n // First flatten any nested AND expressions to avoid and(and(...), ...)\n const finalWhere: Array<Where> =\n remainingWhereClauses.length > 1\n ? [\n combineWithAnd(\n remainingWhereClauses.flatMap((clause) =>\n splitAndClausesRecursive(getWhereExpression(clause))\n )\n ),\n ]\n : remainingWhereClauses\n\n // Create a completely new query object to ensure immutability\n const optimizedQuery: QueryIR = {\n // Copy all non-optimized fields as-is\n select: query.select,\n groupBy: query.groupBy ? [...query.groupBy] : undefined,\n having: query.having ? [...query.having] : undefined,\n orderBy: query.orderBy ? [...query.orderBy] : undefined,\n limit: query.limit,\n offset: query.offset,\n distinct: query.distinct,\n fnSelect: query.fnSelect,\n fnWhere: query.fnWhere ? [...query.fnWhere] : undefined,\n fnHaving: query.fnHaving ? [...query.fnHaving] : undefined,\n\n // Use the optimized FROM and JOIN clauses\n from: optimizedFrom,\n join: optimizedJoins,\n\n // Include combined WHERE clauses\n where: finalWhere.length > 0 ? finalWhere : [],\n }\n\n return optimizedQuery\n}\n\n/**\n * Helper function to create a deep copy of a QueryIR object for immutability.\n *\n * This ensures that all optimizations create new objects rather than modifying\n * existing ones, preventing infinite recursion and shared reference issues.\n *\n * @param query - QueryIR to deep copy\n * @returns New QueryIR object with all nested objects copied\n */\nfunction deepCopyQuery(query: QueryIR): QueryIR {\n return {\n // Recursively copy the FROM clause\n from:\n query.from.type === `collectionRef`\n ? new CollectionRefClass(query.from.collection, query.from.alias)\n : new QueryRefClass(deepCopyQuery(query.from.query), query.from.alias),\n\n // Copy all other fields, creating new arrays where necessary\n select: query.select,\n join: query.join\n ? query.join.map((joinClause) => ({\n type: joinClause.type,\n left: joinClause.left,\n right: joinClause.right,\n from:\n joinClause.from.type === `collectionRef`\n ? new CollectionRefClass(\n joinClause.from.collection,\n joinClause.from.alias\n )\n : new QueryRefClass(\n deepCopyQuery(joinClause.from.query),\n joinClause.from.alias\n ),\n }))\n : undefined,\n where: query.where ? [...query.where] : undefined,\n groupBy: query.groupBy ? [...query.groupBy] : undefined,\n having: query.having ? [...query.having] : undefined,\n orderBy: query.orderBy ? [...query.orderBy] : undefined,\n limit: query.limit,\n offset: query.offset,\n fnSelect: query.fnSelect,\n fnWhere: query.fnWhere ? [...query.fnWhere] : undefined,\n fnHaving: query.fnHaving ? [...query.fnHaving] : undefined,\n }\n}\n\n/**\n * Helper function to optimize a FROM clause while tracking what was actually optimized.\n *\n * @param from - FROM clause to optimize\n * @param singleSourceClauses - Map of source aliases to their WHERE clauses\n * @param actuallyOptimized - Set to track which sources were actually optimized\n * @returns New FROM clause, potentially wrapped in a subquery\n */\nfunction optimizeFromWithTracking(\n from: From,\n singleSourceClauses: Map<string, BasicExpression<boolean>>,\n actuallyOptimized: Set<string>\n): From {\n const whereClause = singleSourceClauses.get(from.alias)\n\n if (!whereClause) {\n // No optimization needed, but return a copy to maintain immutability\n if (from.type === `collectionRef`) {\n return new CollectionRefClass(from.collection, from.alias)\n }\n // Must be queryRef due to type system\n return new QueryRefClass(deepCopyQuery(from.query), from.alias)\n }\n\n if (from.type === `collectionRef`) {\n // Create a new subquery with the WHERE clause for the collection\n // This is always safe since we're creating a new subquery\n const subQuery: QueryIR = {\n from: new CollectionRefClass(from.collection, from.alias),\n where: [whereClause],\n }\n actuallyOptimized.add(from.alias) // Mark as successfully optimized\n return new QueryRefClass(subQuery, from.alias)\n }\n\n // SAFETY CHECK: Only check safety when pushing WHERE clauses into existing subqueries\n // We need to be careful about pushing WHERE clauses into subqueries that already have\n // aggregates, HAVING, or ORDER BY + LIMIT since that could change their semantics\n if (!isSafeToPushIntoExistingSubquery(from.query, whereClause, from.alias)) {\n // Return a copy without optimization to maintain immutability\n // Do NOT mark as optimized since we didn't actually optimize it\n return new QueryRefClass(deepCopyQuery(from.query), from.alias)\n }\n\n // Skip pushdown when a clause references a field that only exists via a renamed\n // projection inside the subquery; leaving it outside preserves the alias mapping.\n if (referencesAliasWithRemappedSelect(from.query, whereClause, from.alias)) {\n return new QueryRefClass(deepCopyQuery(from.query), from.alias)\n }\n\n // Add the WHERE clause to the existing subquery\n // Create a deep copy to ensure immutability\n const existingWhere = from.query.where || []\n const optimizedSubQuery: QueryIR = {\n ...deepCopyQuery(from.query),\n where: [...existingWhere, whereClause],\n }\n actuallyOptimized.add(from.alias) // Mark as successfully optimized\n return new QueryRefClass(optimizedSubQuery, from.alias)\n}\n\nfunction unsafeSelect(\n query: QueryIR,\n whereClause: BasicExpression<boolean>,\n outerAlias: string\n): boolean {\n if (!query.select) return false\n\n return (\n selectHasAggregates(query.select) ||\n whereReferencesComputedSelectFields(query.select, whereClause, outerAlias)\n )\n}\n\nfunction unsafeGroupBy(query: QueryIR) {\n return query.groupBy && query.groupBy.length > 0\n}\n\nfunction unsafeHaving(query: QueryIR) {\n return query.having && query.having.length > 0\n}\n\nfunction unsafeOrderBy(query: QueryIR) {\n return (\n query.orderBy &&\n query.orderBy.length > 0 &&\n (query.limit !== undefined || query.offset !== undefined)\n )\n}\n\nfunction unsafeFnSelect(query: QueryIR) {\n return (\n query.fnSelect ||\n (query.fnWhere && query.fnWhere.length > 0) ||\n (query.fnHaving && query.fnHaving.length > 0)\n )\n}\n\nfunction isSafeToPushIntoExistingSubquery(\n query: QueryIR,\n whereClause: BasicExpression<boolean>,\n outerAlias: string\n): boolean {\n return !(\n unsafeSelect(query, whereClause, outerAlias) ||\n unsafeGroupBy(query) ||\n unsafeHaving(query) ||\n unsafeOrderBy(query) ||\n unsafeFnSelect(query)\n )\n}\n\n/**\n * Detects whether a SELECT projection contains any aggregate expressions.\n * Recursively traverses nested select objects.\n *\n * @param select - The SELECT object from the IR\n * @returns True if any field is an aggregate, false otherwise\n */\nfunction selectHasAggregates(select: Select): boolean {\n for (const value of Object.values(select)) {\n if (typeof value === `object`) {\n const v: any = value\n if (v.type === `agg`) return true\n if (!(`type` in v)) {\n if (selectHasAggregates(v as unknown as Select)) return true\n }\n }\n }\n return false\n}\n\n/**\n * Recursively collects all PropRef references from an expression.\n *\n * @param expr - The expression to traverse\n * @returns Array of PropRef references found in the expression\n */\nfunction collectRefs(expr: any): Array<PropRef> {\n const refs: Array<PropRef> = []\n\n if (expr == null || typeof expr !== `object`) return refs\n\n switch (expr.type) {\n case `ref`:\n refs.push(expr as PropRef)\n break\n case `func`:\n case `agg`:\n for (const arg of expr.args ?? []) {\n refs.push(...collectRefs(arg))\n }\n break\n default:\n break\n }\n\n return refs\n}\n\n/**\n * Determines whether the provided WHERE clause references fields that are\n * computed by a subquery SELECT rather than pass-through properties.\n *\n * If true, pushing the WHERE clause into the subquery could change semantics\n * (since computed fields do not necessarily exist at the subquery input level),\n * so predicate pushdown must be avoided.\n *\n * @param select - The subquery SELECT map\n * @param whereClause - The WHERE expression to analyze\n * @param outerAlias - The alias of the subquery in the outer query\n * @returns True if WHERE references computed fields, otherwise false\n */\nfunction whereReferencesComputedSelectFields(\n select: Select,\n whereClause: BasicExpression<boolean>,\n outerAlias: string\n): boolean {\n // Build a set of computed field names at the top-level of the subquery output\n const computed = new Set<string>()\n for (const [key, value] of Object.entries(select)) {\n if (key.startsWith(`__SPREAD_SENTINEL__`)) continue\n if (value instanceof PropRef) continue\n // Nested object or non-PropRef expression counts as computed\n computed.add(key)\n }\n\n const refs = collectRefs(whereClause)\n\n for (const ref of refs) {\n const path = (ref as any).path as Array<string>\n if (!Array.isArray(path) || path.length < 2) continue\n const alias = path[0]\n const field = path[1] as string\n if (alias !== outerAlias) continue\n if (computed.has(field)) return true\n }\n return false\n}\n\n/**\n * Detects whether a WHERE clause references the subquery alias through fields that\n * are re-exposed under different names (renamed SELECT projections or fnSelect output).\n * In those cases we keep the clause at the outer level to avoid alias remapping bugs.\n * TODO: in future we should handle this by rewriting the clause to use the subquery's\n * internal field references, but it likely needs a wider refactor to do cleanly.\n */\nfunction referencesAliasWithRemappedSelect(\n subquery: QueryIR,\n whereClause: BasicExpression<boolean>,\n outerAlias: string\n): boolean {\n const refs = collectRefs(whereClause)\n // Only care about clauses that actually reference the outer alias.\n if (refs.every((ref) => ref.path[0] !== outerAlias)) {\n return false\n }\n\n // fnSelect always rewrites the row shape, so alias-safe pushdown is impossible.\n if (subquery.fnSelect) {\n return true\n }\n\n const select = subquery.select\n // Without an explicit SELECT the clause still refers to the original collection.\n if (!select) {\n return false\n }\n\n for (const ref of refs) {\n const path = ref.path\n // Need at least alias + field to matter.\n if (path.length < 2) continue\n if (path[0] !== outerAlias) continue\n\n const projected = select[path[1]!]\n // Unselected fields can't be remapped, so skip - only care about fields in the SELECT.\n if (!projected) continue\n\n // Non-PropRef projections are computed values; cannot push down.\n if (!(projected instanceof PropRef)) {\n return true\n }\n\n // If the projection is just the alias (whole row) without a specific field,\n // we can't verify whether the field we're referencing is being preserved or remapped.\n if (projected.path.length < 2) {\n return true\n }\n\n const [innerAlias, innerField] = projected.path\n\n // Safe only when the projection points straight back to the same alias or the\n // underlying source alias and preserves the field name.\n if (innerAlias !== outerAlias && innerAlias !== subquery.from.alias) {\n return true\n }\n\n if (innerField !== path[1]) {\n return true\n }\n }\n\n return false\n}\n\n/**\n * Helper function to combine multiple expressions with AND.\n *\n * If there's only one expression, it's returned as-is.\n * If there are multiple expressions, they're combined with an AND function.\n *\n * @param expressions - Array of expressions to combine\n * @returns Single expression representing the AND combination\n * @throws Error if the expressions array is empty\n */\nfunction combineWithAnd(\n expressions: Array<BasicExpression<boolean>>\n): BasicExpression<boolean> {\n if (expressions.length === 0) {\n throw new CannotCombineEmptyExpressionListError()\n }\n\n if (expressions.length === 1) {\n return expressions[0]!\n }\n\n // Create an AND function with all expressions as arguments\n return new Func(`and`, expressions)\n}\n"],"names":["deepEquals","isConvertibleToCollectionFilter","QueryRefClass","splitWhereClauses","isResidualWhere","CollectionRefClass","getWhereExpression","createResidualWhere","PropRef","expressions","CannotCombineEmptyExpressionListError","Func"],"mappings":";;;;;;AA+LO,SAAS,cAAc,OAAoC;AAEhE,QAAM,qBAAqB,0BAA0B,KAAK;AAG1D,MAAI,YAAY;AAChB,MAAI;AACJ,MAAI,aAAa;AACjB,QAAM,gBAAgB;AAGtB,SACE,aAAa,iBACb,CAACA,MAAAA,WAAW,WAAW,iBAAiB,GACxC;AACA,wBAAoB;AACpB,gBAAY,2BAA2B,SAAS;AAChD;AAAA,EACF;AAGA,QAAM,UAAU,0BAA0B,SAAS;AAEnD,SAAO;AAAA,IACL,gBAAgB;AAAA,IAChB;AAAA,EAAA;AAEJ;AAUA,SAAS,0BACP,OACuC;AACvC,QAAM,yCAAyB,IAAA;AAG/B,MAAI,CAAC,MAAM,SAAS,MAAM,MAAM,WAAW,GAAG;A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;AAEA,UAAM,CAAC,YAAY,UAAU,IAAI,UAAU;AAI3C,QAAI,eAAe,cAAc,eAAe,SAAS,KAAK,OAAO;AACnE,aAAO;AAAA,IACT;AAEA,QAAI,eAAe,KAAK,CAAC,GAAG;AAC1B,aAAO;AAAA,IACT;AAAA,EACF;AAEA,SAAO;AACT;AAYA,SAAS,eACPC,cAC0B;AAC1B,MAAIA,aAAY,WAAW,GAAG;AAC5B,UAAM,IAAIC,OAAAA,sCAAA;AAAA,EACZ;AAEA,MAAID,aAAY,WAAW,GAAG;AAC5B,WAAOA,aAAY,CAAC;AAAA,EACtB;AAGA,SAAO,IAAIE,GAAAA,KAAK,OAAOF,YAAW;AACpC;;"}
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const debouncer = require("@tanstack/pacer/debouncer");
4
+ function debounceStrategy(options) {
5
+ const debouncer$1 = new debouncer.Debouncer(
6
+ (callback) => callback(),
7
+ options
8
+ );
9
+ return {
10
+ _type: `debounce`,
11
+ options,
12
+ execute: (fn) => {
13
+ debouncer$1.maybeExecute(fn);
14
+ },
15
+ cleanup: () => {
16
+ debouncer$1.cancel();
17
+ }
18
+ };
19
+ }
20
+ exports.debounceStrategy = debounceStrategy;
21
+ //# sourceMappingURL=debounceStrategy.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"debounceStrategy.cjs","sources":["../../../src/strategies/debounceStrategy.ts"],"sourcesContent":["import { Debouncer } from \"@tanstack/pacer/debouncer\"\nimport type { DebounceStrategy, DebounceStrategyOptions } from \"./types\"\nimport type { Transaction } from \"../transactions\"\n\n/**\n * Creates a debounce strategy that delays transaction execution until after\n * a period of inactivity.\n *\n * Ideal for scenarios like search inputs or auto-save fields where you want\n * to wait for the user to stop typing before persisting changes.\n *\n * @param options - Configuration for the debounce behavior\n * @returns A debounce strategy instance\n *\n * @example\n * ```ts\n * const mutate = useSerializedTransaction({\n * mutationFn: async ({ transaction }) => {\n * await api.save(transaction.mutations)\n * },\n * strategy: debounceStrategy({ wait: 500 })\n * })\n * ```\n */\nexport function debounceStrategy(\n options: DebounceStrategyOptions\n): DebounceStrategy {\n const debouncer = new Debouncer(\n (callback: () => Transaction) => callback(),\n options\n )\n\n return {\n _type: `debounce`,\n options,\n execute: <T extends object = Record<string, unknown>>(\n fn: () => Transaction<T>\n ) => {\n debouncer.maybeExecute(fn as () => Transaction)\n },\n cleanup: () => {\n debouncer.cancel()\n },\n }\n}\n"],"names":["debouncer","Debouncer"],"mappings":";;;AAwBO,SAAS,iBACd,SACkB;AAClB,QAAMA,cAAY,IAAIC,UAAAA;AAAAA,IACpB,CAAC,aAAgC,SAAA;AAAA,IACjC;AAAA,EAAA;AAGF,SAAO;AAAA,IACL,OAAO;AAAA,IACP;AAAA,IACA,SAAS,CACP,OACG;AACHD,kBAAU,aAAa,EAAuB;AAAA,IAChD;AAAA,IACA,SAAS,MAAM;AACbA,kBAAU,OAAA;AAAA,IACZ;AAAA,EAAA;AAEJ;;"}
@@ -0,0 +1,22 @@
1
+ import { DebounceStrategy, DebounceStrategyOptions } from './types.cjs';
2
+ /**
3
+ * Creates a debounce strategy that delays transaction execution until after
4
+ * a period of inactivity.
5
+ *
6
+ * Ideal for scenarios like search inputs or auto-save fields where you want
7
+ * to wait for the user to stop typing before persisting changes.
8
+ *
9
+ * @param options - Configuration for the debounce behavior
10
+ * @returns A debounce strategy instance
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * const mutate = useSerializedTransaction({
15
+ * mutationFn: async ({ transaction }) => {
16
+ * await api.save(transaction.mutations)
17
+ * },
18
+ * strategy: debounceStrategy({ wait: 500 })
19
+ * })
20
+ * ```
21
+ */
22
+ export declare function debounceStrategy(options: DebounceStrategyOptions): DebounceStrategy;
@@ -0,0 +1,4 @@
1
+ export { debounceStrategy } from './debounceStrategy.cjs';
2
+ export { queueStrategy } from './queueStrategy.cjs';
3
+ export { throttleStrategy } from './throttleStrategy.cjs';
4
+ export type { Strategy, BaseStrategy, DebounceStrategy, DebounceStrategyOptions, QueueStrategy, QueueStrategyOptions, ThrottleStrategy, ThrottleStrategyOptions, StrategyOptions, } from './types.cjs';
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const asyncQueuer = require("@tanstack/pacer/async-queuer");
4
+ function queueStrategy(options) {
5
+ const queuer = new asyncQueuer.AsyncQueuer({
6
+ concurrency: 1,
7
+ // Process one at a time to ensure serialization
8
+ wait: options?.wait,
9
+ maxSize: options?.maxSize,
10
+ addItemsTo: options?.addItemsTo ?? `back`,
11
+ // Default FIFO: add to back
12
+ getItemsFrom: options?.getItemsFrom ?? `front`,
13
+ // Default FIFO: get from front
14
+ started: true
15
+ // Start processing immediately
16
+ });
17
+ return {
18
+ _type: `queue`,
19
+ options,
20
+ execute: (fn) => {
21
+ queuer.addItem(async () => {
22
+ const transaction = fn();
23
+ await transaction.isPersisted.promise;
24
+ });
25
+ },
26
+ cleanup: () => {
27
+ queuer.stop();
28
+ queuer.clear();
29
+ }
30
+ };
31
+ }
32
+ exports.queueStrategy = queueStrategy;
33
+ //# sourceMappingURL=queueStrategy.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queueStrategy.cjs","sources":["../../../src/strategies/queueStrategy.ts"],"sourcesContent":["import { AsyncQueuer } from \"@tanstack/pacer/async-queuer\"\nimport type { QueueStrategy, QueueStrategyOptions } from \"./types\"\nimport type { Transaction } from \"../transactions\"\n\n/**\n * Creates a queue strategy that processes all mutations in order with proper serialization.\n *\n * Unlike other strategies that may drop executions, queue ensures every\n * mutation is processed sequentially. Each transaction commit completes before\n * the next one starts. Useful when data consistency is critical and\n * every operation must complete in order.\n *\n * @param options - Configuration for queue behavior (FIFO/LIFO, timing, size limits)\n * @returns A queue strategy instance\n *\n * @example\n * ```ts\n * // FIFO queue - process in order received\n * const mutate = usePacedMutations({\n * mutationFn: async ({ transaction }) => {\n * await api.save(transaction.mutations)\n * },\n * strategy: queueStrategy({\n * wait: 200,\n * addItemsTo: 'back',\n * getItemsFrom: 'front'\n * })\n * })\n * ```\n *\n * @example\n * ```ts\n * // LIFO queue - process most recent first\n * const mutate = usePacedMutations({\n * mutationFn: async ({ transaction }) => {\n * await api.save(transaction.mutations)\n * },\n * strategy: queueStrategy({\n * wait: 200,\n * addItemsTo: 'back',\n * getItemsFrom: 'back'\n * })\n * })\n * ```\n */\nexport function queueStrategy(options?: QueueStrategyOptions): QueueStrategy {\n const queuer = new AsyncQueuer<void>({\n concurrency: 1, // Process one at a time to ensure serialization\n wait: options?.wait,\n maxSize: options?.maxSize,\n addItemsTo: options?.addItemsTo ?? `back`, // Default FIFO: add to back\n getItemsFrom: options?.getItemsFrom ?? `front`, // Default FIFO: get from front\n started: true, // Start processing immediately\n })\n\n return {\n _type: `queue`,\n options,\n execute: <T extends object = Record<string, unknown>>(\n fn: () => Transaction<T>\n ) => {\n // Wrap the callback in an async function that waits for persistence\n queuer.addItem(async () => {\n const transaction = fn()\n // Wait for the transaction to be persisted before processing next item\n // Note: fn() already calls commit(), we just wait for it to complete\n await transaction.isPersisted.promise\n })\n },\n cleanup: () => {\n queuer.stop()\n queuer.clear()\n },\n }\n}\n"],"names":["AsyncQueuer"],"mappings":";;;AA6CO,SAAS,cAAc,SAA+C;AAC3E,QAAM,SAAS,IAAIA,wBAAkB;AAAA,IACnC,aAAa;AAAA;AAAA,IACb,MAAM,SAAS;AAAA,IACf,SAAS,SAAS;AAAA,IAClB,YAAY,SAAS,cAAc;AAAA;AAAA,IACnC,cAAc,SAAS,gBAAgB;AAAA;AAAA,IACvC,SAAS;AAAA;AAAA,EAAA,CACV;AAED,SAAO;AAAA,IACL,OAAO;AAAA,IACP;AAAA,IACA,SAAS,CACP,OACG;AAEH,aAAO,QAAQ,YAAY;AACzB,cAAM,cAAc,GAAA;AAGpB,cAAM,YAAY,YAAY;AAAA,MAChC,CAAC;AAAA,IACH;AAAA,IACA,SAAS,MAAM;AACb,aAAO,KAAA;AACP,aAAO,MAAA;AAAA,IACT;AAAA,EAAA;AAEJ;;"}
@@ -0,0 +1,43 @@
1
+ import { QueueStrategy, QueueStrategyOptions } from './types.cjs';
2
+ /**
3
+ * Creates a queue strategy that processes all mutations in order with proper serialization.
4
+ *
5
+ * Unlike other strategies that may drop executions, queue ensures every
6
+ * mutation is processed sequentially. Each transaction commit completes before
7
+ * the next one starts. Useful when data consistency is critical and
8
+ * every operation must complete in order.
9
+ *
10
+ * @param options - Configuration for queue behavior (FIFO/LIFO, timing, size limits)
11
+ * @returns A queue strategy instance
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * // FIFO queue - process in order received
16
+ * const mutate = usePacedMutations({
17
+ * mutationFn: async ({ transaction }) => {
18
+ * await api.save(transaction.mutations)
19
+ * },
20
+ * strategy: queueStrategy({
21
+ * wait: 200,
22
+ * addItemsTo: 'back',
23
+ * getItemsFrom: 'front'
24
+ * })
25
+ * })
26
+ * ```
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * // LIFO queue - process most recent first
31
+ * const mutate = usePacedMutations({
32
+ * mutationFn: async ({ transaction }) => {
33
+ * await api.save(transaction.mutations)
34
+ * },
35
+ * strategy: queueStrategy({
36
+ * wait: 200,
37
+ * addItemsTo: 'back',
38
+ * getItemsFrom: 'back'
39
+ * })
40
+ * })
41
+ * ```
42
+ */
43
+ export declare function queueStrategy(options?: QueueStrategyOptions): QueueStrategy;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const throttler = require("@tanstack/pacer/throttler");
4
+ function throttleStrategy(options) {
5
+ const throttler$1 = new throttler.Throttler(
6
+ (callback) => callback(),
7
+ options
8
+ );
9
+ return {
10
+ _type: `throttle`,
11
+ options,
12
+ execute: (fn) => {
13
+ throttler$1.maybeExecute(fn);
14
+ },
15
+ cleanup: () => {
16
+ throttler$1.cancel();
17
+ }
18
+ };
19
+ }
20
+ exports.throttleStrategy = throttleStrategy;
21
+ //# sourceMappingURL=throttleStrategy.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"throttleStrategy.cjs","sources":["../../../src/strategies/throttleStrategy.ts"],"sourcesContent":["import { Throttler } from \"@tanstack/pacer/throttler\"\nimport type { ThrottleStrategy, ThrottleStrategyOptions } from \"./types\"\nimport type { Transaction } from \"../transactions\"\n\n/**\n * Creates a throttle strategy that ensures transactions are evenly spaced\n * over time.\n *\n * Provides smooth, controlled execution patterns ideal for UI updates like\n * sliders, progress bars, or scroll handlers where you want consistent\n * execution timing.\n *\n * @param options - Configuration for throttle behavior\n * @returns A throttle strategy instance\n *\n * @example\n * ```ts\n * // Throttle slider updates to every 200ms\n * const mutate = useSerializedTransaction({\n * mutationFn: async ({ transaction }) => {\n * await api.updateVolume(transaction.mutations)\n * },\n * strategy: throttleStrategy({ wait: 200 })\n * })\n * ```\n *\n * @example\n * ```ts\n * // Throttle with leading and trailing execution\n * const mutate = useSerializedTransaction({\n * mutationFn: async ({ transaction }) => {\n * await api.save(transaction.mutations)\n * },\n * strategy: throttleStrategy({\n * wait: 500,\n * leading: true,\n * trailing: true\n * })\n * })\n * ```\n */\nexport function throttleStrategy(\n options: ThrottleStrategyOptions\n): ThrottleStrategy {\n const throttler = new Throttler(\n (callback: () => Transaction) => callback(),\n options\n )\n\n return {\n _type: `throttle`,\n options,\n execute: <T extends object = Record<string, unknown>>(\n fn: () => Transaction<T>\n ) => {\n throttler.maybeExecute(fn as () => Transaction)\n },\n cleanup: () => {\n throttler.cancel()\n },\n }\n}\n"],"names":["throttler","Throttler"],"mappings":";;;AAyCO,SAAS,iBACd,SACkB;AAClB,QAAMA,cAAY,IAAIC,UAAAA;AAAAA,IACpB,CAAC,aAAgC,SAAA;AAAA,IACjC;AAAA,EAAA;AAGF,SAAO;AAAA,IACL,OAAO;AAAA,IACP;AAAA,IACA,SAAS,CACP,OACG;AACHD,kBAAU,aAAa,EAAuB;AAAA,IAChD;AAAA,IACA,SAAS,MAAM;AACbA,kBAAU,OAAA;AAAA,IACZ;AAAA,EAAA;AAEJ;;"}
@@ -0,0 +1,39 @@
1
+ import { ThrottleStrategy, ThrottleStrategyOptions } from './types.cjs';
2
+ /**
3
+ * Creates a throttle strategy that ensures transactions are evenly spaced
4
+ * over time.
5
+ *
6
+ * Provides smooth, controlled execution patterns ideal for UI updates like
7
+ * sliders, progress bars, or scroll handlers where you want consistent
8
+ * execution timing.
9
+ *
10
+ * @param options - Configuration for throttle behavior
11
+ * @returns A throttle strategy instance
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * // Throttle slider updates to every 200ms
16
+ * const mutate = useSerializedTransaction({
17
+ * mutationFn: async ({ transaction }) => {
18
+ * await api.updateVolume(transaction.mutations)
19
+ * },
20
+ * strategy: throttleStrategy({ wait: 200 })
21
+ * })
22
+ * ```
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * // Throttle with leading and trailing execution
27
+ * const mutate = useSerializedTransaction({
28
+ * mutationFn: async ({ transaction }) => {
29
+ * await api.save(transaction.mutations)
30
+ * },
31
+ * strategy: throttleStrategy({
32
+ * wait: 500,
33
+ * leading: true,
34
+ * trailing: true
35
+ * })
36
+ * })
37
+ * ```
38
+ */
39
+ export declare function throttleStrategy(options: ThrottleStrategyOptions): ThrottleStrategy;
@@ -0,0 +1,103 @@
1
+ import { Transaction } from '../transactions.cjs';
2
+ /**
3
+ * Base strategy interface that all strategy implementations must conform to
4
+ */
5
+ export interface BaseStrategy<TName extends string = string> {
6
+ /** Type discriminator for strategy identification */
7
+ _type: TName;
8
+ /**
9
+ * Execute a function according to the strategy's timing rules
10
+ * @param fn - The function to execute
11
+ * @returns The result of the function execution (if applicable)
12
+ */
13
+ execute: <T extends object = Record<string, unknown>>(fn: () => Transaction<T>) => void | Promise<void>;
14
+ /**
15
+ * Clean up any resources held by the strategy
16
+ * Should be called when the strategy is no longer needed
17
+ */
18
+ cleanup: () => void;
19
+ }
20
+ /**
21
+ * Options for debounce strategy
22
+ * Delays execution until after a period of inactivity
23
+ */
24
+ export interface DebounceStrategyOptions {
25
+ /** Wait time in milliseconds before execution */
26
+ wait: number;
27
+ /** Execute immediately on the first call */
28
+ leading?: boolean;
29
+ /** Execute after the wait period on the last call */
30
+ trailing?: boolean;
31
+ }
32
+ /**
33
+ * Debounce strategy that delays execution until activity stops
34
+ */
35
+ export interface DebounceStrategy extends BaseStrategy<`debounce`> {
36
+ options: DebounceStrategyOptions;
37
+ }
38
+ /**
39
+ * Options for queue strategy
40
+ * Processes all executions in order (FIFO/LIFO)
41
+ */
42
+ export interface QueueStrategyOptions {
43
+ /** Wait time between processing queue items (milliseconds) */
44
+ wait?: number;
45
+ /** Maximum queue size (items are dropped if exceeded) */
46
+ maxSize?: number;
47
+ /** Where to add new items in the queue */
48
+ addItemsTo?: `front` | `back`;
49
+ /** Where to get items from when processing */
50
+ getItemsFrom?: `front` | `back`;
51
+ }
52
+ /**
53
+ * Queue strategy that processes all executions in order
54
+ * FIFO: { addItemsTo: 'back', getItemsFrom: 'front' }
55
+ * LIFO: { addItemsTo: 'back', getItemsFrom: 'back' }
56
+ */
57
+ export interface QueueStrategy extends BaseStrategy<`queue`> {
58
+ options?: QueueStrategyOptions;
59
+ }
60
+ /**
61
+ * Options for throttle strategy
62
+ * Ensures executions are evenly spaced over time
63
+ */
64
+ export interface ThrottleStrategyOptions {
65
+ /** Minimum wait time between executions (milliseconds) */
66
+ wait: number;
67
+ /** Execute immediately on the first call */
68
+ leading?: boolean;
69
+ /** Execute on the last call after wait period */
70
+ trailing?: boolean;
71
+ }
72
+ /**
73
+ * Throttle strategy that spaces executions evenly over time
74
+ */
75
+ export interface ThrottleStrategy extends BaseStrategy<`throttle`> {
76
+ options: ThrottleStrategyOptions;
77
+ }
78
+ /**
79
+ * Options for batch strategy
80
+ * Groups multiple executions together
81
+ */
82
+ export interface BatchStrategyOptions {
83
+ /** Maximum items per batch */
84
+ maxSize?: number;
85
+ /** Maximum wait time before processing batch (milliseconds) */
86
+ wait?: number;
87
+ /** Custom logic to determine when to execute batch */
88
+ getShouldExecute?: (items: Array<any>) => boolean;
89
+ }
90
+ /**
91
+ * Batch strategy that groups multiple executions together
92
+ */
93
+ export interface BatchStrategy extends BaseStrategy<`batch`> {
94
+ options?: BatchStrategyOptions;
95
+ }
96
+ /**
97
+ * Union type of all available strategies
98
+ */
99
+ export type Strategy = DebounceStrategy | QueueStrategy | ThrottleStrategy | BatchStrategy;
100
+ /**
101
+ * Extract the options type from a strategy
102
+ */
103
+ export type StrategyOptions<T extends Strategy> = T extends DebounceStrategy ? DebounceStrategyOptions : T extends QueueStrategy ? QueueStrategyOptions : T extends ThrottleStrategy ? ThrottleStrategyOptions : T extends BatchStrategy ? BatchStrategyOptions : never;
@@ -9,6 +9,8 @@ export * from './optimistic-action.js';
9
9
  export * from './local-only.js';
10
10
  export * from './local-storage.js';
11
11
  export * from './errors.js';
12
+ export * from './paced-mutations.js';
13
+ export * from './strategies/index.js';
12
14
  export * from './indexes/base-index.js';
13
15
  export * from './indexes/btree-index.js';
14
16
  export * from './indexes/lazy-index.js';