@xata.io/client 0.0.0-alpha.vf38f30b → 0.0.0-alpha.vf481c73
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 +136 -0
- package/README.md +271 -1
- package/Usage.md +395 -0
- package/dist/index.cjs +703 -270
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +692 -146
- package/dist/index.mjs +696 -271
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -3
- package/tsconfig.json +1 -0
package/dist/index.cjs
CHANGED
@@ -11,27 +11,88 @@ function compact(arr) {
|
|
11
11
|
function isObject(value) {
|
12
12
|
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
13
13
|
}
|
14
|
+
function isDefined(value) {
|
15
|
+
return value !== null && value !== void 0;
|
16
|
+
}
|
14
17
|
function isString(value) {
|
15
|
-
return value
|
18
|
+
return isDefined(value) && typeof value === "string";
|
19
|
+
}
|
20
|
+
function toBase64(value) {
|
21
|
+
try {
|
22
|
+
return btoa(value);
|
23
|
+
} catch (err) {
|
24
|
+
const buf = Buffer;
|
25
|
+
return buf.from(value).toString("base64");
|
26
|
+
}
|
16
27
|
}
|
17
28
|
|
18
|
-
function
|
29
|
+
function getEnvironment() {
|
19
30
|
try {
|
20
|
-
if (isObject(process) &&
|
21
|
-
return
|
31
|
+
if (isObject(process) && isObject(process.env)) {
|
32
|
+
return {
|
33
|
+
apiKey: process.env.XATA_API_KEY ?? getGlobalApiKey(),
|
34
|
+
databaseURL: process.env.XATA_DATABASE_URL ?? getGlobalDatabaseURL(),
|
35
|
+
branch: process.env.XATA_BRANCH,
|
36
|
+
envBranch: process.env.VERCEL_GIT_COMMIT_REF ?? process.env.CF_PAGES_BRANCH ?? process.env.BRANCH,
|
37
|
+
fallbackBranch: process.env.XATA_FALLBACK_BRANCH ?? getGlobalFallbackBranch()
|
38
|
+
};
|
22
39
|
}
|
23
40
|
} catch (err) {
|
24
41
|
}
|
25
42
|
try {
|
26
|
-
if (isObject(Deno) &&
|
27
|
-
return
|
43
|
+
if (isObject(Deno) && isObject(Deno.env)) {
|
44
|
+
return {
|
45
|
+
apiKey: Deno.env.get("XATA_API_KEY") ?? getGlobalApiKey(),
|
46
|
+
databaseURL: Deno.env.get("XATA_DATABASE_URL") ?? getGlobalDatabaseURL(),
|
47
|
+
branch: Deno.env.get("XATA_BRANCH"),
|
48
|
+
envBranch: Deno.env.get("VERCEL_GIT_COMMIT_REF") ?? Deno.env.get("CF_PAGES_BRANCH") ?? Deno.env.get("BRANCH"),
|
49
|
+
fallbackBranch: Deno.env.get("XATA_FALLBACK_BRANCH") ?? getGlobalFallbackBranch()
|
50
|
+
};
|
28
51
|
}
|
29
52
|
} catch (err) {
|
30
53
|
}
|
54
|
+
return {
|
55
|
+
apiKey: getGlobalApiKey(),
|
56
|
+
databaseURL: getGlobalDatabaseURL(),
|
57
|
+
branch: getGlobalBranch(),
|
58
|
+
envBranch: void 0,
|
59
|
+
fallbackBranch: getGlobalFallbackBranch()
|
60
|
+
};
|
61
|
+
}
|
62
|
+
function getGlobalApiKey() {
|
63
|
+
try {
|
64
|
+
return XATA_API_KEY;
|
65
|
+
} catch (err) {
|
66
|
+
return void 0;
|
67
|
+
}
|
68
|
+
}
|
69
|
+
function getGlobalDatabaseURL() {
|
70
|
+
try {
|
71
|
+
return XATA_DATABASE_URL;
|
72
|
+
} catch (err) {
|
73
|
+
return void 0;
|
74
|
+
}
|
75
|
+
}
|
76
|
+
function getGlobalBranch() {
|
77
|
+
try {
|
78
|
+
return XATA_BRANCH;
|
79
|
+
} catch (err) {
|
80
|
+
return void 0;
|
81
|
+
}
|
82
|
+
}
|
83
|
+
function getGlobalFallbackBranch() {
|
84
|
+
try {
|
85
|
+
return XATA_FALLBACK_BRANCH;
|
86
|
+
} catch (err) {
|
87
|
+
return void 0;
|
88
|
+
}
|
31
89
|
}
|
32
90
|
async function getGitBranch() {
|
33
91
|
try {
|
34
|
-
|
92
|
+
if (typeof require === "function") {
|
93
|
+
const req = require;
|
94
|
+
return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
|
95
|
+
}
|
35
96
|
} catch (err) {
|
36
97
|
}
|
37
98
|
try {
|
@@ -49,7 +110,8 @@ async function getGitBranch() {
|
|
49
110
|
|
50
111
|
function getAPIKey() {
|
51
112
|
try {
|
52
|
-
|
113
|
+
const { apiKey } = getEnvironment();
|
114
|
+
return apiKey;
|
53
115
|
} catch (err) {
|
54
116
|
return void 0;
|
55
117
|
}
|
@@ -64,16 +126,28 @@ function getFetchImplementation(userFetch) {
|
|
64
126
|
return fetchImpl;
|
65
127
|
}
|
66
128
|
|
67
|
-
|
68
|
-
|
129
|
+
const VERSION = "0.0.0-alpha.vf481c73";
|
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) {
|
69
138
|
super(getMessage(data));
|
70
139
|
this.status = status;
|
71
140
|
this.errors = isBulkError(data) ? data.errors : void 0;
|
141
|
+
this.requestId = requestId;
|
72
142
|
if (data instanceof Error) {
|
73
143
|
this.stack = data.stack;
|
74
144
|
this.cause = data.cause;
|
75
145
|
}
|
76
146
|
}
|
147
|
+
toString() {
|
148
|
+
const error = super.toString();
|
149
|
+
return `[${this.status}] (${this.requestId ?? "Unknown"}): ${error}`;
|
150
|
+
}
|
77
151
|
}
|
78
152
|
function isBulkError(error) {
|
79
153
|
return isObject(error) && Array.isArray(error.errors);
|
@@ -96,7 +170,12 @@ function getMessage(data) {
|
|
96
170
|
}
|
97
171
|
|
98
172
|
const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
|
99
|
-
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();
|
100
179
|
const queryString = query.length > 0 ? `?${query}` : "";
|
101
180
|
return url.replace(/\{\w*\}/g, (key) => pathParams[key.slice(1, -1)]) + queryString;
|
102
181
|
};
|
@@ -136,6 +215,7 @@ async function fetch$1({
|
|
136
215
|
body: body ? JSON.stringify(body) : void 0,
|
137
216
|
headers: {
|
138
217
|
"Content-Type": "application/json",
|
218
|
+
"User-Agent": `Xata client-ts/${VERSION}`,
|
139
219
|
...headers,
|
140
220
|
...hostHeader(fullUrl),
|
141
221
|
Authorization: `Bearer ${apiKey}`
|
@@ -144,14 +224,15 @@ async function fetch$1({
|
|
144
224
|
if (response.status === 204) {
|
145
225
|
return {};
|
146
226
|
}
|
227
|
+
const requestId = response.headers?.get("x-request-id") ?? void 0;
|
147
228
|
try {
|
148
229
|
const jsonResponse = await response.json();
|
149
230
|
if (response.ok) {
|
150
231
|
return jsonResponse;
|
151
232
|
}
|
152
|
-
throw new FetcherError(response.status, jsonResponse);
|
233
|
+
throw new FetcherError(response.status, jsonResponse, requestId);
|
153
234
|
} catch (error) {
|
154
|
-
throw new FetcherError(response.status, error);
|
235
|
+
throw new FetcherError(response.status, error, requestId);
|
155
236
|
}
|
156
237
|
}
|
157
238
|
|
@@ -245,6 +326,14 @@ const deleteDatabase = (variables) => fetch$1({
|
|
245
326
|
method: "delete",
|
246
327
|
...variables
|
247
328
|
});
|
329
|
+
const getGitBranchesMapping = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "get", ...variables });
|
330
|
+
const addGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "post", ...variables });
|
331
|
+
const removeGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "delete", ...variables });
|
332
|
+
const resolveBranch = (variables) => fetch$1({
|
333
|
+
url: "/dbs/{dbName}/resolveBranch",
|
334
|
+
method: "get",
|
335
|
+
...variables
|
336
|
+
});
|
248
337
|
const getBranchDetails = (variables) => fetch$1({
|
249
338
|
url: "/db/{dbBranchName}",
|
250
339
|
method: "get",
|
@@ -352,6 +441,11 @@ const queryTable = (variables) => fetch$1({
|
|
352
441
|
method: "post",
|
353
442
|
...variables
|
354
443
|
});
|
444
|
+
const searchTable = (variables) => fetch$1({
|
445
|
+
url: "/db/{dbBranchName}/tables/{tableName}/search",
|
446
|
+
method: "post",
|
447
|
+
...variables
|
448
|
+
});
|
355
449
|
const searchBranch = (variables) => fetch$1({
|
356
450
|
url: "/db/{dbBranchName}/search",
|
357
451
|
method: "post",
|
@@ -373,7 +467,15 @@ const operationsByTag = {
|
|
373
467
|
resendWorkspaceMemberInvite,
|
374
468
|
acceptWorkspaceMemberInvite
|
375
469
|
},
|
376
|
-
database: {
|
470
|
+
database: {
|
471
|
+
getDatabaseList,
|
472
|
+
createDatabase,
|
473
|
+
deleteDatabase,
|
474
|
+
getGitBranchesMapping,
|
475
|
+
addGitBranchesEntry,
|
476
|
+
removeGitBranchesEntry,
|
477
|
+
resolveBranch
|
478
|
+
},
|
377
479
|
branch: {
|
378
480
|
getBranchList,
|
379
481
|
getBranchDetails,
|
@@ -407,6 +509,7 @@ const operationsByTag = {
|
|
407
509
|
getRecord,
|
408
510
|
bulkInsertTableRecords,
|
409
511
|
queryTable,
|
512
|
+
searchTable,
|
410
513
|
searchBranch
|
411
514
|
}
|
412
515
|
};
|
@@ -436,35 +539,35 @@ function isValidBuilder(builder) {
|
|
436
539
|
return isObject(builder) && isString(builder.main) && isString(builder.workspaces);
|
437
540
|
}
|
438
541
|
|
439
|
-
var __accessCheck$
|
542
|
+
var __accessCheck$7 = (obj, member, msg) => {
|
440
543
|
if (!member.has(obj))
|
441
544
|
throw TypeError("Cannot " + msg);
|
442
545
|
};
|
443
|
-
var __privateGet$
|
444
|
-
__accessCheck$
|
546
|
+
var __privateGet$7 = (obj, member, getter) => {
|
547
|
+
__accessCheck$7(obj, member, "read from private field");
|
445
548
|
return getter ? getter.call(obj) : member.get(obj);
|
446
549
|
};
|
447
|
-
var __privateAdd$
|
550
|
+
var __privateAdd$7 = (obj, member, value) => {
|
448
551
|
if (member.has(obj))
|
449
552
|
throw TypeError("Cannot add the same private member more than once");
|
450
553
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
451
554
|
};
|
452
|
-
var __privateSet$
|
453
|
-
__accessCheck$
|
555
|
+
var __privateSet$7 = (obj, member, value, setter) => {
|
556
|
+
__accessCheck$7(obj, member, "write to private field");
|
454
557
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
455
558
|
return value;
|
456
559
|
};
|
457
560
|
var _extraProps, _namespaces;
|
458
561
|
class XataApiClient {
|
459
562
|
constructor(options = {}) {
|
460
|
-
__privateAdd$
|
461
|
-
__privateAdd$
|
563
|
+
__privateAdd$7(this, _extraProps, void 0);
|
564
|
+
__privateAdd$7(this, _namespaces, {});
|
462
565
|
const provider = options.host ?? "production";
|
463
566
|
const apiKey = options?.apiKey ?? getAPIKey();
|
464
567
|
if (!apiKey) {
|
465
568
|
throw new Error("Could not resolve a valid apiKey");
|
466
569
|
}
|
467
|
-
__privateSet$
|
570
|
+
__privateSet$7(this, _extraProps, {
|
468
571
|
apiUrl: getHostUrl(provider, "main"),
|
469
572
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
470
573
|
fetchImpl: getFetchImplementation(options.fetch),
|
@@ -472,34 +575,34 @@ class XataApiClient {
|
|
472
575
|
});
|
473
576
|
}
|
474
577
|
get user() {
|
475
|
-
if (!__privateGet$
|
476
|
-
__privateGet$
|
477
|
-
return __privateGet$
|
578
|
+
if (!__privateGet$7(this, _namespaces).user)
|
579
|
+
__privateGet$7(this, _namespaces).user = new UserApi(__privateGet$7(this, _extraProps));
|
580
|
+
return __privateGet$7(this, _namespaces).user;
|
478
581
|
}
|
479
582
|
get workspaces() {
|
480
|
-
if (!__privateGet$
|
481
|
-
__privateGet$
|
482
|
-
return __privateGet$
|
583
|
+
if (!__privateGet$7(this, _namespaces).workspaces)
|
584
|
+
__privateGet$7(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$7(this, _extraProps));
|
585
|
+
return __privateGet$7(this, _namespaces).workspaces;
|
483
586
|
}
|
484
587
|
get databases() {
|
485
|
-
if (!__privateGet$
|
486
|
-
__privateGet$
|
487
|
-
return __privateGet$
|
588
|
+
if (!__privateGet$7(this, _namespaces).databases)
|
589
|
+
__privateGet$7(this, _namespaces).databases = new DatabaseApi(__privateGet$7(this, _extraProps));
|
590
|
+
return __privateGet$7(this, _namespaces).databases;
|
488
591
|
}
|
489
592
|
get branches() {
|
490
|
-
if (!__privateGet$
|
491
|
-
__privateGet$
|
492
|
-
return __privateGet$
|
593
|
+
if (!__privateGet$7(this, _namespaces).branches)
|
594
|
+
__privateGet$7(this, _namespaces).branches = new BranchApi(__privateGet$7(this, _extraProps));
|
595
|
+
return __privateGet$7(this, _namespaces).branches;
|
493
596
|
}
|
494
597
|
get tables() {
|
495
|
-
if (!__privateGet$
|
496
|
-
__privateGet$
|
497
|
-
return __privateGet$
|
598
|
+
if (!__privateGet$7(this, _namespaces).tables)
|
599
|
+
__privateGet$7(this, _namespaces).tables = new TableApi(__privateGet$7(this, _extraProps));
|
600
|
+
return __privateGet$7(this, _namespaces).tables;
|
498
601
|
}
|
499
602
|
get records() {
|
500
|
-
if (!__privateGet$
|
501
|
-
__privateGet$
|
502
|
-
return __privateGet$
|
603
|
+
if (!__privateGet$7(this, _namespaces).records)
|
604
|
+
__privateGet$7(this, _namespaces).records = new RecordsApi(__privateGet$7(this, _extraProps));
|
605
|
+
return __privateGet$7(this, _namespaces).records;
|
503
606
|
}
|
504
607
|
}
|
505
608
|
_extraProps = new WeakMap();
|
@@ -633,6 +736,33 @@ class DatabaseApi {
|
|
633
736
|
...this.extraProps
|
634
737
|
});
|
635
738
|
}
|
739
|
+
getGitBranchesMapping(workspace, dbName) {
|
740
|
+
return operationsByTag.database.getGitBranchesMapping({
|
741
|
+
pathParams: { workspace, dbName },
|
742
|
+
...this.extraProps
|
743
|
+
});
|
744
|
+
}
|
745
|
+
addGitBranchesEntry(workspace, dbName, body) {
|
746
|
+
return operationsByTag.database.addGitBranchesEntry({
|
747
|
+
pathParams: { workspace, dbName },
|
748
|
+
body,
|
749
|
+
...this.extraProps
|
750
|
+
});
|
751
|
+
}
|
752
|
+
removeGitBranchesEntry(workspace, dbName, gitBranch) {
|
753
|
+
return operationsByTag.database.removeGitBranchesEntry({
|
754
|
+
pathParams: { workspace, dbName },
|
755
|
+
queryParams: { gitBranch },
|
756
|
+
...this.extraProps
|
757
|
+
});
|
758
|
+
}
|
759
|
+
resolveBranch(workspace, dbName, gitBranch, fallbackBranch) {
|
760
|
+
return operationsByTag.database.resolveBranch({
|
761
|
+
pathParams: { workspace, dbName },
|
762
|
+
queryParams: { gitBranch, fallbackBranch },
|
763
|
+
...this.extraProps
|
764
|
+
});
|
765
|
+
}
|
636
766
|
}
|
637
767
|
class BranchApi {
|
638
768
|
constructor(extraProps) {
|
@@ -650,10 +780,10 @@ class BranchApi {
|
|
650
780
|
...this.extraProps
|
651
781
|
});
|
652
782
|
}
|
653
|
-
createBranch(workspace, database, branch, from
|
783
|
+
createBranch(workspace, database, branch, from, options = {}) {
|
654
784
|
return operationsByTag.branch.createBranch({
|
655
785
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
656
|
-
queryParams: { from },
|
786
|
+
queryParams: isString(from) ? { from } : void 0,
|
657
787
|
body: options,
|
658
788
|
...this.extraProps
|
659
789
|
});
|
@@ -835,6 +965,13 @@ class RecordsApi {
|
|
835
965
|
...this.extraProps
|
836
966
|
});
|
837
967
|
}
|
968
|
+
searchTable(workspace, database, branch, tableName, query) {
|
969
|
+
return operationsByTag.records.searchTable({
|
970
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
971
|
+
body: query,
|
972
|
+
...this.extraProps
|
973
|
+
});
|
974
|
+
}
|
838
975
|
searchBranch(workspace, database, branch, query) {
|
839
976
|
return operationsByTag.records.searchBranch({
|
840
977
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
@@ -854,43 +991,43 @@ class XataApiPlugin {
|
|
854
991
|
class XataPlugin {
|
855
992
|
}
|
856
993
|
|
857
|
-
var __accessCheck$
|
994
|
+
var __accessCheck$6 = (obj, member, msg) => {
|
858
995
|
if (!member.has(obj))
|
859
996
|
throw TypeError("Cannot " + msg);
|
860
997
|
};
|
861
|
-
var __privateGet$
|
862
|
-
__accessCheck$
|
998
|
+
var __privateGet$6 = (obj, member, getter) => {
|
999
|
+
__accessCheck$6(obj, member, "read from private field");
|
863
1000
|
return getter ? getter.call(obj) : member.get(obj);
|
864
1001
|
};
|
865
|
-
var __privateAdd$
|
1002
|
+
var __privateAdd$6 = (obj, member, value) => {
|
866
1003
|
if (member.has(obj))
|
867
1004
|
throw TypeError("Cannot add the same private member more than once");
|
868
1005
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
869
1006
|
};
|
870
|
-
var __privateSet$
|
871
|
-
__accessCheck$
|
1007
|
+
var __privateSet$6 = (obj, member, value, setter) => {
|
1008
|
+
__accessCheck$6(obj, member, "write to private field");
|
872
1009
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
873
1010
|
return value;
|
874
1011
|
};
|
875
|
-
var _query;
|
1012
|
+
var _query, _page;
|
876
1013
|
class Page {
|
877
1014
|
constructor(query, meta, records = []) {
|
878
|
-
__privateAdd$
|
879
|
-
__privateSet$
|
1015
|
+
__privateAdd$6(this, _query, void 0);
|
1016
|
+
__privateSet$6(this, _query, query);
|
880
1017
|
this.meta = meta;
|
881
|
-
this.records = records;
|
1018
|
+
this.records = new RecordArray(this, records);
|
882
1019
|
}
|
883
1020
|
async nextPage(size, offset) {
|
884
|
-
return __privateGet$
|
1021
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
|
885
1022
|
}
|
886
1023
|
async previousPage(size, offset) {
|
887
|
-
return __privateGet$
|
1024
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, before: this.meta.page.cursor } });
|
888
1025
|
}
|
889
1026
|
async firstPage(size, offset) {
|
890
|
-
return __privateGet$
|
1027
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, first: this.meta.page.cursor } });
|
891
1028
|
}
|
892
1029
|
async lastPage(size, offset) {
|
893
|
-
return __privateGet$
|
1030
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, last: this.meta.page.cursor } });
|
894
1031
|
}
|
895
1032
|
hasNextPage() {
|
896
1033
|
return this.meta.page.more;
|
@@ -898,50 +1035,93 @@ class Page {
|
|
898
1035
|
}
|
899
1036
|
_query = new WeakMap();
|
900
1037
|
const PAGINATION_MAX_SIZE = 200;
|
901
|
-
const PAGINATION_DEFAULT_SIZE =
|
1038
|
+
const PAGINATION_DEFAULT_SIZE = 20;
|
902
1039
|
const PAGINATION_MAX_OFFSET = 800;
|
903
1040
|
const PAGINATION_DEFAULT_OFFSET = 0;
|
1041
|
+
function isCursorPaginationOptions(options) {
|
1042
|
+
return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
|
1043
|
+
}
|
1044
|
+
const _RecordArray = class extends Array {
|
1045
|
+
constructor(page, overrideRecords) {
|
1046
|
+
super(..._RecordArray.parseConstructorParams(page, overrideRecords));
|
1047
|
+
__privateAdd$6(this, _page, void 0);
|
1048
|
+
__privateSet$6(this, _page, page);
|
1049
|
+
}
|
1050
|
+
static parseConstructorParams(...args) {
|
1051
|
+
if (args.length === 1 && typeof args[0] === "number") {
|
1052
|
+
return new Array(args[0]);
|
1053
|
+
}
|
1054
|
+
if (args.length <= 2 && isObject(args[0]?.meta) && Array.isArray(args[1] ?? [])) {
|
1055
|
+
const result = args[1] ?? args[0].records ?? [];
|
1056
|
+
return new Array(...result);
|
1057
|
+
}
|
1058
|
+
return new Array(...args);
|
1059
|
+
}
|
1060
|
+
async nextPage(size, offset) {
|
1061
|
+
const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
|
1062
|
+
return new _RecordArray(newPage);
|
1063
|
+
}
|
1064
|
+
async previousPage(size, offset) {
|
1065
|
+
const newPage = await __privateGet$6(this, _page).previousPage(size, offset);
|
1066
|
+
return new _RecordArray(newPage);
|
1067
|
+
}
|
1068
|
+
async firstPage(size, offset) {
|
1069
|
+
const newPage = await __privateGet$6(this, _page).firstPage(size, offset);
|
1070
|
+
return new _RecordArray(newPage);
|
1071
|
+
}
|
1072
|
+
async lastPage(size, offset) {
|
1073
|
+
const newPage = await __privateGet$6(this, _page).lastPage(size, offset);
|
1074
|
+
return new _RecordArray(newPage);
|
1075
|
+
}
|
1076
|
+
hasNextPage() {
|
1077
|
+
return __privateGet$6(this, _page).meta.page.more;
|
1078
|
+
}
|
1079
|
+
};
|
1080
|
+
let RecordArray = _RecordArray;
|
1081
|
+
_page = new WeakMap();
|
904
1082
|
|
905
|
-
var __accessCheck$
|
1083
|
+
var __accessCheck$5 = (obj, member, msg) => {
|
906
1084
|
if (!member.has(obj))
|
907
1085
|
throw TypeError("Cannot " + msg);
|
908
1086
|
};
|
909
|
-
var __privateGet$
|
910
|
-
__accessCheck$
|
1087
|
+
var __privateGet$5 = (obj, member, getter) => {
|
1088
|
+
__accessCheck$5(obj, member, "read from private field");
|
911
1089
|
return getter ? getter.call(obj) : member.get(obj);
|
912
1090
|
};
|
913
|
-
var __privateAdd$
|
1091
|
+
var __privateAdd$5 = (obj, member, value) => {
|
914
1092
|
if (member.has(obj))
|
915
1093
|
throw TypeError("Cannot add the same private member more than once");
|
916
1094
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
917
1095
|
};
|
918
|
-
var __privateSet$
|
919
|
-
__accessCheck$
|
1096
|
+
var __privateSet$5 = (obj, member, value, setter) => {
|
1097
|
+
__accessCheck$5(obj, member, "write to private field");
|
920
1098
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
921
1099
|
return value;
|
922
1100
|
};
|
923
1101
|
var _table$1, _repository, _data;
|
924
1102
|
const _Query = class {
|
925
|
-
constructor(repository, table, data,
|
926
|
-
__privateAdd$
|
927
|
-
__privateAdd$
|
928
|
-
__privateAdd$
|
1103
|
+
constructor(repository, table, data, rawParent) {
|
1104
|
+
__privateAdd$5(this, _table$1, void 0);
|
1105
|
+
__privateAdd$5(this, _repository, void 0);
|
1106
|
+
__privateAdd$5(this, _data, { filter: {} });
|
929
1107
|
this.meta = { page: { cursor: "start", more: true } };
|
930
|
-
this.records = [];
|
931
|
-
__privateSet$
|
1108
|
+
this.records = new RecordArray(this, []);
|
1109
|
+
__privateSet$5(this, _table$1, table);
|
932
1110
|
if (repository) {
|
933
|
-
__privateSet$
|
1111
|
+
__privateSet$5(this, _repository, repository);
|
934
1112
|
} else {
|
935
|
-
__privateSet$
|
1113
|
+
__privateSet$5(this, _repository, this);
|
936
1114
|
}
|
937
|
-
|
938
|
-
__privateGet$
|
939
|
-
__privateGet$
|
940
|
-
__privateGet$
|
941
|
-
__privateGet$
|
942
|
-
__privateGet$
|
943
|
-
__privateGet$
|
944
|
-
__privateGet$
|
1115
|
+
const parent = cleanParent(data, rawParent);
|
1116
|
+
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
1117
|
+
__privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
|
1118
|
+
__privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
|
1119
|
+
__privateGet$5(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
|
1120
|
+
__privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
|
1121
|
+
__privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
|
1122
|
+
__privateGet$5(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
|
1123
|
+
__privateGet$5(this, _data).pagination = data.pagination ?? parent?.pagination;
|
1124
|
+
__privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
|
945
1125
|
this.any = this.any.bind(this);
|
946
1126
|
this.all = this.all.bind(this);
|
947
1127
|
this.not = this.not.bind(this);
|
@@ -952,75 +1132,88 @@ const _Query = class {
|
|
952
1132
|
Object.defineProperty(this, "repository", { enumerable: false });
|
953
1133
|
}
|
954
1134
|
getQueryOptions() {
|
955
|
-
return __privateGet$
|
1135
|
+
return __privateGet$5(this, _data);
|
1136
|
+
}
|
1137
|
+
key() {
|
1138
|
+
const { columns = [], filter = {}, sort = [], pagination = {} } = __privateGet$5(this, _data);
|
1139
|
+
const key = JSON.stringify({ columns, filter, sort, pagination });
|
1140
|
+
return toBase64(key);
|
956
1141
|
}
|
957
1142
|
any(...queries) {
|
958
1143
|
const $any = queries.map((query) => query.getQueryOptions().filter ?? {});
|
959
|
-
return new _Query(__privateGet$
|
1144
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $any } }, __privateGet$5(this, _data));
|
960
1145
|
}
|
961
1146
|
all(...queries) {
|
962
1147
|
const $all = queries.map((query) => query.getQueryOptions().filter ?? {});
|
963
|
-
return new _Query(__privateGet$
|
1148
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
964
1149
|
}
|
965
1150
|
not(...queries) {
|
966
1151
|
const $not = queries.map((query) => query.getQueryOptions().filter ?? {});
|
967
|
-
return new _Query(__privateGet$
|
1152
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $not } }, __privateGet$5(this, _data));
|
968
1153
|
}
|
969
1154
|
none(...queries) {
|
970
1155
|
const $none = queries.map((query) => query.getQueryOptions().filter ?? {});
|
971
|
-
return new _Query(__privateGet$
|
1156
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $none } }, __privateGet$5(this, _data));
|
972
1157
|
}
|
973
1158
|
filter(a, b) {
|
974
1159
|
if (arguments.length === 1) {
|
975
1160
|
const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
|
976
|
-
const $all = compact([__privateGet$
|
977
|
-
return new _Query(__privateGet$
|
1161
|
+
const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat(constraints));
|
1162
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
978
1163
|
} else {
|
979
|
-
const $all = compact([__privateGet$
|
980
|
-
return new _Query(__privateGet$
|
1164
|
+
const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
|
1165
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
981
1166
|
}
|
982
1167
|
}
|
983
1168
|
sort(column, direction) {
|
984
|
-
const originalSort = [__privateGet$
|
1169
|
+
const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
|
985
1170
|
const sort = [...originalSort, { column, direction }];
|
986
|
-
return new _Query(__privateGet$
|
1171
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
|
987
1172
|
}
|
988
1173
|
select(columns) {
|
989
|
-
return new _Query(__privateGet$
|
1174
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { columns }, __privateGet$5(this, _data));
|
990
1175
|
}
|
991
1176
|
getPaginated(options = {}) {
|
992
|
-
const query = new _Query(__privateGet$
|
993
|
-
return __privateGet$
|
1177
|
+
const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
|
1178
|
+
return __privateGet$5(this, _repository).query(query);
|
994
1179
|
}
|
995
1180
|
async *[Symbol.asyncIterator]() {
|
996
|
-
for await (const [record] of this.getIterator(1)) {
|
1181
|
+
for await (const [record] of this.getIterator({ batchSize: 1 })) {
|
997
1182
|
yield record;
|
998
1183
|
}
|
999
1184
|
}
|
1000
|
-
async *getIterator(
|
1001
|
-
|
1002
|
-
let
|
1003
|
-
|
1004
|
-
|
1005
|
-
|
1006
|
-
|
1007
|
-
|
1185
|
+
async *getIterator(options = {}) {
|
1186
|
+
const { batchSize = 1 } = options;
|
1187
|
+
let page = await this.getPaginated({ ...options, pagination: { size: batchSize, offset: 0 } });
|
1188
|
+
let more = page.hasNextPage();
|
1189
|
+
yield page.records;
|
1190
|
+
while (more) {
|
1191
|
+
page = await page.nextPage();
|
1192
|
+
more = page.hasNextPage();
|
1193
|
+
yield page.records;
|
1008
1194
|
}
|
1009
1195
|
}
|
1010
1196
|
async getMany(options = {}) {
|
1011
|
-
const
|
1012
|
-
|
1197
|
+
const page = await this.getPaginated(options);
|
1198
|
+
if (page.hasNextPage() && options.pagination?.size === void 0) {
|
1199
|
+
console.trace("Calling getMany does not return all results. Paginate to get all results or call getAll.");
|
1200
|
+
}
|
1201
|
+
return page.records;
|
1013
1202
|
}
|
1014
|
-
async getAll(
|
1203
|
+
async getAll(options = {}) {
|
1204
|
+
const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
|
1015
1205
|
const results = [];
|
1016
|
-
for await (const page of this.getIterator(
|
1206
|
+
for await (const page of this.getIterator({ ...rest, batchSize })) {
|
1017
1207
|
results.push(...page);
|
1018
1208
|
}
|
1019
1209
|
return results;
|
1020
1210
|
}
|
1021
|
-
async
|
1022
|
-
const records = await this.getMany({ ...options,
|
1023
|
-
return records[0]
|
1211
|
+
async getFirst(options = {}) {
|
1212
|
+
const records = await this.getMany({ ...options, pagination: { size: 1 } });
|
1213
|
+
return records[0] ?? null;
|
1214
|
+
}
|
1215
|
+
cache(ttl) {
|
1216
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
|
1024
1217
|
}
|
1025
1218
|
nextPage(size, offset) {
|
1026
1219
|
return this.firstPage(size, offset);
|
@@ -1029,10 +1222,10 @@ const _Query = class {
|
|
1029
1222
|
return this.firstPage(size, offset);
|
1030
1223
|
}
|
1031
1224
|
firstPage(size, offset) {
|
1032
|
-
return this.getPaginated({
|
1225
|
+
return this.getPaginated({ pagination: { size, offset } });
|
1033
1226
|
}
|
1034
1227
|
lastPage(size, offset) {
|
1035
|
-
return this.getPaginated({
|
1228
|
+
return this.getPaginated({ pagination: { size, offset, before: "end" } });
|
1036
1229
|
}
|
1037
1230
|
hasNextPage() {
|
1038
1231
|
return this.meta.page.more;
|
@@ -1042,12 +1235,20 @@ let Query = _Query;
|
|
1042
1235
|
_table$1 = new WeakMap();
|
1043
1236
|
_repository = new WeakMap();
|
1044
1237
|
_data = new WeakMap();
|
1238
|
+
function cleanParent(data, parent) {
|
1239
|
+
if (isCursorPaginationOptions(data.pagination)) {
|
1240
|
+
return { ...parent, sorting: void 0, filter: void 0 };
|
1241
|
+
}
|
1242
|
+
return parent;
|
1243
|
+
}
|
1045
1244
|
|
1046
1245
|
function isIdentifiable(x) {
|
1047
1246
|
return isObject(x) && isString(x?.id);
|
1048
1247
|
}
|
1049
1248
|
function isXataRecord(x) {
|
1050
|
-
|
1249
|
+
const record = x;
|
1250
|
+
const metadata = record?.getMetadata();
|
1251
|
+
return isIdentifiable(x) && isObject(metadata) && typeof metadata.version === "number";
|
1051
1252
|
}
|
1052
1253
|
|
1053
1254
|
function isSortFilterString(value) {
|
@@ -1073,169 +1274,232 @@ function buildSortFilter(filter) {
|
|
1073
1274
|
}
|
1074
1275
|
}
|
1075
1276
|
|
1076
|
-
var __accessCheck$
|
1277
|
+
var __accessCheck$4 = (obj, member, msg) => {
|
1077
1278
|
if (!member.has(obj))
|
1078
1279
|
throw TypeError("Cannot " + msg);
|
1079
1280
|
};
|
1080
|
-
var __privateGet$
|
1081
|
-
__accessCheck$
|
1281
|
+
var __privateGet$4 = (obj, member, getter) => {
|
1282
|
+
__accessCheck$4(obj, member, "read from private field");
|
1082
1283
|
return getter ? getter.call(obj) : member.get(obj);
|
1083
1284
|
};
|
1084
|
-
var __privateAdd$
|
1285
|
+
var __privateAdd$4 = (obj, member, value) => {
|
1085
1286
|
if (member.has(obj))
|
1086
1287
|
throw TypeError("Cannot add the same private member more than once");
|
1087
1288
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1088
1289
|
};
|
1089
|
-
var __privateSet$
|
1090
|
-
__accessCheck$
|
1290
|
+
var __privateSet$4 = (obj, member, value, setter) => {
|
1291
|
+
__accessCheck$4(obj, member, "write to private field");
|
1091
1292
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1092
1293
|
return value;
|
1093
1294
|
};
|
1094
1295
|
var __privateMethod$2 = (obj, member, method) => {
|
1095
|
-
__accessCheck$
|
1296
|
+
__accessCheck$4(obj, member, "access private method");
|
1096
1297
|
return method;
|
1097
1298
|
};
|
1098
|
-
var _table,
|
1299
|
+
var _table, _getFetchProps, _cache, _schemaTables$2, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _bulkInsertTableRecords, bulkInsertTableRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn, _invalidateCache, invalidateCache_fn, _setCacheRecord, setCacheRecord_fn, _getCacheRecord, getCacheRecord_fn, _setCacheQuery, setCacheQuery_fn, _getCacheQuery, getCacheQuery_fn, _getSchemaTables$1, getSchemaTables_fn$1;
|
1099
1300
|
class Repository extends Query {
|
1100
1301
|
}
|
1101
1302
|
class RestRepository extends Query {
|
1102
1303
|
constructor(options) {
|
1103
1304
|
super(null, options.table, {});
|
1104
|
-
__privateAdd$
|
1105
|
-
__privateAdd$
|
1106
|
-
__privateAdd$
|
1107
|
-
__privateAdd$
|
1108
|
-
__privateAdd$
|
1109
|
-
__privateAdd$
|
1110
|
-
__privateAdd$
|
1111
|
-
__privateAdd$
|
1112
|
-
__privateAdd$
|
1113
|
-
|
1114
|
-
|
1115
|
-
|
1305
|
+
__privateAdd$4(this, _insertRecordWithoutId);
|
1306
|
+
__privateAdd$4(this, _insertRecordWithId);
|
1307
|
+
__privateAdd$4(this, _bulkInsertTableRecords);
|
1308
|
+
__privateAdd$4(this, _updateRecordWithID);
|
1309
|
+
__privateAdd$4(this, _upsertRecordWithID);
|
1310
|
+
__privateAdd$4(this, _deleteRecord);
|
1311
|
+
__privateAdd$4(this, _invalidateCache);
|
1312
|
+
__privateAdd$4(this, _setCacheRecord);
|
1313
|
+
__privateAdd$4(this, _getCacheRecord);
|
1314
|
+
__privateAdd$4(this, _setCacheQuery);
|
1315
|
+
__privateAdd$4(this, _getCacheQuery);
|
1316
|
+
__privateAdd$4(this, _getSchemaTables$1);
|
1317
|
+
__privateAdd$4(this, _table, void 0);
|
1318
|
+
__privateAdd$4(this, _getFetchProps, void 0);
|
1319
|
+
__privateAdd$4(this, _cache, void 0);
|
1320
|
+
__privateAdd$4(this, _schemaTables$2, void 0);
|
1321
|
+
__privateSet$4(this, _table, options.table);
|
1322
|
+
__privateSet$4(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
1116
1323
|
this.db = options.db;
|
1324
|
+
__privateSet$4(this, _cache, options.pluginOptions.cache);
|
1325
|
+
__privateSet$4(this, _schemaTables$2, options.schemaTables);
|
1117
1326
|
}
|
1118
1327
|
async create(a, b) {
|
1119
1328
|
if (Array.isArray(a)) {
|
1120
|
-
|
1329
|
+
if (a.length === 0)
|
1330
|
+
return [];
|
1331
|
+
const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
|
1332
|
+
await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
|
1333
|
+
return records;
|
1121
1334
|
}
|
1122
1335
|
if (isString(a) && isObject(b)) {
|
1123
1336
|
if (a === "")
|
1124
1337
|
throw new Error("The id can't be empty");
|
1125
|
-
|
1338
|
+
const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b);
|
1339
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1340
|
+
return record;
|
1126
1341
|
}
|
1127
1342
|
if (isObject(a) && isString(a.id)) {
|
1128
1343
|
if (a.id === "")
|
1129
1344
|
throw new Error("The id can't be empty");
|
1130
|
-
|
1345
|
+
const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 });
|
1346
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1347
|
+
return record;
|
1131
1348
|
}
|
1132
1349
|
if (isObject(a)) {
|
1133
|
-
|
1350
|
+
const record = await __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a);
|
1351
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1352
|
+
return record;
|
1134
1353
|
}
|
1135
1354
|
throw new Error("Invalid arguments for create method");
|
1136
1355
|
}
|
1137
|
-
async read(
|
1138
|
-
|
1139
|
-
|
1140
|
-
|
1141
|
-
|
1142
|
-
|
1143
|
-
|
1144
|
-
|
1145
|
-
|
1146
|
-
|
1147
|
-
|
1356
|
+
async read(a) {
|
1357
|
+
if (Array.isArray(a)) {
|
1358
|
+
if (a.length === 0)
|
1359
|
+
return [];
|
1360
|
+
const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
|
1361
|
+
return this.getAll({ filter: { id: { $any: ids } } });
|
1362
|
+
}
|
1363
|
+
const id = isString(a) ? a : a.id;
|
1364
|
+
if (isString(id)) {
|
1365
|
+
const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, id);
|
1366
|
+
if (cacheRecord)
|
1367
|
+
return cacheRecord;
|
1368
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1369
|
+
try {
|
1370
|
+
const response = await getRecord({
|
1371
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
|
1372
|
+
...fetchProps
|
1373
|
+
});
|
1374
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1375
|
+
return initObject(this.db, schemaTables, __privateGet$4(this, _table), response);
|
1376
|
+
} catch (e) {
|
1377
|
+
if (isObject(e) && e.status === 404) {
|
1378
|
+
return null;
|
1379
|
+
}
|
1380
|
+
throw e;
|
1148
1381
|
}
|
1149
|
-
throw e;
|
1150
1382
|
}
|
1151
1383
|
}
|
1152
1384
|
async update(a, b) {
|
1153
1385
|
if (Array.isArray(a)) {
|
1386
|
+
if (a.length === 0)
|
1387
|
+
return [];
|
1154
1388
|
if (a.length > 100) {
|
1155
1389
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1156
1390
|
}
|
1157
1391
|
return Promise.all(a.map((object) => this.update(object)));
|
1158
1392
|
}
|
1159
1393
|
if (isString(a) && isObject(b)) {
|
1160
|
-
|
1394
|
+
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
|
1395
|
+
const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b);
|
1396
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1397
|
+
return record;
|
1161
1398
|
}
|
1162
1399
|
if (isObject(a) && isString(a.id)) {
|
1163
|
-
|
1400
|
+
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
|
1401
|
+
const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
|
1402
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1403
|
+
return record;
|
1164
1404
|
}
|
1165
1405
|
throw new Error("Invalid arguments for update method");
|
1166
1406
|
}
|
1167
1407
|
async createOrUpdate(a, b) {
|
1168
1408
|
if (Array.isArray(a)) {
|
1409
|
+
if (a.length === 0)
|
1410
|
+
return [];
|
1169
1411
|
if (a.length > 100) {
|
1170
1412
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1171
1413
|
}
|
1172
1414
|
return Promise.all(a.map((object) => this.createOrUpdate(object)));
|
1173
1415
|
}
|
1174
1416
|
if (isString(a) && isObject(b)) {
|
1175
|
-
|
1417
|
+
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
|
1418
|
+
const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b);
|
1419
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1420
|
+
return record;
|
1176
1421
|
}
|
1177
1422
|
if (isObject(a) && isString(a.id)) {
|
1178
|
-
|
1423
|
+
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
|
1424
|
+
const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
|
1425
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1426
|
+
return record;
|
1179
1427
|
}
|
1180
1428
|
throw new Error("Invalid arguments for createOrUpdate method");
|
1181
1429
|
}
|
1182
|
-
async delete(
|
1183
|
-
if (Array.isArray(
|
1184
|
-
if (
|
1430
|
+
async delete(a) {
|
1431
|
+
if (Array.isArray(a)) {
|
1432
|
+
if (a.length === 0)
|
1433
|
+
return;
|
1434
|
+
if (a.length > 100) {
|
1185
1435
|
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
1186
1436
|
}
|
1187
|
-
await Promise.all(
|
1437
|
+
await Promise.all(a.map((id) => this.delete(id)));
|
1188
1438
|
return;
|
1189
1439
|
}
|
1190
|
-
if (isString(
|
1191
|
-
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this,
|
1440
|
+
if (isString(a)) {
|
1441
|
+
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a);
|
1442
|
+
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
|
1192
1443
|
return;
|
1193
1444
|
}
|
1194
|
-
if (isObject(
|
1195
|
-
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this,
|
1445
|
+
if (isObject(a) && isString(a.id)) {
|
1446
|
+
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id);
|
1447
|
+
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
|
1196
1448
|
return;
|
1197
1449
|
}
|
1198
1450
|
throw new Error("Invalid arguments for delete method");
|
1199
1451
|
}
|
1200
1452
|
async search(query, options = {}) {
|
1201
|
-
const fetchProps = await __privateGet$
|
1202
|
-
const { records } = await
|
1203
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1204
|
-
body: {
|
1453
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1454
|
+
const { records } = await searchTable({
|
1455
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1456
|
+
body: {
|
1457
|
+
query,
|
1458
|
+
fuzziness: options.fuzziness,
|
1459
|
+
highlight: options.highlight,
|
1460
|
+
filter: options.filter
|
1461
|
+
},
|
1205
1462
|
...fetchProps
|
1206
1463
|
});
|
1207
|
-
|
1464
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1465
|
+
return records.map((item) => initObject(this.db, schemaTables, __privateGet$4(this, _table), item));
|
1208
1466
|
}
|
1209
1467
|
async query(query) {
|
1468
|
+
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
1469
|
+
if (cacheQuery)
|
1470
|
+
return new Page(query, cacheQuery.meta, cacheQuery.records);
|
1210
1471
|
const data = query.getQueryOptions();
|
1211
1472
|
const body = {
|
1212
1473
|
filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
|
1213
|
-
sort: data.sort ? buildSortFilter(data.sort) : void 0,
|
1214
|
-
page: data.
|
1474
|
+
sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
|
1475
|
+
page: data.pagination,
|
1215
1476
|
columns: data.columns
|
1216
1477
|
};
|
1217
|
-
const fetchProps = await __privateGet$
|
1478
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1218
1479
|
const { meta, records: objects } = await queryTable({
|
1219
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1480
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1220
1481
|
body,
|
1221
1482
|
...fetchProps
|
1222
1483
|
});
|
1223
|
-
const
|
1484
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1485
|
+
const records = objects.map((record) => initObject(this.db, schemaTables, __privateGet$4(this, _table), record));
|
1486
|
+
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1224
1487
|
return new Page(query, meta, records);
|
1225
1488
|
}
|
1226
1489
|
}
|
1227
1490
|
_table = new WeakMap();
|
1228
|
-
_links = new WeakMap();
|
1229
1491
|
_getFetchProps = new WeakMap();
|
1492
|
+
_cache = new WeakMap();
|
1493
|
+
_schemaTables$2 = new WeakMap();
|
1230
1494
|
_insertRecordWithoutId = new WeakSet();
|
1231
1495
|
insertRecordWithoutId_fn = async function(object) {
|
1232
|
-
const fetchProps = await __privateGet$
|
1496
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1233
1497
|
const record = transformObjectLinks(object);
|
1234
1498
|
const response = await insertRecord({
|
1235
1499
|
pathParams: {
|
1236
1500
|
workspace: "{workspaceId}",
|
1237
1501
|
dbBranchName: "{dbBranch}",
|
1238
|
-
tableName: __privateGet$
|
1502
|
+
tableName: __privateGet$4(this, _table)
|
1239
1503
|
},
|
1240
1504
|
body: record,
|
1241
1505
|
...fetchProps
|
@@ -1248,13 +1512,13 @@ insertRecordWithoutId_fn = async function(object) {
|
|
1248
1512
|
};
|
1249
1513
|
_insertRecordWithId = new WeakSet();
|
1250
1514
|
insertRecordWithId_fn = async function(recordId, object) {
|
1251
|
-
const fetchProps = await __privateGet$
|
1515
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1252
1516
|
const record = transformObjectLinks(object);
|
1253
1517
|
const response = await insertRecordWithID({
|
1254
1518
|
pathParams: {
|
1255
1519
|
workspace: "{workspaceId}",
|
1256
1520
|
dbBranchName: "{dbBranch}",
|
1257
|
-
tableName: __privateGet$
|
1521
|
+
tableName: __privateGet$4(this, _table),
|
1258
1522
|
recordId
|
1259
1523
|
},
|
1260
1524
|
body: record,
|
@@ -1269,25 +1533,29 @@ insertRecordWithId_fn = async function(recordId, object) {
|
|
1269
1533
|
};
|
1270
1534
|
_bulkInsertTableRecords = new WeakSet();
|
1271
1535
|
bulkInsertTableRecords_fn = async function(objects) {
|
1272
|
-
const fetchProps = await __privateGet$
|
1536
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1273
1537
|
const records = objects.map((object) => transformObjectLinks(object));
|
1274
|
-
const
|
1275
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1538
|
+
const { recordIDs } = await bulkInsertTableRecords({
|
1539
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1276
1540
|
body: { records },
|
1277
1541
|
...fetchProps
|
1278
1542
|
});
|
1279
|
-
const finalObjects = await this.
|
1543
|
+
const finalObjects = await this.read(recordIDs);
|
1280
1544
|
if (finalObjects.length !== objects.length) {
|
1281
1545
|
throw new Error("The server failed to save some records");
|
1282
1546
|
}
|
1283
|
-
|
1547
|
+
const dictionary = finalObjects.reduce((acc, object) => {
|
1548
|
+
acc[object.id] = object;
|
1549
|
+
return acc;
|
1550
|
+
}, {});
|
1551
|
+
return recordIDs.map((id) => dictionary[id]);
|
1284
1552
|
};
|
1285
1553
|
_updateRecordWithID = new WeakSet();
|
1286
1554
|
updateRecordWithID_fn = async function(recordId, object) {
|
1287
|
-
const fetchProps = await __privateGet$
|
1555
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1288
1556
|
const record = transformObjectLinks(object);
|
1289
1557
|
const response = await updateRecordWithID({
|
1290
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1558
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1291
1559
|
body: record,
|
1292
1560
|
...fetchProps
|
1293
1561
|
});
|
@@ -1298,9 +1566,9 @@ updateRecordWithID_fn = async function(recordId, object) {
|
|
1298
1566
|
};
|
1299
1567
|
_upsertRecordWithID = new WeakSet();
|
1300
1568
|
upsertRecordWithID_fn = async function(recordId, object) {
|
1301
|
-
const fetchProps = await __privateGet$
|
1569
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1302
1570
|
const response = await upsertRecordWithID({
|
1303
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1571
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1304
1572
|
body: object,
|
1305
1573
|
...fetchProps
|
1306
1574
|
});
|
@@ -1311,12 +1579,63 @@ upsertRecordWithID_fn = async function(recordId, object) {
|
|
1311
1579
|
};
|
1312
1580
|
_deleteRecord = new WeakSet();
|
1313
1581
|
deleteRecord_fn = async function(recordId) {
|
1314
|
-
const fetchProps = await __privateGet$
|
1582
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1315
1583
|
await deleteRecord({
|
1316
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1584
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1317
1585
|
...fetchProps
|
1318
1586
|
});
|
1319
1587
|
};
|
1588
|
+
_invalidateCache = new WeakSet();
|
1589
|
+
invalidateCache_fn = async function(recordId) {
|
1590
|
+
await __privateGet$4(this, _cache).delete(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1591
|
+
const cacheItems = await __privateGet$4(this, _cache).getAll();
|
1592
|
+
const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
|
1593
|
+
for (const [key, value] of queries) {
|
1594
|
+
const ids = getIds(value);
|
1595
|
+
if (ids.includes(recordId))
|
1596
|
+
await __privateGet$4(this, _cache).delete(key);
|
1597
|
+
}
|
1598
|
+
};
|
1599
|
+
_setCacheRecord = new WeakSet();
|
1600
|
+
setCacheRecord_fn = async function(record) {
|
1601
|
+
if (!__privateGet$4(this, _cache).cacheRecords)
|
1602
|
+
return;
|
1603
|
+
await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
|
1604
|
+
};
|
1605
|
+
_getCacheRecord = new WeakSet();
|
1606
|
+
getCacheRecord_fn = async function(recordId) {
|
1607
|
+
if (!__privateGet$4(this, _cache).cacheRecords)
|
1608
|
+
return null;
|
1609
|
+
return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1610
|
+
};
|
1611
|
+
_setCacheQuery = new WeakSet();
|
1612
|
+
setCacheQuery_fn = async function(query, meta, records) {
|
1613
|
+
await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
|
1614
|
+
};
|
1615
|
+
_getCacheQuery = new WeakSet();
|
1616
|
+
getCacheQuery_fn = async function(query) {
|
1617
|
+
const key = `query_${__privateGet$4(this, _table)}:${query.key()}`;
|
1618
|
+
const result = await __privateGet$4(this, _cache).get(key);
|
1619
|
+
if (!result)
|
1620
|
+
return null;
|
1621
|
+
const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
|
1622
|
+
if (ttl < 0)
|
1623
|
+
return null;
|
1624
|
+
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1625
|
+
return hasExpired ? null : result;
|
1626
|
+
};
|
1627
|
+
_getSchemaTables$1 = new WeakSet();
|
1628
|
+
getSchemaTables_fn$1 = async function() {
|
1629
|
+
if (__privateGet$4(this, _schemaTables$2))
|
1630
|
+
return __privateGet$4(this, _schemaTables$2);
|
1631
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1632
|
+
const { schema } = await getBranchDetails({
|
1633
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1634
|
+
...fetchProps
|
1635
|
+
});
|
1636
|
+
__privateSet$4(this, _schemaTables$2, schema.tables);
|
1637
|
+
return schema.tables;
|
1638
|
+
};
|
1320
1639
|
const transformObjectLinks = (object) => {
|
1321
1640
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
1322
1641
|
if (key === "xata")
|
@@ -1324,15 +1643,34 @@ const transformObjectLinks = (object) => {
|
|
1324
1643
|
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1325
1644
|
}, {});
|
1326
1645
|
};
|
1327
|
-
const initObject = (db,
|
1646
|
+
const initObject = (db, schemaTables, table, object) => {
|
1328
1647
|
const result = {};
|
1329
|
-
|
1330
|
-
|
1331
|
-
|
1332
|
-
|
1333
|
-
|
1334
|
-
|
1335
|
-
|
1648
|
+
const { xata, ...rest } = object ?? {};
|
1649
|
+
Object.assign(result, rest);
|
1650
|
+
const { columns } = schemaTables.find(({ name }) => name === table) ?? {};
|
1651
|
+
if (!columns)
|
1652
|
+
console.error(`Table ${table} not found in schema`);
|
1653
|
+
for (const column of columns ?? []) {
|
1654
|
+
const value = result[column.name];
|
1655
|
+
switch (column.type) {
|
1656
|
+
case "datetime": {
|
1657
|
+
const date = value !== void 0 ? new Date(value) : void 0;
|
1658
|
+
if (date && isNaN(date.getTime())) {
|
1659
|
+
console.error(`Failed to parse date ${value} for field ${column.name}`);
|
1660
|
+
} else if (date) {
|
1661
|
+
result[column.name] = date;
|
1662
|
+
}
|
1663
|
+
break;
|
1664
|
+
}
|
1665
|
+
case "link": {
|
1666
|
+
const linkTable = column.link?.table;
|
1667
|
+
if (!linkTable) {
|
1668
|
+
console.error(`Failed to parse link for field ${column.name}`);
|
1669
|
+
} else if (isObject(value)) {
|
1670
|
+
result[column.name] = initObject(db, schemaTables, linkTable, value);
|
1671
|
+
}
|
1672
|
+
break;
|
1673
|
+
}
|
1336
1674
|
}
|
1337
1675
|
}
|
1338
1676
|
result.read = function() {
|
@@ -1344,12 +1682,74 @@ const initObject = (db, links, table, object) => {
|
|
1344
1682
|
result.delete = function() {
|
1345
1683
|
return db[table].delete(result["id"]);
|
1346
1684
|
};
|
1347
|
-
|
1685
|
+
result.getMetadata = function() {
|
1686
|
+
return xata;
|
1687
|
+
};
|
1688
|
+
for (const prop of ["read", "update", "delete", "getMetadata"]) {
|
1348
1689
|
Object.defineProperty(result, prop, { enumerable: false });
|
1349
1690
|
}
|
1350
1691
|
Object.freeze(result);
|
1351
1692
|
return result;
|
1352
1693
|
};
|
1694
|
+
function getIds(value) {
|
1695
|
+
if (Array.isArray(value)) {
|
1696
|
+
return value.map((item) => getIds(item)).flat();
|
1697
|
+
}
|
1698
|
+
if (!isObject(value))
|
1699
|
+
return [];
|
1700
|
+
const nestedIds = Object.values(value).map((item) => getIds(item)).flat();
|
1701
|
+
return isString(value.id) ? [value.id, ...nestedIds] : nestedIds;
|
1702
|
+
}
|
1703
|
+
|
1704
|
+
var __accessCheck$3 = (obj, member, msg) => {
|
1705
|
+
if (!member.has(obj))
|
1706
|
+
throw TypeError("Cannot " + msg);
|
1707
|
+
};
|
1708
|
+
var __privateGet$3 = (obj, member, getter) => {
|
1709
|
+
__accessCheck$3(obj, member, "read from private field");
|
1710
|
+
return getter ? getter.call(obj) : member.get(obj);
|
1711
|
+
};
|
1712
|
+
var __privateAdd$3 = (obj, member, value) => {
|
1713
|
+
if (member.has(obj))
|
1714
|
+
throw TypeError("Cannot add the same private member more than once");
|
1715
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1716
|
+
};
|
1717
|
+
var __privateSet$3 = (obj, member, value, setter) => {
|
1718
|
+
__accessCheck$3(obj, member, "write to private field");
|
1719
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1720
|
+
return value;
|
1721
|
+
};
|
1722
|
+
var _map;
|
1723
|
+
class SimpleCache {
|
1724
|
+
constructor(options = {}) {
|
1725
|
+
__privateAdd$3(this, _map, void 0);
|
1726
|
+
__privateSet$3(this, _map, /* @__PURE__ */ new Map());
|
1727
|
+
this.capacity = options.max ?? 500;
|
1728
|
+
this.cacheRecords = options.cacheRecords ?? true;
|
1729
|
+
this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
|
1730
|
+
}
|
1731
|
+
async getAll() {
|
1732
|
+
return Object.fromEntries(__privateGet$3(this, _map));
|
1733
|
+
}
|
1734
|
+
async get(key) {
|
1735
|
+
return __privateGet$3(this, _map).get(key) ?? null;
|
1736
|
+
}
|
1737
|
+
async set(key, value) {
|
1738
|
+
await this.delete(key);
|
1739
|
+
__privateGet$3(this, _map).set(key, value);
|
1740
|
+
if (__privateGet$3(this, _map).size > this.capacity) {
|
1741
|
+
const leastRecentlyUsed = __privateGet$3(this, _map).keys().next().value;
|
1742
|
+
await this.delete(leastRecentlyUsed);
|
1743
|
+
}
|
1744
|
+
}
|
1745
|
+
async delete(key) {
|
1746
|
+
__privateGet$3(this, _map).delete(key);
|
1747
|
+
}
|
1748
|
+
async clear() {
|
1749
|
+
return __privateGet$3(this, _map).clear();
|
1750
|
+
}
|
1751
|
+
}
|
1752
|
+
_map = new WeakMap();
|
1353
1753
|
|
1354
1754
|
const gt = (value) => ({ $gt: value });
|
1355
1755
|
const ge = (value) => ({ $ge: value });
|
@@ -1374,7 +1774,7 @@ var __accessCheck$2 = (obj, member, msg) => {
|
|
1374
1774
|
if (!member.has(obj))
|
1375
1775
|
throw TypeError("Cannot " + msg);
|
1376
1776
|
};
|
1377
|
-
var __privateGet$
|
1777
|
+
var __privateGet$2 = (obj, member, getter) => {
|
1378
1778
|
__accessCheck$2(obj, member, "read from private field");
|
1379
1779
|
return getter ? getter.call(obj) : member.get(obj);
|
1380
1780
|
};
|
@@ -1383,122 +1783,158 @@ var __privateAdd$2 = (obj, member, value) => {
|
|
1383
1783
|
throw TypeError("Cannot add the same private member more than once");
|
1384
1784
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1385
1785
|
};
|
1386
|
-
var
|
1786
|
+
var __privateSet$2 = (obj, member, value, setter) => {
|
1787
|
+
__accessCheck$2(obj, member, "write to private field");
|
1788
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1789
|
+
return value;
|
1790
|
+
};
|
1791
|
+
var _tables, _schemaTables$1;
|
1387
1792
|
class SchemaPlugin extends XataPlugin {
|
1388
|
-
constructor(
|
1793
|
+
constructor(schemaTables) {
|
1389
1794
|
super();
|
1390
|
-
this.links = links;
|
1391
|
-
this.tableNames = tableNames;
|
1392
1795
|
__privateAdd$2(this, _tables, {});
|
1796
|
+
__privateAdd$2(this, _schemaTables$1, void 0);
|
1797
|
+
__privateSet$2(this, _schemaTables$1, schemaTables);
|
1393
1798
|
}
|
1394
|
-
build(
|
1395
|
-
const { getFetchProps } = options;
|
1396
|
-
const links = this.links;
|
1799
|
+
build(pluginOptions) {
|
1397
1800
|
const db = new Proxy({}, {
|
1398
1801
|
get: (_target, table) => {
|
1399
1802
|
if (!isString(table))
|
1400
1803
|
throw new Error("Invalid table name");
|
1401
|
-
if (
|
1402
|
-
__privateGet$
|
1403
|
-
|
1804
|
+
if (__privateGet$2(this, _tables)[table] === void 0) {
|
1805
|
+
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
|
1806
|
+
}
|
1807
|
+
return __privateGet$2(this, _tables)[table];
|
1404
1808
|
}
|
1405
1809
|
});
|
1406
|
-
|
1407
|
-
|
1810
|
+
const tableNames = __privateGet$2(this, _schemaTables$1)?.map(({ name }) => name) ?? [];
|
1811
|
+
for (const table of tableNames) {
|
1812
|
+
db[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1408
1813
|
}
|
1409
1814
|
return db;
|
1410
1815
|
}
|
1411
1816
|
}
|
1412
1817
|
_tables = new WeakMap();
|
1818
|
+
_schemaTables$1 = new WeakMap();
|
1413
1819
|
|
1414
1820
|
var __accessCheck$1 = (obj, member, msg) => {
|
1415
1821
|
if (!member.has(obj))
|
1416
1822
|
throw TypeError("Cannot " + msg);
|
1417
1823
|
};
|
1824
|
+
var __privateGet$1 = (obj, member, getter) => {
|
1825
|
+
__accessCheck$1(obj, member, "read from private field");
|
1826
|
+
return getter ? getter.call(obj) : member.get(obj);
|
1827
|
+
};
|
1418
1828
|
var __privateAdd$1 = (obj, member, value) => {
|
1419
1829
|
if (member.has(obj))
|
1420
1830
|
throw TypeError("Cannot add the same private member more than once");
|
1421
1831
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1422
1832
|
};
|
1833
|
+
var __privateSet$1 = (obj, member, value, setter) => {
|
1834
|
+
__accessCheck$1(obj, member, "write to private field");
|
1835
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1836
|
+
return value;
|
1837
|
+
};
|
1423
1838
|
var __privateMethod$1 = (obj, member, method) => {
|
1424
1839
|
__accessCheck$1(obj, member, "access private method");
|
1425
1840
|
return method;
|
1426
1841
|
};
|
1427
|
-
var _search, search_fn;
|
1842
|
+
var _schemaTables, _search, search_fn, _getSchemaTables, getSchemaTables_fn;
|
1428
1843
|
class SearchPlugin extends XataPlugin {
|
1429
|
-
constructor(db,
|
1844
|
+
constructor(db, schemaTables) {
|
1430
1845
|
super();
|
1431
1846
|
this.db = db;
|
1432
|
-
this.links = links;
|
1433
1847
|
__privateAdd$1(this, _search);
|
1848
|
+
__privateAdd$1(this, _getSchemaTables);
|
1849
|
+
__privateAdd$1(this, _schemaTables, void 0);
|
1850
|
+
__privateSet$1(this, _schemaTables, schemaTables);
|
1434
1851
|
}
|
1435
1852
|
build({ getFetchProps }) {
|
1436
1853
|
return {
|
1437
1854
|
all: async (query, options = {}) => {
|
1438
1855
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1856
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1439
1857
|
return records.map((record) => {
|
1440
1858
|
const { table = "orphan" } = record.xata;
|
1441
|
-
return { table, record: initObject(this.db,
|
1859
|
+
return { table, record: initObject(this.db, schemaTables, table, record) };
|
1442
1860
|
});
|
1443
1861
|
},
|
1444
1862
|
byTable: async (query, options = {}) => {
|
1445
1863
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1864
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1446
1865
|
return records.reduce((acc, record) => {
|
1447
1866
|
const { table = "orphan" } = record.xata;
|
1448
1867
|
const items = acc[table] ?? [];
|
1449
|
-
const item = initObject(this.db,
|
1868
|
+
const item = initObject(this.db, schemaTables, table, record);
|
1450
1869
|
return { ...acc, [table]: [...items, item] };
|
1451
1870
|
}, {});
|
1452
1871
|
}
|
1453
1872
|
};
|
1454
1873
|
}
|
1455
1874
|
}
|
1875
|
+
_schemaTables = new WeakMap();
|
1456
1876
|
_search = new WeakSet();
|
1457
1877
|
search_fn = async function(query, options, getFetchProps) {
|
1458
1878
|
const fetchProps = await getFetchProps();
|
1459
|
-
const { tables, fuzziness } = options ?? {};
|
1879
|
+
const { tables, fuzziness, highlight } = options ?? {};
|
1460
1880
|
const { records } = await searchBranch({
|
1461
1881
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1462
|
-
body: { tables, query, fuzziness },
|
1882
|
+
body: { tables, query, fuzziness, highlight },
|
1463
1883
|
...fetchProps
|
1464
1884
|
});
|
1465
1885
|
return records;
|
1466
1886
|
};
|
1887
|
+
_getSchemaTables = new WeakSet();
|
1888
|
+
getSchemaTables_fn = async function(getFetchProps) {
|
1889
|
+
if (__privateGet$1(this, _schemaTables))
|
1890
|
+
return __privateGet$1(this, _schemaTables);
|
1891
|
+
const fetchProps = await getFetchProps();
|
1892
|
+
const { schema } = await getBranchDetails({
|
1893
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1894
|
+
...fetchProps
|
1895
|
+
});
|
1896
|
+
__privateSet$1(this, _schemaTables, schema.tables);
|
1897
|
+
return schema.tables;
|
1898
|
+
};
|
1467
1899
|
|
1468
1900
|
const isBranchStrategyBuilder = (strategy) => {
|
1469
1901
|
return typeof strategy === "function";
|
1470
1902
|
};
|
1471
1903
|
|
1472
|
-
const envBranchNames = [
|
1473
|
-
"XATA_BRANCH",
|
1474
|
-
"VERCEL_GIT_COMMIT_REF",
|
1475
|
-
"CF_PAGES_BRANCH",
|
1476
|
-
"BRANCH"
|
1477
|
-
];
|
1478
|
-
const defaultBranch = "main";
|
1479
1904
|
async function getCurrentBranchName(options) {
|
1480
|
-
const
|
1481
|
-
if (
|
1482
|
-
|
1483
|
-
|
1484
|
-
|
1485
|
-
|
1486
|
-
|
1487
|
-
|
1488
|
-
|
1489
|
-
return defaultBranch;
|
1905
|
+
const { branch, envBranch } = getEnvironment();
|
1906
|
+
if (branch) {
|
1907
|
+
const details = await getDatabaseBranch(branch, options);
|
1908
|
+
if (details)
|
1909
|
+
return branch;
|
1910
|
+
console.warn(`Branch ${branch} not found in Xata. Ignoring...`);
|
1911
|
+
}
|
1912
|
+
const gitBranch = envBranch || await getGitBranch();
|
1913
|
+
return resolveXataBranch(gitBranch, options);
|
1490
1914
|
}
|
1491
1915
|
async function getCurrentBranchDetails(options) {
|
1492
|
-
const
|
1493
|
-
|
1494
|
-
|
1495
|
-
|
1496
|
-
|
1497
|
-
|
1498
|
-
|
1499
|
-
|
1500
|
-
|
1501
|
-
|
1916
|
+
const branch = await getCurrentBranchName(options);
|
1917
|
+
return getDatabaseBranch(branch, options);
|
1918
|
+
}
|
1919
|
+
async function resolveXataBranch(gitBranch, options) {
|
1920
|
+
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1921
|
+
const apiKey = options?.apiKey || getAPIKey();
|
1922
|
+
if (!databaseURL)
|
1923
|
+
throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
|
1924
|
+
if (!apiKey)
|
1925
|
+
throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
|
1926
|
+
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1927
|
+
const [workspace] = host.split(".");
|
1928
|
+
const { fallbackBranch } = getEnvironment();
|
1929
|
+
const { branch } = await resolveBranch({
|
1930
|
+
apiKey,
|
1931
|
+
apiUrl: databaseURL,
|
1932
|
+
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1933
|
+
workspacesApiUrl: `${protocol}//${host}`,
|
1934
|
+
pathParams: { dbName, workspace },
|
1935
|
+
queryParams: { gitBranch, fallbackBranch }
|
1936
|
+
});
|
1937
|
+
return branch;
|
1502
1938
|
}
|
1503
1939
|
async function getDatabaseBranch(branch, options) {
|
1504
1940
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
@@ -1516,10 +1952,7 @@ async function getDatabaseBranch(branch, options) {
|
|
1516
1952
|
apiUrl: databaseURL,
|
1517
1953
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1518
1954
|
workspacesApiUrl: `${protocol}//${host}`,
|
1519
|
-
pathParams: {
|
1520
|
-
dbBranchName,
|
1521
|
-
workspace
|
1522
|
-
}
|
1955
|
+
pathParams: { dbBranchName, workspace }
|
1523
1956
|
});
|
1524
1957
|
} catch (err) {
|
1525
1958
|
if (isObject(err) && err.status === 404)
|
@@ -1527,21 +1960,10 @@ async function getDatabaseBranch(branch, options) {
|
|
1527
1960
|
throw err;
|
1528
1961
|
}
|
1529
1962
|
}
|
1530
|
-
function getBranchByEnvVariable() {
|
1531
|
-
for (const name of envBranchNames) {
|
1532
|
-
const value = getEnvVariable(name);
|
1533
|
-
if (value) {
|
1534
|
-
return value;
|
1535
|
-
}
|
1536
|
-
}
|
1537
|
-
try {
|
1538
|
-
return XATA_BRANCH;
|
1539
|
-
} catch (err) {
|
1540
|
-
}
|
1541
|
-
}
|
1542
1963
|
function getDatabaseURL() {
|
1543
1964
|
try {
|
1544
|
-
|
1965
|
+
const { databaseURL } = getEnvironment();
|
1966
|
+
return databaseURL;
|
1545
1967
|
} catch (err) {
|
1546
1968
|
return void 0;
|
1547
1969
|
}
|
@@ -1572,22 +1994,24 @@ var __privateMethod = (obj, member, method) => {
|
|
1572
1994
|
const buildClient = (plugins) => {
|
1573
1995
|
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1574
1996
|
return _a = class {
|
1575
|
-
constructor(options = {},
|
1997
|
+
constructor(options = {}, schemaTables) {
|
1576
1998
|
__privateAdd(this, _parseOptions);
|
1577
1999
|
__privateAdd(this, _getFetchProps);
|
1578
2000
|
__privateAdd(this, _evaluateBranch);
|
1579
2001
|
__privateAdd(this, _branch, void 0);
|
1580
2002
|
const safeOptions = __privateMethod(this, _parseOptions, parseOptions_fn).call(this, options);
|
1581
|
-
const
|
1582
|
-
|
1583
|
-
|
1584
|
-
}
|
2003
|
+
const pluginOptions = {
|
2004
|
+
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
2005
|
+
cache: safeOptions.cache
|
2006
|
+
};
|
2007
|
+
const db = new SchemaPlugin(schemaTables).build(pluginOptions);
|
2008
|
+
const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
|
1585
2009
|
this.db = db;
|
1586
2010
|
this.search = search;
|
1587
2011
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
1588
|
-
if (
|
2012
|
+
if (namespace === void 0)
|
1589
2013
|
continue;
|
1590
|
-
const result = namespace.build(
|
2014
|
+
const result = namespace.build(pluginOptions);
|
1591
2015
|
if (result instanceof Promise) {
|
1592
2016
|
void result.then((namespace2) => {
|
1593
2017
|
this[key] = namespace2;
|
@@ -1601,11 +2025,12 @@ const buildClient = (plugins) => {
|
|
1601
2025
|
const fetch = getFetchImplementation(options?.fetch);
|
1602
2026
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1603
2027
|
const apiKey = options?.apiKey || getAPIKey();
|
1604
|
-
const
|
2028
|
+
const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
|
2029
|
+
const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
1605
2030
|
if (!databaseURL || !apiKey) {
|
1606
2031
|
throw new Error("Options databaseURL and apiKey are required");
|
1607
2032
|
}
|
1608
|
-
return { fetch, databaseURL, apiKey, branch };
|
2033
|
+
return { fetch, databaseURL, apiKey, branch, cache };
|
1609
2034
|
}, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({
|
1610
2035
|
fetch,
|
1611
2036
|
apiKey,
|
@@ -1628,7 +2053,7 @@ const buildClient = (plugins) => {
|
|
1628
2053
|
}, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
|
1629
2054
|
if (__privateGet(this, _branch))
|
1630
2055
|
return __privateGet(this, _branch);
|
1631
|
-
if (
|
2056
|
+
if (param === void 0)
|
1632
2057
|
return void 0;
|
1633
2058
|
const strategies = Array.isArray(param) ? [...param] : [param];
|
1634
2059
|
const evaluateBranch = async (strategy) => {
|
@@ -1661,15 +2086,18 @@ exports.PAGINATION_MAX_OFFSET = PAGINATION_MAX_OFFSET;
|
|
1661
2086
|
exports.PAGINATION_MAX_SIZE = PAGINATION_MAX_SIZE;
|
1662
2087
|
exports.Page = Page;
|
1663
2088
|
exports.Query = Query;
|
2089
|
+
exports.RecordArray = RecordArray;
|
1664
2090
|
exports.Repository = Repository;
|
1665
2091
|
exports.RestRepository = RestRepository;
|
1666
2092
|
exports.SchemaPlugin = SchemaPlugin;
|
1667
2093
|
exports.SearchPlugin = SearchPlugin;
|
2094
|
+
exports.SimpleCache = SimpleCache;
|
1668
2095
|
exports.XataApiClient = XataApiClient;
|
1669
2096
|
exports.XataApiPlugin = XataApiPlugin;
|
1670
2097
|
exports.XataError = XataError;
|
1671
2098
|
exports.XataPlugin = XataPlugin;
|
1672
2099
|
exports.acceptWorkspaceMemberInvite = acceptWorkspaceMemberInvite;
|
2100
|
+
exports.addGitBranchesEntry = addGitBranchesEntry;
|
1673
2101
|
exports.addTableColumn = addTableColumn;
|
1674
2102
|
exports.buildClient = buildClient;
|
1675
2103
|
exports.bulkInsertTableRecords = bulkInsertTableRecords;
|
@@ -1704,6 +2132,7 @@ exports.getCurrentBranchDetails = getCurrentBranchDetails;
|
|
1704
2132
|
exports.getCurrentBranchName = getCurrentBranchName;
|
1705
2133
|
exports.getDatabaseList = getDatabaseList;
|
1706
2134
|
exports.getDatabaseURL = getDatabaseURL;
|
2135
|
+
exports.getGitBranchesMapping = getGitBranchesMapping;
|
1707
2136
|
exports.getRecord = getRecord;
|
1708
2137
|
exports.getTableColumns = getTableColumns;
|
1709
2138
|
exports.getTableSchema = getTableSchema;
|
@@ -1722,6 +2151,7 @@ exports.insertRecord = insertRecord;
|
|
1722
2151
|
exports.insertRecordWithID = insertRecordWithID;
|
1723
2152
|
exports.inviteWorkspaceMember = inviteWorkspaceMember;
|
1724
2153
|
exports.is = is;
|
2154
|
+
exports.isCursorPaginationOptions = isCursorPaginationOptions;
|
1725
2155
|
exports.isIdentifiable = isIdentifiable;
|
1726
2156
|
exports.isNot = isNot;
|
1727
2157
|
exports.isXataRecord = isXataRecord;
|
@@ -1732,9 +2162,12 @@ exports.notExists = notExists;
|
|
1732
2162
|
exports.operationsByTag = operationsByTag;
|
1733
2163
|
exports.pattern = pattern;
|
1734
2164
|
exports.queryTable = queryTable;
|
2165
|
+
exports.removeGitBranchesEntry = removeGitBranchesEntry;
|
1735
2166
|
exports.removeWorkspaceMember = removeWorkspaceMember;
|
1736
2167
|
exports.resendWorkspaceMemberInvite = resendWorkspaceMemberInvite;
|
2168
|
+
exports.resolveBranch = resolveBranch;
|
1737
2169
|
exports.searchBranch = searchBranch;
|
2170
|
+
exports.searchTable = searchTable;
|
1738
2171
|
exports.setTableSchema = setTableSchema;
|
1739
2172
|
exports.startsWith = startsWith;
|
1740
2173
|
exports.updateBranchMetadata = updateBranchMetadata;
|