From 55e808a02b0659af416e72a9a423a89803566a82 Mon Sep 17 00:00:00 2001 From: steve-chavez Date: Wed, 17 Aug 2022 01:00:19 -0500 Subject: [PATCH 1/2] feat: add text format to explain() --- src/PostgrestBuilder.ts | 5 +++++ src/PostgrestTransformBuilder.ts | 12 +++++++++--- test/transforms.ts | 13 ++++++++++--- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/PostgrestBuilder.ts b/src/PostgrestBuilder.ts index 39b65c3d..e54116a7 100644 --- a/src/PostgrestBuilder.ts +++ b/src/PostgrestBuilder.ts @@ -87,6 +87,11 @@ export default abstract class PostgrestBuilder // discard `text` } else if (this.headers['Accept'] === 'text/csv') { data = text + } else if ( + this.headers['Accept'] && + this.headers['Accept'].indexOf('application/vnd.pgrst.plan+text') !== -1 + ) { + data = text } else { data = JSON.parse(text) } diff --git a/src/PostgrestTransformBuilder.ts b/src/PostgrestTransformBuilder.ts index 573914e8..4b8c04f4 100644 --- a/src/PostgrestTransformBuilder.ts +++ b/src/PostgrestTransformBuilder.ts @@ -156,6 +156,7 @@ export default class PostgrestTransformBuilder< * @param settings If `true`, include information on configuration parameters that affect query planning. * @param buffers If `true`, include information on buffer usage. * @param wal If `true`, include information on WAL record generation + * @param format The format of the output, can be 'json'(default) or 'text' */ explain({ analyze = false, @@ -163,13 +164,17 @@ export default class PostgrestTransformBuilder< settings = false, buffers = false, wal = false, + format = 'json', }: { analyze?: boolean verbose?: boolean settings?: boolean buffers?: boolean wal?: boolean - } = {}): PromiseLike>> { + format?: 'json' | 'text' + } = {}): + | PromiseLike>> + | PromiseLike> { const options = [ analyze ? 'analyze' : null, verbose ? 'verbose' : null, @@ -183,7 +188,8 @@ export default class PostgrestTransformBuilder< const forMediatype = this.headers['Accept'] this.headers[ 'Accept' - ] = `application/vnd.pgrst.plan+json; for="${forMediatype}"; options=${options};` - return this as PromiseLike>> + ] = `application/vnd.pgrst.plan+${format}; for="${forMediatype}"; options=${options};` + if (format == 'json') return this as PromiseLike>> + else return this as PromiseLike> } } diff --git a/test/transforms.ts b/test/transforms.ts index aca65817..55c7885d 100644 --- a/test/transforms.ts +++ b/test/transforms.ts @@ -102,9 +102,9 @@ test('abort signal', async () => { // expect(res).toMatchInlineSnapshot() // }) -test('explain', async () => { - const res = await postgrest.from('users').select().explain() - expect(res).toMatchInlineSnapshot(` +test('explain with json/text format', async () => { + const res1 = await postgrest.from('users').select().explain() + expect(res1).toMatchInlineSnapshot(` Object { "count": undefined, "data": Array [ @@ -141,6 +141,13 @@ test('explain', async () => { "statusText": "OK", } `) + + const res2 = await postgrest.from('users').select().explain({ format: 'text' }) + expect(res2.data).toMatch( + `Aggregate (cost=17.65..17.68 rows=1 width=112) + -> Seq Scan on users (cost=0.00..15.10 rows=510 width=132) +` + ) }) test('explain with options', async () => { From b569ef105f635fac191889b9c7b9b4ec15c525f4 Mon Sep 17 00:00:00 2001 From: Steve Chavez Date: Wed, 17 Aug 2022 16:19:32 -0500 Subject: [PATCH 2/2] Update src/PostgrestTransformBuilder.ts Co-authored-by: Div Arora --- src/PostgrestTransformBuilder.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PostgrestTransformBuilder.ts b/src/PostgrestTransformBuilder.ts index 4b8c04f4..f409aec5 100644 --- a/src/PostgrestTransformBuilder.ts +++ b/src/PostgrestTransformBuilder.ts @@ -189,7 +189,7 @@ export default class PostgrestTransformBuilder< this.headers[ 'Accept' ] = `application/vnd.pgrst.plan+${format}; for="${forMediatype}"; options=${options};` - if (format == 'json') return this as PromiseLike>> + if (format === 'json') return this as PromiseLike>> else return this as PromiseLike> } }