Skip to content

Commit

Permalink
fixup! Strip primary keys from the update payload
Browse files Browse the repository at this point in the history
  • Loading branch information
elyobo committed Aug 28, 2024
1 parent fa65272 commit 63a1366
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
11 changes: 6 additions & 5 deletions packages/postgrest-core/src/update-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,18 @@ export const buildUpdateFetcher =
async (
input: Partial<T['Row']>,
): Promise<MutationFetcherResponse<R> | null> => {
const payload = { ...input };
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)
throw new Error(`Missing value for primary key ${String(key)}`);
filterBuilder = filterBuilder.eq(key as string, value);

if (stripPrimaryKeys) {
payload[key] = undefined;
}
}

const query = buildNormalizedQuery<Q>(opts);
Expand Down
43 changes: 39 additions & 4 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 qb = client.from('contact');
const updateSpy = vi.spyOn(qb, 'update');
const username = `${testRunPrefix}-username-2`;
const updatedContact = await buildUpdateFetcher(
client.from('contact'),
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

0 comments on commit 63a1366

Please sign in to comment.