@xata.io/client 0.0.0-alpha.vfb4a018 → 0.0.0-alpha.vfc692f9
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 +146 -0
- package/README.md +271 -1
- package/Usage.md +428 -0
- package/dist/index.cjs +569 -357
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +814 -228
- package/dist/index.mjs +548 -358
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -4
- package/tsconfig.json +1 -0
package/dist/index.mjs
CHANGED
@@ -7,46 +7,101 @@ function compact(arr) {
|
|
7
7
|
function isObject(value) {
|
8
8
|
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
9
9
|
}
|
10
|
+
function isDefined(value) {
|
11
|
+
return value !== null && value !== void 0;
|
12
|
+
}
|
10
13
|
function isString(value) {
|
11
|
-
return value
|
14
|
+
return isDefined(value) && typeof value === "string";
|
15
|
+
}
|
16
|
+
function isStringArray(value) {
|
17
|
+
return isDefined(value) && Array.isArray(value) && value.every(isString);
|
12
18
|
}
|
13
19
|
function toBase64(value) {
|
14
20
|
try {
|
15
21
|
return btoa(value);
|
16
22
|
} catch (err) {
|
17
|
-
|
23
|
+
const buf = Buffer;
|
24
|
+
return buf.from(value).toString("base64");
|
18
25
|
}
|
19
26
|
}
|
20
27
|
|
21
|
-
function
|
28
|
+
function getEnvironment() {
|
22
29
|
try {
|
23
|
-
if (isObject(process) &&
|
24
|
-
return
|
30
|
+
if (isObject(process) && isObject(process.env)) {
|
31
|
+
return {
|
32
|
+
apiKey: process.env.XATA_API_KEY ?? getGlobalApiKey(),
|
33
|
+
databaseURL: process.env.XATA_DATABASE_URL ?? getGlobalDatabaseURL(),
|
34
|
+
branch: process.env.XATA_BRANCH ?? getGlobalBranch(),
|
35
|
+
envBranch: process.env.VERCEL_GIT_COMMIT_REF ?? process.env.CF_PAGES_BRANCH ?? process.env.BRANCH,
|
36
|
+
fallbackBranch: process.env.XATA_FALLBACK_BRANCH ?? getGlobalFallbackBranch()
|
37
|
+
};
|
25
38
|
}
|
26
39
|
} catch (err) {
|
27
40
|
}
|
28
41
|
try {
|
29
|
-
if (isObject(Deno) &&
|
30
|
-
return
|
42
|
+
if (isObject(Deno) && isObject(Deno.env)) {
|
43
|
+
return {
|
44
|
+
apiKey: Deno.env.get("XATA_API_KEY") ?? getGlobalApiKey(),
|
45
|
+
databaseURL: Deno.env.get("XATA_DATABASE_URL") ?? getGlobalDatabaseURL(),
|
46
|
+
branch: Deno.env.get("XATA_BRANCH") ?? getGlobalBranch(),
|
47
|
+
envBranch: Deno.env.get("VERCEL_GIT_COMMIT_REF") ?? Deno.env.get("CF_PAGES_BRANCH") ?? Deno.env.get("BRANCH"),
|
48
|
+
fallbackBranch: Deno.env.get("XATA_FALLBACK_BRANCH") ?? getGlobalFallbackBranch()
|
49
|
+
};
|
31
50
|
}
|
32
51
|
} catch (err) {
|
33
52
|
}
|
53
|
+
return {
|
54
|
+
apiKey: getGlobalApiKey(),
|
55
|
+
databaseURL: getGlobalDatabaseURL(),
|
56
|
+
branch: getGlobalBranch(),
|
57
|
+
envBranch: void 0,
|
58
|
+
fallbackBranch: getGlobalFallbackBranch()
|
59
|
+
};
|
60
|
+
}
|
61
|
+
function getGlobalApiKey() {
|
62
|
+
try {
|
63
|
+
return XATA_API_KEY;
|
64
|
+
} catch (err) {
|
65
|
+
return void 0;
|
66
|
+
}
|
67
|
+
}
|
68
|
+
function getGlobalDatabaseURL() {
|
69
|
+
try {
|
70
|
+
return XATA_DATABASE_URL;
|
71
|
+
} catch (err) {
|
72
|
+
return void 0;
|
73
|
+
}
|
74
|
+
}
|
75
|
+
function getGlobalBranch() {
|
76
|
+
try {
|
77
|
+
return XATA_BRANCH;
|
78
|
+
} catch (err) {
|
79
|
+
return void 0;
|
80
|
+
}
|
81
|
+
}
|
82
|
+
function getGlobalFallbackBranch() {
|
83
|
+
try {
|
84
|
+
return XATA_FALLBACK_BRANCH;
|
85
|
+
} catch (err) {
|
86
|
+
return void 0;
|
87
|
+
}
|
34
88
|
}
|
35
89
|
async function getGitBranch() {
|
90
|
+
const cmd = ["git", "branch", "--show-current"];
|
91
|
+
const fullCmd = cmd.join(" ");
|
92
|
+
const nodeModule = ["child", "process"].join("_");
|
93
|
+
const execOptions = { encoding: "utf-8", stdio: ["ignore", "pipe", "ignore"] };
|
36
94
|
try {
|
37
95
|
if (typeof require === "function") {
|
38
|
-
|
39
|
-
return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
|
96
|
+
return require(nodeModule).execSync(fullCmd, execOptions).trim();
|
40
97
|
}
|
98
|
+
const { execSync } = await import(nodeModule);
|
99
|
+
return execSync(fullCmd, execOptions).toString().trim();
|
41
100
|
} catch (err) {
|
42
101
|
}
|
43
102
|
try {
|
44
103
|
if (isObject(Deno)) {
|
45
|
-
const process2 = Deno.run({
|
46
|
-
cmd: ["git", "branch", "--show-current"],
|
47
|
-
stdout: "piped",
|
48
|
-
stderr: "piped"
|
49
|
-
});
|
104
|
+
const process2 = Deno.run({ cmd, stdout: "piped", stderr: "null" });
|
50
105
|
return new TextDecoder().decode(await process2.output()).trim();
|
51
106
|
}
|
52
107
|
} catch (err) {
|
@@ -55,7 +110,8 @@ async function getGitBranch() {
|
|
55
110
|
|
56
111
|
function getAPIKey() {
|
57
112
|
try {
|
58
|
-
|
113
|
+
const { apiKey } = getEnvironment();
|
114
|
+
return apiKey;
|
59
115
|
} catch (err) {
|
60
116
|
return void 0;
|
61
117
|
}
|
@@ -70,16 +126,28 @@ function getFetchImplementation(userFetch) {
|
|
70
126
|
return fetchImpl;
|
71
127
|
}
|
72
128
|
|
73
|
-
|
74
|
-
|
129
|
+
const VERSION = "0.0.0-alpha.vfc692f9";
|
130
|
+
|
131
|
+
class ErrorWithCause extends Error {
|
132
|
+
constructor(message, options) {
|
133
|
+
super(message, options);
|
134
|
+
}
|
135
|
+
}
|
136
|
+
class FetcherError extends ErrorWithCause {
|
137
|
+
constructor(status, data, requestId) {
|
75
138
|
super(getMessage(data));
|
76
139
|
this.status = status;
|
77
140
|
this.errors = isBulkError(data) ? data.errors : void 0;
|
141
|
+
this.requestId = requestId;
|
78
142
|
if (data instanceof Error) {
|
79
143
|
this.stack = data.stack;
|
80
144
|
this.cause = data.cause;
|
81
145
|
}
|
82
146
|
}
|
147
|
+
toString() {
|
148
|
+
const error = super.toString();
|
149
|
+
return `[${this.status}] (${this.requestId ?? "Unknown"}): ${error}`;
|
150
|
+
}
|
83
151
|
}
|
84
152
|
function isBulkError(error) {
|
85
153
|
return isObject(error) && Array.isArray(error.errors);
|
@@ -102,7 +170,12 @@ function getMessage(data) {
|
|
102
170
|
}
|
103
171
|
|
104
172
|
const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
|
105
|
-
const
|
173
|
+
const cleanQueryParams = Object.entries(queryParams).reduce((acc, [key, value]) => {
|
174
|
+
if (value === void 0 || value === null)
|
175
|
+
return acc;
|
176
|
+
return { ...acc, [key]: value };
|
177
|
+
}, {});
|
178
|
+
const query = new URLSearchParams(cleanQueryParams).toString();
|
106
179
|
const queryString = query.length > 0 ? `?${query}` : "";
|
107
180
|
return url.replace(/\{\w*\}/g, (key) => pathParams[key.slice(1, -1)]) + queryString;
|
108
181
|
};
|
@@ -142,6 +215,7 @@ async function fetch$1({
|
|
142
215
|
body: body ? JSON.stringify(body) : void 0,
|
143
216
|
headers: {
|
144
217
|
"Content-Type": "application/json",
|
218
|
+
"User-Agent": `Xata client-ts/${VERSION}`,
|
145
219
|
...headers,
|
146
220
|
...hostHeader(fullUrl),
|
147
221
|
Authorization: `Bearer ${apiKey}`
|
@@ -150,14 +224,15 @@ async function fetch$1({
|
|
150
224
|
if (response.status === 204) {
|
151
225
|
return {};
|
152
226
|
}
|
227
|
+
const requestId = response.headers?.get("x-request-id") ?? void 0;
|
153
228
|
try {
|
154
229
|
const jsonResponse = await response.json();
|
155
230
|
if (response.ok) {
|
156
231
|
return jsonResponse;
|
157
232
|
}
|
158
|
-
throw new FetcherError(response.status, jsonResponse);
|
233
|
+
throw new FetcherError(response.status, jsonResponse, requestId);
|
159
234
|
} catch (error) {
|
160
|
-
throw new FetcherError(response.status, error);
|
235
|
+
throw new FetcherError(response.status, error, requestId);
|
161
236
|
}
|
162
237
|
}
|
163
238
|
|
@@ -216,6 +291,7 @@ const removeWorkspaceMember = (variables) => fetch$1({
|
|
216
291
|
...variables
|
217
292
|
});
|
218
293
|
const inviteWorkspaceMember = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/invites", method: "post", ...variables });
|
294
|
+
const updateWorkspaceMemberInvite = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/invites/{inviteId}", method: "patch", ...variables });
|
219
295
|
const cancelWorkspaceMemberInvite = (variables) => fetch$1({
|
220
296
|
url: "/workspaces/{workspaceId}/invites/{inviteId}",
|
221
297
|
method: "delete",
|
@@ -264,11 +340,7 @@ const getBranchDetails = (variables) => fetch$1({
|
|
264
340
|
method: "get",
|
265
341
|
...variables
|
266
342
|
});
|
267
|
-
const createBranch = (variables) => fetch$1({
|
268
|
-
url: "/db/{dbBranchName}",
|
269
|
-
method: "put",
|
270
|
-
...variables
|
271
|
-
});
|
343
|
+
const createBranch = (variables) => fetch$1({ url: "/db/{dbBranchName}", method: "put", ...variables });
|
272
344
|
const deleteBranch = (variables) => fetch$1({
|
273
345
|
url: "/db/{dbBranchName}",
|
274
346
|
method: "delete",
|
@@ -342,11 +414,7 @@ const updateColumn = (variables) => fetch$1({
|
|
342
414
|
method: "patch",
|
343
415
|
...variables
|
344
416
|
});
|
345
|
-
const insertRecord = (variables) => fetch$1({
|
346
|
-
url: "/db/{dbBranchName}/tables/{tableName}/data",
|
347
|
-
method: "post",
|
348
|
-
...variables
|
349
|
-
});
|
417
|
+
const insertRecord = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data", method: "post", ...variables });
|
350
418
|
const insertRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "put", ...variables });
|
351
419
|
const updateRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "patch", ...variables });
|
352
420
|
const upsertRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "post", ...variables });
|
@@ -366,6 +434,11 @@ const queryTable = (variables) => fetch$1({
|
|
366
434
|
method: "post",
|
367
435
|
...variables
|
368
436
|
});
|
437
|
+
const searchTable = (variables) => fetch$1({
|
438
|
+
url: "/db/{dbBranchName}/tables/{tableName}/search",
|
439
|
+
method: "post",
|
440
|
+
...variables
|
441
|
+
});
|
369
442
|
const searchBranch = (variables) => fetch$1({
|
370
443
|
url: "/db/{dbBranchName}/search",
|
371
444
|
method: "post",
|
@@ -383,6 +456,7 @@ const operationsByTag = {
|
|
383
456
|
updateWorkspaceMemberRole,
|
384
457
|
removeWorkspaceMember,
|
385
458
|
inviteWorkspaceMember,
|
459
|
+
updateWorkspaceMemberInvite,
|
386
460
|
cancelWorkspaceMemberInvite,
|
387
461
|
resendWorkspaceMemberInvite,
|
388
462
|
acceptWorkspaceMemberInvite
|
@@ -429,6 +503,7 @@ const operationsByTag = {
|
|
429
503
|
getRecord,
|
430
504
|
bulkInsertTableRecords,
|
431
505
|
queryTable,
|
506
|
+
searchTable,
|
432
507
|
searchBranch
|
433
508
|
}
|
434
509
|
};
|
@@ -462,7 +537,7 @@ var __accessCheck$7 = (obj, member, msg) => {
|
|
462
537
|
if (!member.has(obj))
|
463
538
|
throw TypeError("Cannot " + msg);
|
464
539
|
};
|
465
|
-
var __privateGet$
|
540
|
+
var __privateGet$7 = (obj, member, getter) => {
|
466
541
|
__accessCheck$7(obj, member, "read from private field");
|
467
542
|
return getter ? getter.call(obj) : member.get(obj);
|
468
543
|
};
|
@@ -471,7 +546,7 @@ var __privateAdd$7 = (obj, member, value) => {
|
|
471
546
|
throw TypeError("Cannot add the same private member more than once");
|
472
547
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
473
548
|
};
|
474
|
-
var __privateSet$
|
549
|
+
var __privateSet$7 = (obj, member, value, setter) => {
|
475
550
|
__accessCheck$7(obj, member, "write to private field");
|
476
551
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
477
552
|
return value;
|
@@ -486,7 +561,7 @@ class XataApiClient {
|
|
486
561
|
if (!apiKey) {
|
487
562
|
throw new Error("Could not resolve a valid apiKey");
|
488
563
|
}
|
489
|
-
__privateSet$
|
564
|
+
__privateSet$7(this, _extraProps, {
|
490
565
|
apiUrl: getHostUrl(provider, "main"),
|
491
566
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
492
567
|
fetchImpl: getFetchImplementation(options.fetch),
|
@@ -494,34 +569,34 @@ class XataApiClient {
|
|
494
569
|
});
|
495
570
|
}
|
496
571
|
get user() {
|
497
|
-
if (!__privateGet$
|
498
|
-
__privateGet$
|
499
|
-
return __privateGet$
|
572
|
+
if (!__privateGet$7(this, _namespaces).user)
|
573
|
+
__privateGet$7(this, _namespaces).user = new UserApi(__privateGet$7(this, _extraProps));
|
574
|
+
return __privateGet$7(this, _namespaces).user;
|
500
575
|
}
|
501
576
|
get workspaces() {
|
502
|
-
if (!__privateGet$
|
503
|
-
__privateGet$
|
504
|
-
return __privateGet$
|
577
|
+
if (!__privateGet$7(this, _namespaces).workspaces)
|
578
|
+
__privateGet$7(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$7(this, _extraProps));
|
579
|
+
return __privateGet$7(this, _namespaces).workspaces;
|
505
580
|
}
|
506
581
|
get databases() {
|
507
|
-
if (!__privateGet$
|
508
|
-
__privateGet$
|
509
|
-
return __privateGet$
|
582
|
+
if (!__privateGet$7(this, _namespaces).databases)
|
583
|
+
__privateGet$7(this, _namespaces).databases = new DatabaseApi(__privateGet$7(this, _extraProps));
|
584
|
+
return __privateGet$7(this, _namespaces).databases;
|
510
585
|
}
|
511
586
|
get branches() {
|
512
|
-
if (!__privateGet$
|
513
|
-
__privateGet$
|
514
|
-
return __privateGet$
|
587
|
+
if (!__privateGet$7(this, _namespaces).branches)
|
588
|
+
__privateGet$7(this, _namespaces).branches = new BranchApi(__privateGet$7(this, _extraProps));
|
589
|
+
return __privateGet$7(this, _namespaces).branches;
|
515
590
|
}
|
516
591
|
get tables() {
|
517
|
-
if (!__privateGet$
|
518
|
-
__privateGet$
|
519
|
-
return __privateGet$
|
592
|
+
if (!__privateGet$7(this, _namespaces).tables)
|
593
|
+
__privateGet$7(this, _namespaces).tables = new TableApi(__privateGet$7(this, _extraProps));
|
594
|
+
return __privateGet$7(this, _namespaces).tables;
|
520
595
|
}
|
521
596
|
get records() {
|
522
|
-
if (!__privateGet$
|
523
|
-
__privateGet$
|
524
|
-
return __privateGet$
|
597
|
+
if (!__privateGet$7(this, _namespaces).records)
|
598
|
+
__privateGet$7(this, _namespaces).records = new RecordsApi(__privateGet$7(this, _extraProps));
|
599
|
+
return __privateGet$7(this, _namespaces).records;
|
525
600
|
}
|
526
601
|
}
|
527
602
|
_extraProps = new WeakMap();
|
@@ -613,6 +688,13 @@ class WorkspaceApi {
|
|
613
688
|
...this.extraProps
|
614
689
|
});
|
615
690
|
}
|
691
|
+
updateWorkspaceMemberInvite(workspaceId, inviteId, role) {
|
692
|
+
return operationsByTag.workspaces.updateWorkspaceMemberInvite({
|
693
|
+
pathParams: { workspaceId, inviteId },
|
694
|
+
body: { role },
|
695
|
+
...this.extraProps
|
696
|
+
});
|
697
|
+
}
|
616
698
|
cancelWorkspaceMemberInvite(workspaceId, inviteId) {
|
617
699
|
return operationsByTag.workspaces.cancelWorkspaceMemberInvite({
|
618
700
|
pathParams: { workspaceId, inviteId },
|
@@ -675,10 +757,10 @@ class DatabaseApi {
|
|
675
757
|
...this.extraProps
|
676
758
|
});
|
677
759
|
}
|
678
|
-
resolveBranch(workspace, dbName, gitBranch) {
|
760
|
+
resolveBranch(workspace, dbName, gitBranch, fallbackBranch) {
|
679
761
|
return operationsByTag.database.resolveBranch({
|
680
762
|
pathParams: { workspace, dbName },
|
681
|
-
queryParams: { gitBranch },
|
763
|
+
queryParams: { gitBranch, fallbackBranch },
|
682
764
|
...this.extraProps
|
683
765
|
});
|
684
766
|
}
|
@@ -699,10 +781,10 @@ class BranchApi {
|
|
699
781
|
...this.extraProps
|
700
782
|
});
|
701
783
|
}
|
702
|
-
createBranch(workspace, database, branch, from
|
784
|
+
createBranch(workspace, database, branch, from, options = {}) {
|
703
785
|
return operationsByTag.branch.createBranch({
|
704
786
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
705
|
-
queryParams: { from },
|
787
|
+
queryParams: isString(from) ? { from } : void 0,
|
706
788
|
body: options,
|
707
789
|
...this.extraProps
|
708
790
|
});
|
@@ -827,9 +909,10 @@ class RecordsApi {
|
|
827
909
|
constructor(extraProps) {
|
828
910
|
this.extraProps = extraProps;
|
829
911
|
}
|
830
|
-
insertRecord(workspace, database, branch, tableName, record) {
|
912
|
+
insertRecord(workspace, database, branch, tableName, record, options = {}) {
|
831
913
|
return operationsByTag.records.insertRecord({
|
832
914
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
915
|
+
queryParams: options,
|
833
916
|
body: record,
|
834
917
|
...this.extraProps
|
835
918
|
});
|
@@ -858,21 +941,24 @@ class RecordsApi {
|
|
858
941
|
...this.extraProps
|
859
942
|
});
|
860
943
|
}
|
861
|
-
deleteRecord(workspace, database, branch, tableName, recordId) {
|
944
|
+
deleteRecord(workspace, database, branch, tableName, recordId, options = {}) {
|
862
945
|
return operationsByTag.records.deleteRecord({
|
863
946
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
|
947
|
+
queryParams: options,
|
864
948
|
...this.extraProps
|
865
949
|
});
|
866
950
|
}
|
867
951
|
getRecord(workspace, database, branch, tableName, recordId, options = {}) {
|
868
952
|
return operationsByTag.records.getRecord({
|
869
953
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
|
954
|
+
queryParams: options,
|
870
955
|
...this.extraProps
|
871
956
|
});
|
872
957
|
}
|
873
|
-
bulkInsertTableRecords(workspace, database, branch, tableName, records) {
|
958
|
+
bulkInsertTableRecords(workspace, database, branch, tableName, records, options = {}) {
|
874
959
|
return operationsByTag.records.bulkInsertTableRecords({
|
875
960
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
961
|
+
queryParams: options,
|
876
962
|
body: { records },
|
877
963
|
...this.extraProps
|
878
964
|
});
|
@@ -884,6 +970,13 @@ class RecordsApi {
|
|
884
970
|
...this.extraProps
|
885
971
|
});
|
886
972
|
}
|
973
|
+
searchTable(workspace, database, branch, tableName, query) {
|
974
|
+
return operationsByTag.records.searchTable({
|
975
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
976
|
+
body: query,
|
977
|
+
...this.extraProps
|
978
|
+
});
|
979
|
+
}
|
887
980
|
searchBranch(workspace, database, branch, query) {
|
888
981
|
return operationsByTag.records.searchBranch({
|
889
982
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
@@ -907,7 +1000,7 @@ var __accessCheck$6 = (obj, member, msg) => {
|
|
907
1000
|
if (!member.has(obj))
|
908
1001
|
throw TypeError("Cannot " + msg);
|
909
1002
|
};
|
910
|
-
var __privateGet$
|
1003
|
+
var __privateGet$6 = (obj, member, getter) => {
|
911
1004
|
__accessCheck$6(obj, member, "read from private field");
|
912
1005
|
return getter ? getter.call(obj) : member.get(obj);
|
913
1006
|
};
|
@@ -916,30 +1009,30 @@ var __privateAdd$6 = (obj, member, value) => {
|
|
916
1009
|
throw TypeError("Cannot add the same private member more than once");
|
917
1010
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
918
1011
|
};
|
919
|
-
var __privateSet$
|
1012
|
+
var __privateSet$6 = (obj, member, value, setter) => {
|
920
1013
|
__accessCheck$6(obj, member, "write to private field");
|
921
1014
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
922
1015
|
return value;
|
923
1016
|
};
|
924
|
-
var _query;
|
1017
|
+
var _query, _page;
|
925
1018
|
class Page {
|
926
1019
|
constructor(query, meta, records = []) {
|
927
1020
|
__privateAdd$6(this, _query, void 0);
|
928
|
-
__privateSet$
|
1021
|
+
__privateSet$6(this, _query, query);
|
929
1022
|
this.meta = meta;
|
930
|
-
this.records = records;
|
1023
|
+
this.records = new RecordArray(this, records);
|
931
1024
|
}
|
932
1025
|
async nextPage(size, offset) {
|
933
|
-
return __privateGet$
|
1026
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
|
934
1027
|
}
|
935
1028
|
async previousPage(size, offset) {
|
936
|
-
return __privateGet$
|
1029
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, before: this.meta.page.cursor } });
|
937
1030
|
}
|
938
1031
|
async firstPage(size, offset) {
|
939
|
-
return __privateGet$
|
1032
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, first: this.meta.page.cursor } });
|
940
1033
|
}
|
941
1034
|
async lastPage(size, offset) {
|
942
|
-
return __privateGet$
|
1035
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, last: this.meta.page.cursor } });
|
943
1036
|
}
|
944
1037
|
hasNextPage() {
|
945
1038
|
return this.meta.page.more;
|
@@ -947,15 +1040,62 @@ class Page {
|
|
947
1040
|
}
|
948
1041
|
_query = new WeakMap();
|
949
1042
|
const PAGINATION_MAX_SIZE = 200;
|
950
|
-
const PAGINATION_DEFAULT_SIZE =
|
1043
|
+
const PAGINATION_DEFAULT_SIZE = 20;
|
951
1044
|
const PAGINATION_MAX_OFFSET = 800;
|
952
1045
|
const PAGINATION_DEFAULT_OFFSET = 0;
|
1046
|
+
function isCursorPaginationOptions(options) {
|
1047
|
+
return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
|
1048
|
+
}
|
1049
|
+
const _RecordArray = class extends Array {
|
1050
|
+
constructor(...args) {
|
1051
|
+
super(..._RecordArray.parseConstructorParams(...args));
|
1052
|
+
__privateAdd$6(this, _page, void 0);
|
1053
|
+
__privateSet$6(this, _page, isObject(args[0]?.meta) ? args[0] : { meta: { page: { cursor: "", more: false } }, records: [] });
|
1054
|
+
}
|
1055
|
+
static parseConstructorParams(...args) {
|
1056
|
+
if (args.length === 1 && typeof args[0] === "number") {
|
1057
|
+
return new Array(args[0]);
|
1058
|
+
}
|
1059
|
+
if (args.length <= 2 && isObject(args[0]?.meta) && Array.isArray(args[1] ?? [])) {
|
1060
|
+
const result = args[1] ?? args[0].records ?? [];
|
1061
|
+
return new Array(...result);
|
1062
|
+
}
|
1063
|
+
return new Array(...args);
|
1064
|
+
}
|
1065
|
+
toArray() {
|
1066
|
+
return new Array(...this);
|
1067
|
+
}
|
1068
|
+
map(callbackfn, thisArg) {
|
1069
|
+
return this.toArray().map(callbackfn, thisArg);
|
1070
|
+
}
|
1071
|
+
async nextPage(size, offset) {
|
1072
|
+
const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
|
1073
|
+
return new _RecordArray(newPage);
|
1074
|
+
}
|
1075
|
+
async previousPage(size, offset) {
|
1076
|
+
const newPage = await __privateGet$6(this, _page).previousPage(size, offset);
|
1077
|
+
return new _RecordArray(newPage);
|
1078
|
+
}
|
1079
|
+
async firstPage(size, offset) {
|
1080
|
+
const newPage = await __privateGet$6(this, _page).firstPage(size, offset);
|
1081
|
+
return new _RecordArray(newPage);
|
1082
|
+
}
|
1083
|
+
async lastPage(size, offset) {
|
1084
|
+
const newPage = await __privateGet$6(this, _page).lastPage(size, offset);
|
1085
|
+
return new _RecordArray(newPage);
|
1086
|
+
}
|
1087
|
+
hasNextPage() {
|
1088
|
+
return __privateGet$6(this, _page).meta.page.more;
|
1089
|
+
}
|
1090
|
+
};
|
1091
|
+
let RecordArray = _RecordArray;
|
1092
|
+
_page = new WeakMap();
|
953
1093
|
|
954
1094
|
var __accessCheck$5 = (obj, member, msg) => {
|
955
1095
|
if (!member.has(obj))
|
956
1096
|
throw TypeError("Cannot " + msg);
|
957
1097
|
};
|
958
|
-
var __privateGet$
|
1098
|
+
var __privateGet$5 = (obj, member, getter) => {
|
959
1099
|
__accessCheck$5(obj, member, "read from private field");
|
960
1100
|
return getter ? getter.call(obj) : member.get(obj);
|
961
1101
|
};
|
@@ -964,34 +1104,35 @@ var __privateAdd$5 = (obj, member, value) => {
|
|
964
1104
|
throw TypeError("Cannot add the same private member more than once");
|
965
1105
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
966
1106
|
};
|
967
|
-
var __privateSet$
|
1107
|
+
var __privateSet$5 = (obj, member, value, setter) => {
|
968
1108
|
__accessCheck$5(obj, member, "write to private field");
|
969
1109
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
970
1110
|
return value;
|
971
1111
|
};
|
972
1112
|
var _table$1, _repository, _data;
|
973
1113
|
const _Query = class {
|
974
|
-
constructor(repository, table, data,
|
1114
|
+
constructor(repository, table, data, rawParent) {
|
975
1115
|
__privateAdd$5(this, _table$1, void 0);
|
976
1116
|
__privateAdd$5(this, _repository, void 0);
|
977
1117
|
__privateAdd$5(this, _data, { filter: {} });
|
978
1118
|
this.meta = { page: { cursor: "start", more: true } };
|
979
|
-
this.records = [];
|
980
|
-
__privateSet$
|
1119
|
+
this.records = new RecordArray(this, []);
|
1120
|
+
__privateSet$5(this, _table$1, table);
|
981
1121
|
if (repository) {
|
982
|
-
__privateSet$
|
1122
|
+
__privateSet$5(this, _repository, repository);
|
983
1123
|
} else {
|
984
|
-
__privateSet$
|
1124
|
+
__privateSet$5(this, _repository, this);
|
985
1125
|
}
|
986
|
-
|
987
|
-
__privateGet$
|
988
|
-
__privateGet$
|
989
|
-
__privateGet$
|
990
|
-
__privateGet$
|
991
|
-
__privateGet$
|
992
|
-
__privateGet$
|
993
|
-
__privateGet$
|
994
|
-
__privateGet$
|
1126
|
+
const parent = cleanParent(data, rawParent);
|
1127
|
+
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
1128
|
+
__privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
|
1129
|
+
__privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
|
1130
|
+
__privateGet$5(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
|
1131
|
+
__privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
|
1132
|
+
__privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
|
1133
|
+
__privateGet$5(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
|
1134
|
+
__privateGet$5(this, _data).pagination = data.pagination ?? parent?.pagination;
|
1135
|
+
__privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
|
995
1136
|
this.any = this.any.bind(this);
|
996
1137
|
this.all = this.all.bind(this);
|
997
1138
|
this.not = this.not.bind(this);
|
@@ -1002,50 +1143,50 @@ const _Query = class {
|
|
1002
1143
|
Object.defineProperty(this, "repository", { enumerable: false });
|
1003
1144
|
}
|
1004
1145
|
getQueryOptions() {
|
1005
|
-
return __privateGet$
|
1146
|
+
return __privateGet$5(this, _data);
|
1006
1147
|
}
|
1007
1148
|
key() {
|
1008
|
-
const { columns = [], filter = {}, sort = [],
|
1009
|
-
const key = JSON.stringify({ columns, filter, sort,
|
1149
|
+
const { columns = [], filter = {}, sort = [], pagination = {} } = __privateGet$5(this, _data);
|
1150
|
+
const key = JSON.stringify({ columns, filter, sort, pagination });
|
1010
1151
|
return toBase64(key);
|
1011
1152
|
}
|
1012
1153
|
any(...queries) {
|
1013
1154
|
const $any = queries.map((query) => query.getQueryOptions().filter ?? {});
|
1014
|
-
return new _Query(__privateGet$
|
1155
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $any } }, __privateGet$5(this, _data));
|
1015
1156
|
}
|
1016
1157
|
all(...queries) {
|
1017
1158
|
const $all = queries.map((query) => query.getQueryOptions().filter ?? {});
|
1018
|
-
return new _Query(__privateGet$
|
1159
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
1019
1160
|
}
|
1020
1161
|
not(...queries) {
|
1021
1162
|
const $not = queries.map((query) => query.getQueryOptions().filter ?? {});
|
1022
|
-
return new _Query(__privateGet$
|
1163
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $not } }, __privateGet$5(this, _data));
|
1023
1164
|
}
|
1024
1165
|
none(...queries) {
|
1025
1166
|
const $none = queries.map((query) => query.getQueryOptions().filter ?? {});
|
1026
|
-
return new _Query(__privateGet$
|
1167
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $none } }, __privateGet$5(this, _data));
|
1027
1168
|
}
|
1028
1169
|
filter(a, b) {
|
1029
1170
|
if (arguments.length === 1) {
|
1030
1171
|
const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
|
1031
|
-
const $all = compact([__privateGet$
|
1032
|
-
return new _Query(__privateGet$
|
1172
|
+
const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat(constraints));
|
1173
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
1033
1174
|
} else {
|
1034
|
-
const $all = compact([__privateGet$
|
1035
|
-
return new _Query(__privateGet$
|
1175
|
+
const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
|
1176
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
1036
1177
|
}
|
1037
1178
|
}
|
1038
1179
|
sort(column, direction) {
|
1039
|
-
const originalSort = [__privateGet$
|
1180
|
+
const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
|
1040
1181
|
const sort = [...originalSort, { column, direction }];
|
1041
|
-
return new _Query(__privateGet$
|
1182
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
|
1042
1183
|
}
|
1043
1184
|
select(columns) {
|
1044
|
-
return new _Query(__privateGet$
|
1185
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { columns }, __privateGet$5(this, _data));
|
1045
1186
|
}
|
1046
1187
|
getPaginated(options = {}) {
|
1047
|
-
const query = new _Query(__privateGet$
|
1048
|
-
return __privateGet$
|
1188
|
+
const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
|
1189
|
+
return __privateGet$5(this, _repository).query(query);
|
1049
1190
|
}
|
1050
1191
|
async *[Symbol.asyncIterator]() {
|
1051
1192
|
for await (const [record] of this.getIterator({ batchSize: 1 })) {
|
@@ -1054,18 +1195,21 @@ const _Query = class {
|
|
1054
1195
|
}
|
1055
1196
|
async *getIterator(options = {}) {
|
1056
1197
|
const { batchSize = 1 } = options;
|
1057
|
-
let
|
1058
|
-
let
|
1059
|
-
|
1060
|
-
|
1061
|
-
|
1062
|
-
|
1063
|
-
|
1198
|
+
let page = await this.getPaginated({ ...options, pagination: { size: batchSize, offset: 0 } });
|
1199
|
+
let more = page.hasNextPage();
|
1200
|
+
yield page.records;
|
1201
|
+
while (more) {
|
1202
|
+
page = await page.nextPage();
|
1203
|
+
more = page.hasNextPage();
|
1204
|
+
yield page.records;
|
1064
1205
|
}
|
1065
1206
|
}
|
1066
1207
|
async getMany(options = {}) {
|
1067
|
-
const
|
1068
|
-
|
1208
|
+
const page = await this.getPaginated(options);
|
1209
|
+
if (page.hasNextPage() && options.pagination?.size === void 0) {
|
1210
|
+
console.trace("Calling getMany does not return all results. Paginate to get all results or call getAll.");
|
1211
|
+
}
|
1212
|
+
return page.records;
|
1069
1213
|
}
|
1070
1214
|
async getAll(options = {}) {
|
1071
1215
|
const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
|
@@ -1076,11 +1220,11 @@ const _Query = class {
|
|
1076
1220
|
return results;
|
1077
1221
|
}
|
1078
1222
|
async getFirst(options = {}) {
|
1079
|
-
const records = await this.getMany({ ...options,
|
1080
|
-
return records[0]
|
1223
|
+
const records = await this.getMany({ ...options, pagination: { size: 1 } });
|
1224
|
+
return records[0] ?? null;
|
1081
1225
|
}
|
1082
1226
|
cache(ttl) {
|
1083
|
-
return new _Query(__privateGet$
|
1227
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
|
1084
1228
|
}
|
1085
1229
|
nextPage(size, offset) {
|
1086
1230
|
return this.firstPage(size, offset);
|
@@ -1089,10 +1233,10 @@ const _Query = class {
|
|
1089
1233
|
return this.firstPage(size, offset);
|
1090
1234
|
}
|
1091
1235
|
firstPage(size, offset) {
|
1092
|
-
return this.getPaginated({
|
1236
|
+
return this.getPaginated({ pagination: { size, offset } });
|
1093
1237
|
}
|
1094
1238
|
lastPage(size, offset) {
|
1095
|
-
return this.getPaginated({
|
1239
|
+
return this.getPaginated({ pagination: { size, offset, before: "end" } });
|
1096
1240
|
}
|
1097
1241
|
hasNextPage() {
|
1098
1242
|
return this.meta.page.more;
|
@@ -1102,12 +1246,20 @@ let Query = _Query;
|
|
1102
1246
|
_table$1 = new WeakMap();
|
1103
1247
|
_repository = new WeakMap();
|
1104
1248
|
_data = new WeakMap();
|
1249
|
+
function cleanParent(data, parent) {
|
1250
|
+
if (isCursorPaginationOptions(data.pagination)) {
|
1251
|
+
return { ...parent, sorting: void 0, filter: void 0 };
|
1252
|
+
}
|
1253
|
+
return parent;
|
1254
|
+
}
|
1105
1255
|
|
1106
1256
|
function isIdentifiable(x) {
|
1107
1257
|
return isObject(x) && isString(x?.id);
|
1108
1258
|
}
|
1109
1259
|
function isXataRecord(x) {
|
1110
|
-
|
1260
|
+
const record = x;
|
1261
|
+
const metadata = record?.getMetadata();
|
1262
|
+
return isIdentifiable(x) && isObject(metadata) && typeof metadata.version === "number";
|
1111
1263
|
}
|
1112
1264
|
|
1113
1265
|
function isSortFilterString(value) {
|
@@ -1137,7 +1289,7 @@ var __accessCheck$4 = (obj, member, msg) => {
|
|
1137
1289
|
if (!member.has(obj))
|
1138
1290
|
throw TypeError("Cannot " + msg);
|
1139
1291
|
};
|
1140
|
-
var __privateGet$
|
1292
|
+
var __privateGet$4 = (obj, member, getter) => {
|
1141
1293
|
__accessCheck$4(obj, member, "read from private field");
|
1142
1294
|
return getter ? getter.call(obj) : member.get(obj);
|
1143
1295
|
};
|
@@ -1146,7 +1298,7 @@ var __privateAdd$4 = (obj, member, value) => {
|
|
1146
1298
|
throw TypeError("Cannot add the same private member more than once");
|
1147
1299
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1148
1300
|
};
|
1149
|
-
var __privateSet$
|
1301
|
+
var __privateSet$4 = (obj, member, value, setter) => {
|
1150
1302
|
__accessCheck$4(obj, member, "write to private field");
|
1151
1303
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1152
1304
|
return value;
|
@@ -1155,7 +1307,7 @@ var __privateMethod$2 = (obj, member, method) => {
|
|
1155
1307
|
__accessCheck$4(obj, member, "access private method");
|
1156
1308
|
return method;
|
1157
1309
|
};
|
1158
|
-
var _table,
|
1310
|
+
var _table, _getFetchProps, _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;
|
1159
1311
|
class Repository extends Query {
|
1160
1312
|
}
|
1161
1313
|
class RestRepository extends Query {
|
@@ -1167,110 +1319,121 @@ class RestRepository extends Query {
|
|
1167
1319
|
__privateAdd$4(this, _updateRecordWithID);
|
1168
1320
|
__privateAdd$4(this, _upsertRecordWithID);
|
1169
1321
|
__privateAdd$4(this, _deleteRecord);
|
1170
|
-
__privateAdd$4(this, _invalidateCache);
|
1171
|
-
__privateAdd$4(this, _setCacheRecord);
|
1172
|
-
__privateAdd$4(this, _getCacheRecord);
|
1173
1322
|
__privateAdd$4(this, _setCacheQuery);
|
1174
1323
|
__privateAdd$4(this, _getCacheQuery);
|
1324
|
+
__privateAdd$4(this, _getSchemaTables$1);
|
1175
1325
|
__privateAdd$4(this, _table, void 0);
|
1176
|
-
__privateAdd$4(this, _links, void 0);
|
1177
1326
|
__privateAdd$4(this, _getFetchProps, void 0);
|
1178
1327
|
__privateAdd$4(this, _cache, void 0);
|
1179
|
-
|
1180
|
-
__privateSet$
|
1181
|
-
__privateSet$
|
1328
|
+
__privateAdd$4(this, _schemaTables$2, void 0);
|
1329
|
+
__privateSet$4(this, _table, options.table);
|
1330
|
+
__privateSet$4(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
1182
1331
|
this.db = options.db;
|
1183
|
-
__privateSet$
|
1332
|
+
__privateSet$4(this, _cache, options.pluginOptions.cache);
|
1333
|
+
__privateSet$4(this, _schemaTables$2, options.schemaTables);
|
1184
1334
|
}
|
1185
|
-
async create(a, b) {
|
1335
|
+
async create(a, b, c) {
|
1186
1336
|
if (Array.isArray(a)) {
|
1187
|
-
|
1188
|
-
|
1189
|
-
|
1337
|
+
if (a.length === 0)
|
1338
|
+
return [];
|
1339
|
+
const columns = isStringArray(b) ? b : void 0;
|
1340
|
+
return __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a, columns);
|
1190
1341
|
}
|
1191
1342
|
if (isString(a) && isObject(b)) {
|
1192
1343
|
if (a === "")
|
1193
1344
|
throw new Error("The id can't be empty");
|
1194
|
-
const
|
1195
|
-
|
1196
|
-
return record;
|
1345
|
+
const columns = isStringArray(c) ? c : void 0;
|
1346
|
+
return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b, columns);
|
1197
1347
|
}
|
1198
1348
|
if (isObject(a) && isString(a.id)) {
|
1199
1349
|
if (a.id === "")
|
1200
1350
|
throw new Error("The id can't be empty");
|
1201
|
-
const
|
1202
|
-
|
1203
|
-
return record;
|
1351
|
+
const columns = isStringArray(b) ? b : void 0;
|
1352
|
+
return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1204
1353
|
}
|
1205
1354
|
if (isObject(a)) {
|
1206
|
-
const
|
1207
|
-
|
1208
|
-
return record;
|
1355
|
+
const columns = isStringArray(b) ? b : void 0;
|
1356
|
+
return __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a, columns);
|
1209
1357
|
}
|
1210
1358
|
throw new Error("Invalid arguments for create method");
|
1211
1359
|
}
|
1212
|
-
async read(
|
1213
|
-
const
|
1214
|
-
if (
|
1215
|
-
|
1216
|
-
|
1217
|
-
|
1218
|
-
const
|
1219
|
-
|
1220
|
-
|
1221
|
-
|
1222
|
-
|
1223
|
-
|
1224
|
-
|
1225
|
-
|
1360
|
+
async read(a, b) {
|
1361
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1362
|
+
if (Array.isArray(a)) {
|
1363
|
+
if (a.length === 0)
|
1364
|
+
return [];
|
1365
|
+
const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
|
1366
|
+
const finalObjects = await this.getAll({ filter: { id: { $any: ids } }, columns });
|
1367
|
+
const dictionary = finalObjects.reduce((acc, object) => {
|
1368
|
+
acc[object.id] = object;
|
1369
|
+
return acc;
|
1370
|
+
}, {});
|
1371
|
+
return ids.map((id2) => dictionary[id2] ?? null);
|
1372
|
+
}
|
1373
|
+
const id = isString(a) ? a : a.id;
|
1374
|
+
if (isString(id)) {
|
1375
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1376
|
+
try {
|
1377
|
+
const response = await getRecord({
|
1378
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
|
1379
|
+
queryParams: { columns },
|
1380
|
+
...fetchProps
|
1381
|
+
});
|
1382
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1383
|
+
return initObject(this.db, schemaTables, __privateGet$4(this, _table), response);
|
1384
|
+
} catch (e) {
|
1385
|
+
if (isObject(e) && e.status === 404) {
|
1386
|
+
return null;
|
1387
|
+
}
|
1388
|
+
throw e;
|
1226
1389
|
}
|
1227
|
-
throw e;
|
1228
1390
|
}
|
1391
|
+
return null;
|
1229
1392
|
}
|
1230
|
-
async update(a, b) {
|
1393
|
+
async update(a, b, c) {
|
1231
1394
|
if (Array.isArray(a)) {
|
1395
|
+
if (a.length === 0)
|
1396
|
+
return [];
|
1232
1397
|
if (a.length > 100) {
|
1233
1398
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1234
1399
|
}
|
1235
|
-
|
1400
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1401
|
+
return Promise.all(a.map((object) => this.update(object, columns)));
|
1236
1402
|
}
|
1237
1403
|
if (isString(a) && isObject(b)) {
|
1238
|
-
|
1239
|
-
|
1240
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1241
|
-
return record;
|
1404
|
+
const columns = isStringArray(c) ? c : void 0;
|
1405
|
+
return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b, columns);
|
1242
1406
|
}
|
1243
1407
|
if (isObject(a) && isString(a.id)) {
|
1244
|
-
|
1245
|
-
|
1246
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1247
|
-
return record;
|
1408
|
+
const columns = isStringArray(b) ? b : void 0;
|
1409
|
+
return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1248
1410
|
}
|
1249
1411
|
throw new Error("Invalid arguments for update method");
|
1250
1412
|
}
|
1251
|
-
async createOrUpdate(a, b) {
|
1413
|
+
async createOrUpdate(a, b, c) {
|
1252
1414
|
if (Array.isArray(a)) {
|
1415
|
+
if (a.length === 0)
|
1416
|
+
return [];
|
1253
1417
|
if (a.length > 100) {
|
1254
1418
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1255
1419
|
}
|
1256
|
-
|
1420
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1421
|
+
return Promise.all(a.map((object) => this.createOrUpdate(object, columns)));
|
1257
1422
|
}
|
1258
1423
|
if (isString(a) && isObject(b)) {
|
1259
|
-
|
1260
|
-
|
1261
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1262
|
-
return record;
|
1424
|
+
const columns = isStringArray(c) ? c : void 0;
|
1425
|
+
return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b, columns);
|
1263
1426
|
}
|
1264
1427
|
if (isObject(a) && isString(a.id)) {
|
1265
|
-
|
1266
|
-
|
1267
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1268
|
-
return record;
|
1428
|
+
const columns = isStringArray(c) ? c : void 0;
|
1429
|
+
return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1269
1430
|
}
|
1270
1431
|
throw new Error("Invalid arguments for createOrUpdate method");
|
1271
1432
|
}
|
1272
1433
|
async delete(a) {
|
1273
1434
|
if (Array.isArray(a)) {
|
1435
|
+
if (a.length === 0)
|
1436
|
+
return;
|
1274
1437
|
if (a.length > 100) {
|
1275
1438
|
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
1276
1439
|
}
|
@@ -1279,24 +1442,30 @@ class RestRepository extends Query {
|
|
1279
1442
|
}
|
1280
1443
|
if (isString(a)) {
|
1281
1444
|
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a);
|
1282
|
-
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
|
1283
1445
|
return;
|
1284
1446
|
}
|
1285
1447
|
if (isObject(a) && isString(a.id)) {
|
1286
1448
|
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id);
|
1287
|
-
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
|
1288
1449
|
return;
|
1289
1450
|
}
|
1290
1451
|
throw new Error("Invalid arguments for delete method");
|
1291
1452
|
}
|
1292
1453
|
async search(query, options = {}) {
|
1293
|
-
const fetchProps = await __privateGet$
|
1294
|
-
const { records } = await
|
1295
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1296
|
-
body: {
|
1454
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1455
|
+
const { records } = await searchTable({
|
1456
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1457
|
+
body: {
|
1458
|
+
query,
|
1459
|
+
fuzziness: options.fuzziness,
|
1460
|
+
prefix: options.prefix,
|
1461
|
+
highlight: options.highlight,
|
1462
|
+
filter: options.filter,
|
1463
|
+
boosters: options.boosters
|
1464
|
+
},
|
1297
1465
|
...fetchProps
|
1298
1466
|
});
|
1299
|
-
|
1467
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1468
|
+
return records.map((item) => initObject(this.db, schemaTables, __privateGet$4(this, _table), item));
|
1300
1469
|
}
|
1301
1470
|
async query(query) {
|
1302
1471
|
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
@@ -1305,154 +1474,138 @@ class RestRepository extends Query {
|
|
1305
1474
|
const data = query.getQueryOptions();
|
1306
1475
|
const body = {
|
1307
1476
|
filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
|
1308
|
-
sort: data.sort ? buildSortFilter(data.sort) : void 0,
|
1309
|
-
page: data.
|
1477
|
+
sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
|
1478
|
+
page: data.pagination,
|
1310
1479
|
columns: data.columns
|
1311
1480
|
};
|
1312
|
-
const fetchProps = await __privateGet$
|
1481
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1313
1482
|
const { meta, records: objects } = await queryTable({
|
1314
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1483
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1315
1484
|
body,
|
1316
1485
|
...fetchProps
|
1317
1486
|
});
|
1318
|
-
const
|
1487
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1488
|
+
const records = objects.map((record) => initObject(this.db, schemaTables, __privateGet$4(this, _table), record));
|
1319
1489
|
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1320
1490
|
return new Page(query, meta, records);
|
1321
1491
|
}
|
1322
1492
|
}
|
1323
1493
|
_table = new WeakMap();
|
1324
|
-
_links = new WeakMap();
|
1325
1494
|
_getFetchProps = new WeakMap();
|
1326
1495
|
_cache = new WeakMap();
|
1496
|
+
_schemaTables$2 = new WeakMap();
|
1327
1497
|
_insertRecordWithoutId = new WeakSet();
|
1328
|
-
insertRecordWithoutId_fn = async function(object) {
|
1329
|
-
const fetchProps = await __privateGet$
|
1498
|
+
insertRecordWithoutId_fn = async function(object, columns = ["*"]) {
|
1499
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1330
1500
|
const record = transformObjectLinks(object);
|
1331
1501
|
const response = await insertRecord({
|
1332
1502
|
pathParams: {
|
1333
1503
|
workspace: "{workspaceId}",
|
1334
1504
|
dbBranchName: "{dbBranch}",
|
1335
|
-
tableName: __privateGet$
|
1505
|
+
tableName: __privateGet$4(this, _table)
|
1336
1506
|
},
|
1507
|
+
queryParams: { columns },
|
1337
1508
|
body: record,
|
1338
1509
|
...fetchProps
|
1339
1510
|
});
|
1340
|
-
const
|
1341
|
-
|
1342
|
-
throw new Error("The server failed to save the record");
|
1343
|
-
}
|
1344
|
-
return finalObject;
|
1511
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1512
|
+
return initObject(this.db, schemaTables, __privateGet$4(this, _table), response);
|
1345
1513
|
};
|
1346
1514
|
_insertRecordWithId = new WeakSet();
|
1347
|
-
insertRecordWithId_fn = async function(recordId, object) {
|
1348
|
-
const fetchProps = await __privateGet$
|
1515
|
+
insertRecordWithId_fn = async function(recordId, object, columns = ["*"]) {
|
1516
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1349
1517
|
const record = transformObjectLinks(object);
|
1350
1518
|
const response = await insertRecordWithID({
|
1351
1519
|
pathParams: {
|
1352
1520
|
workspace: "{workspaceId}",
|
1353
1521
|
dbBranchName: "{dbBranch}",
|
1354
|
-
tableName: __privateGet$
|
1522
|
+
tableName: __privateGet$4(this, _table),
|
1355
1523
|
recordId
|
1356
1524
|
},
|
1357
1525
|
body: record,
|
1358
|
-
queryParams: { createOnly: true },
|
1526
|
+
queryParams: { createOnly: true, columns },
|
1359
1527
|
...fetchProps
|
1360
1528
|
});
|
1361
|
-
const
|
1362
|
-
|
1363
|
-
throw new Error("The server failed to save the record");
|
1364
|
-
}
|
1365
|
-
return finalObject;
|
1529
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1530
|
+
return initObject(this.db, schemaTables, __privateGet$4(this, _table), response);
|
1366
1531
|
};
|
1367
1532
|
_bulkInsertTableRecords = new WeakSet();
|
1368
|
-
bulkInsertTableRecords_fn = async function(objects) {
|
1369
|
-
const fetchProps = await __privateGet$
|
1533
|
+
bulkInsertTableRecords_fn = async function(objects, columns = ["*"]) {
|
1534
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1370
1535
|
const records = objects.map((object) => transformObjectLinks(object));
|
1371
1536
|
const response = await bulkInsertTableRecords({
|
1372
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1537
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1538
|
+
queryParams: { columns },
|
1373
1539
|
body: { records },
|
1374
1540
|
...fetchProps
|
1375
1541
|
});
|
1376
|
-
|
1377
|
-
|
1378
|
-
throw new Error("The server failed to save some records");
|
1542
|
+
if (!isResponseWithRecords(response)) {
|
1543
|
+
throw new Error("Request included columns but server didn't include them");
|
1379
1544
|
}
|
1380
|
-
|
1545
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1546
|
+
return response.records?.map((item) => initObject(this.db, schemaTables, __privateGet$4(this, _table), item));
|
1381
1547
|
};
|
1382
1548
|
_updateRecordWithID = new WeakSet();
|
1383
|
-
updateRecordWithID_fn = async function(recordId, object) {
|
1384
|
-
const fetchProps = await __privateGet$
|
1549
|
+
updateRecordWithID_fn = async function(recordId, object, columns = ["*"]) {
|
1550
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1385
1551
|
const record = transformObjectLinks(object);
|
1386
1552
|
const response = await updateRecordWithID({
|
1387
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1553
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1554
|
+
queryParams: { columns },
|
1388
1555
|
body: record,
|
1389
1556
|
...fetchProps
|
1390
1557
|
});
|
1391
|
-
const
|
1392
|
-
|
1393
|
-
throw new Error("The server failed to save the record");
|
1394
|
-
return item;
|
1558
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1559
|
+
return initObject(this.db, schemaTables, __privateGet$4(this, _table), response);
|
1395
1560
|
};
|
1396
1561
|
_upsertRecordWithID = new WeakSet();
|
1397
|
-
upsertRecordWithID_fn = async function(recordId, object) {
|
1398
|
-
const fetchProps = await __privateGet$
|
1562
|
+
upsertRecordWithID_fn = async function(recordId, object, columns = ["*"]) {
|
1563
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1399
1564
|
const response = await upsertRecordWithID({
|
1400
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1565
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1566
|
+
queryParams: { columns },
|
1401
1567
|
body: object,
|
1402
1568
|
...fetchProps
|
1403
1569
|
});
|
1404
|
-
const
|
1405
|
-
|
1406
|
-
throw new Error("The server failed to save the record");
|
1407
|
-
return item;
|
1570
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1571
|
+
return initObject(this.db, schemaTables, __privateGet$4(this, _table), response);
|
1408
1572
|
};
|
1409
1573
|
_deleteRecord = new WeakSet();
|
1410
1574
|
deleteRecord_fn = async function(recordId) {
|
1411
|
-
const fetchProps = await __privateGet$
|
1575
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1412
1576
|
await deleteRecord({
|
1413
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1577
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1414
1578
|
...fetchProps
|
1415
1579
|
});
|
1416
1580
|
};
|
1417
|
-
_invalidateCache = new WeakSet();
|
1418
|
-
invalidateCache_fn = async function(recordId) {
|
1419
|
-
await __privateGet$3(this, _cache).delete(`rec_${__privateGet$3(this, _table)}:${recordId}`);
|
1420
|
-
const cacheItems = await __privateGet$3(this, _cache).getAll();
|
1421
|
-
const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
|
1422
|
-
for (const [key, value] of queries) {
|
1423
|
-
const ids = getIds(value);
|
1424
|
-
if (ids.includes(recordId))
|
1425
|
-
await __privateGet$3(this, _cache).delete(key);
|
1426
|
-
}
|
1427
|
-
};
|
1428
|
-
_setCacheRecord = new WeakSet();
|
1429
|
-
setCacheRecord_fn = async function(record) {
|
1430
|
-
if (!__privateGet$3(this, _cache).cacheRecords)
|
1431
|
-
return;
|
1432
|
-
await __privateGet$3(this, _cache).set(`rec_${__privateGet$3(this, _table)}:${record.id}`, record);
|
1433
|
-
};
|
1434
|
-
_getCacheRecord = new WeakSet();
|
1435
|
-
getCacheRecord_fn = async function(recordId) {
|
1436
|
-
if (!__privateGet$3(this, _cache).cacheRecords)
|
1437
|
-
return null;
|
1438
|
-
return __privateGet$3(this, _cache).get(`rec_${__privateGet$3(this, _table)}:${recordId}`);
|
1439
|
-
};
|
1440
1581
|
_setCacheQuery = new WeakSet();
|
1441
1582
|
setCacheQuery_fn = async function(query, meta, records) {
|
1442
|
-
await __privateGet$
|
1583
|
+
await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
|
1443
1584
|
};
|
1444
1585
|
_getCacheQuery = new WeakSet();
|
1445
1586
|
getCacheQuery_fn = async function(query) {
|
1446
|
-
const key = `query_${__privateGet$
|
1447
|
-
const result = await __privateGet$
|
1587
|
+
const key = `query_${__privateGet$4(this, _table)}:${query.key()}`;
|
1588
|
+
const result = await __privateGet$4(this, _cache).get(key);
|
1448
1589
|
if (!result)
|
1449
1590
|
return null;
|
1450
|
-
const { cache: ttl = __privateGet$
|
1451
|
-
if (
|
1452
|
-
return
|
1591
|
+
const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
|
1592
|
+
if (ttl < 0)
|
1593
|
+
return null;
|
1453
1594
|
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1454
1595
|
return hasExpired ? null : result;
|
1455
1596
|
};
|
1597
|
+
_getSchemaTables$1 = new WeakSet();
|
1598
|
+
getSchemaTables_fn$1 = async function() {
|
1599
|
+
if (__privateGet$4(this, _schemaTables$2))
|
1600
|
+
return __privateGet$4(this, _schemaTables$2);
|
1601
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1602
|
+
const { schema } = await getBranchDetails({
|
1603
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1604
|
+
...fetchProps
|
1605
|
+
});
|
1606
|
+
__privateSet$4(this, _schemaTables$2, schema.tables);
|
1607
|
+
return schema.tables;
|
1608
|
+
};
|
1456
1609
|
const transformObjectLinks = (object) => {
|
1457
1610
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
1458
1611
|
if (key === "xata")
|
@@ -1460,47 +1613,63 @@ const transformObjectLinks = (object) => {
|
|
1460
1613
|
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1461
1614
|
}, {});
|
1462
1615
|
};
|
1463
|
-
const initObject = (db,
|
1616
|
+
const initObject = (db, schemaTables, table, object) => {
|
1464
1617
|
const result = {};
|
1465
|
-
|
1466
|
-
|
1467
|
-
|
1468
|
-
|
1469
|
-
|
1470
|
-
|
1471
|
-
|
1618
|
+
const { xata, ...rest } = object ?? {};
|
1619
|
+
Object.assign(result, rest);
|
1620
|
+
const { columns } = schemaTables.find(({ name }) => name === table) ?? {};
|
1621
|
+
if (!columns)
|
1622
|
+
console.error(`Table ${table} not found in schema`);
|
1623
|
+
for (const column of columns ?? []) {
|
1624
|
+
const value = result[column.name];
|
1625
|
+
switch (column.type) {
|
1626
|
+
case "datetime": {
|
1627
|
+
const date = value !== void 0 ? new Date(value) : void 0;
|
1628
|
+
if (date && isNaN(date.getTime())) {
|
1629
|
+
console.error(`Failed to parse date ${value} for field ${column.name}`);
|
1630
|
+
} else if (date) {
|
1631
|
+
result[column.name] = date;
|
1632
|
+
}
|
1633
|
+
break;
|
1634
|
+
}
|
1635
|
+
case "link": {
|
1636
|
+
const linkTable = column.link?.table;
|
1637
|
+
if (!linkTable) {
|
1638
|
+
console.error(`Failed to parse link for field ${column.name}`);
|
1639
|
+
} else if (isObject(value)) {
|
1640
|
+
result[column.name] = initObject(db, schemaTables, linkTable, value);
|
1641
|
+
}
|
1642
|
+
break;
|
1643
|
+
}
|
1472
1644
|
}
|
1473
1645
|
}
|
1474
|
-
result.read = function() {
|
1475
|
-
return db[table].read(result["id"]);
|
1646
|
+
result.read = function(columns2) {
|
1647
|
+
return db[table].read(result["id"], columns2);
|
1476
1648
|
};
|
1477
|
-
result.update = function(data) {
|
1478
|
-
return db[table].update(result["id"], data);
|
1649
|
+
result.update = function(data, columns2) {
|
1650
|
+
return db[table].update(result["id"], data, columns2);
|
1479
1651
|
};
|
1480
1652
|
result.delete = function() {
|
1481
1653
|
return db[table].delete(result["id"]);
|
1482
1654
|
};
|
1483
|
-
|
1655
|
+
result.getMetadata = function() {
|
1656
|
+
return xata;
|
1657
|
+
};
|
1658
|
+
for (const prop of ["read", "update", "delete", "getMetadata"]) {
|
1484
1659
|
Object.defineProperty(result, prop, { enumerable: false });
|
1485
1660
|
}
|
1486
1661
|
Object.freeze(result);
|
1487
1662
|
return result;
|
1488
1663
|
};
|
1489
|
-
function
|
1490
|
-
|
1491
|
-
return value.map((item) => getIds(item)).flat();
|
1492
|
-
}
|
1493
|
-
if (!isObject(value))
|
1494
|
-
return [];
|
1495
|
-
const nestedIds = Object.values(value).map((item) => getIds(item)).flat();
|
1496
|
-
return isString(value.id) ? [value.id, ...nestedIds] : nestedIds;
|
1664
|
+
function isResponseWithRecords(value) {
|
1665
|
+
return isObject(value) && Array.isArray(value.records);
|
1497
1666
|
}
|
1498
1667
|
|
1499
1668
|
var __accessCheck$3 = (obj, member, msg) => {
|
1500
1669
|
if (!member.has(obj))
|
1501
1670
|
throw TypeError("Cannot " + msg);
|
1502
1671
|
};
|
1503
|
-
var __privateGet$
|
1672
|
+
var __privateGet$3 = (obj, member, getter) => {
|
1504
1673
|
__accessCheck$3(obj, member, "read from private field");
|
1505
1674
|
return getter ? getter.call(obj) : member.get(obj);
|
1506
1675
|
};
|
@@ -1509,7 +1678,7 @@ var __privateAdd$3 = (obj, member, value) => {
|
|
1509
1678
|
throw TypeError("Cannot add the same private member more than once");
|
1510
1679
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1511
1680
|
};
|
1512
|
-
var __privateSet$
|
1681
|
+
var __privateSet$3 = (obj, member, value, setter) => {
|
1513
1682
|
__accessCheck$3(obj, member, "write to private field");
|
1514
1683
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1515
1684
|
return value;
|
@@ -1518,30 +1687,29 @@ var _map;
|
|
1518
1687
|
class SimpleCache {
|
1519
1688
|
constructor(options = {}) {
|
1520
1689
|
__privateAdd$3(this, _map, void 0);
|
1521
|
-
__privateSet$
|
1690
|
+
__privateSet$3(this, _map, /* @__PURE__ */ new Map());
|
1522
1691
|
this.capacity = options.max ?? 500;
|
1523
|
-
this.cacheRecords = options.cacheRecords ?? true;
|
1524
1692
|
this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
|
1525
1693
|
}
|
1526
1694
|
async getAll() {
|
1527
|
-
return Object.fromEntries(__privateGet$
|
1695
|
+
return Object.fromEntries(__privateGet$3(this, _map));
|
1528
1696
|
}
|
1529
1697
|
async get(key) {
|
1530
|
-
return __privateGet$
|
1698
|
+
return __privateGet$3(this, _map).get(key) ?? null;
|
1531
1699
|
}
|
1532
1700
|
async set(key, value) {
|
1533
1701
|
await this.delete(key);
|
1534
|
-
__privateGet$
|
1535
|
-
if (__privateGet$
|
1536
|
-
const leastRecentlyUsed = __privateGet$
|
1702
|
+
__privateGet$3(this, _map).set(key, value);
|
1703
|
+
if (__privateGet$3(this, _map).size > this.capacity) {
|
1704
|
+
const leastRecentlyUsed = __privateGet$3(this, _map).keys().next().value;
|
1537
1705
|
await this.delete(leastRecentlyUsed);
|
1538
1706
|
}
|
1539
1707
|
}
|
1540
1708
|
async delete(key) {
|
1541
|
-
__privateGet$
|
1709
|
+
__privateGet$3(this, _map).delete(key);
|
1542
1710
|
}
|
1543
1711
|
async clear() {
|
1544
|
-
return __privateGet$
|
1712
|
+
return __privateGet$3(this, _map).clear();
|
1545
1713
|
}
|
1546
1714
|
}
|
1547
1715
|
_map = new WeakMap();
|
@@ -1569,7 +1737,7 @@ var __accessCheck$2 = (obj, member, msg) => {
|
|
1569
1737
|
if (!member.has(obj))
|
1570
1738
|
throw TypeError("Cannot " + msg);
|
1571
1739
|
};
|
1572
|
-
var __privateGet$
|
1740
|
+
var __privateGet$2 = (obj, member, getter) => {
|
1573
1741
|
__accessCheck$2(obj, member, "read from private field");
|
1574
1742
|
return getter ? getter.call(obj) : member.get(obj);
|
1575
1743
|
};
|
@@ -1578,122 +1746,158 @@ var __privateAdd$2 = (obj, member, value) => {
|
|
1578
1746
|
throw TypeError("Cannot add the same private member more than once");
|
1579
1747
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1580
1748
|
};
|
1581
|
-
var
|
1749
|
+
var __privateSet$2 = (obj, member, value, setter) => {
|
1750
|
+
__accessCheck$2(obj, member, "write to private field");
|
1751
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1752
|
+
return value;
|
1753
|
+
};
|
1754
|
+
var _tables, _schemaTables$1;
|
1582
1755
|
class SchemaPlugin extends XataPlugin {
|
1583
|
-
constructor(
|
1756
|
+
constructor(schemaTables) {
|
1584
1757
|
super();
|
1585
|
-
this.links = links;
|
1586
|
-
this.tableNames = tableNames;
|
1587
1758
|
__privateAdd$2(this, _tables, {});
|
1759
|
+
__privateAdd$2(this, _schemaTables$1, void 0);
|
1760
|
+
__privateSet$2(this, _schemaTables$1, schemaTables);
|
1588
1761
|
}
|
1589
1762
|
build(pluginOptions) {
|
1590
|
-
const links = this.links;
|
1591
1763
|
const db = new Proxy({}, {
|
1592
1764
|
get: (_target, table) => {
|
1593
1765
|
if (!isString(table))
|
1594
1766
|
throw new Error("Invalid table name");
|
1595
|
-
if (
|
1596
|
-
__privateGet$
|
1767
|
+
if (__privateGet$2(this, _tables)[table] === void 0) {
|
1768
|
+
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1597
1769
|
}
|
1598
|
-
return __privateGet$
|
1770
|
+
return __privateGet$2(this, _tables)[table];
|
1599
1771
|
}
|
1600
1772
|
});
|
1601
|
-
|
1602
|
-
|
1773
|
+
const tableNames = __privateGet$2(this, _schemaTables$1)?.map(({ name }) => name) ?? [];
|
1774
|
+
for (const table of tableNames) {
|
1775
|
+
db[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1603
1776
|
}
|
1604
1777
|
return db;
|
1605
1778
|
}
|
1606
1779
|
}
|
1607
1780
|
_tables = new WeakMap();
|
1781
|
+
_schemaTables$1 = new WeakMap();
|
1608
1782
|
|
1609
1783
|
var __accessCheck$1 = (obj, member, msg) => {
|
1610
1784
|
if (!member.has(obj))
|
1611
1785
|
throw TypeError("Cannot " + msg);
|
1612
1786
|
};
|
1787
|
+
var __privateGet$1 = (obj, member, getter) => {
|
1788
|
+
__accessCheck$1(obj, member, "read from private field");
|
1789
|
+
return getter ? getter.call(obj) : member.get(obj);
|
1790
|
+
};
|
1613
1791
|
var __privateAdd$1 = (obj, member, value) => {
|
1614
1792
|
if (member.has(obj))
|
1615
1793
|
throw TypeError("Cannot add the same private member more than once");
|
1616
1794
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1617
1795
|
};
|
1796
|
+
var __privateSet$1 = (obj, member, value, setter) => {
|
1797
|
+
__accessCheck$1(obj, member, "write to private field");
|
1798
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1799
|
+
return value;
|
1800
|
+
};
|
1618
1801
|
var __privateMethod$1 = (obj, member, method) => {
|
1619
1802
|
__accessCheck$1(obj, member, "access private method");
|
1620
1803
|
return method;
|
1621
1804
|
};
|
1622
|
-
var _search, search_fn;
|
1805
|
+
var _schemaTables, _search, search_fn, _getSchemaTables, getSchemaTables_fn;
|
1623
1806
|
class SearchPlugin extends XataPlugin {
|
1624
|
-
constructor(db,
|
1807
|
+
constructor(db, schemaTables) {
|
1625
1808
|
super();
|
1626
1809
|
this.db = db;
|
1627
|
-
this.links = links;
|
1628
1810
|
__privateAdd$1(this, _search);
|
1811
|
+
__privateAdd$1(this, _getSchemaTables);
|
1812
|
+
__privateAdd$1(this, _schemaTables, void 0);
|
1813
|
+
__privateSet$1(this, _schemaTables, schemaTables);
|
1629
1814
|
}
|
1630
1815
|
build({ getFetchProps }) {
|
1631
1816
|
return {
|
1632
1817
|
all: async (query, options = {}) => {
|
1633
1818
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1819
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1634
1820
|
return records.map((record) => {
|
1635
1821
|
const { table = "orphan" } = record.xata;
|
1636
|
-
return { table, record: initObject(this.db,
|
1822
|
+
return { table, record: initObject(this.db, schemaTables, table, record) };
|
1637
1823
|
});
|
1638
1824
|
},
|
1639
1825
|
byTable: async (query, options = {}) => {
|
1640
1826
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1827
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1641
1828
|
return records.reduce((acc, record) => {
|
1642
1829
|
const { table = "orphan" } = record.xata;
|
1643
1830
|
const items = acc[table] ?? [];
|
1644
|
-
const item = initObject(this.db,
|
1831
|
+
const item = initObject(this.db, schemaTables, table, record);
|
1645
1832
|
return { ...acc, [table]: [...items, item] };
|
1646
1833
|
}, {});
|
1647
1834
|
}
|
1648
1835
|
};
|
1649
1836
|
}
|
1650
1837
|
}
|
1838
|
+
_schemaTables = new WeakMap();
|
1651
1839
|
_search = new WeakSet();
|
1652
1840
|
search_fn = async function(query, options, getFetchProps) {
|
1653
1841
|
const fetchProps = await getFetchProps();
|
1654
|
-
const { tables, fuzziness } = options ?? {};
|
1842
|
+
const { tables, fuzziness, highlight, prefix } = options ?? {};
|
1655
1843
|
const { records } = await searchBranch({
|
1656
1844
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1657
|
-
body: { tables, query, fuzziness },
|
1845
|
+
body: { tables, query, fuzziness, prefix, highlight },
|
1658
1846
|
...fetchProps
|
1659
1847
|
});
|
1660
1848
|
return records;
|
1661
1849
|
};
|
1850
|
+
_getSchemaTables = new WeakSet();
|
1851
|
+
getSchemaTables_fn = async function(getFetchProps) {
|
1852
|
+
if (__privateGet$1(this, _schemaTables))
|
1853
|
+
return __privateGet$1(this, _schemaTables);
|
1854
|
+
const fetchProps = await getFetchProps();
|
1855
|
+
const { schema } = await getBranchDetails({
|
1856
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1857
|
+
...fetchProps
|
1858
|
+
});
|
1859
|
+
__privateSet$1(this, _schemaTables, schema.tables);
|
1860
|
+
return schema.tables;
|
1861
|
+
};
|
1662
1862
|
|
1663
1863
|
const isBranchStrategyBuilder = (strategy) => {
|
1664
1864
|
return typeof strategy === "function";
|
1665
1865
|
};
|
1666
1866
|
|
1667
|
-
const envBranchNames = [
|
1668
|
-
"XATA_BRANCH",
|
1669
|
-
"VERCEL_GIT_COMMIT_REF",
|
1670
|
-
"CF_PAGES_BRANCH",
|
1671
|
-
"BRANCH"
|
1672
|
-
];
|
1673
|
-
const defaultBranch = "main";
|
1674
1867
|
async function getCurrentBranchName(options) {
|
1675
|
-
const
|
1676
|
-
if (
|
1677
|
-
|
1678
|
-
|
1679
|
-
|
1680
|
-
|
1681
|
-
|
1682
|
-
|
1683
|
-
|
1684
|
-
return defaultBranch;
|
1868
|
+
const { branch, envBranch } = getEnvironment();
|
1869
|
+
if (branch) {
|
1870
|
+
const details = await getDatabaseBranch(branch, options);
|
1871
|
+
if (details)
|
1872
|
+
return branch;
|
1873
|
+
console.warn(`Branch ${branch} not found in Xata. Ignoring...`);
|
1874
|
+
}
|
1875
|
+
const gitBranch = envBranch || await getGitBranch();
|
1876
|
+
return resolveXataBranch(gitBranch, options);
|
1685
1877
|
}
|
1686
1878
|
async function getCurrentBranchDetails(options) {
|
1687
|
-
const
|
1688
|
-
|
1689
|
-
|
1690
|
-
|
1691
|
-
|
1692
|
-
|
1693
|
-
|
1694
|
-
|
1695
|
-
|
1696
|
-
|
1879
|
+
const branch = await getCurrentBranchName(options);
|
1880
|
+
return getDatabaseBranch(branch, options);
|
1881
|
+
}
|
1882
|
+
async function resolveXataBranch(gitBranch, options) {
|
1883
|
+
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1884
|
+
const apiKey = options?.apiKey || getAPIKey();
|
1885
|
+
if (!databaseURL)
|
1886
|
+
throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
|
1887
|
+
if (!apiKey)
|
1888
|
+
throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
|
1889
|
+
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1890
|
+
const [workspace] = host.split(".");
|
1891
|
+
const { fallbackBranch } = getEnvironment();
|
1892
|
+
const { branch } = await resolveBranch({
|
1893
|
+
apiKey,
|
1894
|
+
apiUrl: databaseURL,
|
1895
|
+
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1896
|
+
workspacesApiUrl: `${protocol}//${host}`,
|
1897
|
+
pathParams: { dbName, workspace },
|
1898
|
+
queryParams: { gitBranch, fallbackBranch }
|
1899
|
+
});
|
1900
|
+
return branch;
|
1697
1901
|
}
|
1698
1902
|
async function getDatabaseBranch(branch, options) {
|
1699
1903
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
@@ -1711,10 +1915,7 @@ async function getDatabaseBranch(branch, options) {
|
|
1711
1915
|
apiUrl: databaseURL,
|
1712
1916
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1713
1917
|
workspacesApiUrl: `${protocol}//${host}`,
|
1714
|
-
pathParams: {
|
1715
|
-
dbBranchName,
|
1716
|
-
workspace
|
1717
|
-
}
|
1918
|
+
pathParams: { dbBranchName, workspace }
|
1718
1919
|
});
|
1719
1920
|
} catch (err) {
|
1720
1921
|
if (isObject(err) && err.status === 404)
|
@@ -1722,21 +1923,10 @@ async function getDatabaseBranch(branch, options) {
|
|
1722
1923
|
throw err;
|
1723
1924
|
}
|
1724
1925
|
}
|
1725
|
-
function getBranchByEnvVariable() {
|
1726
|
-
for (const name of envBranchNames) {
|
1727
|
-
const value = getEnvVariable(name);
|
1728
|
-
if (value) {
|
1729
|
-
return value;
|
1730
|
-
}
|
1731
|
-
}
|
1732
|
-
try {
|
1733
|
-
return XATA_BRANCH;
|
1734
|
-
} catch (err) {
|
1735
|
-
}
|
1736
|
-
}
|
1737
1926
|
function getDatabaseURL() {
|
1738
1927
|
try {
|
1739
|
-
|
1928
|
+
const { databaseURL } = getEnvironment();
|
1929
|
+
return databaseURL;
|
1740
1930
|
} catch (err) {
|
1741
1931
|
return void 0;
|
1742
1932
|
}
|
@@ -1767,7 +1957,7 @@ var __privateMethod = (obj, member, method) => {
|
|
1767
1957
|
const buildClient = (plugins) => {
|
1768
1958
|
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1769
1959
|
return _a = class {
|
1770
|
-
constructor(options = {},
|
1960
|
+
constructor(options = {}, schemaTables) {
|
1771
1961
|
__privateAdd(this, _parseOptions);
|
1772
1962
|
__privateAdd(this, _getFetchProps);
|
1773
1963
|
__privateAdd(this, _evaluateBranch);
|
@@ -1777,12 +1967,12 @@ const buildClient = (plugins) => {
|
|
1777
1967
|
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
1778
1968
|
cache: safeOptions.cache
|
1779
1969
|
};
|
1780
|
-
const db = new SchemaPlugin(
|
1781
|
-
const search = new SearchPlugin(db,
|
1970
|
+
const db = new SchemaPlugin(schemaTables).build(pluginOptions);
|
1971
|
+
const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
|
1782
1972
|
this.db = db;
|
1783
1973
|
this.search = search;
|
1784
1974
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
1785
|
-
if (
|
1975
|
+
if (namespace === void 0)
|
1786
1976
|
continue;
|
1787
1977
|
const result = namespace.build(pluginOptions);
|
1788
1978
|
if (result instanceof Promise) {
|
@@ -1798,8 +1988,8 @@ const buildClient = (plugins) => {
|
|
1798
1988
|
const fetch = getFetchImplementation(options?.fetch);
|
1799
1989
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1800
1990
|
const apiKey = options?.apiKey || getAPIKey();
|
1801
|
-
const cache = options?.cache ?? new SimpleCache({
|
1802
|
-
const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
1991
|
+
const cache = options?.cache ?? new SimpleCache({ defaultQueryTTL: 0 });
|
1992
|
+
const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
1803
1993
|
if (!databaseURL || !apiKey) {
|
1804
1994
|
throw new Error("Options databaseURL and apiKey are required");
|
1805
1995
|
}
|
@@ -1826,7 +2016,7 @@ const buildClient = (plugins) => {
|
|
1826
2016
|
}, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
|
1827
2017
|
if (__privateGet(this, _branch))
|
1828
2018
|
return __privateGet(this, _branch);
|
1829
|
-
if (
|
2019
|
+
if (param === void 0)
|
1830
2020
|
return void 0;
|
1831
2021
|
const strategies = Array.isArray(param) ? [...param] : [param];
|
1832
2022
|
const evaluateBranch = async (strategy) => {
|
@@ -1851,5 +2041,5 @@ class XataError extends Error {
|
|
1851
2041
|
}
|
1852
2042
|
}
|
1853
2043
|
|
1854
|
-
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 };
|
2044
|
+
export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, RecordArray, 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, isCursorPaginationOptions, isIdentifiable, isNot, isXataRecord, le, lt, lte, notExists, operationsByTag, pattern, queryTable, removeGitBranchesEntry, removeWorkspaceMember, resendWorkspaceMemberInvite, resolveBranch, searchBranch, searchTable, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberInvite, updateWorkspaceMemberRole, upsertRecordWithID };
|
1855
2045
|
//# sourceMappingURL=index.mjs.map
|