Skip to content

Commit 7ce9f04

Browse files
sandros94samwillis
andauthored
fix(Vue): nullish params in useLiveQueryImpl (#466)
* fix(Vue): nullish params * fix(Vue): simplify live `params` manipulations * Changeset * Formatting * Add test --------- Co-authored-by: Sam Willis <[email protected]>
1 parent e037883 commit 7ce9f04

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

.changeset/weak-pigs-begin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@electric-sql/pglite-vue': patch
3+
---
4+
5+
Fix Vue useLiveQuery to allow no parameters to be provided

packages/pglite-vue/src/hooks.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ function useLiveQueryImpl<T = { [key: string]: unknown }>(
4444

4545
const querySource = typeof query === 'string' ? ref(query) : query
4646
const paramsSources = !params
47-
? [ref(params)]
47+
? []
4848
: Array.isArray(params)
4949
? params.map(ref)
50-
: [params]
50+
: [ref(params)]
5151

5252
const keySource = typeof key === 'string' ? ref(key) : key
5353

@@ -68,13 +68,11 @@ function useLiveQueryImpl<T = { [key: string]: unknown }>(
6868

6969
const query = isRef(querySource) ? unref(querySource) : querySource()
7070

71-
const paramVals = isRef(params)
72-
? unref(params)
71+
const paramVals = Array.isArray(params)
72+
? params.map((p) => (typeof p === 'function' ? p() : unref(p)))
7373
: typeof params === 'function'
7474
? params()
75-
: Array.isArray(params)
76-
? params.map(unref)
77-
: [params]
75+
: unref(params)
7876

7977
const key = isRef(keySource) ? keySource.value : keySource?.()
8078

packages/pglite-vue/test/hooks.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,35 @@ describe('hooks', () => {
4343
`)
4444
})
4545

46+
it('updates when query without parameters is provided', async () => {
47+
const { useLiveQuery } = await import('../src')
48+
await db.exec(`INSERT INTO test (name) VALUES ('test1');`)
49+
50+
const result = useLiveQuery('SELECT * FROM test;')
51+
52+
await flushPromises()
53+
expect(result?.rows?.value).toEqual([
54+
{
55+
id: 1,
56+
name: 'test1',
57+
},
58+
])
59+
60+
await db.exec(`INSERT INTO test (name) VALUES ('test2');`)
61+
62+
await flushPromises()
63+
expect(result?.rows?.value).toEqual([
64+
{
65+
id: 1,
66+
name: 'test1',
67+
},
68+
{
69+
id: 2,
70+
name: 'test2',
71+
},
72+
])
73+
})
74+
4675
it('updates when query parameter ref changes', async () => {
4776
const { useLiveQuery } = await import('../src')
4877
await db.exec(`INSERT INTO test (name) VALUES ('test1'),('test2');`)

packages/pglite-vue/vitest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default defineConfig({
55
// @ts-ignore type mismsatch but works?
66
plugins: [vue()],
77
test: {
8-
name: 'pglite-react',
8+
name: 'pglite-vue',
99
dir: './test',
1010
watch: false,
1111
environment: 'jsdom',

0 commit comments

Comments
 (0)