Skip to content

Commit e6502c4

Browse files
authored
Merge pull request #518 from damien-schneider/replace-falsy-check-on-primary-key-values
2 parents 3ce09a8 + e9f49c9 commit e6502c4

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

.changeset/six-timers-turn.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@supabase-cache-helpers/postgrest-core": patch
3+
---
4+
5+
fix update fetcher when primary key value is equal to false or 0

packages/postgrest-core/src/update-fetcher.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ export const buildUpdateFetcher =
5757
let filterBuilder = qb.update(payload as any, opts); // todo fix type;
5858
for (const key of primaryKeys) {
5959
const value = input[key];
60-
if (!value)
60+
// The value can be 0 or false, so we need to check if it's null or undefined instead of falsy
61+
if (value === null || value === undefined)
6162
throw new Error(`Missing value for primary key ${String(key)}`);
6263
filterBuilder = filterBuilder.eq(key as string, value);
6364
}

packages/postgrest-core/tests/update-fetcher.spec.ts

+36
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,40 @@ describe('update', () => {
147147
.maybeSingle();
148148
expect(data?.username).toEqual(`${testRunPrefix}-username-4`);
149149
});
150+
151+
it('should not throw error when primary key value is false', async () => {
152+
const qb = {
153+
update: vi.fn().mockReturnThis(),
154+
eq: vi.fn().mockReturnThis(),
155+
select: vi.fn().mockReturnThis(),
156+
throwOnError: vi.fn().mockReturnThis(),
157+
single: vi.fn().mockResolvedValue({ data: null }),
158+
};
159+
160+
await expect(
161+
buildUpdateFetcher(qb as any, ['is_active'], {
162+
stripPrimaryKeys: false,
163+
queriesForTable: () => [],
164+
})({ is_active: false, username: 'testuser' }),
165+
).resolves.not.toThrow();
166+
expect(qb.eq).toHaveBeenCalledWith('is_active', false);
167+
});
168+
169+
it('should not throw error when primary key value is 0', async () => {
170+
const qb = {
171+
update: vi.fn().mockReturnThis(),
172+
eq: vi.fn().mockReturnThis(),
173+
select: vi.fn().mockReturnThis(),
174+
throwOnError: vi.fn().mockReturnThis(),
175+
single: vi.fn().mockResolvedValue({ data: null }),
176+
};
177+
178+
await expect(
179+
buildUpdateFetcher(qb as any, ['id'], {
180+
stripPrimaryKeys: false,
181+
queriesForTable: () => [],
182+
})({ id: 0, username: 'testuser' }),
183+
).resolves.not.toThrow();
184+
expect(qb.eq).toHaveBeenCalledWith('id', 0);
185+
});
150186
});

0 commit comments

Comments
 (0)