1
1
import { MultiSet } from "@tanstack/db-ivm"
2
- import { convertToBasicExpression } from "../compiler/expressions.js"
2
+ import { convertOrderByToBasicExpression , convertToBasicExpression } from "../compiler/expressions.js"
3
3
import type { FullSyncState } from "./types.js"
4
4
import type { MultiSetArray , RootStreamBuilder } from "@tanstack/db-ivm"
5
5
import type { Collection } from "../../collection/index.js"
@@ -16,26 +16,29 @@ export class CollectionSubscriber<
16
16
// Keep track of the biggest value we've sent so far (needed for orderBy optimization)
17
17
private biggest : any = undefined
18
18
19
+ private collectionAlias : string
20
+
19
21
constructor (
20
22
private collectionId : string ,
21
23
private collection : Collection ,
22
24
private config : Parameters < SyncConfig < TResult > [ `sync`] > [ 0 ] ,
23
25
private syncState : FullSyncState ,
24
26
private collectionConfigBuilder : CollectionConfigBuilder < TContext , TResult >
25
- ) { }
26
-
27
- subscribe ( ) : CollectionSubscription {
28
- const collectionAlias = findCollectionAlias (
27
+ ) {
28
+ this . collectionAlias = findCollectionAlias (
29
29
this . collectionId ,
30
30
this . collectionConfigBuilder . query
31
- )
32
- const whereClause = this . getWhereClauseFromAlias ( collectionAlias )
31
+ ) !
32
+ }
33
+
34
+ subscribe ( ) : CollectionSubscription {
35
+ const whereClause = this . getWhereClauseFromAlias ( this . collectionAlias )
33
36
34
37
if ( whereClause ) {
35
38
// Convert WHERE clause to BasicExpression format for collection subscription
36
39
const whereExpression = convertToBasicExpression (
37
40
whereClause ,
38
- collectionAlias !
41
+ this . collectionAlias
39
42
)
40
43
41
44
if ( whereExpression ) {
@@ -164,11 +167,14 @@ export class CollectionSubscriber<
164
167
165
168
subscription . setOrderByIndex ( index )
166
169
170
+ // Normalize the orderBy clauses such that the references are relative to the collection
171
+ const normalizedOrderBy = convertOrderByToBasicExpression ( orderBy , this . collectionAlias )
172
+
167
173
// Load the first `offset + limit` values from the index
168
174
// i.e. the K items from the collection that fall into the requested range: [offset, offset + limit[
169
175
subscription . requestLimitedSnapshot ( {
170
176
limit : offset + limit ,
171
- orderBy,
177
+ orderBy : normalizedOrderBy ,
172
178
} )
173
179
174
180
return subscription
@@ -234,9 +240,16 @@ export class CollectionSubscriber<
234
240
const biggestSentValue = biggestSentRow
235
241
? valueExtractorForRawRow ( biggestSentRow )
236
242
: biggestSentRow
243
+
244
+ // Normalize the orderBy clauses such that the references are relative to the collection
245
+ const normalizedOrderBy = convertOrderByToBasicExpression (
246
+ orderBy ,
247
+ this . collectionAlias
248
+ )
249
+
237
250
// Take the `n` items after the biggest sent value
238
251
subscription . requestLimitedSnapshot ( {
239
- orderBy,
252
+ orderBy : normalizedOrderBy ,
240
253
limit : n ,
241
254
minValue : biggestSentValue ,
242
255
} )
0 commit comments