Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Vue): nullish params in useLiveQueryImpl #466

Merged
merged 5 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/weak-pigs-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@electric-sql/pglite-vue': patch
---

Fix Vue useLiveQuery to allow no parameters to be provided
12 changes: 5 additions & 7 deletions packages/pglite-vue/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ function useLiveQueryImpl<T = { [key: string]: unknown }>(

const querySource = typeof query === 'string' ? ref(query) : query
const paramsSources = !params
? [ref(params)]
? []
: Array.isArray(params)
? params.map(ref)
: [params]
: [ref(params)]

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

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

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

const paramVals = isRef(params)
? unref(params)
const paramVals = Array.isArray(params)
? params.map((p) => (typeof p === 'function' ? p() : unref(p)))
: typeof params === 'function'
? params()
: Array.isArray(params)
? params.map(unref)
: [params]
: unref(params)

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

Expand Down
29 changes: 29 additions & 0 deletions packages/pglite-vue/test/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,35 @@ describe('hooks', () => {
`)
})

it('updates when query without parameters is provided', async () => {
const { useLiveQuery } = await import('../src')
await db.exec(`INSERT INTO test (name) VALUES ('test1');`)

const result = useLiveQuery('SELECT * FROM test;')

await flushPromises()
expect(result?.rows?.value).toEqual([
{
id: 1,
name: 'test1',
},
])

await db.exec(`INSERT INTO test (name) VALUES ('test2');`)

await flushPromises()
expect(result?.rows?.value).toEqual([
{
id: 1,
name: 'test1',
},
{
id: 2,
name: 'test2',
},
])
})

it('updates when query parameter ref changes', async () => {
const { useLiveQuery } = await import('../src')
await db.exec(`INSERT INTO test (name) VALUES ('test1'),('test2');`)
Expand Down
2 changes: 1 addition & 1 deletion packages/pglite-vue/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default defineConfig({
// @ts-ignore type mismsatch but works?
plugins: [vue()],
test: {
name: 'pglite-react',
name: 'pglite-vue',
dir: './test',
watch: false,
environment: 'jsdom',
Expand Down