@xata.io/client 0.0.0-alpha.vf2696e7 → 0.0.0-alpha.vf344dbc
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 +96 -0
- package/README.md +265 -1
- package/dist/index.cjs +416 -208
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +514 -79
- package/dist/index.mjs +410 -209
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -2
- package/tsconfig.json +1 -0
package/dist/index.cjs
CHANGED
@@ -11,8 +11,11 @@ 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";
|
16
19
|
}
|
17
20
|
function toBase64(value) {
|
18
21
|
try {
|
@@ -38,7 +41,10 @@ function getEnvVariable(name) {
|
|
38
41
|
}
|
39
42
|
async function getGitBranch() {
|
40
43
|
try {
|
41
|
-
|
44
|
+
if (typeof require === "function") {
|
45
|
+
const req = require;
|
46
|
+
return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
|
47
|
+
}
|
42
48
|
} catch (err) {
|
43
49
|
}
|
44
50
|
try {
|
@@ -71,7 +77,12 @@ function getFetchImplementation(userFetch) {
|
|
71
77
|
return fetchImpl;
|
72
78
|
}
|
73
79
|
|
74
|
-
class
|
80
|
+
class ErrorWithCause extends Error {
|
81
|
+
constructor(message, options) {
|
82
|
+
super(message, options);
|
83
|
+
}
|
84
|
+
}
|
85
|
+
class FetcherError extends ErrorWithCause {
|
75
86
|
constructor(status, data) {
|
76
87
|
super(getMessage(data));
|
77
88
|
this.status = status;
|
@@ -252,6 +263,14 @@ const deleteDatabase = (variables) => fetch$1({
|
|
252
263
|
method: "delete",
|
253
264
|
...variables
|
254
265
|
});
|
266
|
+
const getGitBranchesMapping = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "get", ...variables });
|
267
|
+
const addGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "post", ...variables });
|
268
|
+
const removeGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "delete", ...variables });
|
269
|
+
const resolveBranch = (variables) => fetch$1({
|
270
|
+
url: "/dbs/{dbName}/resolveBranch",
|
271
|
+
method: "get",
|
272
|
+
...variables
|
273
|
+
});
|
255
274
|
const getBranchDetails = (variables) => fetch$1({
|
256
275
|
url: "/db/{dbBranchName}",
|
257
276
|
method: "get",
|
@@ -359,6 +378,11 @@ const queryTable = (variables) => fetch$1({
|
|
359
378
|
method: "post",
|
360
379
|
...variables
|
361
380
|
});
|
381
|
+
const searchTable = (variables) => fetch$1({
|
382
|
+
url: "/db/{dbBranchName}/tables/{tableName}/search",
|
383
|
+
method: "post",
|
384
|
+
...variables
|
385
|
+
});
|
362
386
|
const searchBranch = (variables) => fetch$1({
|
363
387
|
url: "/db/{dbBranchName}/search",
|
364
388
|
method: "post",
|
@@ -380,7 +404,15 @@ const operationsByTag = {
|
|
380
404
|
resendWorkspaceMemberInvite,
|
381
405
|
acceptWorkspaceMemberInvite
|
382
406
|
},
|
383
|
-
database: {
|
407
|
+
database: {
|
408
|
+
getDatabaseList,
|
409
|
+
createDatabase,
|
410
|
+
deleteDatabase,
|
411
|
+
getGitBranchesMapping,
|
412
|
+
addGitBranchesEntry,
|
413
|
+
removeGitBranchesEntry,
|
414
|
+
resolveBranch
|
415
|
+
},
|
384
416
|
branch: {
|
385
417
|
getBranchList,
|
386
418
|
getBranchDetails,
|
@@ -414,6 +446,7 @@ const operationsByTag = {
|
|
414
446
|
getRecord,
|
415
447
|
bulkInsertTableRecords,
|
416
448
|
queryTable,
|
449
|
+
searchTable,
|
417
450
|
searchBranch
|
418
451
|
}
|
419
452
|
};
|
@@ -447,7 +480,7 @@ var __accessCheck$7 = (obj, member, msg) => {
|
|
447
480
|
if (!member.has(obj))
|
448
481
|
throw TypeError("Cannot " + msg);
|
449
482
|
};
|
450
|
-
var __privateGet$
|
483
|
+
var __privateGet$7 = (obj, member, getter) => {
|
451
484
|
__accessCheck$7(obj, member, "read from private field");
|
452
485
|
return getter ? getter.call(obj) : member.get(obj);
|
453
486
|
};
|
@@ -456,7 +489,7 @@ var __privateAdd$7 = (obj, member, value) => {
|
|
456
489
|
throw TypeError("Cannot add the same private member more than once");
|
457
490
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
458
491
|
};
|
459
|
-
var __privateSet$
|
492
|
+
var __privateSet$6 = (obj, member, value, setter) => {
|
460
493
|
__accessCheck$7(obj, member, "write to private field");
|
461
494
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
462
495
|
return value;
|
@@ -471,7 +504,7 @@ class XataApiClient {
|
|
471
504
|
if (!apiKey) {
|
472
505
|
throw new Error("Could not resolve a valid apiKey");
|
473
506
|
}
|
474
|
-
__privateSet$
|
507
|
+
__privateSet$6(this, _extraProps, {
|
475
508
|
apiUrl: getHostUrl(provider, "main"),
|
476
509
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
477
510
|
fetchImpl: getFetchImplementation(options.fetch),
|
@@ -479,34 +512,34 @@ class XataApiClient {
|
|
479
512
|
});
|
480
513
|
}
|
481
514
|
get user() {
|
482
|
-
if (!__privateGet$
|
483
|
-
__privateGet$
|
484
|
-
return __privateGet$
|
515
|
+
if (!__privateGet$7(this, _namespaces).user)
|
516
|
+
__privateGet$7(this, _namespaces).user = new UserApi(__privateGet$7(this, _extraProps));
|
517
|
+
return __privateGet$7(this, _namespaces).user;
|
485
518
|
}
|
486
519
|
get workspaces() {
|
487
|
-
if (!__privateGet$
|
488
|
-
__privateGet$
|
489
|
-
return __privateGet$
|
520
|
+
if (!__privateGet$7(this, _namespaces).workspaces)
|
521
|
+
__privateGet$7(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$7(this, _extraProps));
|
522
|
+
return __privateGet$7(this, _namespaces).workspaces;
|
490
523
|
}
|
491
524
|
get databases() {
|
492
|
-
if (!__privateGet$
|
493
|
-
__privateGet$
|
494
|
-
return __privateGet$
|
525
|
+
if (!__privateGet$7(this, _namespaces).databases)
|
526
|
+
__privateGet$7(this, _namespaces).databases = new DatabaseApi(__privateGet$7(this, _extraProps));
|
527
|
+
return __privateGet$7(this, _namespaces).databases;
|
495
528
|
}
|
496
529
|
get branches() {
|
497
|
-
if (!__privateGet$
|
498
|
-
__privateGet$
|
499
|
-
return __privateGet$
|
530
|
+
if (!__privateGet$7(this, _namespaces).branches)
|
531
|
+
__privateGet$7(this, _namespaces).branches = new BranchApi(__privateGet$7(this, _extraProps));
|
532
|
+
return __privateGet$7(this, _namespaces).branches;
|
500
533
|
}
|
501
534
|
get tables() {
|
502
|
-
if (!__privateGet$
|
503
|
-
__privateGet$
|
504
|
-
return __privateGet$
|
535
|
+
if (!__privateGet$7(this, _namespaces).tables)
|
536
|
+
__privateGet$7(this, _namespaces).tables = new TableApi(__privateGet$7(this, _extraProps));
|
537
|
+
return __privateGet$7(this, _namespaces).tables;
|
505
538
|
}
|
506
539
|
get records() {
|
507
|
-
if (!__privateGet$
|
508
|
-
__privateGet$
|
509
|
-
return __privateGet$
|
540
|
+
if (!__privateGet$7(this, _namespaces).records)
|
541
|
+
__privateGet$7(this, _namespaces).records = new RecordsApi(__privateGet$7(this, _extraProps));
|
542
|
+
return __privateGet$7(this, _namespaces).records;
|
510
543
|
}
|
511
544
|
}
|
512
545
|
_extraProps = new WeakMap();
|
@@ -640,6 +673,33 @@ class DatabaseApi {
|
|
640
673
|
...this.extraProps
|
641
674
|
});
|
642
675
|
}
|
676
|
+
getGitBranchesMapping(workspace, dbName) {
|
677
|
+
return operationsByTag.database.getGitBranchesMapping({
|
678
|
+
pathParams: { workspace, dbName },
|
679
|
+
...this.extraProps
|
680
|
+
});
|
681
|
+
}
|
682
|
+
addGitBranchesEntry(workspace, dbName, body) {
|
683
|
+
return operationsByTag.database.addGitBranchesEntry({
|
684
|
+
pathParams: { workspace, dbName },
|
685
|
+
body,
|
686
|
+
...this.extraProps
|
687
|
+
});
|
688
|
+
}
|
689
|
+
removeGitBranchesEntry(workspace, dbName, gitBranch) {
|
690
|
+
return operationsByTag.database.removeGitBranchesEntry({
|
691
|
+
pathParams: { workspace, dbName },
|
692
|
+
queryParams: { gitBranch },
|
693
|
+
...this.extraProps
|
694
|
+
});
|
695
|
+
}
|
696
|
+
resolveBranch(workspace, dbName, gitBranch) {
|
697
|
+
return operationsByTag.database.resolveBranch({
|
698
|
+
pathParams: { workspace, dbName },
|
699
|
+
queryParams: { gitBranch },
|
700
|
+
...this.extraProps
|
701
|
+
});
|
702
|
+
}
|
643
703
|
}
|
644
704
|
class BranchApi {
|
645
705
|
constructor(extraProps) {
|
@@ -657,10 +717,10 @@ class BranchApi {
|
|
657
717
|
...this.extraProps
|
658
718
|
});
|
659
719
|
}
|
660
|
-
createBranch(workspace, database, branch, from
|
720
|
+
createBranch(workspace, database, branch, from, options = {}) {
|
661
721
|
return operationsByTag.branch.createBranch({
|
662
722
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
663
|
-
queryParams: { from },
|
723
|
+
queryParams: isString(from) ? { from } : void 0,
|
664
724
|
body: options,
|
665
725
|
...this.extraProps
|
666
726
|
});
|
@@ -842,6 +902,13 @@ class RecordsApi {
|
|
842
902
|
...this.extraProps
|
843
903
|
});
|
844
904
|
}
|
905
|
+
searchTable(workspace, database, branch, tableName, query) {
|
906
|
+
return operationsByTag.records.searchTable({
|
907
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
908
|
+
body: query,
|
909
|
+
...this.extraProps
|
910
|
+
});
|
911
|
+
}
|
845
912
|
searchBranch(workspace, database, branch, query) {
|
846
913
|
return operationsByTag.records.searchBranch({
|
847
914
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
@@ -865,7 +932,7 @@ var __accessCheck$6 = (obj, member, msg) => {
|
|
865
932
|
if (!member.has(obj))
|
866
933
|
throw TypeError("Cannot " + msg);
|
867
934
|
};
|
868
|
-
var __privateGet$
|
935
|
+
var __privateGet$6 = (obj, member, getter) => {
|
869
936
|
__accessCheck$6(obj, member, "read from private field");
|
870
937
|
return getter ? getter.call(obj) : member.get(obj);
|
871
938
|
};
|
@@ -874,30 +941,30 @@ var __privateAdd$6 = (obj, member, value) => {
|
|
874
941
|
throw TypeError("Cannot add the same private member more than once");
|
875
942
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
876
943
|
};
|
877
|
-
var __privateSet$
|
944
|
+
var __privateSet$5 = (obj, member, value, setter) => {
|
878
945
|
__accessCheck$6(obj, member, "write to private field");
|
879
946
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
880
947
|
return value;
|
881
948
|
};
|
882
|
-
var _query;
|
949
|
+
var _query, _page;
|
883
950
|
class Page {
|
884
951
|
constructor(query, meta, records = []) {
|
885
952
|
__privateAdd$6(this, _query, void 0);
|
886
|
-
__privateSet$
|
953
|
+
__privateSet$5(this, _query, query);
|
887
954
|
this.meta = meta;
|
888
|
-
this.records = records;
|
955
|
+
this.records = new RecordArray(this, records);
|
889
956
|
}
|
890
957
|
async nextPage(size, offset) {
|
891
|
-
return __privateGet$
|
958
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
|
892
959
|
}
|
893
960
|
async previousPage(size, offset) {
|
894
|
-
return __privateGet$
|
961
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, before: this.meta.page.cursor } });
|
895
962
|
}
|
896
963
|
async firstPage(size, offset) {
|
897
|
-
return __privateGet$
|
964
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, first: this.meta.page.cursor } });
|
898
965
|
}
|
899
966
|
async lastPage(size, offset) {
|
900
|
-
return __privateGet$
|
967
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, last: this.meta.page.cursor } });
|
901
968
|
}
|
902
969
|
hasNextPage() {
|
903
970
|
return this.meta.page.more;
|
@@ -905,15 +972,46 @@ class Page {
|
|
905
972
|
}
|
906
973
|
_query = new WeakMap();
|
907
974
|
const PAGINATION_MAX_SIZE = 200;
|
908
|
-
const PAGINATION_DEFAULT_SIZE =
|
975
|
+
const PAGINATION_DEFAULT_SIZE = 20;
|
909
976
|
const PAGINATION_MAX_OFFSET = 800;
|
910
977
|
const PAGINATION_DEFAULT_OFFSET = 0;
|
978
|
+
function isCursorPaginationOptions(options) {
|
979
|
+
return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
|
980
|
+
}
|
981
|
+
const _RecordArray = class extends Array {
|
982
|
+
constructor(page, overrideRecords) {
|
983
|
+
super(...overrideRecords ?? page.records);
|
984
|
+
__privateAdd$6(this, _page, void 0);
|
985
|
+
__privateSet$5(this, _page, page);
|
986
|
+
}
|
987
|
+
async nextPage(size, offset) {
|
988
|
+
const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
|
989
|
+
return new _RecordArray(newPage);
|
990
|
+
}
|
991
|
+
async previousPage(size, offset) {
|
992
|
+
const newPage = await __privateGet$6(this, _page).previousPage(size, offset);
|
993
|
+
return new _RecordArray(newPage);
|
994
|
+
}
|
995
|
+
async firstPage(size, offset) {
|
996
|
+
const newPage = await __privateGet$6(this, _page).firstPage(size, offset);
|
997
|
+
return new _RecordArray(newPage);
|
998
|
+
}
|
999
|
+
async lastPage(size, offset) {
|
1000
|
+
const newPage = await __privateGet$6(this, _page).lastPage(size, offset);
|
1001
|
+
return new _RecordArray(newPage);
|
1002
|
+
}
|
1003
|
+
hasNextPage() {
|
1004
|
+
return __privateGet$6(this, _page).meta.page.more;
|
1005
|
+
}
|
1006
|
+
};
|
1007
|
+
let RecordArray = _RecordArray;
|
1008
|
+
_page = new WeakMap();
|
911
1009
|
|
912
1010
|
var __accessCheck$5 = (obj, member, msg) => {
|
913
1011
|
if (!member.has(obj))
|
914
1012
|
throw TypeError("Cannot " + msg);
|
915
1013
|
};
|
916
|
-
var __privateGet$
|
1014
|
+
var __privateGet$5 = (obj, member, getter) => {
|
917
1015
|
__accessCheck$5(obj, member, "read from private field");
|
918
1016
|
return getter ? getter.call(obj) : member.get(obj);
|
919
1017
|
};
|
@@ -922,34 +1020,35 @@ var __privateAdd$5 = (obj, member, value) => {
|
|
922
1020
|
throw TypeError("Cannot add the same private member more than once");
|
923
1021
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
924
1022
|
};
|
925
|
-
var __privateSet$
|
1023
|
+
var __privateSet$4 = (obj, member, value, setter) => {
|
926
1024
|
__accessCheck$5(obj, member, "write to private field");
|
927
1025
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
928
1026
|
return value;
|
929
1027
|
};
|
930
1028
|
var _table$1, _repository, _data;
|
931
1029
|
const _Query = class {
|
932
|
-
constructor(repository, table, data,
|
1030
|
+
constructor(repository, table, data, rawParent) {
|
933
1031
|
__privateAdd$5(this, _table$1, void 0);
|
934
1032
|
__privateAdd$5(this, _repository, void 0);
|
935
1033
|
__privateAdd$5(this, _data, { filter: {} });
|
936
1034
|
this.meta = { page: { cursor: "start", more: true } };
|
937
|
-
this.records = [];
|
938
|
-
__privateSet$
|
1035
|
+
this.records = new RecordArray(this, []);
|
1036
|
+
__privateSet$4(this, _table$1, table);
|
939
1037
|
if (repository) {
|
940
|
-
__privateSet$
|
1038
|
+
__privateSet$4(this, _repository, repository);
|
941
1039
|
} else {
|
942
|
-
__privateSet$
|
1040
|
+
__privateSet$4(this, _repository, this);
|
943
1041
|
}
|
944
|
-
|
945
|
-
__privateGet$
|
946
|
-
__privateGet$
|
947
|
-
__privateGet$
|
948
|
-
__privateGet$
|
949
|
-
__privateGet$
|
950
|
-
__privateGet$
|
951
|
-
__privateGet$
|
952
|
-
__privateGet$
|
1042
|
+
const parent = cleanParent(data, rawParent);
|
1043
|
+
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
1044
|
+
__privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
|
1045
|
+
__privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
|
1046
|
+
__privateGet$5(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
|
1047
|
+
__privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
|
1048
|
+
__privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
|
1049
|
+
__privateGet$5(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
|
1050
|
+
__privateGet$5(this, _data).pagination = data.pagination ?? parent?.pagination;
|
1051
|
+
__privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
|
953
1052
|
this.any = this.any.bind(this);
|
954
1053
|
this.all = this.all.bind(this);
|
955
1054
|
this.not = this.not.bind(this);
|
@@ -960,83 +1059,88 @@ const _Query = class {
|
|
960
1059
|
Object.defineProperty(this, "repository", { enumerable: false });
|
961
1060
|
}
|
962
1061
|
getQueryOptions() {
|
963
|
-
return __privateGet$
|
1062
|
+
return __privateGet$5(this, _data);
|
964
1063
|
}
|
965
1064
|
key() {
|
966
|
-
const { columns = [], filter = {}, sort = [],
|
967
|
-
const key = JSON.stringify({ columns, filter, sort,
|
1065
|
+
const { columns = [], filter = {}, sort = [], pagination = {} } = __privateGet$5(this, _data);
|
1066
|
+
const key = JSON.stringify({ columns, filter, sort, pagination });
|
968
1067
|
return toBase64(key);
|
969
1068
|
}
|
970
1069
|
any(...queries) {
|
971
1070
|
const $any = queries.map((query) => query.getQueryOptions().filter ?? {});
|
972
|
-
return new _Query(__privateGet$
|
1071
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $any } }, __privateGet$5(this, _data));
|
973
1072
|
}
|
974
1073
|
all(...queries) {
|
975
1074
|
const $all = queries.map((query) => query.getQueryOptions().filter ?? {});
|
976
|
-
return new _Query(__privateGet$
|
1075
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
977
1076
|
}
|
978
1077
|
not(...queries) {
|
979
1078
|
const $not = queries.map((query) => query.getQueryOptions().filter ?? {});
|
980
|
-
return new _Query(__privateGet$
|
1079
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $not } }, __privateGet$5(this, _data));
|
981
1080
|
}
|
982
1081
|
none(...queries) {
|
983
1082
|
const $none = queries.map((query) => query.getQueryOptions().filter ?? {});
|
984
|
-
return new _Query(__privateGet$
|
1083
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $none } }, __privateGet$5(this, _data));
|
985
1084
|
}
|
986
1085
|
filter(a, b) {
|
987
1086
|
if (arguments.length === 1) {
|
988
1087
|
const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
|
989
|
-
const $all = compact([__privateGet$
|
990
|
-
return new _Query(__privateGet$
|
1088
|
+
const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat(constraints));
|
1089
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
991
1090
|
} else {
|
992
|
-
const $all = compact([__privateGet$
|
993
|
-
return new _Query(__privateGet$
|
1091
|
+
const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
|
1092
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
994
1093
|
}
|
995
1094
|
}
|
996
1095
|
sort(column, direction) {
|
997
|
-
const originalSort = [__privateGet$
|
1096
|
+
const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
|
998
1097
|
const sort = [...originalSort, { column, direction }];
|
999
|
-
return new _Query(__privateGet$
|
1098
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
|
1000
1099
|
}
|
1001
1100
|
select(columns) {
|
1002
|
-
return new _Query(__privateGet$
|
1101
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { columns }, __privateGet$5(this, _data));
|
1003
1102
|
}
|
1004
1103
|
getPaginated(options = {}) {
|
1005
|
-
const query = new _Query(__privateGet$
|
1006
|
-
return __privateGet$
|
1104
|
+
const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
|
1105
|
+
return __privateGet$5(this, _repository).query(query);
|
1007
1106
|
}
|
1008
1107
|
async *[Symbol.asyncIterator]() {
|
1009
|
-
for await (const [record] of this.getIterator(1)) {
|
1108
|
+
for await (const [record] of this.getIterator({ batchSize: 1 })) {
|
1010
1109
|
yield record;
|
1011
1110
|
}
|
1012
1111
|
}
|
1013
|
-
async *getIterator(
|
1014
|
-
|
1015
|
-
let
|
1016
|
-
|
1017
|
-
|
1018
|
-
|
1019
|
-
|
1020
|
-
|
1112
|
+
async *getIterator(options = {}) {
|
1113
|
+
const { batchSize = 1 } = options;
|
1114
|
+
let page = await this.getPaginated({ ...options, pagination: { size: batchSize, offset: 0 } });
|
1115
|
+
let more = page.hasNextPage();
|
1116
|
+
yield page.records;
|
1117
|
+
while (more) {
|
1118
|
+
page = await page.nextPage();
|
1119
|
+
more = page.hasNextPage();
|
1120
|
+
yield page.records;
|
1021
1121
|
}
|
1022
1122
|
}
|
1023
1123
|
async getMany(options = {}) {
|
1024
|
-
const
|
1025
|
-
|
1124
|
+
const page = await this.getPaginated(options);
|
1125
|
+
if (page.hasNextPage() && options.pagination?.size === void 0) {
|
1126
|
+
console.trace("Calling getMany does not return all results. Paginate to get all results or call getAll.");
|
1127
|
+
}
|
1128
|
+
return page.records;
|
1026
1129
|
}
|
1027
|
-
async getAll(
|
1130
|
+
async getAll(options = {}) {
|
1131
|
+
const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
|
1028
1132
|
const results = [];
|
1029
|
-
for await (const page of this.getIterator(
|
1133
|
+
for await (const page of this.getIterator({ ...rest, batchSize })) {
|
1030
1134
|
results.push(...page);
|
1031
1135
|
}
|
1032
1136
|
return results;
|
1033
1137
|
}
|
1034
1138
|
async getFirst(options = {}) {
|
1035
|
-
const records = await this.getMany({ ...options,
|
1036
|
-
return records[0]
|
1139
|
+
const records = await this.getMany({ ...options, pagination: { size: 1 } });
|
1140
|
+
return records[0] ?? null;
|
1037
1141
|
}
|
1038
1142
|
cache(ttl) {
|
1039
|
-
return new _Query(__privateGet$
|
1143
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
|
1040
1144
|
}
|
1041
1145
|
nextPage(size, offset) {
|
1042
1146
|
return this.firstPage(size, offset);
|
@@ -1045,10 +1149,10 @@ const _Query = class {
|
|
1045
1149
|
return this.firstPage(size, offset);
|
1046
1150
|
}
|
1047
1151
|
firstPage(size, offset) {
|
1048
|
-
return this.getPaginated({
|
1152
|
+
return this.getPaginated({ pagination: { size, offset } });
|
1049
1153
|
}
|
1050
1154
|
lastPage(size, offset) {
|
1051
|
-
return this.getPaginated({
|
1155
|
+
return this.getPaginated({ pagination: { size, offset, before: "end" } });
|
1052
1156
|
}
|
1053
1157
|
hasNextPage() {
|
1054
1158
|
return this.meta.page.more;
|
@@ -1058,12 +1162,20 @@ let Query = _Query;
|
|
1058
1162
|
_table$1 = new WeakMap();
|
1059
1163
|
_repository = new WeakMap();
|
1060
1164
|
_data = new WeakMap();
|
1165
|
+
function cleanParent(data, parent) {
|
1166
|
+
if (isCursorPaginationOptions(data.pagination)) {
|
1167
|
+
return { ...parent, sorting: void 0, filter: void 0 };
|
1168
|
+
}
|
1169
|
+
return parent;
|
1170
|
+
}
|
1061
1171
|
|
1062
1172
|
function isIdentifiable(x) {
|
1063
1173
|
return isObject(x) && isString(x?.id);
|
1064
1174
|
}
|
1065
1175
|
function isXataRecord(x) {
|
1066
|
-
|
1176
|
+
const record = x;
|
1177
|
+
const metadata = record?.getMetadata();
|
1178
|
+
return isIdentifiable(x) && isObject(metadata) && typeof metadata.version === "number";
|
1067
1179
|
}
|
1068
1180
|
|
1069
1181
|
function isSortFilterString(value) {
|
@@ -1093,7 +1205,7 @@ var __accessCheck$4 = (obj, member, msg) => {
|
|
1093
1205
|
if (!member.has(obj))
|
1094
1206
|
throw TypeError("Cannot " + msg);
|
1095
1207
|
};
|
1096
|
-
var __privateGet$
|
1208
|
+
var __privateGet$4 = (obj, member, getter) => {
|
1097
1209
|
__accessCheck$4(obj, member, "read from private field");
|
1098
1210
|
return getter ? getter.call(obj) : member.get(obj);
|
1099
1211
|
};
|
@@ -1102,7 +1214,7 @@ var __privateAdd$4 = (obj, member, value) => {
|
|
1102
1214
|
throw TypeError("Cannot add the same private member more than once");
|
1103
1215
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1104
1216
|
};
|
1105
|
-
var __privateSet$
|
1217
|
+
var __privateSet$3 = (obj, member, value, setter) => {
|
1106
1218
|
__accessCheck$4(obj, member, "write to private field");
|
1107
1219
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1108
1220
|
return value;
|
@@ -1111,7 +1223,7 @@ var __privateMethod$2 = (obj, member, method) => {
|
|
1111
1223
|
__accessCheck$4(obj, member, "access private method");
|
1112
1224
|
return method;
|
1113
1225
|
};
|
1114
|
-
var _table,
|
1226
|
+
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;
|
1115
1227
|
class Repository extends Query {
|
1116
1228
|
}
|
1117
1229
|
class RestRepository extends Query {
|
@@ -1128,18 +1240,20 @@ class RestRepository extends Query {
|
|
1128
1240
|
__privateAdd$4(this, _getCacheRecord);
|
1129
1241
|
__privateAdd$4(this, _setCacheQuery);
|
1130
1242
|
__privateAdd$4(this, _getCacheQuery);
|
1243
|
+
__privateAdd$4(this, _getSchema$1);
|
1131
1244
|
__privateAdd$4(this, _table, void 0);
|
1132
|
-
__privateAdd$4(this, _links, void 0);
|
1133
1245
|
__privateAdd$4(this, _getFetchProps, void 0);
|
1134
1246
|
__privateAdd$4(this, _cache, void 0);
|
1135
|
-
|
1136
|
-
__privateSet$
|
1137
|
-
__privateSet$
|
1247
|
+
__privateAdd$4(this, _schema$1, void 0);
|
1248
|
+
__privateSet$3(this, _table, options.table);
|
1249
|
+
__privateSet$3(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
1138
1250
|
this.db = options.db;
|
1139
|
-
__privateSet$
|
1251
|
+
__privateSet$3(this, _cache, options.pluginOptions.cache);
|
1140
1252
|
}
|
1141
1253
|
async create(a, b) {
|
1142
1254
|
if (Array.isArray(a)) {
|
1255
|
+
if (a.length === 0)
|
1256
|
+
return [];
|
1143
1257
|
const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
|
1144
1258
|
await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
|
1145
1259
|
return records;
|
@@ -1165,26 +1279,36 @@ class RestRepository extends Query {
|
|
1165
1279
|
}
|
1166
1280
|
throw new Error("Invalid arguments for create method");
|
1167
1281
|
}
|
1168
|
-
async read(
|
1169
|
-
|
1170
|
-
|
1171
|
-
|
1172
|
-
|
1173
|
-
|
1174
|
-
|
1175
|
-
|
1176
|
-
|
1177
|
-
|
1178
|
-
|
1179
|
-
|
1180
|
-
|
1181
|
-
|
1282
|
+
async read(a) {
|
1283
|
+
if (Array.isArray(a)) {
|
1284
|
+
if (a.length === 0)
|
1285
|
+
return [];
|
1286
|
+
return this.getAll({ filter: { id: { $any: a } } });
|
1287
|
+
}
|
1288
|
+
if (isString(a)) {
|
1289
|
+
const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, a);
|
1290
|
+
if (cacheRecord)
|
1291
|
+
return cacheRecord;
|
1292
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1293
|
+
try {
|
1294
|
+
const response = await getRecord({
|
1295
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: a },
|
1296
|
+
...fetchProps
|
1297
|
+
});
|
1298
|
+
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1299
|
+
return initObject(this.db, schema, __privateGet$4(this, _table), response);
|
1300
|
+
} catch (e) {
|
1301
|
+
if (isObject(e) && e.status === 404) {
|
1302
|
+
return null;
|
1303
|
+
}
|
1304
|
+
throw e;
|
1182
1305
|
}
|
1183
|
-
throw e;
|
1184
1306
|
}
|
1185
1307
|
}
|
1186
1308
|
async update(a, b) {
|
1187
1309
|
if (Array.isArray(a)) {
|
1310
|
+
if (a.length === 0)
|
1311
|
+
return [];
|
1188
1312
|
if (a.length > 100) {
|
1189
1313
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1190
1314
|
}
|
@@ -1206,6 +1330,8 @@ class RestRepository extends Query {
|
|
1206
1330
|
}
|
1207
1331
|
async createOrUpdate(a, b) {
|
1208
1332
|
if (Array.isArray(a)) {
|
1333
|
+
if (a.length === 0)
|
1334
|
+
return [];
|
1209
1335
|
if (a.length > 100) {
|
1210
1336
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1211
1337
|
}
|
@@ -1227,6 +1353,8 @@ class RestRepository extends Query {
|
|
1227
1353
|
}
|
1228
1354
|
async delete(a) {
|
1229
1355
|
if (Array.isArray(a)) {
|
1356
|
+
if (a.length === 0)
|
1357
|
+
return;
|
1230
1358
|
if (a.length > 100) {
|
1231
1359
|
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
1232
1360
|
}
|
@@ -1246,13 +1374,19 @@ class RestRepository extends Query {
|
|
1246
1374
|
throw new Error("Invalid arguments for delete method");
|
1247
1375
|
}
|
1248
1376
|
async search(query, options = {}) {
|
1249
|
-
const fetchProps = await __privateGet$
|
1250
|
-
const { records } = await
|
1251
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1252
|
-
body: {
|
1377
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1378
|
+
const { records } = await searchTable({
|
1379
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1380
|
+
body: {
|
1381
|
+
query,
|
1382
|
+
fuzziness: options.fuzziness,
|
1383
|
+
highlight: options.highlight,
|
1384
|
+
filter: options.filter
|
1385
|
+
},
|
1253
1386
|
...fetchProps
|
1254
1387
|
});
|
1255
|
-
|
1388
|
+
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1389
|
+
return records.map((item) => initObject(this.db, schema, __privateGet$4(this, _table), item));
|
1256
1390
|
}
|
1257
1391
|
async query(query) {
|
1258
1392
|
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
@@ -1261,34 +1395,35 @@ class RestRepository extends Query {
|
|
1261
1395
|
const data = query.getQueryOptions();
|
1262
1396
|
const body = {
|
1263
1397
|
filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
|
1264
|
-
sort: data.sort ? buildSortFilter(data.sort) : void 0,
|
1265
|
-
page: data.
|
1398
|
+
sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
|
1399
|
+
page: data.pagination,
|
1266
1400
|
columns: data.columns
|
1267
1401
|
};
|
1268
|
-
const fetchProps = await __privateGet$
|
1402
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1269
1403
|
const { meta, records: objects } = await queryTable({
|
1270
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1404
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1271
1405
|
body,
|
1272
1406
|
...fetchProps
|
1273
1407
|
});
|
1274
|
-
const
|
1408
|
+
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1409
|
+
const records = objects.map((record) => initObject(this.db, schema, __privateGet$4(this, _table), record));
|
1275
1410
|
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1276
1411
|
return new Page(query, meta, records);
|
1277
1412
|
}
|
1278
1413
|
}
|
1279
1414
|
_table = new WeakMap();
|
1280
|
-
_links = new WeakMap();
|
1281
1415
|
_getFetchProps = new WeakMap();
|
1282
1416
|
_cache = new WeakMap();
|
1417
|
+
_schema$1 = new WeakMap();
|
1283
1418
|
_insertRecordWithoutId = new WeakSet();
|
1284
1419
|
insertRecordWithoutId_fn = async function(object) {
|
1285
|
-
const fetchProps = await __privateGet$
|
1420
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1286
1421
|
const record = transformObjectLinks(object);
|
1287
1422
|
const response = await insertRecord({
|
1288
1423
|
pathParams: {
|
1289
1424
|
workspace: "{workspaceId}",
|
1290
1425
|
dbBranchName: "{dbBranch}",
|
1291
|
-
tableName: __privateGet$
|
1426
|
+
tableName: __privateGet$4(this, _table)
|
1292
1427
|
},
|
1293
1428
|
body: record,
|
1294
1429
|
...fetchProps
|
@@ -1301,13 +1436,13 @@ insertRecordWithoutId_fn = async function(object) {
|
|
1301
1436
|
};
|
1302
1437
|
_insertRecordWithId = new WeakSet();
|
1303
1438
|
insertRecordWithId_fn = async function(recordId, object) {
|
1304
|
-
const fetchProps = await __privateGet$
|
1439
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1305
1440
|
const record = transformObjectLinks(object);
|
1306
1441
|
const response = await insertRecordWithID({
|
1307
1442
|
pathParams: {
|
1308
1443
|
workspace: "{workspaceId}",
|
1309
1444
|
dbBranchName: "{dbBranch}",
|
1310
|
-
tableName: __privateGet$
|
1445
|
+
tableName: __privateGet$4(this, _table),
|
1311
1446
|
recordId
|
1312
1447
|
},
|
1313
1448
|
body: record,
|
@@ -1322,10 +1457,10 @@ insertRecordWithId_fn = async function(recordId, object) {
|
|
1322
1457
|
};
|
1323
1458
|
_bulkInsertTableRecords = new WeakSet();
|
1324
1459
|
bulkInsertTableRecords_fn = async function(objects) {
|
1325
|
-
const fetchProps = await __privateGet$
|
1460
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1326
1461
|
const records = objects.map((object) => transformObjectLinks(object));
|
1327
1462
|
const response = await bulkInsertTableRecords({
|
1328
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1463
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1329
1464
|
body: { records },
|
1330
1465
|
...fetchProps
|
1331
1466
|
});
|
@@ -1337,10 +1472,10 @@ bulkInsertTableRecords_fn = async function(objects) {
|
|
1337
1472
|
};
|
1338
1473
|
_updateRecordWithID = new WeakSet();
|
1339
1474
|
updateRecordWithID_fn = async function(recordId, object) {
|
1340
|
-
const fetchProps = await __privateGet$
|
1475
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1341
1476
|
const record = transformObjectLinks(object);
|
1342
1477
|
const response = await updateRecordWithID({
|
1343
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1478
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1344
1479
|
body: record,
|
1345
1480
|
...fetchProps
|
1346
1481
|
});
|
@@ -1351,9 +1486,9 @@ updateRecordWithID_fn = async function(recordId, object) {
|
|
1351
1486
|
};
|
1352
1487
|
_upsertRecordWithID = new WeakSet();
|
1353
1488
|
upsertRecordWithID_fn = async function(recordId, object) {
|
1354
|
-
const fetchProps = await __privateGet$
|
1489
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1355
1490
|
const response = await upsertRecordWithID({
|
1356
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1491
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1357
1492
|
body: object,
|
1358
1493
|
...fetchProps
|
1359
1494
|
});
|
@@ -1364,51 +1499,63 @@ upsertRecordWithID_fn = async function(recordId, object) {
|
|
1364
1499
|
};
|
1365
1500
|
_deleteRecord = new WeakSet();
|
1366
1501
|
deleteRecord_fn = async function(recordId) {
|
1367
|
-
const fetchProps = await __privateGet$
|
1502
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1368
1503
|
await deleteRecord({
|
1369
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1504
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1370
1505
|
...fetchProps
|
1371
1506
|
});
|
1372
1507
|
};
|
1373
1508
|
_invalidateCache = new WeakSet();
|
1374
1509
|
invalidateCache_fn = async function(recordId) {
|
1375
|
-
await __privateGet$
|
1376
|
-
const cacheItems = await __privateGet$
|
1510
|
+
await __privateGet$4(this, _cache).delete(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1511
|
+
const cacheItems = await __privateGet$4(this, _cache).getAll();
|
1377
1512
|
const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
|
1378
1513
|
for (const [key, value] of queries) {
|
1379
1514
|
const ids = getIds(value);
|
1380
1515
|
if (ids.includes(recordId))
|
1381
|
-
await __privateGet$
|
1516
|
+
await __privateGet$4(this, _cache).delete(key);
|
1382
1517
|
}
|
1383
1518
|
};
|
1384
1519
|
_setCacheRecord = new WeakSet();
|
1385
1520
|
setCacheRecord_fn = async function(record) {
|
1386
|
-
if (!__privateGet$
|
1521
|
+
if (!__privateGet$4(this, _cache).cacheRecords)
|
1387
1522
|
return;
|
1388
|
-
await __privateGet$
|
1523
|
+
await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
|
1389
1524
|
};
|
1390
1525
|
_getCacheRecord = new WeakSet();
|
1391
1526
|
getCacheRecord_fn = async function(recordId) {
|
1392
|
-
if (!__privateGet$
|
1527
|
+
if (!__privateGet$4(this, _cache).cacheRecords)
|
1393
1528
|
return null;
|
1394
|
-
return __privateGet$
|
1529
|
+
return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1395
1530
|
};
|
1396
1531
|
_setCacheQuery = new WeakSet();
|
1397
1532
|
setCacheQuery_fn = async function(query, meta, records) {
|
1398
|
-
await __privateGet$
|
1533
|
+
await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
|
1399
1534
|
};
|
1400
1535
|
_getCacheQuery = new WeakSet();
|
1401
1536
|
getCacheQuery_fn = async function(query) {
|
1402
|
-
const key = `query_${__privateGet$
|
1403
|
-
const result = await __privateGet$
|
1537
|
+
const key = `query_${__privateGet$4(this, _table)}:${query.key()}`;
|
1538
|
+
const result = await __privateGet$4(this, _cache).get(key);
|
1404
1539
|
if (!result)
|
1405
1540
|
return null;
|
1406
|
-
const { cache: ttl = __privateGet$
|
1407
|
-
if (
|
1408
|
-
return
|
1541
|
+
const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
|
1542
|
+
if (ttl < 0)
|
1543
|
+
return null;
|
1409
1544
|
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1410
1545
|
return hasExpired ? null : result;
|
1411
1546
|
};
|
1547
|
+
_getSchema$1 = new WeakSet();
|
1548
|
+
getSchema_fn$1 = async function() {
|
1549
|
+
if (__privateGet$4(this, _schema$1))
|
1550
|
+
return __privateGet$4(this, _schema$1);
|
1551
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1552
|
+
const { schema } = await getBranchDetails({
|
1553
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1554
|
+
...fetchProps
|
1555
|
+
});
|
1556
|
+
__privateSet$3(this, _schema$1, schema);
|
1557
|
+
return schema;
|
1558
|
+
};
|
1412
1559
|
const transformObjectLinks = (object) => {
|
1413
1560
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
1414
1561
|
if (key === "xata")
|
@@ -1416,15 +1563,34 @@ const transformObjectLinks = (object) => {
|
|
1416
1563
|
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1417
1564
|
}, {});
|
1418
1565
|
};
|
1419
|
-
const initObject = (db,
|
1566
|
+
const initObject = (db, schema, table, object) => {
|
1420
1567
|
const result = {};
|
1421
|
-
|
1422
|
-
|
1423
|
-
|
1424
|
-
|
1425
|
-
|
1426
|
-
|
1427
|
-
|
1568
|
+
const { xata, ...rest } = object ?? {};
|
1569
|
+
Object.assign(result, rest);
|
1570
|
+
const { columns } = schema.tables.find(({ name }) => name === table) ?? {};
|
1571
|
+
if (!columns)
|
1572
|
+
console.error(`Table ${table} not found in schema`);
|
1573
|
+
for (const column of columns ?? []) {
|
1574
|
+
const value = result[column.name];
|
1575
|
+
switch (column.type) {
|
1576
|
+
case "datetime": {
|
1577
|
+
const date = value !== void 0 ? new Date(value) : void 0;
|
1578
|
+
if (date && isNaN(date.getTime())) {
|
1579
|
+
console.error(`Failed to parse date ${value} for field ${column.name}`);
|
1580
|
+
} else if (date) {
|
1581
|
+
result[column.name] = date;
|
1582
|
+
}
|
1583
|
+
break;
|
1584
|
+
}
|
1585
|
+
case "link": {
|
1586
|
+
const linkTable = column.link?.table;
|
1587
|
+
if (!linkTable) {
|
1588
|
+
console.error(`Failed to parse link for field ${column.name}`);
|
1589
|
+
} else if (isObject(value)) {
|
1590
|
+
result[column.name] = initObject(db, schema, linkTable, value);
|
1591
|
+
}
|
1592
|
+
break;
|
1593
|
+
}
|
1428
1594
|
}
|
1429
1595
|
}
|
1430
1596
|
result.read = function() {
|
@@ -1436,7 +1602,10 @@ const initObject = (db, links, table, object) => {
|
|
1436
1602
|
result.delete = function() {
|
1437
1603
|
return db[table].delete(result["id"]);
|
1438
1604
|
};
|
1439
|
-
|
1605
|
+
result.getMetadata = function() {
|
1606
|
+
return xata;
|
1607
|
+
};
|
1608
|
+
for (const prop of ["read", "update", "delete", "getMetadata"]) {
|
1440
1609
|
Object.defineProperty(result, prop, { enumerable: false });
|
1441
1610
|
}
|
1442
1611
|
Object.freeze(result);
|
@@ -1456,7 +1625,7 @@ var __accessCheck$3 = (obj, member, msg) => {
|
|
1456
1625
|
if (!member.has(obj))
|
1457
1626
|
throw TypeError("Cannot " + msg);
|
1458
1627
|
};
|
1459
|
-
var __privateGet$
|
1628
|
+
var __privateGet$3 = (obj, member, getter) => {
|
1460
1629
|
__accessCheck$3(obj, member, "read from private field");
|
1461
1630
|
return getter ? getter.call(obj) : member.get(obj);
|
1462
1631
|
};
|
@@ -1465,7 +1634,7 @@ var __privateAdd$3 = (obj, member, value) => {
|
|
1465
1634
|
throw TypeError("Cannot add the same private member more than once");
|
1466
1635
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1467
1636
|
};
|
1468
|
-
var __privateSet$
|
1637
|
+
var __privateSet$2 = (obj, member, value, setter) => {
|
1469
1638
|
__accessCheck$3(obj, member, "write to private field");
|
1470
1639
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1471
1640
|
return value;
|
@@ -1474,30 +1643,30 @@ var _map;
|
|
1474
1643
|
class SimpleCache {
|
1475
1644
|
constructor(options = {}) {
|
1476
1645
|
__privateAdd$3(this, _map, void 0);
|
1477
|
-
__privateSet$
|
1646
|
+
__privateSet$2(this, _map, /* @__PURE__ */ new Map());
|
1478
1647
|
this.capacity = options.max ?? 500;
|
1479
1648
|
this.cacheRecords = options.cacheRecords ?? true;
|
1480
1649
|
this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
|
1481
1650
|
}
|
1482
1651
|
async getAll() {
|
1483
|
-
return Object.fromEntries(__privateGet$
|
1652
|
+
return Object.fromEntries(__privateGet$3(this, _map));
|
1484
1653
|
}
|
1485
1654
|
async get(key) {
|
1486
|
-
return __privateGet$
|
1655
|
+
return __privateGet$3(this, _map).get(key) ?? null;
|
1487
1656
|
}
|
1488
1657
|
async set(key, value) {
|
1489
1658
|
await this.delete(key);
|
1490
|
-
__privateGet$
|
1491
|
-
if (__privateGet$
|
1492
|
-
const leastRecentlyUsed = __privateGet$
|
1659
|
+
__privateGet$3(this, _map).set(key, value);
|
1660
|
+
if (__privateGet$3(this, _map).size > this.capacity) {
|
1661
|
+
const leastRecentlyUsed = __privateGet$3(this, _map).keys().next().value;
|
1493
1662
|
await this.delete(leastRecentlyUsed);
|
1494
1663
|
}
|
1495
1664
|
}
|
1496
1665
|
async delete(key) {
|
1497
|
-
__privateGet$
|
1666
|
+
__privateGet$3(this, _map).delete(key);
|
1498
1667
|
}
|
1499
1668
|
async clear() {
|
1500
|
-
return __privateGet$
|
1669
|
+
return __privateGet$3(this, _map).clear();
|
1501
1670
|
}
|
1502
1671
|
}
|
1503
1672
|
_map = new WeakMap();
|
@@ -1525,7 +1694,7 @@ var __accessCheck$2 = (obj, member, msg) => {
|
|
1525
1694
|
if (!member.has(obj))
|
1526
1695
|
throw TypeError("Cannot " + msg);
|
1527
1696
|
};
|
1528
|
-
var __privateGet$
|
1697
|
+
var __privateGet$2 = (obj, member, getter) => {
|
1529
1698
|
__accessCheck$2(obj, member, "read from private field");
|
1530
1699
|
return getter ? getter.call(obj) : member.get(obj);
|
1531
1700
|
};
|
@@ -1536,26 +1705,24 @@ var __privateAdd$2 = (obj, member, value) => {
|
|
1536
1705
|
};
|
1537
1706
|
var _tables;
|
1538
1707
|
class SchemaPlugin extends XataPlugin {
|
1539
|
-
constructor(
|
1708
|
+
constructor(tableNames) {
|
1540
1709
|
super();
|
1541
|
-
this.links = links;
|
1542
1710
|
this.tableNames = tableNames;
|
1543
1711
|
__privateAdd$2(this, _tables, {});
|
1544
1712
|
}
|
1545
1713
|
build(pluginOptions) {
|
1546
|
-
const links = this.links;
|
1547
1714
|
const db = new Proxy({}, {
|
1548
1715
|
get: (_target, table) => {
|
1549
1716
|
if (!isString(table))
|
1550
1717
|
throw new Error("Invalid table name");
|
1551
|
-
if (
|
1552
|
-
__privateGet$
|
1718
|
+
if (__privateGet$2(this, _tables)[table] === void 0) {
|
1719
|
+
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
|
1553
1720
|
}
|
1554
|
-
return __privateGet$
|
1721
|
+
return __privateGet$2(this, _tables)[table];
|
1555
1722
|
}
|
1556
1723
|
});
|
1557
1724
|
for (const table of this.tableNames ?? []) {
|
1558
|
-
db[table] = new RestRepository({ db, pluginOptions, table
|
1725
|
+
db[table] = new RestRepository({ db, pluginOptions, table });
|
1559
1726
|
}
|
1560
1727
|
return db;
|
1561
1728
|
}
|
@@ -1566,55 +1733,80 @@ var __accessCheck$1 = (obj, member, msg) => {
|
|
1566
1733
|
if (!member.has(obj))
|
1567
1734
|
throw TypeError("Cannot " + msg);
|
1568
1735
|
};
|
1736
|
+
var __privateGet$1 = (obj, member, getter) => {
|
1737
|
+
__accessCheck$1(obj, member, "read from private field");
|
1738
|
+
return getter ? getter.call(obj) : member.get(obj);
|
1739
|
+
};
|
1569
1740
|
var __privateAdd$1 = (obj, member, value) => {
|
1570
1741
|
if (member.has(obj))
|
1571
1742
|
throw TypeError("Cannot add the same private member more than once");
|
1572
1743
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1573
1744
|
};
|
1745
|
+
var __privateSet$1 = (obj, member, value, setter) => {
|
1746
|
+
__accessCheck$1(obj, member, "write to private field");
|
1747
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1748
|
+
return value;
|
1749
|
+
};
|
1574
1750
|
var __privateMethod$1 = (obj, member, method) => {
|
1575
1751
|
__accessCheck$1(obj, member, "access private method");
|
1576
1752
|
return method;
|
1577
1753
|
};
|
1578
|
-
var _search, search_fn;
|
1754
|
+
var _schema, _search, search_fn, _getSchema, getSchema_fn;
|
1579
1755
|
class SearchPlugin extends XataPlugin {
|
1580
|
-
constructor(db
|
1756
|
+
constructor(db) {
|
1581
1757
|
super();
|
1582
1758
|
this.db = db;
|
1583
|
-
this.links = links;
|
1584
1759
|
__privateAdd$1(this, _search);
|
1760
|
+
__privateAdd$1(this, _getSchema);
|
1761
|
+
__privateAdd$1(this, _schema, void 0);
|
1585
1762
|
}
|
1586
1763
|
build({ getFetchProps }) {
|
1587
1764
|
return {
|
1588
1765
|
all: async (query, options = {}) => {
|
1589
1766
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1767
|
+
const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
|
1590
1768
|
return records.map((record) => {
|
1591
1769
|
const { table = "orphan" } = record.xata;
|
1592
|
-
return { table, record: initObject(this.db,
|
1770
|
+
return { table, record: initObject(this.db, schema, table, record) };
|
1593
1771
|
});
|
1594
1772
|
},
|
1595
1773
|
byTable: async (query, options = {}) => {
|
1596
1774
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1775
|
+
const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
|
1597
1776
|
return records.reduce((acc, record) => {
|
1598
1777
|
const { table = "orphan" } = record.xata;
|
1599
1778
|
const items = acc[table] ?? [];
|
1600
|
-
const item = initObject(this.db,
|
1779
|
+
const item = initObject(this.db, schema, table, record);
|
1601
1780
|
return { ...acc, [table]: [...items, item] };
|
1602
1781
|
}, {});
|
1603
1782
|
}
|
1604
1783
|
};
|
1605
1784
|
}
|
1606
1785
|
}
|
1786
|
+
_schema = new WeakMap();
|
1607
1787
|
_search = new WeakSet();
|
1608
1788
|
search_fn = async function(query, options, getFetchProps) {
|
1609
1789
|
const fetchProps = await getFetchProps();
|
1610
|
-
const { tables, fuzziness } = options ?? {};
|
1790
|
+
const { tables, fuzziness, highlight } = options ?? {};
|
1611
1791
|
const { records } = await searchBranch({
|
1612
1792
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1613
|
-
body: { tables, query, fuzziness },
|
1793
|
+
body: { tables, query, fuzziness, highlight },
|
1614
1794
|
...fetchProps
|
1615
1795
|
});
|
1616
1796
|
return records;
|
1617
1797
|
};
|
1798
|
+
_getSchema = new WeakSet();
|
1799
|
+
getSchema_fn = async function(getFetchProps) {
|
1800
|
+
if (__privateGet$1(this, _schema))
|
1801
|
+
return __privateGet$1(this, _schema);
|
1802
|
+
const fetchProps = await getFetchProps();
|
1803
|
+
const { schema } = await getBranchDetails({
|
1804
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1805
|
+
...fetchProps
|
1806
|
+
});
|
1807
|
+
__privateSet$1(this, _schema, schema);
|
1808
|
+
return schema;
|
1809
|
+
};
|
1618
1810
|
|
1619
1811
|
const isBranchStrategyBuilder = (strategy) => {
|
1620
1812
|
return typeof strategy === "function";
|
@@ -1626,30 +1818,39 @@ const envBranchNames = [
|
|
1626
1818
|
"CF_PAGES_BRANCH",
|
1627
1819
|
"BRANCH"
|
1628
1820
|
];
|
1629
|
-
const defaultBranch = "main";
|
1630
1821
|
async function getCurrentBranchName(options) {
|
1631
|
-
const env =
|
1632
|
-
if (env)
|
1633
|
-
|
1634
|
-
|
1635
|
-
|
1636
|
-
|
1637
|
-
|
1638
|
-
|
1639
|
-
|
1640
|
-
return defaultBranch;
|
1822
|
+
const env = getBranchByEnvVariable();
|
1823
|
+
if (env) {
|
1824
|
+
const details = await getDatabaseBranch(env, options);
|
1825
|
+
if (details)
|
1826
|
+
return env;
|
1827
|
+
console.warn(`Branch ${env} not found in Xata. Ignoring...`);
|
1828
|
+
}
|
1829
|
+
const gitBranch = await getGitBranch();
|
1830
|
+
return resolveXataBranch(gitBranch, options);
|
1641
1831
|
}
|
1642
1832
|
async function getCurrentBranchDetails(options) {
|
1643
|
-
const
|
1644
|
-
|
1645
|
-
|
1646
|
-
|
1647
|
-
|
1648
|
-
|
1649
|
-
|
1650
|
-
|
1651
|
-
|
1652
|
-
|
1833
|
+
const branch = await getCurrentBranchName(options);
|
1834
|
+
return getDatabaseBranch(branch, options);
|
1835
|
+
}
|
1836
|
+
async function resolveXataBranch(gitBranch, options) {
|
1837
|
+
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1838
|
+
const apiKey = options?.apiKey || getAPIKey();
|
1839
|
+
if (!databaseURL)
|
1840
|
+
throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
|
1841
|
+
if (!apiKey)
|
1842
|
+
throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
|
1843
|
+
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1844
|
+
const [workspace] = host.split(".");
|
1845
|
+
const { branch } = await resolveBranch({
|
1846
|
+
apiKey,
|
1847
|
+
apiUrl: databaseURL,
|
1848
|
+
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1849
|
+
workspacesApiUrl: `${protocol}//${host}`,
|
1850
|
+
pathParams: { dbName, workspace },
|
1851
|
+
queryParams: { gitBranch, fallbackBranch: getEnvVariable("XATA_FALLBACK_BRANCH") }
|
1852
|
+
});
|
1853
|
+
return branch;
|
1653
1854
|
}
|
1654
1855
|
async function getDatabaseBranch(branch, options) {
|
1655
1856
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
@@ -1723,7 +1924,7 @@ var __privateMethod = (obj, member, method) => {
|
|
1723
1924
|
const buildClient = (plugins) => {
|
1724
1925
|
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1725
1926
|
return _a = class {
|
1726
|
-
constructor(options = {},
|
1927
|
+
constructor(options = {}, tables) {
|
1727
1928
|
__privateAdd(this, _parseOptions);
|
1728
1929
|
__privateAdd(this, _getFetchProps);
|
1729
1930
|
__privateAdd(this, _evaluateBranch);
|
@@ -1733,12 +1934,12 @@ const buildClient = (plugins) => {
|
|
1733
1934
|
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
1734
1935
|
cache: safeOptions.cache
|
1735
1936
|
};
|
1736
|
-
const db = new SchemaPlugin(
|
1737
|
-
const search = new SearchPlugin(db
|
1937
|
+
const db = new SchemaPlugin(tables).build(pluginOptions);
|
1938
|
+
const search = new SearchPlugin(db).build(pluginOptions);
|
1738
1939
|
this.db = db;
|
1739
1940
|
this.search = search;
|
1740
1941
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
1741
|
-
if (
|
1942
|
+
if (namespace === void 0)
|
1742
1943
|
continue;
|
1743
1944
|
const result = namespace.build(pluginOptions);
|
1744
1945
|
if (result instanceof Promise) {
|
@@ -1755,7 +1956,7 @@ const buildClient = (plugins) => {
|
|
1755
1956
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1756
1957
|
const apiKey = options?.apiKey || getAPIKey();
|
1757
1958
|
const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
|
1758
|
-
const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
1959
|
+
const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
1759
1960
|
if (!databaseURL || !apiKey) {
|
1760
1961
|
throw new Error("Options databaseURL and apiKey are required");
|
1761
1962
|
}
|
@@ -1782,7 +1983,7 @@ const buildClient = (plugins) => {
|
|
1782
1983
|
}, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
|
1783
1984
|
if (__privateGet(this, _branch))
|
1784
1985
|
return __privateGet(this, _branch);
|
1785
|
-
if (
|
1986
|
+
if (param === void 0)
|
1786
1987
|
return void 0;
|
1787
1988
|
const strategies = Array.isArray(param) ? [...param] : [param];
|
1788
1989
|
const evaluateBranch = async (strategy) => {
|
@@ -1815,6 +2016,7 @@ exports.PAGINATION_MAX_OFFSET = PAGINATION_MAX_OFFSET;
|
|
1815
2016
|
exports.PAGINATION_MAX_SIZE = PAGINATION_MAX_SIZE;
|
1816
2017
|
exports.Page = Page;
|
1817
2018
|
exports.Query = Query;
|
2019
|
+
exports.RecordArray = RecordArray;
|
1818
2020
|
exports.Repository = Repository;
|
1819
2021
|
exports.RestRepository = RestRepository;
|
1820
2022
|
exports.SchemaPlugin = SchemaPlugin;
|
@@ -1825,6 +2027,7 @@ exports.XataApiPlugin = XataApiPlugin;
|
|
1825
2027
|
exports.XataError = XataError;
|
1826
2028
|
exports.XataPlugin = XataPlugin;
|
1827
2029
|
exports.acceptWorkspaceMemberInvite = acceptWorkspaceMemberInvite;
|
2030
|
+
exports.addGitBranchesEntry = addGitBranchesEntry;
|
1828
2031
|
exports.addTableColumn = addTableColumn;
|
1829
2032
|
exports.buildClient = buildClient;
|
1830
2033
|
exports.bulkInsertTableRecords = bulkInsertTableRecords;
|
@@ -1859,6 +2062,7 @@ exports.getCurrentBranchDetails = getCurrentBranchDetails;
|
|
1859
2062
|
exports.getCurrentBranchName = getCurrentBranchName;
|
1860
2063
|
exports.getDatabaseList = getDatabaseList;
|
1861
2064
|
exports.getDatabaseURL = getDatabaseURL;
|
2065
|
+
exports.getGitBranchesMapping = getGitBranchesMapping;
|
1862
2066
|
exports.getRecord = getRecord;
|
1863
2067
|
exports.getTableColumns = getTableColumns;
|
1864
2068
|
exports.getTableSchema = getTableSchema;
|
@@ -1877,6 +2081,7 @@ exports.insertRecord = insertRecord;
|
|
1877
2081
|
exports.insertRecordWithID = insertRecordWithID;
|
1878
2082
|
exports.inviteWorkspaceMember = inviteWorkspaceMember;
|
1879
2083
|
exports.is = is;
|
2084
|
+
exports.isCursorPaginationOptions = isCursorPaginationOptions;
|
1880
2085
|
exports.isIdentifiable = isIdentifiable;
|
1881
2086
|
exports.isNot = isNot;
|
1882
2087
|
exports.isXataRecord = isXataRecord;
|
@@ -1887,9 +2092,12 @@ exports.notExists = notExists;
|
|
1887
2092
|
exports.operationsByTag = operationsByTag;
|
1888
2093
|
exports.pattern = pattern;
|
1889
2094
|
exports.queryTable = queryTable;
|
2095
|
+
exports.removeGitBranchesEntry = removeGitBranchesEntry;
|
1890
2096
|
exports.removeWorkspaceMember = removeWorkspaceMember;
|
1891
2097
|
exports.resendWorkspaceMemberInvite = resendWorkspaceMemberInvite;
|
2098
|
+
exports.resolveBranch = resolveBranch;
|
1892
2099
|
exports.searchBranch = searchBranch;
|
2100
|
+
exports.searchTable = searchTable;
|
1893
2101
|
exports.setTableSchema = setTableSchema;
|
1894
2102
|
exports.startsWith = startsWith;
|
1895
2103
|
exports.updateBranchMetadata = updateBranchMetadata;
|