diff --git a/.gitignore b/.gitignore index 5044e41f..1b1d4286 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ docs # nyc coverage .nyc_output + +.idea \ No newline at end of file diff --git a/src/lib/PostgrestTransformBuilder.ts b/src/lib/PostgrestTransformBuilder.ts index f2e48e0c..fa8d54de 100644 --- a/src/lib/PostgrestTransformBuilder.ts +++ b/src/lib/PostgrestTransformBuilder.ts @@ -109,23 +109,8 @@ export default class PostgrestTransformBuilder extends PostgrestBuilder { */ maybeSingle(): PromiseLike> { 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> + this.allowEmpty = true + return this as PromiseLike> } /** diff --git a/src/lib/types.ts b/src/lib/types.ts index 40a2548f..2670a4ca 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -59,6 +59,7 @@ export abstract class PostgrestBuilder implements PromiseLike) { Object.assign(this, builder) @@ -72,6 +73,7 @@ export abstract class PostgrestBuilder implements PromiseLike _fetch(...args) this.shouldThrowOnError = builder.shouldThrowOnError || false + this.allowEmpty = builder.allowEmpty || false } /** @@ -116,6 +118,8 @@ export abstract class PostgrestBuilder implements PromiseLike implements PromiseLike implements PromiseLike { 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