@adrdp/pg-rest 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +184 -2
- package/dist/adapter-SqPs6SuJ.d.ts +27 -0
- package/dist/adapters/bun.d.ts +22 -0
- package/dist/adapters/bun.js +22 -0
- package/dist/adapters/node-postgres.d.ts +27 -0
- package/dist/adapters/node-postgres.js +34 -0
- package/dist/adapters/pglite.d.ts +24 -0
- package/dist/adapters/pglite.js +20 -0
- package/dist/adapters/postgres-js.d.ts +24 -0
- package/dist/adapters/postgres-js.js +21 -0
- package/dist/index.d.ts +259 -0
- package/dist/index.js +3549 -0
- package/package.json +87 -4
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { n as DbSession, r as Row, t as DbAdapter } from "./adapter-SqPs6SuJ.js";
|
|
2
|
+
|
|
3
|
+
//#region src/parse/request.d.ts
|
|
4
|
+
type Method = "GET" | "HEAD" | "POST" | "PATCH" | "PUT" | "DELETE" | "OPTIONS";
|
|
5
|
+
//#endregion
|
|
6
|
+
//#region src/schema.d.ts
|
|
7
|
+
interface Column {
|
|
8
|
+
readonly name: string;
|
|
9
|
+
/** Result of `format_type`, e.g. `integer`, `character varying(10)`, `text[]`. */
|
|
10
|
+
readonly type: string;
|
|
11
|
+
readonly nullable: boolean;
|
|
12
|
+
readonly hasDefault: boolean;
|
|
13
|
+
/** SQL text of the default expression, when there is one. */
|
|
14
|
+
readonly defaultExpr: string | null;
|
|
15
|
+
readonly generated: boolean;
|
|
16
|
+
readonly comment: string | null;
|
|
17
|
+
readonly position: number;
|
|
18
|
+
}
|
|
19
|
+
type RelationKind = "table" | "view" | "matview" | "foreign" | "partitioned";
|
|
20
|
+
interface Relation {
|
|
21
|
+
readonly schema: string;
|
|
22
|
+
readonly name: string;
|
|
23
|
+
readonly kind: RelationKind;
|
|
24
|
+
readonly columns: ReadonlyArray<Column>;
|
|
25
|
+
readonly pk: ReadonlyArray<string>;
|
|
26
|
+
readonly uniques: ReadonlyArray<ReadonlyArray<string>>;
|
|
27
|
+
readonly insertable: boolean;
|
|
28
|
+
readonly updatable: boolean;
|
|
29
|
+
readonly deletable: boolean;
|
|
30
|
+
readonly comment: string | null;
|
|
31
|
+
}
|
|
32
|
+
interface ForeignKey {
|
|
33
|
+
readonly name: string;
|
|
34
|
+
readonly schema: string;
|
|
35
|
+
readonly table: string;
|
|
36
|
+
readonly cols: ReadonlyArray<string>;
|
|
37
|
+
readonly fschema: string;
|
|
38
|
+
readonly ftable: string;
|
|
39
|
+
readonly fcols: ReadonlyArray<string>;
|
|
40
|
+
}
|
|
41
|
+
interface FunctionArg {
|
|
42
|
+
readonly name: string;
|
|
43
|
+
readonly type: string;
|
|
44
|
+
readonly hasDefault: boolean;
|
|
45
|
+
readonly variadic: boolean;
|
|
46
|
+
}
|
|
47
|
+
interface FunctionDef {
|
|
48
|
+
readonly schema: string;
|
|
49
|
+
readonly name: string;
|
|
50
|
+
readonly args: ReadonlyArray<FunctionArg>;
|
|
51
|
+
readonly returnType: string;
|
|
52
|
+
readonly returnsSet: boolean;
|
|
53
|
+
/** True if the function returns a composite / record / TABLE(...) row type. */
|
|
54
|
+
readonly returnsComposite: boolean;
|
|
55
|
+
readonly returnsVoid: boolean;
|
|
56
|
+
readonly volatility: "immutable" | "stable" | "volatile";
|
|
57
|
+
readonly comment: string | null;
|
|
58
|
+
/** The table when the function's only parameter is a table row (computed relationships). */
|
|
59
|
+
readonly argRelation?: {
|
|
60
|
+
readonly schema: string;
|
|
61
|
+
readonly name: string;
|
|
62
|
+
};
|
|
63
|
+
/** The table whose rows the function returns. */
|
|
64
|
+
readonly returnRelation?: {
|
|
65
|
+
readonly schema: string;
|
|
66
|
+
readonly name: string;
|
|
67
|
+
};
|
|
68
|
+
/** Planner row estimate; 1 on a set-returning function makes a computed relationship to-one. */
|
|
69
|
+
readonly rows: number;
|
|
70
|
+
/** Scalar functions returning a domain named like a media type (`"text/plain"`, …): the raw value is the body. */
|
|
71
|
+
readonly returnMediaType?: {
|
|
72
|
+
readonly name: string;
|
|
73
|
+
readonly bytea: boolean;
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/** A custom media type handler: an aggregate named after the media type that returns a domain (see PostgREST's "custom media types"). */
|
|
77
|
+
interface MediaHandler {
|
|
78
|
+
readonly schema: string;
|
|
79
|
+
/** The aggregate's name, i.e. the media type (for example `text/plain`, or the catch-all). */
|
|
80
|
+
readonly mediaType: string;
|
|
81
|
+
/** The table whose rows it aggregates, or `undefined` for `anyelement`. */
|
|
82
|
+
readonly argRelation?: {
|
|
83
|
+
readonly schema: string;
|
|
84
|
+
readonly name: string;
|
|
85
|
+
};
|
|
86
|
+
/** True when the domain wraps `bytea` (the body is binary). */
|
|
87
|
+
readonly bytea: boolean;
|
|
88
|
+
}
|
|
89
|
+
interface SchemaCache {
|
|
90
|
+
readonly mediaHandlers: ReadonlyArray<MediaHandler>;
|
|
91
|
+
readonly schemas: ReadonlyArray<string>;
|
|
92
|
+
readonly relations: ReadonlyMap<string, Relation>;
|
|
93
|
+
readonly foreignKeys: ReadonlyArray<ForeignKey>;
|
|
94
|
+
readonly functions: ReadonlyMap<string, ReadonlyArray<FunctionDef>>;
|
|
95
|
+
}
|
|
96
|
+
//#endregion
|
|
97
|
+
//#region src/core.d.ts
|
|
98
|
+
/** Who is making the request, as far as the database is concerned. */
|
|
99
|
+
interface RequestContext {
|
|
100
|
+
/** Database role to run the request as. Defaults to the anonymous role. */
|
|
101
|
+
readonly role?: string | undefined;
|
|
102
|
+
/**
|
|
103
|
+
* Claims exposed to SQL as `current_setting('request.jwt.claims')`.
|
|
104
|
+
* This is what RLS policies read, e.g. `{ sub: "user-123" }`.
|
|
105
|
+
* No signature is involved: you decide who the user is.
|
|
106
|
+
*/
|
|
107
|
+
readonly claims?: Readonly<Record<string, unknown>> | undefined;
|
|
108
|
+
/** Extra settings, set with `set_config(name, value, true)`. Names need a dot: `app.tenant_id`. */
|
|
109
|
+
readonly settings?: Readonly<Record<string, string>> | undefined;
|
|
110
|
+
}
|
|
111
|
+
interface PreRequestInfo {
|
|
112
|
+
readonly role: string;
|
|
113
|
+
readonly claims: Readonly<Record<string, unknown>>;
|
|
114
|
+
readonly method: Method;
|
|
115
|
+
readonly path: string;
|
|
116
|
+
readonly request: Request;
|
|
117
|
+
}
|
|
118
|
+
interface CoreConfig {
|
|
119
|
+
readonly schemas: ReadonlyArray<string>;
|
|
120
|
+
/** Role for requests without one; `undefined` refuses anonymous access (PGRST302). */
|
|
121
|
+
readonly anonymousRole: string | undefined;
|
|
122
|
+
readonly maxRows?: number | undefined;
|
|
123
|
+
readonly basePath: string;
|
|
124
|
+
readonly extraSearchPath: ReadonlyArray<string>;
|
|
125
|
+
/** Honor `Prefer: tx=rollback|commit` (PostgREST `db-tx-end`). */
|
|
126
|
+
readonly allowTxOverride: boolean;
|
|
127
|
+
/** Answer `Accept: application/vnd.pgrst.plan` with the query plan. */
|
|
128
|
+
readonly planEnabled: boolean;
|
|
129
|
+
readonly openApi: boolean;
|
|
130
|
+
/** Expose `request.headers` / `request.cookies` / `request.method` / `request.path` to SQL. */
|
|
131
|
+
readonly requestSettings: boolean;
|
|
132
|
+
/** SQL function (`"auth"."check"`) or callback run in the transaction before the request (db-pre-request). */
|
|
133
|
+
readonly preRequest?: string | ((session: DbSession, info: PreRequestInfo) => Promise<void>) | undefined;
|
|
134
|
+
readonly serverUrl?: string | undefined;
|
|
135
|
+
}
|
|
136
|
+
//#endregion
|
|
137
|
+
//#region src/errors.d.ts
|
|
138
|
+
/**
|
|
139
|
+
* Error body, in the exact shape PostgREST returns:
|
|
140
|
+
* `{ code, message, details, hint }`.
|
|
141
|
+
*/
|
|
142
|
+
interface ErrorBody {
|
|
143
|
+
readonly code: string;
|
|
144
|
+
readonly message: string;
|
|
145
|
+
readonly details: string | null;
|
|
146
|
+
readonly hint: string | null;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Thrown by the pure parts of the library (parsers, SQL builders). It is
|
|
150
|
+
* converted into an {@link ApiError} at the request boundary.
|
|
151
|
+
*/
|
|
152
|
+
declare class PgRestError extends Error {
|
|
153
|
+
readonly status: number;
|
|
154
|
+
readonly code: string;
|
|
155
|
+
readonly details: string | null;
|
|
156
|
+
readonly hint: string | null;
|
|
157
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
158
|
+
constructor(status: number, code: string, message: string, details?: string | null, hint?: string | null, headers?: Readonly<Record<string, string>>);
|
|
159
|
+
get body(): ErrorBody;
|
|
160
|
+
}
|
|
161
|
+
/** An error that has already been mapped to an HTTP response. */
|
|
162
|
+
declare class ApiError extends Error {
|
|
163
|
+
readonly _tag = "ApiError";
|
|
164
|
+
readonly status: number;
|
|
165
|
+
readonly body: ErrorBody;
|
|
166
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
167
|
+
constructor(fields: {
|
|
168
|
+
readonly status: number;
|
|
169
|
+
readonly body: ErrorBody;
|
|
170
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
/** Failure talking to the database, carrying Postgres error fields. */
|
|
174
|
+
interface DbErrorInfo {
|
|
175
|
+
/** SQLSTATE, e.g. "23505", or a custom code such as "PT401" / "PGRST". */
|
|
176
|
+
readonly sqlState: string;
|
|
177
|
+
readonly message: string;
|
|
178
|
+
readonly detail?: string | undefined;
|
|
179
|
+
readonly hint?: string | undefined;
|
|
180
|
+
}
|
|
181
|
+
/** Normalized error thrown by a {@link DbAdapter}. */
|
|
182
|
+
declare class DbQueryError extends Error implements DbErrorInfo {
|
|
183
|
+
readonly sqlState: string;
|
|
184
|
+
readonly detail?: string | undefined;
|
|
185
|
+
readonly hint?: string | undefined;
|
|
186
|
+
constructor(sqlState: string, message: string, detail?: string | undefined, hint?: string | undefined);
|
|
187
|
+
}
|
|
188
|
+
//#endregion
|
|
189
|
+
//#region src/service.d.ts
|
|
190
|
+
/** The schema could not be read from the database catalog. */
|
|
191
|
+
declare class SchemaLoadError extends Error {
|
|
192
|
+
readonly cause: unknown;
|
|
193
|
+
readonly _tag = "SchemaLoadError";
|
|
194
|
+
constructor(cause: unknown);
|
|
195
|
+
}
|
|
196
|
+
//#endregion
|
|
197
|
+
//#region src/index.d.ts
|
|
198
|
+
interface PgRestOptions {
|
|
199
|
+
/**
|
|
200
|
+
* The database, as a {@link DbAdapter} you create from your driver
|
|
201
|
+
* (Bun.sql, node-postgres, postgres.js, PGlite...). See `example/adapters`.
|
|
202
|
+
* pg-rest never closes it: you own its lifecycle.
|
|
203
|
+
*/
|
|
204
|
+
readonly db: DbAdapter;
|
|
205
|
+
/** Schemas to expose. The first one is the default. Default `["public"]`. */
|
|
206
|
+
readonly schemas?: ReadonlyArray<string>;
|
|
207
|
+
/**
|
|
208
|
+
* Role used when the request context has none. Default `"anon"`. `false` refuses anonymous requests
|
|
209
|
+
* with `401 PGRST302` (like PostgREST without `db-anon-role`).
|
|
210
|
+
*/
|
|
211
|
+
readonly anonymousRole?: string | false;
|
|
212
|
+
/**
|
|
213
|
+
* Decide who is calling. Return the role and claims (what RLS policies read
|
|
214
|
+
* from `request.jwt.claims`). There is no JWT: use a session cookie, an API
|
|
215
|
+
* key, your auth library... Throw a {@link PgRestError} to reject the request
|
|
216
|
+
* (e.g. `new PgRestError(401, "unauthorized", "Please sign in")`).
|
|
217
|
+
*
|
|
218
|
+
* Used by `handle(request)` when no explicit context is passed.
|
|
219
|
+
*/
|
|
220
|
+
readonly resolveContext?: (request: Request) => RequestContext | undefined | Promise<RequestContext | undefined>;
|
|
221
|
+
/** Maximum rows returned by a read (PostgREST `db-max-rows`). */
|
|
222
|
+
readonly maxRows?: number;
|
|
223
|
+
/** Mount path to strip from request URLs, e.g. `/api`. */
|
|
224
|
+
readonly basePath?: string;
|
|
225
|
+
/** Appended to `search_path` after the selected schema. Default `["public"]`. */
|
|
226
|
+
readonly extraSearchPath?: ReadonlyArray<string>;
|
|
227
|
+
/** Honor `Prefer: tx=rollback` (PostgREST `db-tx-end`). Default `false`. */
|
|
228
|
+
readonly allowTxOverride?: boolean;
|
|
229
|
+
/**
|
|
230
|
+
* Answer `Accept: application/vnd.pgrst.plan` requests with the query plan (PostgREST `db-plan-enabled`).
|
|
231
|
+
* A plan reveals the structure of the database: keep it off, or restrict it, in production. Default `false`.
|
|
232
|
+
*/
|
|
233
|
+
readonly planEnabled?: boolean;
|
|
234
|
+
/** Serve the OpenAPI document at `/`. Default `true`. */
|
|
235
|
+
readonly openApi?: boolean;
|
|
236
|
+
/** Expose `request.headers`, `request.cookies`, `request.method`, `request.path` to SQL. Default `true`. */
|
|
237
|
+
readonly requestSettings?: boolean;
|
|
238
|
+
/** A SQL function (`"auth.check_request"`) or callback to run before each request (PostgREST `db-pre-request`). */
|
|
239
|
+
readonly preRequest?: CoreConfig["preRequest"];
|
|
240
|
+
/** Public URL, used as `host` in the OpenAPI document. Defaults to the request origin. */
|
|
241
|
+
readonly serverUrl?: string;
|
|
242
|
+
}
|
|
243
|
+
interface PgRestInstance {
|
|
244
|
+
/**
|
|
245
|
+
* Handle a request. Pass `ctx` to set the user explicitly; otherwise
|
|
246
|
+
* `resolveContext` is used; otherwise the request runs as the anonymous role.
|
|
247
|
+
*/
|
|
248
|
+
handle(request: Request, ctx?: RequestContext): Promise<Response>;
|
|
249
|
+
/** Re-read the database catalog (after migrations). */
|
|
250
|
+
reloadSchema(): Promise<void>;
|
|
251
|
+
/** The current introspected schema. */
|
|
252
|
+
schemaCache(): Promise<SchemaCache>;
|
|
253
|
+
/** Stop the handler. The database adapter is left open: close it yourself. */
|
|
254
|
+
close(): Promise<void>;
|
|
255
|
+
}
|
|
256
|
+
/** Create a PostgREST-compatible handler over a Postgres database. */
|
|
257
|
+
declare const createPgRest: (options: PgRestOptions) => Promise<PgRestInstance>;
|
|
258
|
+
//#endregion
|
|
259
|
+
export { ApiError, type Column, type DbAdapter, DbQueryError, type DbSession, type ErrorBody, type ForeignKey, type FunctionDef, PgRestError, PgRestInstance, PgRestOptions, type PreRequestInfo, type Relation, type RequestContext, type Row, type SchemaCache, SchemaLoadError, createPgRest };
|