Skip to content
Open
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
11 changes: 2 additions & 9 deletions packages/core/postgrest-js/src/PostgrestBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,8 @@ export default abstract class PostgrestBuilder<
count = parseInt(contentRange[1])
}

// Temporary partial fix for https://github.com/supabase/postgrest-js/issues/361
// Issue persists e.g. for `.insert([...]).select().maybeSingle()`
if (this.isMaybeSingle && this.method === 'GET' && Array.isArray(data)) {
// Fix for https://github.com/supabase/postgrest-js/issues/361 — applies to all methods.
if (this.isMaybeSingle && Array.isArray(data)) {
if (data.length > 1) {
error = {
// https://github.com/PostgREST/postgrest/blob/a867d79c42419af16c18c3fb019eba8df992626f/src/PostgREST/Error.hs#L553
Expand Down Expand Up @@ -222,12 +221,6 @@ export default abstract class PostgrestBuilder<
}
}

if (error && this.isMaybeSingle && error?.details?.includes('0 rows')) {
error = null
status = 200
statusText = 'OK'
}

if (error && this.shouldThrowOnError) {
throw new PostgrestError(error)
}
Expand Down
9 changes: 2 additions & 7 deletions packages/core/postgrest-js/src/PostgrestTransformBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -685,13 +685,8 @@ export default class PostgrestTransformBuilder<
maybeSingle<
ResultOne = Result extends (infer ResultOne)[] ? ResultOne : never,
>(): PostgrestBuilder<ClientOptions, ResultOne | null> {
// Temporary partial fix for https://github.com/supabase/postgrest-js/issues/361
// Issue persists e.g. for `.insert([...]).select().maybeSingle()`
if (this.method === 'GET') {
this.headers.set('Accept', 'application/json')
} else {
this.headers.set('Accept', 'application/vnd.pgrst.object+json')
}
// No Accept header override — we fetch as a list and enforce cardinality client-side.
// Fixes https://github.com/supabase/postgrest-js/issues/361 for all request methods.
this.isMaybeSingle = true
return this as unknown as PostgrestBuilder<ClientOptions, ResultOne | null>
}
Expand Down
25 changes: 2 additions & 23 deletions packages/core/postgrest-js/test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2157,29 +2157,8 @@ test('handles empty body with 404 status', async () => {
`)
})

test('maybeSingle handles zero rows error', async () => {
const customFetch = jest.fn().mockImplementation(() =>
Promise.resolve({
ok: false,
status: 406,
statusText: 'Not Acceptable',
text: () =>
Promise.resolve(
JSON.stringify({
code: 'PGRST116',
details: '0 rows',
hint: null,
message: 'JSON object requested, multiple (or no) rows returned',
})
),
})
)

const postgrestWithCustomFetch = new PostgrestClient<Database>(REST_URL, {
fetch: customFetch,
})

const res = await postgrestWithCustomFetch.from('users').select().maybeSingle()
test('maybeSingle returns null for zero rows (GET)', async () => {
const res = await postgrest.from('users').select().eq('username', 'does_not_exist').maybeSingle()

expect(res).toMatchInlineSnapshot(`
{
Expand Down
8 changes: 5 additions & 3 deletions packages/core/postgrest-js/test/transforms.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ test('maybeSingle', async () => {
"data": null,
"error": {
"code": "PGRST116",
"details": "The result contains 2 rows",
"details": "Results contain 2 rows, application/vnd.pgrst.object+json requires 1 row",
"hint": null,
"message": "Cannot coerce the result to a single JSON object",
"message": "JSON object requested, multiple (or no) rows returned",
},
"status": 406,
"statusText": "Not Acceptable",
Expand Down Expand Up @@ -286,7 +286,9 @@ test('csv', async () => {
kiwicopple,,"[25,35)",OFFLINE,"'bat' 'cat'"
awailas,,"[25,35)",ONLINE,"'bat' 'rat'"
jsonuser,"{""foo"": {""bar"": {""nested"": ""value""}, ""baz"": ""string value""}}","[20,30)",ONLINE,"'json' 'test'"
dragarcia,,"[20,30)",ONLINE,"'fat' 'rat'"",
dragarcia,,"[20,30)",ONLINE,"'fat' 'rat'"
a,,,ONLINE,
b,,,ONLINE,",
"error": null,
"status": 200,
"statusText": "OK",
Expand Down
Loading