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