Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ docs
# nyc
coverage
.nyc_output

.idea
19 changes: 2 additions & 17 deletions src/lib/PostgrestTransformBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,8 @@ export default class PostgrestTransformBuilder<T> extends PostgrestBuilder<T> {
*/
maybeSingle(): PromiseLike<PostgrestMaybeSingleResponse<T>> {
this.headers['Accept'] = 'application/vnd.pgrst.object+json'
const _this = new PostgrestTransformBuilder(this)
_this.then = ((onfulfilled: any, onrejected: any) =>
this.then((res: any): any => {
if (res.error?.details?.includes('Results contain 0 rows')) {
return onfulfilled({
error: null,
data: null,
count: res.count,
status: 200,
statusText: 'OK',
body: null,
})
}

return onfulfilled(res)
}, onrejected)) as any
return _this as PromiseLike<PostgrestMaybeSingleResponse<T>>
this.allowEmpty = true
return this as PromiseLike<PostgrestMaybeSingleResponse<T>>
}

/**
Expand Down
14 changes: 12 additions & 2 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
protected shouldThrowOnError: boolean
protected signal?: AbortSignal
protected fetch: Fetch
protected allowEmpty: boolean

constructor(builder: PostgrestBuilder<T>) {
Object.assign(this, builder)
Expand All @@ -72,6 +73,7 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
}
this.fetch = (...args) => _fetch(...args)
this.shouldThrowOnError = builder.shouldThrowOnError || false
this.allowEmpty = builder.allowEmpty || false
}

/**
Expand Down Expand Up @@ -116,6 +118,8 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
let error = null
let data = null
let count = null
let status = res.status
let statusText = res.statusText

if (res.ok) {
const isReturnMinimal = this.headers['Prefer']?.split(',').includes('return=minimal')
Expand Down Expand Up @@ -146,6 +150,12 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
}
}

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

if (error && this.shouldThrowOnError) {
throw error
}
Expand All @@ -155,8 +165,8 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
error,
data,
count,
status: res.status,
statusText: res.statusText,
status,
statusText,
body: data,
}

Expand Down
14 changes: 14 additions & 0 deletions test/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,20 @@ test('connection error w/ throwOnError', async () => {
expect(isErrorCaught).toBe(true)
})

test('maybeSingle w/ throwOnError', async () => {
let passes = true
await postgrest
.from('messages')
.select()
.eq('message', 'i do not exist')
.throwOnError(true)
.maybeSingle()
.then(undefined, () => {
passes = false
})
expect(passes).toEqual(true)
})

test('custom type', async () => {
interface User {
username: string
Expand Down