@xata.io/client 0.0.0-alpha.vf3ed7b8 → 0.0.0-alpha.vf45a2df
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 +633 -242
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +635 -142
- package/dist/index.mjs +626 -243
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -3
- package/tsconfig.json +1 -0
package/dist/index.mjs
CHANGED
@@ -7,8 +7,19 @@ 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 toBase64(value) {
|
17
|
+
try {
|
18
|
+
return btoa(value);
|
19
|
+
} catch (err) {
|
20
|
+
const buf = Buffer;
|
21
|
+
return buf.from(value).toString("base64");
|
22
|
+
}
|
12
23
|
}
|
13
24
|
|
14
25
|
function getEnvVariable(name) {
|
@@ -27,7 +38,10 @@ function getEnvVariable(name) {
|
|
27
38
|
}
|
28
39
|
async function getGitBranch() {
|
29
40
|
try {
|
30
|
-
|
41
|
+
if (typeof require === "function") {
|
42
|
+
const req = require;
|
43
|
+
return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
|
44
|
+
}
|
31
45
|
} catch (err) {
|
32
46
|
}
|
33
47
|
try {
|
@@ -60,16 +74,28 @@ function getFetchImplementation(userFetch) {
|
|
60
74
|
return fetchImpl;
|
61
75
|
}
|
62
76
|
|
63
|
-
|
64
|
-
|
77
|
+
const VERSION = "0.0.0-alpha.vf45a2df";
|
78
|
+
|
79
|
+
class ErrorWithCause extends Error {
|
80
|
+
constructor(message, options) {
|
81
|
+
super(message, options);
|
82
|
+
}
|
83
|
+
}
|
84
|
+
class FetcherError extends ErrorWithCause {
|
85
|
+
constructor(status, data, requestId) {
|
65
86
|
super(getMessage(data));
|
66
87
|
this.status = status;
|
67
88
|
this.errors = isBulkError(data) ? data.errors : void 0;
|
89
|
+
this.requestId = requestId;
|
68
90
|
if (data instanceof Error) {
|
69
91
|
this.stack = data.stack;
|
70
92
|
this.cause = data.cause;
|
71
93
|
}
|
72
94
|
}
|
95
|
+
toString() {
|
96
|
+
const error = super.toString();
|
97
|
+
return `[${this.status}] (${this.requestId ?? "Unknown"}): ${error}`;
|
98
|
+
}
|
73
99
|
}
|
74
100
|
function isBulkError(error) {
|
75
101
|
return isObject(error) && Array.isArray(error.errors);
|
@@ -92,7 +118,12 @@ function getMessage(data) {
|
|
92
118
|
}
|
93
119
|
|
94
120
|
const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
|
95
|
-
const
|
121
|
+
const cleanQueryParams = Object.entries(queryParams).reduce((acc, [key, value]) => {
|
122
|
+
if (value === void 0 || value === null)
|
123
|
+
return acc;
|
124
|
+
return { ...acc, [key]: value };
|
125
|
+
}, {});
|
126
|
+
const query = new URLSearchParams(cleanQueryParams).toString();
|
96
127
|
const queryString = query.length > 0 ? `?${query}` : "";
|
97
128
|
return url.replace(/\{\w*\}/g, (key) => pathParams[key.slice(1, -1)]) + queryString;
|
98
129
|
};
|
@@ -132,6 +163,7 @@ async function fetch$1({
|
|
132
163
|
body: body ? JSON.stringify(body) : void 0,
|
133
164
|
headers: {
|
134
165
|
"Content-Type": "application/json",
|
166
|
+
"User-Agent": `Xata client-ts/${VERSION}`,
|
135
167
|
...headers,
|
136
168
|
...hostHeader(fullUrl),
|
137
169
|
Authorization: `Bearer ${apiKey}`
|
@@ -140,14 +172,15 @@ async function fetch$1({
|
|
140
172
|
if (response.status === 204) {
|
141
173
|
return {};
|
142
174
|
}
|
175
|
+
const requestId = response.headers?.get("x-request-id") ?? void 0;
|
143
176
|
try {
|
144
177
|
const jsonResponse = await response.json();
|
145
178
|
if (response.ok) {
|
146
179
|
return jsonResponse;
|
147
180
|
}
|
148
|
-
throw new FetcherError(response.status, jsonResponse);
|
181
|
+
throw new FetcherError(response.status, jsonResponse, requestId);
|
149
182
|
} catch (error) {
|
150
|
-
throw new FetcherError(response.status, error);
|
183
|
+
throw new FetcherError(response.status, error, requestId);
|
151
184
|
}
|
152
185
|
}
|
153
186
|
|
@@ -241,6 +274,14 @@ const deleteDatabase = (variables) => fetch$1({
|
|
241
274
|
method: "delete",
|
242
275
|
...variables
|
243
276
|
});
|
277
|
+
const getGitBranchesMapping = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "get", ...variables });
|
278
|
+
const addGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "post", ...variables });
|
279
|
+
const removeGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "delete", ...variables });
|
280
|
+
const resolveBranch = (variables) => fetch$1({
|
281
|
+
url: "/dbs/{dbName}/resolveBranch",
|
282
|
+
method: "get",
|
283
|
+
...variables
|
284
|
+
});
|
244
285
|
const getBranchDetails = (variables) => fetch$1({
|
245
286
|
url: "/db/{dbBranchName}",
|
246
287
|
method: "get",
|
@@ -348,6 +389,11 @@ const queryTable = (variables) => fetch$1({
|
|
348
389
|
method: "post",
|
349
390
|
...variables
|
350
391
|
});
|
392
|
+
const searchTable = (variables) => fetch$1({
|
393
|
+
url: "/db/{dbBranchName}/tables/{tableName}/search",
|
394
|
+
method: "post",
|
395
|
+
...variables
|
396
|
+
});
|
351
397
|
const searchBranch = (variables) => fetch$1({
|
352
398
|
url: "/db/{dbBranchName}/search",
|
353
399
|
method: "post",
|
@@ -369,7 +415,15 @@ const operationsByTag = {
|
|
369
415
|
resendWorkspaceMemberInvite,
|
370
416
|
acceptWorkspaceMemberInvite
|
371
417
|
},
|
372
|
-
database: {
|
418
|
+
database: {
|
419
|
+
getDatabaseList,
|
420
|
+
createDatabase,
|
421
|
+
deleteDatabase,
|
422
|
+
getGitBranchesMapping,
|
423
|
+
addGitBranchesEntry,
|
424
|
+
removeGitBranchesEntry,
|
425
|
+
resolveBranch
|
426
|
+
},
|
373
427
|
branch: {
|
374
428
|
getBranchList,
|
375
429
|
getBranchDetails,
|
@@ -403,6 +457,7 @@ const operationsByTag = {
|
|
403
457
|
getRecord,
|
404
458
|
bulkInsertTableRecords,
|
405
459
|
queryTable,
|
460
|
+
searchTable,
|
406
461
|
searchBranch
|
407
462
|
}
|
408
463
|
};
|
@@ -432,35 +487,35 @@ function isValidBuilder(builder) {
|
|
432
487
|
return isObject(builder) && isString(builder.main) && isString(builder.workspaces);
|
433
488
|
}
|
434
489
|
|
435
|
-
var __accessCheck$
|
490
|
+
var __accessCheck$7 = (obj, member, msg) => {
|
436
491
|
if (!member.has(obj))
|
437
492
|
throw TypeError("Cannot " + msg);
|
438
493
|
};
|
439
|
-
var __privateGet$
|
440
|
-
__accessCheck$
|
494
|
+
var __privateGet$7 = (obj, member, getter) => {
|
495
|
+
__accessCheck$7(obj, member, "read from private field");
|
441
496
|
return getter ? getter.call(obj) : member.get(obj);
|
442
497
|
};
|
443
|
-
var __privateAdd$
|
498
|
+
var __privateAdd$7 = (obj, member, value) => {
|
444
499
|
if (member.has(obj))
|
445
500
|
throw TypeError("Cannot add the same private member more than once");
|
446
501
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
447
502
|
};
|
448
|
-
var __privateSet$
|
449
|
-
__accessCheck$
|
503
|
+
var __privateSet$6 = (obj, member, value, setter) => {
|
504
|
+
__accessCheck$7(obj, member, "write to private field");
|
450
505
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
451
506
|
return value;
|
452
507
|
};
|
453
508
|
var _extraProps, _namespaces;
|
454
509
|
class XataApiClient {
|
455
510
|
constructor(options = {}) {
|
456
|
-
__privateAdd$
|
457
|
-
__privateAdd$
|
511
|
+
__privateAdd$7(this, _extraProps, void 0);
|
512
|
+
__privateAdd$7(this, _namespaces, {});
|
458
513
|
const provider = options.host ?? "production";
|
459
514
|
const apiKey = options?.apiKey ?? getAPIKey();
|
460
515
|
if (!apiKey) {
|
461
516
|
throw new Error("Could not resolve a valid apiKey");
|
462
517
|
}
|
463
|
-
__privateSet$
|
518
|
+
__privateSet$6(this, _extraProps, {
|
464
519
|
apiUrl: getHostUrl(provider, "main"),
|
465
520
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
466
521
|
fetchImpl: getFetchImplementation(options.fetch),
|
@@ -468,34 +523,34 @@ class XataApiClient {
|
|
468
523
|
});
|
469
524
|
}
|
470
525
|
get user() {
|
471
|
-
if (!__privateGet$
|
472
|
-
__privateGet$
|
473
|
-
return __privateGet$
|
526
|
+
if (!__privateGet$7(this, _namespaces).user)
|
527
|
+
__privateGet$7(this, _namespaces).user = new UserApi(__privateGet$7(this, _extraProps));
|
528
|
+
return __privateGet$7(this, _namespaces).user;
|
474
529
|
}
|
475
530
|
get workspaces() {
|
476
|
-
if (!__privateGet$
|
477
|
-
__privateGet$
|
478
|
-
return __privateGet$
|
531
|
+
if (!__privateGet$7(this, _namespaces).workspaces)
|
532
|
+
__privateGet$7(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$7(this, _extraProps));
|
533
|
+
return __privateGet$7(this, _namespaces).workspaces;
|
479
534
|
}
|
480
535
|
get databases() {
|
481
|
-
if (!__privateGet$
|
482
|
-
__privateGet$
|
483
|
-
return __privateGet$
|
536
|
+
if (!__privateGet$7(this, _namespaces).databases)
|
537
|
+
__privateGet$7(this, _namespaces).databases = new DatabaseApi(__privateGet$7(this, _extraProps));
|
538
|
+
return __privateGet$7(this, _namespaces).databases;
|
484
539
|
}
|
485
540
|
get branches() {
|
486
|
-
if (!__privateGet$
|
487
|
-
__privateGet$
|
488
|
-
return __privateGet$
|
541
|
+
if (!__privateGet$7(this, _namespaces).branches)
|
542
|
+
__privateGet$7(this, _namespaces).branches = new BranchApi(__privateGet$7(this, _extraProps));
|
543
|
+
return __privateGet$7(this, _namespaces).branches;
|
489
544
|
}
|
490
545
|
get tables() {
|
491
|
-
if (!__privateGet$
|
492
|
-
__privateGet$
|
493
|
-
return __privateGet$
|
546
|
+
if (!__privateGet$7(this, _namespaces).tables)
|
547
|
+
__privateGet$7(this, _namespaces).tables = new TableApi(__privateGet$7(this, _extraProps));
|
548
|
+
return __privateGet$7(this, _namespaces).tables;
|
494
549
|
}
|
495
550
|
get records() {
|
496
|
-
if (!__privateGet$
|
497
|
-
__privateGet$
|
498
|
-
return __privateGet$
|
551
|
+
if (!__privateGet$7(this, _namespaces).records)
|
552
|
+
__privateGet$7(this, _namespaces).records = new RecordsApi(__privateGet$7(this, _extraProps));
|
553
|
+
return __privateGet$7(this, _namespaces).records;
|
499
554
|
}
|
500
555
|
}
|
501
556
|
_extraProps = new WeakMap();
|
@@ -629,6 +684,33 @@ class DatabaseApi {
|
|
629
684
|
...this.extraProps
|
630
685
|
});
|
631
686
|
}
|
687
|
+
getGitBranchesMapping(workspace, dbName) {
|
688
|
+
return operationsByTag.database.getGitBranchesMapping({
|
689
|
+
pathParams: { workspace, dbName },
|
690
|
+
...this.extraProps
|
691
|
+
});
|
692
|
+
}
|
693
|
+
addGitBranchesEntry(workspace, dbName, body) {
|
694
|
+
return operationsByTag.database.addGitBranchesEntry({
|
695
|
+
pathParams: { workspace, dbName },
|
696
|
+
body,
|
697
|
+
...this.extraProps
|
698
|
+
});
|
699
|
+
}
|
700
|
+
removeGitBranchesEntry(workspace, dbName, gitBranch) {
|
701
|
+
return operationsByTag.database.removeGitBranchesEntry({
|
702
|
+
pathParams: { workspace, dbName },
|
703
|
+
queryParams: { gitBranch },
|
704
|
+
...this.extraProps
|
705
|
+
});
|
706
|
+
}
|
707
|
+
resolveBranch(workspace, dbName, gitBranch, fallbackBranch) {
|
708
|
+
return operationsByTag.database.resolveBranch({
|
709
|
+
pathParams: { workspace, dbName },
|
710
|
+
queryParams: { gitBranch, fallbackBranch },
|
711
|
+
...this.extraProps
|
712
|
+
});
|
713
|
+
}
|
632
714
|
}
|
633
715
|
class BranchApi {
|
634
716
|
constructor(extraProps) {
|
@@ -646,10 +728,10 @@ class BranchApi {
|
|
646
728
|
...this.extraProps
|
647
729
|
});
|
648
730
|
}
|
649
|
-
createBranch(workspace, database, branch, from
|
731
|
+
createBranch(workspace, database, branch, from, options = {}) {
|
650
732
|
return operationsByTag.branch.createBranch({
|
651
733
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
652
|
-
queryParams: { from },
|
734
|
+
queryParams: isString(from) ? { from } : void 0,
|
653
735
|
body: options,
|
654
736
|
...this.extraProps
|
655
737
|
});
|
@@ -831,6 +913,13 @@ class RecordsApi {
|
|
831
913
|
...this.extraProps
|
832
914
|
});
|
833
915
|
}
|
916
|
+
searchTable(workspace, database, branch, tableName, query) {
|
917
|
+
return operationsByTag.records.searchTable({
|
918
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
919
|
+
body: query,
|
920
|
+
...this.extraProps
|
921
|
+
});
|
922
|
+
}
|
834
923
|
searchBranch(workspace, database, branch, query) {
|
835
924
|
return operationsByTag.records.searchBranch({
|
836
925
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
@@ -850,43 +939,43 @@ class XataApiPlugin {
|
|
850
939
|
class XataPlugin {
|
851
940
|
}
|
852
941
|
|
853
|
-
var __accessCheck$
|
942
|
+
var __accessCheck$6 = (obj, member, msg) => {
|
854
943
|
if (!member.has(obj))
|
855
944
|
throw TypeError("Cannot " + msg);
|
856
945
|
};
|
857
|
-
var __privateGet$
|
858
|
-
__accessCheck$
|
946
|
+
var __privateGet$6 = (obj, member, getter) => {
|
947
|
+
__accessCheck$6(obj, member, "read from private field");
|
859
948
|
return getter ? getter.call(obj) : member.get(obj);
|
860
949
|
};
|
861
|
-
var __privateAdd$
|
950
|
+
var __privateAdd$6 = (obj, member, value) => {
|
862
951
|
if (member.has(obj))
|
863
952
|
throw TypeError("Cannot add the same private member more than once");
|
864
953
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
865
954
|
};
|
866
|
-
var __privateSet$
|
867
|
-
__accessCheck$
|
955
|
+
var __privateSet$5 = (obj, member, value, setter) => {
|
956
|
+
__accessCheck$6(obj, member, "write to private field");
|
868
957
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
869
958
|
return value;
|
870
959
|
};
|
871
|
-
var _query;
|
960
|
+
var _query, _page;
|
872
961
|
class Page {
|
873
962
|
constructor(query, meta, records = []) {
|
874
|
-
__privateAdd$
|
875
|
-
__privateSet$
|
963
|
+
__privateAdd$6(this, _query, void 0);
|
964
|
+
__privateSet$5(this, _query, query);
|
876
965
|
this.meta = meta;
|
877
|
-
this.records = records;
|
966
|
+
this.records = new RecordArray(this, records);
|
878
967
|
}
|
879
968
|
async nextPage(size, offset) {
|
880
|
-
return __privateGet$
|
969
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
|
881
970
|
}
|
882
971
|
async previousPage(size, offset) {
|
883
|
-
return __privateGet$
|
972
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, before: this.meta.page.cursor } });
|
884
973
|
}
|
885
974
|
async firstPage(size, offset) {
|
886
|
-
return __privateGet$
|
975
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, first: this.meta.page.cursor } });
|
887
976
|
}
|
888
977
|
async lastPage(size, offset) {
|
889
|
-
return __privateGet$
|
978
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, last: this.meta.page.cursor } });
|
890
979
|
}
|
891
980
|
hasNextPage() {
|
892
981
|
return this.meta.page.more;
|
@@ -894,50 +983,93 @@ class Page {
|
|
894
983
|
}
|
895
984
|
_query = new WeakMap();
|
896
985
|
const PAGINATION_MAX_SIZE = 200;
|
897
|
-
const PAGINATION_DEFAULT_SIZE =
|
986
|
+
const PAGINATION_DEFAULT_SIZE = 20;
|
898
987
|
const PAGINATION_MAX_OFFSET = 800;
|
899
988
|
const PAGINATION_DEFAULT_OFFSET = 0;
|
989
|
+
function isCursorPaginationOptions(options) {
|
990
|
+
return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
|
991
|
+
}
|
992
|
+
const _RecordArray = class extends Array {
|
993
|
+
constructor(page, overrideRecords) {
|
994
|
+
super(..._RecordArray.parseConstructorParams(page, overrideRecords));
|
995
|
+
__privateAdd$6(this, _page, void 0);
|
996
|
+
__privateSet$5(this, _page, page);
|
997
|
+
}
|
998
|
+
static parseConstructorParams(...args) {
|
999
|
+
if (args.length === 1 && typeof args[0] === "number") {
|
1000
|
+
return new Array(args[0]);
|
1001
|
+
}
|
1002
|
+
if (args.length <= 2 && isObject(args[0]?.meta) && Array.isArray(args[1] ?? [])) {
|
1003
|
+
const result = args[1] ?? args[0].records ?? [];
|
1004
|
+
return new Array(...result);
|
1005
|
+
}
|
1006
|
+
return new Array(...args);
|
1007
|
+
}
|
1008
|
+
async nextPage(size, offset) {
|
1009
|
+
const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
|
1010
|
+
return new _RecordArray(newPage);
|
1011
|
+
}
|
1012
|
+
async previousPage(size, offset) {
|
1013
|
+
const newPage = await __privateGet$6(this, _page).previousPage(size, offset);
|
1014
|
+
return new _RecordArray(newPage);
|
1015
|
+
}
|
1016
|
+
async firstPage(size, offset) {
|
1017
|
+
const newPage = await __privateGet$6(this, _page).firstPage(size, offset);
|
1018
|
+
return new _RecordArray(newPage);
|
1019
|
+
}
|
1020
|
+
async lastPage(size, offset) {
|
1021
|
+
const newPage = await __privateGet$6(this, _page).lastPage(size, offset);
|
1022
|
+
return new _RecordArray(newPage);
|
1023
|
+
}
|
1024
|
+
hasNextPage() {
|
1025
|
+
return __privateGet$6(this, _page).meta.page.more;
|
1026
|
+
}
|
1027
|
+
};
|
1028
|
+
let RecordArray = _RecordArray;
|
1029
|
+
_page = new WeakMap();
|
900
1030
|
|
901
|
-
var __accessCheck$
|
1031
|
+
var __accessCheck$5 = (obj, member, msg) => {
|
902
1032
|
if (!member.has(obj))
|
903
1033
|
throw TypeError("Cannot " + msg);
|
904
1034
|
};
|
905
|
-
var __privateGet$
|
906
|
-
__accessCheck$
|
1035
|
+
var __privateGet$5 = (obj, member, getter) => {
|
1036
|
+
__accessCheck$5(obj, member, "read from private field");
|
907
1037
|
return getter ? getter.call(obj) : member.get(obj);
|
908
1038
|
};
|
909
|
-
var __privateAdd$
|
1039
|
+
var __privateAdd$5 = (obj, member, value) => {
|
910
1040
|
if (member.has(obj))
|
911
1041
|
throw TypeError("Cannot add the same private member more than once");
|
912
1042
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
913
1043
|
};
|
914
|
-
var __privateSet$
|
915
|
-
__accessCheck$
|
1044
|
+
var __privateSet$4 = (obj, member, value, setter) => {
|
1045
|
+
__accessCheck$5(obj, member, "write to private field");
|
916
1046
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
917
1047
|
return value;
|
918
1048
|
};
|
919
1049
|
var _table$1, _repository, _data;
|
920
1050
|
const _Query = class {
|
921
|
-
constructor(repository, table, data,
|
922
|
-
__privateAdd$
|
923
|
-
__privateAdd$
|
924
|
-
__privateAdd$
|
1051
|
+
constructor(repository, table, data, rawParent) {
|
1052
|
+
__privateAdd$5(this, _table$1, void 0);
|
1053
|
+
__privateAdd$5(this, _repository, void 0);
|
1054
|
+
__privateAdd$5(this, _data, { filter: {} });
|
925
1055
|
this.meta = { page: { cursor: "start", more: true } };
|
926
|
-
this.records = [];
|
927
|
-
__privateSet$
|
1056
|
+
this.records = new RecordArray(this, []);
|
1057
|
+
__privateSet$4(this, _table$1, table);
|
928
1058
|
if (repository) {
|
929
|
-
__privateSet$
|
1059
|
+
__privateSet$4(this, _repository, repository);
|
930
1060
|
} else {
|
931
|
-
__privateSet$
|
1061
|
+
__privateSet$4(this, _repository, this);
|
932
1062
|
}
|
933
|
-
|
934
|
-
__privateGet$
|
935
|
-
__privateGet$
|
936
|
-
__privateGet$
|
937
|
-
__privateGet$
|
938
|
-
__privateGet$
|
939
|
-
__privateGet$
|
940
|
-
__privateGet$
|
1063
|
+
const parent = cleanParent(data, rawParent);
|
1064
|
+
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
1065
|
+
__privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
|
1066
|
+
__privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
|
1067
|
+
__privateGet$5(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
|
1068
|
+
__privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
|
1069
|
+
__privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
|
1070
|
+
__privateGet$5(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
|
1071
|
+
__privateGet$5(this, _data).pagination = data.pagination ?? parent?.pagination;
|
1072
|
+
__privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
|
941
1073
|
this.any = this.any.bind(this);
|
942
1074
|
this.all = this.all.bind(this);
|
943
1075
|
this.not = this.not.bind(this);
|
@@ -948,75 +1080,88 @@ const _Query = class {
|
|
948
1080
|
Object.defineProperty(this, "repository", { enumerable: false });
|
949
1081
|
}
|
950
1082
|
getQueryOptions() {
|
951
|
-
return __privateGet$
|
1083
|
+
return __privateGet$5(this, _data);
|
1084
|
+
}
|
1085
|
+
key() {
|
1086
|
+
const { columns = [], filter = {}, sort = [], pagination = {} } = __privateGet$5(this, _data);
|
1087
|
+
const key = JSON.stringify({ columns, filter, sort, pagination });
|
1088
|
+
return toBase64(key);
|
952
1089
|
}
|
953
1090
|
any(...queries) {
|
954
1091
|
const $any = queries.map((query) => query.getQueryOptions().filter ?? {});
|
955
|
-
return new _Query(__privateGet$
|
1092
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $any } }, __privateGet$5(this, _data));
|
956
1093
|
}
|
957
1094
|
all(...queries) {
|
958
1095
|
const $all = queries.map((query) => query.getQueryOptions().filter ?? {});
|
959
|
-
return new _Query(__privateGet$
|
1096
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
960
1097
|
}
|
961
1098
|
not(...queries) {
|
962
1099
|
const $not = queries.map((query) => query.getQueryOptions().filter ?? {});
|
963
|
-
return new _Query(__privateGet$
|
1100
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $not } }, __privateGet$5(this, _data));
|
964
1101
|
}
|
965
1102
|
none(...queries) {
|
966
1103
|
const $none = queries.map((query) => query.getQueryOptions().filter ?? {});
|
967
|
-
return new _Query(__privateGet$
|
1104
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $none } }, __privateGet$5(this, _data));
|
968
1105
|
}
|
969
1106
|
filter(a, b) {
|
970
1107
|
if (arguments.length === 1) {
|
971
1108
|
const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
|
972
|
-
const $all = compact([__privateGet$
|
973
|
-
return new _Query(__privateGet$
|
1109
|
+
const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat(constraints));
|
1110
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
974
1111
|
} else {
|
975
|
-
const $all = compact([__privateGet$
|
976
|
-
return new _Query(__privateGet$
|
1112
|
+
const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
|
1113
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
977
1114
|
}
|
978
1115
|
}
|
979
1116
|
sort(column, direction) {
|
980
|
-
const originalSort = [__privateGet$
|
1117
|
+
const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
|
981
1118
|
const sort = [...originalSort, { column, direction }];
|
982
|
-
return new _Query(__privateGet$
|
1119
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
|
983
1120
|
}
|
984
1121
|
select(columns) {
|
985
|
-
return new _Query(__privateGet$
|
1122
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { columns }, __privateGet$5(this, _data));
|
986
1123
|
}
|
987
1124
|
getPaginated(options = {}) {
|
988
|
-
const query = new _Query(__privateGet$
|
989
|
-
return __privateGet$
|
1125
|
+
const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
|
1126
|
+
return __privateGet$5(this, _repository).query(query);
|
990
1127
|
}
|
991
1128
|
async *[Symbol.asyncIterator]() {
|
992
|
-
for await (const [record] of this.getIterator(1)) {
|
1129
|
+
for await (const [record] of this.getIterator({ batchSize: 1 })) {
|
993
1130
|
yield record;
|
994
1131
|
}
|
995
1132
|
}
|
996
|
-
async *getIterator(
|
997
|
-
|
998
|
-
let
|
999
|
-
|
1000
|
-
|
1001
|
-
|
1002
|
-
|
1003
|
-
|
1133
|
+
async *getIterator(options = {}) {
|
1134
|
+
const { batchSize = 1 } = options;
|
1135
|
+
let page = await this.getPaginated({ ...options, pagination: { size: batchSize, offset: 0 } });
|
1136
|
+
let more = page.hasNextPage();
|
1137
|
+
yield page.records;
|
1138
|
+
while (more) {
|
1139
|
+
page = await page.nextPage();
|
1140
|
+
more = page.hasNextPage();
|
1141
|
+
yield page.records;
|
1004
1142
|
}
|
1005
1143
|
}
|
1006
1144
|
async getMany(options = {}) {
|
1007
|
-
const
|
1008
|
-
|
1145
|
+
const page = await this.getPaginated(options);
|
1146
|
+
if (page.hasNextPage() && options.pagination?.size === void 0) {
|
1147
|
+
console.trace("Calling getMany does not return all results. Paginate to get all results or call getAll.");
|
1148
|
+
}
|
1149
|
+
return page.records;
|
1009
1150
|
}
|
1010
|
-
async getAll(
|
1151
|
+
async getAll(options = {}) {
|
1152
|
+
const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
|
1011
1153
|
const results = [];
|
1012
|
-
for await (const page of this.getIterator(
|
1154
|
+
for await (const page of this.getIterator({ ...rest, batchSize })) {
|
1013
1155
|
results.push(...page);
|
1014
1156
|
}
|
1015
1157
|
return results;
|
1016
1158
|
}
|
1017
|
-
async
|
1018
|
-
const records = await this.getMany({ ...options,
|
1019
|
-
return records[0]
|
1159
|
+
async getFirst(options = {}) {
|
1160
|
+
const records = await this.getMany({ ...options, pagination: { size: 1 } });
|
1161
|
+
return records[0] ?? null;
|
1162
|
+
}
|
1163
|
+
cache(ttl) {
|
1164
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
|
1020
1165
|
}
|
1021
1166
|
nextPage(size, offset) {
|
1022
1167
|
return this.firstPage(size, offset);
|
@@ -1025,10 +1170,10 @@ const _Query = class {
|
|
1025
1170
|
return this.firstPage(size, offset);
|
1026
1171
|
}
|
1027
1172
|
firstPage(size, offset) {
|
1028
|
-
return this.getPaginated({
|
1173
|
+
return this.getPaginated({ pagination: { size, offset } });
|
1029
1174
|
}
|
1030
1175
|
lastPage(size, offset) {
|
1031
|
-
return this.getPaginated({
|
1176
|
+
return this.getPaginated({ pagination: { size, offset, before: "end" } });
|
1032
1177
|
}
|
1033
1178
|
hasNextPage() {
|
1034
1179
|
return this.meta.page.more;
|
@@ -1038,12 +1183,20 @@ let Query = _Query;
|
|
1038
1183
|
_table$1 = new WeakMap();
|
1039
1184
|
_repository = new WeakMap();
|
1040
1185
|
_data = new WeakMap();
|
1186
|
+
function cleanParent(data, parent) {
|
1187
|
+
if (isCursorPaginationOptions(data.pagination)) {
|
1188
|
+
return { ...parent, sorting: void 0, filter: void 0 };
|
1189
|
+
}
|
1190
|
+
return parent;
|
1191
|
+
}
|
1041
1192
|
|
1042
1193
|
function isIdentifiable(x) {
|
1043
1194
|
return isObject(x) && isString(x?.id);
|
1044
1195
|
}
|
1045
1196
|
function isXataRecord(x) {
|
1046
|
-
|
1197
|
+
const record = x;
|
1198
|
+
const metadata = record?.getMetadata();
|
1199
|
+
return isIdentifiable(x) && isObject(metadata) && typeof metadata.version === "number";
|
1047
1200
|
}
|
1048
1201
|
|
1049
1202
|
function isSortFilterString(value) {
|
@@ -1069,169 +1222,231 @@ function buildSortFilter(filter) {
|
|
1069
1222
|
}
|
1070
1223
|
}
|
1071
1224
|
|
1072
|
-
var __accessCheck$
|
1225
|
+
var __accessCheck$4 = (obj, member, msg) => {
|
1073
1226
|
if (!member.has(obj))
|
1074
1227
|
throw TypeError("Cannot " + msg);
|
1075
1228
|
};
|
1076
|
-
var __privateGet$
|
1077
|
-
__accessCheck$
|
1229
|
+
var __privateGet$4 = (obj, member, getter) => {
|
1230
|
+
__accessCheck$4(obj, member, "read from private field");
|
1078
1231
|
return getter ? getter.call(obj) : member.get(obj);
|
1079
1232
|
};
|
1080
|
-
var __privateAdd$
|
1233
|
+
var __privateAdd$4 = (obj, member, value) => {
|
1081
1234
|
if (member.has(obj))
|
1082
1235
|
throw TypeError("Cannot add the same private member more than once");
|
1083
1236
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1084
1237
|
};
|
1085
|
-
var __privateSet$
|
1086
|
-
__accessCheck$
|
1238
|
+
var __privateSet$3 = (obj, member, value, setter) => {
|
1239
|
+
__accessCheck$4(obj, member, "write to private field");
|
1087
1240
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1088
1241
|
return value;
|
1089
1242
|
};
|
1090
1243
|
var __privateMethod$2 = (obj, member, method) => {
|
1091
|
-
__accessCheck$
|
1244
|
+
__accessCheck$4(obj, member, "access private method");
|
1092
1245
|
return method;
|
1093
1246
|
};
|
1094
|
-
var _table,
|
1247
|
+
var _table, _getFetchProps, _cache, _schema$1, _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, _getSchema$1, getSchema_fn$1;
|
1095
1248
|
class Repository extends Query {
|
1096
1249
|
}
|
1097
1250
|
class RestRepository extends Query {
|
1098
1251
|
constructor(options) {
|
1099
1252
|
super(null, options.table, {});
|
1100
|
-
__privateAdd$
|
1101
|
-
__privateAdd$
|
1102
|
-
__privateAdd$
|
1103
|
-
__privateAdd$
|
1104
|
-
__privateAdd$
|
1105
|
-
__privateAdd$
|
1106
|
-
__privateAdd$
|
1107
|
-
__privateAdd$
|
1108
|
-
__privateAdd$
|
1109
|
-
|
1110
|
-
|
1111
|
-
|
1253
|
+
__privateAdd$4(this, _insertRecordWithoutId);
|
1254
|
+
__privateAdd$4(this, _insertRecordWithId);
|
1255
|
+
__privateAdd$4(this, _bulkInsertTableRecords);
|
1256
|
+
__privateAdd$4(this, _updateRecordWithID);
|
1257
|
+
__privateAdd$4(this, _upsertRecordWithID);
|
1258
|
+
__privateAdd$4(this, _deleteRecord);
|
1259
|
+
__privateAdd$4(this, _invalidateCache);
|
1260
|
+
__privateAdd$4(this, _setCacheRecord);
|
1261
|
+
__privateAdd$4(this, _getCacheRecord);
|
1262
|
+
__privateAdd$4(this, _setCacheQuery);
|
1263
|
+
__privateAdd$4(this, _getCacheQuery);
|
1264
|
+
__privateAdd$4(this, _getSchema$1);
|
1265
|
+
__privateAdd$4(this, _table, void 0);
|
1266
|
+
__privateAdd$4(this, _getFetchProps, void 0);
|
1267
|
+
__privateAdd$4(this, _cache, void 0);
|
1268
|
+
__privateAdd$4(this, _schema$1, void 0);
|
1269
|
+
__privateSet$3(this, _table, options.table);
|
1270
|
+
__privateSet$3(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
1112
1271
|
this.db = options.db;
|
1272
|
+
__privateSet$3(this, _cache, options.pluginOptions.cache);
|
1113
1273
|
}
|
1114
1274
|
async create(a, b) {
|
1115
1275
|
if (Array.isArray(a)) {
|
1116
|
-
|
1276
|
+
if (a.length === 0)
|
1277
|
+
return [];
|
1278
|
+
const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
|
1279
|
+
await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
|
1280
|
+
return records;
|
1117
1281
|
}
|
1118
1282
|
if (isString(a) && isObject(b)) {
|
1119
1283
|
if (a === "")
|
1120
1284
|
throw new Error("The id can't be empty");
|
1121
|
-
|
1285
|
+
const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b);
|
1286
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1287
|
+
return record;
|
1122
1288
|
}
|
1123
1289
|
if (isObject(a) && isString(a.id)) {
|
1124
1290
|
if (a.id === "")
|
1125
1291
|
throw new Error("The id can't be empty");
|
1126
|
-
|
1292
|
+
const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 });
|
1293
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1294
|
+
return record;
|
1127
1295
|
}
|
1128
1296
|
if (isObject(a)) {
|
1129
|
-
|
1297
|
+
const record = await __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a);
|
1298
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1299
|
+
return record;
|
1130
1300
|
}
|
1131
1301
|
throw new Error("Invalid arguments for create method");
|
1132
1302
|
}
|
1133
|
-
async read(
|
1134
|
-
|
1135
|
-
|
1136
|
-
|
1137
|
-
|
1138
|
-
|
1139
|
-
|
1140
|
-
|
1141
|
-
|
1142
|
-
|
1143
|
-
|
1303
|
+
async read(a) {
|
1304
|
+
if (Array.isArray(a)) {
|
1305
|
+
if (a.length === 0)
|
1306
|
+
return [];
|
1307
|
+
const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
|
1308
|
+
return this.getAll({ filter: { id: { $any: ids } } });
|
1309
|
+
}
|
1310
|
+
const id = isString(a) ? a : a.id;
|
1311
|
+
if (isString(id)) {
|
1312
|
+
const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, id);
|
1313
|
+
if (cacheRecord)
|
1314
|
+
return cacheRecord;
|
1315
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1316
|
+
try {
|
1317
|
+
const response = await getRecord({
|
1318
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
|
1319
|
+
...fetchProps
|
1320
|
+
});
|
1321
|
+
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1322
|
+
return initObject(this.db, schema, __privateGet$4(this, _table), response);
|
1323
|
+
} catch (e) {
|
1324
|
+
if (isObject(e) && e.status === 404) {
|
1325
|
+
return null;
|
1326
|
+
}
|
1327
|
+
throw e;
|
1144
1328
|
}
|
1145
|
-
throw e;
|
1146
1329
|
}
|
1147
1330
|
}
|
1148
1331
|
async update(a, b) {
|
1149
1332
|
if (Array.isArray(a)) {
|
1333
|
+
if (a.length === 0)
|
1334
|
+
return [];
|
1150
1335
|
if (a.length > 100) {
|
1151
1336
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1152
1337
|
}
|
1153
1338
|
return Promise.all(a.map((object) => this.update(object)));
|
1154
1339
|
}
|
1155
1340
|
if (isString(a) && isObject(b)) {
|
1156
|
-
|
1341
|
+
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
|
1342
|
+
const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b);
|
1343
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1344
|
+
return record;
|
1157
1345
|
}
|
1158
1346
|
if (isObject(a) && isString(a.id)) {
|
1159
|
-
|
1347
|
+
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
|
1348
|
+
const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
|
1349
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1350
|
+
return record;
|
1160
1351
|
}
|
1161
1352
|
throw new Error("Invalid arguments for update method");
|
1162
1353
|
}
|
1163
1354
|
async createOrUpdate(a, b) {
|
1164
1355
|
if (Array.isArray(a)) {
|
1356
|
+
if (a.length === 0)
|
1357
|
+
return [];
|
1165
1358
|
if (a.length > 100) {
|
1166
1359
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1167
1360
|
}
|
1168
1361
|
return Promise.all(a.map((object) => this.createOrUpdate(object)));
|
1169
1362
|
}
|
1170
1363
|
if (isString(a) && isObject(b)) {
|
1171
|
-
|
1364
|
+
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
|
1365
|
+
const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b);
|
1366
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1367
|
+
return record;
|
1172
1368
|
}
|
1173
1369
|
if (isObject(a) && isString(a.id)) {
|
1174
|
-
|
1370
|
+
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
|
1371
|
+
const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
|
1372
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1373
|
+
return record;
|
1175
1374
|
}
|
1176
1375
|
throw new Error("Invalid arguments for createOrUpdate method");
|
1177
1376
|
}
|
1178
|
-
async delete(
|
1179
|
-
if (Array.isArray(
|
1180
|
-
if (
|
1377
|
+
async delete(a) {
|
1378
|
+
if (Array.isArray(a)) {
|
1379
|
+
if (a.length === 0)
|
1380
|
+
return;
|
1381
|
+
if (a.length > 100) {
|
1181
1382
|
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
1182
1383
|
}
|
1183
|
-
await Promise.all(
|
1384
|
+
await Promise.all(a.map((id) => this.delete(id)));
|
1184
1385
|
return;
|
1185
1386
|
}
|
1186
|
-
if (isString(
|
1187
|
-
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this,
|
1387
|
+
if (isString(a)) {
|
1388
|
+
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a);
|
1389
|
+
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
|
1188
1390
|
return;
|
1189
1391
|
}
|
1190
|
-
if (isObject(
|
1191
|
-
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this,
|
1392
|
+
if (isObject(a) && isString(a.id)) {
|
1393
|
+
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id);
|
1394
|
+
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
|
1192
1395
|
return;
|
1193
1396
|
}
|
1194
1397
|
throw new Error("Invalid arguments for delete method");
|
1195
1398
|
}
|
1196
1399
|
async search(query, options = {}) {
|
1197
|
-
const fetchProps = await __privateGet$
|
1198
|
-
const { records } = await
|
1199
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1200
|
-
body: {
|
1400
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1401
|
+
const { records } = await searchTable({
|
1402
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1403
|
+
body: {
|
1404
|
+
query,
|
1405
|
+
fuzziness: options.fuzziness,
|
1406
|
+
highlight: options.highlight,
|
1407
|
+
filter: options.filter
|
1408
|
+
},
|
1201
1409
|
...fetchProps
|
1202
1410
|
});
|
1203
|
-
|
1411
|
+
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1412
|
+
return records.map((item) => initObject(this.db, schema, __privateGet$4(this, _table), item));
|
1204
1413
|
}
|
1205
1414
|
async query(query) {
|
1415
|
+
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
1416
|
+
if (cacheQuery)
|
1417
|
+
return new Page(query, cacheQuery.meta, cacheQuery.records);
|
1206
1418
|
const data = query.getQueryOptions();
|
1207
1419
|
const body = {
|
1208
1420
|
filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
|
1209
|
-
sort: data.sort ? buildSortFilter(data.sort) : void 0,
|
1210
|
-
page: data.
|
1421
|
+
sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
|
1422
|
+
page: data.pagination,
|
1211
1423
|
columns: data.columns
|
1212
1424
|
};
|
1213
|
-
const fetchProps = await __privateGet$
|
1425
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1214
1426
|
const { meta, records: objects } = await queryTable({
|
1215
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1427
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1216
1428
|
body,
|
1217
1429
|
...fetchProps
|
1218
1430
|
});
|
1219
|
-
const
|
1431
|
+
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1432
|
+
const records = objects.map((record) => initObject(this.db, schema, __privateGet$4(this, _table), record));
|
1433
|
+
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1220
1434
|
return new Page(query, meta, records);
|
1221
1435
|
}
|
1222
1436
|
}
|
1223
1437
|
_table = new WeakMap();
|
1224
|
-
_links = new WeakMap();
|
1225
1438
|
_getFetchProps = new WeakMap();
|
1439
|
+
_cache = new WeakMap();
|
1440
|
+
_schema$1 = new WeakMap();
|
1226
1441
|
_insertRecordWithoutId = new WeakSet();
|
1227
1442
|
insertRecordWithoutId_fn = async function(object) {
|
1228
|
-
const fetchProps = await __privateGet$
|
1443
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1229
1444
|
const record = transformObjectLinks(object);
|
1230
1445
|
const response = await insertRecord({
|
1231
1446
|
pathParams: {
|
1232
1447
|
workspace: "{workspaceId}",
|
1233
1448
|
dbBranchName: "{dbBranch}",
|
1234
|
-
tableName: __privateGet$
|
1449
|
+
tableName: __privateGet$4(this, _table)
|
1235
1450
|
},
|
1236
1451
|
body: record,
|
1237
1452
|
...fetchProps
|
@@ -1244,13 +1459,13 @@ insertRecordWithoutId_fn = async function(object) {
|
|
1244
1459
|
};
|
1245
1460
|
_insertRecordWithId = new WeakSet();
|
1246
1461
|
insertRecordWithId_fn = async function(recordId, object) {
|
1247
|
-
const fetchProps = await __privateGet$
|
1462
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1248
1463
|
const record = transformObjectLinks(object);
|
1249
1464
|
const response = await insertRecordWithID({
|
1250
1465
|
pathParams: {
|
1251
1466
|
workspace: "{workspaceId}",
|
1252
1467
|
dbBranchName: "{dbBranch}",
|
1253
|
-
tableName: __privateGet$
|
1468
|
+
tableName: __privateGet$4(this, _table),
|
1254
1469
|
recordId
|
1255
1470
|
},
|
1256
1471
|
body: record,
|
@@ -1265,25 +1480,29 @@ insertRecordWithId_fn = async function(recordId, object) {
|
|
1265
1480
|
};
|
1266
1481
|
_bulkInsertTableRecords = new WeakSet();
|
1267
1482
|
bulkInsertTableRecords_fn = async function(objects) {
|
1268
|
-
const fetchProps = await __privateGet$
|
1483
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1269
1484
|
const records = objects.map((object) => transformObjectLinks(object));
|
1270
|
-
const
|
1271
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1485
|
+
const { recordIDs } = await bulkInsertTableRecords({
|
1486
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1272
1487
|
body: { records },
|
1273
1488
|
...fetchProps
|
1274
1489
|
});
|
1275
|
-
const finalObjects = await this.
|
1490
|
+
const finalObjects = await this.read(recordIDs);
|
1276
1491
|
if (finalObjects.length !== objects.length) {
|
1277
1492
|
throw new Error("The server failed to save some records");
|
1278
1493
|
}
|
1279
|
-
|
1494
|
+
const dictionary = finalObjects.reduce((acc, object) => {
|
1495
|
+
acc[object.id] = object;
|
1496
|
+
return acc;
|
1497
|
+
}, {});
|
1498
|
+
return recordIDs.map((id) => dictionary[id]);
|
1280
1499
|
};
|
1281
1500
|
_updateRecordWithID = new WeakSet();
|
1282
1501
|
updateRecordWithID_fn = async function(recordId, object) {
|
1283
|
-
const fetchProps = await __privateGet$
|
1502
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1284
1503
|
const record = transformObjectLinks(object);
|
1285
1504
|
const response = await updateRecordWithID({
|
1286
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1505
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1287
1506
|
body: record,
|
1288
1507
|
...fetchProps
|
1289
1508
|
});
|
@@ -1294,9 +1513,9 @@ updateRecordWithID_fn = async function(recordId, object) {
|
|
1294
1513
|
};
|
1295
1514
|
_upsertRecordWithID = new WeakSet();
|
1296
1515
|
upsertRecordWithID_fn = async function(recordId, object) {
|
1297
|
-
const fetchProps = await __privateGet$
|
1516
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1298
1517
|
const response = await upsertRecordWithID({
|
1299
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1518
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1300
1519
|
body: object,
|
1301
1520
|
...fetchProps
|
1302
1521
|
});
|
@@ -1307,12 +1526,63 @@ upsertRecordWithID_fn = async function(recordId, object) {
|
|
1307
1526
|
};
|
1308
1527
|
_deleteRecord = new WeakSet();
|
1309
1528
|
deleteRecord_fn = async function(recordId) {
|
1310
|
-
const fetchProps = await __privateGet$
|
1529
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1311
1530
|
await deleteRecord({
|
1312
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1531
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1313
1532
|
...fetchProps
|
1314
1533
|
});
|
1315
1534
|
};
|
1535
|
+
_invalidateCache = new WeakSet();
|
1536
|
+
invalidateCache_fn = async function(recordId) {
|
1537
|
+
await __privateGet$4(this, _cache).delete(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1538
|
+
const cacheItems = await __privateGet$4(this, _cache).getAll();
|
1539
|
+
const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
|
1540
|
+
for (const [key, value] of queries) {
|
1541
|
+
const ids = getIds(value);
|
1542
|
+
if (ids.includes(recordId))
|
1543
|
+
await __privateGet$4(this, _cache).delete(key);
|
1544
|
+
}
|
1545
|
+
};
|
1546
|
+
_setCacheRecord = new WeakSet();
|
1547
|
+
setCacheRecord_fn = async function(record) {
|
1548
|
+
if (!__privateGet$4(this, _cache).cacheRecords)
|
1549
|
+
return;
|
1550
|
+
await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
|
1551
|
+
};
|
1552
|
+
_getCacheRecord = new WeakSet();
|
1553
|
+
getCacheRecord_fn = async function(recordId) {
|
1554
|
+
if (!__privateGet$4(this, _cache).cacheRecords)
|
1555
|
+
return null;
|
1556
|
+
return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1557
|
+
};
|
1558
|
+
_setCacheQuery = new WeakSet();
|
1559
|
+
setCacheQuery_fn = async function(query, meta, records) {
|
1560
|
+
await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
|
1561
|
+
};
|
1562
|
+
_getCacheQuery = new WeakSet();
|
1563
|
+
getCacheQuery_fn = async function(query) {
|
1564
|
+
const key = `query_${__privateGet$4(this, _table)}:${query.key()}`;
|
1565
|
+
const result = await __privateGet$4(this, _cache).get(key);
|
1566
|
+
if (!result)
|
1567
|
+
return null;
|
1568
|
+
const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
|
1569
|
+
if (ttl < 0)
|
1570
|
+
return null;
|
1571
|
+
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1572
|
+
return hasExpired ? null : result;
|
1573
|
+
};
|
1574
|
+
_getSchema$1 = new WeakSet();
|
1575
|
+
getSchema_fn$1 = async function() {
|
1576
|
+
if (__privateGet$4(this, _schema$1))
|
1577
|
+
return __privateGet$4(this, _schema$1);
|
1578
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1579
|
+
const { schema } = await getBranchDetails({
|
1580
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1581
|
+
...fetchProps
|
1582
|
+
});
|
1583
|
+
__privateSet$3(this, _schema$1, schema);
|
1584
|
+
return schema;
|
1585
|
+
};
|
1316
1586
|
const transformObjectLinks = (object) => {
|
1317
1587
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
1318
1588
|
if (key === "xata")
|
@@ -1320,15 +1590,34 @@ const transformObjectLinks = (object) => {
|
|
1320
1590
|
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1321
1591
|
}, {});
|
1322
1592
|
};
|
1323
|
-
const initObject = (db,
|
1593
|
+
const initObject = (db, schema, table, object) => {
|
1324
1594
|
const result = {};
|
1325
|
-
|
1326
|
-
|
1327
|
-
|
1328
|
-
|
1329
|
-
|
1330
|
-
|
1331
|
-
|
1595
|
+
const { xata, ...rest } = object ?? {};
|
1596
|
+
Object.assign(result, rest);
|
1597
|
+
const { columns } = schema.tables.find(({ name }) => name === table) ?? {};
|
1598
|
+
if (!columns)
|
1599
|
+
console.error(`Table ${table} not found in schema`);
|
1600
|
+
for (const column of columns ?? []) {
|
1601
|
+
const value = result[column.name];
|
1602
|
+
switch (column.type) {
|
1603
|
+
case "datetime": {
|
1604
|
+
const date = value !== void 0 ? new Date(value) : void 0;
|
1605
|
+
if (date && isNaN(date.getTime())) {
|
1606
|
+
console.error(`Failed to parse date ${value} for field ${column.name}`);
|
1607
|
+
} else if (date) {
|
1608
|
+
result[column.name] = date;
|
1609
|
+
}
|
1610
|
+
break;
|
1611
|
+
}
|
1612
|
+
case "link": {
|
1613
|
+
const linkTable = column.link?.table;
|
1614
|
+
if (!linkTable) {
|
1615
|
+
console.error(`Failed to parse link for field ${column.name}`);
|
1616
|
+
} else if (isObject(value)) {
|
1617
|
+
result[column.name] = initObject(db, schema, linkTable, value);
|
1618
|
+
}
|
1619
|
+
break;
|
1620
|
+
}
|
1332
1621
|
}
|
1333
1622
|
}
|
1334
1623
|
result.read = function() {
|
@@ -1340,12 +1629,74 @@ const initObject = (db, links, table, object) => {
|
|
1340
1629
|
result.delete = function() {
|
1341
1630
|
return db[table].delete(result["id"]);
|
1342
1631
|
};
|
1343
|
-
|
1632
|
+
result.getMetadata = function() {
|
1633
|
+
return xata;
|
1634
|
+
};
|
1635
|
+
for (const prop of ["read", "update", "delete", "getMetadata"]) {
|
1344
1636
|
Object.defineProperty(result, prop, { enumerable: false });
|
1345
1637
|
}
|
1346
1638
|
Object.freeze(result);
|
1347
1639
|
return result;
|
1348
1640
|
};
|
1641
|
+
function getIds(value) {
|
1642
|
+
if (Array.isArray(value)) {
|
1643
|
+
return value.map((item) => getIds(item)).flat();
|
1644
|
+
}
|
1645
|
+
if (!isObject(value))
|
1646
|
+
return [];
|
1647
|
+
const nestedIds = Object.values(value).map((item) => getIds(item)).flat();
|
1648
|
+
return isString(value.id) ? [value.id, ...nestedIds] : nestedIds;
|
1649
|
+
}
|
1650
|
+
|
1651
|
+
var __accessCheck$3 = (obj, member, msg) => {
|
1652
|
+
if (!member.has(obj))
|
1653
|
+
throw TypeError("Cannot " + msg);
|
1654
|
+
};
|
1655
|
+
var __privateGet$3 = (obj, member, getter) => {
|
1656
|
+
__accessCheck$3(obj, member, "read from private field");
|
1657
|
+
return getter ? getter.call(obj) : member.get(obj);
|
1658
|
+
};
|
1659
|
+
var __privateAdd$3 = (obj, member, value) => {
|
1660
|
+
if (member.has(obj))
|
1661
|
+
throw TypeError("Cannot add the same private member more than once");
|
1662
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1663
|
+
};
|
1664
|
+
var __privateSet$2 = (obj, member, value, setter) => {
|
1665
|
+
__accessCheck$3(obj, member, "write to private field");
|
1666
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1667
|
+
return value;
|
1668
|
+
};
|
1669
|
+
var _map;
|
1670
|
+
class SimpleCache {
|
1671
|
+
constructor(options = {}) {
|
1672
|
+
__privateAdd$3(this, _map, void 0);
|
1673
|
+
__privateSet$2(this, _map, /* @__PURE__ */ new Map());
|
1674
|
+
this.capacity = options.max ?? 500;
|
1675
|
+
this.cacheRecords = options.cacheRecords ?? true;
|
1676
|
+
this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
|
1677
|
+
}
|
1678
|
+
async getAll() {
|
1679
|
+
return Object.fromEntries(__privateGet$3(this, _map));
|
1680
|
+
}
|
1681
|
+
async get(key) {
|
1682
|
+
return __privateGet$3(this, _map).get(key) ?? null;
|
1683
|
+
}
|
1684
|
+
async set(key, value) {
|
1685
|
+
await this.delete(key);
|
1686
|
+
__privateGet$3(this, _map).set(key, value);
|
1687
|
+
if (__privateGet$3(this, _map).size > this.capacity) {
|
1688
|
+
const leastRecentlyUsed = __privateGet$3(this, _map).keys().next().value;
|
1689
|
+
await this.delete(leastRecentlyUsed);
|
1690
|
+
}
|
1691
|
+
}
|
1692
|
+
async delete(key) {
|
1693
|
+
__privateGet$3(this, _map).delete(key);
|
1694
|
+
}
|
1695
|
+
async clear() {
|
1696
|
+
return __privateGet$3(this, _map).clear();
|
1697
|
+
}
|
1698
|
+
}
|
1699
|
+
_map = new WeakMap();
|
1349
1700
|
|
1350
1701
|
const gt = (value) => ({ $gt: value });
|
1351
1702
|
const ge = (value) => ({ $ge: value });
|
@@ -1370,7 +1721,7 @@ var __accessCheck$2 = (obj, member, msg) => {
|
|
1370
1721
|
if (!member.has(obj))
|
1371
1722
|
throw TypeError("Cannot " + msg);
|
1372
1723
|
};
|
1373
|
-
var __privateGet$
|
1724
|
+
var __privateGet$2 = (obj, member, getter) => {
|
1374
1725
|
__accessCheck$2(obj, member, "read from private field");
|
1375
1726
|
return getter ? getter.call(obj) : member.get(obj);
|
1376
1727
|
};
|
@@ -1381,26 +1732,24 @@ var __privateAdd$2 = (obj, member, value) => {
|
|
1381
1732
|
};
|
1382
1733
|
var _tables;
|
1383
1734
|
class SchemaPlugin extends XataPlugin {
|
1384
|
-
constructor(
|
1735
|
+
constructor(tableNames) {
|
1385
1736
|
super();
|
1386
|
-
this.links = links;
|
1387
1737
|
this.tableNames = tableNames;
|
1388
1738
|
__privateAdd$2(this, _tables, {});
|
1389
1739
|
}
|
1390
|
-
build(
|
1391
|
-
const { getFetchProps } = options;
|
1392
|
-
const links = this.links;
|
1740
|
+
build(pluginOptions) {
|
1393
1741
|
const db = new Proxy({}, {
|
1394
1742
|
get: (_target, table) => {
|
1395
1743
|
if (!isString(table))
|
1396
1744
|
throw new Error("Invalid table name");
|
1397
|
-
if (
|
1398
|
-
__privateGet$
|
1399
|
-
|
1745
|
+
if (__privateGet$2(this, _tables)[table] === void 0) {
|
1746
|
+
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
|
1747
|
+
}
|
1748
|
+
return __privateGet$2(this, _tables)[table];
|
1400
1749
|
}
|
1401
1750
|
});
|
1402
1751
|
for (const table of this.tableNames ?? []) {
|
1403
|
-
db[table] = new RestRepository({ db,
|
1752
|
+
db[table] = new RestRepository({ db, pluginOptions, table });
|
1404
1753
|
}
|
1405
1754
|
return db;
|
1406
1755
|
}
|
@@ -1411,55 +1760,80 @@ var __accessCheck$1 = (obj, member, msg) => {
|
|
1411
1760
|
if (!member.has(obj))
|
1412
1761
|
throw TypeError("Cannot " + msg);
|
1413
1762
|
};
|
1763
|
+
var __privateGet$1 = (obj, member, getter) => {
|
1764
|
+
__accessCheck$1(obj, member, "read from private field");
|
1765
|
+
return getter ? getter.call(obj) : member.get(obj);
|
1766
|
+
};
|
1414
1767
|
var __privateAdd$1 = (obj, member, value) => {
|
1415
1768
|
if (member.has(obj))
|
1416
1769
|
throw TypeError("Cannot add the same private member more than once");
|
1417
1770
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1418
1771
|
};
|
1772
|
+
var __privateSet$1 = (obj, member, value, setter) => {
|
1773
|
+
__accessCheck$1(obj, member, "write to private field");
|
1774
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1775
|
+
return value;
|
1776
|
+
};
|
1419
1777
|
var __privateMethod$1 = (obj, member, method) => {
|
1420
1778
|
__accessCheck$1(obj, member, "access private method");
|
1421
1779
|
return method;
|
1422
1780
|
};
|
1423
|
-
var _search, search_fn;
|
1781
|
+
var _schema, _search, search_fn, _getSchema, getSchema_fn;
|
1424
1782
|
class SearchPlugin extends XataPlugin {
|
1425
|
-
constructor(db
|
1783
|
+
constructor(db) {
|
1426
1784
|
super();
|
1427
1785
|
this.db = db;
|
1428
|
-
this.links = links;
|
1429
1786
|
__privateAdd$1(this, _search);
|
1787
|
+
__privateAdd$1(this, _getSchema);
|
1788
|
+
__privateAdd$1(this, _schema, void 0);
|
1430
1789
|
}
|
1431
1790
|
build({ getFetchProps }) {
|
1432
1791
|
return {
|
1433
1792
|
all: async (query, options = {}) => {
|
1434
1793
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1794
|
+
const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
|
1435
1795
|
return records.map((record) => {
|
1436
1796
|
const { table = "orphan" } = record.xata;
|
1437
|
-
return { table, record: initObject(this.db,
|
1797
|
+
return { table, record: initObject(this.db, schema, table, record) };
|
1438
1798
|
});
|
1439
1799
|
},
|
1440
1800
|
byTable: async (query, options = {}) => {
|
1441
1801
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1802
|
+
const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
|
1442
1803
|
return records.reduce((acc, record) => {
|
1443
1804
|
const { table = "orphan" } = record.xata;
|
1444
1805
|
const items = acc[table] ?? [];
|
1445
|
-
const item = initObject(this.db,
|
1806
|
+
const item = initObject(this.db, schema, table, record);
|
1446
1807
|
return { ...acc, [table]: [...items, item] };
|
1447
1808
|
}, {});
|
1448
1809
|
}
|
1449
1810
|
};
|
1450
1811
|
}
|
1451
1812
|
}
|
1813
|
+
_schema = new WeakMap();
|
1452
1814
|
_search = new WeakSet();
|
1453
1815
|
search_fn = async function(query, options, getFetchProps) {
|
1454
1816
|
const fetchProps = await getFetchProps();
|
1455
|
-
const { tables, fuzziness } = options ?? {};
|
1817
|
+
const { tables, fuzziness, highlight } = options ?? {};
|
1456
1818
|
const { records } = await searchBranch({
|
1457
1819
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1458
|
-
body: { tables, query, fuzziness },
|
1820
|
+
body: { tables, query, fuzziness, highlight },
|
1459
1821
|
...fetchProps
|
1460
1822
|
});
|
1461
1823
|
return records;
|
1462
1824
|
};
|
1825
|
+
_getSchema = new WeakSet();
|
1826
|
+
getSchema_fn = async function(getFetchProps) {
|
1827
|
+
if (__privateGet$1(this, _schema))
|
1828
|
+
return __privateGet$1(this, _schema);
|
1829
|
+
const fetchProps = await getFetchProps();
|
1830
|
+
const { schema } = await getBranchDetails({
|
1831
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1832
|
+
...fetchProps
|
1833
|
+
});
|
1834
|
+
__privateSet$1(this, _schema, schema);
|
1835
|
+
return schema;
|
1836
|
+
};
|
1463
1837
|
|
1464
1838
|
const isBranchStrategyBuilder = (strategy) => {
|
1465
1839
|
return typeof strategy === "function";
|
@@ -1471,30 +1845,39 @@ const envBranchNames = [
|
|
1471
1845
|
"CF_PAGES_BRANCH",
|
1472
1846
|
"BRANCH"
|
1473
1847
|
];
|
1474
|
-
const defaultBranch = "main";
|
1475
1848
|
async function getCurrentBranchName(options) {
|
1476
|
-
const env =
|
1477
|
-
if (env)
|
1478
|
-
|
1479
|
-
|
1480
|
-
|
1481
|
-
|
1482
|
-
|
1483
|
-
|
1484
|
-
|
1485
|
-
return defaultBranch;
|
1849
|
+
const env = getBranchByEnvVariable();
|
1850
|
+
if (env) {
|
1851
|
+
const details = await getDatabaseBranch(env, options);
|
1852
|
+
if (details)
|
1853
|
+
return env;
|
1854
|
+
console.warn(`Branch ${env} not found in Xata. Ignoring...`);
|
1855
|
+
}
|
1856
|
+
const gitBranch = await getGitBranch();
|
1857
|
+
return resolveXataBranch(gitBranch, options);
|
1486
1858
|
}
|
1487
1859
|
async function getCurrentBranchDetails(options) {
|
1488
|
-
const
|
1489
|
-
|
1490
|
-
|
1491
|
-
|
1492
|
-
|
1493
|
-
|
1494
|
-
|
1495
|
-
|
1496
|
-
|
1497
|
-
|
1860
|
+
const branch = await getCurrentBranchName(options);
|
1861
|
+
return getDatabaseBranch(branch, options);
|
1862
|
+
}
|
1863
|
+
async function resolveXataBranch(gitBranch, options) {
|
1864
|
+
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1865
|
+
const apiKey = options?.apiKey || getAPIKey();
|
1866
|
+
if (!databaseURL)
|
1867
|
+
throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
|
1868
|
+
if (!apiKey)
|
1869
|
+
throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
|
1870
|
+
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1871
|
+
const [workspace] = host.split(".");
|
1872
|
+
const { branch } = await resolveBranch({
|
1873
|
+
apiKey,
|
1874
|
+
apiUrl: databaseURL,
|
1875
|
+
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1876
|
+
workspacesApiUrl: `${protocol}//${host}`,
|
1877
|
+
pathParams: { dbName, workspace },
|
1878
|
+
queryParams: { gitBranch, fallbackBranch: getEnvVariable("XATA_FALLBACK_BRANCH") }
|
1879
|
+
});
|
1880
|
+
return branch;
|
1498
1881
|
}
|
1499
1882
|
async function getDatabaseBranch(branch, options) {
|
1500
1883
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
@@ -1512,10 +1895,7 @@ async function getDatabaseBranch(branch, options) {
|
|
1512
1895
|
apiUrl: databaseURL,
|
1513
1896
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1514
1897
|
workspacesApiUrl: `${protocol}//${host}`,
|
1515
|
-
pathParams: {
|
1516
|
-
dbBranchName,
|
1517
|
-
workspace
|
1518
|
-
}
|
1898
|
+
pathParams: { dbBranchName, workspace }
|
1519
1899
|
});
|
1520
1900
|
} catch (err) {
|
1521
1901
|
if (isObject(err) && err.status === 404)
|
@@ -1568,22 +1948,24 @@ var __privateMethod = (obj, member, method) => {
|
|
1568
1948
|
const buildClient = (plugins) => {
|
1569
1949
|
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1570
1950
|
return _a = class {
|
1571
|
-
constructor(options = {},
|
1951
|
+
constructor(options = {}, tables) {
|
1572
1952
|
__privateAdd(this, _parseOptions);
|
1573
1953
|
__privateAdd(this, _getFetchProps);
|
1574
1954
|
__privateAdd(this, _evaluateBranch);
|
1575
1955
|
__privateAdd(this, _branch, void 0);
|
1576
1956
|
const safeOptions = __privateMethod(this, _parseOptions, parseOptions_fn).call(this, options);
|
1577
|
-
const
|
1578
|
-
|
1579
|
-
|
1580
|
-
}
|
1957
|
+
const pluginOptions = {
|
1958
|
+
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
1959
|
+
cache: safeOptions.cache
|
1960
|
+
};
|
1961
|
+
const db = new SchemaPlugin(tables).build(pluginOptions);
|
1962
|
+
const search = new SearchPlugin(db).build(pluginOptions);
|
1581
1963
|
this.db = db;
|
1582
1964
|
this.search = search;
|
1583
1965
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
1584
|
-
if (
|
1966
|
+
if (namespace === void 0)
|
1585
1967
|
continue;
|
1586
|
-
const result = namespace.build(
|
1968
|
+
const result = namespace.build(pluginOptions);
|
1587
1969
|
if (result instanceof Promise) {
|
1588
1970
|
void result.then((namespace2) => {
|
1589
1971
|
this[key] = namespace2;
|
@@ -1597,11 +1979,12 @@ const buildClient = (plugins) => {
|
|
1597
1979
|
const fetch = getFetchImplementation(options?.fetch);
|
1598
1980
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1599
1981
|
const apiKey = options?.apiKey || getAPIKey();
|
1600
|
-
const
|
1982
|
+
const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
|
1983
|
+
const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
1601
1984
|
if (!databaseURL || !apiKey) {
|
1602
1985
|
throw new Error("Options databaseURL and apiKey are required");
|
1603
1986
|
}
|
1604
|
-
return { fetch, databaseURL, apiKey, branch };
|
1987
|
+
return { fetch, databaseURL, apiKey, branch, cache };
|
1605
1988
|
}, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({
|
1606
1989
|
fetch,
|
1607
1990
|
apiKey,
|
@@ -1624,7 +2007,7 @@ const buildClient = (plugins) => {
|
|
1624
2007
|
}, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
|
1625
2008
|
if (__privateGet(this, _branch))
|
1626
2009
|
return __privateGet(this, _branch);
|
1627
|
-
if (
|
2010
|
+
if (param === void 0)
|
1628
2011
|
return void 0;
|
1629
2012
|
const strategies = Array.isArray(param) ? [...param] : [param];
|
1630
2013
|
const evaluateBranch = async (strategy) => {
|
@@ -1649,5 +2032,5 @@ class XataError extends Error {
|
|
1649
2032
|
}
|
1650
2033
|
}
|
1651
2034
|
|
1652
|
-
export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, Repository, RestRepository, SchemaPlugin, SearchPlugin, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addTableColumn, buildClient, bulkInsertTableRecords, cancelWorkspaceMemberInvite, contains, createBranch, createDatabase, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, endsWith, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseURL, getRecord, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getWorkspace, getWorkspaceMembersList, getWorkspacesList, gt, gte, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isIdentifiable, isNot, isXataRecord, le, lt, lte, notExists, operationsByTag, pattern, queryTable, removeWorkspaceMember, resendWorkspaceMemberInvite, searchBranch, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
|
2035
|
+
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, updateWorkspaceMemberRole, upsertRecordWithID };
|
1653
2036
|
//# sourceMappingURL=index.mjs.map
|