Skip to content

Commit 65a0ea3

Browse files
Simplify query options for watched queries
1 parent 81d68e3 commit 65a0ea3

File tree

9 files changed

+47
-202
lines changed

9 files changed

+47
-202
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { CompiledQuery } from '../../types/types.js';
2+
import { WatchCompatibleQuery, WatchExecuteOptions } from './WatchedQuery.js';
3+
4+
/**
5+
* Options for {@link GetAllQuery}.
6+
*/
7+
export type GetAllQueryOptions = {
8+
sql: string;
9+
parameters?: ReadonlyArray<unknown>;
10+
};
11+
12+
/**
13+
* Performs a {@link AbstractPowerSyncDatabase.getAll} operation for a watched query.
14+
*/
15+
export class GetAllQuery<RowType = unknown> implements WatchCompatibleQuery<RowType[]> {
16+
constructor(protected options: GetAllQueryOptions) {}
17+
18+
compile(): CompiledQuery {
19+
return {
20+
sql: this.options.sql,
21+
parameters: this.options.parameters ?? []
22+
};
23+
}
24+
25+
execute(options: WatchExecuteOptions): Promise<RowType[]> {
26+
const { db, sql, parameters } = options;
27+
return db.getAll<RowType>(sql, parameters);
28+
}
29+
}

packages/common/src/client/watched/WatchedQuery.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { CompiledQuery } from '../../types/types.js';
22
import { BaseListener, BaseObserverInterface } from '../../utils/BaseObserver.js';
3+
import { AbstractPowerSyncDatabase } from '../AbstractPowerSyncDatabase.js';
34

45
export interface WatchedQueryState<Data> {
56
/**
@@ -27,13 +28,12 @@ export interface WatchedQueryState<Data> {
2728
}
2829

2930
/**
30-
* Similar to {@link CompiledQuery}, but used for watched queries.
31-
* The parameters are not read-only, as they can be modified. This is useful for compatibility with
32-
* PowerSync queries.
31+
* Options provided to the `execute` method of a {@link WatchCompatibleQuery}.
3332
*/
34-
export interface WatchCompiledQuery {
33+
export interface WatchExecuteOptions {
3534
sql: string;
3635
parameters: any[];
36+
db: AbstractPowerSyncDatabase;
3737
}
3838

3939
/**
@@ -43,7 +43,7 @@ export interface WatchCompiledQuery {
4343
* does not enforce an Array result type.
4444
*/
4545
export interface WatchCompatibleQuery<ResultType> {
46-
execute(compiled: WatchCompiledQuery): Promise<ResultType>;
46+
execute(options: WatchExecuteOptions): Promise<ResultType>;
4747
compile(): CompiledQuery;
4848
}
4949

packages/common/src/client/watched/processors/OnChangeQueryProcessor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ export class OnChangeQueryProcessor<Data> extends AbstractQueryProcessor<Data> {
8484
sql: compiledQuery.sql,
8585
// Allows casting from ReadOnlyArray[unknown] to Array<unknown>
8686
// This allows simpler compatibility with PowerSync queries
87-
parameters: [...compiledQuery.parameters]
87+
parameters: [...compiledQuery.parameters],
88+
db: this.options.db
8889
});
8990

9091
if (this.reportFetching) {

packages/common/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export * from './db/schema/Schema.js';
3030
export * from './db/schema/Table.js';
3131
export * from './db/schema/TableV2.js';
3232

33+
export * from './client/watched/GetAllQuery.js';
3334
export * from './client/watched/processors/AbstractQueryProcessor.js';
3435
export * from './client/watched/processors/comparators.js';
3536
export * from './client/watched/WatchedQuery.js';

packages/react/src/WatchedQuery.ts

Lines changed: 0 additions & 185 deletions
This file was deleted.

packages/react/src/hooks/suspense/useSingleSuspenseQuery.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ export const useSingleSuspenseQuery = <T = any>(
6767
const compiledQuery = parsedQuery.compile();
6868
const result = await parsedQuery.execute({
6969
sql: compiledQuery.sql,
70-
parameters: [...compiledQuery.parameters]
70+
parameters: [...compiledQuery.parameters],
71+
db: powerSync
7172
});
7273
if (signal.aborted) {
7374
return; // Abort if the signal is already aborted

packages/react/src/hooks/watched/useSingleQuery.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export const useSingleQuery = <RowType = any>(options: InternalHookOptions<RowTy
1919
const compiledQuery = query.compile();
2020
const result = await query.execute({
2121
sql: compiledQuery.sql,
22-
parameters: [...compiledQuery.parameters]
22+
parameters: [...compiledQuery.parameters],
23+
db: powerSync
2324
});
2425
if (signal.aborted) {
2526
return;

packages/web/tests/watch.test.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AbstractPowerSyncDatabase, WatchedQueryState } from '@powersync/common';
1+
import { AbstractPowerSyncDatabase, GetAllQuery, WatchedQueryState } from '@powersync/common';
22
import { PowerSyncDatabase } from '@powersync/web';
33
import { v4 as uuid } from 'uuid';
44
import { afterEach, beforeEach, describe, expect, it, onTestFinished, vi } from 'vitest';
@@ -328,13 +328,10 @@ describe('Watch Tests', { sequential: true }, () => {
328328
it('should stream watch results', async () => {
329329
const watch = powersync.incrementalWatch({
330330
watch: {
331-
query: {
332-
compile: () => ({
333-
sql: 'SELECT * FROM assets',
334-
parameters: []
335-
}),
336-
execute: ({ sql, parameters }) => powersync.getAll(sql, parameters)
337-
},
331+
query: new GetAllQuery({
332+
sql: 'SELECT * FROM assets',
333+
parameters: []
334+
}),
338335
placeholderData: []
339336
}
340337
});

packages/web/vitest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const config: UserConfigExport = {
5050
*/
5151
isolate: true,
5252
provider: 'playwright',
53-
headless: true,
53+
headless: false,
5454
instances: [
5555
{
5656
browser: 'chromium'

0 commit comments

Comments
 (0)