@xata.io/client 0.8.2 → 0.8.3
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/CHANGELOG.md +6 -0
- package/dist/index.cjs +1762 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +2731 -7
- package/dist/index.mjs +1667 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +9 -5
- package/rollup.config.js +33 -0
- package/tsconfig.json +2 -2
- package/dist/api/client.d.ts +0 -95
- package/dist/api/client.js +0 -251
- package/dist/api/components.d.ts +0 -1437
- package/dist/api/components.js +0 -998
- package/dist/api/fetcher.d.ts +0 -40
- package/dist/api/fetcher.js +0 -79
- package/dist/api/index.d.ts +0 -13
- package/dist/api/index.js +0 -40
- package/dist/api/parameters.d.ts +0 -16
- package/dist/api/parameters.js +0 -2
- package/dist/api/providers.d.ts +0 -8
- package/dist/api/providers.js +0 -30
- package/dist/api/responses.d.ts +0 -50
- package/dist/api/responses.js +0 -2
- package/dist/api/schemas.d.ts +0 -311
- package/dist/api/schemas.js +0 -2
- package/dist/client.d.ts +0 -27
- package/dist/client.js +0 -131
- package/dist/index.js +0 -30
- package/dist/plugins.d.ts +0 -7
- package/dist/plugins.js +0 -6
- package/dist/schema/filters.d.ts +0 -96
- package/dist/schema/filters.js +0 -2
- package/dist/schema/filters.spec.d.ts +0 -1
- package/dist/schema/filters.spec.js +0 -177
- package/dist/schema/index.d.ts +0 -24
- package/dist/schema/index.js +0 -60
- package/dist/schema/operators.d.ts +0 -74
- package/dist/schema/operators.js +0 -93
- package/dist/schema/pagination.d.ts +0 -83
- package/dist/schema/pagination.js +0 -93
- package/dist/schema/query.d.ts +0 -118
- package/dist/schema/query.js +0 -242
- package/dist/schema/record.d.ts +0 -66
- package/dist/schema/record.js +0 -13
- package/dist/schema/repository.d.ts +0 -135
- package/dist/schema/repository.js +0 -283
- package/dist/schema/selection.d.ts +0 -25
- package/dist/schema/selection.js +0 -2
- package/dist/schema/selection.spec.d.ts +0 -1
- package/dist/schema/selection.spec.js +0 -204
- package/dist/schema/sorting.d.ts +0 -22
- package/dist/schema/sorting.js +0 -35
- package/dist/schema/sorting.spec.d.ts +0 -1
- package/dist/schema/sorting.spec.js +0 -11
- package/dist/search/index.d.ts +0 -34
- package/dist/search/index.js +0 -55
- package/dist/util/branches.d.ts +0 -5
- package/dist/util/branches.js +0 -7
- package/dist/util/config.d.ts +0 -11
- package/dist/util/config.js +0 -121
- package/dist/util/environment.d.ts +0 -5
- package/dist/util/environment.js +0 -68
- package/dist/util/fetch.d.ts +0 -2
- package/dist/util/fetch.js +0 -13
- package/dist/util/lang.d.ts +0 -5
- package/dist/util/lang.js +0 -22
- package/dist/util/types.d.ts +0 -25
- package/dist/util/types.js +0 -2
package/dist/index.mjs
ADDED
@@ -0,0 +1,1667 @@
|
|
1
|
+
function notEmpty(value) {
|
2
|
+
return value !== null && value !== void 0;
|
3
|
+
}
|
4
|
+
function compact(arr) {
|
5
|
+
return arr.filter(notEmpty);
|
6
|
+
}
|
7
|
+
function isObject(value) {
|
8
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
9
|
+
}
|
10
|
+
function isString(value) {
|
11
|
+
return value !== void 0 && value !== null && typeof value === "string";
|
12
|
+
}
|
13
|
+
|
14
|
+
function getEnvVariable(name) {
|
15
|
+
var _a, _b;
|
16
|
+
try {
|
17
|
+
if (isObject(process) && isString((_a = process == null ? void 0 : process.env) == null ? void 0 : _a[name])) {
|
18
|
+
return process.env[name];
|
19
|
+
}
|
20
|
+
} catch (err) {
|
21
|
+
}
|
22
|
+
try {
|
23
|
+
if (isObject(Deno) && isString((_b = Deno == null ? void 0 : Deno.env) == null ? void 0 : _b.get(name))) {
|
24
|
+
return Deno.env.get(name);
|
25
|
+
}
|
26
|
+
} catch (err) {
|
27
|
+
}
|
28
|
+
}
|
29
|
+
async function getGitBranch() {
|
30
|
+
try {
|
31
|
+
return require("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
|
32
|
+
} catch (err) {
|
33
|
+
}
|
34
|
+
try {
|
35
|
+
if (isObject(Deno)) {
|
36
|
+
const process2 = Deno.run({
|
37
|
+
cmd: ["git", "branch", "--show-current"],
|
38
|
+
stdout: "piped",
|
39
|
+
stderr: "piped"
|
40
|
+
});
|
41
|
+
return new TextDecoder().decode(await process2.output()).trim();
|
42
|
+
}
|
43
|
+
} catch (err) {
|
44
|
+
}
|
45
|
+
}
|
46
|
+
|
47
|
+
function getFetchImplementation(userFetch) {
|
48
|
+
const globalFetch = typeof fetch !== "undefined" ? fetch : void 0;
|
49
|
+
const fetchImpl = userFetch != null ? userFetch : globalFetch;
|
50
|
+
if (!fetchImpl) {
|
51
|
+
throw new Error(`The \`fetch\` option passed to the Xata client is resolving to a falsy value and may not be correctly imported.`);
|
52
|
+
}
|
53
|
+
return fetchImpl;
|
54
|
+
}
|
55
|
+
|
56
|
+
const envBranchNames = [
|
57
|
+
"XATA_BRANCH",
|
58
|
+
"VERCEL_GIT_COMMIT_REF",
|
59
|
+
"CF_PAGES_BRANCH",
|
60
|
+
"BRANCH"
|
61
|
+
];
|
62
|
+
const defaultBranch = "main";
|
63
|
+
async function getCurrentBranchName(options) {
|
64
|
+
const env = await getBranchByEnvVariable();
|
65
|
+
if (env)
|
66
|
+
return env;
|
67
|
+
const branch = await getGitBranch();
|
68
|
+
if (!branch)
|
69
|
+
return defaultBranch;
|
70
|
+
const details = await getDatabaseBranch(branch, options);
|
71
|
+
if (details)
|
72
|
+
return branch;
|
73
|
+
return defaultBranch;
|
74
|
+
}
|
75
|
+
async function getCurrentBranchDetails(options) {
|
76
|
+
const env = await getBranchByEnvVariable();
|
77
|
+
if (env)
|
78
|
+
return getDatabaseBranch(env, options);
|
79
|
+
const branch = await getGitBranch();
|
80
|
+
if (!branch)
|
81
|
+
return getDatabaseBranch(defaultBranch, options);
|
82
|
+
const details = await getDatabaseBranch(branch, options);
|
83
|
+
if (details)
|
84
|
+
return details;
|
85
|
+
return getDatabaseBranch(defaultBranch, options);
|
86
|
+
}
|
87
|
+
async function getDatabaseBranch(branch, options) {
|
88
|
+
const databaseURL = (options == null ? void 0 : options.databaseURL) || getDatabaseURL();
|
89
|
+
const apiKey = (options == null ? void 0 : options.apiKey) || getAPIKey();
|
90
|
+
if (!databaseURL)
|
91
|
+
throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
|
92
|
+
if (!apiKey)
|
93
|
+
throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
|
94
|
+
const [protocol, , host, , database] = databaseURL.split("/");
|
95
|
+
const [workspace] = host.split(".");
|
96
|
+
const dbBranchName = `${database}:${branch}`;
|
97
|
+
try {
|
98
|
+
return await getBranchDetails({
|
99
|
+
apiKey,
|
100
|
+
apiUrl: databaseURL,
|
101
|
+
fetchImpl: getFetchImplementation(options == null ? void 0 : options.fetchImpl),
|
102
|
+
workspacesApiUrl: `${protocol}//${host}`,
|
103
|
+
pathParams: {
|
104
|
+
dbBranchName,
|
105
|
+
workspace
|
106
|
+
}
|
107
|
+
});
|
108
|
+
} catch (err) {
|
109
|
+
if (isObject(err) && err.status === 404)
|
110
|
+
return null;
|
111
|
+
throw err;
|
112
|
+
}
|
113
|
+
}
|
114
|
+
function getBranchByEnvVariable() {
|
115
|
+
for (const name of envBranchNames) {
|
116
|
+
const value = getEnvVariable(name);
|
117
|
+
if (value) {
|
118
|
+
return value;
|
119
|
+
}
|
120
|
+
}
|
121
|
+
try {
|
122
|
+
return XATA_BRANCH;
|
123
|
+
} catch (err) {
|
124
|
+
}
|
125
|
+
}
|
126
|
+
function getDatabaseURL() {
|
127
|
+
var _a;
|
128
|
+
try {
|
129
|
+
return (_a = getEnvVariable("XATA_DATABASE_URL")) != null ? _a : XATA_DATABASE_URL;
|
130
|
+
} catch (err) {
|
131
|
+
return void 0;
|
132
|
+
}
|
133
|
+
}
|
134
|
+
function getAPIKey() {
|
135
|
+
var _a;
|
136
|
+
try {
|
137
|
+
return (_a = getEnvVariable("XATA_API_KEY")) != null ? _a : XATA_API_KEY;
|
138
|
+
} catch (err) {
|
139
|
+
return void 0;
|
140
|
+
}
|
141
|
+
}
|
142
|
+
|
143
|
+
const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
|
144
|
+
const query = new URLSearchParams(queryParams).toString();
|
145
|
+
const queryString = query.length > 0 ? `?${query}` : "";
|
146
|
+
return url.replace(/\{\w*\}/g, (key) => pathParams[key.slice(1, -1)]) + queryString;
|
147
|
+
};
|
148
|
+
function buildBaseUrl({
|
149
|
+
path,
|
150
|
+
workspacesApiUrl,
|
151
|
+
apiUrl,
|
152
|
+
pathParams
|
153
|
+
}) {
|
154
|
+
if (!(pathParams == null ? void 0 : pathParams.workspace))
|
155
|
+
return `${apiUrl}${path}`;
|
156
|
+
const url = typeof workspacesApiUrl === "string" ? `${workspacesApiUrl}${path}` : workspacesApiUrl(path, pathParams);
|
157
|
+
return url.replace("{workspaceId}", pathParams.workspace);
|
158
|
+
}
|
159
|
+
function hostHeader(url) {
|
160
|
+
var _a;
|
161
|
+
const pattern = /.*:\/\/(?<host>[^/]+).*/;
|
162
|
+
const { groups } = (_a = pattern.exec(url)) != null ? _a : {};
|
163
|
+
return (groups == null ? void 0 : groups.host) ? { Host: groups.host } : {};
|
164
|
+
}
|
165
|
+
async function fetch$1({
|
166
|
+
url: path,
|
167
|
+
method,
|
168
|
+
body,
|
169
|
+
headers,
|
170
|
+
pathParams,
|
171
|
+
queryParams,
|
172
|
+
fetchImpl,
|
173
|
+
apiKey,
|
174
|
+
apiUrl,
|
175
|
+
workspacesApiUrl
|
176
|
+
}) {
|
177
|
+
const baseUrl = buildBaseUrl({ path, workspacesApiUrl, pathParams, apiUrl });
|
178
|
+
const fullUrl = resolveUrl(baseUrl, queryParams, pathParams);
|
179
|
+
const url = fullUrl.includes("localhost") ? fullUrl.replace(/^[^.]+\./, "http://") : fullUrl;
|
180
|
+
const response = await fetchImpl(url, {
|
181
|
+
method: method.toUpperCase(),
|
182
|
+
body: body ? JSON.stringify(body) : void 0,
|
183
|
+
headers: {
|
184
|
+
"Content-Type": "application/json",
|
185
|
+
...headers,
|
186
|
+
...hostHeader(fullUrl),
|
187
|
+
Authorization: `Bearer ${apiKey}`
|
188
|
+
}
|
189
|
+
});
|
190
|
+
if (response.status === 204) {
|
191
|
+
return {};
|
192
|
+
}
|
193
|
+
try {
|
194
|
+
const jsonResponse = await response.json();
|
195
|
+
if (response.ok) {
|
196
|
+
return jsonResponse;
|
197
|
+
}
|
198
|
+
const { message = "Unknown error", errors } = jsonResponse;
|
199
|
+
throw new FetcherError({ message, status: response.status, errors });
|
200
|
+
} catch (error) {
|
201
|
+
const message = hasMessage(error) ? error.message : "Unknown network error";
|
202
|
+
const parent = error instanceof Error ? error : void 0;
|
203
|
+
throw new FetcherError({ message, status: response.status }, parent);
|
204
|
+
}
|
205
|
+
}
|
206
|
+
const hasMessage = (error) => {
|
207
|
+
return isObject(error) && isString(error.message);
|
208
|
+
};
|
209
|
+
class FetcherError extends Error {
|
210
|
+
constructor(data, parent) {
|
211
|
+
super(data.message);
|
212
|
+
this.status = data.status;
|
213
|
+
this.errors = data.errors;
|
214
|
+
if (parent) {
|
215
|
+
this.stack = parent.stack;
|
216
|
+
this.cause = parent.cause;
|
217
|
+
}
|
218
|
+
}
|
219
|
+
}
|
220
|
+
|
221
|
+
const getUser = (variables) => fetch$1({ url: "/user", method: "get", ...variables });
|
222
|
+
const updateUser = (variables) => fetch$1({ url: "/user", method: "put", ...variables });
|
223
|
+
const deleteUser = (variables) => fetch$1({ url: "/user", method: "delete", ...variables });
|
224
|
+
const getUserAPIKeys = (variables) => fetch$1({ url: "/user/keys", method: "get", ...variables });
|
225
|
+
const createUserAPIKey = (variables) => fetch$1({
|
226
|
+
url: "/user/keys/{keyName}",
|
227
|
+
method: "post",
|
228
|
+
...variables
|
229
|
+
});
|
230
|
+
const deleteUserAPIKey = (variables) => fetch$1({
|
231
|
+
url: "/user/keys/{keyName}",
|
232
|
+
method: "delete",
|
233
|
+
...variables
|
234
|
+
});
|
235
|
+
const createWorkspace = (variables) => fetch$1({ url: "/workspaces", method: "post", ...variables });
|
236
|
+
const getWorkspacesList = (variables) => fetch$1({ url: "/workspaces", method: "get", ...variables });
|
237
|
+
const getWorkspace = (variables) => fetch$1({
|
238
|
+
url: "/workspaces/{workspaceId}",
|
239
|
+
method: "get",
|
240
|
+
...variables
|
241
|
+
});
|
242
|
+
const updateWorkspace = (variables) => fetch$1({
|
243
|
+
url: "/workspaces/{workspaceId}",
|
244
|
+
method: "put",
|
245
|
+
...variables
|
246
|
+
});
|
247
|
+
const deleteWorkspace = (variables) => fetch$1({
|
248
|
+
url: "/workspaces/{workspaceId}",
|
249
|
+
method: "delete",
|
250
|
+
...variables
|
251
|
+
});
|
252
|
+
const getWorkspaceMembersList = (variables) => fetch$1({
|
253
|
+
url: "/workspaces/{workspaceId}/members",
|
254
|
+
method: "get",
|
255
|
+
...variables
|
256
|
+
});
|
257
|
+
const updateWorkspaceMemberRole = (variables) => fetch$1({
|
258
|
+
url: "/workspaces/{workspaceId}/members/{userId}",
|
259
|
+
method: "put",
|
260
|
+
...variables
|
261
|
+
});
|
262
|
+
const removeWorkspaceMember = (variables) => fetch$1({
|
263
|
+
url: "/workspaces/{workspaceId}/members/{userId}",
|
264
|
+
method: "delete",
|
265
|
+
...variables
|
266
|
+
});
|
267
|
+
const inviteWorkspaceMember = (variables) => fetch$1({
|
268
|
+
url: "/workspaces/{workspaceId}/invites",
|
269
|
+
method: "post",
|
270
|
+
...variables
|
271
|
+
});
|
272
|
+
const cancelWorkspaceMemberInvite = (variables) => fetch$1({
|
273
|
+
url: "/workspaces/{workspaceId}/invites/{inviteId}",
|
274
|
+
method: "delete",
|
275
|
+
...variables
|
276
|
+
});
|
277
|
+
const resendWorkspaceMemberInvite = (variables) => fetch$1({
|
278
|
+
url: "/workspaces/{workspaceId}/invites/{inviteId}/resend",
|
279
|
+
method: "post",
|
280
|
+
...variables
|
281
|
+
});
|
282
|
+
const acceptWorkspaceMemberInvite = (variables) => fetch$1({
|
283
|
+
url: "/workspaces/{workspaceId}/invites/{inviteKey}/accept",
|
284
|
+
method: "post",
|
285
|
+
...variables
|
286
|
+
});
|
287
|
+
const getDatabaseList = (variables) => fetch$1({
|
288
|
+
url: "/dbs",
|
289
|
+
method: "get",
|
290
|
+
...variables
|
291
|
+
});
|
292
|
+
const getBranchList = (variables) => fetch$1({
|
293
|
+
url: "/dbs/{dbName}",
|
294
|
+
method: "get",
|
295
|
+
...variables
|
296
|
+
});
|
297
|
+
const createDatabase = (variables) => fetch$1({
|
298
|
+
url: "/dbs/{dbName}",
|
299
|
+
method: "put",
|
300
|
+
...variables
|
301
|
+
});
|
302
|
+
const deleteDatabase = (variables) => fetch$1({
|
303
|
+
url: "/dbs/{dbName}",
|
304
|
+
method: "delete",
|
305
|
+
...variables
|
306
|
+
});
|
307
|
+
const getBranchDetails = (variables) => fetch$1({
|
308
|
+
url: "/db/{dbBranchName}",
|
309
|
+
method: "get",
|
310
|
+
...variables
|
311
|
+
});
|
312
|
+
const createBranch = (variables) => fetch$1({
|
313
|
+
url: "/db/{dbBranchName}",
|
314
|
+
method: "put",
|
315
|
+
...variables
|
316
|
+
});
|
317
|
+
const deleteBranch = (variables) => fetch$1({
|
318
|
+
url: "/db/{dbBranchName}",
|
319
|
+
method: "delete",
|
320
|
+
...variables
|
321
|
+
});
|
322
|
+
const updateBranchMetadata = (variables) => fetch$1({
|
323
|
+
url: "/db/{dbBranchName}/metadata",
|
324
|
+
method: "put",
|
325
|
+
...variables
|
326
|
+
});
|
327
|
+
const getBranchMetadata = (variables) => fetch$1({
|
328
|
+
url: "/db/{dbBranchName}/metadata",
|
329
|
+
method: "get",
|
330
|
+
...variables
|
331
|
+
});
|
332
|
+
const getBranchMigrationHistory = (variables) => fetch$1({ url: "/db/{dbBranchName}/migrations", method: "get", ...variables });
|
333
|
+
const executeBranchMigrationPlan = (variables) => fetch$1({
|
334
|
+
url: "/db/{dbBranchName}/migrations/execute",
|
335
|
+
method: "post",
|
336
|
+
...variables
|
337
|
+
});
|
338
|
+
const getBranchMigrationPlan = (variables) => fetch$1({
|
339
|
+
url: "/db/{dbBranchName}/migrations/plan",
|
340
|
+
method: "post",
|
341
|
+
...variables
|
342
|
+
});
|
343
|
+
const getBranchStats = (variables) => fetch$1({
|
344
|
+
url: "/db/{dbBranchName}/stats",
|
345
|
+
method: "get",
|
346
|
+
...variables
|
347
|
+
});
|
348
|
+
const createTable = (variables) => fetch$1({
|
349
|
+
url: "/db/{dbBranchName}/tables/{tableName}",
|
350
|
+
method: "put",
|
351
|
+
...variables
|
352
|
+
});
|
353
|
+
const deleteTable = (variables) => fetch$1({
|
354
|
+
url: "/db/{dbBranchName}/tables/{tableName}",
|
355
|
+
method: "delete",
|
356
|
+
...variables
|
357
|
+
});
|
358
|
+
const updateTable = (variables) => fetch$1({
|
359
|
+
url: "/db/{dbBranchName}/tables/{tableName}",
|
360
|
+
method: "patch",
|
361
|
+
...variables
|
362
|
+
});
|
363
|
+
const getTableSchema = (variables) => fetch$1({
|
364
|
+
url: "/db/{dbBranchName}/tables/{tableName}/schema",
|
365
|
+
method: "get",
|
366
|
+
...variables
|
367
|
+
});
|
368
|
+
const setTableSchema = (variables) => fetch$1({
|
369
|
+
url: "/db/{dbBranchName}/tables/{tableName}/schema",
|
370
|
+
method: "put",
|
371
|
+
...variables
|
372
|
+
});
|
373
|
+
const getTableColumns = (variables) => fetch$1({
|
374
|
+
url: "/db/{dbBranchName}/tables/{tableName}/columns",
|
375
|
+
method: "get",
|
376
|
+
...variables
|
377
|
+
});
|
378
|
+
const addTableColumn = (variables) => fetch$1({
|
379
|
+
url: "/db/{dbBranchName}/tables/{tableName}/columns",
|
380
|
+
method: "post",
|
381
|
+
...variables
|
382
|
+
});
|
383
|
+
const getColumn = (variables) => fetch$1({
|
384
|
+
url: "/db/{dbBranchName}/tables/{tableName}/columns/{columnName}",
|
385
|
+
method: "get",
|
386
|
+
...variables
|
387
|
+
});
|
388
|
+
const deleteColumn = (variables) => fetch$1({
|
389
|
+
url: "/db/{dbBranchName}/tables/{tableName}/columns/{columnName}",
|
390
|
+
method: "delete",
|
391
|
+
...variables
|
392
|
+
});
|
393
|
+
const updateColumn = (variables) => fetch$1({
|
394
|
+
url: "/db/{dbBranchName}/tables/{tableName}/columns/{columnName}",
|
395
|
+
method: "patch",
|
396
|
+
...variables
|
397
|
+
});
|
398
|
+
const insertRecord = (variables) => fetch$1({
|
399
|
+
url: "/db/{dbBranchName}/tables/{tableName}/data",
|
400
|
+
method: "post",
|
401
|
+
...variables
|
402
|
+
});
|
403
|
+
const insertRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "put", ...variables });
|
404
|
+
const updateRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "patch", ...variables });
|
405
|
+
const upsertRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "post", ...variables });
|
406
|
+
const deleteRecord = (variables) => fetch$1({
|
407
|
+
url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}",
|
408
|
+
method: "delete",
|
409
|
+
...variables
|
410
|
+
});
|
411
|
+
const getRecord = (variables) => fetch$1({
|
412
|
+
url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}",
|
413
|
+
method: "get",
|
414
|
+
...variables
|
415
|
+
});
|
416
|
+
const bulkInsertTableRecords = (variables) => fetch$1({
|
417
|
+
url: "/db/{dbBranchName}/tables/{tableName}/bulk",
|
418
|
+
method: "post",
|
419
|
+
...variables
|
420
|
+
});
|
421
|
+
const queryTable = (variables) => fetch$1({
|
422
|
+
url: "/db/{dbBranchName}/tables/{tableName}/query",
|
423
|
+
method: "post",
|
424
|
+
...variables
|
425
|
+
});
|
426
|
+
const searchBranch = (variables) => fetch$1({
|
427
|
+
url: "/db/{dbBranchName}/search",
|
428
|
+
method: "post",
|
429
|
+
...variables
|
430
|
+
});
|
431
|
+
const operationsByTag = {
|
432
|
+
users: { getUser, updateUser, deleteUser, getUserAPIKeys, createUserAPIKey, deleteUserAPIKey },
|
433
|
+
workspaces: {
|
434
|
+
createWorkspace,
|
435
|
+
getWorkspacesList,
|
436
|
+
getWorkspace,
|
437
|
+
updateWorkspace,
|
438
|
+
deleteWorkspace,
|
439
|
+
getWorkspaceMembersList,
|
440
|
+
updateWorkspaceMemberRole,
|
441
|
+
removeWorkspaceMember,
|
442
|
+
inviteWorkspaceMember,
|
443
|
+
cancelWorkspaceMemberInvite,
|
444
|
+
resendWorkspaceMemberInvite,
|
445
|
+
acceptWorkspaceMemberInvite
|
446
|
+
},
|
447
|
+
database: { getDatabaseList, createDatabase, deleteDatabase },
|
448
|
+
branch: {
|
449
|
+
getBranchList,
|
450
|
+
getBranchDetails,
|
451
|
+
createBranch,
|
452
|
+
deleteBranch,
|
453
|
+
updateBranchMetadata,
|
454
|
+
getBranchMetadata,
|
455
|
+
getBranchMigrationHistory,
|
456
|
+
executeBranchMigrationPlan,
|
457
|
+
getBranchMigrationPlan,
|
458
|
+
getBranchStats
|
459
|
+
},
|
460
|
+
table: {
|
461
|
+
createTable,
|
462
|
+
deleteTable,
|
463
|
+
updateTable,
|
464
|
+
getTableSchema,
|
465
|
+
setTableSchema,
|
466
|
+
getTableColumns,
|
467
|
+
addTableColumn,
|
468
|
+
getColumn,
|
469
|
+
deleteColumn,
|
470
|
+
updateColumn
|
471
|
+
},
|
472
|
+
records: {
|
473
|
+
insertRecord,
|
474
|
+
insertRecordWithID,
|
475
|
+
updateRecordWithID,
|
476
|
+
upsertRecordWithID,
|
477
|
+
deleteRecord,
|
478
|
+
getRecord,
|
479
|
+
bulkInsertTableRecords,
|
480
|
+
queryTable,
|
481
|
+
searchBranch
|
482
|
+
}
|
483
|
+
};
|
484
|
+
|
485
|
+
function getHostUrl(provider, type) {
|
486
|
+
if (isValidAlias(provider)) {
|
487
|
+
return providers[provider][type];
|
488
|
+
} else if (isValidBuilder(provider)) {
|
489
|
+
return provider[type];
|
490
|
+
}
|
491
|
+
throw new Error("Invalid API provider");
|
492
|
+
}
|
493
|
+
const providers = {
|
494
|
+
production: {
|
495
|
+
main: "https://api.xata.io",
|
496
|
+
workspaces: "https://{workspaceId}.xata.sh"
|
497
|
+
},
|
498
|
+
staging: {
|
499
|
+
main: "https://staging.xatabase.co",
|
500
|
+
workspaces: "https://{workspaceId}.staging.xatabase.co"
|
501
|
+
}
|
502
|
+
};
|
503
|
+
function isValidAlias(alias) {
|
504
|
+
return isString(alias) && Object.keys(providers).includes(alias);
|
505
|
+
}
|
506
|
+
function isValidBuilder(builder) {
|
507
|
+
return isObject(builder) && isString(builder.main) && isString(builder.workspaces);
|
508
|
+
}
|
509
|
+
|
510
|
+
var __accessCheck$6 = (obj, member, msg) => {
|
511
|
+
if (!member.has(obj))
|
512
|
+
throw TypeError("Cannot " + msg);
|
513
|
+
};
|
514
|
+
var __privateGet$5 = (obj, member, getter) => {
|
515
|
+
__accessCheck$6(obj, member, "read from private field");
|
516
|
+
return getter ? getter.call(obj) : member.get(obj);
|
517
|
+
};
|
518
|
+
var __privateAdd$6 = (obj, member, value) => {
|
519
|
+
if (member.has(obj))
|
520
|
+
throw TypeError("Cannot add the same private member more than once");
|
521
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
522
|
+
};
|
523
|
+
var __privateSet$4 = (obj, member, value, setter) => {
|
524
|
+
__accessCheck$6(obj, member, "write to private field");
|
525
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
526
|
+
return value;
|
527
|
+
};
|
528
|
+
var _extraProps, _namespaces;
|
529
|
+
class XataApiClient {
|
530
|
+
constructor(options) {
|
531
|
+
__privateAdd$6(this, _extraProps, void 0);
|
532
|
+
__privateAdd$6(this, _namespaces, {});
|
533
|
+
var _a, _b;
|
534
|
+
const provider = (_a = options.host) != null ? _a : "production";
|
535
|
+
const apiKey = (_b = options == null ? void 0 : options.apiKey) != null ? _b : getAPIKey();
|
536
|
+
if (!apiKey) {
|
537
|
+
throw new Error("Could not resolve a valid apiKey");
|
538
|
+
}
|
539
|
+
__privateSet$4(this, _extraProps, {
|
540
|
+
apiUrl: getHostUrl(provider, "main"),
|
541
|
+
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
542
|
+
fetchImpl: getFetchImplementation(options.fetch),
|
543
|
+
apiKey
|
544
|
+
});
|
545
|
+
}
|
546
|
+
get user() {
|
547
|
+
if (!__privateGet$5(this, _namespaces).user)
|
548
|
+
__privateGet$5(this, _namespaces).user = new UserApi(__privateGet$5(this, _extraProps));
|
549
|
+
return __privateGet$5(this, _namespaces).user;
|
550
|
+
}
|
551
|
+
get workspaces() {
|
552
|
+
if (!__privateGet$5(this, _namespaces).workspaces)
|
553
|
+
__privateGet$5(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$5(this, _extraProps));
|
554
|
+
return __privateGet$5(this, _namespaces).workspaces;
|
555
|
+
}
|
556
|
+
get databases() {
|
557
|
+
if (!__privateGet$5(this, _namespaces).databases)
|
558
|
+
__privateGet$5(this, _namespaces).databases = new DatabaseApi(__privateGet$5(this, _extraProps));
|
559
|
+
return __privateGet$5(this, _namespaces).databases;
|
560
|
+
}
|
561
|
+
get branches() {
|
562
|
+
if (!__privateGet$5(this, _namespaces).branches)
|
563
|
+
__privateGet$5(this, _namespaces).branches = new BranchApi(__privateGet$5(this, _extraProps));
|
564
|
+
return __privateGet$5(this, _namespaces).branches;
|
565
|
+
}
|
566
|
+
get tables() {
|
567
|
+
if (!__privateGet$5(this, _namespaces).tables)
|
568
|
+
__privateGet$5(this, _namespaces).tables = new TableApi(__privateGet$5(this, _extraProps));
|
569
|
+
return __privateGet$5(this, _namespaces).tables;
|
570
|
+
}
|
571
|
+
get records() {
|
572
|
+
if (!__privateGet$5(this, _namespaces).records)
|
573
|
+
__privateGet$5(this, _namespaces).records = new RecordsApi(__privateGet$5(this, _extraProps));
|
574
|
+
return __privateGet$5(this, _namespaces).records;
|
575
|
+
}
|
576
|
+
}
|
577
|
+
_extraProps = new WeakMap();
|
578
|
+
_namespaces = new WeakMap();
|
579
|
+
class UserApi {
|
580
|
+
constructor(extraProps) {
|
581
|
+
this.extraProps = extraProps;
|
582
|
+
}
|
583
|
+
getUser() {
|
584
|
+
return operationsByTag.users.getUser({ ...this.extraProps });
|
585
|
+
}
|
586
|
+
updateUser(user) {
|
587
|
+
return operationsByTag.users.updateUser({ body: user, ...this.extraProps });
|
588
|
+
}
|
589
|
+
deleteUser() {
|
590
|
+
return operationsByTag.users.deleteUser({ ...this.extraProps });
|
591
|
+
}
|
592
|
+
getUserAPIKeys() {
|
593
|
+
return operationsByTag.users.getUserAPIKeys({ ...this.extraProps });
|
594
|
+
}
|
595
|
+
createUserAPIKey(keyName) {
|
596
|
+
return operationsByTag.users.createUserAPIKey({
|
597
|
+
pathParams: { keyName },
|
598
|
+
...this.extraProps
|
599
|
+
});
|
600
|
+
}
|
601
|
+
deleteUserAPIKey(keyName) {
|
602
|
+
return operationsByTag.users.deleteUserAPIKey({
|
603
|
+
pathParams: { keyName },
|
604
|
+
...this.extraProps
|
605
|
+
});
|
606
|
+
}
|
607
|
+
}
|
608
|
+
class WorkspaceApi {
|
609
|
+
constructor(extraProps) {
|
610
|
+
this.extraProps = extraProps;
|
611
|
+
}
|
612
|
+
createWorkspace(workspaceMeta) {
|
613
|
+
return operationsByTag.workspaces.createWorkspace({
|
614
|
+
body: workspaceMeta,
|
615
|
+
...this.extraProps
|
616
|
+
});
|
617
|
+
}
|
618
|
+
getWorkspacesList() {
|
619
|
+
return operationsByTag.workspaces.getWorkspacesList({ ...this.extraProps });
|
620
|
+
}
|
621
|
+
getWorkspace(workspaceId) {
|
622
|
+
return operationsByTag.workspaces.getWorkspace({
|
623
|
+
pathParams: { workspaceId },
|
624
|
+
...this.extraProps
|
625
|
+
});
|
626
|
+
}
|
627
|
+
updateWorkspace(workspaceId, workspaceMeta) {
|
628
|
+
return operationsByTag.workspaces.updateWorkspace({
|
629
|
+
pathParams: { workspaceId },
|
630
|
+
body: workspaceMeta,
|
631
|
+
...this.extraProps
|
632
|
+
});
|
633
|
+
}
|
634
|
+
deleteWorkspace(workspaceId) {
|
635
|
+
return operationsByTag.workspaces.deleteWorkspace({
|
636
|
+
pathParams: { workspaceId },
|
637
|
+
...this.extraProps
|
638
|
+
});
|
639
|
+
}
|
640
|
+
getWorkspaceMembersList(workspaceId) {
|
641
|
+
return operationsByTag.workspaces.getWorkspaceMembersList({
|
642
|
+
pathParams: { workspaceId },
|
643
|
+
...this.extraProps
|
644
|
+
});
|
645
|
+
}
|
646
|
+
updateWorkspaceMemberRole(workspaceId, userId, role) {
|
647
|
+
return operationsByTag.workspaces.updateWorkspaceMemberRole({
|
648
|
+
pathParams: { workspaceId, userId },
|
649
|
+
body: { role },
|
650
|
+
...this.extraProps
|
651
|
+
});
|
652
|
+
}
|
653
|
+
removeWorkspaceMember(workspaceId, userId) {
|
654
|
+
return operationsByTag.workspaces.removeWorkspaceMember({
|
655
|
+
pathParams: { workspaceId, userId },
|
656
|
+
...this.extraProps
|
657
|
+
});
|
658
|
+
}
|
659
|
+
inviteWorkspaceMember(workspaceId, email, role) {
|
660
|
+
return operationsByTag.workspaces.inviteWorkspaceMember({
|
661
|
+
pathParams: { workspaceId },
|
662
|
+
body: { email, role },
|
663
|
+
...this.extraProps
|
664
|
+
});
|
665
|
+
}
|
666
|
+
cancelWorkspaceMemberInvite(workspaceId, inviteId) {
|
667
|
+
return operationsByTag.workspaces.cancelWorkspaceMemberInvite({
|
668
|
+
pathParams: { workspaceId, inviteId },
|
669
|
+
...this.extraProps
|
670
|
+
});
|
671
|
+
}
|
672
|
+
resendWorkspaceMemberInvite(workspaceId, inviteId) {
|
673
|
+
return operationsByTag.workspaces.resendWorkspaceMemberInvite({
|
674
|
+
pathParams: { workspaceId, inviteId },
|
675
|
+
...this.extraProps
|
676
|
+
});
|
677
|
+
}
|
678
|
+
acceptWorkspaceMemberInvite(workspaceId, inviteKey) {
|
679
|
+
return operationsByTag.workspaces.acceptWorkspaceMemberInvite({
|
680
|
+
pathParams: { workspaceId, inviteKey },
|
681
|
+
...this.extraProps
|
682
|
+
});
|
683
|
+
}
|
684
|
+
}
|
685
|
+
class DatabaseApi {
|
686
|
+
constructor(extraProps) {
|
687
|
+
this.extraProps = extraProps;
|
688
|
+
}
|
689
|
+
getDatabaseList(workspace) {
|
690
|
+
return operationsByTag.database.getDatabaseList({
|
691
|
+
pathParams: { workspace },
|
692
|
+
...this.extraProps
|
693
|
+
});
|
694
|
+
}
|
695
|
+
createDatabase(workspace, dbName, options = {}) {
|
696
|
+
return operationsByTag.database.createDatabase({
|
697
|
+
pathParams: { workspace, dbName },
|
698
|
+
body: options,
|
699
|
+
...this.extraProps
|
700
|
+
});
|
701
|
+
}
|
702
|
+
deleteDatabase(workspace, dbName) {
|
703
|
+
return operationsByTag.database.deleteDatabase({
|
704
|
+
pathParams: { workspace, dbName },
|
705
|
+
...this.extraProps
|
706
|
+
});
|
707
|
+
}
|
708
|
+
}
|
709
|
+
class BranchApi {
|
710
|
+
constructor(extraProps) {
|
711
|
+
this.extraProps = extraProps;
|
712
|
+
}
|
713
|
+
getBranchList(workspace, dbName) {
|
714
|
+
return operationsByTag.branch.getBranchList({
|
715
|
+
pathParams: { workspace, dbName },
|
716
|
+
...this.extraProps
|
717
|
+
});
|
718
|
+
}
|
719
|
+
getBranchDetails(workspace, database, branch) {
|
720
|
+
return operationsByTag.branch.getBranchDetails({
|
721
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
722
|
+
...this.extraProps
|
723
|
+
});
|
724
|
+
}
|
725
|
+
createBranch(workspace, database, branch, from = "", options = {}) {
|
726
|
+
return operationsByTag.branch.createBranch({
|
727
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
728
|
+
queryParams: { from },
|
729
|
+
body: options,
|
730
|
+
...this.extraProps
|
731
|
+
});
|
732
|
+
}
|
733
|
+
deleteBranch(workspace, database, branch) {
|
734
|
+
return operationsByTag.branch.deleteBranch({
|
735
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
736
|
+
...this.extraProps
|
737
|
+
});
|
738
|
+
}
|
739
|
+
updateBranchMetadata(workspace, database, branch, metadata = {}) {
|
740
|
+
return operationsByTag.branch.updateBranchMetadata({
|
741
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
742
|
+
body: metadata,
|
743
|
+
...this.extraProps
|
744
|
+
});
|
745
|
+
}
|
746
|
+
getBranchMetadata(workspace, database, branch) {
|
747
|
+
return operationsByTag.branch.getBranchMetadata({
|
748
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
749
|
+
...this.extraProps
|
750
|
+
});
|
751
|
+
}
|
752
|
+
getBranchMigrationHistory(workspace, database, branch, options = {}) {
|
753
|
+
return operationsByTag.branch.getBranchMigrationHistory({
|
754
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
755
|
+
body: options,
|
756
|
+
...this.extraProps
|
757
|
+
});
|
758
|
+
}
|
759
|
+
executeBranchMigrationPlan(workspace, database, branch, migrationPlan) {
|
760
|
+
return operationsByTag.branch.executeBranchMigrationPlan({
|
761
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
762
|
+
body: migrationPlan,
|
763
|
+
...this.extraProps
|
764
|
+
});
|
765
|
+
}
|
766
|
+
getBranchMigrationPlan(workspace, database, branch, schema) {
|
767
|
+
return operationsByTag.branch.getBranchMigrationPlan({
|
768
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
769
|
+
body: schema,
|
770
|
+
...this.extraProps
|
771
|
+
});
|
772
|
+
}
|
773
|
+
getBranchStats(workspace, database, branch) {
|
774
|
+
return operationsByTag.branch.getBranchStats({
|
775
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
776
|
+
...this.extraProps
|
777
|
+
});
|
778
|
+
}
|
779
|
+
}
|
780
|
+
class TableApi {
|
781
|
+
constructor(extraProps) {
|
782
|
+
this.extraProps = extraProps;
|
783
|
+
}
|
784
|
+
createTable(workspace, database, branch, tableName) {
|
785
|
+
return operationsByTag.table.createTable({
|
786
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
787
|
+
...this.extraProps
|
788
|
+
});
|
789
|
+
}
|
790
|
+
deleteTable(workspace, database, branch, tableName) {
|
791
|
+
return operationsByTag.table.deleteTable({
|
792
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
793
|
+
...this.extraProps
|
794
|
+
});
|
795
|
+
}
|
796
|
+
updateTable(workspace, database, branch, tableName, options) {
|
797
|
+
return operationsByTag.table.updateTable({
|
798
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
799
|
+
body: options,
|
800
|
+
...this.extraProps
|
801
|
+
});
|
802
|
+
}
|
803
|
+
getTableSchema(workspace, database, branch, tableName) {
|
804
|
+
return operationsByTag.table.getTableSchema({
|
805
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
806
|
+
...this.extraProps
|
807
|
+
});
|
808
|
+
}
|
809
|
+
setTableSchema(workspace, database, branch, tableName, options) {
|
810
|
+
return operationsByTag.table.setTableSchema({
|
811
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
812
|
+
body: options,
|
813
|
+
...this.extraProps
|
814
|
+
});
|
815
|
+
}
|
816
|
+
getTableColumns(workspace, database, branch, tableName) {
|
817
|
+
return operationsByTag.table.getTableColumns({
|
818
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
819
|
+
...this.extraProps
|
820
|
+
});
|
821
|
+
}
|
822
|
+
addTableColumn(workspace, database, branch, tableName, column) {
|
823
|
+
return operationsByTag.table.addTableColumn({
|
824
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
825
|
+
body: column,
|
826
|
+
...this.extraProps
|
827
|
+
});
|
828
|
+
}
|
829
|
+
getColumn(workspace, database, branch, tableName, columnName) {
|
830
|
+
return operationsByTag.table.getColumn({
|
831
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, columnName },
|
832
|
+
...this.extraProps
|
833
|
+
});
|
834
|
+
}
|
835
|
+
deleteColumn(workspace, database, branch, tableName, columnName) {
|
836
|
+
return operationsByTag.table.deleteColumn({
|
837
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, columnName },
|
838
|
+
...this.extraProps
|
839
|
+
});
|
840
|
+
}
|
841
|
+
updateColumn(workspace, database, branch, tableName, columnName, options) {
|
842
|
+
return operationsByTag.table.updateColumn({
|
843
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, columnName },
|
844
|
+
body: options,
|
845
|
+
...this.extraProps
|
846
|
+
});
|
847
|
+
}
|
848
|
+
}
|
849
|
+
class RecordsApi {
|
850
|
+
constructor(extraProps) {
|
851
|
+
this.extraProps = extraProps;
|
852
|
+
}
|
853
|
+
insertRecord(workspace, database, branch, tableName, record) {
|
854
|
+
return operationsByTag.records.insertRecord({
|
855
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
856
|
+
body: record,
|
857
|
+
...this.extraProps
|
858
|
+
});
|
859
|
+
}
|
860
|
+
insertRecordWithID(workspace, database, branch, tableName, recordId, record, options = {}) {
|
861
|
+
return operationsByTag.records.insertRecordWithID({
|
862
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
|
863
|
+
queryParams: options,
|
864
|
+
body: record,
|
865
|
+
...this.extraProps
|
866
|
+
});
|
867
|
+
}
|
868
|
+
updateRecordWithID(workspace, database, branch, tableName, recordId, record, options = {}) {
|
869
|
+
return operationsByTag.records.updateRecordWithID({
|
870
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
|
871
|
+
queryParams: options,
|
872
|
+
body: record,
|
873
|
+
...this.extraProps
|
874
|
+
});
|
875
|
+
}
|
876
|
+
upsertRecordWithID(workspace, database, branch, tableName, recordId, record, options = {}) {
|
877
|
+
return operationsByTag.records.upsertRecordWithID({
|
878
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
|
879
|
+
queryParams: options,
|
880
|
+
body: record,
|
881
|
+
...this.extraProps
|
882
|
+
});
|
883
|
+
}
|
884
|
+
deleteRecord(workspace, database, branch, tableName, recordId) {
|
885
|
+
return operationsByTag.records.deleteRecord({
|
886
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
|
887
|
+
...this.extraProps
|
888
|
+
});
|
889
|
+
}
|
890
|
+
getRecord(workspace, database, branch, tableName, recordId, options = {}) {
|
891
|
+
return operationsByTag.records.getRecord({
|
892
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
|
893
|
+
...this.extraProps
|
894
|
+
});
|
895
|
+
}
|
896
|
+
bulkInsertTableRecords(workspace, database, branch, tableName, records) {
|
897
|
+
return operationsByTag.records.bulkInsertTableRecords({
|
898
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
899
|
+
body: { records },
|
900
|
+
...this.extraProps
|
901
|
+
});
|
902
|
+
}
|
903
|
+
queryTable(workspace, database, branch, tableName, query) {
|
904
|
+
return operationsByTag.records.queryTable({
|
905
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
906
|
+
body: query,
|
907
|
+
...this.extraProps
|
908
|
+
});
|
909
|
+
}
|
910
|
+
searchBranch(workspace, database, branch, query) {
|
911
|
+
return operationsByTag.records.searchBranch({
|
912
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
913
|
+
body: query,
|
914
|
+
...this.extraProps
|
915
|
+
});
|
916
|
+
}
|
917
|
+
}
|
918
|
+
|
919
|
+
class XataApiPlugin {
|
920
|
+
async build(options) {
|
921
|
+
const { fetchImpl, apiKey } = await options.getFetchProps();
|
922
|
+
return new XataApiClient({ fetch: fetchImpl, apiKey });
|
923
|
+
}
|
924
|
+
}
|
925
|
+
|
926
|
+
class XataPlugin {
|
927
|
+
}
|
928
|
+
|
929
|
+
var __accessCheck$5 = (obj, member, msg) => {
|
930
|
+
if (!member.has(obj))
|
931
|
+
throw TypeError("Cannot " + msg);
|
932
|
+
};
|
933
|
+
var __privateGet$4 = (obj, member, getter) => {
|
934
|
+
__accessCheck$5(obj, member, "read from private field");
|
935
|
+
return getter ? getter.call(obj) : member.get(obj);
|
936
|
+
};
|
937
|
+
var __privateAdd$5 = (obj, member, value) => {
|
938
|
+
if (member.has(obj))
|
939
|
+
throw TypeError("Cannot add the same private member more than once");
|
940
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
941
|
+
};
|
942
|
+
var __privateSet$3 = (obj, member, value, setter) => {
|
943
|
+
__accessCheck$5(obj, member, "write to private field");
|
944
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
945
|
+
return value;
|
946
|
+
};
|
947
|
+
var _query;
|
948
|
+
class Page {
|
949
|
+
constructor(query, meta, records = []) {
|
950
|
+
__privateAdd$5(this, _query, void 0);
|
951
|
+
__privateSet$3(this, _query, query);
|
952
|
+
this.meta = meta;
|
953
|
+
this.records = records;
|
954
|
+
}
|
955
|
+
async nextPage(size, offset) {
|
956
|
+
return __privateGet$4(this, _query).getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
|
957
|
+
}
|
958
|
+
async previousPage(size, offset) {
|
959
|
+
return __privateGet$4(this, _query).getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
|
960
|
+
}
|
961
|
+
async firstPage(size, offset) {
|
962
|
+
return __privateGet$4(this, _query).getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
|
963
|
+
}
|
964
|
+
async lastPage(size, offset) {
|
965
|
+
return __privateGet$4(this, _query).getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
|
966
|
+
}
|
967
|
+
hasNextPage() {
|
968
|
+
return this.meta.page.more;
|
969
|
+
}
|
970
|
+
}
|
971
|
+
_query = new WeakMap();
|
972
|
+
const PAGINATION_MAX_SIZE = 200;
|
973
|
+
const PAGINATION_DEFAULT_SIZE = 200;
|
974
|
+
const PAGINATION_MAX_OFFSET = 800;
|
975
|
+
const PAGINATION_DEFAULT_OFFSET = 0;
|
976
|
+
|
977
|
+
var __accessCheck$4 = (obj, member, msg) => {
|
978
|
+
if (!member.has(obj))
|
979
|
+
throw TypeError("Cannot " + msg);
|
980
|
+
};
|
981
|
+
var __privateGet$3 = (obj, member, getter) => {
|
982
|
+
__accessCheck$4(obj, member, "read from private field");
|
983
|
+
return getter ? getter.call(obj) : member.get(obj);
|
984
|
+
};
|
985
|
+
var __privateAdd$4 = (obj, member, value) => {
|
986
|
+
if (member.has(obj))
|
987
|
+
throw TypeError("Cannot add the same private member more than once");
|
988
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
989
|
+
};
|
990
|
+
var __privateSet$2 = (obj, member, value, setter) => {
|
991
|
+
__accessCheck$4(obj, member, "write to private field");
|
992
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
993
|
+
return value;
|
994
|
+
};
|
995
|
+
var _table$1, _repository, _data;
|
996
|
+
const _Query = class {
|
997
|
+
constructor(repository, table, data, parent) {
|
998
|
+
__privateAdd$4(this, _table$1, void 0);
|
999
|
+
__privateAdd$4(this, _repository, void 0);
|
1000
|
+
__privateAdd$4(this, _data, { filter: {} });
|
1001
|
+
this.meta = { page: { cursor: "start", more: true } };
|
1002
|
+
this.records = [];
|
1003
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
|
1004
|
+
__privateSet$2(this, _table$1, table);
|
1005
|
+
if (repository) {
|
1006
|
+
__privateSet$2(this, _repository, repository);
|
1007
|
+
} else {
|
1008
|
+
__privateSet$2(this, _repository, this);
|
1009
|
+
}
|
1010
|
+
__privateGet$3(this, _data).filter = (_b = (_a = data.filter) != null ? _a : parent == null ? void 0 : parent.filter) != null ? _b : {};
|
1011
|
+
__privateGet$3(this, _data).filter.$any = (_e = (_c = data.filter) == null ? void 0 : _c.$any) != null ? _e : (_d = parent == null ? void 0 : parent.filter) == null ? void 0 : _d.$any;
|
1012
|
+
__privateGet$3(this, _data).filter.$all = (_h = (_f = data.filter) == null ? void 0 : _f.$all) != null ? _h : (_g = parent == null ? void 0 : parent.filter) == null ? void 0 : _g.$all;
|
1013
|
+
__privateGet$3(this, _data).filter.$not = (_k = (_i = data.filter) == null ? void 0 : _i.$not) != null ? _k : (_j = parent == null ? void 0 : parent.filter) == null ? void 0 : _j.$not;
|
1014
|
+
__privateGet$3(this, _data).filter.$none = (_n = (_l = data.filter) == null ? void 0 : _l.$none) != null ? _n : (_m = parent == null ? void 0 : parent.filter) == null ? void 0 : _m.$none;
|
1015
|
+
__privateGet$3(this, _data).sort = (_o = data.sort) != null ? _o : parent == null ? void 0 : parent.sort;
|
1016
|
+
__privateGet$3(this, _data).columns = (_q = (_p = data.columns) != null ? _p : parent == null ? void 0 : parent.columns) != null ? _q : ["*"];
|
1017
|
+
__privateGet$3(this, _data).page = (_r = data.page) != null ? _r : parent == null ? void 0 : parent.page;
|
1018
|
+
this.any = this.any.bind(this);
|
1019
|
+
this.all = this.all.bind(this);
|
1020
|
+
this.not = this.not.bind(this);
|
1021
|
+
this.filter = this.filter.bind(this);
|
1022
|
+
this.sort = this.sort.bind(this);
|
1023
|
+
this.none = this.none.bind(this);
|
1024
|
+
Object.defineProperty(this, "table", { enumerable: false });
|
1025
|
+
Object.defineProperty(this, "repository", { enumerable: false });
|
1026
|
+
}
|
1027
|
+
getQueryOptions() {
|
1028
|
+
return __privateGet$3(this, _data);
|
1029
|
+
}
|
1030
|
+
any(...queries) {
|
1031
|
+
const $any = queries.map((query) => {
|
1032
|
+
var _a;
|
1033
|
+
return (_a = query.getQueryOptions().filter) != null ? _a : {};
|
1034
|
+
});
|
1035
|
+
return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $any } }, __privateGet$3(this, _data));
|
1036
|
+
}
|
1037
|
+
all(...queries) {
|
1038
|
+
const $all = queries.map((query) => {
|
1039
|
+
var _a;
|
1040
|
+
return (_a = query.getQueryOptions().filter) != null ? _a : {};
|
1041
|
+
});
|
1042
|
+
return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $all } }, __privateGet$3(this, _data));
|
1043
|
+
}
|
1044
|
+
not(...queries) {
|
1045
|
+
const $not = queries.map((query) => {
|
1046
|
+
var _a;
|
1047
|
+
return (_a = query.getQueryOptions().filter) != null ? _a : {};
|
1048
|
+
});
|
1049
|
+
return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $not } }, __privateGet$3(this, _data));
|
1050
|
+
}
|
1051
|
+
none(...queries) {
|
1052
|
+
const $none = queries.map((query) => {
|
1053
|
+
var _a;
|
1054
|
+
return (_a = query.getQueryOptions().filter) != null ? _a : {};
|
1055
|
+
});
|
1056
|
+
return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $none } }, __privateGet$3(this, _data));
|
1057
|
+
}
|
1058
|
+
filter(a, b) {
|
1059
|
+
var _a, _b;
|
1060
|
+
if (arguments.length === 1) {
|
1061
|
+
const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
|
1062
|
+
const $all = compact([(_a = __privateGet$3(this, _data).filter) == null ? void 0 : _a.$all].flat().concat(constraints));
|
1063
|
+
return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $all } }, __privateGet$3(this, _data));
|
1064
|
+
} else {
|
1065
|
+
const $all = compact([(_b = __privateGet$3(this, _data).filter) == null ? void 0 : _b.$all].flat().concat([{ [a]: b }]));
|
1066
|
+
return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $all } }, __privateGet$3(this, _data));
|
1067
|
+
}
|
1068
|
+
}
|
1069
|
+
sort(column, direction) {
|
1070
|
+
var _a;
|
1071
|
+
const originalSort = [(_a = __privateGet$3(this, _data).sort) != null ? _a : []].flat();
|
1072
|
+
const sort = [...originalSort, { column, direction }];
|
1073
|
+
return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { sort }, __privateGet$3(this, _data));
|
1074
|
+
}
|
1075
|
+
select(columns) {
|
1076
|
+
return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { columns }, __privateGet$3(this, _data));
|
1077
|
+
}
|
1078
|
+
getPaginated(options = {}) {
|
1079
|
+
const query = new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), options, __privateGet$3(this, _data));
|
1080
|
+
return __privateGet$3(this, _repository).query(query);
|
1081
|
+
}
|
1082
|
+
async *[Symbol.asyncIterator]() {
|
1083
|
+
for await (const [record] of this.getIterator(1)) {
|
1084
|
+
yield record;
|
1085
|
+
}
|
1086
|
+
}
|
1087
|
+
async *getIterator(chunk, options = {}) {
|
1088
|
+
let offset = 0;
|
1089
|
+
let end = false;
|
1090
|
+
while (!end) {
|
1091
|
+
const { records, meta } = await this.getPaginated({ ...options, page: { size: chunk, offset } });
|
1092
|
+
yield records;
|
1093
|
+
offset += chunk;
|
1094
|
+
end = !meta.page.more;
|
1095
|
+
}
|
1096
|
+
}
|
1097
|
+
async getMany(options = {}) {
|
1098
|
+
const { records } = await this.getPaginated(options);
|
1099
|
+
return records;
|
1100
|
+
}
|
1101
|
+
async getAll(chunk = PAGINATION_MAX_SIZE, options = {}) {
|
1102
|
+
const results = [];
|
1103
|
+
for await (const page of this.getIterator(chunk, options)) {
|
1104
|
+
results.push(...page);
|
1105
|
+
}
|
1106
|
+
return results;
|
1107
|
+
}
|
1108
|
+
async getOne(options = {}) {
|
1109
|
+
const records = await this.getMany({ ...options, page: { size: 1 } });
|
1110
|
+
return records[0] || null;
|
1111
|
+
}
|
1112
|
+
nextPage(size, offset) {
|
1113
|
+
return this.firstPage(size, offset);
|
1114
|
+
}
|
1115
|
+
previousPage(size, offset) {
|
1116
|
+
return this.firstPage(size, offset);
|
1117
|
+
}
|
1118
|
+
firstPage(size, offset) {
|
1119
|
+
return this.getPaginated({ page: { size, offset } });
|
1120
|
+
}
|
1121
|
+
lastPage(size, offset) {
|
1122
|
+
return this.getPaginated({ page: { size, offset, before: "end" } });
|
1123
|
+
}
|
1124
|
+
hasNextPage() {
|
1125
|
+
return this.meta.page.more;
|
1126
|
+
}
|
1127
|
+
};
|
1128
|
+
let Query = _Query;
|
1129
|
+
_table$1 = new WeakMap();
|
1130
|
+
_repository = new WeakMap();
|
1131
|
+
_data = new WeakMap();
|
1132
|
+
|
1133
|
+
function isIdentifiable(x) {
|
1134
|
+
return isObject(x) && isString(x == null ? void 0 : x.id);
|
1135
|
+
}
|
1136
|
+
function isXataRecord(x) {
|
1137
|
+
var _a;
|
1138
|
+
return isIdentifiable(x) && typeof (x == null ? void 0 : x.xata) === "object" && typeof ((_a = x == null ? void 0 : x.xata) == null ? void 0 : _a.version) === "number";
|
1139
|
+
}
|
1140
|
+
|
1141
|
+
function isSortFilterString(value) {
|
1142
|
+
return isString(value);
|
1143
|
+
}
|
1144
|
+
function isSortFilterBase(filter) {
|
1145
|
+
return isObject(filter) && Object.values(filter).every((value) => value === "asc" || value === "desc");
|
1146
|
+
}
|
1147
|
+
function isSortFilterObject(filter) {
|
1148
|
+
return isObject(filter) && !isSortFilterBase(filter) && filter.column !== void 0;
|
1149
|
+
}
|
1150
|
+
function buildSortFilter(filter) {
|
1151
|
+
var _a;
|
1152
|
+
if (isSortFilterString(filter)) {
|
1153
|
+
return { [filter]: "asc" };
|
1154
|
+
} else if (Array.isArray(filter)) {
|
1155
|
+
return filter.map((item) => buildSortFilter(item));
|
1156
|
+
} else if (isSortFilterBase(filter)) {
|
1157
|
+
return filter;
|
1158
|
+
} else if (isSortFilterObject(filter)) {
|
1159
|
+
return { [filter.column]: (_a = filter.direction) != null ? _a : "asc" };
|
1160
|
+
} else {
|
1161
|
+
throw new Error(`Invalid sort filter: ${filter}`);
|
1162
|
+
}
|
1163
|
+
}
|
1164
|
+
|
1165
|
+
var __accessCheck$3 = (obj, member, msg) => {
|
1166
|
+
if (!member.has(obj))
|
1167
|
+
throw TypeError("Cannot " + msg);
|
1168
|
+
};
|
1169
|
+
var __privateGet$2 = (obj, member, getter) => {
|
1170
|
+
__accessCheck$3(obj, member, "read from private field");
|
1171
|
+
return getter ? getter.call(obj) : member.get(obj);
|
1172
|
+
};
|
1173
|
+
var __privateAdd$3 = (obj, member, value) => {
|
1174
|
+
if (member.has(obj))
|
1175
|
+
throw TypeError("Cannot add the same private member more than once");
|
1176
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1177
|
+
};
|
1178
|
+
var __privateSet$1 = (obj, member, value, setter) => {
|
1179
|
+
__accessCheck$3(obj, member, "write to private field");
|
1180
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1181
|
+
return value;
|
1182
|
+
};
|
1183
|
+
var __privateMethod$2 = (obj, member, method) => {
|
1184
|
+
__accessCheck$3(obj, member, "access private method");
|
1185
|
+
return method;
|
1186
|
+
};
|
1187
|
+
var _table, _links, _getFetchProps, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _bulkInsertTableRecords, bulkInsertTableRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn, _initObject, initObject_fn;
|
1188
|
+
class Repository extends Query {
|
1189
|
+
}
|
1190
|
+
class RestRepository extends Query {
|
1191
|
+
constructor(options) {
|
1192
|
+
super(null, options.table, {});
|
1193
|
+
__privateAdd$3(this, _insertRecordWithoutId);
|
1194
|
+
__privateAdd$3(this, _insertRecordWithId);
|
1195
|
+
__privateAdd$3(this, _bulkInsertTableRecords);
|
1196
|
+
__privateAdd$3(this, _updateRecordWithID);
|
1197
|
+
__privateAdd$3(this, _upsertRecordWithID);
|
1198
|
+
__privateAdd$3(this, _deleteRecord);
|
1199
|
+
__privateAdd$3(this, _initObject);
|
1200
|
+
__privateAdd$3(this, _table, void 0);
|
1201
|
+
__privateAdd$3(this, _links, void 0);
|
1202
|
+
__privateAdd$3(this, _getFetchProps, void 0);
|
1203
|
+
var _a;
|
1204
|
+
__privateSet$1(this, _table, options.table);
|
1205
|
+
__privateSet$1(this, _links, (_a = options.links) != null ? _a : {});
|
1206
|
+
__privateSet$1(this, _getFetchProps, options.getFetchProps);
|
1207
|
+
this.db = options.db;
|
1208
|
+
}
|
1209
|
+
async create(a, b) {
|
1210
|
+
if (Array.isArray(a)) {
|
1211
|
+
return __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
|
1212
|
+
}
|
1213
|
+
if (isString(a) && isObject(b)) {
|
1214
|
+
if (a === "")
|
1215
|
+
throw new Error("The id can't be empty");
|
1216
|
+
return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b);
|
1217
|
+
}
|
1218
|
+
if (isObject(a) && isString(a.id)) {
|
1219
|
+
if (a.id === "")
|
1220
|
+
throw new Error("The id can't be empty");
|
1221
|
+
return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 });
|
1222
|
+
}
|
1223
|
+
if (isObject(a)) {
|
1224
|
+
return __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a);
|
1225
|
+
}
|
1226
|
+
throw new Error("Invalid arguments for create method");
|
1227
|
+
}
|
1228
|
+
async read(recordId) {
|
1229
|
+
const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
|
1230
|
+
try {
|
1231
|
+
const response = await getRecord({
|
1232
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
|
1233
|
+
...fetchProps
|
1234
|
+
});
|
1235
|
+
return __privateMethod$2(this, _initObject, initObject_fn).call(this, __privateGet$2(this, _table), response);
|
1236
|
+
} catch (e) {
|
1237
|
+
if (isObject(e) && e.status === 404) {
|
1238
|
+
return null;
|
1239
|
+
}
|
1240
|
+
throw e;
|
1241
|
+
}
|
1242
|
+
}
|
1243
|
+
async update(a, b) {
|
1244
|
+
if (Array.isArray(a)) {
|
1245
|
+
if (a.length > 100) {
|
1246
|
+
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1247
|
+
}
|
1248
|
+
return Promise.all(a.map((object) => this.update(object)));
|
1249
|
+
}
|
1250
|
+
if (isString(a) && isObject(b)) {
|
1251
|
+
return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b);
|
1252
|
+
}
|
1253
|
+
if (isObject(a) && isString(a.id)) {
|
1254
|
+
return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
|
1255
|
+
}
|
1256
|
+
throw new Error("Invalid arguments for update method");
|
1257
|
+
}
|
1258
|
+
async createOrUpdate(a, b) {
|
1259
|
+
if (Array.isArray(a)) {
|
1260
|
+
if (a.length > 100) {
|
1261
|
+
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1262
|
+
}
|
1263
|
+
return Promise.all(a.map((object) => this.createOrUpdate(object)));
|
1264
|
+
}
|
1265
|
+
if (isString(a) && isObject(b)) {
|
1266
|
+
return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b);
|
1267
|
+
}
|
1268
|
+
if (isObject(a) && isString(a.id)) {
|
1269
|
+
return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
|
1270
|
+
}
|
1271
|
+
throw new Error("Invalid arguments for createOrUpdate method");
|
1272
|
+
}
|
1273
|
+
async delete(recordId) {
|
1274
|
+
if (Array.isArray(recordId)) {
|
1275
|
+
if (recordId.length > 100) {
|
1276
|
+
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
1277
|
+
}
|
1278
|
+
await Promise.all(recordId.map((id) => this.delete(id)));
|
1279
|
+
return;
|
1280
|
+
}
|
1281
|
+
if (isString(recordId)) {
|
1282
|
+
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, recordId);
|
1283
|
+
return;
|
1284
|
+
}
|
1285
|
+
if (isObject(recordId) && isString(recordId.id)) {
|
1286
|
+
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, recordId.id);
|
1287
|
+
return;
|
1288
|
+
}
|
1289
|
+
throw new Error("Invalid arguments for delete method");
|
1290
|
+
}
|
1291
|
+
async search(query, options = {}) {
|
1292
|
+
const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
|
1293
|
+
const { records } = await searchBranch({
|
1294
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1295
|
+
body: { tables: [__privateGet$2(this, _table)], query, fuzziness: options.fuzziness },
|
1296
|
+
...fetchProps
|
1297
|
+
});
|
1298
|
+
return records.map((item) => __privateMethod$2(this, _initObject, initObject_fn).call(this, __privateGet$2(this, _table), item));
|
1299
|
+
}
|
1300
|
+
async query(query) {
|
1301
|
+
var _a;
|
1302
|
+
const data = query.getQueryOptions();
|
1303
|
+
const body = {
|
1304
|
+
filter: Object.values((_a = data.filter) != null ? _a : {}).some(Boolean) ? data.filter : void 0,
|
1305
|
+
sort: data.sort ? buildSortFilter(data.sort) : void 0,
|
1306
|
+
page: data.page,
|
1307
|
+
columns: data.columns
|
1308
|
+
};
|
1309
|
+
const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
|
1310
|
+
const { meta, records: objects } = await queryTable({
|
1311
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table) },
|
1312
|
+
body,
|
1313
|
+
...fetchProps
|
1314
|
+
});
|
1315
|
+
const records = objects.map((record) => __privateMethod$2(this, _initObject, initObject_fn).call(this, __privateGet$2(this, _table), record));
|
1316
|
+
return new Page(query, meta, records);
|
1317
|
+
}
|
1318
|
+
}
|
1319
|
+
_table = new WeakMap();
|
1320
|
+
_links = new WeakMap();
|
1321
|
+
_getFetchProps = new WeakMap();
|
1322
|
+
_insertRecordWithoutId = new WeakSet();
|
1323
|
+
insertRecordWithoutId_fn = async function(object) {
|
1324
|
+
const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
|
1325
|
+
const record = transformObjectLinks(object);
|
1326
|
+
const response = await insertRecord({
|
1327
|
+
pathParams: {
|
1328
|
+
workspace: "{workspaceId}",
|
1329
|
+
dbBranchName: "{dbBranch}",
|
1330
|
+
tableName: __privateGet$2(this, _table)
|
1331
|
+
},
|
1332
|
+
body: record,
|
1333
|
+
...fetchProps
|
1334
|
+
});
|
1335
|
+
const finalObject = await this.read(response.id);
|
1336
|
+
if (!finalObject) {
|
1337
|
+
throw new Error("The server failed to save the record");
|
1338
|
+
}
|
1339
|
+
return finalObject;
|
1340
|
+
};
|
1341
|
+
_insertRecordWithId = new WeakSet();
|
1342
|
+
insertRecordWithId_fn = async function(recordId, object) {
|
1343
|
+
const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
|
1344
|
+
const record = transformObjectLinks(object);
|
1345
|
+
const response = await insertRecordWithID({
|
1346
|
+
pathParams: {
|
1347
|
+
workspace: "{workspaceId}",
|
1348
|
+
dbBranchName: "{dbBranch}",
|
1349
|
+
tableName: __privateGet$2(this, _table),
|
1350
|
+
recordId
|
1351
|
+
},
|
1352
|
+
body: record,
|
1353
|
+
queryParams: { createOnly: true },
|
1354
|
+
...fetchProps
|
1355
|
+
});
|
1356
|
+
const finalObject = await this.read(response.id);
|
1357
|
+
if (!finalObject) {
|
1358
|
+
throw new Error("The server failed to save the record");
|
1359
|
+
}
|
1360
|
+
return finalObject;
|
1361
|
+
};
|
1362
|
+
_bulkInsertTableRecords = new WeakSet();
|
1363
|
+
bulkInsertTableRecords_fn = async function(objects) {
|
1364
|
+
const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
|
1365
|
+
const records = objects.map((object) => transformObjectLinks(object));
|
1366
|
+
const response = await bulkInsertTableRecords({
|
1367
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table) },
|
1368
|
+
body: { records },
|
1369
|
+
...fetchProps
|
1370
|
+
});
|
1371
|
+
const finalObjects = await this.any(...response.recordIDs.map((id) => this.filter("id", id))).getAll();
|
1372
|
+
if (finalObjects.length !== objects.length) {
|
1373
|
+
throw new Error("The server failed to save some records");
|
1374
|
+
}
|
1375
|
+
return finalObjects;
|
1376
|
+
};
|
1377
|
+
_updateRecordWithID = new WeakSet();
|
1378
|
+
updateRecordWithID_fn = async function(recordId, object) {
|
1379
|
+
const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
|
1380
|
+
const record = transformObjectLinks(object);
|
1381
|
+
const response = await updateRecordWithID({
|
1382
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
|
1383
|
+
body: record,
|
1384
|
+
...fetchProps
|
1385
|
+
});
|
1386
|
+
const item = await this.read(response.id);
|
1387
|
+
if (!item)
|
1388
|
+
throw new Error("The server failed to save the record");
|
1389
|
+
return item;
|
1390
|
+
};
|
1391
|
+
_upsertRecordWithID = new WeakSet();
|
1392
|
+
upsertRecordWithID_fn = async function(recordId, object) {
|
1393
|
+
const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
|
1394
|
+
const response = await upsertRecordWithID({
|
1395
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
|
1396
|
+
body: object,
|
1397
|
+
...fetchProps
|
1398
|
+
});
|
1399
|
+
const item = await this.read(response.id);
|
1400
|
+
if (!item)
|
1401
|
+
throw new Error("The server failed to save the record");
|
1402
|
+
return item;
|
1403
|
+
};
|
1404
|
+
_deleteRecord = new WeakSet();
|
1405
|
+
deleteRecord_fn = async function(recordId) {
|
1406
|
+
const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
|
1407
|
+
await deleteRecord({
|
1408
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
|
1409
|
+
...fetchProps
|
1410
|
+
});
|
1411
|
+
};
|
1412
|
+
_initObject = new WeakSet();
|
1413
|
+
initObject_fn = function(table, object) {
|
1414
|
+
const result = {};
|
1415
|
+
Object.assign(result, object);
|
1416
|
+
const tableLinks = __privateGet$2(this, _links)[table] || [];
|
1417
|
+
for (const link of tableLinks) {
|
1418
|
+
const [field, linkTable] = link;
|
1419
|
+
const value = result[field];
|
1420
|
+
if (value && isObject(value)) {
|
1421
|
+
result[field] = __privateMethod$2(this, _initObject, initObject_fn).call(this, linkTable, value);
|
1422
|
+
}
|
1423
|
+
}
|
1424
|
+
const db = this.db;
|
1425
|
+
result.read = function() {
|
1426
|
+
return db[table].read(result["id"]);
|
1427
|
+
};
|
1428
|
+
result.update = function(data) {
|
1429
|
+
return db[table].update(result["id"], data);
|
1430
|
+
};
|
1431
|
+
result.delete = function() {
|
1432
|
+
return db[table].delete(result["id"]);
|
1433
|
+
};
|
1434
|
+
for (const prop of ["read", "update", "delete"]) {
|
1435
|
+
Object.defineProperty(result, prop, { enumerable: false });
|
1436
|
+
}
|
1437
|
+
Object.freeze(result);
|
1438
|
+
return result;
|
1439
|
+
};
|
1440
|
+
const transformObjectLinks = (object) => {
|
1441
|
+
return Object.entries(object).reduce((acc, [key, value]) => {
|
1442
|
+
if (key === "xata")
|
1443
|
+
return acc;
|
1444
|
+
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1445
|
+
}, {});
|
1446
|
+
};
|
1447
|
+
|
1448
|
+
const gt = (value) => ({ $gt: value });
|
1449
|
+
const ge = (value) => ({ $ge: value });
|
1450
|
+
const gte = (value) => ({ $ge: value });
|
1451
|
+
const lt = (value) => ({ $lt: value });
|
1452
|
+
const lte = (value) => ({ $le: value });
|
1453
|
+
const le = (value) => ({ $le: value });
|
1454
|
+
const exists = (column) => ({ $exists: column });
|
1455
|
+
const notExists = (column) => ({ $notExists: column });
|
1456
|
+
const startsWith = (value) => ({ $startsWith: value });
|
1457
|
+
const endsWith = (value) => ({ $endsWith: value });
|
1458
|
+
const pattern = (value) => ({ $pattern: value });
|
1459
|
+
const is = (value) => ({ $is: value });
|
1460
|
+
const isNot = (value) => ({ $isNot: value });
|
1461
|
+
const contains = (value) => ({ $contains: value });
|
1462
|
+
const includes = (value) => ({ $includes: value });
|
1463
|
+
const includesAll = (value) => ({ $includesAll: value });
|
1464
|
+
const includesNone = (value) => ({ $includesNone: value });
|
1465
|
+
const includesAny = (value) => ({ $includesAny: value });
|
1466
|
+
|
1467
|
+
var __accessCheck$2 = (obj, member, msg) => {
|
1468
|
+
if (!member.has(obj))
|
1469
|
+
throw TypeError("Cannot " + msg);
|
1470
|
+
};
|
1471
|
+
var __privateGet$1 = (obj, member, getter) => {
|
1472
|
+
__accessCheck$2(obj, member, "read from private field");
|
1473
|
+
return getter ? getter.call(obj) : member.get(obj);
|
1474
|
+
};
|
1475
|
+
var __privateAdd$2 = (obj, member, value) => {
|
1476
|
+
if (member.has(obj))
|
1477
|
+
throw TypeError("Cannot add the same private member more than once");
|
1478
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1479
|
+
};
|
1480
|
+
var _tables;
|
1481
|
+
class SchemaPlugin extends XataPlugin {
|
1482
|
+
constructor(links) {
|
1483
|
+
super();
|
1484
|
+
this.links = links;
|
1485
|
+
__privateAdd$2(this, _tables, {});
|
1486
|
+
}
|
1487
|
+
build(options) {
|
1488
|
+
const { getFetchProps } = options;
|
1489
|
+
const links = this.links;
|
1490
|
+
const db = new Proxy({}, {
|
1491
|
+
get: (_target, table) => {
|
1492
|
+
if (!isString(table))
|
1493
|
+
throw new Error("Invalid table name");
|
1494
|
+
if (!__privateGet$1(this, _tables)[table])
|
1495
|
+
__privateGet$1(this, _tables)[table] = new RestRepository({ db, getFetchProps, table, links });
|
1496
|
+
return __privateGet$1(this, _tables)[table];
|
1497
|
+
}
|
1498
|
+
});
|
1499
|
+
return db;
|
1500
|
+
}
|
1501
|
+
}
|
1502
|
+
_tables = new WeakMap();
|
1503
|
+
|
1504
|
+
var __accessCheck$1 = (obj, member, msg) => {
|
1505
|
+
if (!member.has(obj))
|
1506
|
+
throw TypeError("Cannot " + msg);
|
1507
|
+
};
|
1508
|
+
var __privateAdd$1 = (obj, member, value) => {
|
1509
|
+
if (member.has(obj))
|
1510
|
+
throw TypeError("Cannot add the same private member more than once");
|
1511
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1512
|
+
};
|
1513
|
+
var __privateMethod$1 = (obj, member, method) => {
|
1514
|
+
__accessCheck$1(obj, member, "access private method");
|
1515
|
+
return method;
|
1516
|
+
};
|
1517
|
+
var _search, search_fn;
|
1518
|
+
class SearchPlugin extends XataPlugin {
|
1519
|
+
constructor() {
|
1520
|
+
super(...arguments);
|
1521
|
+
__privateAdd$1(this, _search);
|
1522
|
+
}
|
1523
|
+
build({ getFetchProps }) {
|
1524
|
+
return {
|
1525
|
+
all: async (query, options = {}) => {
|
1526
|
+
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1527
|
+
return records.map((record) => {
|
1528
|
+
const { table = "orphan" } = record.xata;
|
1529
|
+
return { table, record };
|
1530
|
+
});
|
1531
|
+
},
|
1532
|
+
byTable: async (query, options = {}) => {
|
1533
|
+
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1534
|
+
return records.reduce((acc, record) => {
|
1535
|
+
var _a;
|
1536
|
+
const { table = "orphan" } = record.xata;
|
1537
|
+
const items = (_a = acc[table]) != null ? _a : [];
|
1538
|
+
return { ...acc, [table]: [...items, record] };
|
1539
|
+
}, {});
|
1540
|
+
}
|
1541
|
+
};
|
1542
|
+
}
|
1543
|
+
}
|
1544
|
+
_search = new WeakSet();
|
1545
|
+
search_fn = async function(query, options, getFetchProps) {
|
1546
|
+
const fetchProps = await getFetchProps();
|
1547
|
+
const { tables, fuzziness } = options != null ? options : {};
|
1548
|
+
const { records } = await searchBranch({
|
1549
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1550
|
+
body: { tables, query, fuzziness },
|
1551
|
+
...fetchProps
|
1552
|
+
});
|
1553
|
+
return records;
|
1554
|
+
};
|
1555
|
+
|
1556
|
+
const isBranchStrategyBuilder = (strategy) => {
|
1557
|
+
return typeof strategy === "function";
|
1558
|
+
};
|
1559
|
+
|
1560
|
+
var __accessCheck = (obj, member, msg) => {
|
1561
|
+
if (!member.has(obj))
|
1562
|
+
throw TypeError("Cannot " + msg);
|
1563
|
+
};
|
1564
|
+
var __privateGet = (obj, member, getter) => {
|
1565
|
+
__accessCheck(obj, member, "read from private field");
|
1566
|
+
return getter ? getter.call(obj) : member.get(obj);
|
1567
|
+
};
|
1568
|
+
var __privateAdd = (obj, member, value) => {
|
1569
|
+
if (member.has(obj))
|
1570
|
+
throw TypeError("Cannot add the same private member more than once");
|
1571
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1572
|
+
};
|
1573
|
+
var __privateSet = (obj, member, value, setter) => {
|
1574
|
+
__accessCheck(obj, member, "write to private field");
|
1575
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1576
|
+
return value;
|
1577
|
+
};
|
1578
|
+
var __privateMethod = (obj, member, method) => {
|
1579
|
+
__accessCheck(obj, member, "access private method");
|
1580
|
+
return method;
|
1581
|
+
};
|
1582
|
+
const buildClient = (plugins) => {
|
1583
|
+
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1584
|
+
return _a = class {
|
1585
|
+
constructor(options = {}, links) {
|
1586
|
+
__privateAdd(this, _parseOptions);
|
1587
|
+
__privateAdd(this, _getFetchProps);
|
1588
|
+
__privateAdd(this, _evaluateBranch);
|
1589
|
+
__privateAdd(this, _branch, void 0);
|
1590
|
+
const safeOptions = __privateMethod(this, _parseOptions, parseOptions_fn).call(this, options);
|
1591
|
+
const namespaces = {
|
1592
|
+
db: new SchemaPlugin(links),
|
1593
|
+
search: new SearchPlugin(),
|
1594
|
+
...plugins
|
1595
|
+
};
|
1596
|
+
for (const [key, namespace] of Object.entries(namespaces)) {
|
1597
|
+
if (!namespace)
|
1598
|
+
continue;
|
1599
|
+
const result = namespace.build({ getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions) });
|
1600
|
+
if (result instanceof Promise) {
|
1601
|
+
void result.then((namespace2) => {
|
1602
|
+
this[key] = namespace2;
|
1603
|
+
});
|
1604
|
+
} else {
|
1605
|
+
this[key] = result;
|
1606
|
+
}
|
1607
|
+
}
|
1608
|
+
}
|
1609
|
+
}, _branch = new WeakMap(), _parseOptions = new WeakSet(), parseOptions_fn = function(options) {
|
1610
|
+
const fetch = getFetchImplementation(options == null ? void 0 : options.fetch);
|
1611
|
+
const databaseURL = (options == null ? void 0 : options.databaseURL) || getDatabaseURL();
|
1612
|
+
const apiKey = (options == null ? void 0 : options.apiKey) || getAPIKey();
|
1613
|
+
const branch = async () => (options == null ? void 0 : options.branch) ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options == null ? void 0 : options.fetch });
|
1614
|
+
if (!databaseURL || !apiKey) {
|
1615
|
+
throw new Error("Options databaseURL and apiKey are required");
|
1616
|
+
}
|
1617
|
+
return { fetch, databaseURL, apiKey, branch };
|
1618
|
+
}, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({
|
1619
|
+
fetch,
|
1620
|
+
apiKey,
|
1621
|
+
databaseURL,
|
1622
|
+
branch
|
1623
|
+
}) {
|
1624
|
+
const branchValue = await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, branch);
|
1625
|
+
if (!branchValue)
|
1626
|
+
throw new Error("Unable to resolve branch value");
|
1627
|
+
return {
|
1628
|
+
fetchImpl: fetch,
|
1629
|
+
apiKey,
|
1630
|
+
apiUrl: "",
|
1631
|
+
workspacesApiUrl: (path, params) => {
|
1632
|
+
var _a2;
|
1633
|
+
const hasBranch = (_a2 = params.dbBranchName) != null ? _a2 : params.branch;
|
1634
|
+
const newPath = path.replace(/^\/db\/[^/]+/, hasBranch ? `:${branchValue}` : "");
|
1635
|
+
return databaseURL + newPath;
|
1636
|
+
}
|
1637
|
+
};
|
1638
|
+
}, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
|
1639
|
+
if (__privateGet(this, _branch))
|
1640
|
+
return __privateGet(this, _branch);
|
1641
|
+
if (!param)
|
1642
|
+
return void 0;
|
1643
|
+
const strategies = Array.isArray(param) ? [...param] : [param];
|
1644
|
+
const evaluateBranch = async (strategy) => {
|
1645
|
+
return isBranchStrategyBuilder(strategy) ? await strategy() : strategy;
|
1646
|
+
};
|
1647
|
+
for await (const strategy of strategies) {
|
1648
|
+
const branch = await evaluateBranch(strategy);
|
1649
|
+
if (branch) {
|
1650
|
+
__privateSet(this, _branch, branch);
|
1651
|
+
return branch;
|
1652
|
+
}
|
1653
|
+
}
|
1654
|
+
}, _a;
|
1655
|
+
};
|
1656
|
+
class BaseClient extends buildClient() {
|
1657
|
+
}
|
1658
|
+
|
1659
|
+
class XataError extends Error {
|
1660
|
+
constructor(message, status) {
|
1661
|
+
super(message);
|
1662
|
+
this.status = status;
|
1663
|
+
}
|
1664
|
+
}
|
1665
|
+
|
1666
|
+
export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, Repository, RestRepository, SchemaPlugin, SearchPlugin, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addTableColumn, buildClient, bulkInsertTableRecords, cancelWorkspaceMemberInvite, contains, createBranch, createDatabase, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, endsWith, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseURL, getRecord, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getWorkspace, getWorkspaceMembersList, getWorkspacesList, gt, gte, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isIdentifiable, isNot, isXataRecord, le, lt, lte, notExists, operationsByTag, pattern, queryTable, removeWorkspaceMember, resendWorkspaceMemberInvite, searchBranch, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
|
1667
|
+
//# sourceMappingURL=index.mjs.map
|