@tanstack/db 0.4.8 → 0.4.10

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 (134) hide show
  1. package/dist/cjs/collection/events.cjs +9 -51
  2. package/dist/cjs/collection/events.cjs.map +1 -1
  3. package/dist/cjs/collection/events.d.cts +18 -7
  4. package/dist/cjs/collection/index.cjs +9 -12
  5. package/dist/cjs/collection/index.cjs.map +1 -1
  6. package/dist/cjs/collection/index.d.cts +13 -14
  7. package/dist/cjs/collection/subscription.cjs +62 -6
  8. package/dist/cjs/collection/subscription.cjs.map +1 -1
  9. package/dist/cjs/collection/subscription.d.cts +16 -3
  10. package/dist/cjs/collection/sync.cjs +58 -6
  11. package/dist/cjs/collection/sync.cjs.map +1 -1
  12. package/dist/cjs/collection/sync.d.cts +18 -4
  13. package/dist/cjs/errors.cjs +59 -17
  14. package/dist/cjs/errors.cjs.map +1 -1
  15. package/dist/cjs/errors.d.cts +44 -8
  16. package/dist/cjs/event-emitter.cjs +94 -0
  17. package/dist/cjs/event-emitter.cjs.map +1 -0
  18. package/dist/cjs/event-emitter.d.cts +45 -0
  19. package/dist/cjs/index.cjs +9 -4
  20. package/dist/cjs/index.cjs.map +1 -1
  21. package/dist/cjs/local-only.cjs.map +1 -1
  22. package/dist/cjs/local-only.d.cts +2 -5
  23. package/dist/cjs/query/builder/types.d.cts +1 -1
  24. package/dist/cjs/query/compiler/index.cjs +46 -19
  25. package/dist/cjs/query/compiler/index.cjs.map +1 -1
  26. package/dist/cjs/query/compiler/index.d.cts +35 -9
  27. package/dist/cjs/query/compiler/joins.cjs +91 -66
  28. package/dist/cjs/query/compiler/joins.cjs.map +1 -1
  29. package/dist/cjs/query/compiler/joins.d.cts +6 -3
  30. package/dist/cjs/query/compiler/order-by.cjs +20 -4
  31. package/dist/cjs/query/compiler/order-by.cjs.map +1 -1
  32. package/dist/cjs/query/compiler/order-by.d.cts +3 -1
  33. package/dist/cjs/query/compiler/select.cjs.map +1 -1
  34. package/dist/cjs/query/compiler/types.d.cts +4 -0
  35. package/dist/cjs/query/index.d.cts +1 -0
  36. package/dist/cjs/query/live/collection-config-builder.cjs +306 -46
  37. package/dist/cjs/query/live/collection-config-builder.cjs.map +1 -1
  38. package/dist/cjs/query/live/collection-config-builder.d.cts +97 -9
  39. package/dist/cjs/query/live/collection-registry.cjs +16 -0
  40. package/dist/cjs/query/live/collection-registry.cjs.map +1 -0
  41. package/dist/cjs/query/live/collection-registry.d.cts +26 -0
  42. package/dist/cjs/query/live/collection-subscriber.cjs +86 -58
  43. package/dist/cjs/query/live/collection-subscriber.cjs.map +1 -1
  44. package/dist/cjs/query/live/collection-subscriber.d.cts +5 -7
  45. package/dist/cjs/query/live-query-collection.cjs +11 -5
  46. package/dist/cjs/query/live-query-collection.cjs.map +1 -1
  47. package/dist/cjs/query/live-query-collection.d.cts +12 -5
  48. package/dist/cjs/query/optimizer.cjs +44 -7
  49. package/dist/cjs/query/optimizer.cjs.map +1 -1
  50. package/dist/cjs/query/optimizer.d.cts +4 -4
  51. package/dist/cjs/scheduler.cjs +137 -0
  52. package/dist/cjs/scheduler.cjs.map +1 -0
  53. package/dist/cjs/scheduler.d.cts +56 -0
  54. package/dist/cjs/transactions.cjs +7 -1
  55. package/dist/cjs/transactions.cjs.map +1 -1
  56. package/dist/cjs/types.d.cts +82 -11
  57. package/dist/esm/collection/events.d.ts +18 -7
  58. package/dist/esm/collection/events.js +9 -51
  59. package/dist/esm/collection/events.js.map +1 -1
  60. package/dist/esm/collection/index.d.ts +13 -14
  61. package/dist/esm/collection/index.js +9 -12
  62. package/dist/esm/collection/index.js.map +1 -1
  63. package/dist/esm/collection/subscription.d.ts +16 -3
  64. package/dist/esm/collection/subscription.js +62 -6
  65. package/dist/esm/collection/subscription.js.map +1 -1
  66. package/dist/esm/collection/sync.d.ts +18 -4
  67. package/dist/esm/collection/sync.js +59 -7
  68. package/dist/esm/collection/sync.js.map +1 -1
  69. package/dist/esm/errors.d.ts +44 -8
  70. package/dist/esm/errors.js +60 -18
  71. package/dist/esm/errors.js.map +1 -1
  72. package/dist/esm/event-emitter.d.ts +45 -0
  73. package/dist/esm/event-emitter.js +94 -0
  74. package/dist/esm/event-emitter.js.map +1 -0
  75. package/dist/esm/index.js +10 -5
  76. package/dist/esm/local-only.d.ts +2 -5
  77. package/dist/esm/local-only.js.map +1 -1
  78. package/dist/esm/query/builder/types.d.ts +1 -1
  79. package/dist/esm/query/compiler/index.d.ts +35 -9
  80. package/dist/esm/query/compiler/index.js +46 -19
  81. package/dist/esm/query/compiler/index.js.map +1 -1
  82. package/dist/esm/query/compiler/joins.d.ts +6 -3
  83. package/dist/esm/query/compiler/joins.js +93 -68
  84. package/dist/esm/query/compiler/joins.js.map +1 -1
  85. package/dist/esm/query/compiler/order-by.d.ts +3 -1
  86. package/dist/esm/query/compiler/order-by.js +20 -4
  87. package/dist/esm/query/compiler/order-by.js.map +1 -1
  88. package/dist/esm/query/compiler/select.js.map +1 -1
  89. package/dist/esm/query/compiler/types.d.ts +4 -0
  90. package/dist/esm/query/index.d.ts +1 -0
  91. package/dist/esm/query/live/collection-config-builder.d.ts +97 -9
  92. package/dist/esm/query/live/collection-config-builder.js +306 -46
  93. package/dist/esm/query/live/collection-config-builder.js.map +1 -1
  94. package/dist/esm/query/live/collection-registry.d.ts +26 -0
  95. package/dist/esm/query/live/collection-registry.js +16 -0
  96. package/dist/esm/query/live/collection-registry.js.map +1 -0
  97. package/dist/esm/query/live/collection-subscriber.d.ts +5 -7
  98. package/dist/esm/query/live/collection-subscriber.js +86 -58
  99. package/dist/esm/query/live/collection-subscriber.js.map +1 -1
  100. package/dist/esm/query/live-query-collection.d.ts +12 -5
  101. package/dist/esm/query/live-query-collection.js +11 -5
  102. package/dist/esm/query/live-query-collection.js.map +1 -1
  103. package/dist/esm/query/optimizer.d.ts +4 -4
  104. package/dist/esm/query/optimizer.js +44 -7
  105. package/dist/esm/query/optimizer.js.map +1 -1
  106. package/dist/esm/scheduler.d.ts +56 -0
  107. package/dist/esm/scheduler.js +137 -0
  108. package/dist/esm/scheduler.js.map +1 -0
  109. package/dist/esm/transactions.js +7 -1
  110. package/dist/esm/transactions.js.map +1 -1
  111. package/dist/esm/types.d.ts +82 -11
  112. package/package.json +2 -2
  113. package/src/collection/events.ts +25 -74
  114. package/src/collection/index.ts +15 -19
  115. package/src/collection/subscription.ts +88 -6
  116. package/src/collection/sync.ts +81 -9
  117. package/src/errors.ts +91 -13
  118. package/src/event-emitter.ts +118 -0
  119. package/src/local-only.ts +5 -12
  120. package/src/query/builder/types.ts +1 -1
  121. package/src/query/compiler/index.ts +124 -33
  122. package/src/query/compiler/joins.ts +187 -128
  123. package/src/query/compiler/order-by.ts +30 -2
  124. package/src/query/compiler/select.ts +2 -3
  125. package/src/query/compiler/types.ts +5 -0
  126. package/src/query/index.ts +1 -0
  127. package/src/query/live/collection-config-builder.ts +501 -60
  128. package/src/query/live/collection-registry.ts +47 -0
  129. package/src/query/live/collection-subscriber.ts +137 -105
  130. package/src/query/live-query-collection.ts +47 -18
  131. package/src/query/optimizer.ts +85 -15
  132. package/src/scheduler.ts +198 -0
  133. package/src/transactions.ts +12 -1
  134. package/src/types.ts +93 -11
