Skip to content

feat: postgrest 13 add maxaffected in client libraries #619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: avallete/psql-436-feedback-request-postgrest-js-and-postgrest-v13-integration
Choose a base branch
from
Draft
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: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:

- uses: actions/setup-node@v2
with:
node-version: '16'
node-version: '18'

- run: |
npm clean-install
Expand Down
38 changes: 24 additions & 14 deletions src/PostgrestBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,33 @@ export default abstract class PostgrestBuilder<
{
protected method: 'GET' | 'HEAD' | 'POST' | 'PATCH' | 'DELETE'
protected url: URL
protected headers: Record<string, string>
protected headers: Headers
protected schema?: string
protected body?: unknown
protected shouldThrowOnError = false
protected signal?: AbortSignal
protected fetch: Fetch
protected isMaybeSingle: boolean

constructor(builder: PostgrestBuilder<ClientOptions, Result>) {
constructor(builder: {
method: 'GET' | 'HEAD' | 'POST' | 'PATCH' | 'DELETE'
url: URL
headers: HeadersInit
schema?: string
body?: unknown
shouldThrowOnError?: boolean
signal?: AbortSignal
fetch?: Fetch
isMaybeSingle?: boolean
}) {
this.method = builder.method
this.url = builder.url
this.headers = builder.headers
this.headers = new Headers(builder.headers)
this.schema = builder.schema
this.body = builder.body
this.shouldThrowOnError = builder.shouldThrowOnError
this.shouldThrowOnError = builder.shouldThrowOnError ?? false
this.signal = builder.signal
this.isMaybeSingle = builder.isMaybeSingle
this.isMaybeSingle = builder.isMaybeSingle ?? false

if (builder.fetch) {
this.fetch = builder.fetch
Expand All @@ -66,8 +76,8 @@ export default abstract class PostgrestBuilder<
* Set an HTTP header for the request.
*/
setHeader(name: string, value: string): this {
this.headers = { ...this.headers }
this.headers[name] = value
this.headers = new Headers(this.headers)
this.headers.set(name, value)
return this
}

Expand All @@ -91,12 +101,12 @@ export default abstract class PostgrestBuilder<
if (this.schema === undefined) {
// skip
} else if (['GET', 'HEAD'].includes(this.method)) {
this.headers['Accept-Profile'] = this.schema
this.headers.set('Accept-Profile', this.schema)
} else {
this.headers['Content-Profile'] = this.schema
this.headers.set('Content-Profile', this.schema)
}
if (this.method !== 'GET' && this.method !== 'HEAD') {
this.headers['Content-Type'] = 'application/json'
this.headers.set('Content-Type', 'application/json')
}

// NOTE: Invoke w/o `this` to avoid illegal invocation error.
Expand All @@ -119,19 +129,19 @@ export default abstract class PostgrestBuilder<
const body = await res.text()
if (body === '') {
// Prefer: return=minimal
} else if (this.headers['Accept'] === 'text/csv') {
} else if (this.headers.get('Accept') === 'text/csv') {
data = body
} else if (
this.headers['Accept'] &&
this.headers['Accept'].includes('application/vnd.pgrst.plan+text')
this.headers.get('Accept') &&
this.headers.get('Accept')?.includes('application/vnd.pgrst.plan+text')
) {
data = body
} else {
data = JSON.parse(body)
}
}

const countHeader = this.headers['Prefer']?.match(/count=(exact|planned|estimated)/)
const countHeader = this.headers.get('Prefer')?.match(/count=(exact|planned|estimated)/)
const contentRange = res.headers.get('content-range')?.split('/')
if (countHeader && contentRange && contentRange.length > 1) {
count = parseInt(contentRange[1])
Expand Down
20 changes: 8 additions & 12 deletions src/PostgrestClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import PostgrestQueryBuilder from './PostgrestQueryBuilder'
import PostgrestFilterBuilder from './PostgrestFilterBuilder'
import PostgrestBuilder from './PostgrestBuilder'
import { DEFAULT_HEADERS } from './constants'
import { Fetch, GenericSchema, ClientServerOptions, GetGenericDatabaseWithOptions } from './types'

/**
Expand Down Expand Up @@ -29,7 +27,7 @@ export default class PostgrestClient<
: any
> {
url: string
headers: Record<string, string>
headers: Headers
schemaName?: SchemaName
fetch?: Fetch

Expand All @@ -50,17 +48,16 @@ export default class PostgrestClient<
schema,
fetch,
}: {
headers?: Record<string, string>
headers?: HeadersInit
schema?: SchemaName
fetch?: Fetch
} = {}
) {
this.url = url
this.headers = { ...DEFAULT_HEADERS, ...headers }
this.headers = new Headers(headers)
this.schemaName = schema
this.fetch = fetch
}

from<
TableName extends string & keyof Schema['Tables'],
Table extends Schema['Tables'][TableName]
Expand All @@ -76,7 +73,7 @@ export default class PostgrestClient<
from(relation: string): PostgrestQueryBuilder<ClientOptions, Schema, any, any> {
const url = new URL(`${this.url}/${relation}`)
return new PostgrestQueryBuilder(url, {
headers: { ...this.headers },
headers: new Headers(this.headers),
schema: this.schemaName,
fetch: this.fetch,
})
Expand Down Expand Up @@ -170,9 +167,9 @@ export default class PostgrestClient<
body = args
}

const headers = { ...this.headers }
const headers = new Headers(this.headers)
if (count) {
headers['Prefer'] = `count=${count}`
headers.set('Prefer', `count=${count}`)
}

return new PostgrestFilterBuilder({
Expand All @@ -181,8 +178,7 @@ export default class PostgrestClient<
headers,
schema: this.schemaName,
body,
fetch: this.fetch,
allowEmpty: false,
} as unknown as PostgrestBuilder<ClientOptions, Fn['Returns']>)
fetch: this.fetch ?? fetch,
})
}
}
6 changes: 4 additions & 2 deletions src/PostgrestFilterBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,16 @@ export default class PostgrestFilterBuilder<
Row extends Record<string, unknown>,
Result,
RelationName = unknown,
Relationships = unknown
Relationships = unknown,
Method = unknown
> extends PostgrestTransformBuilder<
ClientOptions,
Schema,
Row,
Result,
RelationName,
Relationships
Relationships,
Method
> {
/**
* Match only rows where `column` is equal to `value`.
Expand Down
Loading