Skip to content

fix: partial fix for maybeSingle logging 406s #424

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

Merged
merged 1 commit into from
May 11, 2023
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
"scripts": {
"clean": "rimraf dist docs/v2",
"format": "prettier --write \"{src,test}/**/*.ts\"",
"format:check": "prettier --check \"{src,test}/**/*.ts\"",
"build": "run-s clean format build:*",
"build:main": "tsc -p tsconfig.json",
"build:module": "tsc -p tsconfig.module.json",
"docs": "typedoc src/index.ts --out docs/v2",
"docs:json": "typedoc --json docs/v2/spec.json --excludeExternals src/index.ts",
"test": "run-s test:types db:clean db:run test:run db:clean",
"test": "run-s format:check test:types db:clean db:run test:run db:clean",
"test:run": "jest --runInBand",
"test:update": "run-s db:clean db:run && jest --runInBand --updateSnapshot && run-s db:clean",
"test:types": "run-s build:module && tsd --files test/*.test-d.ts",
Expand Down
28 changes: 25 additions & 3 deletions src/PostgrestBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default abstract class PostgrestBuilder<Result>
protected shouldThrowOnError = false
protected signal?: AbortSignal
protected fetch: Fetch
protected allowEmpty: boolean
protected isMaybeSingle: boolean

constructor(builder: PostgrestBuilder<Result>) {
this.method = builder.method
Expand All @@ -23,7 +23,7 @@ export default abstract class PostgrestBuilder<Result>
this.body = builder.body
this.shouldThrowOnError = builder.shouldThrowOnError
this.signal = builder.signal
this.allowEmpty = builder.allowEmpty
this.isMaybeSingle = builder.isMaybeSingle

if (builder.fetch) {
this.fetch = builder.fetch
Expand Down Expand Up @@ -101,6 +101,28 @@ export default abstract class PostgrestBuilder<Result>
if (countHeader && contentRange && contentRange.length > 1) {
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)) {
if (data.length > 1) {
error = {
// https://github.com/PostgREST/postgrest/blob/a867d79c42419af16c18c3fb019eba8df992626f/src/PostgREST/Error.hs#L553
code: 'PGRST116',
details: `Results contain ${data.length} rows, application/vnd.pgrst.object+json requires 1 row`,
hint: null,
message: 'JSON object requested, multiple (or no) rows returned',
}
data = null
count = null
status = 406
statusText = 'Not Acceptable'
} else if (data.length === 1) {
data = data[0]
} else {
data = null
}
}
} else {
const body = await res.text()

Expand All @@ -126,7 +148,7 @@ export default abstract class PostgrestBuilder<Result>
}
}

if (error && this.allowEmpty && error?.details?.includes('Results contain 0 rows')) {
if (error && this.isMaybeSingle && error?.details?.includes('Results contain 0 rows')) {
error = null
status = 200
statusText = 'OK'
Expand Down
10 changes: 8 additions & 2 deletions src/PostgrestTransformBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,14 @@ export default class PostgrestTransformBuilder<
maybeSingle<
ResultOne = Result extends (infer ResultOne)[] ? ResultOne : never
>(): PostgrestBuilder<ResultOne | null> {
this.headers['Accept'] = 'application/vnd.pgrst.object+json'
this.allowEmpty = true
// 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['Accept'] = 'application/json'
} else {
this.headers['Accept'] = 'application/vnd.pgrst.object+json'
}
this.isMaybeSingle = true
return this as PostgrestBuilder<ResultOne | null>
}

Expand Down
22 changes: 22 additions & 0 deletions test/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,28 @@ test('maybeSingle', async () => {
`)
})

test('maybeSingle', async () => {
const res = await postgrest
.from('users')
.insert([{ username: 'a' }, { username: 'b' }])
.select()
.maybeSingle()
expect(res).toMatchInlineSnapshot(`
Object {
"count": null,
"data": null,
"error": Object {
"code": "PGRST116",
"details": "Results contain 2 rows, application/vnd.pgrst.object+json requires 1 row",
"hint": null,
"message": "JSON object requested, multiple (or no) rows returned",
},
"status": 406,
"statusText": "Not Acceptable",
}
`)
})

test('select on insert', async () => {
const res = await postgrest.from('users').insert({ username: 'foo' }).select('status')
expect(res).toMatchInlineSnapshot(`
Expand Down