@data-loom/postgrest-js 0.0.2-alpha.2 → 0.0.2
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/dist/cjs/PostgrestBuilder.d.ts +61 -0
- package/dist/cjs/PostgrestBuilder.d.ts.map +1 -0
- package/dist/cjs/PostgrestBuilder.js +237 -0
- package/dist/cjs/PostgrestBuilder.js.map +1 -0
- package/dist/cjs/PostgrestClient.d.ts +72 -0
- package/dist/cjs/PostgrestClient.d.ts.map +1 -0
- package/dist/cjs/PostgrestClient.js +121 -0
- package/dist/cjs/PostgrestClient.js.map +1 -0
- package/dist/cjs/PostgrestError.d.ts +17 -0
- package/dist/cjs/PostgrestError.d.ts.map +1 -0
- package/dist/cjs/PostgrestError.js +18 -0
- package/dist/cjs/PostgrestError.js.map +1 -0
- package/dist/cjs/PostgrestFilterBuilder.d.ts +105 -0
- package/dist/cjs/PostgrestFilterBuilder.d.ts.map +1 -0
- package/dist/cjs/PostgrestFilterBuilder.js +381 -0
- package/dist/cjs/PostgrestFilterBuilder.js.map +1 -0
- package/dist/cjs/PostgrestQueryBuilder.d.ts +117 -0
- package/dist/cjs/PostgrestQueryBuilder.d.ts.map +1 -0
- package/dist/cjs/PostgrestQueryBuilder.js +271 -0
- package/dist/cjs/PostgrestQueryBuilder.js.map +1 -0
- package/dist/cjs/PostgrestTransformBuilder.d.ts +149 -0
- package/dist/cjs/PostgrestTransformBuilder.d.ts.map +1 -0
- package/dist/cjs/PostgrestTransformBuilder.js +222 -0
- package/dist/cjs/PostgrestTransformBuilder.js.map +1 -0
- package/dist/cjs/constants.d.ts +4 -0
- package/dist/cjs/constants.d.ts.map +1 -0
- package/dist/cjs/constants.js +6 -0
- package/dist/cjs/constants.js.map +1 -0
- package/dist/cjs/index.d.ts +19 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +28 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/select-query-parser/parser.d.ts +261 -0
- package/dist/cjs/select-query-parser/parser.d.ts.map +1 -0
- package/dist/cjs/select-query-parser/parser.js +5 -0
- package/dist/cjs/select-query-parser/parser.js.map +1 -0
- package/dist/cjs/select-query-parser/result.d.ts +149 -0
- package/dist/cjs/select-query-parser/result.d.ts.map +1 -0
- package/dist/cjs/select-query-parser/result.js +3 -0
- package/dist/cjs/select-query-parser/result.js.map +1 -0
- package/dist/cjs/select-query-parser/types.d.ts +31 -0
- package/dist/cjs/select-query-parser/types.d.ts.map +1 -0
- package/dist/cjs/select-query-parser/types.js +3 -0
- package/dist/cjs/select-query-parser/types.js.map +1 -0
- package/dist/cjs/select-query-parser/utils.d.ts +255 -0
- package/dist/cjs/select-query-parser/utils.d.ts.map +1 -0
- package/dist/cjs/select-query-parser/utils.js +3 -0
- package/dist/cjs/select-query-parser/utils.js.map +1 -0
- package/dist/cjs/types.d.ts +98 -0
- package/dist/cjs/types.d.ts.map +1 -0
- package/dist/cjs/types.js +3 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/cjs/version.d.ts +2 -0
- package/dist/cjs/version.d.ts.map +1 -0
- package/dist/cjs/version.js +5 -0
- package/dist/cjs/version.js.map +1 -0
- package/dist/esm/wrapper.mjs +28 -0
- package/package.json +2 -2
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { Fetch, PostgrestSingleResponse, PostgrestResponseSuccess, CheckMatchingArrayTypes, MergePartialResult, IsValidResultOverride } from './types';
|
|
2
|
+
import { ContainsNull } from './select-query-parser/types';
|
|
3
|
+
export default abstract class PostgrestBuilder<Result, ThrowOnError extends boolean = false> implements PromiseLike<ThrowOnError extends true ? PostgrestResponseSuccess<Result> : PostgrestSingleResponse<Result>> {
|
|
4
|
+
protected method: 'GET' | 'HEAD' | 'POST' | 'PATCH' | 'DELETE';
|
|
5
|
+
protected url: URL;
|
|
6
|
+
protected headers: Record<string, string>;
|
|
7
|
+
protected schema?: string;
|
|
8
|
+
protected body?: unknown;
|
|
9
|
+
protected shouldThrowOnError: boolean;
|
|
10
|
+
protected signal?: AbortSignal;
|
|
11
|
+
protected fetch: Fetch;
|
|
12
|
+
protected isMaybeSingle: boolean;
|
|
13
|
+
constructor(builder: PostgrestBuilder<Result>);
|
|
14
|
+
/**
|
|
15
|
+
* If there's an error with the query, throwOnError will reject the promise by
|
|
16
|
+
* throwing the error instead of returning it as part of a successful response.
|
|
17
|
+
*
|
|
18
|
+
* {@link https://github.com/supabase/supabase-js/issues/92}
|
|
19
|
+
*/
|
|
20
|
+
throwOnError(): this & PostgrestBuilder<Result, true>;
|
|
21
|
+
/**
|
|
22
|
+
* Set an HTTP header for the request.
|
|
23
|
+
*/
|
|
24
|
+
setHeader(name: string, value: string): this;
|
|
25
|
+
then<TResult1 = ThrowOnError extends true ? PostgrestResponseSuccess<Result> : PostgrestSingleResponse<Result>, TResult2 = never>(onfulfilled?: ((value: ThrowOnError extends true ? PostgrestResponseSuccess<Result> : PostgrestSingleResponse<Result>) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): PromiseLike<TResult1 | TResult2>;
|
|
26
|
+
/**
|
|
27
|
+
* Override the type of the returned `data`.
|
|
28
|
+
*
|
|
29
|
+
* @typeParam NewResult - The new result type to override with
|
|
30
|
+
* @deprecated Use overrideTypes<yourType, { merge: false }>() method at the end of your call chain instead
|
|
31
|
+
*/
|
|
32
|
+
returns<NewResult>(): PostgrestBuilder<CheckMatchingArrayTypes<Result, NewResult>, ThrowOnError>;
|
|
33
|
+
/**
|
|
34
|
+
* Override the type of the returned `data` field in the response.
|
|
35
|
+
*
|
|
36
|
+
* @typeParam NewResult - The new type to cast the response data to
|
|
37
|
+
* @typeParam Options - Optional type configuration (defaults to { merge: true })
|
|
38
|
+
* @typeParam Options.merge - When true, merges the new type with existing return type. When false, replaces the existing types entirely (defaults to true)
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* // Merge with existing types (default behavior)
|
|
42
|
+
* const query = supabase
|
|
43
|
+
* .from('users')
|
|
44
|
+
* .select()
|
|
45
|
+
* .overrideTypes<{ custom_field: string }>()
|
|
46
|
+
*
|
|
47
|
+
* // Replace existing types completely
|
|
48
|
+
* const replaceQuery = supabase
|
|
49
|
+
* .from('users')
|
|
50
|
+
* .select()
|
|
51
|
+
* .overrideTypes<{ id: number; name: string }, { merge: false }>()
|
|
52
|
+
* ```
|
|
53
|
+
* @returns A PostgrestBuilder instance with the new type
|
|
54
|
+
*/
|
|
55
|
+
overrideTypes<NewResult, Options extends {
|
|
56
|
+
merge?: boolean;
|
|
57
|
+
} = {
|
|
58
|
+
merge: true;
|
|
59
|
+
}>(): PostgrestBuilder<IsValidResultOverride<Result, NewResult, false, false> extends true ? ContainsNull<Result> extends true ? MergePartialResult<NewResult, NonNullable<Result>, Options> | null : MergePartialResult<NewResult, Result, Options> : CheckMatchingArrayTypes<Result, NewResult>, ThrowOnError>;
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=PostgrestBuilder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PostgrestBuilder.d.ts","sourceRoot":"","sources":["../../src/PostgrestBuilder.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,KAAK,EACL,uBAAuB,EACvB,wBAAwB,EACxB,uBAAuB,EACvB,kBAAkB,EAClB,qBAAqB,EACtB,MAAM,SAAS,CAAA;AAEhB,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAA;AAE1D,MAAM,CAAC,OAAO,CAAC,QAAQ,OAAO,gBAAgB,CAAC,MAAM,EAAE,YAAY,SAAS,OAAO,GAAG,KAAK,CACzF,YACE,WAAW,CACT,YAAY,SAAS,IAAI,GAAG,wBAAwB,CAAC,MAAM,CAAC,GAAG,uBAAuB,CAAC,MAAM,CAAC,CAC/F;IAEH,SAAS,CAAC,MAAM,EAAE,KAAK,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAA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AAE,YAAY,CAAC;IAQhG;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,aAAa,CACX,SAAS,EACT,OAAO,SAAS;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG;QAAE,KAAK,EAAE,IAAI,CAAA;KAAE,KAClD,gBAAgB,CACnB,qBAAqB,CAAC,MAAM,EAAE,SAAS,EAAE,KAAK,EAAE,KAAK,CAAC,SAAS,IAAI,GAE/D,YAAY,CAAC,MAAM,CAAC,SAAS,IAAI,GAC/B,kBAAkB,CAAC,SAAS,EAAE,WAAW,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,GAAG,IAAI,GAClE,kBAAkB,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,GAChD,uBAAuB,CAAC,MAAM,EAAE,SAAS,CAAC,EAC9C,YAAY,CACb;CAWF"}
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
const node_fetch_1 = __importDefault(require("@data-loom/node-fetch"));
|
|
8
|
+
const PostgrestError_1 = __importDefault(require("./PostgrestError"));
|
|
9
|
+
class PostgrestBuilder {
|
|
10
|
+
constructor(builder) {
|
|
11
|
+
this.shouldThrowOnError = false;
|
|
12
|
+
this.method = builder.method;
|
|
13
|
+
this.url = builder.url;
|
|
14
|
+
this.headers = builder.headers;
|
|
15
|
+
this.schema = builder.schema;
|
|
16
|
+
this.body = builder.body;
|
|
17
|
+
this.shouldThrowOnError = builder.shouldThrowOnError;
|
|
18
|
+
this.signal = builder.signal;
|
|
19
|
+
this.isMaybeSingle = builder.isMaybeSingle;
|
|
20
|
+
if (builder.fetch) {
|
|
21
|
+
this.fetch = builder.fetch;
|
|
22
|
+
}
|
|
23
|
+
else if (typeof fetch === 'undefined') {
|
|
24
|
+
this.fetch = node_fetch_1.default;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
this.fetch = fetch;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* If there's an error with the query, throwOnError will reject the promise by
|
|
32
|
+
* throwing the error instead of returning it as part of a successful response.
|
|
33
|
+
*
|
|
34
|
+
* {@link https://github.com/supabase/supabase-js/issues/92}
|
|
35
|
+
*/
|
|
36
|
+
throwOnError() {
|
|
37
|
+
this.shouldThrowOnError = true;
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Set an HTTP header for the request.
|
|
42
|
+
*/
|
|
43
|
+
setHeader(name, value) {
|
|
44
|
+
this.headers = Object.assign({}, this.headers);
|
|
45
|
+
this.headers[name] = value;
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
then(onfulfilled, onrejected) {
|
|
49
|
+
// https://postgrest.org/en/stable/api.html#switching-schemas
|
|
50
|
+
if (this.schema === undefined) {
|
|
51
|
+
// skip
|
|
52
|
+
}
|
|
53
|
+
else if (['GET', 'HEAD'].includes(this.method)) {
|
|
54
|
+
this.headers['Accept-Profile'] = this.schema;
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
this.headers['Content-Profile'] = this.schema;
|
|
58
|
+
}
|
|
59
|
+
if (this.method !== 'GET' && this.method !== 'HEAD') {
|
|
60
|
+
this.headers['Content-Type'] = 'application/json';
|
|
61
|
+
}
|
|
62
|
+
// NOTE: Invoke w/o `this` to avoid illegal invocation error.
|
|
63
|
+
// https://github.com/supabase/postgrest-js/pull/247
|
|
64
|
+
const _fetch = this.fetch;
|
|
65
|
+
let res = _fetch(this.url.toString(), {
|
|
66
|
+
method: this.method,
|
|
67
|
+
mode: 'cors',
|
|
68
|
+
redirect: 'manual',
|
|
69
|
+
credentials: 'include',
|
|
70
|
+
headers: this.headers,
|
|
71
|
+
body: JSON.stringify(this.body),
|
|
72
|
+
signal: this.signal,
|
|
73
|
+
}).then(async (res) => {
|
|
74
|
+
var _a, _b, _c;
|
|
75
|
+
let error = null;
|
|
76
|
+
let data = null;
|
|
77
|
+
let count = null;
|
|
78
|
+
let status = res.status;
|
|
79
|
+
let statusText = res.statusText;
|
|
80
|
+
const locationUrl = res.headers.get('Location');
|
|
81
|
+
if (res.status === 401 && locationUrl) {
|
|
82
|
+
location.href = locationUrl;
|
|
83
|
+
}
|
|
84
|
+
else if (res.ok) {
|
|
85
|
+
if (this.method !== 'HEAD') {
|
|
86
|
+
const body = await res.text();
|
|
87
|
+
if (body === '') {
|
|
88
|
+
// Prefer: return=minimal
|
|
89
|
+
}
|
|
90
|
+
else if (this.headers['Accept'] === 'text/csv') {
|
|
91
|
+
data = body;
|
|
92
|
+
}
|
|
93
|
+
else if (this.headers['Accept'] &&
|
|
94
|
+
this.headers['Accept'].includes('application/vnd.pgrst.plan+text')) {
|
|
95
|
+
data = body;
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
// body 结构: {data: {}, status_code: 0}
|
|
99
|
+
try {
|
|
100
|
+
data = JSON.parse(body).data;
|
|
101
|
+
}
|
|
102
|
+
catch (e) {
|
|
103
|
+
data = null;
|
|
104
|
+
error = JSON.parse(body);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const countHeader = (_a = this.headers['Prefer']) === null || _a === void 0 ? void 0 : _a.match(/count=(exact|planned|estimated)/);
|
|
109
|
+
const contentRange = (_b = res.headers.get('content-range')) === null || _b === void 0 ? void 0 : _b.split('/');
|
|
110
|
+
if (countHeader && contentRange && contentRange.length > 1) {
|
|
111
|
+
// 这种情况 method 是 GET,但是取值需要通过 header 取
|
|
112
|
+
count = parseInt(contentRange[1]);
|
|
113
|
+
data = null;
|
|
114
|
+
}
|
|
115
|
+
// Temporary partial fix for https://github.com/supabase/postgrest-js/issues/361
|
|
116
|
+
// Issue persists e.g. for `.insert([...]).select().maybeSingle()`
|
|
117
|
+
if (this.isMaybeSingle && this.method === 'GET' && Array.isArray(data)) {
|
|
118
|
+
if (data.length > 1) {
|
|
119
|
+
error = {
|
|
120
|
+
// https://github.com/PostgREST/postgrest/blob/a867d79c42419af16c18c3fb019eba8df992626f/src/PostgREST/Error.hs#L553
|
|
121
|
+
code: 'PGRST116',
|
|
122
|
+
details: `Results contain ${data.length} rows, application/vnd.pgrst.object+json requires 1 row`,
|
|
123
|
+
hint: null,
|
|
124
|
+
message: 'JSON object requested, multiple (or no) rows returned',
|
|
125
|
+
};
|
|
126
|
+
data = null;
|
|
127
|
+
count = null;
|
|
128
|
+
status = 406;
|
|
129
|
+
statusText = 'Not Acceptable';
|
|
130
|
+
}
|
|
131
|
+
else if (data.length === 1) {
|
|
132
|
+
data = data[0];
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
data = null;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
const body = await res.text();
|
|
141
|
+
try {
|
|
142
|
+
error = JSON.parse(body);
|
|
143
|
+
// Workaround for https://github.com/supabase/postgrest-js/issues/295
|
|
144
|
+
if (Array.isArray(error) && res.status === 404) {
|
|
145
|
+
data = [];
|
|
146
|
+
error = null;
|
|
147
|
+
status = 200;
|
|
148
|
+
statusText = 'OK';
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
catch (_d) {
|
|
152
|
+
// Workaround for https://github.com/supabase/postgrest-js/issues/295
|
|
153
|
+
if (res.status === 404 && body === '') {
|
|
154
|
+
status = 204;
|
|
155
|
+
statusText = 'No Content';
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
error = {
|
|
159
|
+
message: body,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (error && this.isMaybeSingle && ((_c = error === null || error === void 0 ? void 0 : error.details) === null || _c === void 0 ? void 0 : _c.includes('0 rows'))) {
|
|
164
|
+
error = null;
|
|
165
|
+
status = 200;
|
|
166
|
+
statusText = 'OK';
|
|
167
|
+
}
|
|
168
|
+
if (error && this.shouldThrowOnError) {
|
|
169
|
+
throw new PostgrestError_1.default(error);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
const postgrestResponse = {
|
|
173
|
+
error,
|
|
174
|
+
data,
|
|
175
|
+
count,
|
|
176
|
+
status,
|
|
177
|
+
statusText,
|
|
178
|
+
};
|
|
179
|
+
return postgrestResponse;
|
|
180
|
+
});
|
|
181
|
+
if (!this.shouldThrowOnError) {
|
|
182
|
+
res = res.catch((fetchError) => {
|
|
183
|
+
var _a, _b, _c;
|
|
184
|
+
return ({
|
|
185
|
+
error: {
|
|
186
|
+
message: `${(_a = fetchError === null || fetchError === void 0 ? void 0 : fetchError.name) !== null && _a !== void 0 ? _a : 'FetchError'}: ${fetchError === null || fetchError === void 0 ? void 0 : fetchError.message}`,
|
|
187
|
+
details: `${(_b = fetchError === null || fetchError === void 0 ? void 0 : fetchError.stack) !== null && _b !== void 0 ? _b : ''}`,
|
|
188
|
+
hint: '',
|
|
189
|
+
code: `${(_c = fetchError === null || fetchError === void 0 ? void 0 : fetchError.code) !== null && _c !== void 0 ? _c : ''}`,
|
|
190
|
+
},
|
|
191
|
+
data: null,
|
|
192
|
+
count: null,
|
|
193
|
+
status: 0,
|
|
194
|
+
statusText: '',
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
return res.then(onfulfilled, onrejected);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Override the type of the returned `data`.
|
|
202
|
+
*
|
|
203
|
+
* @typeParam NewResult - The new result type to override with
|
|
204
|
+
* @deprecated Use overrideTypes<yourType, { merge: false }>() method at the end of your call chain instead
|
|
205
|
+
*/
|
|
206
|
+
returns() {
|
|
207
|
+
/* istanbul ignore next */
|
|
208
|
+
return this;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Override the type of the returned `data` field in the response.
|
|
212
|
+
*
|
|
213
|
+
* @typeParam NewResult - The new type to cast the response data to
|
|
214
|
+
* @typeParam Options - Optional type configuration (defaults to { merge: true })
|
|
215
|
+
* @typeParam Options.merge - When true, merges the new type with existing return type. When false, replaces the existing types entirely (defaults to true)
|
|
216
|
+
* @example
|
|
217
|
+
* ```typescript
|
|
218
|
+
* // Merge with existing types (default behavior)
|
|
219
|
+
* const query = supabase
|
|
220
|
+
* .from('users')
|
|
221
|
+
* .select()
|
|
222
|
+
* .overrideTypes<{ custom_field: string }>()
|
|
223
|
+
*
|
|
224
|
+
* // Replace existing types completely
|
|
225
|
+
* const replaceQuery = supabase
|
|
226
|
+
* .from('users')
|
|
227
|
+
* .select()
|
|
228
|
+
* .overrideTypes<{ id: number; name: string }, { merge: false }>()
|
|
229
|
+
* ```
|
|
230
|
+
* @returns A PostgrestBuilder instance with the new type
|
|
231
|
+
*/
|
|
232
|
+
overrideTypes() {
|
|
233
|
+
return this;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
exports.default = PostgrestBuilder;
|
|
237
|
+
//# sourceMappingURL=PostgrestBuilder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PostgrestBuilder.js","sourceRoot":"","sources":["../../src/PostgrestBuilder.ts"],"names":[],"mappings":";;;;;AAAA,aAAa;AACb,uEAA6C;AAU7C,sEAA6C;AAG7C,MAA8B,gBAAgB;IAgB5C,YAAY,OAAiC;QALnC,uBAAkB,GAAG,KAAK,CAAA;QAMlC,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAA;QAC5B,IAAI,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,CAAA;QACtB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAA;QAC9B,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAA;QAC5B,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAA;QACxB,IAAI,CAAC,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aACF;YAED,MAAM,iBAAiB,GAAG;gBACxB,KAAK;gBACL,IAAI;gBACJ,KAAK;gBACL,MAAM;gBACN,UAAU;aACX,CAAA;YAED,OAAO,iBAAiB,CAAA;QAC1B,CAAC,CAAC,CAAA;QACF,IAAI,CAAC,IAAI,CAAC,kBAAkB,EAAE;YAC5B,GAAG,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,UAAU,EAAE,EAAE;;gBAAC,OAAA,CAAC;oBAC/B,KAAK,EAAE;wBACL,OAAO,EAAE,GAAG,MAAA,UAAU,aAAV,UAAU,uBAAV,UAAU,CAAE,IAAI,mCAAI,YAAY,KAAK,UAAU,aAAV,UAAU,uBAAV,UAAU,CAAE,OAAO,EAAE;wBACtE,OAAO,EAAE,GAAG,MAAA,UAAU,aAAV,UAAU,uBAAV,UAAU,CAAE,KAAK,mCAAI,EAAE,EAAE;wBACrC,IAAI,EAAE,EAAE;wBACR,IAAI,EAAE,GAAG,MAAA,UAAU,aAAV,UAAU,uBAAV,UAAU,CAAE,IAAI,mCAAI,EAAE,EAAE;qBAClC;oBACD,IAAI,EAAE,IAAI;oBACV,KAAK,EAAE,IAAI;oBACX,MAAM,EAAE,CAAC;oBACT,UAAU,EAAE,EAAE;iBACf,CAAC,CAAA;aAAA,CAAC,CAAA;SACJ;QAED,OAAO,GAAG,CAAC,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC,CAAA;IAC1C,CAAC;IAED;;;;;OAKG;IACH,OAAO;QACL,0BAA0B;QAC1B,OAAO,IAGN,CAAA;IACH,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,aAAa;QAYX,OAAO,IAQN,CAAA;IACH,CAAC;CACF;AArRD,mCAqRC"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import PostgrestQueryBuilder from './PostgrestQueryBuilder';
|
|
2
|
+
import PostgrestFilterBuilder from './PostgrestFilterBuilder';
|
|
3
|
+
import { Fetch, GenericSchema } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* PostgREST client.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam Database - Types for the schema from the [type
|
|
8
|
+
* generator](https://supabase.com/docs/reference/javascript/next/typescript-support)
|
|
9
|
+
*
|
|
10
|
+
* @typeParam SchemaName - Postgres schema to switch to. Must be a string
|
|
11
|
+
* literal, the same one passed to the constructor. If the schema is not
|
|
12
|
+
* `"public"`, this must be supplied manually.
|
|
13
|
+
*/
|
|
14
|
+
export default class PostgrestClient<Database = any, SchemaName extends string & keyof Database = 'public' extends keyof Database ? 'public' : string & keyof Database, Schema extends GenericSchema = Database[SchemaName] extends GenericSchema ? Database[SchemaName] : any> {
|
|
15
|
+
url: string;
|
|
16
|
+
headers: Record<string, string>;
|
|
17
|
+
schemaName?: SchemaName;
|
|
18
|
+
fetch?: Fetch;
|
|
19
|
+
/**
|
|
20
|
+
* Creates a PostgREST client.
|
|
21
|
+
*
|
|
22
|
+
* @param url - URL of the PostgREST endpoint
|
|
23
|
+
* @param options - Named parameters
|
|
24
|
+
* @param options.headers - Custom headers
|
|
25
|
+
* @param options.schema - Postgres schema to switch to
|
|
26
|
+
* @param options.fetch - Custom fetch
|
|
27
|
+
*/
|
|
28
|
+
constructor(url: string, { headers, schema, fetch, }?: {
|
|
29
|
+
headers?: Record<string, string>;
|
|
30
|
+
schema?: SchemaName;
|
|
31
|
+
fetch?: Fetch;
|
|
32
|
+
});
|
|
33
|
+
from<TableName extends string & keyof Schema['Tables'], Table extends Schema['Tables'][TableName]>(relation: TableName): PostgrestQueryBuilder<Schema, Table, TableName>;
|
|
34
|
+
from<ViewName extends string & keyof Schema['Views'], View extends Schema['Views'][ViewName]>(relation: ViewName): PostgrestQueryBuilder<Schema, View, ViewName>;
|
|
35
|
+
/**
|
|
36
|
+
* Select a schema to query or perform an function (rpc) call.
|
|
37
|
+
*
|
|
38
|
+
* The schema needs to be on the list of exposed schemas inside Supabase.
|
|
39
|
+
*
|
|
40
|
+
* @param schema - The schema to query
|
|
41
|
+
*/
|
|
42
|
+
schema<DynamicSchema extends string & keyof Database>(schema: DynamicSchema): PostgrestClient<Database, DynamicSchema, Database[DynamicSchema] extends GenericSchema ? Database[DynamicSchema] : any>;
|
|
43
|
+
/**
|
|
44
|
+
* Perform a function call.
|
|
45
|
+
*
|
|
46
|
+
* @param fn - The function name to call
|
|
47
|
+
* @param args - The arguments to pass to the function call
|
|
48
|
+
* @param options - Named parameters
|
|
49
|
+
* @param options.head - When set to `true`, `data` will not be returned.
|
|
50
|
+
* Useful if you only need the count.
|
|
51
|
+
* @param options.get - When set to `true`, the function will be called with
|
|
52
|
+
* read-only access mode.
|
|
53
|
+
* @param options.count - Count algorithm to use to count rows returned by the
|
|
54
|
+
* function. Only applicable for [set-returning
|
|
55
|
+
* functions](https://www.postgresql.org/docs/current/functions-srf.html).
|
|
56
|
+
*
|
|
57
|
+
* `"exact"`: Exact but slow count algorithm. Performs a `COUNT(*)` under the
|
|
58
|
+
* hood.
|
|
59
|
+
*
|
|
60
|
+
* `"planned"`: Approximated but fast count algorithm. Uses the Postgres
|
|
61
|
+
* statistics under the hood.
|
|
62
|
+
*
|
|
63
|
+
* `"estimated"`: Uses exact count for low numbers and planned count for high
|
|
64
|
+
* numbers.
|
|
65
|
+
*/
|
|
66
|
+
rpc<FnName extends string & keyof Schema['Functions'], Fn extends Schema['Functions'][FnName]>(fn: FnName, args?: Fn['Args'], { head, get, count, }?: {
|
|
67
|
+
head?: boolean;
|
|
68
|
+
get?: boolean;
|
|
69
|
+
count?: 'exact' | 'planned' | 'estimated';
|
|
70
|
+
}): PostgrestFilterBuilder<Schema, Fn['Returns'] extends any[] ? Fn['Returns'][number] extends Record<string, unknown> ? Fn['Returns'][number] : never : never, Fn['Returns'], FnName, null>;
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=PostgrestClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PostgrestClient.d.ts","sourceRoot":"","sources":["../../src/PostgrestClient.ts"],"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AAE,QAAQ,GACjB,qBAAqB,CAAC,MAAM,EAAE,IAAI,EAAE,QAAQ,CAAC;IAehD;;;;;;OAMG;IACH,MAAM,CAAC,aAAa,SAAS,MAAM,GAAG,MAAM,QAAQ,EAClD,MAAM,EAAE,aAAa,GACpB,eAAe,CAChB,QAAQ,EACR,aAAa,EACb,QAAQ,CAAC,aAAa,CAAC,SAAS,aAAa,GAAG,QAAQ,CAAC,aAAa,CAAC,GAAG,GAAG,CAC9E;IAQD;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,GAAG,CAAC,MAAM,SAAS,MAAM,GAAG,MAAM,MAAM,CAAC,WAAW,CAAC,EAAE,EAAE,SAAS,MAAM,CAAC,WAAW,CAAC,CAAC,MAAM,CAAC,EAC3F,EAAE,EAAE,MAAM,EACV,IAAI,GAAE,EAAE,CAAC,MAAM,CAAM,EACrB,EACE,IAAY,EACZ,GAAW,EACX,KAAK,GACN,GAAE;QACD,IAAI,CAAC,EAAE,OAAO,CAAA;QACd,GAAG,CAAC,EAAE,OAAO,CAAA;QACb,KAAK,CAAC,EAAE,OAAO,GAAG,SAAS,GAAG,WAAW,CAAA;KACrC,GACL,sBAAsB,CACvB,MAAM,EACN,EAAE,CAAC,SAAS,CAAC,SAAS,GAAG,EAAE,GACvB,EAAE,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GACnD,EAAE,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,GACrB,KAAK,GACP,KAAK,EACT,EAAE,CAAC,SAAS,CAAC,EACb,MAAM,EACN,IAAI,CACL;CAmCF"}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const PostgrestQueryBuilder_1 = __importDefault(require("./PostgrestQueryBuilder"));
|
|
7
|
+
const PostgrestFilterBuilder_1 = __importDefault(require("./PostgrestFilterBuilder"));
|
|
8
|
+
/**
|
|
9
|
+
* PostgREST client.
|
|
10
|
+
*
|
|
11
|
+
* @typeParam Database - Types for the schema from the [type
|
|
12
|
+
* generator](https://supabase.com/docs/reference/javascript/next/typescript-support)
|
|
13
|
+
*
|
|
14
|
+
* @typeParam SchemaName - Postgres schema to switch to. Must be a string
|
|
15
|
+
* literal, the same one passed to the constructor. If the schema is not
|
|
16
|
+
* `"public"`, this must be supplied manually.
|
|
17
|
+
*/
|
|
18
|
+
class PostgrestClient {
|
|
19
|
+
// TODO: Add back shouldThrowOnError once we figure out the typings
|
|
20
|
+
/**
|
|
21
|
+
* Creates a PostgREST client.
|
|
22
|
+
*
|
|
23
|
+
* @param url - URL of the PostgREST endpoint
|
|
24
|
+
* @param options - Named parameters
|
|
25
|
+
* @param options.headers - Custom headers
|
|
26
|
+
* @param options.schema - Postgres schema to switch to
|
|
27
|
+
* @param options.fetch - Custom fetch
|
|
28
|
+
*/
|
|
29
|
+
constructor(url, { headers = {}, schema, fetch, } = {}) {
|
|
30
|
+
this.url = url;
|
|
31
|
+
this.headers = Object.assign({}, headers);
|
|
32
|
+
this.schemaName = schema;
|
|
33
|
+
this.fetch = fetch;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Perform a query on a table or a view.
|
|
37
|
+
*
|
|
38
|
+
* @param relation - The table or view name to query
|
|
39
|
+
*/
|
|
40
|
+
from(relation) {
|
|
41
|
+
const url = new URL(`${this.url}/${relation}`);
|
|
42
|
+
return new PostgrestQueryBuilder_1.default(url, {
|
|
43
|
+
headers: Object.assign({}, this.headers),
|
|
44
|
+
schema: this.schemaName,
|
|
45
|
+
fetch: this.fetch,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Select a schema to query or perform an function (rpc) call.
|
|
50
|
+
*
|
|
51
|
+
* The schema needs to be on the list of exposed schemas inside Supabase.
|
|
52
|
+
*
|
|
53
|
+
* @param schema - The schema to query
|
|
54
|
+
*/
|
|
55
|
+
schema(schema) {
|
|
56
|
+
return new PostgrestClient(this.url, {
|
|
57
|
+
headers: this.headers,
|
|
58
|
+
schema,
|
|
59
|
+
fetch: this.fetch,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Perform a function call.
|
|
64
|
+
*
|
|
65
|
+
* @param fn - The function name to call
|
|
66
|
+
* @param args - The arguments to pass to the function call
|
|
67
|
+
* @param options - Named parameters
|
|
68
|
+
* @param options.head - When set to `true`, `data` will not be returned.
|
|
69
|
+
* Useful if you only need the count.
|
|
70
|
+
* @param options.get - When set to `true`, the function will be called with
|
|
71
|
+
* read-only access mode.
|
|
72
|
+
* @param options.count - Count algorithm to use to count rows returned by the
|
|
73
|
+
* function. Only applicable for [set-returning
|
|
74
|
+
* functions](https://www.postgresql.org/docs/current/functions-srf.html).
|
|
75
|
+
*
|
|
76
|
+
* `"exact"`: Exact but slow count algorithm. Performs a `COUNT(*)` under the
|
|
77
|
+
* hood.
|
|
78
|
+
*
|
|
79
|
+
* `"planned"`: Approximated but fast count algorithm. Uses the Postgres
|
|
80
|
+
* statistics under the hood.
|
|
81
|
+
*
|
|
82
|
+
* `"estimated"`: Uses exact count for low numbers and planned count for high
|
|
83
|
+
* numbers.
|
|
84
|
+
*/
|
|
85
|
+
rpc(fn, args = {}, { head = false, get = false, count, } = {}) {
|
|
86
|
+
let method;
|
|
87
|
+
const url = new URL(`${this.url}/rpc/${fn}`);
|
|
88
|
+
let body;
|
|
89
|
+
if (head || get) {
|
|
90
|
+
method = head ? 'HEAD' : 'GET';
|
|
91
|
+
Object.entries(args)
|
|
92
|
+
// params with undefined value needs to be filtered out, otherwise it'll
|
|
93
|
+
// show up as `?param=undefined`
|
|
94
|
+
.filter(([_, value]) => value !== undefined)
|
|
95
|
+
// array values need special syntax
|
|
96
|
+
.map(([name, value]) => [name, Array.isArray(value) ? `{${value.join(',')}}` : `${value}`])
|
|
97
|
+
.forEach(([name, value]) => {
|
|
98
|
+
url.searchParams.append(name, value);
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
method = 'POST';
|
|
103
|
+
body = args;
|
|
104
|
+
}
|
|
105
|
+
const headers = Object.assign({}, this.headers);
|
|
106
|
+
if (count) {
|
|
107
|
+
headers['Prefer'] = `count=${count}`;
|
|
108
|
+
}
|
|
109
|
+
return new PostgrestFilterBuilder_1.default({
|
|
110
|
+
method,
|
|
111
|
+
url,
|
|
112
|
+
headers,
|
|
113
|
+
schema: this.schemaName,
|
|
114
|
+
body,
|
|
115
|
+
fetch: this.fetch,
|
|
116
|
+
allowEmpty: false,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
exports.default = PostgrestClient;
|
|
121
|
+
//# sourceMappingURL=PostgrestClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PostgrestClient.js","sourceRoot":"","sources":["../../src/PostgrestClient.ts"],"names":[],"mappings":";;;;;AAAA,oFAA2D;AAC3D,sFAA6D;AAK7D;;;;;;;;;GASG;AACH,MAAqB,eAAe;IAclC,mEAAmE;IACnE;;;;;;;;OAQG;IACH,YACE,GAAW,EACX,EACE,OAAO,GAAG,EAAE,EACZ,MAAM,EACN,KAAK,MAKH,EAAE;QAEN,IAAI,CAAC,GAAG,GAAG,GAAG,CAAA;QACd,IAAI,CAAC,OAAO,qBAAQ,OAAO,CAAE,CAAA;QAC7B,IAAI,CAAC,UAAU,GAAG,MAAM,CAAA;QACxB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;IACpB,CAAC;IASD;;;;OAIG;IACH,IAAI,CAAC,QAAgB;QACnB,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,QAAQ,EAAE,CAAC,CAAA;QAC9C,OAAO,IAAI,+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AC1F,OAAO,CAAC,CAAC,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE,EAAE;gBACzB,GAAG,CAAC,YAAY,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,CAAA;YACtC,CAAC,CAAC,CAAA;SACL;aAAM;YACL,MAAM,GAAG,MAAM,CAAA;YACf,IAAI,GAAG,IAAI,CAAA;SACZ;QAED,MAAM,OAAO,qBAAQ,IAAI,CAAC,OAAO,CAAE,CAAA;QACnC,IAAI,KAAK,EAAE;YACT,OAAO,CAAC,QAAQ,CAAC,GAAG,SAAS,KAAK,EAAE,CAAA;SACrC;QAED,OAAO,IAAI,gCAAsB,CAAC;YAChC,MAAM;YACN,GAAG;YACH,OAAO;YACP,MAAM,EAAE,IAAI,CAAC,UAAU;YACvB,IAAI;YACJ,KAAK,EAAE,IAAI,CAAC,KAAK;YACjB,UAAU,EAAE,KAAK;SAC4B,CAAC,CAAA;IAClD,CAAC;CACF;AApKD,kCAoKC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error format
|
|
3
|
+
*
|
|
4
|
+
* {@link https://postgrest.org/en/stable/api.html?highlight=options#errors-and-http-status-codes}
|
|
5
|
+
*/
|
|
6
|
+
export default class PostgrestError extends Error {
|
|
7
|
+
details: string;
|
|
8
|
+
hint: string;
|
|
9
|
+
code: string;
|
|
10
|
+
constructor(context: {
|
|
11
|
+
message: string;
|
|
12
|
+
details: string;
|
|
13
|
+
hint: string;
|
|
14
|
+
code: string;
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=PostgrestError.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PostgrestError.d.ts","sourceRoot":"","sources":["../../src/PostgrestError.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,CAAC,OAAO,OAAO,cAAe,SAAQ,KAAK;IAC/C,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;gBAEA,OAAO,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE;CAOtF"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* Error format
|
|
5
|
+
*
|
|
6
|
+
* {@link https://postgrest.org/en/stable/api.html?highlight=options#errors-and-http-status-codes}
|
|
7
|
+
*/
|
|
8
|
+
class PostgrestError extends Error {
|
|
9
|
+
constructor(context) {
|
|
10
|
+
super(context.message);
|
|
11
|
+
this.name = 'PostgrestError';
|
|
12
|
+
this.details = context.details;
|
|
13
|
+
this.hint = context.hint;
|
|
14
|
+
this.code = context.code;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.default = PostgrestError;
|
|
18
|
+
//# sourceMappingURL=PostgrestError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PostgrestError.js","sourceRoot":"","sources":["../../src/PostgrestError.ts"],"names":[],"mappings":";;AAAA;;;;GAIG;AACH,MAAqB,cAAe,SAAQ,KAAK;IAK/C,YAAY,OAAyE;QACnF,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;QACtB,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAA;QAC5B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAA;QAC9B,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAA;QACxB,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAA;IAC1B,CAAC;CACF;AAZD,iCAYC"}
|