From f7809b3406a200ced1adf9879e86fc807dd42cd8 Mon Sep 17 00:00:00 2001 From: Sam Willis Date: Wed, 7 Aug 2024 14:57:01 +0100 Subject: [PATCH] Address feedback --- packages/pglite-react/test/react.test.tsx | 2 +- packages/pglite/src/live/index.ts | 4 ++-- packages/pglite/src/utils.ts | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/pglite-react/test/react.test.tsx b/packages/pglite-react/test/react.test.tsx index 9b7af026..c2f2ee43 100644 --- a/packages/pglite-react/test/react.test.tsx +++ b/packages/pglite-react/test/react.test.tsx @@ -186,7 +186,7 @@ function testLiveQuery(queryHook: "useLiveQuery" | "useLiveIncrementalQuery") { await waitFor(() => expect(result.current?.rows).toHaveLength(1)); }); - it.skip("updates when query parameter changes", async () => { + it("updates when query parameter changes", async () => { await db.exec(`INSERT INTO test (name) VALUES ('test1'),('test2');`); const { result, rerender } = renderHook( diff --git a/packages/pglite/src/live/index.ts b/packages/pglite/src/live/index.ts index 4d215734..47c4b472 100644 --- a/packages/pglite/src/live/index.ts +++ b/packages/pglite/src/live/index.ts @@ -33,7 +33,7 @@ const setup = async (pg: PGliteInterface, emscriptenOpts: any) => { const init = async () => { await pg.transaction(async (tx) => { // Create a temporary view with the query - const formattedQuery = await formatQuery(tx, query, params || []); + const formattedQuery = await formatQuery(tx, query, params); await tx.query( `CREATE OR REPLACE TEMP VIEW live_query_${id}_view AS ${formattedQuery}`, ); @@ -124,7 +124,7 @@ const setup = async (pg: PGliteInterface, emscriptenOpts: any) => { const init = async () => { await pg.transaction(async (tx) => { // Create a temporary view with the query - const formattedQuery = await formatQuery(tx, query, params || []); + const formattedQuery = await formatQuery(tx, query, params); await tx.query( `CREATE OR REPLACE TEMP VIEW live_query_${id}_view AS ${formattedQuery}`, ); diff --git a/packages/pglite/src/utils.ts b/packages/pglite/src/utils.ts index 57abc2a3..03a9db23 100644 --- a/packages/pglite/src/utils.ts +++ b/packages/pglite/src/utils.ts @@ -74,7 +74,7 @@ export const uuid = (): string => { export async function formatQuery( pg: PGliteInterface | Transaction, query: string, - params?: any[], + params?: any[] | null, ) { if (!params || params.length === 0) { // no params so no formatting needed @@ -82,7 +82,7 @@ export async function formatQuery( } // replace $1, $2, etc with %1L, %2L, etc - query = query.replace(/\$([0-9]+)/g, (_, num) => { + const subbedQuery = query.replace(/\$([0-9]+)/g, (_, num) => { return "%" + num + "L"; }); @@ -90,7 +90,7 @@ export async function formatQuery( query: string; }>( `SELECT format($1, ${params.map((_, i) => `$${i + 2}`).join(", ")}) as query`, - [query, ...params], + [subbedQuery, ...params], { setAllTypes: true, },