@xata.io/client 0.8.3 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +31 -0
- package/dist/index.cjs +528 -337
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +784 -38
- package/dist/index.mjs +524 -338
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/rollup.config.js +6 -10
- package/tsconfig.json +5 -4
package/dist/index.mjs
CHANGED
@@ -10,17 +10,23 @@ function isObject(value) {
|
|
10
10
|
function isString(value) {
|
11
11
|
return value !== void 0 && value !== null && typeof value === "string";
|
12
12
|
}
|
13
|
+
function toBase64(value) {
|
14
|
+
try {
|
15
|
+
return btoa(value);
|
16
|
+
} catch (err) {
|
17
|
+
return Buffer.from(value).toString("base64");
|
18
|
+
}
|
19
|
+
}
|
13
20
|
|
14
21
|
function getEnvVariable(name) {
|
15
|
-
var _a, _b;
|
16
22
|
try {
|
17
|
-
if (isObject(process) && isString(
|
23
|
+
if (isObject(process) && isString(process?.env?.[name])) {
|
18
24
|
return process.env[name];
|
19
25
|
}
|
20
26
|
} catch (err) {
|
21
27
|
}
|
22
28
|
try {
|
23
|
-
if (isObject(Deno) && isString(
|
29
|
+
if (isObject(Deno) && isString(Deno?.env?.get(name))) {
|
24
30
|
return Deno.env.get(name);
|
25
31
|
}
|
26
32
|
} catch (err) {
|
@@ -28,7 +34,10 @@ function getEnvVariable(name) {
|
|
28
34
|
}
|
29
35
|
async function getGitBranch() {
|
30
36
|
try {
|
31
|
-
|
37
|
+
if (typeof require === "function") {
|
38
|
+
const req = require;
|
39
|
+
return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
|
40
|
+
}
|
32
41
|
} catch (err) {
|
33
42
|
}
|
34
43
|
try {
|
@@ -44,99 +53,51 @@ async function getGitBranch() {
|
|
44
53
|
}
|
45
54
|
}
|
46
55
|
|
56
|
+
function getAPIKey() {
|
57
|
+
try {
|
58
|
+
return getEnvVariable("XATA_API_KEY") ?? XATA_API_KEY;
|
59
|
+
} catch (err) {
|
60
|
+
return void 0;
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
47
64
|
function getFetchImplementation(userFetch) {
|
48
65
|
const globalFetch = typeof fetch !== "undefined" ? fetch : void 0;
|
49
|
-
const fetchImpl = userFetch
|
66
|
+
const fetchImpl = userFetch ?? globalFetch;
|
50
67
|
if (!fetchImpl) {
|
51
68
|
throw new Error(`The \`fetch\` option passed to the Xata client is resolving to a falsy value and may not be correctly imported.`);
|
52
69
|
}
|
53
70
|
return fetchImpl;
|
54
71
|
}
|
55
72
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
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;
|
73
|
+
class FetcherError extends Error {
|
74
|
+
constructor(status, data) {
|
75
|
+
super(getMessage(data));
|
76
|
+
this.status = status;
|
77
|
+
this.errors = isBulkError(data) ? data.errors : void 0;
|
78
|
+
if (data instanceof Error) {
|
79
|
+
this.stack = data.stack;
|
80
|
+
this.cause = data.cause;
|
119
81
|
}
|
120
82
|
}
|
121
|
-
try {
|
122
|
-
return XATA_BRANCH;
|
123
|
-
} catch (err) {
|
124
|
-
}
|
125
83
|
}
|
126
|
-
function
|
127
|
-
|
128
|
-
try {
|
129
|
-
return (_a = getEnvVariable("XATA_DATABASE_URL")) != null ? _a : XATA_DATABASE_URL;
|
130
|
-
} catch (err) {
|
131
|
-
return void 0;
|
132
|
-
}
|
84
|
+
function isBulkError(error) {
|
85
|
+
return isObject(error) && Array.isArray(error.errors);
|
133
86
|
}
|
134
|
-
function
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
return
|
87
|
+
function isErrorWithMessage(error) {
|
88
|
+
return isObject(error) && isString(error.message);
|
89
|
+
}
|
90
|
+
function getMessage(data) {
|
91
|
+
if (data instanceof Error) {
|
92
|
+
return data.message;
|
93
|
+
} else if (isString(data)) {
|
94
|
+
return data;
|
95
|
+
} else if (isErrorWithMessage(data)) {
|
96
|
+
return data.message;
|
97
|
+
} else if (isBulkError(data)) {
|
98
|
+
return "Bulk operation failed";
|
99
|
+
} else {
|
100
|
+
return "Unexpected error";
|
140
101
|
}
|
141
102
|
}
|
142
103
|
|
@@ -151,16 +112,15 @@ function buildBaseUrl({
|
|
151
112
|
apiUrl,
|
152
113
|
pathParams
|
153
114
|
}) {
|
154
|
-
if (!
|
115
|
+
if (!pathParams?.workspace)
|
155
116
|
return `${apiUrl}${path}`;
|
156
117
|
const url = typeof workspacesApiUrl === "string" ? `${workspacesApiUrl}${path}` : workspacesApiUrl(path, pathParams);
|
157
118
|
return url.replace("{workspaceId}", pathParams.workspace);
|
158
119
|
}
|
159
120
|
function hostHeader(url) {
|
160
|
-
var _a;
|
161
121
|
const pattern = /.*:\/\/(?<host>[^/]+).*/;
|
162
|
-
const { groups } =
|
163
|
-
return
|
122
|
+
const { groups } = pattern.exec(url) ?? {};
|
123
|
+
return groups?.host ? { Host: groups.host } : {};
|
164
124
|
}
|
165
125
|
async function fetch$1({
|
166
126
|
url: path,
|
@@ -195,33 +155,20 @@ async function fetch$1({
|
|
195
155
|
if (response.ok) {
|
196
156
|
return jsonResponse;
|
197
157
|
}
|
198
|
-
|
199
|
-
throw new FetcherError({ message, status: response.status, errors });
|
158
|
+
throw new FetcherError(response.status, jsonResponse);
|
200
159
|
} catch (error) {
|
201
|
-
|
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
|
-
}
|
160
|
+
throw new FetcherError(response.status, error);
|
218
161
|
}
|
219
162
|
}
|
220
163
|
|
221
164
|
const getUser = (variables) => fetch$1({ url: "/user", method: "get", ...variables });
|
222
165
|
const updateUser = (variables) => fetch$1({ url: "/user", method: "put", ...variables });
|
223
166
|
const deleteUser = (variables) => fetch$1({ url: "/user", method: "delete", ...variables });
|
224
|
-
const getUserAPIKeys = (variables) => fetch$1({
|
167
|
+
const getUserAPIKeys = (variables) => fetch$1({
|
168
|
+
url: "/user/keys",
|
169
|
+
method: "get",
|
170
|
+
...variables
|
171
|
+
});
|
225
172
|
const createUserAPIKey = (variables) => fetch$1({
|
226
173
|
url: "/user/keys/{keyName}",
|
227
174
|
method: "post",
|
@@ -232,8 +179,16 @@ const deleteUserAPIKey = (variables) => fetch$1({
|
|
232
179
|
method: "delete",
|
233
180
|
...variables
|
234
181
|
});
|
235
|
-
const createWorkspace = (variables) => fetch$1({
|
236
|
-
|
182
|
+
const createWorkspace = (variables) => fetch$1({
|
183
|
+
url: "/workspaces",
|
184
|
+
method: "post",
|
185
|
+
...variables
|
186
|
+
});
|
187
|
+
const getWorkspacesList = (variables) => fetch$1({
|
188
|
+
url: "/workspaces",
|
189
|
+
method: "get",
|
190
|
+
...variables
|
191
|
+
});
|
237
192
|
const getWorkspace = (variables) => fetch$1({
|
238
193
|
url: "/workspaces/{workspaceId}",
|
239
194
|
method: "get",
|
@@ -254,21 +209,13 @@ const getWorkspaceMembersList = (variables) => fetch$1({
|
|
254
209
|
method: "get",
|
255
210
|
...variables
|
256
211
|
});
|
257
|
-
const updateWorkspaceMemberRole = (variables) => fetch$1({
|
258
|
-
url: "/workspaces/{workspaceId}/members/{userId}",
|
259
|
-
method: "put",
|
260
|
-
...variables
|
261
|
-
});
|
212
|
+
const updateWorkspaceMemberRole = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/members/{userId}", method: "put", ...variables });
|
262
213
|
const removeWorkspaceMember = (variables) => fetch$1({
|
263
214
|
url: "/workspaces/{workspaceId}/members/{userId}",
|
264
215
|
method: "delete",
|
265
216
|
...variables
|
266
217
|
});
|
267
|
-
const inviteWorkspaceMember = (variables) => fetch$1({
|
268
|
-
url: "/workspaces/{workspaceId}/invites",
|
269
|
-
method: "post",
|
270
|
-
...variables
|
271
|
-
});
|
218
|
+
const inviteWorkspaceMember = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/invites", method: "post", ...variables });
|
272
219
|
const cancelWorkspaceMemberInvite = (variables) => fetch$1({
|
273
220
|
url: "/workspaces/{workspaceId}/invites/{inviteId}",
|
274
221
|
method: "delete",
|
@@ -304,6 +251,14 @@ const deleteDatabase = (variables) => fetch$1({
|
|
304
251
|
method: "delete",
|
305
252
|
...variables
|
306
253
|
});
|
254
|
+
const getGitBranchesMapping = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "get", ...variables });
|
255
|
+
const addGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "post", ...variables });
|
256
|
+
const removeGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "delete", ...variables });
|
257
|
+
const resolveBranch = (variables) => fetch$1({
|
258
|
+
url: "/dbs/{dbName}/resolveBranch",
|
259
|
+
method: "get",
|
260
|
+
...variables
|
261
|
+
});
|
307
262
|
const getBranchDetails = (variables) => fetch$1({
|
308
263
|
url: "/db/{dbBranchName}",
|
309
264
|
method: "get",
|
@@ -330,16 +285,8 @@ const getBranchMetadata = (variables) => fetch$1({
|
|
330
285
|
...variables
|
331
286
|
});
|
332
287
|
const getBranchMigrationHistory = (variables) => fetch$1({ url: "/db/{dbBranchName}/migrations", method: "get", ...variables });
|
333
|
-
const executeBranchMigrationPlan = (variables) => fetch$1({
|
334
|
-
|
335
|
-
method: "post",
|
336
|
-
...variables
|
337
|
-
});
|
338
|
-
const getBranchMigrationPlan = (variables) => fetch$1({
|
339
|
-
url: "/db/{dbBranchName}/migrations/plan",
|
340
|
-
method: "post",
|
341
|
-
...variables
|
342
|
-
});
|
288
|
+
const executeBranchMigrationPlan = (variables) => fetch$1({ url: "/db/{dbBranchName}/migrations/execute", method: "post", ...variables });
|
289
|
+
const getBranchMigrationPlan = (variables) => fetch$1({ url: "/db/{dbBranchName}/migrations/plan", method: "post", ...variables });
|
343
290
|
const getBranchStats = (variables) => fetch$1({
|
344
291
|
url: "/db/{dbBranchName}/stats",
|
345
292
|
method: "get",
|
@@ -413,11 +360,7 @@ const getRecord = (variables) => fetch$1({
|
|
413
360
|
method: "get",
|
414
361
|
...variables
|
415
362
|
});
|
416
|
-
const bulkInsertTableRecords = (variables) => fetch$1({
|
417
|
-
url: "/db/{dbBranchName}/tables/{tableName}/bulk",
|
418
|
-
method: "post",
|
419
|
-
...variables
|
420
|
-
});
|
363
|
+
const bulkInsertTableRecords = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/bulk", method: "post", ...variables });
|
421
364
|
const queryTable = (variables) => fetch$1({
|
422
365
|
url: "/db/{dbBranchName}/tables/{tableName}/query",
|
423
366
|
method: "post",
|
@@ -444,7 +387,15 @@ const operationsByTag = {
|
|
444
387
|
resendWorkspaceMemberInvite,
|
445
388
|
acceptWorkspaceMemberInvite
|
446
389
|
},
|
447
|
-
database: {
|
390
|
+
database: {
|
391
|
+
getDatabaseList,
|
392
|
+
createDatabase,
|
393
|
+
deleteDatabase,
|
394
|
+
getGitBranchesMapping,
|
395
|
+
addGitBranchesEntry,
|
396
|
+
removeGitBranchesEntry,
|
397
|
+
resolveBranch
|
398
|
+
},
|
448
399
|
branch: {
|
449
400
|
getBranchList,
|
450
401
|
getBranchDetails,
|
@@ -507,36 +458,35 @@ function isValidBuilder(builder) {
|
|
507
458
|
return isObject(builder) && isString(builder.main) && isString(builder.workspaces);
|
508
459
|
}
|
509
460
|
|
510
|
-
var __accessCheck$
|
461
|
+
var __accessCheck$7 = (obj, member, msg) => {
|
511
462
|
if (!member.has(obj))
|
512
463
|
throw TypeError("Cannot " + msg);
|
513
464
|
};
|
514
|
-
var __privateGet$
|
515
|
-
__accessCheck$
|
465
|
+
var __privateGet$6 = (obj, member, getter) => {
|
466
|
+
__accessCheck$7(obj, member, "read from private field");
|
516
467
|
return getter ? getter.call(obj) : member.get(obj);
|
517
468
|
};
|
518
|
-
var __privateAdd$
|
469
|
+
var __privateAdd$7 = (obj, member, value) => {
|
519
470
|
if (member.has(obj))
|
520
471
|
throw TypeError("Cannot add the same private member more than once");
|
521
472
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
522
473
|
};
|
523
|
-
var __privateSet$
|
524
|
-
__accessCheck$
|
474
|
+
var __privateSet$5 = (obj, member, value, setter) => {
|
475
|
+
__accessCheck$7(obj, member, "write to private field");
|
525
476
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
526
477
|
return value;
|
527
478
|
};
|
528
479
|
var _extraProps, _namespaces;
|
529
480
|
class XataApiClient {
|
530
|
-
constructor(options) {
|
531
|
-
__privateAdd$
|
532
|
-
__privateAdd$
|
533
|
-
|
534
|
-
const
|
535
|
-
const apiKey = (_b = options == null ? void 0 : options.apiKey) != null ? _b : getAPIKey();
|
481
|
+
constructor(options = {}) {
|
482
|
+
__privateAdd$7(this, _extraProps, void 0);
|
483
|
+
__privateAdd$7(this, _namespaces, {});
|
484
|
+
const provider = options.host ?? "production";
|
485
|
+
const apiKey = options?.apiKey ?? getAPIKey();
|
536
486
|
if (!apiKey) {
|
537
487
|
throw new Error("Could not resolve a valid apiKey");
|
538
488
|
}
|
539
|
-
__privateSet$
|
489
|
+
__privateSet$5(this, _extraProps, {
|
540
490
|
apiUrl: getHostUrl(provider, "main"),
|
541
491
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
542
492
|
fetchImpl: getFetchImplementation(options.fetch),
|
@@ -544,34 +494,34 @@ class XataApiClient {
|
|
544
494
|
});
|
545
495
|
}
|
546
496
|
get user() {
|
547
|
-
if (!__privateGet$
|
548
|
-
__privateGet$
|
549
|
-
return __privateGet$
|
497
|
+
if (!__privateGet$6(this, _namespaces).user)
|
498
|
+
__privateGet$6(this, _namespaces).user = new UserApi(__privateGet$6(this, _extraProps));
|
499
|
+
return __privateGet$6(this, _namespaces).user;
|
550
500
|
}
|
551
501
|
get workspaces() {
|
552
|
-
if (!__privateGet$
|
553
|
-
__privateGet$
|
554
|
-
return __privateGet$
|
502
|
+
if (!__privateGet$6(this, _namespaces).workspaces)
|
503
|
+
__privateGet$6(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$6(this, _extraProps));
|
504
|
+
return __privateGet$6(this, _namespaces).workspaces;
|
555
505
|
}
|
556
506
|
get databases() {
|
557
|
-
if (!__privateGet$
|
558
|
-
__privateGet$
|
559
|
-
return __privateGet$
|
507
|
+
if (!__privateGet$6(this, _namespaces).databases)
|
508
|
+
__privateGet$6(this, _namespaces).databases = new DatabaseApi(__privateGet$6(this, _extraProps));
|
509
|
+
return __privateGet$6(this, _namespaces).databases;
|
560
510
|
}
|
561
511
|
get branches() {
|
562
|
-
if (!__privateGet$
|
563
|
-
__privateGet$
|
564
|
-
return __privateGet$
|
512
|
+
if (!__privateGet$6(this, _namespaces).branches)
|
513
|
+
__privateGet$6(this, _namespaces).branches = new BranchApi(__privateGet$6(this, _extraProps));
|
514
|
+
return __privateGet$6(this, _namespaces).branches;
|
565
515
|
}
|
566
516
|
get tables() {
|
567
|
-
if (!__privateGet$
|
568
|
-
__privateGet$
|
569
|
-
return __privateGet$
|
517
|
+
if (!__privateGet$6(this, _namespaces).tables)
|
518
|
+
__privateGet$6(this, _namespaces).tables = new TableApi(__privateGet$6(this, _extraProps));
|
519
|
+
return __privateGet$6(this, _namespaces).tables;
|
570
520
|
}
|
571
521
|
get records() {
|
572
|
-
if (!__privateGet$
|
573
|
-
__privateGet$
|
574
|
-
return __privateGet$
|
522
|
+
if (!__privateGet$6(this, _namespaces).records)
|
523
|
+
__privateGet$6(this, _namespaces).records = new RecordsApi(__privateGet$6(this, _extraProps));
|
524
|
+
return __privateGet$6(this, _namespaces).records;
|
575
525
|
}
|
576
526
|
}
|
577
527
|
_extraProps = new WeakMap();
|
@@ -705,6 +655,33 @@ class DatabaseApi {
|
|
705
655
|
...this.extraProps
|
706
656
|
});
|
707
657
|
}
|
658
|
+
getGitBranchesMapping(workspace, dbName) {
|
659
|
+
return operationsByTag.database.getGitBranchesMapping({
|
660
|
+
pathParams: { workspace, dbName },
|
661
|
+
...this.extraProps
|
662
|
+
});
|
663
|
+
}
|
664
|
+
addGitBranchesEntry(workspace, dbName, body) {
|
665
|
+
return operationsByTag.database.addGitBranchesEntry({
|
666
|
+
pathParams: { workspace, dbName },
|
667
|
+
body,
|
668
|
+
...this.extraProps
|
669
|
+
});
|
670
|
+
}
|
671
|
+
removeGitBranchesEntry(workspace, dbName, gitBranch) {
|
672
|
+
return operationsByTag.database.removeGitBranchesEntry({
|
673
|
+
pathParams: { workspace, dbName },
|
674
|
+
queryParams: { gitBranch },
|
675
|
+
...this.extraProps
|
676
|
+
});
|
677
|
+
}
|
678
|
+
resolveBranch(workspace, dbName, gitBranch) {
|
679
|
+
return operationsByTag.database.resolveBranch({
|
680
|
+
pathParams: { workspace, dbName },
|
681
|
+
queryParams: { gitBranch },
|
682
|
+
...this.extraProps
|
683
|
+
});
|
684
|
+
}
|
708
685
|
}
|
709
686
|
class BranchApi {
|
710
687
|
constructor(extraProps) {
|
@@ -926,43 +903,43 @@ class XataApiPlugin {
|
|
926
903
|
class XataPlugin {
|
927
904
|
}
|
928
905
|
|
929
|
-
var __accessCheck$
|
906
|
+
var __accessCheck$6 = (obj, member, msg) => {
|
930
907
|
if (!member.has(obj))
|
931
908
|
throw TypeError("Cannot " + msg);
|
932
909
|
};
|
933
|
-
var __privateGet$
|
934
|
-
__accessCheck$
|
910
|
+
var __privateGet$5 = (obj, member, getter) => {
|
911
|
+
__accessCheck$6(obj, member, "read from private field");
|
935
912
|
return getter ? getter.call(obj) : member.get(obj);
|
936
913
|
};
|
937
|
-
var __privateAdd$
|
914
|
+
var __privateAdd$6 = (obj, member, value) => {
|
938
915
|
if (member.has(obj))
|
939
916
|
throw TypeError("Cannot add the same private member more than once");
|
940
917
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
941
918
|
};
|
942
|
-
var __privateSet$
|
943
|
-
__accessCheck$
|
919
|
+
var __privateSet$4 = (obj, member, value, setter) => {
|
920
|
+
__accessCheck$6(obj, member, "write to private field");
|
944
921
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
945
922
|
return value;
|
946
923
|
};
|
947
924
|
var _query;
|
948
925
|
class Page {
|
949
926
|
constructor(query, meta, records = []) {
|
950
|
-
__privateAdd$
|
951
|
-
__privateSet$
|
927
|
+
__privateAdd$6(this, _query, void 0);
|
928
|
+
__privateSet$4(this, _query, query);
|
952
929
|
this.meta = meta;
|
953
930
|
this.records = records;
|
954
931
|
}
|
955
932
|
async nextPage(size, offset) {
|
956
|
-
return __privateGet$
|
933
|
+
return __privateGet$5(this, _query).getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
|
957
934
|
}
|
958
935
|
async previousPage(size, offset) {
|
959
|
-
return __privateGet$
|
936
|
+
return __privateGet$5(this, _query).getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
|
960
937
|
}
|
961
938
|
async firstPage(size, offset) {
|
962
|
-
return __privateGet$
|
939
|
+
return __privateGet$5(this, _query).getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
|
963
940
|
}
|
964
941
|
async lastPage(size, offset) {
|
965
|
-
return __privateGet$
|
942
|
+
return __privateGet$5(this, _query).getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
|
966
943
|
}
|
967
944
|
hasNextPage() {
|
968
945
|
return this.meta.page.more;
|
@@ -974,47 +951,47 @@ const PAGINATION_DEFAULT_SIZE = 200;
|
|
974
951
|
const PAGINATION_MAX_OFFSET = 800;
|
975
952
|
const PAGINATION_DEFAULT_OFFSET = 0;
|
976
953
|
|
977
|
-
var __accessCheck$
|
954
|
+
var __accessCheck$5 = (obj, member, msg) => {
|
978
955
|
if (!member.has(obj))
|
979
956
|
throw TypeError("Cannot " + msg);
|
980
957
|
};
|
981
|
-
var __privateGet$
|
982
|
-
__accessCheck$
|
958
|
+
var __privateGet$4 = (obj, member, getter) => {
|
959
|
+
__accessCheck$5(obj, member, "read from private field");
|
983
960
|
return getter ? getter.call(obj) : member.get(obj);
|
984
961
|
};
|
985
|
-
var __privateAdd$
|
962
|
+
var __privateAdd$5 = (obj, member, value) => {
|
986
963
|
if (member.has(obj))
|
987
964
|
throw TypeError("Cannot add the same private member more than once");
|
988
965
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
989
966
|
};
|
990
|
-
var __privateSet$
|
991
|
-
__accessCheck$
|
967
|
+
var __privateSet$3 = (obj, member, value, setter) => {
|
968
|
+
__accessCheck$5(obj, member, "write to private field");
|
992
969
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
993
970
|
return value;
|
994
971
|
};
|
995
972
|
var _table$1, _repository, _data;
|
996
973
|
const _Query = class {
|
997
974
|
constructor(repository, table, data, parent) {
|
998
|
-
__privateAdd$
|
999
|
-
__privateAdd$
|
1000
|
-
__privateAdd$
|
975
|
+
__privateAdd$5(this, _table$1, void 0);
|
976
|
+
__privateAdd$5(this, _repository, void 0);
|
977
|
+
__privateAdd$5(this, _data, { filter: {} });
|
1001
978
|
this.meta = { page: { cursor: "start", more: true } };
|
1002
979
|
this.records = [];
|
1003
|
-
|
1004
|
-
__privateSet$2(this, _table$1, table);
|
980
|
+
__privateSet$3(this, _table$1, table);
|
1005
981
|
if (repository) {
|
1006
|
-
__privateSet$
|
982
|
+
__privateSet$3(this, _repository, repository);
|
1007
983
|
} else {
|
1008
|
-
__privateSet$
|
984
|
+
__privateSet$3(this, _repository, this);
|
1009
985
|
}
|
1010
|
-
__privateGet$
|
1011
|
-
__privateGet$
|
1012
|
-
__privateGet$
|
1013
|
-
__privateGet$
|
1014
|
-
__privateGet$
|
1015
|
-
__privateGet$
|
1016
|
-
__privateGet$
|
1017
|
-
__privateGet$
|
986
|
+
__privateGet$4(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
987
|
+
__privateGet$4(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
|
988
|
+
__privateGet$4(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
|
989
|
+
__privateGet$4(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
|
990
|
+
__privateGet$4(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
|
991
|
+
__privateGet$4(this, _data).sort = data.sort ?? parent?.sort;
|
992
|
+
__privateGet$4(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
|
993
|
+
__privateGet$4(this, _data).page = data.page ?? parent?.page;
|
994
|
+
__privateGet$4(this, _data).cache = data.cache ?? parent?.cache;
|
1018
995
|
this.any = this.any.bind(this);
|
1019
996
|
this.all = this.all.bind(this);
|
1020
997
|
this.not = this.not.bind(this);
|
@@ -1025,59 +1002,50 @@ const _Query = class {
|
|
1025
1002
|
Object.defineProperty(this, "repository", { enumerable: false });
|
1026
1003
|
}
|
1027
1004
|
getQueryOptions() {
|
1028
|
-
return __privateGet$
|
1005
|
+
return __privateGet$4(this, _data);
|
1006
|
+
}
|
1007
|
+
key() {
|
1008
|
+
const { columns = [], filter = {}, sort = [], page = {} } = __privateGet$4(this, _data);
|
1009
|
+
const key = JSON.stringify({ columns, filter, sort, page });
|
1010
|
+
return toBase64(key);
|
1029
1011
|
}
|
1030
1012
|
any(...queries) {
|
1031
|
-
const $any = queries.map((query) => {
|
1032
|
-
|
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));
|
1013
|
+
const $any = queries.map((query) => query.getQueryOptions().filter ?? {});
|
1014
|
+
return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $any } }, __privateGet$4(this, _data));
|
1036
1015
|
}
|
1037
1016
|
all(...queries) {
|
1038
|
-
const $all = queries.map((query) => {
|
1039
|
-
|
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));
|
1017
|
+
const $all = queries.map((query) => query.getQueryOptions().filter ?? {});
|
1018
|
+
return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $all } }, __privateGet$4(this, _data));
|
1043
1019
|
}
|
1044
1020
|
not(...queries) {
|
1045
|
-
const $not = queries.map((query) => {
|
1046
|
-
|
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));
|
1021
|
+
const $not = queries.map((query) => query.getQueryOptions().filter ?? {});
|
1022
|
+
return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $not } }, __privateGet$4(this, _data));
|
1050
1023
|
}
|
1051
1024
|
none(...queries) {
|
1052
|
-
const $none = queries.map((query) => {
|
1053
|
-
|
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));
|
1025
|
+
const $none = queries.map((query) => query.getQueryOptions().filter ?? {});
|
1026
|
+
return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $none } }, __privateGet$4(this, _data));
|
1057
1027
|
}
|
1058
1028
|
filter(a, b) {
|
1059
|
-
var _a, _b;
|
1060
1029
|
if (arguments.length === 1) {
|
1061
1030
|
const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
|
1062
|
-
const $all = compact([
|
1063
|
-
return new _Query(__privateGet$
|
1031
|
+
const $all = compact([__privateGet$4(this, _data).filter?.$all].flat().concat(constraints));
|
1032
|
+
return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $all } }, __privateGet$4(this, _data));
|
1064
1033
|
} else {
|
1065
|
-
const $all = compact([
|
1066
|
-
return new _Query(__privateGet$
|
1034
|
+
const $all = compact([__privateGet$4(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
|
1035
|
+
return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $all } }, __privateGet$4(this, _data));
|
1067
1036
|
}
|
1068
1037
|
}
|
1069
1038
|
sort(column, direction) {
|
1070
|
-
|
1071
|
-
const originalSort = [(_a = __privateGet$3(this, _data).sort) != null ? _a : []].flat();
|
1039
|
+
const originalSort = [__privateGet$4(this, _data).sort ?? []].flat();
|
1072
1040
|
const sort = [...originalSort, { column, direction }];
|
1073
|
-
return new _Query(__privateGet$
|
1041
|
+
return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { sort }, __privateGet$4(this, _data));
|
1074
1042
|
}
|
1075
1043
|
select(columns) {
|
1076
|
-
return new _Query(__privateGet$
|
1044
|
+
return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { columns }, __privateGet$4(this, _data));
|
1077
1045
|
}
|
1078
1046
|
getPaginated(options = {}) {
|
1079
|
-
const query = new _Query(__privateGet$
|
1080
|
-
return __privateGet$
|
1047
|
+
const query = new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), options, __privateGet$4(this, _data));
|
1048
|
+
return __privateGet$4(this, _repository).query(query);
|
1081
1049
|
}
|
1082
1050
|
async *[Symbol.asyncIterator]() {
|
1083
1051
|
for await (const [record] of this.getIterator(1)) {
|
@@ -1105,10 +1073,13 @@ const _Query = class {
|
|
1105
1073
|
}
|
1106
1074
|
return results;
|
1107
1075
|
}
|
1108
|
-
async
|
1076
|
+
async getFirst(options = {}) {
|
1109
1077
|
const records = await this.getMany({ ...options, page: { size: 1 } });
|
1110
1078
|
return records[0] || null;
|
1111
1079
|
}
|
1080
|
+
cache(ttl) {
|
1081
|
+
return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { cache: ttl }, __privateGet$4(this, _data));
|
1082
|
+
}
|
1112
1083
|
nextPage(size, offset) {
|
1113
1084
|
return this.firstPage(size, offset);
|
1114
1085
|
}
|
@@ -1131,11 +1102,10 @@ _repository = new WeakMap();
|
|
1131
1102
|
_data = new WeakMap();
|
1132
1103
|
|
1133
1104
|
function isIdentifiable(x) {
|
1134
|
-
return isObject(x) && isString(x
|
1105
|
+
return isObject(x) && isString(x?.id);
|
1135
1106
|
}
|
1136
1107
|
function isXataRecord(x) {
|
1137
|
-
|
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";
|
1108
|
+
return isIdentifiable(x) && typeof x?.xata === "object" && typeof x?.xata?.version === "number";
|
1139
1109
|
}
|
1140
1110
|
|
1141
1111
|
function isSortFilterString(value) {
|
@@ -1148,7 +1118,6 @@ function isSortFilterObject(filter) {
|
|
1148
1118
|
return isObject(filter) && !isSortFilterBase(filter) && filter.column !== void 0;
|
1149
1119
|
}
|
1150
1120
|
function buildSortFilter(filter) {
|
1151
|
-
var _a;
|
1152
1121
|
if (isSortFilterString(filter)) {
|
1153
1122
|
return { [filter]: "asc" };
|
1154
1123
|
} else if (Array.isArray(filter)) {
|
@@ -1156,83 +1125,99 @@ function buildSortFilter(filter) {
|
|
1156
1125
|
} else if (isSortFilterBase(filter)) {
|
1157
1126
|
return filter;
|
1158
1127
|
} else if (isSortFilterObject(filter)) {
|
1159
|
-
return { [filter.column]:
|
1128
|
+
return { [filter.column]: filter.direction ?? "asc" };
|
1160
1129
|
} else {
|
1161
1130
|
throw new Error(`Invalid sort filter: ${filter}`);
|
1162
1131
|
}
|
1163
1132
|
}
|
1164
1133
|
|
1165
|
-
var __accessCheck$
|
1134
|
+
var __accessCheck$4 = (obj, member, msg) => {
|
1166
1135
|
if (!member.has(obj))
|
1167
1136
|
throw TypeError("Cannot " + msg);
|
1168
1137
|
};
|
1169
|
-
var __privateGet$
|
1170
|
-
__accessCheck$
|
1138
|
+
var __privateGet$3 = (obj, member, getter) => {
|
1139
|
+
__accessCheck$4(obj, member, "read from private field");
|
1171
1140
|
return getter ? getter.call(obj) : member.get(obj);
|
1172
1141
|
};
|
1173
|
-
var __privateAdd$
|
1142
|
+
var __privateAdd$4 = (obj, member, value) => {
|
1174
1143
|
if (member.has(obj))
|
1175
1144
|
throw TypeError("Cannot add the same private member more than once");
|
1176
1145
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1177
1146
|
};
|
1178
|
-
var __privateSet$
|
1179
|
-
__accessCheck$
|
1147
|
+
var __privateSet$2 = (obj, member, value, setter) => {
|
1148
|
+
__accessCheck$4(obj, member, "write to private field");
|
1180
1149
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1181
1150
|
return value;
|
1182
1151
|
};
|
1183
1152
|
var __privateMethod$2 = (obj, member, method) => {
|
1184
|
-
__accessCheck$
|
1153
|
+
__accessCheck$4(obj, member, "access private method");
|
1185
1154
|
return method;
|
1186
1155
|
};
|
1187
|
-
var _table, _links, _getFetchProps, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _bulkInsertTableRecords, bulkInsertTableRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn,
|
1156
|
+
var _table, _links, _getFetchProps, _cache, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _bulkInsertTableRecords, bulkInsertTableRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn, _invalidateCache, invalidateCache_fn, _setCacheRecord, setCacheRecord_fn, _getCacheRecord, getCacheRecord_fn, _setCacheQuery, setCacheQuery_fn, _getCacheQuery, getCacheQuery_fn;
|
1188
1157
|
class Repository extends Query {
|
1189
1158
|
}
|
1190
1159
|
class RestRepository extends Query {
|
1191
1160
|
constructor(options) {
|
1192
1161
|
super(null, options.table, {});
|
1193
|
-
__privateAdd$
|
1194
|
-
__privateAdd$
|
1195
|
-
__privateAdd$
|
1196
|
-
__privateAdd$
|
1197
|
-
__privateAdd$
|
1198
|
-
__privateAdd$
|
1199
|
-
__privateAdd$
|
1200
|
-
__privateAdd$
|
1201
|
-
__privateAdd$
|
1202
|
-
__privateAdd$
|
1203
|
-
|
1204
|
-
|
1205
|
-
|
1206
|
-
|
1162
|
+
__privateAdd$4(this, _insertRecordWithoutId);
|
1163
|
+
__privateAdd$4(this, _insertRecordWithId);
|
1164
|
+
__privateAdd$4(this, _bulkInsertTableRecords);
|
1165
|
+
__privateAdd$4(this, _updateRecordWithID);
|
1166
|
+
__privateAdd$4(this, _upsertRecordWithID);
|
1167
|
+
__privateAdd$4(this, _deleteRecord);
|
1168
|
+
__privateAdd$4(this, _invalidateCache);
|
1169
|
+
__privateAdd$4(this, _setCacheRecord);
|
1170
|
+
__privateAdd$4(this, _getCacheRecord);
|
1171
|
+
__privateAdd$4(this, _setCacheQuery);
|
1172
|
+
__privateAdd$4(this, _getCacheQuery);
|
1173
|
+
__privateAdd$4(this, _table, void 0);
|
1174
|
+
__privateAdd$4(this, _links, void 0);
|
1175
|
+
__privateAdd$4(this, _getFetchProps, void 0);
|
1176
|
+
__privateAdd$4(this, _cache, void 0);
|
1177
|
+
__privateSet$2(this, _table, options.table);
|
1178
|
+
__privateSet$2(this, _links, options.links ?? {});
|
1179
|
+
__privateSet$2(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
1207
1180
|
this.db = options.db;
|
1181
|
+
__privateSet$2(this, _cache, options.pluginOptions.cache);
|
1208
1182
|
}
|
1209
1183
|
async create(a, b) {
|
1210
1184
|
if (Array.isArray(a)) {
|
1211
|
-
|
1185
|
+
const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
|
1186
|
+
await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
|
1187
|
+
return records;
|
1212
1188
|
}
|
1213
1189
|
if (isString(a) && isObject(b)) {
|
1214
1190
|
if (a === "")
|
1215
1191
|
throw new Error("The id can't be empty");
|
1216
|
-
|
1192
|
+
const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b);
|
1193
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1194
|
+
return record;
|
1217
1195
|
}
|
1218
1196
|
if (isObject(a) && isString(a.id)) {
|
1219
1197
|
if (a.id === "")
|
1220
1198
|
throw new Error("The id can't be empty");
|
1221
|
-
|
1199
|
+
const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 });
|
1200
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1201
|
+
return record;
|
1222
1202
|
}
|
1223
1203
|
if (isObject(a)) {
|
1224
|
-
|
1204
|
+
const record = await __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a);
|
1205
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1206
|
+
return record;
|
1225
1207
|
}
|
1226
1208
|
throw new Error("Invalid arguments for create method");
|
1227
1209
|
}
|
1228
1210
|
async read(recordId) {
|
1229
|
-
const
|
1211
|
+
const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, recordId);
|
1212
|
+
if (cacheRecord)
|
1213
|
+
return cacheRecord;
|
1214
|
+
const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
|
1230
1215
|
try {
|
1231
1216
|
const response = await getRecord({
|
1232
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1217
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
|
1233
1218
|
...fetchProps
|
1234
1219
|
});
|
1235
|
-
return
|
1220
|
+
return initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), response);
|
1236
1221
|
} catch (e) {
|
1237
1222
|
if (isObject(e) && e.status === 404) {
|
1238
1223
|
return null;
|
@@ -1248,10 +1233,16 @@ class RestRepository extends Query {
|
|
1248
1233
|
return Promise.all(a.map((object) => this.update(object)));
|
1249
1234
|
}
|
1250
1235
|
if (isString(a) && isObject(b)) {
|
1251
|
-
|
1236
|
+
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
|
1237
|
+
const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b);
|
1238
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1239
|
+
return record;
|
1252
1240
|
}
|
1253
1241
|
if (isObject(a) && isString(a.id)) {
|
1254
|
-
|
1242
|
+
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
|
1243
|
+
const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
|
1244
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1245
|
+
return record;
|
1255
1246
|
}
|
1256
1247
|
throw new Error("Invalid arguments for update method");
|
1257
1248
|
}
|
@@ -1263,71 +1254,83 @@ class RestRepository extends Query {
|
|
1263
1254
|
return Promise.all(a.map((object) => this.createOrUpdate(object)));
|
1264
1255
|
}
|
1265
1256
|
if (isString(a) && isObject(b)) {
|
1266
|
-
|
1257
|
+
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
|
1258
|
+
const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b);
|
1259
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1260
|
+
return record;
|
1267
1261
|
}
|
1268
1262
|
if (isObject(a) && isString(a.id)) {
|
1269
|
-
|
1263
|
+
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
|
1264
|
+
const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
|
1265
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1266
|
+
return record;
|
1270
1267
|
}
|
1271
1268
|
throw new Error("Invalid arguments for createOrUpdate method");
|
1272
1269
|
}
|
1273
|
-
async delete(
|
1274
|
-
if (Array.isArray(
|
1275
|
-
if (
|
1270
|
+
async delete(a) {
|
1271
|
+
if (Array.isArray(a)) {
|
1272
|
+
if (a.length > 100) {
|
1276
1273
|
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
1277
1274
|
}
|
1278
|
-
await Promise.all(
|
1275
|
+
await Promise.all(a.map((id) => this.delete(id)));
|
1279
1276
|
return;
|
1280
1277
|
}
|
1281
|
-
if (isString(
|
1282
|
-
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this,
|
1278
|
+
if (isString(a)) {
|
1279
|
+
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a);
|
1280
|
+
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
|
1283
1281
|
return;
|
1284
1282
|
}
|
1285
|
-
if (isObject(
|
1286
|
-
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this,
|
1283
|
+
if (isObject(a) && isString(a.id)) {
|
1284
|
+
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id);
|
1285
|
+
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
|
1287
1286
|
return;
|
1288
1287
|
}
|
1289
1288
|
throw new Error("Invalid arguments for delete method");
|
1290
1289
|
}
|
1291
1290
|
async search(query, options = {}) {
|
1292
|
-
const fetchProps = await __privateGet$
|
1291
|
+
const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
|
1293
1292
|
const { records } = await searchBranch({
|
1294
1293
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1295
|
-
body: { tables: [__privateGet$
|
1294
|
+
body: { tables: [__privateGet$3(this, _table)], query, fuzziness: options.fuzziness },
|
1296
1295
|
...fetchProps
|
1297
1296
|
});
|
1298
|
-
return records.map((item) =>
|
1297
|
+
return records.map((item) => initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), item));
|
1299
1298
|
}
|
1300
1299
|
async query(query) {
|
1301
|
-
|
1300
|
+
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
1301
|
+
if (cacheQuery)
|
1302
|
+
return new Page(query, cacheQuery.meta, cacheQuery.records);
|
1302
1303
|
const data = query.getQueryOptions();
|
1303
1304
|
const body = {
|
1304
|
-
filter: Object.values(
|
1305
|
+
filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
|
1305
1306
|
sort: data.sort ? buildSortFilter(data.sort) : void 0,
|
1306
1307
|
page: data.page,
|
1307
1308
|
columns: data.columns
|
1308
1309
|
};
|
1309
|
-
const fetchProps = await __privateGet$
|
1310
|
+
const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
|
1310
1311
|
const { meta, records: objects } = await queryTable({
|
1311
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1312
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table) },
|
1312
1313
|
body,
|
1313
1314
|
...fetchProps
|
1314
1315
|
});
|
1315
|
-
const records = objects.map((record) =>
|
1316
|
+
const records = objects.map((record) => initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), record));
|
1317
|
+
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1316
1318
|
return new Page(query, meta, records);
|
1317
1319
|
}
|
1318
1320
|
}
|
1319
1321
|
_table = new WeakMap();
|
1320
1322
|
_links = new WeakMap();
|
1321
1323
|
_getFetchProps = new WeakMap();
|
1324
|
+
_cache = new WeakMap();
|
1322
1325
|
_insertRecordWithoutId = new WeakSet();
|
1323
1326
|
insertRecordWithoutId_fn = async function(object) {
|
1324
|
-
const fetchProps = await __privateGet$
|
1327
|
+
const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
|
1325
1328
|
const record = transformObjectLinks(object);
|
1326
1329
|
const response = await insertRecord({
|
1327
1330
|
pathParams: {
|
1328
1331
|
workspace: "{workspaceId}",
|
1329
1332
|
dbBranchName: "{dbBranch}",
|
1330
|
-
tableName: __privateGet$
|
1333
|
+
tableName: __privateGet$3(this, _table)
|
1331
1334
|
},
|
1332
1335
|
body: record,
|
1333
1336
|
...fetchProps
|
@@ -1340,13 +1343,13 @@ insertRecordWithoutId_fn = async function(object) {
|
|
1340
1343
|
};
|
1341
1344
|
_insertRecordWithId = new WeakSet();
|
1342
1345
|
insertRecordWithId_fn = async function(recordId, object) {
|
1343
|
-
const fetchProps = await __privateGet$
|
1346
|
+
const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
|
1344
1347
|
const record = transformObjectLinks(object);
|
1345
1348
|
const response = await insertRecordWithID({
|
1346
1349
|
pathParams: {
|
1347
1350
|
workspace: "{workspaceId}",
|
1348
1351
|
dbBranchName: "{dbBranch}",
|
1349
|
-
tableName: __privateGet$
|
1352
|
+
tableName: __privateGet$3(this, _table),
|
1350
1353
|
recordId
|
1351
1354
|
},
|
1352
1355
|
body: record,
|
@@ -1361,10 +1364,10 @@ insertRecordWithId_fn = async function(recordId, object) {
|
|
1361
1364
|
};
|
1362
1365
|
_bulkInsertTableRecords = new WeakSet();
|
1363
1366
|
bulkInsertTableRecords_fn = async function(objects) {
|
1364
|
-
const fetchProps = await __privateGet$
|
1367
|
+
const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
|
1365
1368
|
const records = objects.map((object) => transformObjectLinks(object));
|
1366
1369
|
const response = await bulkInsertTableRecords({
|
1367
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1370
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table) },
|
1368
1371
|
body: { records },
|
1369
1372
|
...fetchProps
|
1370
1373
|
});
|
@@ -1376,10 +1379,10 @@ bulkInsertTableRecords_fn = async function(objects) {
|
|
1376
1379
|
};
|
1377
1380
|
_updateRecordWithID = new WeakSet();
|
1378
1381
|
updateRecordWithID_fn = async function(recordId, object) {
|
1379
|
-
const fetchProps = await __privateGet$
|
1382
|
+
const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
|
1380
1383
|
const record = transformObjectLinks(object);
|
1381
1384
|
const response = await updateRecordWithID({
|
1382
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1385
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
|
1383
1386
|
body: record,
|
1384
1387
|
...fetchProps
|
1385
1388
|
});
|
@@ -1390,9 +1393,9 @@ updateRecordWithID_fn = async function(recordId, object) {
|
|
1390
1393
|
};
|
1391
1394
|
_upsertRecordWithID = new WeakSet();
|
1392
1395
|
upsertRecordWithID_fn = async function(recordId, object) {
|
1393
|
-
const fetchProps = await __privateGet$
|
1396
|
+
const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
|
1394
1397
|
const response = await upsertRecordWithID({
|
1395
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1398
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
|
1396
1399
|
body: object,
|
1397
1400
|
...fetchProps
|
1398
1401
|
});
|
@@ -1403,25 +1406,69 @@ upsertRecordWithID_fn = async function(recordId, object) {
|
|
1403
1406
|
};
|
1404
1407
|
_deleteRecord = new WeakSet();
|
1405
1408
|
deleteRecord_fn = async function(recordId) {
|
1406
|
-
const fetchProps = await __privateGet$
|
1409
|
+
const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
|
1407
1410
|
await deleteRecord({
|
1408
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1411
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
|
1409
1412
|
...fetchProps
|
1410
1413
|
});
|
1411
1414
|
};
|
1412
|
-
|
1413
|
-
|
1415
|
+
_invalidateCache = new WeakSet();
|
1416
|
+
invalidateCache_fn = async function(recordId) {
|
1417
|
+
await __privateGet$3(this, _cache).delete(`rec_${__privateGet$3(this, _table)}:${recordId}`);
|
1418
|
+
const cacheItems = await __privateGet$3(this, _cache).getAll();
|
1419
|
+
const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
|
1420
|
+
for (const [key, value] of queries) {
|
1421
|
+
const ids = getIds(value);
|
1422
|
+
if (ids.includes(recordId))
|
1423
|
+
await __privateGet$3(this, _cache).delete(key);
|
1424
|
+
}
|
1425
|
+
};
|
1426
|
+
_setCacheRecord = new WeakSet();
|
1427
|
+
setCacheRecord_fn = async function(record) {
|
1428
|
+
if (!__privateGet$3(this, _cache).cacheRecords)
|
1429
|
+
return;
|
1430
|
+
await __privateGet$3(this, _cache).set(`rec_${__privateGet$3(this, _table)}:${record.id}`, record);
|
1431
|
+
};
|
1432
|
+
_getCacheRecord = new WeakSet();
|
1433
|
+
getCacheRecord_fn = async function(recordId) {
|
1434
|
+
if (!__privateGet$3(this, _cache).cacheRecords)
|
1435
|
+
return null;
|
1436
|
+
return __privateGet$3(this, _cache).get(`rec_${__privateGet$3(this, _table)}:${recordId}`);
|
1437
|
+
};
|
1438
|
+
_setCacheQuery = new WeakSet();
|
1439
|
+
setCacheQuery_fn = async function(query, meta, records) {
|
1440
|
+
await __privateGet$3(this, _cache).set(`query_${__privateGet$3(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
|
1441
|
+
};
|
1442
|
+
_getCacheQuery = new WeakSet();
|
1443
|
+
getCacheQuery_fn = async function(query) {
|
1444
|
+
const key = `query_${__privateGet$3(this, _table)}:${query.key()}`;
|
1445
|
+
const result = await __privateGet$3(this, _cache).get(key);
|
1446
|
+
if (!result)
|
1447
|
+
return null;
|
1448
|
+
const { cache: ttl = __privateGet$3(this, _cache).defaultQueryTTL } = query.getQueryOptions();
|
1449
|
+
if (!ttl || ttl < 0)
|
1450
|
+
return result;
|
1451
|
+
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1452
|
+
return hasExpired ? null : result;
|
1453
|
+
};
|
1454
|
+
const transformObjectLinks = (object) => {
|
1455
|
+
return Object.entries(object).reduce((acc, [key, value]) => {
|
1456
|
+
if (key === "xata")
|
1457
|
+
return acc;
|
1458
|
+
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1459
|
+
}, {});
|
1460
|
+
};
|
1461
|
+
const initObject = (db, links, table, object) => {
|
1414
1462
|
const result = {};
|
1415
1463
|
Object.assign(result, object);
|
1416
|
-
const tableLinks =
|
1464
|
+
const tableLinks = links[table] || [];
|
1417
1465
|
for (const link of tableLinks) {
|
1418
1466
|
const [field, linkTable] = link;
|
1419
1467
|
const value = result[field];
|
1420
1468
|
if (value && isObject(value)) {
|
1421
|
-
result[field] =
|
1469
|
+
result[field] = initObject(db, links, linkTable, value);
|
1422
1470
|
}
|
1423
1471
|
}
|
1424
|
-
const db = this.db;
|
1425
1472
|
result.read = function() {
|
1426
1473
|
return db[table].read(result["id"]);
|
1427
1474
|
};
|
@@ -1437,13 +1484,65 @@ initObject_fn = function(table, object) {
|
|
1437
1484
|
Object.freeze(result);
|
1438
1485
|
return result;
|
1439
1486
|
};
|
1440
|
-
|
1441
|
-
|
1442
|
-
|
1443
|
-
|
1444
|
-
|
1445
|
-
|
1487
|
+
function getIds(value) {
|
1488
|
+
if (Array.isArray(value)) {
|
1489
|
+
return value.map((item) => getIds(item)).flat();
|
1490
|
+
}
|
1491
|
+
if (!isObject(value))
|
1492
|
+
return [];
|
1493
|
+
const nestedIds = Object.values(value).map((item) => getIds(item)).flat();
|
1494
|
+
return isString(value.id) ? [value.id, ...nestedIds] : nestedIds;
|
1495
|
+
}
|
1496
|
+
|
1497
|
+
var __accessCheck$3 = (obj, member, msg) => {
|
1498
|
+
if (!member.has(obj))
|
1499
|
+
throw TypeError("Cannot " + msg);
|
1446
1500
|
};
|
1501
|
+
var __privateGet$2 = (obj, member, getter) => {
|
1502
|
+
__accessCheck$3(obj, member, "read from private field");
|
1503
|
+
return getter ? getter.call(obj) : member.get(obj);
|
1504
|
+
};
|
1505
|
+
var __privateAdd$3 = (obj, member, value) => {
|
1506
|
+
if (member.has(obj))
|
1507
|
+
throw TypeError("Cannot add the same private member more than once");
|
1508
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1509
|
+
};
|
1510
|
+
var __privateSet$1 = (obj, member, value, setter) => {
|
1511
|
+
__accessCheck$3(obj, member, "write to private field");
|
1512
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1513
|
+
return value;
|
1514
|
+
};
|
1515
|
+
var _map;
|
1516
|
+
class SimpleCache {
|
1517
|
+
constructor(options = {}) {
|
1518
|
+
__privateAdd$3(this, _map, void 0);
|
1519
|
+
__privateSet$1(this, _map, /* @__PURE__ */ new Map());
|
1520
|
+
this.capacity = options.max ?? 500;
|
1521
|
+
this.cacheRecords = options.cacheRecords ?? true;
|
1522
|
+
this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
|
1523
|
+
}
|
1524
|
+
async getAll() {
|
1525
|
+
return Object.fromEntries(__privateGet$2(this, _map));
|
1526
|
+
}
|
1527
|
+
async get(key) {
|
1528
|
+
return __privateGet$2(this, _map).get(key) ?? null;
|
1529
|
+
}
|
1530
|
+
async set(key, value) {
|
1531
|
+
await this.delete(key);
|
1532
|
+
__privateGet$2(this, _map).set(key, value);
|
1533
|
+
if (__privateGet$2(this, _map).size > this.capacity) {
|
1534
|
+
const leastRecentlyUsed = __privateGet$2(this, _map).keys().next().value;
|
1535
|
+
await this.delete(leastRecentlyUsed);
|
1536
|
+
}
|
1537
|
+
}
|
1538
|
+
async delete(key) {
|
1539
|
+
__privateGet$2(this, _map).delete(key);
|
1540
|
+
}
|
1541
|
+
async clear() {
|
1542
|
+
return __privateGet$2(this, _map).clear();
|
1543
|
+
}
|
1544
|
+
}
|
1545
|
+
_map = new WeakMap();
|
1447
1546
|
|
1448
1547
|
const gt = (value) => ({ $gt: value });
|
1449
1548
|
const ge = (value) => ({ $ge: value });
|
@@ -1479,23 +1578,27 @@ var __privateAdd$2 = (obj, member, value) => {
|
|
1479
1578
|
};
|
1480
1579
|
var _tables;
|
1481
1580
|
class SchemaPlugin extends XataPlugin {
|
1482
|
-
constructor(links) {
|
1581
|
+
constructor(links, tableNames) {
|
1483
1582
|
super();
|
1484
1583
|
this.links = links;
|
1584
|
+
this.tableNames = tableNames;
|
1485
1585
|
__privateAdd$2(this, _tables, {});
|
1486
1586
|
}
|
1487
|
-
build(
|
1488
|
-
const { getFetchProps } = options;
|
1587
|
+
build(pluginOptions) {
|
1489
1588
|
const links = this.links;
|
1490
1589
|
const db = new Proxy({}, {
|
1491
1590
|
get: (_target, table) => {
|
1492
1591
|
if (!isString(table))
|
1493
1592
|
throw new Error("Invalid table name");
|
1494
|
-
if (!__privateGet$1(this, _tables)[table])
|
1495
|
-
__privateGet$1(this, _tables)[table] = new RestRepository({ db,
|
1593
|
+
if (!__privateGet$1(this, _tables)[table]) {
|
1594
|
+
__privateGet$1(this, _tables)[table] = new RestRepository({ db, pluginOptions, table, links });
|
1595
|
+
}
|
1496
1596
|
return __privateGet$1(this, _tables)[table];
|
1497
1597
|
}
|
1498
1598
|
});
|
1599
|
+
for (const table of this.tableNames ?? []) {
|
1600
|
+
db[table] = new RestRepository({ db, pluginOptions, table, links });
|
1601
|
+
}
|
1499
1602
|
return db;
|
1500
1603
|
}
|
1501
1604
|
}
|
@@ -1516,8 +1619,10 @@ var __privateMethod$1 = (obj, member, method) => {
|
|
1516
1619
|
};
|
1517
1620
|
var _search, search_fn;
|
1518
1621
|
class SearchPlugin extends XataPlugin {
|
1519
|
-
constructor() {
|
1520
|
-
super(
|
1622
|
+
constructor(db, links) {
|
1623
|
+
super();
|
1624
|
+
this.db = db;
|
1625
|
+
this.links = links;
|
1521
1626
|
__privateAdd$1(this, _search);
|
1522
1627
|
}
|
1523
1628
|
build({ getFetchProps }) {
|
@@ -1526,16 +1631,16 @@ class SearchPlugin extends XataPlugin {
|
|
1526
1631
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1527
1632
|
return records.map((record) => {
|
1528
1633
|
const { table = "orphan" } = record.xata;
|
1529
|
-
return { table, record };
|
1634
|
+
return { table, record: initObject(this.db, this.links, table, record) };
|
1530
1635
|
});
|
1531
1636
|
},
|
1532
1637
|
byTable: async (query, options = {}) => {
|
1533
1638
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1534
1639
|
return records.reduce((acc, record) => {
|
1535
|
-
var _a;
|
1536
1640
|
const { table = "orphan" } = record.xata;
|
1537
|
-
const items =
|
1538
|
-
|
1641
|
+
const items = acc[table] ?? [];
|
1642
|
+
const item = initObject(this.db, this.links, table, record);
|
1643
|
+
return { ...acc, [table]: [...items, item] };
|
1539
1644
|
}, {});
|
1540
1645
|
}
|
1541
1646
|
};
|
@@ -1544,7 +1649,7 @@ class SearchPlugin extends XataPlugin {
|
|
1544
1649
|
_search = new WeakSet();
|
1545
1650
|
search_fn = async function(query, options, getFetchProps) {
|
1546
1651
|
const fetchProps = await getFetchProps();
|
1547
|
-
const { tables, fuzziness } = options
|
1652
|
+
const { tables, fuzziness } = options ?? {};
|
1548
1653
|
const { records } = await searchBranch({
|
1549
1654
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1550
1655
|
body: { tables, query, fuzziness },
|
@@ -1557,6 +1662,84 @@ const isBranchStrategyBuilder = (strategy) => {
|
|
1557
1662
|
return typeof strategy === "function";
|
1558
1663
|
};
|
1559
1664
|
|
1665
|
+
const envBranchNames = [
|
1666
|
+
"XATA_BRANCH",
|
1667
|
+
"VERCEL_GIT_COMMIT_REF",
|
1668
|
+
"CF_PAGES_BRANCH",
|
1669
|
+
"BRANCH"
|
1670
|
+
];
|
1671
|
+
const defaultBranch = "main";
|
1672
|
+
async function getCurrentBranchName(options) {
|
1673
|
+
const env = await getBranchByEnvVariable();
|
1674
|
+
if (env)
|
1675
|
+
return env;
|
1676
|
+
const branch = await getGitBranch();
|
1677
|
+
if (!branch)
|
1678
|
+
return defaultBranch;
|
1679
|
+
const details = await getDatabaseBranch(branch, options);
|
1680
|
+
if (details)
|
1681
|
+
return branch;
|
1682
|
+
return defaultBranch;
|
1683
|
+
}
|
1684
|
+
async function getCurrentBranchDetails(options) {
|
1685
|
+
const env = await getBranchByEnvVariable();
|
1686
|
+
if (env)
|
1687
|
+
return getDatabaseBranch(env, options);
|
1688
|
+
const branch = await getGitBranch();
|
1689
|
+
if (!branch)
|
1690
|
+
return getDatabaseBranch(defaultBranch, options);
|
1691
|
+
const details = await getDatabaseBranch(branch, options);
|
1692
|
+
if (details)
|
1693
|
+
return details;
|
1694
|
+
return getDatabaseBranch(defaultBranch, options);
|
1695
|
+
}
|
1696
|
+
async function getDatabaseBranch(branch, options) {
|
1697
|
+
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1698
|
+
const apiKey = options?.apiKey || getAPIKey();
|
1699
|
+
if (!databaseURL)
|
1700
|
+
throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
|
1701
|
+
if (!apiKey)
|
1702
|
+
throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
|
1703
|
+
const [protocol, , host, , database] = databaseURL.split("/");
|
1704
|
+
const [workspace] = host.split(".");
|
1705
|
+
const dbBranchName = `${database}:${branch}`;
|
1706
|
+
try {
|
1707
|
+
return await getBranchDetails({
|
1708
|
+
apiKey,
|
1709
|
+
apiUrl: databaseURL,
|
1710
|
+
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1711
|
+
workspacesApiUrl: `${protocol}//${host}`,
|
1712
|
+
pathParams: {
|
1713
|
+
dbBranchName,
|
1714
|
+
workspace
|
1715
|
+
}
|
1716
|
+
});
|
1717
|
+
} catch (err) {
|
1718
|
+
if (isObject(err) && err.status === 404)
|
1719
|
+
return null;
|
1720
|
+
throw err;
|
1721
|
+
}
|
1722
|
+
}
|
1723
|
+
function getBranchByEnvVariable() {
|
1724
|
+
for (const name of envBranchNames) {
|
1725
|
+
const value = getEnvVariable(name);
|
1726
|
+
if (value) {
|
1727
|
+
return value;
|
1728
|
+
}
|
1729
|
+
}
|
1730
|
+
try {
|
1731
|
+
return XATA_BRANCH;
|
1732
|
+
} catch (err) {
|
1733
|
+
}
|
1734
|
+
}
|
1735
|
+
function getDatabaseURL() {
|
1736
|
+
try {
|
1737
|
+
return getEnvVariable("XATA_DATABASE_URL") ?? XATA_DATABASE_URL;
|
1738
|
+
} catch (err) {
|
1739
|
+
return void 0;
|
1740
|
+
}
|
1741
|
+
}
|
1742
|
+
|
1560
1743
|
var __accessCheck = (obj, member, msg) => {
|
1561
1744
|
if (!member.has(obj))
|
1562
1745
|
throw TypeError("Cannot " + msg);
|
@@ -1582,21 +1765,24 @@ var __privateMethod = (obj, member, method) => {
|
|
1582
1765
|
const buildClient = (plugins) => {
|
1583
1766
|
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1584
1767
|
return _a = class {
|
1585
|
-
constructor(options = {}, links) {
|
1768
|
+
constructor(options = {}, links, tables) {
|
1586
1769
|
__privateAdd(this, _parseOptions);
|
1587
1770
|
__privateAdd(this, _getFetchProps);
|
1588
1771
|
__privateAdd(this, _evaluateBranch);
|
1589
1772
|
__privateAdd(this, _branch, void 0);
|
1590
1773
|
const safeOptions = __privateMethod(this, _parseOptions, parseOptions_fn).call(this, options);
|
1591
|
-
const
|
1592
|
-
|
1593
|
-
|
1594
|
-
...plugins
|
1774
|
+
const pluginOptions = {
|
1775
|
+
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
1776
|
+
cache: safeOptions.cache
|
1595
1777
|
};
|
1596
|
-
|
1778
|
+
const db = new SchemaPlugin(links, tables).build(pluginOptions);
|
1779
|
+
const search = new SearchPlugin(db, links ?? {}).build(pluginOptions);
|
1780
|
+
this.db = db;
|
1781
|
+
this.search = search;
|
1782
|
+
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
1597
1783
|
if (!namespace)
|
1598
1784
|
continue;
|
1599
|
-
const result = namespace.build(
|
1785
|
+
const result = namespace.build(pluginOptions);
|
1600
1786
|
if (result instanceof Promise) {
|
1601
1787
|
void result.then((namespace2) => {
|
1602
1788
|
this[key] = namespace2;
|
@@ -1607,14 +1793,15 @@ const buildClient = (plugins) => {
|
|
1607
1793
|
}
|
1608
1794
|
}
|
1609
1795
|
}, _branch = new WeakMap(), _parseOptions = new WeakSet(), parseOptions_fn = function(options) {
|
1610
|
-
const fetch = getFetchImplementation(options
|
1611
|
-
const databaseURL =
|
1612
|
-
const apiKey =
|
1613
|
-
const
|
1796
|
+
const fetch = getFetchImplementation(options?.fetch);
|
1797
|
+
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1798
|
+
const apiKey = options?.apiKey || getAPIKey();
|
1799
|
+
const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
|
1800
|
+
const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
1614
1801
|
if (!databaseURL || !apiKey) {
|
1615
1802
|
throw new Error("Options databaseURL and apiKey are required");
|
1616
1803
|
}
|
1617
|
-
return { fetch, databaseURL, apiKey, branch };
|
1804
|
+
return { fetch, databaseURL, apiKey, branch, cache };
|
1618
1805
|
}, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({
|
1619
1806
|
fetch,
|
1620
1807
|
apiKey,
|
@@ -1629,8 +1816,7 @@ const buildClient = (plugins) => {
|
|
1629
1816
|
apiKey,
|
1630
1817
|
apiUrl: "",
|
1631
1818
|
workspacesApiUrl: (path, params) => {
|
1632
|
-
|
1633
|
-
const hasBranch = (_a2 = params.dbBranchName) != null ? _a2 : params.branch;
|
1819
|
+
const hasBranch = params.dbBranchName ?? params.branch;
|
1634
1820
|
const newPath = path.replace(/^\/db\/[^/]+/, hasBranch ? `:${branchValue}` : "");
|
1635
1821
|
return databaseURL + newPath;
|
1636
1822
|
}
|
@@ -1663,5 +1849,5 @@ class XataError extends Error {
|
|
1663
1849
|
}
|
1664
1850
|
}
|
1665
1851
|
|
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 };
|
1852
|
+
export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, Repository, RestRepository, SchemaPlugin, SearchPlugin, SimpleCache, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addGitBranchesEntry, 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, getGitBranchesMapping, 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, removeGitBranchesEntry, removeWorkspaceMember, resendWorkspaceMemberInvite, resolveBranch, searchBranch, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
|
1667
1853
|
//# sourceMappingURL=index.mjs.map
|