Skip to content

Commit 86cfa71

Browse files
authored
[Fix] Vue useQuery comlink error caused when using reactive sql parameters (#219)
1 parent 9842df6 commit 86cfa71

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

.changeset/short-snails-visit.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/vue': patch
3+
---
4+
5+
Fixed comlink issue caused when using reactive sql parameters.

packages/vue/src/composables/useQuery.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export const useQuery = <T = any>(
105105

106106
let parsedQuery: ParsedQuery;
107107
try {
108-
parsedQuery = parseQuery(toValue(query), toValue(sqlParameters));
108+
parsedQuery = parseQuery(toValue(query), toValue(sqlParameters).map(toValue));
109109
} catch (e) {
110110
console.error('Failed to parse query:', e);
111111
handleError(e);

packages/vue/tests/useQuery.test.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import flushPromises from 'flush-promises';
22
import { afterEach, describe, expect, it, vi } from 'vitest';
3-
import { ref } from 'vue';
3+
import { isProxy, isRef, ref } from 'vue';
44
import * as PowerSync from '../src/composables/powerSync';
55
import { useQuery } from '../src/composables/useQuery';
66
import { withSetup } from './utils';
@@ -61,6 +61,21 @@ describe('useQuery', () => {
6161
expect(error.value).toEqual(undefined);
6262
});
6363

64+
// ensure that Proxy wrapper object is stripped
65+
it('should propagate raw reactive sql parameters', async () => {
66+
vi.spyOn(PowerSync, 'usePowerSync').mockReturnValue(ref(mockPowerSync) as any);
67+
const getAllSpy = mockPowerSync.getAll;
68+
69+
const [{ data, isLoading, isFetching, error }] = withSetup(() =>
70+
useQuery('SELECT * from lists where id = $1', ref([ref('test')]))
71+
);
72+
await flushPromises();
73+
expect(getAllSpy).toHaveBeenCalledTimes(1);
74+
const sqlParam = (getAllSpy.mock.calls[0] as Array<any>)[1];
75+
expect(isRef(sqlParam)).toEqual(false);
76+
expect(isProxy(sqlParam)).toEqual(false);
77+
});
78+
6479
it('should rerun the query when refresh is used', async () => {
6580
vi.spyOn(PowerSync, 'usePowerSync').mockReturnValue(ref(mockPowerSync) as any);
6681
const getAllSpy = mockPowerSync.getAll;
@@ -132,7 +147,7 @@ describe('useQuery', () => {
132147
it('should set error for compilable query on useQuery parameters', async () => {
133148
vi.spyOn(PowerSync, 'usePowerSync').mockReturnValue(ref(mockPowerSync) as any);
134149

135-
const [{ isLoading, error }] = withSetup(() =>
150+
const [{ error }] = withSetup(() =>
136151
useQuery({ execute: () => [] as any, compile: () => ({ sql: 'SELECT * from lists', parameters: [] }) }, ['x'])
137152
);
138153

0 commit comments

Comments
 (0)