Skip to content

Commit 2275648

Browse files
committed
Merge branch 'next' into merge-next-to-rc
2 parents 95aaa04 + d09fb89 commit 2275648

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

src/PostgrestBuilder.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ export default abstract class PostgrestBuilder<Result>
8787
// discard `text`
8888
} else if (this.headers['Accept'] === 'text/csv') {
8989
data = text
90+
} else if (
91+
this.headers['Accept'] &&
92+
this.headers['Accept'].indexOf('application/vnd.pgrst.plan+text') !== -1
93+
) {
94+
data = text
9095
} else {
9196
data = JSON.parse(text)
9297
}

src/PostgrestTransformBuilder.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,20 +156,25 @@ export default class PostgrestTransformBuilder<
156156
* @param settings If `true`, include information on configuration parameters that affect query planning.
157157
* @param buffers If `true`, include information on buffer usage.
158158
* @param wal If `true`, include information on WAL record generation
159+
* @param format The format of the output, can be 'text'(default) or `json`
159160
*/
160161
explain({
161162
analyze = false,
162163
verbose = false,
163164
settings = false,
164165
buffers = false,
165166
wal = false,
167+
format = 'text',
166168
}: {
167169
analyze?: boolean
168170
verbose?: boolean
169171
settings?: boolean
170172
buffers?: boolean
171173
wal?: boolean
172-
} = {}): PromiseLike<PostgrestResponse<Record<string, unknown>>> {
174+
format?: 'json' | 'text'
175+
} = {}):
176+
| PromiseLike<PostgrestResponse<Record<string, unknown>>>
177+
| PromiseLike<PostgrestSingleResponse<string>> {
173178
const options = [
174179
analyze ? 'analyze' : null,
175180
verbose ? 'verbose' : null,
@@ -183,7 +188,8 @@ export default class PostgrestTransformBuilder<
183188
const forMediatype = this.headers['Accept']
184189
this.headers[
185190
'Accept'
186-
] = `application/vnd.pgrst.plan+json; for="${forMediatype}"; options=${options};`
187-
return this as PromiseLike<PostgrestResponse<Record<string, unknown>>>
191+
] = `application/vnd.pgrst.plan+${format}; for="${forMediatype}"; options=${options};`
192+
if (format === 'json') return this as PromiseLike<PostgrestResponse<Record<string, unknown>>>
193+
else return this as PromiseLike<PostgrestSingleResponse<string>>
188194
}
189195
}

test/transforms.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ test('abort signal', async () => {
102102
// expect(res).toMatchInlineSnapshot()
103103
// })
104104

105-
test('explain', async () => {
106-
const res = await postgrest.from('users').select().explain()
107-
expect(res).toMatchInlineSnapshot(`
105+
test('explain with json/text format', async () => {
106+
const res1 = await postgrest.from('users').select().explain({format: 'json'})
107+
expect(res1).toMatchInlineSnapshot(`
108108
Object {
109109
"count": undefined,
110110
"data": Array [
@@ -141,10 +141,17 @@ test('explain', async () => {
141141
"statusText": "OK",
142142
}
143143
`)
144+
145+
const res2 = await postgrest.from('users').select().explain()
146+
expect(res2.data).toMatch(
147+
`Aggregate (cost=17.65..17.68 rows=1 width=112)
148+
-> Seq Scan on users (cost=0.00..15.10 rows=510 width=132)
149+
`
150+
)
144151
})
145152

146153
test('explain with options', async () => {
147-
const res = await postgrest.from('users').select().explain({ verbose: true, settings: true })
154+
const res = await postgrest.from('users').select().explain({ verbose: true, settings: true, format: 'json' })
148155
expect(res).toMatchInlineSnapshot(`
149156
Object {
150157
"count": undefined,

0 commit comments

Comments
 (0)