@tealbase/postgres-js
Version:
Isomorphic PostgREST client
44 lines • 1.3 kB
JavaScript
import { PostgrestQueryBuilder } from './builder';
export class PostgrestClient {
/**
* Creates a PostgREST client.
*
* @param url URL of the PostgREST endpoint.
* @param headers Custom headers.
* @param schema Postgres schema to switch to.
*/
constructor(url, { headers = {}, schema } = {}) {
this.url = url;
this.headers = headers;
this.schema = schema;
}
/**
* Authenticates the request with JWT.
*
* @param token The JWT token to use.
*/
auth(token) {
this.headers['Authorization'] = `Bearer ${token}`;
return this;
}
/**
* Perform a table operation.
*
* @param table The table name to operate on.
*/
from(table) {
const url = `${this.url}/${table}`;
return new PostgrestQueryBuilder(url, { headers: this.headers, schema: this.schema });
}
/**
* Perform a stored procedure call.
*
* @param fn The function name to call.
* @param params The parameters to pass to the function call.
*/
rpc(fn, params) {
const url = `${this.url}/rpc/${fn}`;
return new PostgrestQueryBuilder(url, { headers: this.headers, schema: this.schema }).rpc(params);
}
}
//# sourceMappingURL=index.js.map