From 6ac17131fa8e899cdff415179b6f0cc31dcfc117 Mon Sep 17 00:00:00 2001 From: Sandros94 Date: Wed, 11 Dec 2024 13:06:45 +0100 Subject: [PATCH 1/5] fix(Vue): nullish params --- packages/pglite-vue/src/hooks.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/pglite-vue/src/hooks.ts b/packages/pglite-vue/src/hooks.ts index 5db43b438..d51158b80 100644 --- a/packages/pglite-vue/src/hooks.ts +++ b/packages/pglite-vue/src/hooks.ts @@ -44,7 +44,7 @@ function useLiveQueryImpl( const querySource = typeof query === 'string' ? ref(query) : query const paramsSources = !params - ? [ref(params)] + ? [] : Array.isArray(params) ? params.map(ref) : [params] @@ -74,7 +74,7 @@ function useLiveQueryImpl( ? params() : Array.isArray(params) ? params.map(unref) - : [params] + : null const key = isRef(keySource) ? keySource.value : keySource?.() From 8c56b4dfad4832a98b69d59e3d13ed3fb26275b0 Mon Sep 17 00:00:00 2001 From: Sandros94 Date: Thu, 12 Dec 2024 13:08:52 +0100 Subject: [PATCH 2/5] fix(Vue): simplify live `params` manipulations --- packages/pglite-vue/src/hooks.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/pglite-vue/src/hooks.ts b/packages/pglite-vue/src/hooks.ts index d51158b80..4a4ab4095 100644 --- a/packages/pglite-vue/src/hooks.ts +++ b/packages/pglite-vue/src/hooks.ts @@ -47,7 +47,7 @@ function useLiveQueryImpl( ? [] : Array.isArray(params) ? params.map(ref) - : [params] + : [ref(params)] const keySource = typeof key === 'string' ? ref(key) : key @@ -68,13 +68,11 @@ function useLiveQueryImpl( 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) - : null + : unref(params) const key = isRef(keySource) ? keySource.value : keySource?.() From a67f576e72711a07fd1db4470aa3780a04932a5c Mon Sep 17 00:00:00 2001 From: Sam Willis Date: Mon, 13 Jan 2025 15:26:03 +0000 Subject: [PATCH 3/5] Changeset --- .changeset/weak-pigs-begin.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/weak-pigs-begin.md diff --git a/.changeset/weak-pigs-begin.md b/.changeset/weak-pigs-begin.md new file mode 100644 index 000000000..c29988598 --- /dev/null +++ b/.changeset/weak-pigs-begin.md @@ -0,0 +1,5 @@ +--- +'@electric-sql/pglite-vue': patch +--- + +Fix Vue useLiveQuery to allow no parameters to be provided From c8fce57694e5ee24f4e1fd22446ac723406bf618 Mon Sep 17 00:00:00 2001 From: Sam Willis Date: Mon, 13 Jan 2025 15:28:39 +0000 Subject: [PATCH 4/5] Formatting --- packages/pglite-vue/src/hooks.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pglite-vue/src/hooks.ts b/packages/pglite-vue/src/hooks.ts index 4a4ab4095..fa4fb71da 100644 --- a/packages/pglite-vue/src/hooks.ts +++ b/packages/pglite-vue/src/hooks.ts @@ -69,7 +69,7 @@ function useLiveQueryImpl( const query = isRef(querySource) ? unref(querySource) : querySource() const paramVals = Array.isArray(params) - ? params.map(p => typeof p === 'function' ? p() : unref(p)) + ? params.map((p) => (typeof p === 'function' ? p() : unref(p))) : typeof params === 'function' ? params() : unref(params) From 5d6d51bfcd034aaccb7dd79b6c6572fcd5ffc983 Mon Sep 17 00:00:00 2001 From: Sam Willis Date: Mon, 13 Jan 2025 17:37:48 +0000 Subject: [PATCH 5/5] Add test --- packages/pglite-vue/test/hooks.test.ts | 29 ++++++++++++++++++++++++++ packages/pglite-vue/vitest.config.ts | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/pglite-vue/test/hooks.test.ts b/packages/pglite-vue/test/hooks.test.ts index d1debcfec..6abf3c79d 100644 --- a/packages/pglite-vue/test/hooks.test.ts +++ b/packages/pglite-vue/test/hooks.test.ts @@ -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');`) diff --git a/packages/pglite-vue/vitest.config.ts b/packages/pglite-vue/vitest.config.ts index e41124086..39360a47a 100644 --- a/packages/pglite-vue/vitest.config.ts +++ b/packages/pglite-vue/vitest.config.ts @@ -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',