Skip to content

Commit

Permalink
fix: use schema of table when not explicitly set in revalidateTables …
Browse files Browse the repository at this point in the history
…and revalidateRelations
  • Loading branch information
psteinroe committed Jul 13, 2023
1 parent e9a18c4 commit 7b57743
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-phones-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@supabase-cache-helpers/postgrest-mutate": patch
---

fix: use schema of table when not explicitly set in revalidateTables and revalidateRelations
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:

- uses: supabase/setup-cli@v1
with:
version: latest
version: 1.77.4

- name: Start Supabase
run: supabase start
Expand Down
71 changes: 70 additions & 1 deletion packages/postgrest-mutate/__tests__/lib/mutate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type MockMutateProps = {
decode: DecodedKey | null;
input?: ItemType;
opts?: PostgrestMutatorOpts<ItemType>;
schema?: string;
};

const mockMutate = async ({
Expand All @@ -36,12 +37,13 @@ const mockMutate = async ({
decode,
opts,
input = { id: '0', value: 'test', fkey: 'fkey' },
schema = 'schema',
}: MockMutateProps) => {
const mutateMockFn = jest.fn();
await mutate(
{
input: input as ItemType,
schema: 'schema',
schema,
table: 'table',
type,
primaryKeys: ['id'],
Expand Down Expand Up @@ -323,6 +325,43 @@ describe('mutate', () => {
expect(mutateMock).toHaveBeenCalledWith('1');
});

it('should use same schema as table if none is set on revalidateRelations', async () => {
const mutateMock = await mockMutate({
type: 'UPSERT',
postgrestFilter: {
apply: true,
applyFilters: true,
hasPaths: true,

hasFiltersOnPaths: true,
applyFiltersOnPaths: true,
},
schema: 'public',
decode: {
queryKey: 'queryKey',
table: 'relation',
bodyKey: undefined,
count: null,
orderByKey: '',
isHead: false,
schema: 'schema',
limit: undefined,
offset: undefined,
},
opts: {
revalidateRelations: [
{
relation: 'relation',
fKeyColumn: 'fkey',
relationIdColumn: 'id',
},
],
},
});
expect(mutateMock).toHaveBeenCalledTimes(1);
expect(mutateMock).toHaveBeenCalledWith('1');
});

it('should set tables defined in revalidateTables to stale', async () => {
const mutateMock = await mockMutate({
type: 'UPSERT',
Expand Down Expand Up @@ -352,4 +391,34 @@ describe('mutate', () => {
expect(mutateMock).toHaveBeenCalledTimes(1);
expect(mutateMock).toHaveBeenCalledWith('1');
});

it('should use same schema as table if none is defined in revalidateTables', async () => {
const mutateMock = await mockMutate({
type: 'UPSERT',
postgrestFilter: {
apply: true,
applyFilters: true,
hasPaths: true,
hasFiltersOnPaths: true,
applyFiltersOnPaths: true,
},
schema: 'public',
decode: {
queryKey: 'queryKey',
table: 'relation',
bodyKey: undefined,
count: null,
isHead: false,
schema: 'schema',
limit: undefined,
orderByKey: '',
offset: undefined,
},
opts: {
revalidateTables: [{ table: 'relation' }],
},
});
expect(mutateMock).toHaveBeenCalledTimes(1);
expect(mutateMock).toHaveBeenCalledWith('1');
});
});
4 changes: 2 additions & 2 deletions packages/postgrest-mutate/src/lib/mutate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export const mutate = async <KeyType, Type extends Record<string, unknown>>(

for (const r of opts?.revalidateRelations ?? []) {
if (
r.schema === key.schema &&
(!r.schema || r.schema === key.schema) &&
r.relation === key.table &&
getPostgrestFilter(key.queryKey, {
exclusivePaths: [r.relationIdColumn],
Expand All @@ -136,7 +136,7 @@ export const mutate = async <KeyType, Type extends Record<string, unknown>>(

if (
opts?.revalidateTables?.find(
(t) => t.schema === key.schema && t.table === key.table
(t) => (!t.schema || t.schema === key.schema) && t.table === key.table
)
) {
mutations.push(mutate(k));
Expand Down

0 comments on commit 7b57743

Please sign in to comment.