Skip to content

Commit

Permalink
Merge pull request #392 from psteinroe/fix/revalidate-relation
Browse files Browse the repository at this point in the history
fixes #390
  • Loading branch information
psteinroe authored Feb 29, 2024
2 parents 4f191af + b3915e4 commit 6c84008
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 287 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-owls-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@supabase-cache-helpers/postgrest-core": patch
---

fix: revalidate relation now uses the input instead of transformedInput, and delete fetcher properly builds the query and returns the parsed results
1 change: 1 addition & 0 deletions examples/react-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@supabase/auth-helpers-nextjs": "0.8.7",
"@supabase/auth-helpers-react": "0.4.2",
"@tanstack/react-query": "^5.0.0",
"@supabase/supabase-js": "2.38.5",
"@vercel/analytics": "^0.1.11",
"class-variance-authority": "0.7.0",
"clsx": "^1.2.1",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
"ci:release": "changeset publish"
},
"devDependencies": {
"@changesets/cli": "2.27.0",
"eslint": "8.54.0",
"prettier": "3.2.0",
"@changesets/cli": "2.27.0",
"supabase": "^1.145.4",
"turbo": "1.10.16"
},
"engines": {
Expand Down
4 changes: 2 additions & 2 deletions packages/postgrest-core/__tests__/delete-fetcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ describe('delete', () => {
]);
expect(result).toEqual([
{
normalizedData: { id: contact?.id },
userQueryData: { id: contact?.id, ticket_number: 1234 },
normalizedData: { id: contact?.id, ticket_number: 1234 },
userQueryData: { ticket_number: 1234 },
},
]);
});
Expand Down
47 changes: 30 additions & 17 deletions packages/postgrest-core/src/delete-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ import {
GenericTable,
} from '@supabase/postgrest-js/dist/module/types';

import { MutationFetcherResponse } from './fetch/build-mutation-fetcher-response';
import { BuildNormalizedQueryOps } from './fetch/build-normalized-query';
import { parseSelectParam } from './lib/parse-select-param';
import {
MutationFetcherResponse,
buildMutationFetcherResponse,
} from './fetch/build-mutation-fetcher-response';
import {
BuildNormalizedQueryOps,
buildNormalizedQuery,
} from './fetch/build-normalized-query';

export type DeleteFetcher<T extends GenericTable, R> = (
input: Partial<T['Row']>[],
Expand Down Expand Up @@ -79,22 +84,30 @@ export const buildDeleteFetcher =
}, {} as R),
);

if (!opts.disabled && opts.query) {
// make sure query returns the primary keys
const paths = parseSelectParam(opts.query);
const addKeys = primaryKeys.filter(
(key) => !paths.find((p) => p.path === key),
);
const query = buildNormalizedQuery<Q>(opts);
if (query) {
const { selectQuery, userQueryPaths, paths } = query;
// make sure that primary keys are included in the select query
const pathsWithPrimaryKeys = paths;
const addKeys: string[] = [];
primaryKeys.forEach((key) => {
if (!pathsWithPrimaryKeys.find((p) => p.path === key)) {
pathsWithPrimaryKeys.push({
declaration: key as string,
path: key as string,
});
addKeys.push(key as string);
}
});
const { data } = await filterBuilder
.select([opts.query, ...addKeys].join(','))
.select([selectQuery, ...addKeys].join(','))
.throwOnError();
return primaryKeysData.map<MutationFetcherResponse<R>>((pk) => ({
// since we are deleting, only the primary keys are required
normalizedData: pk,
userQueryData: ((data as R[]) ?? []).find((d) =>
primaryKeys.every((k) => d[k as keyof R] === pk[k as keyof R]),
),
}));
return (data as R[]).map((d) =>
buildMutationFetcherResponse(d, {
paths: pathsWithPrimaryKeys,
userQueryPaths,
}),
);
}

await filterBuilder.throwOnError();
Expand Down
4 changes: 2 additions & 2 deletions packages/postgrest-core/src/delete-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ export const deleteItem = async <KeyType, Type extends Record<string, unknown>>(
if (!key) continue;
const filter = getPostgrestFilter(key.queryKey);
// parse input into expected target format
const transformedInput = filter.denormalize(op.input);
if (key.schema === schema && key.table === table) {
const transformedInput = filter.denormalize(op.input);
if (
// For delete, the input has to have a value for all primary keys
op.primaryKeys.every(
Expand Down Expand Up @@ -142,7 +142,7 @@ export const deleteItem = async <KeyType, Type extends Record<string, unknown>>(
if (
revalidateRelationsOpt &&
shouldRevalidateRelation(revalidateRelationsOpt, {
input: transformedInput,
input: op.input,
getPostgrestFilter,
decodedKey: key,
})
Expand Down
4 changes: 2 additions & 2 deletions packages/postgrest-core/src/upsert-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ export const upsertItem = async <KeyType, Type extends Record<string, unknown>>(
if (!key) continue;
const filter = getPostgrestFilter(key.queryKey);
// parse input into expected target format
const transformedInput = filter.denormalize(op.input);
if (key.schema === schema && key.table === table) {
const transformedInput = filter.denormalize(op.input);
if (
filter.applyFilters(transformedInput) ||
// also allow upsert if either the filter does not apply eq filters on any pk
Expand Down Expand Up @@ -220,7 +220,7 @@ export const upsertItem = async <KeyType, Type extends Record<string, unknown>>(
if (
revalidateRelationsOpt &&
shouldRevalidateRelation(revalidateRelationsOpt, {
input: transformedInput,
input: op.input,
getPostgrestFilter,
decodedKey: key,
})
Expand Down
Loading

0 comments on commit 6c84008

Please sign in to comment.