@@ -32,7 +32,8 @@ export class CollectionConfigBuilder<
32
32
private readonly id : string
33
33
readonly query : QueryIR
34
34
private readonly collections : Record < string , Collection < any , any , any > >
35
- private readonly collectionAliasesById : Map < string , Set < string > >
35
+ private readonly aliasToCollectionId : Record < string , string >
36
+ private readonly collectionByAlias : Record < string , Collection < any , any , any > >
36
37
37
38
// WeakMap to store the keys of the results
38
39
// so that we can retrieve them in the getKey function
@@ -52,7 +53,7 @@ export class CollectionConfigBuilder<
52
53
| Map < string , BasicExpression < boolean > >
53
54
| undefined
54
55
55
- // Map of collection ID to subscription
56
+ // Map of collection alias to subscription
56
57
readonly subscriptions : Record < string , CollectionSubscription > = { }
57
58
// Map of collection IDs to functions that load keys for that lazy collection
58
59
lazyCollectionsCallbacks : Record < string , LazyCollectionCallbacks > = { }
@@ -69,7 +70,18 @@ export class CollectionConfigBuilder<
69
70
70
71
this . query = buildQueryFromConfig ( config )
71
72
this . collections = extractCollectionsFromQuery ( this . query )
72
- this . collectionAliasesById = extractCollectionAliases ( this . query )
73
+ const collectionAliasesById = extractCollectionAliases ( this . query )
74
+
75
+ this . aliasToCollectionId = { }
76
+ this . collectionByAlias = { }
77
+ for ( const [ collectionId , aliases ] of collectionAliasesById . entries ( ) ) {
78
+ const collection = this . collections [ collectionId ]
79
+ if ( ! collection ) continue
80
+ for ( const alias of aliases ) {
81
+ this . aliasToCollectionId [ alias ] = collectionId
82
+ this . collectionByAlias [ alias ] = collection
83
+ }
84
+ }
73
85
74
86
// Create compare function for ordering if the query has orderBy
75
87
if ( this . query . orderBy && this . query . orderBy . length > 0 ) {
@@ -98,9 +110,12 @@ export class CollectionConfigBuilder<
98
110
}
99
111
}
100
112
101
- getCollectionAliases ( collectionId : string ) : Array < string > {
102
- const aliases = this . collectionAliasesById . get ( collectionId )
103
- return aliases ? Array . from ( aliases ) : [ ]
113
+ getCollectionIdForAlias ( alias : string ) : string {
114
+ const collectionId = this . aliasToCollectionId [ alias ]
115
+ if ( ! collectionId ) {
116
+ throw new Error ( `Unknown collection alias "${ alias } "` )
117
+ }
118
+ return collectionId
104
119
}
105
120
106
121
// The callback function is called after the graph has run.
@@ -203,8 +218,8 @@ export class CollectionConfigBuilder<
203
218
private compileBasePipeline ( ) {
204
219
this . graphCache = new D2 ( )
205
220
this . inputsCache = Object . fromEntries (
206
- Object . entries ( this . collections ) . map ( ( [ key ] ) => [
207
- key ,
221
+ Object . keys ( this . collectionByAlias ) . map ( ( alias ) => [
222
+ alias ,
208
223
this . graphCache ! . newInput < any > ( ) ,
209
224
] )
210
225
)
@@ -340,9 +355,11 @@ export class CollectionConfigBuilder<
340
355
config : Parameters < SyncConfig < TResult > [ `sync`] > [ 0 ] ,
341
356
syncState : FullSyncState
342
357
) {
343
- const loaders = Object . entries ( this . collections ) . map (
344
- ( [ collectionId , collection ] ) => {
358
+ const loaders = Object . entries ( this . collectionByAlias ) . map (
359
+ ( [ alias , collection ] ) => {
360
+ const collectionId = this . aliasToCollectionId [ alias ] !
345
361
const collectionSubscriber = new CollectionSubscriber (
362
+ alias ,
346
363
collectionId ,
347
364
collection ,
348
365
config ,
@@ -351,7 +368,9 @@ export class CollectionConfigBuilder<
351
368
)
352
369
353
370
const subscription = collectionSubscriber . subscribe ( )
354
- this . subscriptions [ collectionId ] = subscription
371
+ this . subscriptions [ alias ] = subscription
372
+ const collectionKey = `__collection:${ collectionId } `
373
+ this . subscriptions [ collectionKey ] = subscription
355
374
356
375
const loadMore = collectionSubscriber . loadMoreIfNeeded . bind (
357
376
collectionSubscriber ,
@@ -470,7 +489,7 @@ function extractCollectionAliases(query: QueryIR): Map<string, Set<string>> {
470
489
}
471
490
}
472
491
473
- function traverse ( q : QueryIR ) {
492
+ function traverse ( q ? : QueryIR ) {
474
493
if ( ! q ) return
475
494
476
495
recordAlias ( q . from )
0 commit comments