Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip primary keys from the update payload #495

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/tricky-pans-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@supabase-cache-helpers/postgrest-core": minor
"@supabase-cache-helpers/postgrest-react-query": minor
"@supabase-cache-helpers/postgrest-swr": minor
---

Strip primary keys from the update payload
21 changes: 17 additions & 4 deletions packages/postgrest-core/src/update-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export type UpdateFetcherOptions<
S extends GenericSchema,
T extends GenericTable,
Re = T extends { Relationships: infer R } ? R : unknown,
> = Parameters<PostgrestQueryBuilder<S, T, Re>['update']>[1];
> = Parameters<PostgrestQueryBuilder<S, T, Re>['update']>[1] & {
stripPrimaryKeys?: boolean;
};

export const buildUpdateFetcher =
<
Expand All @@ -35,13 +37,24 @@ export const buildUpdateFetcher =
>(
qb: PostgrestQueryBuilder<S, T, Re>,
primaryKeys: (keyof T['Row'])[],
opts: BuildNormalizedQueryOps<Q> & UpdateFetcherOptions<S, T>,
{
stripPrimaryKeys = true,
...opts
}: BuildNormalizedQueryOps<Q> & UpdateFetcherOptions<S, T>,
): UpdateFetcher<T, R> =>
async (
input: Partial<T['Row']>,
): Promise<MutationFetcherResponse<R> | null> => {
let filterBuilder = qb.update(input as any, opts); // todo fix type;

const payload = stripPrimaryKeys
? primaryKeys.reduce<typeof input>(
(acc, key) => {
delete acc[key];
return acc;
},
{ ...input },
)
: input;
let filterBuilder = qb.update(payload as any, opts); // todo fix type;
for (const key of primaryKeys) {
const value = input[key];
if (!value)
Expand Down
51 changes: 43 additions & 8 deletions packages/postgrest-core/tests/update-fetcher.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type SupabaseClient, createClient } from '@supabase/supabase-js';
import { beforeAll, describe, expect, it } from 'vitest';
import { beforeAll, describe, expect, it, vi } from 'vitest';

import type { Database } from './database.types';
import './utils';
Expand Down Expand Up @@ -41,20 +41,55 @@ describe('update', () => {
});

it('should update entity by primary keys', async () => {
const updatedContact = await buildUpdateFetcher(
client.from('contact'),
['id'],
{ queriesForTable: () => [] },
)({
const qb = client.from('contact');
const updateSpy = vi.spyOn(qb, 'update');
const username = `${testRunPrefix}-username-2`;
const updatedContact = await buildUpdateFetcher(qb, ['id'], {
stripPrimaryKeys: false,
queriesForTable: () => [],
})({
id: contact?.id,
username,
});
expect(updatedContact).toEqual({
normalizedData: {
id: expect.anything(),
username,
},
});
expect(updateSpy).toHaveBeenCalledWith(
{
id: expect.anything(),
username,
},
expect.anything(),
);
const { data } = await client
.from('contact')
.select('*')
.eq('id', contact?.id ?? '')
.throwOnError()
.maybeSingle();
expect(data?.username).toEqual(`${testRunPrefix}-username-2`);
});

it('should update entity by primary keys excluding primary keys in payload', async () => {
const qb = client.from('contact');
const updateSpy = vi.spyOn(qb, 'update');
const username = `${testRunPrefix}-username-2`;
const updatedContact = await buildUpdateFetcher(qb, ['id'], {
queriesForTable: () => [],
})({
id: contact?.id,
username: `${testRunPrefix}-username-2`,
username,
});
expect(updatedContact).toEqual({
normalizedData: {
id: expect.anything(),
username: `${testRunPrefix}-username-2`,
username,
},
});
expect(updateSpy).toHaveBeenCalledWith({ username }, expect.anything());
const { data } = await client
.from('contact')
.select('*')
Expand Down