Skip to content

Commit 7816d88

Browse files
committed
Vue queries will recalculate dependent tables on schema change.
1 parent ab34416 commit 7816d88

File tree

3 files changed

+48
-38
lines changed

3 files changed

+48
-38
lines changed

.changeset/ten-birds-camp.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/vue': patch
3+
---
4+
5+
Queries will recalculate dependent tables if schema is updated.

packages/vue/src/composables/useQuery.ts

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { type CompilableQuery, ParsedQuery, type SQLWatchOptions, parseQuery } from '@powersync/common';
1+
import {
2+
type CompilableQuery,
3+
ParsedQuery,
4+
type SQLWatchOptions,
5+
parseQuery,
6+
runOnSchemaChange
7+
} from '@powersync/common';
28
import { type MaybeRef, type Ref, ref, toValue, watchEffect } from 'vue';
39
import { usePowerSync } from './powerSync';
410

@@ -114,38 +120,41 @@ export const useQuery = <T = any>(
114120
}
115121

116122
const { sqlStatement: sql, parameters } = parsedQuery;
123+
const watchQuery = async (abortSignal: AbortSignal) => {
124+
let resolvedTables = [];
125+
try {
126+
resolvedTables = await powerSync.value.resolveTables(sql, parameters, options);
127+
} catch (e) {
128+
console.error('Failed to fetch tables:', e);
129+
handleError(e);
130+
return;
131+
}
132+
// Fetch initial data
133+
const executor =
134+
typeof queryValue == 'string' ? () => powerSync.value.getAll<T>(sql, parameters) : () => queryValue.execute();
135+
fetchData = () => _fetchData(executor);
136+
await fetchData();
137+
138+
if (options.runQueryOnce) {
139+
return;
140+
}
117141

118-
let resolvedTables = [];
119-
try {
120-
resolvedTables = await powerSync.value.resolveTables(sql, parameters, options);
121-
} catch (e) {
122-
console.error('Failed to fetch tables:', e);
123-
handleError(e);
124-
return;
125-
}
126-
// Fetch initial data
127-
const executor =
128-
typeof queryValue == 'string' ? () => powerSync.value.getAll<T>(sql, parameters) : () => queryValue.execute();
129-
fetchData = () => _fetchData(executor);
130-
await fetchData();
131-
132-
if (options.runQueryOnce) {
133-
return;
134-
}
135-
136-
powerSync.value.onChangeWithCallback(
137-
{
138-
onChange: async () => {
139-
await fetchData();
142+
powerSync.value.onChangeWithCallback(
143+
{
144+
onChange: async () => {
145+
await fetchData();
146+
},
147+
onError: handleError
140148
},
141-
onError: handleError
142-
},
143-
{
144-
...options,
145-
signal: abortController.signal,
146-
tables: resolvedTables
147-
}
148-
);
149+
{
150+
...options,
151+
signal: abortSignal,
152+
tables: resolvedTables
153+
}
154+
);
155+
};
156+
157+
runOnSchemaChange(watchQuery, powerSync.value, { signal: abortController.signal });
149158
});
150159

151160
return {

packages/vue/tests/useQuery.test.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import { withSetup } from './utils';
77

88
const mockPowerSync = {
99
currentStatus: { status: 'initial' },
10-
registerListener: vi.fn(() => ({
11-
statusChanged: vi.fn(() => 'updated')
12-
})),
10+
registerListener: vi.fn(() => {}),
1311
resolveTables: vi.fn(),
1412
watch: vi.fn(),
1513
onChangeWithCallback: vi.fn(),
@@ -33,13 +31,11 @@ describe('useQuery', () => {
3331
});
3432

3533
it('should handle error in watchEffect', async () => {
36-
vi.spyOn(PowerSync, 'usePowerSync').mockReturnValue(ref({}) as any);
34+
vi.spyOn(PowerSync, 'usePowerSync').mockReturnValue(undefined);
3735

3836
const [{ data, isLoading, isFetching, error }] = withSetup(() => useQuery('SELECT * from lists'));
3937

40-
expect(error.value).toEqual(
41-
Error('PowerSync failed to fetch data: powerSync.value.resolveTables is not a function')
42-
);
38+
expect(error.value).toEqual(Error('PowerSync not configured.'));
4339
expect(isFetching.value).toEqual(false);
4440
expect(isLoading.value).toEqual(false);
4541
expect(data.value).toEqual([]);

0 commit comments

Comments
 (0)