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
5 changes: 5 additions & 0 deletions src/PostgrestBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ export default abstract class PostgrestBuilder<Result>
// 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)
}
Expand Down
12 changes: 9 additions & 3 deletions src/PostgrestTransformBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,25 @@ 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,
verbose = false,
settings = false,
buffers = false,
wal = false,
format = 'json',
}: {
analyze?: boolean
verbose?: boolean
settings?: boolean
buffers?: boolean
wal?: boolean
} = {}): PromiseLike<PostgrestResponse<Record<string, unknown>>> {
format?: 'json' | 'text'
} = {}):
| PromiseLike<PostgrestResponse<Record<string, unknown>>>
| PromiseLike<PostgrestSingleResponse<string>> {
const options = [
analyze ? 'analyze' : null,
verbose ? 'verbose' : null,
Expand All @@ -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<PostgrestResponse<Record<string, unknown>>>
] = `application/vnd.pgrst.plan+${format}; for="${forMediatype}"; options=${options};`
if (format === 'json') return this as PromiseLike<PostgrestResponse<Record<string, unknown>>>
else return this as PromiseLike<PostgrestSingleResponse<string>>
}
}
13 changes: 10 additions & 3 deletions test/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down Expand Up @@ -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 () => {
Expand Down