@@ -16,6 +16,12 @@ export interface CreateSourceParams {
1616 bucketIdTransformer : BucketIdTransformer ;
1717}
1818
19+ /**
20+ * A BucketSource is a _logical_ bucket or sync stream definition. It is primarily used to group together
21+ * related BucketDataSource, BucketParameterLookupSource and BucketParameterQuerierSource definitions,
22+ * for the purpose of subscribing to specific streams. It does not directly define the implementation
23+ * or replication process.
24+ */
1925export interface BucketSource {
2026 readonly name : string ;
2127 readonly type : BucketSourceType ;
@@ -116,8 +122,6 @@ export interface BucketParameterQuerierSourceDefinition {
116122 * definitions that only consist of a single query.
117123 */
118124export interface BucketDataSource {
119- readonly definition : BucketDataSourceDefinition ;
120-
121125 /**
122126 * Given a row as it appears in a table that affects sync data, return buckets, logical table names and transformed
123127 * data for rows to add to buckets.
@@ -126,7 +130,6 @@ export interface BucketDataSource {
126130}
127131
128132export interface BucketParameterLookupSource {
129- readonly definition : BucketParameterLookupSourceDefinition ;
130133 /**
131134 * Given a row in a source table that affects sync parameters, returns a structure to index which buckets rows should
132135 * be associated with.
@@ -138,8 +141,6 @@ export interface BucketParameterLookupSource {
138141}
139142
140143export interface BucketParameterQuerierSource {
141- readonly definition : BucketParameterQuerierSourceDefinition ;
142-
143144 /**
144145 * Reports {@link BucketParameterQuerier}s resolving buckets that a specific stream request should have access to.
145146 *
@@ -149,9 +150,70 @@ export interface BucketParameterQuerierSource {
149150 pushBucketParameterQueriers ( result : PendingQueriers , options : GetQuerierOptions ) : void ;
150151}
151152
153+ export interface DebugMergedSource
154+ extends BucketDataSource ,
155+ BucketParameterLookupSource ,
156+ BucketParameterQuerierSource { }
157+
152158export enum BucketSourceType {
153159 SYNC_RULE ,
154160 SYNC_STREAM
155161}
156162
157163export type ResultSetDescription = { name : string ; columns : ColumnDefinition [ ] } ;
164+
165+ export function mergeDataSources ( sources : BucketDataSource [ ] ) : BucketDataSource {
166+ return {
167+ evaluateRow ( options : EvaluateRowOptions ) : EvaluationResult [ ] {
168+ let results : EvaluationResult [ ] = [ ] ;
169+ for ( let source of sources ) {
170+ results . push ( ...source . evaluateRow ( options ) ) ;
171+ }
172+ return results ;
173+ }
174+ } ;
175+ }
176+
177+ export function mergeParameterLookupSources ( sources : BucketParameterLookupSource [ ] ) : BucketParameterLookupSource {
178+ return {
179+ evaluateParameterRow ( sourceTable : SourceTableInterface , row : SqliteRow ) : EvaluatedParametersResult [ ] {
180+ let results : EvaluatedParametersResult [ ] = [ ] ;
181+ for ( let source of sources ) {
182+ results . push ( ...source . evaluateParameterRow ( sourceTable , row ) ) ;
183+ }
184+ return results ;
185+ }
186+ } ;
187+ }
188+
189+ export function mergeParameterQuerierSources ( sources : BucketParameterQuerierSource [ ] ) : BucketParameterQuerierSource {
190+ return {
191+ pushBucketParameterQueriers ( result : PendingQueriers , options : GetQuerierOptions ) : void {
192+ for ( let source of sources ) {
193+ source . pushBucketParameterQueriers ( result , options ) ;
194+ }
195+ }
196+ } ;
197+ }
198+
199+ /**
200+ * For production purposes, we typically need to operate on the different sources separately. However, for debugging,
201+ * it is useful to have a single merged source that can evaluate everything.
202+ */
203+ export function debugHydratedMergedSource ( bucketSource : BucketSource , params ?: CreateSourceParams ) : DebugMergedSource {
204+ const resolvedParams = params ?? { bucketIdTransformer : ( id : string ) => id } ;
205+ const dataSource = mergeDataSources (
206+ bucketSource . dataSources . map ( ( source ) => source . createDataSource ( resolvedParams ) )
207+ ) ;
208+ const parameterLookupSource = mergeParameterLookupSources (
209+ bucketSource . parameterLookupSources . map ( ( source ) => source . createParameterLookupSource ( resolvedParams ) )
210+ ) ;
211+ const parameterQuerierSource = mergeParameterQuerierSources (
212+ bucketSource . parameterQuerierSources . map ( ( source ) => source . createParameterQuerierSource ( resolvedParams ) )
213+ ) ;
214+ return {
215+ evaluateParameterRow : parameterLookupSource . evaluateParameterRow . bind ( parameterLookupSource ) ,
216+ evaluateRow : dataSource . evaluateRow . bind ( dataSource ) ,
217+ pushBucketParameterQueriers : parameterQuerierSource . pushBucketParameterQueriers . bind ( parameterQuerierSource )
218+ } ;
219+ }
0 commit comments