Skip to content

Commit

Permalink
fix: make delete work on single query
Browse files Browse the repository at this point in the history
  • Loading branch information
psteinroe committed Nov 8, 2024
1 parent 0df96e2 commit b06405b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/cuddly-islands-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@supabase-cache-helpers/postgrest-core": patch
---

fix: make delete work on single query
11 changes: 10 additions & 1 deletion packages/postgrest-core/src/delete-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,16 @@ export const deleteItem = async <KeyType, Type extends Record<string, unknown>>(
} else if (isAnyPostgrestResponse<Type>(currentData)) {
const { data } = currentData;
if (!Array.isArray(data)) {
return { data: null };
if (
data &&
op.primaryKeys.some(
(pk) => transformedInput[pk] !== data[pk],
)
) {
return currentData;
} else {
return { data: null };
}
}

const newData = filterByPks(
Expand Down
12 changes: 12 additions & 0 deletions packages/postgrest-core/tests/delete-item.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,18 @@ describe('deleteItem', () => {
});
});

it('should not change data if its single and primary keys do not match', async () => {
expect(
await mutateFnResult(
{ id_1: '0', id_2: '1', value: 'a' },
{},
{ data: { id_1: '0', id_2: '0', value: 'test' } },
),
).toMatchObject({
data: { id_1: '0', id_2: '0', value: 'test' },
});
});

it('should delete item from cached array and subtract count', async () => {
expect(
await mutateFnResult(
Expand Down

0 comments on commit b06405b

Please sign in to comment.