@@ -1 +1 @@
1
- {"version":3,"file":"order-by.cjs","sources":["../../../../src/query/compiler/order-by.ts"],"sourcesContent":["import { orderByWithFractionalIndex } from \"@tanstack/db-ivm\"\nimport { defaultComparator, makeComparator } from \"../../utils/comparison.js\"\nimport { PropRef, followRef } from \"../ir.js\"\nimport { ensureIndexForField } from \"../../indexes/auto-index.js\"\nimport { findIndexForField } from \"../../utils/index-optimization.js\"\nimport { compileExpression } from \"./evaluators.js\"\nimport { replaceAggregatesByRefs } from \"./group-by.js\"\nimport type { CompiledSingleRowExpression } from \"./evaluators.js\"\nimport type { OrderBy, OrderByClause, QueryIR, Select } from \"../ir.js\"\nimport type { NamespacedAndKeyedStream, NamespacedRow } from \"../../types.js\"\nimport type { IStreamBuilder, KeyValue } from \"@tanstack/db-ivm\"\nimport type { IndexInterface } from \"../../indexes/base-index.js\"\nimport type { Collection } from \"../../collection/index.js\"\n\nexport type OrderByOptimizationInfo = {\n orderBy: OrderBy\n offset: number\n limit: number\n comparator: (\n a: Record<string, unknown> | null | undefined,\n b: Record<string, unknown> | null | undefined\n ) => number\n valueExtractorForRawRow: (row: Record<string, unknown>) => any\n index: IndexInterface<string | number>\n dataNeeded?: () => number\n}\n\n/**\n * Processes the ORDER BY clause\n * Works with the new structure that has both namespaced row data and __select_results\n * Always uses fractional indexing and adds the index as __ordering_index to the result\n */\nexport function processOrderBy(\n rawQuery: QueryIR,\n pipeline: NamespacedAndKeyedStream,\n orderByClause: Array<OrderByClause>,\n selectClause: Select,\n collection: Collection,\n optimizableOrderByCollections: Record<string, OrderByOptimizationInfo>,\n limit?: number,\n offset?: number\n): IStreamBuilder<KeyValue<unknown, [NamespacedRow, string]>> {\n // Pre-compile all order by expressions\n const compiledOrderBy = orderByClause.map((clause) => {\n const clauseWithoutAggregates = replaceAggregatesByRefs(\n clause.expression,\n selectClause,\n `__select_results`\n )\n return {\n compiledExpression: compileExpression(clauseWithoutAggregates),\n compareOptions: clause.compareOptions,\n }\n })\n\n // Create a value extractor function for the orderBy operator\n const valueExtractor = (row: NamespacedRow & { __select_results?: any }) => {\n // The namespaced row contains:\n // 1. Table aliases as top-level properties (e.g., row[\"tableName\"])\n // 2. SELECT results in __select_results (e.g., row.__select_results[\"aggregateAlias\"])\n // The replaceAggregatesByRefs function has already transformed any aggregate expressions\n // that match SELECT aggregates to use the __select_results namespace.\n const orderByContext = row\n\n if (orderByClause.length > 1) {\n // For multiple orderBy columns, create a composite key\n return compiledOrderBy.map((compiled) =>\n compiled.compiledExpression(orderByContext)\n )\n } else if (orderByClause.length === 1) {\n // For a single orderBy column, use the value directly\n const compiled = compiledOrderBy[0]!\n return compiled.compiledExpression(orderByContext)\n }\n\n // Default case - no ordering\n return null\n }\n\n // Create a multi-property comparator that respects the order and direction of each property\n const compare = (a: unknown, b: unknown) => {\n // If we're comparing arrays (multiple properties), compare each property in order\n if (orderByClause.length > 1) {\n const arrayA = a as Array<unknown>\n const arrayB = b as Array<unknown>\n for (let i = 0; i < orderByClause.length; i++) {\n const clause = orderByClause[i]!\n const compareFn = makeComparator(clause.compareOptions)\n const result = compareFn(arrayA[i], arrayB[i])\n if (result !== 0) {\n return result\n }\n }\n return arrayA.length - arrayB.length\n }\n\n // Single property comparison\n if (orderByClause.length === 1) {\n const clause = orderByClause[0]!\n const compareFn = makeComparator(clause.compareOptions)\n return compareFn(a, b)\n }\n\n return defaultComparator(a, b)\n }\n\n let setSizeCallback: ((getSize: () => number) => void) | undefined\n\n // Optimize the orderBy operator to lazily load elements\n // by using the range index of the collection.\n // Only for orderBy clause on a single column for now (no composite ordering)\n if (limit && orderByClause.length === 1) {\n const clause = orderByClause[0]!\n const orderByExpression = clause.expression\n\n if (orderByExpression.type === `ref`) {\n const followRefResult = followRef(\n rawQuery,\n orderByExpression,\n collection\n )!\n\n const followRefCollection = followRefResult.collection\n const fieldName = followRefResult.path[0]\n if (fieldName) {\n ensureIndexForField(\n fieldName,\n followRefResult.path,\n followRefCollection,\n clause.compareOptions,\n compare\n )\n }\n\n const valueExtractorForRawRow = compileExpression(\n new PropRef(followRefResult.path),\n true\n ) as CompiledSingleRowExpression\n\n const comparator = (\n a: Record<string, unknown> | null | undefined,\n b: Record<string, unknown> | null | undefined\n ) => {\n const extractedA = a ? valueExtractorForRawRow(a) : a\n const extractedB = b ? valueExtractorForRawRow(b) : b\n return compare(extractedA, extractedB)\n }\n\n const index: IndexInterface<string | number> | undefined =\n findIndexForField(\n followRefCollection.indexes,\n followRefResult.path,\n clause.compareOptions\n )\n\n if (index && index.supports(`gt`)) {\n // We found an index that we can use to lazily load ordered data\n const orderByOptimizationInfo = {\n offset: offset ?? 0,\n limit,\n comparator,\n valueExtractorForRawRow,\n index,\n orderBy: orderByClause,\n }\n\n optimizableOrderByCollections[followRefCollection.id] =\n orderByOptimizationInfo\n\n setSizeCallback = (getSize: () => number) => {\n optimizableOrderByCollections[followRefCollection.id] = {\n ...optimizableOrderByCollections[followRefCollection.id]!,\n dataNeeded: () => {\n const size = getSize()\n return Math.max(0, limit - size)\n },\n }\n }\n }\n }\n }\n\n // Use fractional indexing and return the tuple [value, index]\n return pipeline.pipe(\n orderByWithFractionalIndex(valueExtractor, {\n limit,\n offset,\n comparator: compare,\n setSizeCallback,\n })\n // orderByWithFractionalIndex returns [key, [value, index]] - we keep this format\n )\n}\n"],"names":["replaceAggregatesByRefs","compileExpression","makeComparator","defaultComparator","followRef","ensureIndexForField","PropRef","findIndexForField","orderByWithFractionalIndex"],"mappings":";;;;;;;;;AAgCO,SAAS,eACd,UACA,UACA,eACA,cACA,YACA,+BACA,OACA,QAC4D;AAE5D,QAAM,kBAAkB,cAAc,IAAI,CAAC,WAAW;AACpD,UAAM,0BAA0BA,QAAAA;AAAAA,MAC9B,OAAO;AAAA,MACP;AAAA,MACA;AAAA,IAAA;AAEF,WAAO;AAAA,MACL,oBAAoBC,WAAAA,kBAAkB,uBAAuB;AAAA,MAC7D,gBAAgB,OAAO;AAAA,IAAA;AAAA,EAE3B,CAAC;AAGD,QAAM,iBAAiB,CAAC,QAAoD;AAM1E,UAAM,iBAAiB;AAEvB,QAAI,cAAc,SAAS,GAAG;AAE5B,aAAO,gBAAgB;AAAA,QAAI,CAAC,aAC1B,SAAS,mBAAmB,cAAc;AAAA,MAAA;AAAA,IAE9C,WAAW,cAAc,WAAW,GAAG;AAErC,YAAM,WAAW,gBAAgB,CAAC;AAClC,aAAO,SAAS,mBAAmB,cAAc;AAAA,IACnD;AAGA,WAAO;AAAA,EACT;AAGA,QAAM,UAAU,CAAC,GAAY,MAAe;AAE1C,QAAI,cAAc,SAAS,GAAG;AAC5B,YAAM,SAAS;AACf,YAAM,SAAS;AACf,eAAS,IAAI,GAAG,IAAI,cAAc,QAAQ,KAAK;AAC7C,cAAM,SAAS,cAAc,CAAC;AAC9B,cAAM,Y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0BAA0B;AAAA,UAC9B,QAAQ,UAAU;AAAA,UAClB;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA,SAAS;AAAA,QAAA;AAGX,sCAA8B,oBAAoB,EAAE,IAClD;AAEF,0BAAkB,CAAC,YAA0B;AAC3C,wCAA8B,oBAAoB,EAAE,IAAI;AAAA,YACtD,GAAG,8BAA8B,oBAAoB,EAAE;AAAA,YACvD,YAAY,MAAM;AAChB,oBAAM,OAAO,QAAA;AACb,qBAAO,KAAK,IAAI,GAAG,QAAQ,IAAI;AAAA,YACjC;AAAA,UAAA;AAAA,QAEJ;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAGA,SAAO,SAAS;AAAA,IACdC,MAAAA,2BAA2B,gBAAgB;AAAA,MACzC;AAAA,MACA;AAAA,MACA,YAAY;AAAA,MACZ;AAAA,IAAA,CACD;AAAA;AAAA,EAAA;AAGL;;"}
1
+ {"version":3,"file":"order-by.cjs","sources":["../../../../src/query/compiler/order-by.ts"],"sourcesContent":["import { orderByWithFractionalIndex } from \"@tanstack/db-ivm\"\nimport { defaultComparator, makeComparator } from \"../../utils/comparison.js\"\nimport { PropRef, followRef } from \"../ir.js\"\nimport { ensureIndexForField } from \"../../indexes/auto-index.js\"\nimport { findIndexForField } from \"../../utils/index-optimization.js\"\nimport { compileExpression } from \"./evaluators.js\"\nimport { replaceAggregatesByRefs } from \"./group-by.js\"\nimport type { WindowOptions } from \"./types.js\"\nimport type { CompiledSingleRowExpression } from \"./evaluators.js\"\nimport type { OrderBy, OrderByClause, QueryIR, Select } from \"../ir.js\"\nimport type { NamespacedAndKeyedStream, NamespacedRow } from \"../../types.js\"\nimport type { IStreamBuilder, KeyValue } from \"@tanstack/db-ivm\"\nimport type { IndexInterface } from \"../../indexes/base-index.js\"\nimport type { Collection } from \"../../collection/index.js\"\n\nexport type OrderByOptimizationInfo = {\n alias: string\n orderBy: OrderBy\n offset: number\n limit: number\n comparator: (\n a: Record<string, unknown> | null | undefined,\n b: Record<string, unknown> | null | undefined\n ) => number\n valueExtractorForRawRow: (row: Record<string, unknown>) => any\n index: IndexInterface<string | number>\n dataNeeded?: () => number\n}\n\n/**\n * Processes the ORDER BY clause\n * Works with the new structure that has both namespaced row data and __select_results\n * Always uses fractional indexing and adds the index as __ordering_index to the result\n */\nexport function processOrderBy(\n rawQuery: QueryIR,\n pipeline: NamespacedAndKeyedStream,\n orderByClause: Array<OrderByClause>,\n selectClause: Select,\n collection: Collection,\n optimizableOrderByCollections: Record<string, OrderByOptimizationInfo>,\n setWindowFn: (windowFn: (options: WindowOptions) => void) => void,\n limit?: number,\n offset?: number\n): IStreamBuilder<KeyValue<unknown, [NamespacedRow, string]>> {\n // Pre-compile all order by expressions\n const compiledOrderBy = orderByClause.map((clause) => {\n const clauseWithoutAggregates = replaceAggregatesByRefs(\n clause.expression,\n selectClause,\n `__select_results`\n )\n return {\n compiledExpression: compileExpression(clauseWithoutAggregates),\n compareOptions: clause.compareOptions,\n }\n })\n\n // Create a value extractor function for the orderBy operator\n const valueExtractor = (row: NamespacedRow & { __select_results?: any }) => {\n // The namespaced row contains:\n // 1. Table aliases as top-level properties (e.g., row[\"tableName\"])\n // 2. SELECT results in __select_results (e.g., row.__select_results[\"aggregateAlias\"])\n // The replaceAggregatesByRefs function has already transformed any aggregate expressions\n // that match SELECT aggregates to use the __select_results namespace.\n const orderByContext = row\n\n if (orderByClause.length > 1) {\n // For multiple orderBy columns, create a composite key\n return compiledOrderBy.map((compiled) =>\n compiled.compiledExpression(orderByContext)\n )\n } else if (orderByClause.length === 1) {\n // For a single orderBy column, use the value directly\n const compiled = compiledOrderBy[0]!\n return compiled.compiledExpression(orderByContext)\n }\n\n // Default case - no ordering\n return null\n }\n\n // Create a multi-property comparator that respects the order and direction of each property\n const compare = (a: unknown, b: unknown) => {\n // If we're comparing arrays (multiple properties), compare each property in order\n if (orderByClause.length > 1) {\n const arrayA = a as Array<unknown>\n const arrayB = b as Array<unknown>\n for (let i = 0; i < orderByClause.length; i++) {\n const clause = orderByClause[i]!\n const compareFn = makeComparator(clause.compareOptions)\n const result = compareFn(arrayA[i], arrayB[i])\n if (result !== 0) {\n return result\n }\n }\n return arrayA.length - arrayB.length\n }\n\n // Single property comparison\n if (orderByClause.length === 1) {\n const clause = orderByClause[0]!\n const compareFn = makeComparator(clause.compareOptions)\n return compareFn(a, b)\n }\n\n return defaultComparator(a, b)\n }\n\n let setSizeCallback: ((getSize: () => number) => void) | undefined\n\n let orderByOptimizationInfo: OrderByOptimizationInfo | undefined\n\n // Optimize the orderBy operator to lazily load elements\n // by using the range index of the collection.\n // Only for orderBy clause on a single column for now (no composite ordering)\n if (limit && orderByClause.length === 1) {\n const clause = orderByClause[0]!\n const orderByExpression = clause.expression\n\n if (orderByExpression.type === `ref`) {\n const followRefResult = followRef(\n rawQuery,\n orderByExpression,\n collection\n )!\n\n const followRefCollection = followRefResult.collection\n const fieldName = followRefResult.path[0]\n if (fieldName) {\n ensureIndexForField(\n fieldName,\n followRefResult.path,\n followRefCollection,\n clause.compareOptions,\n compare\n )\n }\n\n const valueExtractorForRawRow = compileExpression(\n new PropRef(followRefResult.path),\n true\n ) as CompiledSingleRowExpression\n\n const comparator = (\n a: Record<string, unknown> | null | undefined,\n b: Record<string, unknown> | null | undefined\n ) => {\n const extractedA = a ? valueExtractorForRawRow(a) : a\n const extractedB = b ? valueExtractorForRawRow(b) : b\n return compare(extractedA, extractedB)\n }\n\n const index: IndexInterface<string | number> | undefined =\n findIndexForField(\n followRefCollection.indexes,\n followRefResult.path,\n clause.compareOptions\n )\n\n if (index && index.supports(`gt`)) {\n // We found an index that we can use to lazily load ordered data\n const orderByAlias =\n orderByExpression.path.length > 1\n ? String(orderByExpression.path[0])\n : rawQuery.from.alias\n\n orderByOptimizationInfo = {\n alias: orderByAlias,\n offset: offset ?? 0,\n limit,\n comparator,\n valueExtractorForRawRow,\n index,\n orderBy: orderByClause,\n }\n\n optimizableOrderByCollections[followRefCollection.id] =\n orderByOptimizationInfo\n\n setSizeCallback = (getSize: () => number) => {\n optimizableOrderByCollections[followRefCollection.id] = {\n ...optimizableOrderByCollections[followRefCollection.id]!,\n dataNeeded: () => {\n const size = getSize()\n return Math.max(0, orderByOptimizationInfo!.limit - size)\n },\n }\n }\n }\n }\n }\n\n // Use fractional indexing and return the tuple [value, index]\n return pipeline.pipe(\n orderByWithFractionalIndex(valueExtractor, {\n limit,\n offset,\n comparator: compare,\n setSizeCallback,\n setWindowFn: (\n windowFn: (options: { offset?: number; limit?: number }) => void\n ) => {\n setWindowFn(\n // We wrap the move function such that we update the orderByOptimizationInfo\n // because that is used by the `dataNeeded` callback to determine if we need to load more data\n (options) => {\n windowFn(options)\n if (orderByOptimizationInfo) {\n orderByOptimizationInfo.offset =\n options.offset ?? orderByOptimizationInfo.offset\n orderByOptimizationInfo.limit =\n options.limit ?? orderByOptimizationInfo.limit\n }\n }\n )\n },\n })\n // orderByWithFractionalIndex returns [key, [value, index]] - we keep this format\n )\n}\n"],"names":["replaceAggregatesByRefs","compileExpression","makeComparator","defaultComparator","followRef","ensureIndexForField","PropRef","findIndexForField","orderByWithFractionalIndex"],"mappings":";;;;;;;;;AAkCO,SAAS,eACd,UACA,UACA,eACA,cACA,YACA,+BACA,aACA,OACA,QAC4D;AAE5D,QAAM,kBAAkB,cAAc,IAAI,CAAC,WAAW;AACpD,UAAM,0BAA0BA,QAAAA;AAAAA,MAC9B,OAAO;AAAA,MACP;AAAA,MACA;AAAA,IAAA;AAEF,WAAO;AAAA,MACL,oBAAoBC,WAAAA,kBAAkB,uBAAuB;AAAA,MAC7D,gBAAgB,OAAO;AAAA,IAAA;AAAA,EAE3B,CAAC;AAGD,QAAM,iBAAiB,CAAC,QAAoD;AAM1E,UAAM,iBAAiB;AAEvB,QAAI,cAAc,SAAS,GAAG;AAE5B,aAAO,gBAAgB;AAAA,QAAI,CAAC,aAC1B,SAAS,mBAAmB,cAAc;AAAA,MAAA;AAAA,IAE9C,WAAW,cAAc,WAAW,GAAG;AAErC,YAA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+1,11 @@
1
+ import { WindowOptions } from './types.js';
1
2
  import { OrderBy, OrderByClause, QueryIR, Select } from '../ir.js';
2
3
  import { NamespacedAndKeyedStream, NamespacedRow } from '../../types.js';
3
4
  import { IStreamBuilder, KeyValue } from '@tanstack/db-ivm';
4
5
  import { IndexInterface } from '../../indexes/base-index.js';
5
6
  import { Collection } from '../../collection/index.js';
6
7
  export type OrderByOptimizationInfo = {
8
+ alias: string;
7
9
  orderBy: OrderBy;
8
10
  offset: number;
9
11
  limit: number;
@@ -17,4 +19,4 @@ export type OrderByOptimizationInfo = {
17
19
  * Works with the new structure that has both namespaced row data and __select_results
18
20
  * Always uses fractional indexing and adds the index as __ordering_index to the result
19
21
  */
20
- export declare function processOrderBy(rawQuery: QueryIR, pipeline: NamespacedAndKeyedStream, orderByClause: Array<OrderByClause>, selectClause: Select, collection: Collection, optimizableOrderByCollections: Record<string, OrderByOptimizationInfo>, limit?: number, offset?: number): IStreamBuilder<KeyValue<unknown, [NamespacedRow, string]>>;
22
+ export declare function processOrderBy(rawQuery: QueryIR, pipeline: NamespacedAndKeyedStream, orderByClause: Array<OrderByClause>, selectClause: Select, collection: Collection, optimizableOrderByCollections: Record<string, OrderByOptimizationInfo>, setWindowFn: (windowFn: (options: WindowOptions) => void) => void, limit?: number, offset?: number): IStreamBuilder<KeyValue<unknown, [NamespacedRow, string]>>;
@@ -1 +1 @@
1
- {"version":3,"file":"select.cjs","sources":["../../../../src/query/compiler/select.ts"],"sourcesContent":["import { map } from \"@tanstack/db-ivm\"\nimport { PropRef, Value as ValClass, isExpressionLike } from \"../ir.js\"\nimport { compileExpression } from \"./evaluators.js\"\nimport type { Aggregate, BasicExpression, Select } from \"../ir.js\"\nimport type {\n KeyedStream,\n NamespacedAndKeyedStream,\n NamespacedRow,\n} from \"../../types.js\"\n\n/**\n * Type for operations array used in select processing\n */\ntype SelectOp =\n | {\n kind: `merge`\n targetPath: Array<string>\n source: (row: NamespacedRow) => any\n }\n | { kind: `field`; alias: string; compiled: (row: NamespacedRow) => any }\n\n/**\n * Unwraps any Value expressions\n */\nfunction unwrapVal(input: any): any {\n if (input instanceof ValClass) return input.value\n return input\n}\n\n/**\n * Processes a merge operation by merging source values into the target path\n */\nfunction processMerge(\n op: Extract<SelectOp, { kind: `merge` }>,\n namespacedRow: NamespacedRow,\n selectResults: Record<string, any>\n): void {\n const value = op.source(namespacedRow)\n if (value && typeof value === `object`) {\n // Ensure target object exists\n let cursor: any = selectResults\n const path = op.targetPath\n if (path.length === 0) {\n // Top-level merge\n for (const [k, v] of Object.entries(value)) {\n selectResults[k] = unwrapVal(v)\n }\n } else {\n for (let i = 0; i < path.length; i++) {\n const seg = path[i]!\n if (i === path.length - 1) {\n const dest = (cursor[seg] ??= {})\n if (typeof dest === `object`) {\n for (const [k, v] of Object.entries(value)) {\n dest[k] = unwrapVal(v)\n }\n }\n } else {\n const next = cursor[seg]\n if (next == null || typeof next !== `object`) {\n cursor[seg] = {}\n }\n cursor = cursor[seg]\n }\n }\n }\n }\n}\n\n/**\n * Processes a non-merge operation by setting the field value at the specified alias path\n */\nfunction processNonMergeOp(\n op: Extract<SelectOp, { kind: `field` }>,\n namespacedRow: NamespacedRow,\n selectResults: Record<string, any>\n): void {\n // Support nested alias paths like \"meta.author.name\"\n const path = op.alias.split(`.`)\n if (path.length === 1) {\n selectResults[op.alias] = op.compiled(namespacedRow)\n } else {\n let cursor: any = selectResults\n for (let i = 0; i < path.length - 1; i++) {\n const seg = path[i]!\n const next = cursor[seg]\n if (next == null || typeof next !== `object`) {\n cursor[seg] = {}\n }\n cursor = cursor[seg]\n }\n cursor[path[path.length - 1]!] = unwrapVal(op.compiled(namespacedRow))\n }\n}\n\n/**\n * Processes a single row to generate select results\n */\nfunction processRow(\n [key, namespacedRow]: [unknown, NamespacedRow],\n ops: Array<SelectOp>\n): [unknown, typeof namespacedRow & { __select_results: any }] {\n const selectResults: Record<string, any> = {}\n\n for (const op of ops) {\n if (op.kind === `merge`) {\n processMerge(op, namespacedRow, selectResults)\n } else {\n processNonMergeOp(op, namespacedRow, selectResults)\n }\n }\n\n // Return the namespaced row with __select_results added\n return [\n key,\n {\n ...namespacedRow,\n __select_results: selectResults,\n },\n ] as [\n unknown,\n typeof namespacedRow & { __select_results: typeof selectResults },\n ]\n}\n\n/**\n * Processes the SELECT clause and places results in __select_results\n * while preserving the original namespaced row for ORDER BY access\n */\nexport function processSelect(\n pipeline: NamespacedAndKeyedStream,\n select: Select,\n _allInputs: Record<string, KeyedStream>\n): NamespacedAndKeyedStream {\n // Build ordered operations to preserve authoring order (spreads and fields)\n const ops: Array<SelectOp> = []\n\n addFromObject([], select, ops)\n\n return pipeline.pipe(map((row) => processRow(row, ops)))\n}\n\n/**\n * Helper function to check if an expression is an aggregate\n */\nfunction isAggregateExpression(\n expr: BasicExpression | Aggregate\n): expr is Aggregate {\n return expr.type === `agg`\n}\n\n/**\n * Processes a single argument in a function context\n */\nexport function processArgument(\n arg: BasicExpression | Aggregate,\n namespacedRow: NamespacedRow\n): any {\n if (isAggregateExpression(arg)) {\n throw new Error(\n `Aggregate expressions are not supported in this context. Use GROUP BY clause for aggregates.`\n )\n }\n\n // Pre-compile the expression and evaluate immediately\n const compiledExpression = compileExpression(arg)\n const value = compiledExpression(namespacedRow)\n\n return value\n}\n\n/**\n * Helper function to check if an object is a nested select object\n *\n * .select({\n * id: users.id,\n * profile: { // <-- this is a nested select object\n * name: users.name,\n * email: users.email\n * }\n * })\n */\nfunction isNestedSelectObject(obj: any): boolean {\n return obj && typeof obj === `object` && !isExpressionLike(obj)\n}\n\n/**\n * Helper function to process select objects and build operations array\n */\nfunction addFromObject(\n prefixPath: Array<string>,\n obj: any,\n ops: Array<SelectOp>\n) {\n for (const [key, value] of Object.entries(obj)) {\n if (key.startsWith(`__SPREAD_SENTINEL__`)) {\n const rest = key.slice(`__SPREAD_SENTINEL__`.length)\n const splitIndex = rest.lastIndexOf(`__`)\n const pathStr = splitIndex >= 0 ? rest.slice(0, splitIndex) : rest\n const isRefExpr =\n value &&\n typeof value === `object` &&\n `type` in (value as any) &&\n (value as any).type === `ref`\n if (pathStr.includes(`.`) || isRefExpr) {\n // Merge into the current destination (prefixPath) from the referenced source path\n const targetPath = [...prefixPath]\n const expr = isRefExpr\n ? (value as BasicExpression)\n : (new PropRef(pathStr.split(`.`)) as BasicExpression)\n const compiled = compileExpression(expr)\n ops.push({ kind: `merge`, targetPath, source: compiled })\n } else {\n // Table-level: pathStr is the alias; merge from namespaced row at the current prefix\n const tableAlias = pathStr\n const targetPath = [...prefixPath]\n ops.push({\n kind: `merge`,\n targetPath,\n source: (row) => (row as any)[tableAlias],\n })\n }\n continue\n }\n\n const expression = value as any\n if (isNestedSelectObject(expression)) {\n // Nested selection object\n addFromObject([...prefixPath, key], expression, ops)\n continue\n }\n\n if (isAggregateExpression(expression)) {\n // Placeholder for group-by processing later\n ops.push({\n kind: `field`,\n alias: [...prefixPath, key].join(`.`),\n compiled: () => null,\n })\n } else {\n if (expression === undefined || !isExpressionLike(expression)) {\n ops.push({\n kind: `field`,\n alias: [...prefixPath, key].join(`.`),\n compiled: () => expression,\n })\n continue\n }\n // If the expression is a Value wrapper, embed the literal to avoid re-compilation mishaps\n if (expression instanceof ValClass) {\n const val = expression.value\n ops.push({\n kind: `field`,\n alias: [...prefixPath, key].join(`.`),\n compiled: () => val,\n })\n } else {\n ops.push({\n kind: `field`,\n alias: [...prefixPath, key].join(`.`),\n compiled: compileExpression(expression as BasicExpression),\n })\n }\n }\n }\n}\n"],"names":["ValClass","map","isExpressionLike","PropRef","compileExpression"],"mappings":";;;;;AAwBA,SAAS,UAAU,OAAiB;AAClC,MAAI,iBAAiBA,GAAAA,MAAU,QAAO,MAAM;AAC5C,SAAO;AACT;AAKA,SAAS,aACP,IACA,eACA,eACM;AACN,QAAM,QAAQ,GAAG,OAAO,aAAa;AACrC,MAAI,SAAS,OAAO,UAAU,UAAU;AAEtC,QAAI,SAAc;AAClB,UAAM,OAAO,GAAG;AAChB,QAAI,KAAK,W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AAO;AAEL,cAAM,aAAa;AACnB,cAAM,aAAa,CAAC,GAAG,UAAU;AACjC,YAAI,KAAK;AAAA,UACP,MAAM;AAAA,UACN;AAAA,UACA,QAAQ,CAAC,QAAS,IAAY,UAAU;AAAA,QAAA,CACzC;AAAA,MACH;AACA;AAAA,IACF;AAEA,UAAM,aAAa;AACnB,QAAI,qBAAqB,UAAU,GAAG;AAEpC,oBAAc,CAAC,GAAG,YAAY,GAAG,GAAG,YAAY,GAAG;AACnD;AAAA,IACF;AAEA,QAAI,sBAAsB,UAAU,GAAG;AAErC,UAAI,KAAK;AAAA,QACP,MAAM;AAAA,QACN,OAAO,CAAC,GAAG,YAAY,GAAG,EAAE,KAAK,GAAG;AAAA,QACpC,UAAU,MAAM;AAAA,MAAA,CACjB;AAAA,IACH,OAAO;AACL,UAAI,eAAe,UAAa,CAACF,GAAAA,iBAAiB,UAAU,GAAG;AAC7D,YAAI,KAAK;AAAA,UACP,MAAM;AAAA,UACN,OAAO,CAAC,GAAG,YAAY,GAAG,EAAE,KAAK,GAAG;AAAA,UACpC,UAAU,MAAM;AAAA,QAAA,CACjB;AACD;AAAA,MACF;AAEA,UAAI,sBAAsBF,GAAAA,OAAU;AAClC,cAAM,MAAM,WAAW;AACvB,YAAI,KAAK;AAAA,UACP,MAAM;AAAA,UACN,OAAO,CAAC,GAAG,YAAY,GAAG,EAAE,KAAK,GAAG;AAAA,UACpC,UAAU,MAAM;AAAA,QAAA,CACjB;AAAA,MACH,OAAO;AACL,YAAI,KAAK;AAAA,UACP,MAAM;AAAA,UACN,OAAO,CAAC,GAAG,YAAY,GAAG,EAAE,KAAK,GAAG;AAAA,UACpC,UAAUI,WAAAA,kBAAkB,UAA6B;AAAA,QAAA,CAC1D;AAAA,MACH;AAAA,IACF;AAAA,EACF;AACF;;"}
1
+ {"version":3,"file":"select.cjs","sources":["../../../../src/query/compiler/select.ts"],"sourcesContent":["import { map } from \"@tanstack/db-ivm\"\nimport { PropRef, Value as ValClass, isExpressionLike } from \"../ir.js\"\nimport { AggregateNotSupportedError } from \"../../errors.js\"\nimport { compileExpression } from \"./evaluators.js\"\nimport type { Aggregate, BasicExpression, Select } from \"../ir.js\"\nimport type {\n KeyedStream,\n NamespacedAndKeyedStream,\n NamespacedRow,\n} from \"../../types.js\"\n\n/**\n * Type for operations array used in select processing\n */\ntype SelectOp =\n | {\n kind: `merge`\n targetPath: Array<string>\n source: (row: NamespacedRow) => any\n }\n | { kind: `field`; alias: string; compiled: (row: NamespacedRow) => any }\n\n/**\n * Unwraps any Value expressions\n */\nfunction unwrapVal(input: any): any {\n if (input instanceof ValClass) return input.value\n return input\n}\n\n/**\n * Processes a merge operation by merging source values into the target path\n */\nfunction processMerge(\n op: Extract<SelectOp, { kind: `merge` }>,\n namespacedRow: NamespacedRow,\n selectResults: Record<string, any>\n): void {\n const value = op.source(namespacedRow)\n if (value && typeof value === `object`) {\n // Ensure target object exists\n let cursor: any = selectResults\n const path = op.targetPath\n if (path.length === 0) {\n // Top-level merge\n for (const [k, v] of Object.entries(value)) {\n selectResults[k] = unwrapVal(v)\n }\n } else {\n for (let i = 0; i < path.length; i++) {\n const seg = path[i]!\n if (i === path.length - 1) {\n const dest = (cursor[seg] ??= {})\n if (typeof dest === `object`) {\n for (const [k, v] of Object.entries(value)) {\n dest[k] = unwrapVal(v)\n }\n }\n } else {\n const next = cursor[seg]\n if (next == null || typeof next !== `object`) {\n cursor[seg] = {}\n }\n cursor = cursor[seg]\n }\n }\n }\n }\n}\n\n/**\n * Processes a non-merge operation by setting the field value at the specified alias path\n */\nfunction processNonMergeOp(\n op: Extract<SelectOp, { kind: `field` }>,\n namespacedRow: NamespacedRow,\n selectResults: Record<string, any>\n): void {\n // Support nested alias paths like \"meta.author.name\"\n const path = op.alias.split(`.`)\n if (path.length === 1) {\n selectResults[op.alias] = op.compiled(namespacedRow)\n } else {\n let cursor: any = selectResults\n for (let i = 0; i < path.length - 1; i++) {\n const seg = path[i]!\n const next = cursor[seg]\n if (next == null || typeof next !== `object`) {\n cursor[seg] = {}\n }\n cursor = cursor[seg]\n }\n cursor[path[path.length - 1]!] = unwrapVal(op.compiled(namespacedRow))\n }\n}\n\n/**\n * Processes a single row to generate select results\n */\nfunction processRow(\n [key, namespacedRow]: [unknown, NamespacedRow],\n ops: Array<SelectOp>\n): [unknown, typeof namespacedRow & { __select_results: any }] {\n const selectResults: Record<string, any> = {}\n\n for (const op of ops) {\n if (op.kind === `merge`) {\n processMerge(op, namespacedRow, selectResults)\n } else {\n processNonMergeOp(op, namespacedRow, selectResults)\n }\n }\n\n // Return the namespaced row with __select_results added\n return [\n key,\n {\n ...namespacedRow,\n __select_results: selectResults,\n },\n ] as [\n unknown,\n typeof namespacedRow & { __select_results: typeof selectResults },\n ]\n}\n\n/**\n * Processes the SELECT clause and places results in __select_results\n * while preserving the original namespaced row for ORDER BY access\n */\nexport function processSelect(\n pipeline: NamespacedAndKeyedStream,\n select: Select,\n _allInputs: Record<string, KeyedStream>\n): NamespacedAndKeyedStream {\n // Build ordered operations to preserve authoring order (spreads and fields)\n const ops: Array<SelectOp> = []\n\n addFromObject([], select, ops)\n\n return pipeline.pipe(map((row) => processRow(row, ops)))\n}\n\n/**\n * Helper function to check if an expression is an aggregate\n */\nfunction isAggregateExpression(\n expr: BasicExpression | Aggregate\n): expr is Aggregate {\n return expr.type === `agg`\n}\n\n/**\n * Processes a single argument in a function context\n */\nexport function processArgument(\n arg: BasicExpression | Aggregate,\n namespacedRow: NamespacedRow\n): any {\n if (isAggregateExpression(arg)) {\n throw new AggregateNotSupportedError()\n }\n\n // Pre-compile the expression and evaluate immediately\n const compiledExpression = compileExpression(arg)\n const value = compiledExpression(namespacedRow)\n\n return value\n}\n\n/**\n * Helper function to check if an object is a nested select object\n *\n * .select({\n * id: users.id,\n * profile: { // <-- this is a nested select object\n * name: users.name,\n * email: users.email\n * }\n * })\n */\nfunction isNestedSelectObject(obj: any): boolean {\n return obj && typeof obj === `object` && !isExpressionLike(obj)\n}\n\n/**\n * Helper function to process select objects and build operations array\n */\nfunction addFromObject(\n prefixPath: Array<string>,\n obj: any,\n ops: Array<SelectOp>\n) {\n for (const [key, value] of Object.entries(obj)) {\n if (key.startsWith(`__SPREAD_SENTINEL__`)) {\n const rest = key.slice(`__SPREAD_SENTINEL__`.length)\n const splitIndex = rest.lastIndexOf(`__`)\n const pathStr = splitIndex >= 0 ? rest.slice(0, splitIndex) : rest\n const isRefExpr =\n value &&\n typeof value === `object` &&\n `type` in (value as any) &&\n (value as any).type === `ref`\n if (pathStr.includes(`.`) || isRefExpr) {\n // Merge into the current destination (prefixPath) from the referenced source path\n const targetPath = [...prefixPath]\n const expr = isRefExpr\n ? (value as BasicExpression)\n : (new PropRef(pathStr.split(`.`)) as BasicExpression)\n const compiled = compileExpression(expr)\n ops.push({ kind: `merge`, targetPath, source: compiled })\n } else {\n // Table-level: pathStr is the alias; merge from namespaced row at the current prefix\n const tableAlias = pathStr\n const targetPath = [...prefixPath]\n ops.push({\n kind: `merge`,\n targetPath,\n source: (row) => (row as any)[tableAlias],\n })\n }\n continue\n }\n\n const expression = value as any\n if (isNestedSelectObject(expression)) {\n // Nested selection object\n addFromObject([...prefixPath, key], expression, ops)\n continue\n }\n\n if (isAggregateExpression(expression)) {\n // Placeholder for group-by processing later\n ops.push({\n kind: `field`,\n alias: [...prefixPath, key].join(`.`),\n compiled: () => null,\n })\n } else {\n if (expression === undefined || !isExpressionLike(expression)) {\n ops.push({\n kind: `field`,\n alias: [...prefixPath, key].join(`.`),\n compiled: () => expression,\n })\n continue\n }\n // If the expression is a Value wrapper, embed the literal to avoid re-compilation mishaps\n if (expression instanceof ValClass) {\n const val = expression.value\n ops.push({\n kind: `field`,\n alias: [...prefixPath, key].join(`.`),\n compiled: () => val,\n })\n } else {\n ops.push({\n kind: `field`,\n alias: [...prefixPath, key].join(`.`),\n compiled: compileExpression(expression as BasicExpression),\n })\n }\n }\n }\n}\n"],"names":["ValClass","map","isExpressionLike","PropRef","compileExpression"],"mappings":";;;;;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AC,GAAG,YAAY,GAAG,EAAE,KAAK,GAAG;AAAA,UACpC,UAAUI,WAAAA,kBAAkB,UAA6B;AAAA,QAAA,CAC1D;AAAA,MACH;AAAA,IACF;AAAA,EACF;AACF;;"}
@@ -8,3 +8,7 @@ export type QueryCache = WeakMap<QueryIR, CompilationResult>;
8
8
  * Mapping from optimized queries back to their original queries for caching
9
9
  */
10
10
  export type QueryMapping = WeakMap<QueryIR, QueryIR>;
11
+ export type WindowOptions = {
12
+ offset?: number;
13
+ limit?: number;
14
+ };
@@ -4,3 +4,4 @@ export type { Ref } from './builder/types.js';
4
4
  export { compileQuery } from './compiler/index.js';
5
5
  export { createLiveQueryCollection, liveQueryCollectionOptions, } from './live-query-collection.js';
6
6
  export { type LiveQueryCollectionConfig } from './live/types.js';
7
+ export { type LiveQueryCollectionUtils } from './live/collection-config-builder.js';
@@ -3,22 +3,40 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const dbIvm = require("@tanstack/db-ivm");
4
4
  const index = require("../compiler/index.cjs");
5
5
  const index$1 = require("../builder/index.cjs");
6
+ const errors = require("../../errors.cjs");
7
+ const scheduler = require("../../scheduler.cjs");
8
+ const transactions = require("../../transactions.cjs");
6
9
  const collectionSubscriber = require("./collection-subscriber.cjs");
10
+ const collectionRegistry = require("./collection-registry.cjs");
7
11
  let liveQueryCollectionCounter = 0;
8
12
  class CollectionConfigBuilder {
9
13
  constructor(config) {
10
14
  this.config = config;
15
+ this.compiledAliasToCollectionId = {};
11
16
  this.resultKeys = /* @__PURE__ */ new WeakMap();
12
17
  this.orderByIndices = /* @__PURE__ */ new WeakMap();
13
18
  this.isGraphRunning = false;
19
+ this.runCount = 0;
14
20
  this.isInErrorState = false;
21
+ this.aliasDependencies = {};
22
+ this.builderDependencies = /* @__PURE__ */ new Set();
23
+ this.pendingGraphRuns = /* @__PURE__ */ new Map();
15
24
  this.subscriptions = {};
16
- this.lazyCollectionsCallbacks = {};
17
- this.lazyCollections = /* @__PURE__ */ new Set();
25
+ this.lazySourcesCallbacks = {};
26
+ this.lazySources = /* @__PURE__ */ new Set();
18
27
  this.optimizableOrderByCollections = {};
19
28
  this.id = config.id || `live-query-${++liveQueryCollectionCounter}`;
20
29
  this.query = buildQueryFromConfig(config);
21
30
  this.collections = extractCollectionsFromQuery(this.query);
31
+ const collectionAliasesById = extractCollectionAliases(this.query);
32
+ this.collectionByAlias = {};
33
+ for (const [collectionId, aliases] of collectionAliasesById.entries()) {
34
+ const collection = this.collections[collectionId];
35
+ if (!collection) continue;
36
+ for (const alias of aliases) {
37
+ this.collectionByAlias[alias] = collection;
38
+ }
39
+ }
22
40
  if (this.query.orderBy && this.query.orderBy.length > 0) {
23
41
  this.compare = createOrderByComparator(this.orderByIndices);
24
42
  }
@@ -37,9 +55,60 @@ class CollectionConfigBuilder {
37
55
  onUpdate: this.config.onUpdate,
38
56
  onDelete: this.config.onDelete,
39
57
  startSync: this.config.startSync,
40
- singleResult: this.query.singleResult
58
+ singleResult: this.query.singleResult,
59
+ utils: {
60
+ getRunCount: this.getRunCount.bind(this),
61
+ getBuilder: () => this,
62
+ setWindow: this.setWindow.bind(this)
63
+ }
41
64
  };
42
65
  }
66
+ setWindow(options) {
67
+ if (!this.windowFn) {
68
+ throw new errors.SetWindowRequiresOrderByError();
69
+ }
70
+ this.windowFn(options);
71
+ this.maybeRunGraphFn?.();
72
+ if (this.liveQueryCollection?.isLoadingSubset) {
73
+ return new Promise((resolve) => {
74
+ const unsubscribe = this.liveQueryCollection.on(
75
+ `loadingSubset:change`,
76
+ (event) => {
77
+ if (!event.isLoadingSubset) {
78
+ unsubscribe();
79
+ resolve();
80
+ }
81
+ }
82
+ );
83
+ });
84
+ }
85
+ return true;
86
+ }
87
+ /**
88
+ * Resolves a collection alias to its collection ID.
89
+ *
90
+ * Uses a two-tier lookup strategy:
91
+ * 1. First checks compiled aliases (includes subquery inner aliases)
92
+ * 2. Falls back to declared aliases from the query's from/join clauses
93
+ *
94
+ * @param alias - The alias to resolve (e.g., "employee", "manager")
95
+ * @returns The collection ID that the alias references
96
+ * @throws {Error} If the alias is not found in either lookup
97
+ */
98
+ getCollectionIdForAlias(alias) {
99
+ const compiled = this.compiledAliasToCollectionId[alias];
100
+ if (compiled) {
101
+ return compiled;
102
+ }
103
+ const collection = this.collectionByAlias[alias];
104
+ if (collection) {
105
+ return collection.id;
106
+ }
107
+ throw new Error(`Unknown source alias "${alias}"`);
108
+ }
109
+ isLazyAlias(alias) {
110
+ return this.lazySources.has(alias);
111
+ }
43
112
  // The callback function is called after the graph has run.
44
113
  // This gives the callback a chance to load more data if needed,
45
114
  // that's used to optimize orderBy operators that set a limit,
@@ -47,14 +116,20 @@ class CollectionConfigBuilder {
47
116
  // That can happen because even though we load N rows, the pipeline might filter some of these rows out
48
117
  // causing the orderBy operator to receive less than N rows or even no rows at all.
49
118
  // So this callback would notice that it doesn't have enough rows and load some more.
50
- // The callback returns a boolean, when it's true it's done loading data.
51
- maybeRunGraph(config, syncState, callback) {
119
+ // The callback returns a boolean, when it's true it's done loading data and we can mark the collection as ready.
120
+ maybeRunGraph(callback) {
52
121
  if (this.isGraphRunning) {
53
122
  return;
54
123
  }
124
+ if (!this.currentSyncConfig || !this.currentSyncState) {
125
+ throw new Error(
126
+ `maybeRunGraph called without active sync session. This should not happen.`
127
+ );
128
+ }
55
129
  this.isGraphRunning = true;
56
130
  try {
57
- const { begin, commit } = config;
131
+ const { begin, commit } = this.currentSyncConfig;
132
+ const syncState = this.currentSyncState;
58
133
  if (this.isInErrorState) {
59
134
  return;
60
135
  }
@@ -66,21 +141,136 @@ class CollectionConfigBuilder {
66
141
  if (syncState.messagesCount === 0) {
67
142
  begin();
68
143
  commit();
69
- this.updateLiveQueryStatus(config);
144
+ this.updateLiveQueryStatus(this.currentSyncConfig);
70
145
  }
71
146
  }
72
147
  } finally {
73
148
  this.isGraphRunning = false;
74
149
  }
75
150
  }
151
+ /**
152
+ * Schedules a graph run with the transaction-scoped scheduler.
153
+ * Ensures each builder runs at most once per transaction, with automatic dependency tracking
154
+ * to run parent queries before child queries. Outside a transaction, runs immediately.
155
+ *
156
+ * Multiple calls during a transaction are coalesced into a single execution.
157
+ * Dependencies are auto-discovered from subscribed live queries, or can be overridden.
158
+ * Load callbacks are combined when entries merge.
159
+ *
160
+ * Uses the current sync session's config and syncState from instance properties.
161
+ *
162
+ * @param callback - Optional callback to load more data if needed (returns true when done)
163
+ * @param options - Optional scheduling configuration
164
+ * @param options.contextId - Transaction ID to group work; defaults to active transaction
165
+ * @param options.jobId - Unique identifier for this job; defaults to this builder instance
166
+ * @param options.alias - Source alias that triggered this schedule; adds alias-specific dependencies
167
+ * @param options.dependencies - Explicit dependency list; overrides auto-discovered dependencies
168
+ */
169
+ scheduleGraphRun(callback, options) {
170
+ const contextId = options?.contextId ?? transactions.getActiveTransaction()?.id;
171
+ const jobId = options?.jobId ?? this;
172
+ const dependentBuilders = (() => {
173
+ if (options?.dependencies) {
174
+ return options.dependencies;
175
+ }
176
+ const deps = new Set(this.builderDependencies);
177
+ if (options?.alias) {
178
+ const aliasDeps = this.aliasDependencies[options.alias];
179
+ if (aliasDeps) {
180
+ for (const dep of aliasDeps) {
181
+ deps.add(dep);
182
+ }
183
+ }
184
+ }
185
+ deps.delete(this);
186
+ return Array.from(deps);
187
+ })();
188
+ if (!this.currentSyncConfig || !this.currentSyncState) {
189
+ throw new Error(
190
+ `scheduleGraphRun called without active sync session. This should not happen.`
191
+ );
192
+ }
193
+ let pending = contextId ? this.pendingGraphRuns.get(contextId) : void 0;
194
+ if (!pending) {
195
+ pending = {
196
+ loadCallbacks: /* @__PURE__ */ new Set()
197
+ };
198
+ if (contextId) {
199
+ this.pendingGraphRuns.set(contextId, pending);
200
+ }
201
+ }
202
+ if (callback) {
203
+ pending.loadCallbacks.add(callback);
204
+ }
205
+ const pendingToPass = contextId ? void 0 : pending;
206
+ scheduler.transactionScopedScheduler.schedule({
207
+ contextId,
208
+ jobId,
209
+ dependencies: dependentBuilders,
210
+ run: () => this.executeGraphRun(contextId, pendingToPass)
211
+ });
212
+ }
213
+ /**
214
+ * Clears pending graph run state for a specific context.
215
+ * Called when the scheduler clears a context (e.g., transaction rollback/abort).
216
+ */
217
+ clearPendingGraphRun(contextId) {
218
+ this.pendingGraphRuns.delete(contextId);
219
+ }
220
+ /**
221
+ * Executes a pending graph run. Called by the scheduler when dependencies are satisfied.
222
+ * Clears the pending state BEFORE execution so that any re-schedules during the run
223
+ * create fresh state and don't interfere with the current execution.
224
+ * Uses instance sync state - if sync has ended, gracefully returns without executing.
225
+ *
226
+ * @param contextId - Optional context ID to look up pending state
227
+ * @param pendingParam - For immediate execution (no context), pending state is passed directly
228
+ */
229
+ executeGraphRun(contextId, pendingParam) {
230
+ const pending = pendingParam ?? (contextId ? this.pendingGraphRuns.get(contextId) : void 0);
231
+ if (contextId) {
232
+ this.pendingGraphRuns.delete(contextId);
233
+ }
234
+ if (!pending) {
235
+ return;
236
+ }
237
+ if (!this.currentSyncConfig || !this.currentSyncState) {
238
+ return;
239
+ }
240
+ this.incrementRunCount();
241
+ const combinedLoader = () => {
242
+ let allDone = true;
243
+ let firstError;
244
+ pending.loadCallbacks.forEach((loader) => {
245
+ try {
246
+ allDone = loader() && allDone;
247
+ } catch (error) {
248
+ allDone = false;
249
+ firstError ??= error;
250
+ }
251
+ });
252
+ if (firstError) {
253
+ throw firstError;
254
+ }
255
+ return allDone;
256
+ };
257
+ this.maybeRunGraph(combinedLoader);
258
+ }
76
259
  getSyncConfig() {
77
260
  return {
78
261
  rowUpdateMode: `full`,
79
262
  sync: this.syncFn.bind(this)
80
263
  };
81
264
  }
265
+ incrementRunCount() {
266
+ this.runCount++;
267
+ }
268
+ getRunCount() {
269
+ return this.runCount;
270
+ }
82
271
  syncFn(config) {
83
272
  this.liveQueryCollection = config.collection;
273
+ this.currentSyncConfig = config;
84
274
  const syncState = {
85
275
  messagesCount: 0,
86
276
  subscribedToAllCollections: false,
@@ -90,44 +280,70 @@ class CollectionConfigBuilder {
90
280
  config,
91
281
  syncState
92
282
  );
93
- const loadMoreDataCallbacks = this.subscribeToAllCollections(
283
+ this.currentSyncState = fullSyncState;
284
+ this.unsubscribeFromSchedulerClears = scheduler.transactionScopedScheduler.onClear(
285
+ (contextId) => {
286
+ this.clearPendingGraphRun(contextId);
287
+ }
288
+ );
289
+ const loadSubsetDataCallbacks = this.subscribeToAllCollections(
94
290
  config,
95
291
  fullSyncState
96
292
  );
97
- this.maybeRunGraph(config, fullSyncState, loadMoreDataCallbacks);
293
+ this.maybeRunGraphFn = () => this.scheduleGraphRun(loadSubsetDataCallbacks);
294
+ this.scheduleGraphRun(loadSubsetDataCallbacks);
98
295
  return () => {
99
296
  syncState.unsubscribeCallbacks.forEach((unsubscribe) => unsubscribe());
297
+ this.currentSyncConfig = void 0;
298
+ this.currentSyncState = void 0;
299
+ this.pendingGraphRuns.clear();
100
300
  this.graphCache = void 0;
101
301
  this.inputsCache = void 0;
102
302
  this.pipelineCache = void 0;
103
- this.collectionWhereClausesCache = void 0;
104
- this.lazyCollections.clear();
303
+ this.sourceWhereClausesCache = void 0;
304
+ this.lazySources.clear();
105
305
  this.optimizableOrderByCollections = {};
106
- this.lazyCollectionsCallbacks = {};
306
+ this.lazySourcesCallbacks = {};
307
+ Object.keys(this.subscriptions).forEach(
308
+ (key) => delete this.subscriptions[key]
309
+ );
310
+ this.compiledAliasToCollectionId = {};
311
+ this.unsubscribeFromSchedulerClears?.();
312
+ this.unsubscribeFromSchedulerClears = void 0;
107
313
  };
108
314
  }
315
+ /**
316
+ * Compiles the query pipeline with all declared aliases.
317
+ */
109
318
  compileBasePipeline() {
110
319
  this.graphCache = new dbIvm.D2();
111
320
  this.inputsCache = Object.fromEntries(
112
- Object.entries(this.collections).map(([key]) => [
113
- key,
321
+ Object.keys(this.collectionByAlias).map((alias) => [
322
+ alias,
114
323
  this.graphCache.newInput()
115
324
  ])
116
325
  );
117
- const {
118
- pipeline: pipelineCache,
119
- collectionWhereClauses: collectionWhereClausesCache
120
- } = index.compileQuery(
326
+ const compilation = index.compileQuery(
121
327
  this.query,
122
328
  this.inputsCache,
123
329
  this.collections,
124
330
  this.subscriptions,
125
- this.lazyCollectionsCallbacks,
126
- this.lazyCollections,
127
- this.optimizableOrderByCollections
331
+ this.lazySourcesCallbacks,
332
+ this.lazySources,
333
+ this.optimizableOrderByCollections,
334
+ (windowFn) => {
335
+ this.windowFn = windowFn;
336
+ }
128
337
  );
129
- this.pipelineCache = pipelineCache;
130
- this.collectionWhereClausesCache = collectionWhereClausesCache;
338
+ this.pipelineCache = compilation.pipeline;
339
+ this.sourceWhereClausesCache = compilation.sourceWhereClauses;
340
+ this.compiledAliasToCollectionId = compilation.aliasToCollectionId;
341
+ const missingAliases = Object.keys(this.compiledAliasToCollectionId).filter(
342
+ (alias) => !Object.hasOwn(this.inputsCache, alias)
343
+ );
344
+ if (missingAliases.length > 0) {
345
+ throw new errors.MissingAliasInputsError(missingAliases);
346
+ }
131
347
  }
132
348
  maybeCompileBasePipeline() {
133
349
  if (!this.graphCache || !this.inputsCache || !this.pipelineCache) {
@@ -237,36 +453,52 @@ class CollectionConfigBuilder {
237
453
  (collection) => collection.isReady()
238
454
  );
239
455
  }
456
+ /**
457
+ * Creates per-alias subscriptions enabling self-join support.
458
+ * Each alias gets its own subscription with independent filters, even for the same collection.
459
+ * Example: `{ employee: col, manager: col }` creates two separate subscriptions.
460
+ */
240
461
  subscribeToAllCollections(config, syncState) {
241
- const loaders = Object.entries(this.collections).map(
242
- ([collectionId, collection]) => {
243
- const collectionSubscriber$1 = new collectionSubscriber.CollectionSubscriber(
244
- collectionId,
245
- collection,
246
- config,
247
- syncState,
248
- this
249
- );
250
- const subscription = collectionSubscriber$1.subscribe();
251
- this.subscriptions[collectionId] = subscription;
252
- const statusUnsubscribe = collection.on(`status:change`, (event) => {
253
- this.handleSourceStatusChange(config, collectionId, event);
254
- });
255
- syncState.unsubscribeCallbacks.add(statusUnsubscribe);
256
- const loadMore = collectionSubscriber$1.loadMoreIfNeeded.bind(
257
- collectionSubscriber$1,
258
- subscription
259
- );
260
- return loadMore;
462
+ const compiledAliases = Object.entries(this.compiledAliasToCollectionId);
463
+ if (compiledAliases.length === 0) {
464
+ throw new Error(
465
+ `Compiler returned no alias metadata for query '${this.id}'. This should not happen; please report.`
466
+ );
467
+ }
468
+ const loaders = compiledAliases.map(([alias, collectionId]) => {
469
+ const collection = this.collectionByAlias[alias] ?? this.collections[collectionId];
470
+ const dependencyBuilder = collectionRegistry.getCollectionBuilder(collection);
471
+ if (dependencyBuilder && dependencyBuilder !== this) {
472
+ this.aliasDependencies[alias] = [dependencyBuilder];
473
+ this.builderDependencies.add(dependencyBuilder);
474
+ } else {
475
+ this.aliasDependencies[alias] = [];
261
476
  }
262
- );
263
- const loadMoreDataCallback = () => {
477
+ const collectionSubscriber$1 = new collectionSubscriber.CollectionSubscriber(
478
+ alias,
479
+ collectionId,
480
+ collection,
481
+ this
482
+ );
483
+ const statusUnsubscribe = collection.on(`status:change`, (event) => {
484
+ this.handleSourceStatusChange(config, collectionId, event);
485
+ });
486
+ syncState.unsubscribeCallbacks.add(statusUnsubscribe);
487
+ const subscription = collectionSubscriber$1.subscribe();
488
+ this.subscriptions[alias] = subscription;
489
+ const loadMore = collectionSubscriber$1.loadMoreIfNeeded.bind(
490
+ collectionSubscriber$1,
491
+ subscription
492
+ );
493
+ return loadMore;
494
+ });
495
+ const loadSubsetDataCallbacks = () => {
264
496
  loaders.map((loader) => loader());
265
497
  return true;
266
498
  };
267
499
  syncState.subscribedToAllCollections = true;
268
500
  this.updateLiveQueryStatus(config);
269
- return loadMoreDataCallback;
501
+ return loadSubsetDataCallbacks;
270
502
  }
271
503
  }
272
504
  function buildQueryFromConfig(config) {
@@ -315,6 +547,34 @@ function extractCollectionsFromQuery(query) {
315
547
  extractFromQuery(query);
316
548
  return collections;
317
549
  }
550
+ function extractCollectionAliases(query) {
551
+ const aliasesById = /* @__PURE__ */ new Map();
552
+ function recordAlias(source) {
553
+ if (!source) return;
554
+ if (source.type === `collectionRef`) {
555
+ const { id } = source.collection;
556
+ const existing = aliasesById.get(id);
557
+ if (existing) {
558
+ existing.add(source.alias);
559
+ } else {
560
+ aliasesById.set(id, /* @__PURE__ */ new Set([source.alias]));
561
+ }
562
+ } else if (source.type === `queryRef`) {
563
+ traverse(source.query);
564
+ }
565
+ }
566
+ function traverse(q) {
567
+ if (!q) return;
568
+ recordAlias(q.from);
569
+ if (q.join) {
570
+ for (const joinClause of q.join) {
571
+ recordAlias(joinClause.from);
572
+ }
573
+ }
574
+ }
575
+ traverse(query);
576
+ return aliasesById;
577
+ }
318
578
  function accumulateChanges(acc, [[key, tupleData], multiplicity]) {
319
579
  const [value, orderByIndex] = tupleData;
320
580
  const changes = acc.get(key) || {