@xata.io/client 0.0.0-alpha.vf6d8daa → 0.0.0-alpha.vf73045e
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/.eslintrc.cjs +1 -2
- package/CHANGELOG.md +186 -0
- package/README.md +273 -1
- package/Usage.md +447 -0
- package/dist/index.cjs +772 -370
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1164 -240
- package/dist/index.mjs +742 -371
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -4
- package/tsconfig.json +1 -0
package/dist/index.cjs
CHANGED
@@ -2,6 +2,24 @@
|
|
2
2
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
4
4
|
|
5
|
+
function _interopNamespace(e) {
|
6
|
+
if (e && e.__esModule) return e;
|
7
|
+
var n = Object.create(null);
|
8
|
+
if (e) {
|
9
|
+
Object.keys(e).forEach(function (k) {
|
10
|
+
if (k !== 'default') {
|
11
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
12
|
+
Object.defineProperty(n, k, d.get ? d : {
|
13
|
+
enumerable: true,
|
14
|
+
get: function () { return e[k]; }
|
15
|
+
});
|
16
|
+
}
|
17
|
+
});
|
18
|
+
}
|
19
|
+
n["default"] = e;
|
20
|
+
return Object.freeze(n);
|
21
|
+
}
|
22
|
+
|
5
23
|
function notEmpty(value) {
|
6
24
|
return value !== null && value !== void 0;
|
7
25
|
}
|
@@ -11,43 +29,101 @@ function compact(arr) {
|
|
11
29
|
function isObject(value) {
|
12
30
|
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
13
31
|
}
|
32
|
+
function isDefined(value) {
|
33
|
+
return value !== null && value !== void 0;
|
34
|
+
}
|
14
35
|
function isString(value) {
|
15
|
-
return value
|
36
|
+
return isDefined(value) && typeof value === "string";
|
37
|
+
}
|
38
|
+
function isStringArray(value) {
|
39
|
+
return isDefined(value) && Array.isArray(value) && value.every(isString);
|
16
40
|
}
|
17
41
|
function toBase64(value) {
|
18
42
|
try {
|
19
43
|
return btoa(value);
|
20
44
|
} catch (err) {
|
21
|
-
|
45
|
+
const buf = Buffer;
|
46
|
+
return buf.from(value).toString("base64");
|
22
47
|
}
|
23
48
|
}
|
24
49
|
|
25
|
-
function
|
50
|
+
function getEnvironment() {
|
26
51
|
try {
|
27
|
-
if (isObject(process) &&
|
28
|
-
return
|
52
|
+
if (isObject(process) && isObject(process.env)) {
|
53
|
+
return {
|
54
|
+
apiKey: process.env.XATA_API_KEY ?? getGlobalApiKey(),
|
55
|
+
databaseURL: process.env.XATA_DATABASE_URL ?? getGlobalDatabaseURL(),
|
56
|
+
branch: process.env.XATA_BRANCH ?? getGlobalBranch(),
|
57
|
+
envBranch: process.env.VERCEL_GIT_COMMIT_REF ?? process.env.CF_PAGES_BRANCH ?? process.env.BRANCH,
|
58
|
+
fallbackBranch: process.env.XATA_FALLBACK_BRANCH ?? getGlobalFallbackBranch()
|
59
|
+
};
|
29
60
|
}
|
30
61
|
} catch (err) {
|
31
62
|
}
|
32
63
|
try {
|
33
|
-
if (isObject(Deno) &&
|
34
|
-
return
|
64
|
+
if (isObject(Deno) && isObject(Deno.env)) {
|
65
|
+
return {
|
66
|
+
apiKey: Deno.env.get("XATA_API_KEY") ?? getGlobalApiKey(),
|
67
|
+
databaseURL: Deno.env.get("XATA_DATABASE_URL") ?? getGlobalDatabaseURL(),
|
68
|
+
branch: Deno.env.get("XATA_BRANCH") ?? getGlobalBranch(),
|
69
|
+
envBranch: Deno.env.get("VERCEL_GIT_COMMIT_REF") ?? Deno.env.get("CF_PAGES_BRANCH") ?? Deno.env.get("BRANCH"),
|
70
|
+
fallbackBranch: Deno.env.get("XATA_FALLBACK_BRANCH") ?? getGlobalFallbackBranch()
|
71
|
+
};
|
35
72
|
}
|
36
73
|
} catch (err) {
|
37
74
|
}
|
75
|
+
return {
|
76
|
+
apiKey: getGlobalApiKey(),
|
77
|
+
databaseURL: getGlobalDatabaseURL(),
|
78
|
+
branch: getGlobalBranch(),
|
79
|
+
envBranch: void 0,
|
80
|
+
fallbackBranch: getGlobalFallbackBranch()
|
81
|
+
};
|
82
|
+
}
|
83
|
+
function getGlobalApiKey() {
|
84
|
+
try {
|
85
|
+
return XATA_API_KEY;
|
86
|
+
} catch (err) {
|
87
|
+
return void 0;
|
88
|
+
}
|
89
|
+
}
|
90
|
+
function getGlobalDatabaseURL() {
|
91
|
+
try {
|
92
|
+
return XATA_DATABASE_URL;
|
93
|
+
} catch (err) {
|
94
|
+
return void 0;
|
95
|
+
}
|
96
|
+
}
|
97
|
+
function getGlobalBranch() {
|
98
|
+
try {
|
99
|
+
return XATA_BRANCH;
|
100
|
+
} catch (err) {
|
101
|
+
return void 0;
|
102
|
+
}
|
103
|
+
}
|
104
|
+
function getGlobalFallbackBranch() {
|
105
|
+
try {
|
106
|
+
return XATA_FALLBACK_BRANCH;
|
107
|
+
} catch (err) {
|
108
|
+
return void 0;
|
109
|
+
}
|
38
110
|
}
|
39
111
|
async function getGitBranch() {
|
112
|
+
const cmd = ["git", "branch", "--show-current"];
|
113
|
+
const fullCmd = cmd.join(" ");
|
114
|
+
const nodeModule = ["child", "process"].join("_");
|
115
|
+
const execOptions = { encoding: "utf-8", stdio: ["ignore", "pipe", "ignore"] };
|
40
116
|
try {
|
41
|
-
|
117
|
+
if (typeof require === "function") {
|
118
|
+
return require(nodeModule).execSync(fullCmd, execOptions).trim();
|
119
|
+
}
|
120
|
+
const { execSync } = await (function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(t)); }); })(nodeModule);
|
121
|
+
return execSync(fullCmd, execOptions).toString().trim();
|
42
122
|
} catch (err) {
|
43
123
|
}
|
44
124
|
try {
|
45
125
|
if (isObject(Deno)) {
|
46
|
-
const process2 = Deno.run({
|
47
|
-
cmd: ["git", "branch", "--show-current"],
|
48
|
-
stdout: "piped",
|
49
|
-
stderr: "piped"
|
50
|
-
});
|
126
|
+
const process2 = Deno.run({ cmd, stdout: "piped", stderr: "null" });
|
51
127
|
return new TextDecoder().decode(await process2.output()).trim();
|
52
128
|
}
|
53
129
|
} catch (err) {
|
@@ -56,7 +132,8 @@ async function getGitBranch() {
|
|
56
132
|
|
57
133
|
function getAPIKey() {
|
58
134
|
try {
|
59
|
-
|
135
|
+
const { apiKey } = getEnvironment();
|
136
|
+
return apiKey;
|
60
137
|
} catch (err) {
|
61
138
|
return void 0;
|
62
139
|
}
|
@@ -66,21 +143,35 @@ function getFetchImplementation(userFetch) {
|
|
66
143
|
const globalFetch = typeof fetch !== "undefined" ? fetch : void 0;
|
67
144
|
const fetchImpl = userFetch ?? globalFetch;
|
68
145
|
if (!fetchImpl) {
|
69
|
-
throw new Error(
|
146
|
+
throw new Error(
|
147
|
+
`The \`fetch\` option passed to the Xata client is resolving to a falsy value and may not be correctly imported.`
|
148
|
+
);
|
70
149
|
}
|
71
150
|
return fetchImpl;
|
72
151
|
}
|
73
152
|
|
74
|
-
|
75
|
-
|
153
|
+
const VERSION = "0.0.0-alpha.vf73045e";
|
154
|
+
|
155
|
+
class ErrorWithCause extends Error {
|
156
|
+
constructor(message, options) {
|
157
|
+
super(message, options);
|
158
|
+
}
|
159
|
+
}
|
160
|
+
class FetcherError extends ErrorWithCause {
|
161
|
+
constructor(status, data, requestId) {
|
76
162
|
super(getMessage(data));
|
77
163
|
this.status = status;
|
78
164
|
this.errors = isBulkError(data) ? data.errors : void 0;
|
165
|
+
this.requestId = requestId;
|
79
166
|
if (data instanceof Error) {
|
80
167
|
this.stack = data.stack;
|
81
168
|
this.cause = data.cause;
|
82
169
|
}
|
83
170
|
}
|
171
|
+
toString() {
|
172
|
+
const error = super.toString();
|
173
|
+
return `[${this.status}] (${this.requestId ?? "Unknown"}): ${error}`;
|
174
|
+
}
|
84
175
|
}
|
85
176
|
function isBulkError(error) {
|
86
177
|
return isObject(error) && Array.isArray(error.errors);
|
@@ -103,7 +194,12 @@ function getMessage(data) {
|
|
103
194
|
}
|
104
195
|
|
105
196
|
const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
|
106
|
-
const
|
197
|
+
const cleanQueryParams = Object.entries(queryParams).reduce((acc, [key, value]) => {
|
198
|
+
if (value === void 0 || value === null)
|
199
|
+
return acc;
|
200
|
+
return { ...acc, [key]: value };
|
201
|
+
}, {});
|
202
|
+
const query = new URLSearchParams(cleanQueryParams).toString();
|
107
203
|
const queryString = query.length > 0 ? `?${query}` : "";
|
108
204
|
return url.replace(/\{\w*\}/g, (key) => pathParams[key.slice(1, -1)]) + queryString;
|
109
205
|
};
|
@@ -143,6 +239,7 @@ async function fetch$1({
|
|
143
239
|
body: body ? JSON.stringify(body) : void 0,
|
144
240
|
headers: {
|
145
241
|
"Content-Type": "application/json",
|
242
|
+
"User-Agent": `Xata client-ts/${VERSION}`,
|
146
243
|
...headers,
|
147
244
|
...hostHeader(fullUrl),
|
148
245
|
Authorization: `Bearer ${apiKey}`
|
@@ -151,14 +248,15 @@ async function fetch$1({
|
|
151
248
|
if (response.status === 204) {
|
152
249
|
return {};
|
153
250
|
}
|
251
|
+
const requestId = response.headers?.get("x-request-id") ?? void 0;
|
154
252
|
try {
|
155
253
|
const jsonResponse = await response.json();
|
156
254
|
if (response.ok) {
|
157
255
|
return jsonResponse;
|
158
256
|
}
|
159
|
-
throw new FetcherError(response.status, jsonResponse);
|
257
|
+
throw new FetcherError(response.status, jsonResponse, requestId);
|
160
258
|
} catch (error) {
|
161
|
-
throw new FetcherError(response.status, error);
|
259
|
+
throw new FetcherError(response.status, error, requestId);
|
162
260
|
}
|
163
261
|
}
|
164
262
|
|
@@ -217,6 +315,7 @@ const removeWorkspaceMember = (variables) => fetch$1({
|
|
217
315
|
...variables
|
218
316
|
});
|
219
317
|
const inviteWorkspaceMember = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/invites", method: "post", ...variables });
|
318
|
+
const updateWorkspaceMemberInvite = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/invites/{inviteId}", method: "patch", ...variables });
|
220
319
|
const cancelWorkspaceMemberInvite = (variables) => fetch$1({
|
221
320
|
url: "/workspaces/{workspaceId}/invites/{inviteId}",
|
222
321
|
method: "delete",
|
@@ -252,16 +351,25 @@ const deleteDatabase = (variables) => fetch$1({
|
|
252
351
|
method: "delete",
|
253
352
|
...variables
|
254
353
|
});
|
255
|
-
const
|
256
|
-
url: "/
|
354
|
+
const getDatabaseMetadata = (variables) => fetch$1({
|
355
|
+
url: "/dbs/{dbName}/metadata",
|
257
356
|
method: "get",
|
258
357
|
...variables
|
259
358
|
});
|
260
|
-
const
|
359
|
+
const getGitBranchesMapping = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "get", ...variables });
|
360
|
+
const addGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "post", ...variables });
|
361
|
+
const removeGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "delete", ...variables });
|
362
|
+
const resolveBranch = (variables) => fetch$1({
|
363
|
+
url: "/dbs/{dbName}/resolveBranch",
|
364
|
+
method: "get",
|
365
|
+
...variables
|
366
|
+
});
|
367
|
+
const getBranchDetails = (variables) => fetch$1({
|
261
368
|
url: "/db/{dbBranchName}",
|
262
|
-
method: "
|
369
|
+
method: "get",
|
263
370
|
...variables
|
264
371
|
});
|
372
|
+
const createBranch = (variables) => fetch$1({ url: "/db/{dbBranchName}", method: "put", ...variables });
|
265
373
|
const deleteBranch = (variables) => fetch$1({
|
266
374
|
url: "/db/{dbBranchName}",
|
267
375
|
method: "delete",
|
@@ -335,11 +443,7 @@ const updateColumn = (variables) => fetch$1({
|
|
335
443
|
method: "patch",
|
336
444
|
...variables
|
337
445
|
});
|
338
|
-
const insertRecord = (variables) => fetch$1({
|
339
|
-
url: "/db/{dbBranchName}/tables/{tableName}/data",
|
340
|
-
method: "post",
|
341
|
-
...variables
|
342
|
-
});
|
446
|
+
const insertRecord = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data", method: "post", ...variables });
|
343
447
|
const insertRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "put", ...variables });
|
344
448
|
const updateRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "patch", ...variables });
|
345
449
|
const upsertRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "post", ...variables });
|
@@ -359,6 +463,11 @@ const queryTable = (variables) => fetch$1({
|
|
359
463
|
method: "post",
|
360
464
|
...variables
|
361
465
|
});
|
466
|
+
const searchTable = (variables) => fetch$1({
|
467
|
+
url: "/db/{dbBranchName}/tables/{tableName}/search",
|
468
|
+
method: "post",
|
469
|
+
...variables
|
470
|
+
});
|
362
471
|
const searchBranch = (variables) => fetch$1({
|
363
472
|
url: "/db/{dbBranchName}/search",
|
364
473
|
method: "post",
|
@@ -376,11 +485,21 @@ const operationsByTag = {
|
|
376
485
|
updateWorkspaceMemberRole,
|
377
486
|
removeWorkspaceMember,
|
378
487
|
inviteWorkspaceMember,
|
488
|
+
updateWorkspaceMemberInvite,
|
379
489
|
cancelWorkspaceMemberInvite,
|
380
490
|
resendWorkspaceMemberInvite,
|
381
491
|
acceptWorkspaceMemberInvite
|
382
492
|
},
|
383
|
-
database: {
|
493
|
+
database: {
|
494
|
+
getDatabaseList,
|
495
|
+
createDatabase,
|
496
|
+
deleteDatabase,
|
497
|
+
getDatabaseMetadata,
|
498
|
+
getGitBranchesMapping,
|
499
|
+
addGitBranchesEntry,
|
500
|
+
removeGitBranchesEntry,
|
501
|
+
resolveBranch
|
502
|
+
},
|
384
503
|
branch: {
|
385
504
|
getBranchList,
|
386
505
|
getBranchDetails,
|
@@ -414,6 +533,7 @@ const operationsByTag = {
|
|
414
533
|
getRecord,
|
415
534
|
bulkInsertTableRecords,
|
416
535
|
queryTable,
|
536
|
+
searchTable,
|
417
537
|
searchBranch
|
418
538
|
}
|
419
539
|
};
|
@@ -447,7 +567,7 @@ var __accessCheck$7 = (obj, member, msg) => {
|
|
447
567
|
if (!member.has(obj))
|
448
568
|
throw TypeError("Cannot " + msg);
|
449
569
|
};
|
450
|
-
var __privateGet$
|
570
|
+
var __privateGet$7 = (obj, member, getter) => {
|
451
571
|
__accessCheck$7(obj, member, "read from private field");
|
452
572
|
return getter ? getter.call(obj) : member.get(obj);
|
453
573
|
};
|
@@ -456,7 +576,7 @@ var __privateAdd$7 = (obj, member, value) => {
|
|
456
576
|
throw TypeError("Cannot add the same private member more than once");
|
457
577
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
458
578
|
};
|
459
|
-
var __privateSet$
|
579
|
+
var __privateSet$7 = (obj, member, value, setter) => {
|
460
580
|
__accessCheck$7(obj, member, "write to private field");
|
461
581
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
462
582
|
return value;
|
@@ -471,7 +591,7 @@ class XataApiClient {
|
|
471
591
|
if (!apiKey) {
|
472
592
|
throw new Error("Could not resolve a valid apiKey");
|
473
593
|
}
|
474
|
-
__privateSet$
|
594
|
+
__privateSet$7(this, _extraProps, {
|
475
595
|
apiUrl: getHostUrl(provider, "main"),
|
476
596
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
477
597
|
fetchImpl: getFetchImplementation(options.fetch),
|
@@ -479,34 +599,34 @@ class XataApiClient {
|
|
479
599
|
});
|
480
600
|
}
|
481
601
|
get user() {
|
482
|
-
if (!__privateGet$
|
483
|
-
__privateGet$
|
484
|
-
return __privateGet$
|
602
|
+
if (!__privateGet$7(this, _namespaces).user)
|
603
|
+
__privateGet$7(this, _namespaces).user = new UserApi(__privateGet$7(this, _extraProps));
|
604
|
+
return __privateGet$7(this, _namespaces).user;
|
485
605
|
}
|
486
606
|
get workspaces() {
|
487
|
-
if (!__privateGet$
|
488
|
-
__privateGet$
|
489
|
-
return __privateGet$
|
607
|
+
if (!__privateGet$7(this, _namespaces).workspaces)
|
608
|
+
__privateGet$7(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$7(this, _extraProps));
|
609
|
+
return __privateGet$7(this, _namespaces).workspaces;
|
490
610
|
}
|
491
611
|
get databases() {
|
492
|
-
if (!__privateGet$
|
493
|
-
__privateGet$
|
494
|
-
return __privateGet$
|
612
|
+
if (!__privateGet$7(this, _namespaces).databases)
|
613
|
+
__privateGet$7(this, _namespaces).databases = new DatabaseApi(__privateGet$7(this, _extraProps));
|
614
|
+
return __privateGet$7(this, _namespaces).databases;
|
495
615
|
}
|
496
616
|
get branches() {
|
497
|
-
if (!__privateGet$
|
498
|
-
__privateGet$
|
499
|
-
return __privateGet$
|
617
|
+
if (!__privateGet$7(this, _namespaces).branches)
|
618
|
+
__privateGet$7(this, _namespaces).branches = new BranchApi(__privateGet$7(this, _extraProps));
|
619
|
+
return __privateGet$7(this, _namespaces).branches;
|
500
620
|
}
|
501
621
|
get tables() {
|
502
|
-
if (!__privateGet$
|
503
|
-
__privateGet$
|
504
|
-
return __privateGet$
|
622
|
+
if (!__privateGet$7(this, _namespaces).tables)
|
623
|
+
__privateGet$7(this, _namespaces).tables = new TableApi(__privateGet$7(this, _extraProps));
|
624
|
+
return __privateGet$7(this, _namespaces).tables;
|
505
625
|
}
|
506
626
|
get records() {
|
507
|
-
if (!__privateGet$
|
508
|
-
__privateGet$
|
509
|
-
return __privateGet$
|
627
|
+
if (!__privateGet$7(this, _namespaces).records)
|
628
|
+
__privateGet$7(this, _namespaces).records = new RecordsApi(__privateGet$7(this, _extraProps));
|
629
|
+
return __privateGet$7(this, _namespaces).records;
|
510
630
|
}
|
511
631
|
}
|
512
632
|
_extraProps = new WeakMap();
|
@@ -598,6 +718,13 @@ class WorkspaceApi {
|
|
598
718
|
...this.extraProps
|
599
719
|
});
|
600
720
|
}
|
721
|
+
updateWorkspaceMemberInvite(workspaceId, inviteId, role) {
|
722
|
+
return operationsByTag.workspaces.updateWorkspaceMemberInvite({
|
723
|
+
pathParams: { workspaceId, inviteId },
|
724
|
+
body: { role },
|
725
|
+
...this.extraProps
|
726
|
+
});
|
727
|
+
}
|
601
728
|
cancelWorkspaceMemberInvite(workspaceId, inviteId) {
|
602
729
|
return operationsByTag.workspaces.cancelWorkspaceMemberInvite({
|
603
730
|
pathParams: { workspaceId, inviteId },
|
@@ -640,6 +767,39 @@ class DatabaseApi {
|
|
640
767
|
...this.extraProps
|
641
768
|
});
|
642
769
|
}
|
770
|
+
getDatabaseMetadata(workspace, dbName) {
|
771
|
+
return operationsByTag.database.getDatabaseMetadata({
|
772
|
+
pathParams: { workspace, dbName },
|
773
|
+
...this.extraProps
|
774
|
+
});
|
775
|
+
}
|
776
|
+
getGitBranchesMapping(workspace, dbName) {
|
777
|
+
return operationsByTag.database.getGitBranchesMapping({
|
778
|
+
pathParams: { workspace, dbName },
|
779
|
+
...this.extraProps
|
780
|
+
});
|
781
|
+
}
|
782
|
+
addGitBranchesEntry(workspace, dbName, body) {
|
783
|
+
return operationsByTag.database.addGitBranchesEntry({
|
784
|
+
pathParams: { workspace, dbName },
|
785
|
+
body,
|
786
|
+
...this.extraProps
|
787
|
+
});
|
788
|
+
}
|
789
|
+
removeGitBranchesEntry(workspace, dbName, gitBranch) {
|
790
|
+
return operationsByTag.database.removeGitBranchesEntry({
|
791
|
+
pathParams: { workspace, dbName },
|
792
|
+
queryParams: { gitBranch },
|
793
|
+
...this.extraProps
|
794
|
+
});
|
795
|
+
}
|
796
|
+
resolveBranch(workspace, dbName, gitBranch, fallbackBranch) {
|
797
|
+
return operationsByTag.database.resolveBranch({
|
798
|
+
pathParams: { workspace, dbName },
|
799
|
+
queryParams: { gitBranch, fallbackBranch },
|
800
|
+
...this.extraProps
|
801
|
+
});
|
802
|
+
}
|
643
803
|
}
|
644
804
|
class BranchApi {
|
645
805
|
constructor(extraProps) {
|
@@ -657,10 +817,10 @@ class BranchApi {
|
|
657
817
|
...this.extraProps
|
658
818
|
});
|
659
819
|
}
|
660
|
-
createBranch(workspace, database, branch, from
|
820
|
+
createBranch(workspace, database, branch, from, options = {}) {
|
661
821
|
return operationsByTag.branch.createBranch({
|
662
822
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
663
|
-
queryParams: { from },
|
823
|
+
queryParams: isString(from) ? { from } : void 0,
|
664
824
|
body: options,
|
665
825
|
...this.extraProps
|
666
826
|
});
|
@@ -785,9 +945,10 @@ class RecordsApi {
|
|
785
945
|
constructor(extraProps) {
|
786
946
|
this.extraProps = extraProps;
|
787
947
|
}
|
788
|
-
insertRecord(workspace, database, branch, tableName, record) {
|
948
|
+
insertRecord(workspace, database, branch, tableName, record, options = {}) {
|
789
949
|
return operationsByTag.records.insertRecord({
|
790
950
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
951
|
+
queryParams: options,
|
791
952
|
body: record,
|
792
953
|
...this.extraProps
|
793
954
|
});
|
@@ -816,21 +977,24 @@ class RecordsApi {
|
|
816
977
|
...this.extraProps
|
817
978
|
});
|
818
979
|
}
|
819
|
-
deleteRecord(workspace, database, branch, tableName, recordId) {
|
980
|
+
deleteRecord(workspace, database, branch, tableName, recordId, options = {}) {
|
820
981
|
return operationsByTag.records.deleteRecord({
|
821
982
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
|
983
|
+
queryParams: options,
|
822
984
|
...this.extraProps
|
823
985
|
});
|
824
986
|
}
|
825
987
|
getRecord(workspace, database, branch, tableName, recordId, options = {}) {
|
826
988
|
return operationsByTag.records.getRecord({
|
827
989
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
|
990
|
+
queryParams: options,
|
828
991
|
...this.extraProps
|
829
992
|
});
|
830
993
|
}
|
831
|
-
bulkInsertTableRecords(workspace, database, branch, tableName, records) {
|
994
|
+
bulkInsertTableRecords(workspace, database, branch, tableName, records, options = {}) {
|
832
995
|
return operationsByTag.records.bulkInsertTableRecords({
|
833
996
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
997
|
+
queryParams: options,
|
834
998
|
body: { records },
|
835
999
|
...this.extraProps
|
836
1000
|
});
|
@@ -842,6 +1006,13 @@ class RecordsApi {
|
|
842
1006
|
...this.extraProps
|
843
1007
|
});
|
844
1008
|
}
|
1009
|
+
searchTable(workspace, database, branch, tableName, query) {
|
1010
|
+
return operationsByTag.records.searchTable({
|
1011
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
1012
|
+
body: query,
|
1013
|
+
...this.extraProps
|
1014
|
+
});
|
1015
|
+
}
|
845
1016
|
searchBranch(workspace, database, branch, query) {
|
846
1017
|
return operationsByTag.records.searchBranch({
|
847
1018
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
@@ -865,7 +1036,7 @@ var __accessCheck$6 = (obj, member, msg) => {
|
|
865
1036
|
if (!member.has(obj))
|
866
1037
|
throw TypeError("Cannot " + msg);
|
867
1038
|
};
|
868
|
-
var __privateGet$
|
1039
|
+
var __privateGet$6 = (obj, member, getter) => {
|
869
1040
|
__accessCheck$6(obj, member, "read from private field");
|
870
1041
|
return getter ? getter.call(obj) : member.get(obj);
|
871
1042
|
};
|
@@ -874,30 +1045,30 @@ var __privateAdd$6 = (obj, member, value) => {
|
|
874
1045
|
throw TypeError("Cannot add the same private member more than once");
|
875
1046
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
876
1047
|
};
|
877
|
-
var __privateSet$
|
1048
|
+
var __privateSet$6 = (obj, member, value, setter) => {
|
878
1049
|
__accessCheck$6(obj, member, "write to private field");
|
879
1050
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
880
1051
|
return value;
|
881
1052
|
};
|
882
|
-
var _query;
|
1053
|
+
var _query, _page;
|
883
1054
|
class Page {
|
884
1055
|
constructor(query, meta, records = []) {
|
885
1056
|
__privateAdd$6(this, _query, void 0);
|
886
|
-
__privateSet$
|
1057
|
+
__privateSet$6(this, _query, query);
|
887
1058
|
this.meta = meta;
|
888
|
-
this.records = records;
|
1059
|
+
this.records = new RecordArray(this, records);
|
889
1060
|
}
|
890
1061
|
async nextPage(size, offset) {
|
891
|
-
return __privateGet$
|
1062
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
|
892
1063
|
}
|
893
1064
|
async previousPage(size, offset) {
|
894
|
-
return __privateGet$
|
1065
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, before: this.meta.page.cursor } });
|
895
1066
|
}
|
896
1067
|
async firstPage(size, offset) {
|
897
|
-
return __privateGet$
|
1068
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, first: this.meta.page.cursor } });
|
898
1069
|
}
|
899
1070
|
async lastPage(size, offset) {
|
900
|
-
return __privateGet$
|
1071
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, last: this.meta.page.cursor } });
|
901
1072
|
}
|
902
1073
|
hasNextPage() {
|
903
1074
|
return this.meta.page.more;
|
@@ -905,15 +1076,62 @@ class Page {
|
|
905
1076
|
}
|
906
1077
|
_query = new WeakMap();
|
907
1078
|
const PAGINATION_MAX_SIZE = 200;
|
908
|
-
const PAGINATION_DEFAULT_SIZE =
|
1079
|
+
const PAGINATION_DEFAULT_SIZE = 20;
|
909
1080
|
const PAGINATION_MAX_OFFSET = 800;
|
910
1081
|
const PAGINATION_DEFAULT_OFFSET = 0;
|
1082
|
+
function isCursorPaginationOptions(options) {
|
1083
|
+
return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
|
1084
|
+
}
|
1085
|
+
const _RecordArray = class extends Array {
|
1086
|
+
constructor(...args) {
|
1087
|
+
super(..._RecordArray.parseConstructorParams(...args));
|
1088
|
+
__privateAdd$6(this, _page, void 0);
|
1089
|
+
__privateSet$6(this, _page, isObject(args[0]?.meta) ? args[0] : { meta: { page: { cursor: "", more: false } }, records: [] });
|
1090
|
+
}
|
1091
|
+
static parseConstructorParams(...args) {
|
1092
|
+
if (args.length === 1 && typeof args[0] === "number") {
|
1093
|
+
return new Array(args[0]);
|
1094
|
+
}
|
1095
|
+
if (args.length <= 2 && isObject(args[0]?.meta) && Array.isArray(args[1] ?? [])) {
|
1096
|
+
const result = args[1] ?? args[0].records ?? [];
|
1097
|
+
return new Array(...result);
|
1098
|
+
}
|
1099
|
+
return new Array(...args);
|
1100
|
+
}
|
1101
|
+
toArray() {
|
1102
|
+
return new Array(...this);
|
1103
|
+
}
|
1104
|
+
map(callbackfn, thisArg) {
|
1105
|
+
return this.toArray().map(callbackfn, thisArg);
|
1106
|
+
}
|
1107
|
+
async nextPage(size, offset) {
|
1108
|
+
const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
|
1109
|
+
return new _RecordArray(newPage);
|
1110
|
+
}
|
1111
|
+
async previousPage(size, offset) {
|
1112
|
+
const newPage = await __privateGet$6(this, _page).previousPage(size, offset);
|
1113
|
+
return new _RecordArray(newPage);
|
1114
|
+
}
|
1115
|
+
async firstPage(size, offset) {
|
1116
|
+
const newPage = await __privateGet$6(this, _page).firstPage(size, offset);
|
1117
|
+
return new _RecordArray(newPage);
|
1118
|
+
}
|
1119
|
+
async lastPage(size, offset) {
|
1120
|
+
const newPage = await __privateGet$6(this, _page).lastPage(size, offset);
|
1121
|
+
return new _RecordArray(newPage);
|
1122
|
+
}
|
1123
|
+
hasNextPage() {
|
1124
|
+
return __privateGet$6(this, _page).meta.page.more;
|
1125
|
+
}
|
1126
|
+
};
|
1127
|
+
let RecordArray = _RecordArray;
|
1128
|
+
_page = new WeakMap();
|
911
1129
|
|
912
1130
|
var __accessCheck$5 = (obj, member, msg) => {
|
913
1131
|
if (!member.has(obj))
|
914
1132
|
throw TypeError("Cannot " + msg);
|
915
1133
|
};
|
916
|
-
var __privateGet$
|
1134
|
+
var __privateGet$5 = (obj, member, getter) => {
|
917
1135
|
__accessCheck$5(obj, member, "read from private field");
|
918
1136
|
return getter ? getter.call(obj) : member.get(obj);
|
919
1137
|
};
|
@@ -922,34 +1140,35 @@ var __privateAdd$5 = (obj, member, value) => {
|
|
922
1140
|
throw TypeError("Cannot add the same private member more than once");
|
923
1141
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
924
1142
|
};
|
925
|
-
var __privateSet$
|
1143
|
+
var __privateSet$5 = (obj, member, value, setter) => {
|
926
1144
|
__accessCheck$5(obj, member, "write to private field");
|
927
1145
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
928
1146
|
return value;
|
929
1147
|
};
|
930
1148
|
var _table$1, _repository, _data;
|
931
1149
|
const _Query = class {
|
932
|
-
constructor(repository, table, data,
|
1150
|
+
constructor(repository, table, data, rawParent) {
|
933
1151
|
__privateAdd$5(this, _table$1, void 0);
|
934
1152
|
__privateAdd$5(this, _repository, void 0);
|
935
1153
|
__privateAdd$5(this, _data, { filter: {} });
|
936
1154
|
this.meta = { page: { cursor: "start", more: true } };
|
937
|
-
this.records = [];
|
938
|
-
__privateSet$
|
1155
|
+
this.records = new RecordArray(this, []);
|
1156
|
+
__privateSet$5(this, _table$1, table);
|
939
1157
|
if (repository) {
|
940
|
-
__privateSet$
|
1158
|
+
__privateSet$5(this, _repository, repository);
|
941
1159
|
} else {
|
942
|
-
__privateSet$
|
1160
|
+
__privateSet$5(this, _repository, this);
|
943
1161
|
}
|
944
|
-
|
945
|
-
__privateGet$
|
946
|
-
__privateGet$
|
947
|
-
__privateGet$
|
948
|
-
__privateGet$
|
949
|
-
__privateGet$
|
950
|
-
__privateGet$
|
951
|
-
__privateGet$
|
952
|
-
__privateGet$
|
1162
|
+
const parent = cleanParent(data, rawParent);
|
1163
|
+
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
1164
|
+
__privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
|
1165
|
+
__privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
|
1166
|
+
__privateGet$5(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
|
1167
|
+
__privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
|
1168
|
+
__privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
|
1169
|
+
__privateGet$5(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
|
1170
|
+
__privateGet$5(this, _data).pagination = data.pagination ?? parent?.pagination;
|
1171
|
+
__privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
|
953
1172
|
this.any = this.any.bind(this);
|
954
1173
|
this.all = this.all.bind(this);
|
955
1174
|
this.not = this.not.bind(this);
|
@@ -960,80 +1179,93 @@ const _Query = class {
|
|
960
1179
|
Object.defineProperty(this, "repository", { enumerable: false });
|
961
1180
|
}
|
962
1181
|
getQueryOptions() {
|
963
|
-
return __privateGet$
|
1182
|
+
return __privateGet$5(this, _data);
|
964
1183
|
}
|
965
1184
|
key() {
|
966
|
-
const { columns = [], filter = {}, sort = [],
|
967
|
-
const key = JSON.stringify({ columns, filter, sort,
|
1185
|
+
const { columns = [], filter = {}, sort = [], pagination = {} } = __privateGet$5(this, _data);
|
1186
|
+
const key = JSON.stringify({ columns, filter, sort, pagination });
|
968
1187
|
return toBase64(key);
|
969
1188
|
}
|
970
1189
|
any(...queries) {
|
971
1190
|
const $any = queries.map((query) => query.getQueryOptions().filter ?? {});
|
972
|
-
return new _Query(__privateGet$
|
1191
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $any } }, __privateGet$5(this, _data));
|
973
1192
|
}
|
974
1193
|
all(...queries) {
|
975
1194
|
const $all = queries.map((query) => query.getQueryOptions().filter ?? {});
|
976
|
-
return new _Query(__privateGet$
|
1195
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
977
1196
|
}
|
978
1197
|
not(...queries) {
|
979
1198
|
const $not = queries.map((query) => query.getQueryOptions().filter ?? {});
|
980
|
-
return new _Query(__privateGet$
|
1199
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $not } }, __privateGet$5(this, _data));
|
981
1200
|
}
|
982
1201
|
none(...queries) {
|
983
1202
|
const $none = queries.map((query) => query.getQueryOptions().filter ?? {});
|
984
|
-
return new _Query(__privateGet$
|
1203
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $none } }, __privateGet$5(this, _data));
|
985
1204
|
}
|
986
1205
|
filter(a, b) {
|
987
1206
|
if (arguments.length === 1) {
|
988
1207
|
const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
|
989
|
-
const $all = compact([__privateGet$
|
990
|
-
return new _Query(__privateGet$
|
1208
|
+
const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat(constraints));
|
1209
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
991
1210
|
} else {
|
992
|
-
const $all = compact([__privateGet$
|
993
|
-
return new _Query(__privateGet$
|
1211
|
+
const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
|
1212
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
994
1213
|
}
|
995
1214
|
}
|
996
|
-
sort(column, direction) {
|
997
|
-
const originalSort = [__privateGet$
|
1215
|
+
sort(column, direction = "asc") {
|
1216
|
+
const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
|
998
1217
|
const sort = [...originalSort, { column, direction }];
|
999
|
-
return new _Query(__privateGet$
|
1218
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
|
1000
1219
|
}
|
1001
1220
|
select(columns) {
|
1002
|
-
return new _Query(
|
1221
|
+
return new _Query(
|
1222
|
+
__privateGet$5(this, _repository),
|
1223
|
+
__privateGet$5(this, _table$1),
|
1224
|
+
{ columns },
|
1225
|
+
__privateGet$5(this, _data)
|
1226
|
+
);
|
1003
1227
|
}
|
1004
1228
|
getPaginated(options = {}) {
|
1005
|
-
const query = new _Query(__privateGet$
|
1006
|
-
return __privateGet$
|
1229
|
+
const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
|
1230
|
+
return __privateGet$5(this, _repository).query(query);
|
1007
1231
|
}
|
1008
1232
|
async *[Symbol.asyncIterator]() {
|
1009
|
-
for await (const [record] of this.getIterator(1)) {
|
1233
|
+
for await (const [record] of this.getIterator({ batchSize: 1 })) {
|
1010
1234
|
yield record;
|
1011
1235
|
}
|
1012
1236
|
}
|
1013
|
-
async *getIterator(
|
1014
|
-
|
1015
|
-
let
|
1016
|
-
|
1017
|
-
|
1018
|
-
|
1019
|
-
|
1020
|
-
|
1237
|
+
async *getIterator(options = {}) {
|
1238
|
+
const { batchSize = 1 } = options;
|
1239
|
+
let page = await this.getPaginated({ ...options, pagination: { size: batchSize, offset: 0 } });
|
1240
|
+
let more = page.hasNextPage();
|
1241
|
+
yield page.records;
|
1242
|
+
while (more) {
|
1243
|
+
page = await page.nextPage();
|
1244
|
+
more = page.hasNextPage();
|
1245
|
+
yield page.records;
|
1021
1246
|
}
|
1022
1247
|
}
|
1023
1248
|
async getMany(options = {}) {
|
1024
|
-
const
|
1025
|
-
|
1249
|
+
const page = await this.getPaginated(options);
|
1250
|
+
if (page.hasNextPage() && options.pagination?.size === void 0) {
|
1251
|
+
console.trace("Calling getMany does not return all results. Paginate to get all results or call getAll.");
|
1252
|
+
}
|
1253
|
+
return page.records;
|
1026
1254
|
}
|
1027
|
-
async getAll(
|
1255
|
+
async getAll(options = {}) {
|
1256
|
+
const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
|
1028
1257
|
const results = [];
|
1029
|
-
for await (const page of this.getIterator(
|
1258
|
+
for await (const page of this.getIterator({ ...rest, batchSize })) {
|
1030
1259
|
results.push(...page);
|
1031
1260
|
}
|
1032
1261
|
return results;
|
1033
1262
|
}
|
1034
1263
|
async getFirst(options = {}) {
|
1035
|
-
const records = await this.getMany({ ...options,
|
1036
|
-
return records[0]
|
1264
|
+
const records = await this.getMany({ ...options, pagination: { size: 1 } });
|
1265
|
+
return records[0] ?? null;
|
1266
|
+
}
|
1267
|
+
cache(ttl) {
|
1268
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
|
1037
1269
|
}
|
1038
1270
|
nextPage(size, offset) {
|
1039
1271
|
return this.firstPage(size, offset);
|
@@ -1042,10 +1274,10 @@ const _Query = class {
|
|
1042
1274
|
return this.firstPage(size, offset);
|
1043
1275
|
}
|
1044
1276
|
firstPage(size, offset) {
|
1045
|
-
return this.getPaginated({
|
1277
|
+
return this.getPaginated({ pagination: { size, offset } });
|
1046
1278
|
}
|
1047
1279
|
lastPage(size, offset) {
|
1048
|
-
return this.getPaginated({
|
1280
|
+
return this.getPaginated({ pagination: { size, offset, before: "end" } });
|
1049
1281
|
}
|
1050
1282
|
hasNextPage() {
|
1051
1283
|
return this.meta.page.more;
|
@@ -1055,12 +1287,20 @@ let Query = _Query;
|
|
1055
1287
|
_table$1 = new WeakMap();
|
1056
1288
|
_repository = new WeakMap();
|
1057
1289
|
_data = new WeakMap();
|
1290
|
+
function cleanParent(data, parent) {
|
1291
|
+
if (isCursorPaginationOptions(data.pagination)) {
|
1292
|
+
return { ...parent, sorting: void 0, filter: void 0 };
|
1293
|
+
}
|
1294
|
+
return parent;
|
1295
|
+
}
|
1058
1296
|
|
1059
1297
|
function isIdentifiable(x) {
|
1060
1298
|
return isObject(x) && isString(x?.id);
|
1061
1299
|
}
|
1062
1300
|
function isXataRecord(x) {
|
1063
|
-
|
1301
|
+
const record = x;
|
1302
|
+
const metadata = record?.getMetadata();
|
1303
|
+
return isIdentifiable(x) && isObject(metadata) && typeof metadata.version === "number";
|
1064
1304
|
}
|
1065
1305
|
|
1066
1306
|
function isSortFilterString(value) {
|
@@ -1090,7 +1330,7 @@ var __accessCheck$4 = (obj, member, msg) => {
|
|
1090
1330
|
if (!member.has(obj))
|
1091
1331
|
throw TypeError("Cannot " + msg);
|
1092
1332
|
};
|
1093
|
-
var __privateGet$
|
1333
|
+
var __privateGet$4 = (obj, member, getter) => {
|
1094
1334
|
__accessCheck$4(obj, member, "read from private field");
|
1095
1335
|
return getter ? getter.call(obj) : member.get(obj);
|
1096
1336
|
};
|
@@ -1099,7 +1339,7 @@ var __privateAdd$4 = (obj, member, value) => {
|
|
1099
1339
|
throw TypeError("Cannot add the same private member more than once");
|
1100
1340
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1101
1341
|
};
|
1102
|
-
var __privateSet$
|
1342
|
+
var __privateSet$4 = (obj, member, value, setter) => {
|
1103
1343
|
__accessCheck$4(obj, member, "write to private field");
|
1104
1344
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1105
1345
|
return value;
|
@@ -1108,7 +1348,7 @@ var __privateMethod$2 = (obj, member, method) => {
|
|
1108
1348
|
__accessCheck$4(obj, member, "access private method");
|
1109
1349
|
return method;
|
1110
1350
|
};
|
1111
|
-
var _table,
|
1351
|
+
var _table, _getFetchProps, _db, _cache, _schemaTables$2, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _bulkInsertTableRecords, bulkInsertTableRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn, _setCacheQuery, setCacheQuery_fn, _getCacheQuery, getCacheQuery_fn, _getSchemaTables$1, getSchemaTables_fn$1;
|
1112
1352
|
class Repository extends Query {
|
1113
1353
|
}
|
1114
1354
|
class RestRepository extends Query {
|
@@ -1120,109 +1360,122 @@ class RestRepository extends Query {
|
|
1120
1360
|
__privateAdd$4(this, _updateRecordWithID);
|
1121
1361
|
__privateAdd$4(this, _upsertRecordWithID);
|
1122
1362
|
__privateAdd$4(this, _deleteRecord);
|
1123
|
-
__privateAdd$4(this, _invalidateCache);
|
1124
|
-
__privateAdd$4(this, _setCacheRecord);
|
1125
|
-
__privateAdd$4(this, _getCacheRecord);
|
1126
1363
|
__privateAdd$4(this, _setCacheQuery);
|
1127
1364
|
__privateAdd$4(this, _getCacheQuery);
|
1365
|
+
__privateAdd$4(this, _getSchemaTables$1);
|
1128
1366
|
__privateAdd$4(this, _table, void 0);
|
1129
|
-
__privateAdd$4(this, _links, void 0);
|
1130
1367
|
__privateAdd$4(this, _getFetchProps, void 0);
|
1131
|
-
|
1132
|
-
|
1133
|
-
|
1134
|
-
this
|
1135
|
-
this
|
1136
|
-
|
1137
|
-
|
1368
|
+
__privateAdd$4(this, _db, void 0);
|
1369
|
+
__privateAdd$4(this, _cache, void 0);
|
1370
|
+
__privateAdd$4(this, _schemaTables$2, void 0);
|
1371
|
+
__privateSet$4(this, _table, options.table);
|
1372
|
+
__privateSet$4(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
1373
|
+
__privateSet$4(this, _db, options.db);
|
1374
|
+
__privateSet$4(this, _cache, options.pluginOptions.cache);
|
1375
|
+
__privateSet$4(this, _schemaTables$2, options.schemaTables);
|
1376
|
+
}
|
1377
|
+
async create(a, b, c) {
|
1138
1378
|
if (Array.isArray(a)) {
|
1139
|
-
|
1140
|
-
|
1141
|
-
|
1379
|
+
if (a.length === 0)
|
1380
|
+
return [];
|
1381
|
+
const columns = isStringArray(b) ? b : void 0;
|
1382
|
+
return __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a, columns);
|
1142
1383
|
}
|
1143
1384
|
if (isString(a) && isObject(b)) {
|
1144
1385
|
if (a === "")
|
1145
1386
|
throw new Error("The id can't be empty");
|
1146
|
-
const
|
1147
|
-
|
1148
|
-
return record;
|
1387
|
+
const columns = isStringArray(c) ? c : void 0;
|
1388
|
+
return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b, columns);
|
1149
1389
|
}
|
1150
1390
|
if (isObject(a) && isString(a.id)) {
|
1151
1391
|
if (a.id === "")
|
1152
1392
|
throw new Error("The id can't be empty");
|
1153
|
-
const
|
1154
|
-
|
1155
|
-
return record;
|
1393
|
+
const columns = isStringArray(b) ? b : void 0;
|
1394
|
+
return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1156
1395
|
}
|
1157
1396
|
if (isObject(a)) {
|
1158
|
-
const
|
1159
|
-
|
1160
|
-
return record;
|
1397
|
+
const columns = isStringArray(b) ? b : void 0;
|
1398
|
+
return __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a, columns);
|
1161
1399
|
}
|
1162
1400
|
throw new Error("Invalid arguments for create method");
|
1163
1401
|
}
|
1164
|
-
async read(
|
1165
|
-
const
|
1166
|
-
if (
|
1167
|
-
|
1168
|
-
|
1169
|
-
|
1170
|
-
const
|
1171
|
-
|
1172
|
-
|
1173
|
-
|
1174
|
-
|
1175
|
-
|
1176
|
-
|
1177
|
-
|
1402
|
+
async read(a, b) {
|
1403
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1404
|
+
if (Array.isArray(a)) {
|
1405
|
+
if (a.length === 0)
|
1406
|
+
return [];
|
1407
|
+
const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
|
1408
|
+
const finalObjects = await this.getAll({ filter: { id: { $any: ids } }, columns });
|
1409
|
+
const dictionary = finalObjects.reduce((acc, object) => {
|
1410
|
+
acc[object.id] = object;
|
1411
|
+
return acc;
|
1412
|
+
}, {});
|
1413
|
+
return ids.map((id2) => dictionary[id2] ?? null);
|
1414
|
+
}
|
1415
|
+
const id = isString(a) ? a : a.id;
|
1416
|
+
if (isString(id)) {
|
1417
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1418
|
+
try {
|
1419
|
+
const response = await getRecord({
|
1420
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
|
1421
|
+
queryParams: { columns },
|
1422
|
+
...fetchProps
|
1423
|
+
});
|
1424
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1425
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1426
|
+
} catch (e) {
|
1427
|
+
if (isObject(e) && e.status === 404) {
|
1428
|
+
return null;
|
1429
|
+
}
|
1430
|
+
throw e;
|
1178
1431
|
}
|
1179
|
-
throw e;
|
1180
1432
|
}
|
1433
|
+
return null;
|
1181
1434
|
}
|
1182
|
-
async update(a, b) {
|
1435
|
+
async update(a, b, c) {
|
1183
1436
|
if (Array.isArray(a)) {
|
1437
|
+
if (a.length === 0)
|
1438
|
+
return [];
|
1184
1439
|
if (a.length > 100) {
|
1185
1440
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1186
1441
|
}
|
1187
|
-
|
1442
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1443
|
+
return Promise.all(a.map((object) => this.update(object, columns)));
|
1188
1444
|
}
|
1189
1445
|
if (isString(a) && isObject(b)) {
|
1190
|
-
|
1191
|
-
|
1192
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1193
|
-
return record;
|
1446
|
+
const columns = isStringArray(c) ? c : void 0;
|
1447
|
+
return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b, columns);
|
1194
1448
|
}
|
1195
1449
|
if (isObject(a) && isString(a.id)) {
|
1196
|
-
|
1197
|
-
|
1198
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1199
|
-
return record;
|
1450
|
+
const columns = isStringArray(b) ? b : void 0;
|
1451
|
+
return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1200
1452
|
}
|
1201
1453
|
throw new Error("Invalid arguments for update method");
|
1202
1454
|
}
|
1203
|
-
async createOrUpdate(a, b) {
|
1455
|
+
async createOrUpdate(a, b, c) {
|
1204
1456
|
if (Array.isArray(a)) {
|
1457
|
+
if (a.length === 0)
|
1458
|
+
return [];
|
1205
1459
|
if (a.length > 100) {
|
1206
1460
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1207
1461
|
}
|
1208
|
-
|
1462
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1463
|
+
return Promise.all(a.map((object) => this.createOrUpdate(object, columns)));
|
1209
1464
|
}
|
1210
1465
|
if (isString(a) && isObject(b)) {
|
1211
|
-
|
1212
|
-
|
1213
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1214
|
-
return record;
|
1466
|
+
const columns = isStringArray(c) ? c : void 0;
|
1467
|
+
return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b, columns);
|
1215
1468
|
}
|
1216
1469
|
if (isObject(a) && isString(a.id)) {
|
1217
|
-
|
1218
|
-
|
1219
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1220
|
-
return record;
|
1470
|
+
const columns = isStringArray(c) ? c : void 0;
|
1471
|
+
return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1221
1472
|
}
|
1222
1473
|
throw new Error("Invalid arguments for createOrUpdate method");
|
1223
1474
|
}
|
1224
1475
|
async delete(a) {
|
1225
1476
|
if (Array.isArray(a)) {
|
1477
|
+
if (a.length === 0)
|
1478
|
+
return;
|
1226
1479
|
if (a.length > 100) {
|
1227
1480
|
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
1228
1481
|
}
|
@@ -1231,24 +1484,30 @@ class RestRepository extends Query {
|
|
1231
1484
|
}
|
1232
1485
|
if (isString(a)) {
|
1233
1486
|
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a);
|
1234
|
-
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
|
1235
1487
|
return;
|
1236
1488
|
}
|
1237
1489
|
if (isObject(a) && isString(a.id)) {
|
1238
1490
|
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id);
|
1239
|
-
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
|
1240
1491
|
return;
|
1241
1492
|
}
|
1242
1493
|
throw new Error("Invalid arguments for delete method");
|
1243
1494
|
}
|
1244
1495
|
async search(query, options = {}) {
|
1245
|
-
const fetchProps = await __privateGet$
|
1246
|
-
const { records } = await
|
1247
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1248
|
-
body: {
|
1496
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1497
|
+
const { records } = await searchTable({
|
1498
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1499
|
+
body: {
|
1500
|
+
query,
|
1501
|
+
fuzziness: options.fuzziness,
|
1502
|
+
prefix: options.prefix,
|
1503
|
+
highlight: options.highlight,
|
1504
|
+
filter: options.filter,
|
1505
|
+
boosters: options.boosters
|
1506
|
+
},
|
1249
1507
|
...fetchProps
|
1250
1508
|
});
|
1251
|
-
|
1509
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1510
|
+
return records.map((item) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), item));
|
1252
1511
|
}
|
1253
1512
|
async query(query) {
|
1254
1513
|
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
@@ -1257,149 +1516,139 @@ class RestRepository extends Query {
|
|
1257
1516
|
const data = query.getQueryOptions();
|
1258
1517
|
const body = {
|
1259
1518
|
filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
|
1260
|
-
sort: data.sort ? buildSortFilter(data.sort) : void 0,
|
1261
|
-
page: data.
|
1519
|
+
sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
|
1520
|
+
page: data.pagination,
|
1262
1521
|
columns: data.columns
|
1263
1522
|
};
|
1264
|
-
const fetchProps = await __privateGet$
|
1523
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1265
1524
|
const { meta, records: objects } = await queryTable({
|
1266
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1525
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1267
1526
|
body,
|
1268
1527
|
...fetchProps
|
1269
1528
|
});
|
1270
|
-
const
|
1529
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1530
|
+
const records = objects.map((record) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), record));
|
1271
1531
|
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1272
1532
|
return new Page(query, meta, records);
|
1273
1533
|
}
|
1274
1534
|
}
|
1275
1535
|
_table = new WeakMap();
|
1276
|
-
_links = new WeakMap();
|
1277
1536
|
_getFetchProps = new WeakMap();
|
1537
|
+
_db = new WeakMap();
|
1538
|
+
_cache = new WeakMap();
|
1539
|
+
_schemaTables$2 = new WeakMap();
|
1278
1540
|
_insertRecordWithoutId = new WeakSet();
|
1279
|
-
insertRecordWithoutId_fn = async function(object) {
|
1280
|
-
const fetchProps = await __privateGet$
|
1541
|
+
insertRecordWithoutId_fn = async function(object, columns = ["*"]) {
|
1542
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1281
1543
|
const record = transformObjectLinks(object);
|
1282
1544
|
const response = await insertRecord({
|
1283
1545
|
pathParams: {
|
1284
1546
|
workspace: "{workspaceId}",
|
1285
1547
|
dbBranchName: "{dbBranch}",
|
1286
|
-
tableName: __privateGet$
|
1548
|
+
tableName: __privateGet$4(this, _table)
|
1287
1549
|
},
|
1550
|
+
queryParams: { columns },
|
1288
1551
|
body: record,
|
1289
1552
|
...fetchProps
|
1290
1553
|
});
|
1291
|
-
const
|
1292
|
-
|
1293
|
-
throw new Error("The server failed to save the record");
|
1294
|
-
}
|
1295
|
-
return finalObject;
|
1554
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1555
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1296
1556
|
};
|
1297
1557
|
_insertRecordWithId = new WeakSet();
|
1298
|
-
insertRecordWithId_fn = async function(recordId, object) {
|
1299
|
-
const fetchProps = await __privateGet$
|
1558
|
+
insertRecordWithId_fn = async function(recordId, object, columns = ["*"]) {
|
1559
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1300
1560
|
const record = transformObjectLinks(object);
|
1301
1561
|
const response = await insertRecordWithID({
|
1302
1562
|
pathParams: {
|
1303
1563
|
workspace: "{workspaceId}",
|
1304
1564
|
dbBranchName: "{dbBranch}",
|
1305
|
-
tableName: __privateGet$
|
1565
|
+
tableName: __privateGet$4(this, _table),
|
1306
1566
|
recordId
|
1307
1567
|
},
|
1308
1568
|
body: record,
|
1309
|
-
queryParams: { createOnly: true },
|
1569
|
+
queryParams: { createOnly: true, columns },
|
1310
1570
|
...fetchProps
|
1311
1571
|
});
|
1312
|
-
const
|
1313
|
-
|
1314
|
-
throw new Error("The server failed to save the record");
|
1315
|
-
}
|
1316
|
-
return finalObject;
|
1572
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1573
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1317
1574
|
};
|
1318
1575
|
_bulkInsertTableRecords = new WeakSet();
|
1319
|
-
bulkInsertTableRecords_fn = async function(objects) {
|
1320
|
-
const fetchProps = await __privateGet$
|
1576
|
+
bulkInsertTableRecords_fn = async function(objects, columns = ["*"]) {
|
1577
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1321
1578
|
const records = objects.map((object) => transformObjectLinks(object));
|
1322
1579
|
const response = await bulkInsertTableRecords({
|
1323
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1580
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1581
|
+
queryParams: { columns },
|
1324
1582
|
body: { records },
|
1325
1583
|
...fetchProps
|
1326
1584
|
});
|
1327
|
-
|
1328
|
-
|
1329
|
-
throw new Error("The server failed to save some records");
|
1585
|
+
if (!isResponseWithRecords(response)) {
|
1586
|
+
throw new Error("Request included columns but server didn't include them");
|
1330
1587
|
}
|
1331
|
-
|
1588
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1589
|
+
return response.records?.map((item) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), item));
|
1332
1590
|
};
|
1333
1591
|
_updateRecordWithID = new WeakSet();
|
1334
|
-
updateRecordWithID_fn = async function(recordId, object) {
|
1335
|
-
const fetchProps = await __privateGet$
|
1592
|
+
updateRecordWithID_fn = async function(recordId, object, columns = ["*"]) {
|
1593
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1336
1594
|
const record = transformObjectLinks(object);
|
1337
1595
|
const response = await updateRecordWithID({
|
1338
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1596
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1597
|
+
queryParams: { columns },
|
1339
1598
|
body: record,
|
1340
1599
|
...fetchProps
|
1341
1600
|
});
|
1342
|
-
const
|
1343
|
-
|
1344
|
-
throw new Error("The server failed to save the record");
|
1345
|
-
return item;
|
1601
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1602
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1346
1603
|
};
|
1347
1604
|
_upsertRecordWithID = new WeakSet();
|
1348
|
-
upsertRecordWithID_fn = async function(recordId, object) {
|
1349
|
-
const fetchProps = await __privateGet$
|
1605
|
+
upsertRecordWithID_fn = async function(recordId, object, columns = ["*"]) {
|
1606
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1350
1607
|
const response = await upsertRecordWithID({
|
1351
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1608
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1609
|
+
queryParams: { columns },
|
1352
1610
|
body: object,
|
1353
1611
|
...fetchProps
|
1354
1612
|
});
|
1355
|
-
const
|
1356
|
-
|
1357
|
-
throw new Error("The server failed to save the record");
|
1358
|
-
return item;
|
1613
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1614
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1359
1615
|
};
|
1360
1616
|
_deleteRecord = new WeakSet();
|
1361
1617
|
deleteRecord_fn = async function(recordId) {
|
1362
|
-
const fetchProps = await __privateGet$
|
1618
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1363
1619
|
await deleteRecord({
|
1364
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1620
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1365
1621
|
...fetchProps
|
1366
1622
|
});
|
1367
1623
|
};
|
1368
|
-
_invalidateCache = new WeakSet();
|
1369
|
-
invalidateCache_fn = async function(recordId) {
|
1370
|
-
await this.cache.delete(`rec_${__privateGet$3(this, _table)}:${recordId}`);
|
1371
|
-
const cacheItems = await this.cache.getAll();
|
1372
|
-
const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
|
1373
|
-
for (const [key, value] of queries) {
|
1374
|
-
const ids = getIds(value);
|
1375
|
-
if (ids.includes(recordId))
|
1376
|
-
await this.cache.delete(key);
|
1377
|
-
}
|
1378
|
-
};
|
1379
|
-
_setCacheRecord = new WeakSet();
|
1380
|
-
setCacheRecord_fn = async function(record) {
|
1381
|
-
await this.cache.set(`rec_${__privateGet$3(this, _table)}:${record.id}`, record);
|
1382
|
-
};
|
1383
|
-
_getCacheRecord = new WeakSet();
|
1384
|
-
getCacheRecord_fn = async function(recordId) {
|
1385
|
-
return this.cache.get(`rec_${__privateGet$3(this, _table)}:${recordId}`);
|
1386
|
-
};
|
1387
1624
|
_setCacheQuery = new WeakSet();
|
1388
1625
|
setCacheQuery_fn = async function(query, meta, records) {
|
1389
|
-
await this.
|
1626
|
+
await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
|
1390
1627
|
};
|
1391
1628
|
_getCacheQuery = new WeakSet();
|
1392
1629
|
getCacheQuery_fn = async function(query) {
|
1393
|
-
const key = `query_${__privateGet$
|
1394
|
-
const result = await this.
|
1630
|
+
const key = `query_${__privateGet$4(this, _table)}:${query.key()}`;
|
1631
|
+
const result = await __privateGet$4(this, _cache).get(key);
|
1395
1632
|
if (!result)
|
1396
1633
|
return null;
|
1397
|
-
const { ttl =
|
1398
|
-
if (
|
1399
|
-
return
|
1634
|
+
const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
|
1635
|
+
if (ttl < 0)
|
1636
|
+
return null;
|
1400
1637
|
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1401
1638
|
return hasExpired ? null : result;
|
1402
1639
|
};
|
1640
|
+
_getSchemaTables$1 = new WeakSet();
|
1641
|
+
getSchemaTables_fn$1 = async function() {
|
1642
|
+
if (__privateGet$4(this, _schemaTables$2))
|
1643
|
+
return __privateGet$4(this, _schemaTables$2);
|
1644
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1645
|
+
const { schema } = await getBranchDetails({
|
1646
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1647
|
+
...fetchProps
|
1648
|
+
});
|
1649
|
+
__privateSet$4(this, _schemaTables$2, schema.tables);
|
1650
|
+
return schema.tables;
|
1651
|
+
};
|
1403
1652
|
const transformObjectLinks = (object) => {
|
1404
1653
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
1405
1654
|
if (key === "xata")
|
@@ -1407,47 +1656,63 @@ const transformObjectLinks = (object) => {
|
|
1407
1656
|
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1408
1657
|
}, {});
|
1409
1658
|
};
|
1410
|
-
const initObject = (db,
|
1659
|
+
const initObject = (db, schemaTables, table, object) => {
|
1411
1660
|
const result = {};
|
1412
|
-
|
1413
|
-
|
1414
|
-
|
1415
|
-
|
1416
|
-
|
1417
|
-
|
1418
|
-
|
1661
|
+
const { xata, ...rest } = object ?? {};
|
1662
|
+
Object.assign(result, rest);
|
1663
|
+
const { columns } = schemaTables.find(({ name }) => name === table) ?? {};
|
1664
|
+
if (!columns)
|
1665
|
+
console.error(`Table ${table} not found in schema`);
|
1666
|
+
for (const column of columns ?? []) {
|
1667
|
+
const value = result[column.name];
|
1668
|
+
switch (column.type) {
|
1669
|
+
case "datetime": {
|
1670
|
+
const date = value !== void 0 ? new Date(value) : void 0;
|
1671
|
+
if (date && isNaN(date.getTime())) {
|
1672
|
+
console.error(`Failed to parse date ${value} for field ${column.name}`);
|
1673
|
+
} else if (date) {
|
1674
|
+
result[column.name] = date;
|
1675
|
+
}
|
1676
|
+
break;
|
1677
|
+
}
|
1678
|
+
case "link": {
|
1679
|
+
const linkTable = column.link?.table;
|
1680
|
+
if (!linkTable) {
|
1681
|
+
console.error(`Failed to parse link for field ${column.name}`);
|
1682
|
+
} else if (isObject(value)) {
|
1683
|
+
result[column.name] = initObject(db, schemaTables, linkTable, value);
|
1684
|
+
}
|
1685
|
+
break;
|
1686
|
+
}
|
1419
1687
|
}
|
1420
1688
|
}
|
1421
|
-
result.read = function() {
|
1422
|
-
return db[table].read(result["id"]);
|
1689
|
+
result.read = function(columns2) {
|
1690
|
+
return db[table].read(result["id"], columns2);
|
1423
1691
|
};
|
1424
|
-
result.update = function(data) {
|
1425
|
-
return db[table].update(result["id"], data);
|
1692
|
+
result.update = function(data, columns2) {
|
1693
|
+
return db[table].update(result["id"], data, columns2);
|
1426
1694
|
};
|
1427
1695
|
result.delete = function() {
|
1428
1696
|
return db[table].delete(result["id"]);
|
1429
1697
|
};
|
1430
|
-
|
1698
|
+
result.getMetadata = function() {
|
1699
|
+
return xata;
|
1700
|
+
};
|
1701
|
+
for (const prop of ["read", "update", "delete", "getMetadata"]) {
|
1431
1702
|
Object.defineProperty(result, prop, { enumerable: false });
|
1432
1703
|
}
|
1433
1704
|
Object.freeze(result);
|
1434
1705
|
return result;
|
1435
1706
|
};
|
1436
|
-
function
|
1437
|
-
|
1438
|
-
return value.map((item) => getIds(item)).flat();
|
1439
|
-
}
|
1440
|
-
if (!isObject(value))
|
1441
|
-
return [];
|
1442
|
-
const nestedIds = Object.values(value).map((item) => getIds(item)).flat();
|
1443
|
-
return isString(value.id) ? [value.id, ...nestedIds] : nestedIds;
|
1707
|
+
function isResponseWithRecords(value) {
|
1708
|
+
return isObject(value) && Array.isArray(value.records);
|
1444
1709
|
}
|
1445
1710
|
|
1446
1711
|
var __accessCheck$3 = (obj, member, msg) => {
|
1447
1712
|
if (!member.has(obj))
|
1448
1713
|
throw TypeError("Cannot " + msg);
|
1449
1714
|
};
|
1450
|
-
var __privateGet$
|
1715
|
+
var __privateGet$3 = (obj, member, getter) => {
|
1451
1716
|
__accessCheck$3(obj, member, "read from private field");
|
1452
1717
|
return getter ? getter.call(obj) : member.get(obj);
|
1453
1718
|
};
|
@@ -1456,31 +1721,38 @@ var __privateAdd$3 = (obj, member, value) => {
|
|
1456
1721
|
throw TypeError("Cannot add the same private member more than once");
|
1457
1722
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1458
1723
|
};
|
1459
|
-
var __privateSet$
|
1724
|
+
var __privateSet$3 = (obj, member, value, setter) => {
|
1460
1725
|
__accessCheck$3(obj, member, "write to private field");
|
1461
1726
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1462
1727
|
return value;
|
1463
1728
|
};
|
1464
1729
|
var _map;
|
1465
1730
|
class SimpleCache {
|
1466
|
-
constructor() {
|
1731
|
+
constructor(options = {}) {
|
1467
1732
|
__privateAdd$3(this, _map, void 0);
|
1468
|
-
__privateSet$
|
1733
|
+
__privateSet$3(this, _map, /* @__PURE__ */ new Map());
|
1734
|
+
this.capacity = options.max ?? 500;
|
1735
|
+
this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
|
1469
1736
|
}
|
1470
1737
|
async getAll() {
|
1471
|
-
return Object.fromEntries(__privateGet$
|
1738
|
+
return Object.fromEntries(__privateGet$3(this, _map));
|
1472
1739
|
}
|
1473
1740
|
async get(key) {
|
1474
|
-
return __privateGet$
|
1741
|
+
return __privateGet$3(this, _map).get(key) ?? null;
|
1475
1742
|
}
|
1476
1743
|
async set(key, value) {
|
1477
|
-
|
1744
|
+
await this.delete(key);
|
1745
|
+
__privateGet$3(this, _map).set(key, value);
|
1746
|
+
if (__privateGet$3(this, _map).size > this.capacity) {
|
1747
|
+
const leastRecentlyUsed = __privateGet$3(this, _map).keys().next().value;
|
1748
|
+
await this.delete(leastRecentlyUsed);
|
1749
|
+
}
|
1478
1750
|
}
|
1479
1751
|
async delete(key) {
|
1480
|
-
__privateGet$
|
1752
|
+
__privateGet$3(this, _map).delete(key);
|
1481
1753
|
}
|
1482
1754
|
async clear() {
|
1483
|
-
return __privateGet$
|
1755
|
+
return __privateGet$3(this, _map).clear();
|
1484
1756
|
}
|
1485
1757
|
}
|
1486
1758
|
_map = new WeakMap();
|
@@ -1508,7 +1780,7 @@ var __accessCheck$2 = (obj, member, msg) => {
|
|
1508
1780
|
if (!member.has(obj))
|
1509
1781
|
throw TypeError("Cannot " + msg);
|
1510
1782
|
};
|
1511
|
-
var __privateGet$
|
1783
|
+
var __privateGet$2 = (obj, member, getter) => {
|
1512
1784
|
__accessCheck$2(obj, member, "read from private field");
|
1513
1785
|
return getter ? getter.call(obj) : member.get(obj);
|
1514
1786
|
};
|
@@ -1517,130 +1789,177 @@ var __privateAdd$2 = (obj, member, value) => {
|
|
1517
1789
|
throw TypeError("Cannot add the same private member more than once");
|
1518
1790
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1519
1791
|
};
|
1520
|
-
var
|
1792
|
+
var __privateSet$2 = (obj, member, value, setter) => {
|
1793
|
+
__accessCheck$2(obj, member, "write to private field");
|
1794
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1795
|
+
return value;
|
1796
|
+
};
|
1797
|
+
var _tables, _schemaTables$1;
|
1521
1798
|
class SchemaPlugin extends XataPlugin {
|
1522
|
-
constructor(
|
1799
|
+
constructor(schemaTables) {
|
1523
1800
|
super();
|
1524
|
-
this.links = links;
|
1525
|
-
this.tableNames = tableNames;
|
1526
1801
|
__privateAdd$2(this, _tables, {});
|
1802
|
+
__privateAdd$2(this, _schemaTables$1, void 0);
|
1803
|
+
__privateSet$2(this, _schemaTables$1, schemaTables);
|
1527
1804
|
}
|
1528
1805
|
build(pluginOptions) {
|
1529
|
-
const
|
1530
|
-
|
1531
|
-
|
1532
|
-
|
1533
|
-
|
1534
|
-
|
1535
|
-
__privateGet$
|
1806
|
+
const db = new Proxy(
|
1807
|
+
{},
|
1808
|
+
{
|
1809
|
+
get: (_target, table) => {
|
1810
|
+
if (!isString(table))
|
1811
|
+
throw new Error("Invalid table name");
|
1812
|
+
if (__privateGet$2(this, _tables)[table] === void 0) {
|
1813
|
+
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1814
|
+
}
|
1815
|
+
return __privateGet$2(this, _tables)[table];
|
1536
1816
|
}
|
1537
|
-
return __privateGet$1(this, _tables)[table];
|
1538
1817
|
}
|
1539
|
-
|
1540
|
-
|
1541
|
-
|
1818
|
+
);
|
1819
|
+
const tableNames = __privateGet$2(this, _schemaTables$1)?.map(({ name }) => name) ?? [];
|
1820
|
+
for (const table of tableNames) {
|
1821
|
+
db[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1542
1822
|
}
|
1543
1823
|
return db;
|
1544
1824
|
}
|
1545
1825
|
}
|
1546
1826
|
_tables = new WeakMap();
|
1827
|
+
_schemaTables$1 = new WeakMap();
|
1547
1828
|
|
1548
1829
|
var __accessCheck$1 = (obj, member, msg) => {
|
1549
1830
|
if (!member.has(obj))
|
1550
1831
|
throw TypeError("Cannot " + msg);
|
1551
1832
|
};
|
1833
|
+
var __privateGet$1 = (obj, member, getter) => {
|
1834
|
+
__accessCheck$1(obj, member, "read from private field");
|
1835
|
+
return getter ? getter.call(obj) : member.get(obj);
|
1836
|
+
};
|
1552
1837
|
var __privateAdd$1 = (obj, member, value) => {
|
1553
1838
|
if (member.has(obj))
|
1554
1839
|
throw TypeError("Cannot add the same private member more than once");
|
1555
1840
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1556
1841
|
};
|
1842
|
+
var __privateSet$1 = (obj, member, value, setter) => {
|
1843
|
+
__accessCheck$1(obj, member, "write to private field");
|
1844
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1845
|
+
return value;
|
1846
|
+
};
|
1557
1847
|
var __privateMethod$1 = (obj, member, method) => {
|
1558
1848
|
__accessCheck$1(obj, member, "access private method");
|
1559
1849
|
return method;
|
1560
1850
|
};
|
1561
|
-
var _search, search_fn;
|
1851
|
+
var _schemaTables, _search, search_fn, _getSchemaTables, getSchemaTables_fn;
|
1562
1852
|
class SearchPlugin extends XataPlugin {
|
1563
|
-
constructor(db,
|
1853
|
+
constructor(db, schemaTables) {
|
1564
1854
|
super();
|
1565
1855
|
this.db = db;
|
1566
|
-
this.links = links;
|
1567
1856
|
__privateAdd$1(this, _search);
|
1857
|
+
__privateAdd$1(this, _getSchemaTables);
|
1858
|
+
__privateAdd$1(this, _schemaTables, void 0);
|
1859
|
+
__privateSet$1(this, _schemaTables, schemaTables);
|
1568
1860
|
}
|
1569
1861
|
build({ getFetchProps }) {
|
1570
1862
|
return {
|
1571
1863
|
all: async (query, options = {}) => {
|
1572
1864
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1865
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1573
1866
|
return records.map((record) => {
|
1574
1867
|
const { table = "orphan" } = record.xata;
|
1575
|
-
return { table, record: initObject(this.db,
|
1868
|
+
return { table, record: initObject(this.db, schemaTables, table, record) };
|
1576
1869
|
});
|
1577
1870
|
},
|
1578
1871
|
byTable: async (query, options = {}) => {
|
1579
1872
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1873
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1580
1874
|
return records.reduce((acc, record) => {
|
1581
1875
|
const { table = "orphan" } = record.xata;
|
1582
1876
|
const items = acc[table] ?? [];
|
1583
|
-
const item = initObject(this.db,
|
1877
|
+
const item = initObject(this.db, schemaTables, table, record);
|
1584
1878
|
return { ...acc, [table]: [...items, item] };
|
1585
1879
|
}, {});
|
1586
1880
|
}
|
1587
1881
|
};
|
1588
1882
|
}
|
1589
1883
|
}
|
1884
|
+
_schemaTables = new WeakMap();
|
1590
1885
|
_search = new WeakSet();
|
1591
1886
|
search_fn = async function(query, options, getFetchProps) {
|
1592
1887
|
const fetchProps = await getFetchProps();
|
1593
|
-
const { tables, fuzziness } = options ?? {};
|
1888
|
+
const { tables, fuzziness, highlight, prefix } = options ?? {};
|
1594
1889
|
const { records } = await searchBranch({
|
1595
1890
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1596
|
-
body: { tables, query, fuzziness },
|
1891
|
+
body: { tables, query, fuzziness, prefix, highlight },
|
1597
1892
|
...fetchProps
|
1598
1893
|
});
|
1599
1894
|
return records;
|
1600
1895
|
};
|
1896
|
+
_getSchemaTables = new WeakSet();
|
1897
|
+
getSchemaTables_fn = async function(getFetchProps) {
|
1898
|
+
if (__privateGet$1(this, _schemaTables))
|
1899
|
+
return __privateGet$1(this, _schemaTables);
|
1900
|
+
const fetchProps = await getFetchProps();
|
1901
|
+
const { schema } = await getBranchDetails({
|
1902
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1903
|
+
...fetchProps
|
1904
|
+
});
|
1905
|
+
__privateSet$1(this, _schemaTables, schema.tables);
|
1906
|
+
return schema.tables;
|
1907
|
+
};
|
1601
1908
|
|
1602
1909
|
const isBranchStrategyBuilder = (strategy) => {
|
1603
1910
|
return typeof strategy === "function";
|
1604
1911
|
};
|
1605
1912
|
|
1606
|
-
const envBranchNames = [
|
1607
|
-
"XATA_BRANCH",
|
1608
|
-
"VERCEL_GIT_COMMIT_REF",
|
1609
|
-
"CF_PAGES_BRANCH",
|
1610
|
-
"BRANCH"
|
1611
|
-
];
|
1612
|
-
const defaultBranch = "main";
|
1613
1913
|
async function getCurrentBranchName(options) {
|
1614
|
-
const
|
1615
|
-
if (
|
1616
|
-
|
1617
|
-
|
1618
|
-
|
1619
|
-
|
1620
|
-
|
1621
|
-
|
1622
|
-
|
1623
|
-
return defaultBranch;
|
1914
|
+
const { branch, envBranch } = getEnvironment();
|
1915
|
+
if (branch) {
|
1916
|
+
const details = await getDatabaseBranch(branch, options);
|
1917
|
+
if (details)
|
1918
|
+
return branch;
|
1919
|
+
console.warn(`Branch ${branch} not found in Xata. Ignoring...`);
|
1920
|
+
}
|
1921
|
+
const gitBranch = envBranch || await getGitBranch();
|
1922
|
+
return resolveXataBranch(gitBranch, options);
|
1624
1923
|
}
|
1625
1924
|
async function getCurrentBranchDetails(options) {
|
1626
|
-
const
|
1627
|
-
|
1628
|
-
|
1629
|
-
|
1630
|
-
|
1631
|
-
|
1632
|
-
|
1633
|
-
|
1634
|
-
|
1635
|
-
|
1925
|
+
const branch = await getCurrentBranchName(options);
|
1926
|
+
return getDatabaseBranch(branch, options);
|
1927
|
+
}
|
1928
|
+
async function resolveXataBranch(gitBranch, options) {
|
1929
|
+
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1930
|
+
const apiKey = options?.apiKey || getAPIKey();
|
1931
|
+
if (!databaseURL)
|
1932
|
+
throw new Error(
|
1933
|
+
"A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely"
|
1934
|
+
);
|
1935
|
+
if (!apiKey)
|
1936
|
+
throw new Error(
|
1937
|
+
"An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely"
|
1938
|
+
);
|
1939
|
+
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1940
|
+
const [workspace] = host.split(".");
|
1941
|
+
const { fallbackBranch } = getEnvironment();
|
1942
|
+
const { branch } = await resolveBranch({
|
1943
|
+
apiKey,
|
1944
|
+
apiUrl: databaseURL,
|
1945
|
+
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1946
|
+
workspacesApiUrl: `${protocol}//${host}`,
|
1947
|
+
pathParams: { dbName, workspace },
|
1948
|
+
queryParams: { gitBranch, fallbackBranch }
|
1949
|
+
});
|
1950
|
+
return branch;
|
1636
1951
|
}
|
1637
1952
|
async function getDatabaseBranch(branch, options) {
|
1638
1953
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1639
1954
|
const apiKey = options?.apiKey || getAPIKey();
|
1640
1955
|
if (!databaseURL)
|
1641
|
-
throw new Error(
|
1956
|
+
throw new Error(
|
1957
|
+
"A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely"
|
1958
|
+
);
|
1642
1959
|
if (!apiKey)
|
1643
|
-
throw new Error(
|
1960
|
+
throw new Error(
|
1961
|
+
"An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely"
|
1962
|
+
);
|
1644
1963
|
const [protocol, , host, , database] = databaseURL.split("/");
|
1645
1964
|
const [workspace] = host.split(".");
|
1646
1965
|
const dbBranchName = `${database}:${branch}`;
|
@@ -1650,10 +1969,7 @@ async function getDatabaseBranch(branch, options) {
|
|
1650
1969
|
apiUrl: databaseURL,
|
1651
1970
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1652
1971
|
workspacesApiUrl: `${protocol}//${host}`,
|
1653
|
-
pathParams: {
|
1654
|
-
dbBranchName,
|
1655
|
-
workspace
|
1656
|
-
}
|
1972
|
+
pathParams: { dbBranchName, workspace }
|
1657
1973
|
});
|
1658
1974
|
} catch (err) {
|
1659
1975
|
if (isObject(err) && err.status === 404)
|
@@ -1661,21 +1977,10 @@ async function getDatabaseBranch(branch, options) {
|
|
1661
1977
|
throw err;
|
1662
1978
|
}
|
1663
1979
|
}
|
1664
|
-
function getBranchByEnvVariable() {
|
1665
|
-
for (const name of envBranchNames) {
|
1666
|
-
const value = getEnvVariable(name);
|
1667
|
-
if (value) {
|
1668
|
-
return value;
|
1669
|
-
}
|
1670
|
-
}
|
1671
|
-
try {
|
1672
|
-
return XATA_BRANCH;
|
1673
|
-
} catch (err) {
|
1674
|
-
}
|
1675
|
-
}
|
1676
1980
|
function getDatabaseURL() {
|
1677
1981
|
try {
|
1678
|
-
|
1982
|
+
const { databaseURL } = getEnvironment();
|
1983
|
+
return databaseURL;
|
1679
1984
|
} catch (err) {
|
1680
1985
|
return void 0;
|
1681
1986
|
}
|
@@ -1704,24 +2009,26 @@ var __privateMethod = (obj, member, method) => {
|
|
1704
2009
|
return method;
|
1705
2010
|
};
|
1706
2011
|
const buildClient = (plugins) => {
|
1707
|
-
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
2012
|
+
var _branch, _options, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1708
2013
|
return _a = class {
|
1709
|
-
constructor(options = {},
|
2014
|
+
constructor(options = {}, schemaTables) {
|
1710
2015
|
__privateAdd(this, _parseOptions);
|
1711
2016
|
__privateAdd(this, _getFetchProps);
|
1712
2017
|
__privateAdd(this, _evaluateBranch);
|
1713
2018
|
__privateAdd(this, _branch, void 0);
|
2019
|
+
__privateAdd(this, _options, void 0);
|
1714
2020
|
const safeOptions = __privateMethod(this, _parseOptions, parseOptions_fn).call(this, options);
|
2021
|
+
__privateSet(this, _options, safeOptions);
|
1715
2022
|
const pluginOptions = {
|
1716
2023
|
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
1717
2024
|
cache: safeOptions.cache
|
1718
2025
|
};
|
1719
|
-
const db = new SchemaPlugin(
|
1720
|
-
const search = new SearchPlugin(db,
|
2026
|
+
const db = new SchemaPlugin(schemaTables).build(pluginOptions);
|
2027
|
+
const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
|
1721
2028
|
this.db = db;
|
1722
2029
|
this.search = search;
|
1723
2030
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
1724
|
-
if (
|
2031
|
+
if (namespace === void 0)
|
1725
2032
|
continue;
|
1726
2033
|
const result = namespace.build(pluginOptions);
|
1727
2034
|
if (result instanceof Promise) {
|
@@ -1733,22 +2040,22 @@ const buildClient = (plugins) => {
|
|
1733
2040
|
}
|
1734
2041
|
}
|
1735
2042
|
}
|
1736
|
-
|
2043
|
+
async getConfig() {
|
2044
|
+
const databaseURL = __privateGet(this, _options).databaseURL;
|
2045
|
+
const branch = await __privateGet(this, _options).branch();
|
2046
|
+
return { databaseURL, branch };
|
2047
|
+
}
|
2048
|
+
}, _branch = new WeakMap(), _options = new WeakMap(), _parseOptions = new WeakSet(), parseOptions_fn = function(options) {
|
1737
2049
|
const fetch = getFetchImplementation(options?.fetch);
|
1738
2050
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1739
2051
|
const apiKey = options?.apiKey || getAPIKey();
|
1740
|
-
const cache = options?.cache ?? new SimpleCache();
|
1741
|
-
const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
2052
|
+
const cache = options?.cache ?? new SimpleCache({ defaultQueryTTL: 0 });
|
2053
|
+
const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
1742
2054
|
if (!databaseURL || !apiKey) {
|
1743
2055
|
throw new Error("Options databaseURL and apiKey are required");
|
1744
2056
|
}
|
1745
2057
|
return { fetch, databaseURL, apiKey, branch, cache };
|
1746
|
-
}, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({
|
1747
|
-
fetch,
|
1748
|
-
apiKey,
|
1749
|
-
databaseURL,
|
1750
|
-
branch
|
1751
|
-
}) {
|
2058
|
+
}, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({ fetch, apiKey, databaseURL, branch }) {
|
1752
2059
|
const branchValue = await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, branch);
|
1753
2060
|
if (!branchValue)
|
1754
2061
|
throw new Error("Unable to resolve branch value");
|
@@ -1765,7 +2072,7 @@ const buildClient = (plugins) => {
|
|
1765
2072
|
}, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
|
1766
2073
|
if (__privateGet(this, _branch))
|
1767
2074
|
return __privateGet(this, _branch);
|
1768
|
-
if (
|
2075
|
+
if (param === void 0)
|
1769
2076
|
return void 0;
|
1770
2077
|
const strategies = Array.isArray(param) ? [...param] : [param];
|
1771
2078
|
const evaluateBranch = async (strategy) => {
|
@@ -1783,6 +2090,88 @@ const buildClient = (plugins) => {
|
|
1783
2090
|
class BaseClient extends buildClient() {
|
1784
2091
|
}
|
1785
2092
|
|
2093
|
+
const META = "__";
|
2094
|
+
const VALUE = "___";
|
2095
|
+
class Serializer {
|
2096
|
+
constructor() {
|
2097
|
+
this.classes = {};
|
2098
|
+
}
|
2099
|
+
add(clazz) {
|
2100
|
+
this.classes[clazz.name] = clazz;
|
2101
|
+
}
|
2102
|
+
toJSON(data) {
|
2103
|
+
function visit(obj) {
|
2104
|
+
if (Array.isArray(obj))
|
2105
|
+
return obj.map(visit);
|
2106
|
+
const type = typeof obj;
|
2107
|
+
if (type === "undefined")
|
2108
|
+
return { [META]: "undefined" };
|
2109
|
+
if (type === "bigint")
|
2110
|
+
return { [META]: "bigint", [VALUE]: obj.toString() };
|
2111
|
+
if (obj === null || type !== "object")
|
2112
|
+
return obj;
|
2113
|
+
const constructor = obj.constructor;
|
2114
|
+
const o = { [META]: constructor.name };
|
2115
|
+
for (const [key, value] of Object.entries(obj)) {
|
2116
|
+
o[key] = visit(value);
|
2117
|
+
}
|
2118
|
+
if (constructor === Date)
|
2119
|
+
o[VALUE] = obj.toISOString();
|
2120
|
+
if (constructor === Map)
|
2121
|
+
o[VALUE] = Object.fromEntries(obj);
|
2122
|
+
if (constructor === Set)
|
2123
|
+
o[VALUE] = [...obj];
|
2124
|
+
return o;
|
2125
|
+
}
|
2126
|
+
return JSON.stringify(visit(data));
|
2127
|
+
}
|
2128
|
+
fromJSON(json) {
|
2129
|
+
return JSON.parse(json, (key, value) => {
|
2130
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
2131
|
+
const { [META]: clazz, [VALUE]: val, ...rest } = value;
|
2132
|
+
const constructor = this.classes[clazz];
|
2133
|
+
if (constructor) {
|
2134
|
+
return Object.assign(Object.create(constructor.prototype), rest);
|
2135
|
+
}
|
2136
|
+
if (clazz === "Date")
|
2137
|
+
return new Date(val);
|
2138
|
+
if (clazz === "Set")
|
2139
|
+
return new Set(val);
|
2140
|
+
if (clazz === "Map")
|
2141
|
+
return new Map(Object.entries(val));
|
2142
|
+
if (clazz === "bigint")
|
2143
|
+
return BigInt(val);
|
2144
|
+
if (clazz === "undefined")
|
2145
|
+
return void 0;
|
2146
|
+
return rest;
|
2147
|
+
}
|
2148
|
+
return value;
|
2149
|
+
});
|
2150
|
+
}
|
2151
|
+
}
|
2152
|
+
const defaultSerializer = new Serializer();
|
2153
|
+
const serialize = (data) => {
|
2154
|
+
return defaultSerializer.toJSON(data);
|
2155
|
+
};
|
2156
|
+
const deserialize = (json) => {
|
2157
|
+
return defaultSerializer.fromJSON(json);
|
2158
|
+
};
|
2159
|
+
|
2160
|
+
function buildWorkerRunner(config) {
|
2161
|
+
return function xataWorker(name, _worker) {
|
2162
|
+
return async (...args) => {
|
2163
|
+
const url = process.env.NODE_ENV === "development" ? `http://localhost:64749/${name}` : `https://dispatcher.xata.workers.dev/${config.workspace}/${config.worker}/${name}`;
|
2164
|
+
const result = await fetch(url, {
|
2165
|
+
method: "POST",
|
2166
|
+
headers: { "Content-Type": "application/json" },
|
2167
|
+
body: serialize({ args })
|
2168
|
+
});
|
2169
|
+
const text = await result.text();
|
2170
|
+
return deserialize(text);
|
2171
|
+
};
|
2172
|
+
};
|
2173
|
+
}
|
2174
|
+
|
1786
2175
|
class XataError extends Error {
|
1787
2176
|
constructor(message, status) {
|
1788
2177
|
super(message);
|
@@ -1798,18 +2187,22 @@ exports.PAGINATION_MAX_OFFSET = PAGINATION_MAX_OFFSET;
|
|
1798
2187
|
exports.PAGINATION_MAX_SIZE = PAGINATION_MAX_SIZE;
|
1799
2188
|
exports.Page = Page;
|
1800
2189
|
exports.Query = Query;
|
2190
|
+
exports.RecordArray = RecordArray;
|
1801
2191
|
exports.Repository = Repository;
|
1802
2192
|
exports.RestRepository = RestRepository;
|
1803
2193
|
exports.SchemaPlugin = SchemaPlugin;
|
1804
2194
|
exports.SearchPlugin = SearchPlugin;
|
2195
|
+
exports.Serializer = Serializer;
|
1805
2196
|
exports.SimpleCache = SimpleCache;
|
1806
2197
|
exports.XataApiClient = XataApiClient;
|
1807
2198
|
exports.XataApiPlugin = XataApiPlugin;
|
1808
2199
|
exports.XataError = XataError;
|
1809
2200
|
exports.XataPlugin = XataPlugin;
|
1810
2201
|
exports.acceptWorkspaceMemberInvite = acceptWorkspaceMemberInvite;
|
2202
|
+
exports.addGitBranchesEntry = addGitBranchesEntry;
|
1811
2203
|
exports.addTableColumn = addTableColumn;
|
1812
2204
|
exports.buildClient = buildClient;
|
2205
|
+
exports.buildWorkerRunner = buildWorkerRunner;
|
1813
2206
|
exports.bulkInsertTableRecords = bulkInsertTableRecords;
|
1814
2207
|
exports.cancelWorkspaceMemberInvite = cancelWorkspaceMemberInvite;
|
1815
2208
|
exports.contains = contains;
|
@@ -1826,6 +2219,7 @@ exports.deleteTable = deleteTable;
|
|
1826
2219
|
exports.deleteUser = deleteUser;
|
1827
2220
|
exports.deleteUserAPIKey = deleteUserAPIKey;
|
1828
2221
|
exports.deleteWorkspace = deleteWorkspace;
|
2222
|
+
exports.deserialize = deserialize;
|
1829
2223
|
exports.endsWith = endsWith;
|
1830
2224
|
exports.executeBranchMigrationPlan = executeBranchMigrationPlan;
|
1831
2225
|
exports.exists = exists;
|
@@ -1841,7 +2235,9 @@ exports.getColumn = getColumn;
|
|
1841
2235
|
exports.getCurrentBranchDetails = getCurrentBranchDetails;
|
1842
2236
|
exports.getCurrentBranchName = getCurrentBranchName;
|
1843
2237
|
exports.getDatabaseList = getDatabaseList;
|
2238
|
+
exports.getDatabaseMetadata = getDatabaseMetadata;
|
1844
2239
|
exports.getDatabaseURL = getDatabaseURL;
|
2240
|
+
exports.getGitBranchesMapping = getGitBranchesMapping;
|
1845
2241
|
exports.getRecord = getRecord;
|
1846
2242
|
exports.getTableColumns = getTableColumns;
|
1847
2243
|
exports.getTableSchema = getTableSchema;
|
@@ -1860,6 +2256,7 @@ exports.insertRecord = insertRecord;
|
|
1860
2256
|
exports.insertRecordWithID = insertRecordWithID;
|
1861
2257
|
exports.inviteWorkspaceMember = inviteWorkspaceMember;
|
1862
2258
|
exports.is = is;
|
2259
|
+
exports.isCursorPaginationOptions = isCursorPaginationOptions;
|
1863
2260
|
exports.isIdentifiable = isIdentifiable;
|
1864
2261
|
exports.isNot = isNot;
|
1865
2262
|
exports.isXataRecord = isXataRecord;
|
@@ -1870,9 +2267,13 @@ exports.notExists = notExists;
|
|
1870
2267
|
exports.operationsByTag = operationsByTag;
|
1871
2268
|
exports.pattern = pattern;
|
1872
2269
|
exports.queryTable = queryTable;
|
2270
|
+
exports.removeGitBranchesEntry = removeGitBranchesEntry;
|
1873
2271
|
exports.removeWorkspaceMember = removeWorkspaceMember;
|
1874
2272
|
exports.resendWorkspaceMemberInvite = resendWorkspaceMemberInvite;
|
2273
|
+
exports.resolveBranch = resolveBranch;
|
1875
2274
|
exports.searchBranch = searchBranch;
|
2275
|
+
exports.searchTable = searchTable;
|
2276
|
+
exports.serialize = serialize;
|
1876
2277
|
exports.setTableSchema = setTableSchema;
|
1877
2278
|
exports.startsWith = startsWith;
|
1878
2279
|
exports.updateBranchMetadata = updateBranchMetadata;
|
@@ -1881,6 +2282,7 @@ exports.updateRecordWithID = updateRecordWithID;
|
|
1881
2282
|
exports.updateTable = updateTable;
|
1882
2283
|
exports.updateUser = updateUser;
|
1883
2284
|
exports.updateWorkspace = updateWorkspace;
|
2285
|
+
exports.updateWorkspaceMemberInvite = updateWorkspaceMemberInvite;
|
1884
2286
|
exports.updateWorkspaceMemberRole = updateWorkspaceMemberRole;
|
1885
2287
|
exports.upsertRecordWithID = upsertRecordWithID;
|
1886
2288
|
//# sourceMappingURL=index.cjs.map
|