Skip to content

Commit 94e4785

Browse files
committed
Add debug representation
1 parent b5fd3f0 commit 94e4785

File tree

6 files changed

+63
-16
lines changed

6 files changed

+63
-16
lines changed

packages/service-core/src/routes/endpoints/sync-rules.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ErrorCode, errors, router, schema } from '@powersync/lib-services-framework';
2-
import { SqlBucketDescriptor, SqlSyncRules, SyncRulesErrors } from '@powersync/service-sync-rules';
2+
import { SqlSyncRules, SyncRulesErrors } from '@powersync/service-sync-rules';
33
import type { FastifyPluginAsync } from 'fastify';
44
import * as t from 'ts-codec';
55

packages/sync-rules/src/BaseSqlDataQuery.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,18 @@ export class BaseSqlDataQuery {
163163
return result;
164164
}
165165

166+
resolveResultSets(schema: SourceSchema, tables: Record<string, Record<string, ColumnDefinition>>) {
167+
const outTables = this.getColumnOutputs(schema);
168+
for (let table of outTables) {
169+
tables[table.name] ??= {};
170+
for (let column of table.columns) {
171+
if (column.name != 'id') {
172+
tables[table.name][column.name] ??= column;
173+
}
174+
}
175+
}
176+
}
177+
166178
evaluateRowWithOptions(options: EvaluateRowOptions): EvaluationResult[] {
167179
try {
168180
const { table, row, bucketIds } = options;

packages/sync-rules/src/SqlBucketDescriptor.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,7 @@ export class SqlBucketDescriptor implements BucketSource {
201201

202202
resolveResultSets(schema: SourceSchema, tables: Record<string, Record<string, ColumnDefinition>>) {
203203
for (let query of this.dataQueries) {
204-
const outTables = query.getColumnOutputs(schema);
205-
for (let table of outTables) {
206-
tables[table.name] ??= {};
207-
for (let column of table.columns) {
208-
if (column.name != 'id') {
209-
tables[table.name][column.name] ??= column;
210-
}
211-
}
212-
}
204+
query.resolveResultSets(schema, tables);
213205
}
214206
}
215207

packages/sync-rules/src/streams/parameter.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121
*/
2222
export interface BucketParameter {
2323
lookup: StaticLookup | EqualsRowInSubqueryLookup | OverlapsSubqueryLookup;
24-
2524
/**
2625
* Given a row in the table the stream is selecting from, return all possible instantiations of this parameter that
2726
* would match the row.

packages/sync-rules/src/streams/stream.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { BaseSqlDataQuery } from '../BaseSqlDataQuery.js';
22
import { BucketInclusionReason, BucketPriority, DEFAULT_BUCKET_PRIORITY } from '../BucketDescription.js';
33
import { BucketParameterQuerier, PendingQueriers } from '../BucketParameterQuerier.js';
44
import { BucketSource, BucketSourceType, ResultSetDescription } from '../BucketSource.js';
5+
import { ColumnDefinition } from '../ExpressionType.js';
56
import { SourceTableInterface } from '../SourceTableInterface.js';
67
import { GetQuerierOptions, RequestedStream } from '../SqlSyncRules.js';
78
import { TablePattern } from '../TablePattern.js';
@@ -88,7 +89,7 @@ export class SyncStream implements BucketSource {
8889
}
8990

9091
hasDynamicBucketQueries(): boolean {
91-
throw new Error('Method not implemented.');
92+
return this.variants.some((v) => v.hasDynamicBucketQueries);
9293
}
9394

9495
tableSyncsData(table: SourceTableInterface): boolean {
@@ -108,19 +109,44 @@ export class SyncStream implements BucketSource {
108109
}
109110

110111
getSourceTables(): Set<TablePattern> {
111-
throw new Error('Method not implemented.');
112+
let result = new Set<TablePattern>();
113+
result.add(this.data.sourceTable);
114+
for (let variant of this.variants) {
115+
for (const subquery of variant.subqueries) {
116+
result.add(subquery.parameterTable);
117+
}
118+
}
119+
120+
// Note: No physical tables for global_parameter_queries
121+
122+
return result;
112123
}
113124

114-
resolveResultSets(schema: SourceSchema): ResultSetDescription[] {
115-
throw new Error('Method not implemented.');
125+
resolveResultSets(schema: SourceSchema, tables: Record<string, Record<string, ColumnDefinition>>) {
126+
this.data.resolveResultSets(schema, tables);
116127
}
117128

118129
debugRepresentation() {
130+
return {
131+
name: this.name,
132+
type: this.type.toString(),
133+
variants: this.variants.map((v) => v.debugRepresentation()),
134+
data: {
135+
table: this.data.sourceTable,
136+
columns: this.data.columnOutputNames()
137+
}
138+
};
139+
119140
throw new Error('Method not implemented.');
120141
}
121142

122143
debugWriteOutputTables(result: Record<string, { query: string }[]>): void {
123-
throw new Error('Method not implemented.');
144+
result[this.data.table!] ??= [];
145+
const r = {
146+
query: this.data.sql
147+
};
148+
149+
result[this.data.table!].push(r);
124150
}
125151

126152
evaluateParameterRow(sourceTable: SourceTableInterface, row: SqliteRow): EvaluatedParametersResult[] {

packages/sync-rules/src/streams/variant.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ export class StreamVariant {
112112
return [...cartesianProduct(...instantiations)];
113113
}
114114

115+
get hasDynamicBucketQueries(): boolean {
116+
return this.requestFilters.some((f) => f.type == 'dynamic');
117+
}
118+
115119
querier(stream: SyncStream, reason: BucketInclusionReason, params: RequestParameters): BucketParameterQuerier | null {
116120
const instantiation = this.partiallyEvaluateParameters(params);
117121
if (instantiation == null) {
@@ -242,6 +246,20 @@ export class StreamVariant {
242246
}
243247
}
244248

249+
debugRepresentation(): any {
250+
return {
251+
id: this.id,
252+
parameters: this.parameters.map((p) => ({
253+
type: p.lookup.type
254+
})),
255+
subqueries: this.subqueries.map((s) => ({
256+
table: s.parameterTable
257+
})),
258+
additional_row_filters: this.additionalRowFilters.length,
259+
request_filters: this.requestFilters.map((f) => f.type)
260+
};
261+
}
262+
245263
/**
246264
* Replaces {@link StreamVariant.parameters} with static values looked up in request parameters.
247265
*

0 commit comments

Comments
 (0)