From ecbcbd3e1ab3eec24f7105db52f10ecb467c7037 Mon Sep 17 00:00:00 2001 From: psteinroe Date: Wed, 14 Feb 2024 11:20:26 +0100 Subject: [PATCH 1/3] fix: return input from mutations if no query is passed this enables cache updates, especially revalidateTables, without having to pass a user query when there is no other query for the same table currently in the cache --- .changeset/strong-sheep-laugh.md | 5 +++++ packages/postgrest-core/__tests__/insert-fetcher.spec.ts | 9 ++++++++- packages/postgrest-core/__tests__/update-fetcher.spec.ts | 4 +++- packages/postgrest-core/__tests__/upsert-fetcher.spec.ts | 9 ++++++++- packages/postgrest-core/src/insert-fetcher.ts | 2 +- packages/postgrest-core/src/update-fetcher.ts | 2 +- packages/postgrest-core/src/upsert-fetcher.ts | 4 +++- .../mutate/use-update-mutation.integration.spec.tsx | 2 +- 8 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 .changeset/strong-sheep-laugh.md diff --git a/.changeset/strong-sheep-laugh.md b/.changeset/strong-sheep-laugh.md new file mode 100644 index 00000000..4d3c7bc5 --- /dev/null +++ b/.changeset/strong-sheep-laugh.md @@ -0,0 +1,5 @@ +--- +"@supabase-cache-helpers/postgrest-core": patch +--- + +fix: return input from mutations without query so that cache updates still work diff --git a/packages/postgrest-core/__tests__/insert-fetcher.spec.ts b/packages/postgrest-core/__tests__/insert-fetcher.spec.ts index db461fdb..84d65fba 100644 --- a/packages/postgrest-core/__tests__/insert-fetcher.spec.ts +++ b/packages/postgrest-core/__tests__/insert-fetcher.spec.ts @@ -76,7 +76,14 @@ describe('insert', () => { { username: `${testRunPrefix}-username-1` }, { username: `${testRunPrefix}-username-2` }, ]), - ).resolves.toEqual(null); + ).resolves.toEqual([ + { + normalizedData: { username: `${testRunPrefix}-username-1` }, + }, + { + normalizedData: { username: `${testRunPrefix}-username-2` }, + }, + ]); }); it('should support passing a query', async () => { diff --git a/packages/postgrest-core/__tests__/update-fetcher.spec.ts b/packages/postgrest-core/__tests__/update-fetcher.spec.ts index 2e38a48f..265dab2a 100644 --- a/packages/postgrest-core/__tests__/update-fetcher.spec.ts +++ b/packages/postgrest-core/__tests__/update-fetcher.spec.ts @@ -47,7 +47,9 @@ describe('update', () => { id: contact?.id, username: `${testRunPrefix}-username-2`, }); - expect(updatedContact).toEqual(null); + expect(updatedContact).toEqual({ + normalizedData: { username: 'postgrest-fetcher-insert-94-username-2' }, + }); const { data } = await client .from('contact') .select('*') diff --git a/packages/postgrest-core/__tests__/upsert-fetcher.spec.ts b/packages/postgrest-core/__tests__/upsert-fetcher.spec.ts index 137b38fa..aecfb4e2 100644 --- a/packages/postgrest-core/__tests__/upsert-fetcher.spec.ts +++ b/packages/postgrest-core/__tests__/upsert-fetcher.spec.ts @@ -27,7 +27,14 @@ describe('upsert', () => { { username: `${testRunPrefix}-username-2` }, ], ), - ).resolves.toEqual(null); + ).resolves.toEqual([ + { + normalizedData: { username: `${testRunPrefix}-username-1` }, + }, + { + normalizedData: { username: `${testRunPrefix}-username-2` }, + }, + ]); }); it('should support passing a query', async () => { diff --git a/packages/postgrest-core/src/insert-fetcher.ts b/packages/postgrest-core/src/insert-fetcher.ts index 4a098032..9ffe305b 100644 --- a/packages/postgrest-core/src/insert-fetcher.ts +++ b/packages/postgrest-core/src/insert-fetcher.ts @@ -49,7 +49,7 @@ function buildInsertFetcher< ); } await qb.insert(input as any).throwOnError(); - return null; + return input.map((d) => ({ normalizedData: d as R })); }; } diff --git a/packages/postgrest-core/src/update-fetcher.ts b/packages/postgrest-core/src/update-fetcher.ts index c12c2154..9f3a8c4b 100644 --- a/packages/postgrest-core/src/update-fetcher.ts +++ b/packages/postgrest-core/src/update-fetcher.ts @@ -57,5 +57,5 @@ export const buildUpdateFetcher = return buildMutationFetcherResponse(data as R, { userQueryPaths, paths }); } await filterBuilder.throwOnError().single(); - return null; + return { normalizedData: input as R }; }; diff --git a/packages/postgrest-core/src/upsert-fetcher.ts b/packages/postgrest-core/src/upsert-fetcher.ts index d3f61629..e093ad77 100644 --- a/packages/postgrest-core/src/upsert-fetcher.ts +++ b/packages/postgrest-core/src/upsert-fetcher.ts @@ -51,5 +51,7 @@ export const buildUpsertFetcher = await qb .upsert(input as any) // todo fix type .throwOnError(); - return null; + return input.map((d) => ({ + normalizedData: d as R, + })); }; diff --git a/packages/postgrest-swr/__tests__/mutate/use-update-mutation.integration.spec.tsx b/packages/postgrest-swr/__tests__/mutate/use-update-mutation.integration.spec.tsx index 73e3a29d..685a755b 100644 --- a/packages/postgrest-swr/__tests__/mutate/use-update-mutation.integration.spec.tsx +++ b/packages/postgrest-swr/__tests__/mutate/use-update-mutation.integration.spec.tsx @@ -194,7 +194,7 @@ describe('useUpdateMutation', () => { const { trigger: update } = useUpdateMutation( client.from('contact'), ['id'], - 'id', + null, { revalidateTables: [{ schema: 'public', table: 'contact_note' }], onSuccess: () => setSuccess(true), From ff3903a5f28e871d1bb3e044521470dd9bfa7a7c Mon Sep 17 00:00:00 2001 From: psteinroe Date: Wed, 14 Feb 2024 11:27:20 +0100 Subject: [PATCH 2/3] fix: test --- packages/postgrest-core/__tests__/update-fetcher.spec.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/postgrest-core/__tests__/update-fetcher.spec.ts b/packages/postgrest-core/__tests__/update-fetcher.spec.ts index 265dab2a..f69e8797 100644 --- a/packages/postgrest-core/__tests__/update-fetcher.spec.ts +++ b/packages/postgrest-core/__tests__/update-fetcher.spec.ts @@ -48,7 +48,10 @@ describe('update', () => { username: `${testRunPrefix}-username-2`, }); expect(updatedContact).toEqual({ - normalizedData: { username: 'postgrest-fetcher-insert-94-username-2' }, + normalizedData: { + id: expect.anything(), + username: 'postgrest-fetcher-insert-94-username-2', + }, }); const { data } = await client .from('contact') From 1a011d7e347d7ce5c1a3ca00cc5709110fe74dff Mon Sep 17 00:00:00 2001 From: psteinroe Date: Wed, 14 Feb 2024 11:55:48 +0100 Subject: [PATCH 3/3] fix: test --- packages/postgrest-core/__tests__/update-fetcher.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/postgrest-core/__tests__/update-fetcher.spec.ts b/packages/postgrest-core/__tests__/update-fetcher.spec.ts index f69e8797..d73e2ddf 100644 --- a/packages/postgrest-core/__tests__/update-fetcher.spec.ts +++ b/packages/postgrest-core/__tests__/update-fetcher.spec.ts @@ -50,7 +50,7 @@ describe('update', () => { expect(updatedContact).toEqual({ normalizedData: { id: expect.anything(), - username: 'postgrest-fetcher-insert-94-username-2', + username: `${testRunPrefix}-username-2`, }, }); const { data } = await client