@xata.io/client 0.0.0-alpha.vf4b92f1 → 0.0.0-alpha.vf6c3bd7
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 +76 -0
- package/dist/index.cjs +369 -199
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +459 -62
- package/dist/index.mjs +364 -200
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -2
- package/tsconfig.json +1 -0
package/dist/index.mjs
CHANGED
@@ -7,8 +7,11 @@ function compact(arr) {
|
|
7
7
|
function isObject(value) {
|
8
8
|
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
9
9
|
}
|
10
|
+
function isDefined(value) {
|
11
|
+
return value !== null && value !== void 0;
|
12
|
+
}
|
10
13
|
function isString(value) {
|
11
|
-
return value
|
14
|
+
return isDefined(value) && typeof value === "string";
|
12
15
|
}
|
13
16
|
function toBase64(value) {
|
14
17
|
try {
|
@@ -34,7 +37,10 @@ function getEnvVariable(name) {
|
|
34
37
|
}
|
35
38
|
async function getGitBranch() {
|
36
39
|
try {
|
37
|
-
|
40
|
+
if (typeof require === "function") {
|
41
|
+
const req = require;
|
42
|
+
return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
|
43
|
+
}
|
38
44
|
} catch (err) {
|
39
45
|
}
|
40
46
|
try {
|
@@ -67,7 +73,12 @@ function getFetchImplementation(userFetch) {
|
|
67
73
|
return fetchImpl;
|
68
74
|
}
|
69
75
|
|
70
|
-
class
|
76
|
+
class ErrorWithCause extends Error {
|
77
|
+
constructor(message, options) {
|
78
|
+
super(message, options);
|
79
|
+
}
|
80
|
+
}
|
81
|
+
class FetcherError extends ErrorWithCause {
|
71
82
|
constructor(status, data) {
|
72
83
|
super(getMessage(data));
|
73
84
|
this.status = status;
|
@@ -248,6 +259,14 @@ const deleteDatabase = (variables) => fetch$1({
|
|
248
259
|
method: "delete",
|
249
260
|
...variables
|
250
261
|
});
|
262
|
+
const getGitBranchesMapping = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "get", ...variables });
|
263
|
+
const addGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "post", ...variables });
|
264
|
+
const removeGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "delete", ...variables });
|
265
|
+
const resolveBranch = (variables) => fetch$1({
|
266
|
+
url: "/dbs/{dbName}/resolveBranch",
|
267
|
+
method: "get",
|
268
|
+
...variables
|
269
|
+
});
|
251
270
|
const getBranchDetails = (variables) => fetch$1({
|
252
271
|
url: "/db/{dbBranchName}",
|
253
272
|
method: "get",
|
@@ -355,6 +374,11 @@ const queryTable = (variables) => fetch$1({
|
|
355
374
|
method: "post",
|
356
375
|
...variables
|
357
376
|
});
|
377
|
+
const searchTable = (variables) => fetch$1({
|
378
|
+
url: "/db/{dbBranchName}/tables/{tableName}/search",
|
379
|
+
method: "post",
|
380
|
+
...variables
|
381
|
+
});
|
358
382
|
const searchBranch = (variables) => fetch$1({
|
359
383
|
url: "/db/{dbBranchName}/search",
|
360
384
|
method: "post",
|
@@ -376,7 +400,15 @@ const operationsByTag = {
|
|
376
400
|
resendWorkspaceMemberInvite,
|
377
401
|
acceptWorkspaceMemberInvite
|
378
402
|
},
|
379
|
-
database: {
|
403
|
+
database: {
|
404
|
+
getDatabaseList,
|
405
|
+
createDatabase,
|
406
|
+
deleteDatabase,
|
407
|
+
getGitBranchesMapping,
|
408
|
+
addGitBranchesEntry,
|
409
|
+
removeGitBranchesEntry,
|
410
|
+
resolveBranch
|
411
|
+
},
|
380
412
|
branch: {
|
381
413
|
getBranchList,
|
382
414
|
getBranchDetails,
|
@@ -410,6 +442,7 @@ const operationsByTag = {
|
|
410
442
|
getRecord,
|
411
443
|
bulkInsertTableRecords,
|
412
444
|
queryTable,
|
445
|
+
searchTable,
|
413
446
|
searchBranch
|
414
447
|
}
|
415
448
|
};
|
@@ -443,7 +476,7 @@ var __accessCheck$7 = (obj, member, msg) => {
|
|
443
476
|
if (!member.has(obj))
|
444
477
|
throw TypeError("Cannot " + msg);
|
445
478
|
};
|
446
|
-
var __privateGet$
|
479
|
+
var __privateGet$7 = (obj, member, getter) => {
|
447
480
|
__accessCheck$7(obj, member, "read from private field");
|
448
481
|
return getter ? getter.call(obj) : member.get(obj);
|
449
482
|
};
|
@@ -452,7 +485,7 @@ var __privateAdd$7 = (obj, member, value) => {
|
|
452
485
|
throw TypeError("Cannot add the same private member more than once");
|
453
486
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
454
487
|
};
|
455
|
-
var __privateSet$
|
488
|
+
var __privateSet$6 = (obj, member, value, setter) => {
|
456
489
|
__accessCheck$7(obj, member, "write to private field");
|
457
490
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
458
491
|
return value;
|
@@ -467,7 +500,7 @@ class XataApiClient {
|
|
467
500
|
if (!apiKey) {
|
468
501
|
throw new Error("Could not resolve a valid apiKey");
|
469
502
|
}
|
470
|
-
__privateSet$
|
503
|
+
__privateSet$6(this, _extraProps, {
|
471
504
|
apiUrl: getHostUrl(provider, "main"),
|
472
505
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
473
506
|
fetchImpl: getFetchImplementation(options.fetch),
|
@@ -475,34 +508,34 @@ class XataApiClient {
|
|
475
508
|
});
|
476
509
|
}
|
477
510
|
get user() {
|
478
|
-
if (!__privateGet$
|
479
|
-
__privateGet$
|
480
|
-
return __privateGet$
|
511
|
+
if (!__privateGet$7(this, _namespaces).user)
|
512
|
+
__privateGet$7(this, _namespaces).user = new UserApi(__privateGet$7(this, _extraProps));
|
513
|
+
return __privateGet$7(this, _namespaces).user;
|
481
514
|
}
|
482
515
|
get workspaces() {
|
483
|
-
if (!__privateGet$
|
484
|
-
__privateGet$
|
485
|
-
return __privateGet$
|
516
|
+
if (!__privateGet$7(this, _namespaces).workspaces)
|
517
|
+
__privateGet$7(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$7(this, _extraProps));
|
518
|
+
return __privateGet$7(this, _namespaces).workspaces;
|
486
519
|
}
|
487
520
|
get databases() {
|
488
|
-
if (!__privateGet$
|
489
|
-
__privateGet$
|
490
|
-
return __privateGet$
|
521
|
+
if (!__privateGet$7(this, _namespaces).databases)
|
522
|
+
__privateGet$7(this, _namespaces).databases = new DatabaseApi(__privateGet$7(this, _extraProps));
|
523
|
+
return __privateGet$7(this, _namespaces).databases;
|
491
524
|
}
|
492
525
|
get branches() {
|
493
|
-
if (!__privateGet$
|
494
|
-
__privateGet$
|
495
|
-
return __privateGet$
|
526
|
+
if (!__privateGet$7(this, _namespaces).branches)
|
527
|
+
__privateGet$7(this, _namespaces).branches = new BranchApi(__privateGet$7(this, _extraProps));
|
528
|
+
return __privateGet$7(this, _namespaces).branches;
|
496
529
|
}
|
497
530
|
get tables() {
|
498
|
-
if (!__privateGet$
|
499
|
-
__privateGet$
|
500
|
-
return __privateGet$
|
531
|
+
if (!__privateGet$7(this, _namespaces).tables)
|
532
|
+
__privateGet$7(this, _namespaces).tables = new TableApi(__privateGet$7(this, _extraProps));
|
533
|
+
return __privateGet$7(this, _namespaces).tables;
|
501
534
|
}
|
502
535
|
get records() {
|
503
|
-
if (!__privateGet$
|
504
|
-
__privateGet$
|
505
|
-
return __privateGet$
|
536
|
+
if (!__privateGet$7(this, _namespaces).records)
|
537
|
+
__privateGet$7(this, _namespaces).records = new RecordsApi(__privateGet$7(this, _extraProps));
|
538
|
+
return __privateGet$7(this, _namespaces).records;
|
506
539
|
}
|
507
540
|
}
|
508
541
|
_extraProps = new WeakMap();
|
@@ -636,6 +669,33 @@ class DatabaseApi {
|
|
636
669
|
...this.extraProps
|
637
670
|
});
|
638
671
|
}
|
672
|
+
getGitBranchesMapping(workspace, dbName) {
|
673
|
+
return operationsByTag.database.getGitBranchesMapping({
|
674
|
+
pathParams: { workspace, dbName },
|
675
|
+
...this.extraProps
|
676
|
+
});
|
677
|
+
}
|
678
|
+
addGitBranchesEntry(workspace, dbName, body) {
|
679
|
+
return operationsByTag.database.addGitBranchesEntry({
|
680
|
+
pathParams: { workspace, dbName },
|
681
|
+
body,
|
682
|
+
...this.extraProps
|
683
|
+
});
|
684
|
+
}
|
685
|
+
removeGitBranchesEntry(workspace, dbName, gitBranch) {
|
686
|
+
return operationsByTag.database.removeGitBranchesEntry({
|
687
|
+
pathParams: { workspace, dbName },
|
688
|
+
queryParams: { gitBranch },
|
689
|
+
...this.extraProps
|
690
|
+
});
|
691
|
+
}
|
692
|
+
resolveBranch(workspace, dbName, gitBranch) {
|
693
|
+
return operationsByTag.database.resolveBranch({
|
694
|
+
pathParams: { workspace, dbName },
|
695
|
+
queryParams: { gitBranch },
|
696
|
+
...this.extraProps
|
697
|
+
});
|
698
|
+
}
|
639
699
|
}
|
640
700
|
class BranchApi {
|
641
701
|
constructor(extraProps) {
|
@@ -653,10 +713,10 @@ class BranchApi {
|
|
653
713
|
...this.extraProps
|
654
714
|
});
|
655
715
|
}
|
656
|
-
createBranch(workspace, database, branch, from
|
716
|
+
createBranch(workspace, database, branch, from, options = {}) {
|
657
717
|
return operationsByTag.branch.createBranch({
|
658
718
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
659
|
-
queryParams: { from },
|
719
|
+
queryParams: isString(from) ? { from } : void 0,
|
660
720
|
body: options,
|
661
721
|
...this.extraProps
|
662
722
|
});
|
@@ -838,6 +898,13 @@ class RecordsApi {
|
|
838
898
|
...this.extraProps
|
839
899
|
});
|
840
900
|
}
|
901
|
+
searchTable(workspace, database, branch, tableName, query) {
|
902
|
+
return operationsByTag.records.searchTable({
|
903
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
904
|
+
body: query,
|
905
|
+
...this.extraProps
|
906
|
+
});
|
907
|
+
}
|
841
908
|
searchBranch(workspace, database, branch, query) {
|
842
909
|
return operationsByTag.records.searchBranch({
|
843
910
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
@@ -861,7 +928,7 @@ var __accessCheck$6 = (obj, member, msg) => {
|
|
861
928
|
if (!member.has(obj))
|
862
929
|
throw TypeError("Cannot " + msg);
|
863
930
|
};
|
864
|
-
var __privateGet$
|
931
|
+
var __privateGet$6 = (obj, member, getter) => {
|
865
932
|
__accessCheck$6(obj, member, "read from private field");
|
866
933
|
return getter ? getter.call(obj) : member.get(obj);
|
867
934
|
};
|
@@ -870,7 +937,7 @@ var __privateAdd$6 = (obj, member, value) => {
|
|
870
937
|
throw TypeError("Cannot add the same private member more than once");
|
871
938
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
872
939
|
};
|
873
|
-
var __privateSet$
|
940
|
+
var __privateSet$5 = (obj, member, value, setter) => {
|
874
941
|
__accessCheck$6(obj, member, "write to private field");
|
875
942
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
876
943
|
return value;
|
@@ -879,21 +946,21 @@ var _query;
|
|
879
946
|
class Page {
|
880
947
|
constructor(query, meta, records = []) {
|
881
948
|
__privateAdd$6(this, _query, void 0);
|
882
|
-
__privateSet$
|
949
|
+
__privateSet$5(this, _query, query);
|
883
950
|
this.meta = meta;
|
884
951
|
this.records = records;
|
885
952
|
}
|
886
953
|
async nextPage(size, offset) {
|
887
|
-
return __privateGet$
|
954
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
|
888
955
|
}
|
889
956
|
async previousPage(size, offset) {
|
890
|
-
return __privateGet$
|
957
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, before: this.meta.page.cursor } });
|
891
958
|
}
|
892
959
|
async firstPage(size, offset) {
|
893
|
-
return __privateGet$
|
960
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, first: this.meta.page.cursor } });
|
894
961
|
}
|
895
962
|
async lastPage(size, offset) {
|
896
|
-
return __privateGet$
|
963
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, last: this.meta.page.cursor } });
|
897
964
|
}
|
898
965
|
hasNextPage() {
|
899
966
|
return this.meta.page.more;
|
@@ -904,12 +971,15 @@ const PAGINATION_MAX_SIZE = 200;
|
|
904
971
|
const PAGINATION_DEFAULT_SIZE = 200;
|
905
972
|
const PAGINATION_MAX_OFFSET = 800;
|
906
973
|
const PAGINATION_DEFAULT_OFFSET = 0;
|
974
|
+
function isCursorPaginationOptions(options) {
|
975
|
+
return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
|
976
|
+
}
|
907
977
|
|
908
978
|
var __accessCheck$5 = (obj, member, msg) => {
|
909
979
|
if (!member.has(obj))
|
910
980
|
throw TypeError("Cannot " + msg);
|
911
981
|
};
|
912
|
-
var __privateGet$
|
982
|
+
var __privateGet$5 = (obj, member, getter) => {
|
913
983
|
__accessCheck$5(obj, member, "read from private field");
|
914
984
|
return getter ? getter.call(obj) : member.get(obj);
|
915
985
|
};
|
@@ -918,34 +988,35 @@ var __privateAdd$5 = (obj, member, value) => {
|
|
918
988
|
throw TypeError("Cannot add the same private member more than once");
|
919
989
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
920
990
|
};
|
921
|
-
var __privateSet$
|
991
|
+
var __privateSet$4 = (obj, member, value, setter) => {
|
922
992
|
__accessCheck$5(obj, member, "write to private field");
|
923
993
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
924
994
|
return value;
|
925
995
|
};
|
926
996
|
var _table$1, _repository, _data;
|
927
997
|
const _Query = class {
|
928
|
-
constructor(repository, table, data,
|
998
|
+
constructor(repository, table, data, rawParent) {
|
929
999
|
__privateAdd$5(this, _table$1, void 0);
|
930
1000
|
__privateAdd$5(this, _repository, void 0);
|
931
1001
|
__privateAdd$5(this, _data, { filter: {} });
|
932
1002
|
this.meta = { page: { cursor: "start", more: true } };
|
933
1003
|
this.records = [];
|
934
|
-
__privateSet$
|
1004
|
+
__privateSet$4(this, _table$1, table);
|
935
1005
|
if (repository) {
|
936
|
-
__privateSet$
|
1006
|
+
__privateSet$4(this, _repository, repository);
|
937
1007
|
} else {
|
938
|
-
__privateSet$
|
1008
|
+
__privateSet$4(this, _repository, this);
|
939
1009
|
}
|
940
|
-
|
941
|
-
__privateGet$
|
942
|
-
__privateGet$
|
943
|
-
__privateGet$
|
944
|
-
__privateGet$
|
945
|
-
__privateGet$
|
946
|
-
__privateGet$
|
947
|
-
__privateGet$
|
948
|
-
__privateGet$
|
1010
|
+
const parent = cleanParent(data, rawParent);
|
1011
|
+
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
1012
|
+
__privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
|
1013
|
+
__privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
|
1014
|
+
__privateGet$5(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
|
1015
|
+
__privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
|
1016
|
+
__privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
|
1017
|
+
__privateGet$5(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
|
1018
|
+
__privateGet$5(this, _data).pagination = data.pagination ?? parent?.pagination;
|
1019
|
+
__privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
|
949
1020
|
this.any = this.any.bind(this);
|
950
1021
|
this.all = this.all.bind(this);
|
951
1022
|
this.not = this.not.bind(this);
|
@@ -956,83 +1027,85 @@ const _Query = class {
|
|
956
1027
|
Object.defineProperty(this, "repository", { enumerable: false });
|
957
1028
|
}
|
958
1029
|
getQueryOptions() {
|
959
|
-
return __privateGet$
|
1030
|
+
return __privateGet$5(this, _data);
|
960
1031
|
}
|
961
1032
|
key() {
|
962
|
-
const { columns = [], filter = {}, sort = [],
|
963
|
-
const key = JSON.stringify({ columns, filter, sort,
|
1033
|
+
const { columns = [], filter = {}, sort = [], pagination = {} } = __privateGet$5(this, _data);
|
1034
|
+
const key = JSON.stringify({ columns, filter, sort, pagination });
|
964
1035
|
return toBase64(key);
|
965
1036
|
}
|
966
1037
|
any(...queries) {
|
967
1038
|
const $any = queries.map((query) => query.getQueryOptions().filter ?? {});
|
968
|
-
return new _Query(__privateGet$
|
1039
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $any } }, __privateGet$5(this, _data));
|
969
1040
|
}
|
970
1041
|
all(...queries) {
|
971
1042
|
const $all = queries.map((query) => query.getQueryOptions().filter ?? {});
|
972
|
-
return new _Query(__privateGet$
|
1043
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
973
1044
|
}
|
974
1045
|
not(...queries) {
|
975
1046
|
const $not = queries.map((query) => query.getQueryOptions().filter ?? {});
|
976
|
-
return new _Query(__privateGet$
|
1047
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $not } }, __privateGet$5(this, _data));
|
977
1048
|
}
|
978
1049
|
none(...queries) {
|
979
1050
|
const $none = queries.map((query) => query.getQueryOptions().filter ?? {});
|
980
|
-
return new _Query(__privateGet$
|
1051
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $none } }, __privateGet$5(this, _data));
|
981
1052
|
}
|
982
1053
|
filter(a, b) {
|
983
1054
|
if (arguments.length === 1) {
|
984
1055
|
const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
|
985
|
-
const $all = compact([__privateGet$
|
986
|
-
return new _Query(__privateGet$
|
1056
|
+
const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat(constraints));
|
1057
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
987
1058
|
} else {
|
988
|
-
const $all = compact([__privateGet$
|
989
|
-
return new _Query(__privateGet$
|
1059
|
+
const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
|
1060
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
990
1061
|
}
|
991
1062
|
}
|
992
1063
|
sort(column, direction) {
|
993
|
-
const originalSort = [__privateGet$
|
1064
|
+
const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
|
994
1065
|
const sort = [...originalSort, { column, direction }];
|
995
|
-
return new _Query(__privateGet$
|
1066
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
|
996
1067
|
}
|
997
1068
|
select(columns) {
|
998
|
-
return new _Query(__privateGet$
|
1069
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { columns }, __privateGet$5(this, _data));
|
999
1070
|
}
|
1000
1071
|
getPaginated(options = {}) {
|
1001
|
-
const query = new _Query(__privateGet$
|
1002
|
-
return __privateGet$
|
1072
|
+
const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
|
1073
|
+
return __privateGet$5(this, _repository).query(query);
|
1003
1074
|
}
|
1004
1075
|
async *[Symbol.asyncIterator]() {
|
1005
|
-
for await (const [record] of this.getIterator(1)) {
|
1076
|
+
for await (const [record] of this.getIterator({ batchSize: 1 })) {
|
1006
1077
|
yield record;
|
1007
1078
|
}
|
1008
1079
|
}
|
1009
|
-
async *getIterator(
|
1010
|
-
|
1011
|
-
let
|
1012
|
-
|
1013
|
-
|
1014
|
-
|
1015
|
-
|
1016
|
-
|
1080
|
+
async *getIterator(options = {}) {
|
1081
|
+
const { batchSize = 1 } = options;
|
1082
|
+
let page = await this.getPaginated({ ...options, pagination: { size: batchSize, offset: 0 } });
|
1083
|
+
let more = page.hasNextPage();
|
1084
|
+
yield page.records;
|
1085
|
+
while (more) {
|
1086
|
+
page = await page.nextPage();
|
1087
|
+
more = page.hasNextPage();
|
1088
|
+
yield page.records;
|
1017
1089
|
}
|
1018
1090
|
}
|
1019
1091
|
async getMany(options = {}) {
|
1020
1092
|
const { records } = await this.getPaginated(options);
|
1021
1093
|
return records;
|
1022
1094
|
}
|
1023
|
-
async getAll(
|
1095
|
+
async getAll(options = {}) {
|
1096
|
+
const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
|
1024
1097
|
const results = [];
|
1025
|
-
for await (const page of this.getIterator(
|
1098
|
+
for await (const page of this.getIterator({ ...rest, batchSize })) {
|
1026
1099
|
results.push(...page);
|
1027
1100
|
}
|
1028
1101
|
return results;
|
1029
1102
|
}
|
1030
1103
|
async getFirst(options = {}) {
|
1031
|
-
const records = await this.getMany({ ...options,
|
1032
|
-
return records[0]
|
1104
|
+
const records = await this.getMany({ ...options, pagination: { size: 1 } });
|
1105
|
+
return records[0] ?? null;
|
1033
1106
|
}
|
1034
1107
|
cache(ttl) {
|
1035
|
-
return new _Query(__privateGet$
|
1108
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
|
1036
1109
|
}
|
1037
1110
|
nextPage(size, offset) {
|
1038
1111
|
return this.firstPage(size, offset);
|
@@ -1041,10 +1114,10 @@ const _Query = class {
|
|
1041
1114
|
return this.firstPage(size, offset);
|
1042
1115
|
}
|
1043
1116
|
firstPage(size, offset) {
|
1044
|
-
return this.getPaginated({
|
1117
|
+
return this.getPaginated({ pagination: { size, offset } });
|
1045
1118
|
}
|
1046
1119
|
lastPage(size, offset) {
|
1047
|
-
return this.getPaginated({
|
1120
|
+
return this.getPaginated({ pagination: { size, offset, before: "end" } });
|
1048
1121
|
}
|
1049
1122
|
hasNextPage() {
|
1050
1123
|
return this.meta.page.more;
|
@@ -1054,6 +1127,12 @@ let Query = _Query;
|
|
1054
1127
|
_table$1 = new WeakMap();
|
1055
1128
|
_repository = new WeakMap();
|
1056
1129
|
_data = new WeakMap();
|
1130
|
+
function cleanParent(data, parent) {
|
1131
|
+
if (isCursorPaginationOptions(data.pagination)) {
|
1132
|
+
return { ...parent, sorting: void 0, filter: void 0 };
|
1133
|
+
}
|
1134
|
+
return parent;
|
1135
|
+
}
|
1057
1136
|
|
1058
1137
|
function isIdentifiable(x) {
|
1059
1138
|
return isObject(x) && isString(x?.id);
|
@@ -1089,7 +1168,7 @@ var __accessCheck$4 = (obj, member, msg) => {
|
|
1089
1168
|
if (!member.has(obj))
|
1090
1169
|
throw TypeError("Cannot " + msg);
|
1091
1170
|
};
|
1092
|
-
var __privateGet$
|
1171
|
+
var __privateGet$4 = (obj, member, getter) => {
|
1093
1172
|
__accessCheck$4(obj, member, "read from private field");
|
1094
1173
|
return getter ? getter.call(obj) : member.get(obj);
|
1095
1174
|
};
|
@@ -1098,7 +1177,7 @@ var __privateAdd$4 = (obj, member, value) => {
|
|
1098
1177
|
throw TypeError("Cannot add the same private member more than once");
|
1099
1178
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1100
1179
|
};
|
1101
|
-
var __privateSet$
|
1180
|
+
var __privateSet$3 = (obj, member, value, setter) => {
|
1102
1181
|
__accessCheck$4(obj, member, "write to private field");
|
1103
1182
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1104
1183
|
return value;
|
@@ -1107,7 +1186,7 @@ var __privateMethod$2 = (obj, member, method) => {
|
|
1107
1186
|
__accessCheck$4(obj, member, "access private method");
|
1108
1187
|
return method;
|
1109
1188
|
};
|
1110
|
-
var _table,
|
1189
|
+
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;
|
1111
1190
|
class Repository extends Query {
|
1112
1191
|
}
|
1113
1192
|
class RestRepository extends Query {
|
@@ -1124,18 +1203,20 @@ class RestRepository extends Query {
|
|
1124
1203
|
__privateAdd$4(this, _getCacheRecord);
|
1125
1204
|
__privateAdd$4(this, _setCacheQuery);
|
1126
1205
|
__privateAdd$4(this, _getCacheQuery);
|
1206
|
+
__privateAdd$4(this, _getSchema$1);
|
1127
1207
|
__privateAdd$4(this, _table, void 0);
|
1128
|
-
__privateAdd$4(this, _links, void 0);
|
1129
1208
|
__privateAdd$4(this, _getFetchProps, void 0);
|
1130
1209
|
__privateAdd$4(this, _cache, void 0);
|
1131
|
-
|
1132
|
-
__privateSet$
|
1133
|
-
__privateSet$
|
1210
|
+
__privateAdd$4(this, _schema$1, void 0);
|
1211
|
+
__privateSet$3(this, _table, options.table);
|
1212
|
+
__privateSet$3(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
1134
1213
|
this.db = options.db;
|
1135
|
-
__privateSet$
|
1214
|
+
__privateSet$3(this, _cache, options.pluginOptions.cache);
|
1136
1215
|
}
|
1137
1216
|
async create(a, b) {
|
1138
1217
|
if (Array.isArray(a)) {
|
1218
|
+
if (a.length === 0)
|
1219
|
+
return [];
|
1139
1220
|
const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
|
1140
1221
|
await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
|
1141
1222
|
return records;
|
@@ -1161,26 +1242,36 @@ class RestRepository extends Query {
|
|
1161
1242
|
}
|
1162
1243
|
throw new Error("Invalid arguments for create method");
|
1163
1244
|
}
|
1164
|
-
async read(
|
1165
|
-
|
1166
|
-
|
1167
|
-
|
1168
|
-
|
1169
|
-
|
1170
|
-
|
1171
|
-
|
1172
|
-
|
1173
|
-
|
1174
|
-
|
1175
|
-
|
1176
|
-
|
1177
|
-
|
1245
|
+
async read(a) {
|
1246
|
+
if (Array.isArray(a)) {
|
1247
|
+
if (a.length === 0)
|
1248
|
+
return [];
|
1249
|
+
return this.getAll({ filter: { id: { $any: a } } });
|
1250
|
+
}
|
1251
|
+
if (isString(a)) {
|
1252
|
+
const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, a);
|
1253
|
+
if (cacheRecord)
|
1254
|
+
return cacheRecord;
|
1255
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1256
|
+
try {
|
1257
|
+
const response = await getRecord({
|
1258
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: a },
|
1259
|
+
...fetchProps
|
1260
|
+
});
|
1261
|
+
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1262
|
+
return initObject(this.db, schema, __privateGet$4(this, _table), response);
|
1263
|
+
} catch (e) {
|
1264
|
+
if (isObject(e) && e.status === 404) {
|
1265
|
+
return null;
|
1266
|
+
}
|
1267
|
+
throw e;
|
1178
1268
|
}
|
1179
|
-
throw e;
|
1180
1269
|
}
|
1181
1270
|
}
|
1182
1271
|
async update(a, b) {
|
1183
1272
|
if (Array.isArray(a)) {
|
1273
|
+
if (a.length === 0)
|
1274
|
+
return [];
|
1184
1275
|
if (a.length > 100) {
|
1185
1276
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1186
1277
|
}
|
@@ -1202,6 +1293,8 @@ class RestRepository extends Query {
|
|
1202
1293
|
}
|
1203
1294
|
async createOrUpdate(a, b) {
|
1204
1295
|
if (Array.isArray(a)) {
|
1296
|
+
if (a.length === 0)
|
1297
|
+
return [];
|
1205
1298
|
if (a.length > 100) {
|
1206
1299
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1207
1300
|
}
|
@@ -1223,6 +1316,8 @@ class RestRepository extends Query {
|
|
1223
1316
|
}
|
1224
1317
|
async delete(a) {
|
1225
1318
|
if (Array.isArray(a)) {
|
1319
|
+
if (a.length === 0)
|
1320
|
+
return;
|
1226
1321
|
if (a.length > 100) {
|
1227
1322
|
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
1228
1323
|
}
|
@@ -1242,13 +1337,19 @@ class RestRepository extends Query {
|
|
1242
1337
|
throw new Error("Invalid arguments for delete method");
|
1243
1338
|
}
|
1244
1339
|
async search(query, options = {}) {
|
1245
|
-
const fetchProps = await __privateGet$
|
1246
|
-
const { records } = await
|
1247
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1248
|
-
body: {
|
1340
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1341
|
+
const { records } = await searchTable({
|
1342
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1343
|
+
body: {
|
1344
|
+
query,
|
1345
|
+
fuzziness: options.fuzziness,
|
1346
|
+
highlight: options.highlight,
|
1347
|
+
filter: options.filter
|
1348
|
+
},
|
1249
1349
|
...fetchProps
|
1250
1350
|
});
|
1251
|
-
|
1351
|
+
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1352
|
+
return records.map((item) => initObject(this.db, schema, __privateGet$4(this, _table), item));
|
1252
1353
|
}
|
1253
1354
|
async query(query) {
|
1254
1355
|
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
@@ -1257,34 +1358,35 @@ class RestRepository extends Query {
|
|
1257
1358
|
const data = query.getQueryOptions();
|
1258
1359
|
const body = {
|
1259
1360
|
filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
|
1260
|
-
sort: data.sort ? buildSortFilter(data.sort) : void 0,
|
1261
|
-
page: data.
|
1361
|
+
sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
|
1362
|
+
page: data.pagination,
|
1262
1363
|
columns: data.columns
|
1263
1364
|
};
|
1264
|
-
const fetchProps = await __privateGet$
|
1365
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1265
1366
|
const { meta, records: objects } = await queryTable({
|
1266
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1367
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1267
1368
|
body,
|
1268
1369
|
...fetchProps
|
1269
1370
|
});
|
1270
|
-
const
|
1371
|
+
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1372
|
+
const records = objects.map((record) => initObject(this.db, schema, __privateGet$4(this, _table), record));
|
1271
1373
|
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1272
1374
|
return new Page(query, meta, records);
|
1273
1375
|
}
|
1274
1376
|
}
|
1275
1377
|
_table = new WeakMap();
|
1276
|
-
_links = new WeakMap();
|
1277
1378
|
_getFetchProps = new WeakMap();
|
1278
1379
|
_cache = new WeakMap();
|
1380
|
+
_schema$1 = new WeakMap();
|
1279
1381
|
_insertRecordWithoutId = new WeakSet();
|
1280
1382
|
insertRecordWithoutId_fn = async function(object) {
|
1281
|
-
const fetchProps = await __privateGet$
|
1383
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1282
1384
|
const record = transformObjectLinks(object);
|
1283
1385
|
const response = await insertRecord({
|
1284
1386
|
pathParams: {
|
1285
1387
|
workspace: "{workspaceId}",
|
1286
1388
|
dbBranchName: "{dbBranch}",
|
1287
|
-
tableName: __privateGet$
|
1389
|
+
tableName: __privateGet$4(this, _table)
|
1288
1390
|
},
|
1289
1391
|
body: record,
|
1290
1392
|
...fetchProps
|
@@ -1297,13 +1399,13 @@ insertRecordWithoutId_fn = async function(object) {
|
|
1297
1399
|
};
|
1298
1400
|
_insertRecordWithId = new WeakSet();
|
1299
1401
|
insertRecordWithId_fn = async function(recordId, object) {
|
1300
|
-
const fetchProps = await __privateGet$
|
1402
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1301
1403
|
const record = transformObjectLinks(object);
|
1302
1404
|
const response = await insertRecordWithID({
|
1303
1405
|
pathParams: {
|
1304
1406
|
workspace: "{workspaceId}",
|
1305
1407
|
dbBranchName: "{dbBranch}",
|
1306
|
-
tableName: __privateGet$
|
1408
|
+
tableName: __privateGet$4(this, _table),
|
1307
1409
|
recordId
|
1308
1410
|
},
|
1309
1411
|
body: record,
|
@@ -1318,10 +1420,10 @@ insertRecordWithId_fn = async function(recordId, object) {
|
|
1318
1420
|
};
|
1319
1421
|
_bulkInsertTableRecords = new WeakSet();
|
1320
1422
|
bulkInsertTableRecords_fn = async function(objects) {
|
1321
|
-
const fetchProps = await __privateGet$
|
1423
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1322
1424
|
const records = objects.map((object) => transformObjectLinks(object));
|
1323
1425
|
const response = await bulkInsertTableRecords({
|
1324
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1426
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1325
1427
|
body: { records },
|
1326
1428
|
...fetchProps
|
1327
1429
|
});
|
@@ -1333,10 +1435,10 @@ bulkInsertTableRecords_fn = async function(objects) {
|
|
1333
1435
|
};
|
1334
1436
|
_updateRecordWithID = new WeakSet();
|
1335
1437
|
updateRecordWithID_fn = async function(recordId, object) {
|
1336
|
-
const fetchProps = await __privateGet$
|
1438
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1337
1439
|
const record = transformObjectLinks(object);
|
1338
1440
|
const response = await updateRecordWithID({
|
1339
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1441
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1340
1442
|
body: record,
|
1341
1443
|
...fetchProps
|
1342
1444
|
});
|
@@ -1347,9 +1449,9 @@ updateRecordWithID_fn = async function(recordId, object) {
|
|
1347
1449
|
};
|
1348
1450
|
_upsertRecordWithID = new WeakSet();
|
1349
1451
|
upsertRecordWithID_fn = async function(recordId, object) {
|
1350
|
-
const fetchProps = await __privateGet$
|
1452
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1351
1453
|
const response = await upsertRecordWithID({
|
1352
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1454
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1353
1455
|
body: object,
|
1354
1456
|
...fetchProps
|
1355
1457
|
});
|
@@ -1360,51 +1462,63 @@ upsertRecordWithID_fn = async function(recordId, object) {
|
|
1360
1462
|
};
|
1361
1463
|
_deleteRecord = new WeakSet();
|
1362
1464
|
deleteRecord_fn = async function(recordId) {
|
1363
|
-
const fetchProps = await __privateGet$
|
1465
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1364
1466
|
await deleteRecord({
|
1365
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1467
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1366
1468
|
...fetchProps
|
1367
1469
|
});
|
1368
1470
|
};
|
1369
1471
|
_invalidateCache = new WeakSet();
|
1370
1472
|
invalidateCache_fn = async function(recordId) {
|
1371
|
-
await __privateGet$
|
1372
|
-
const cacheItems = await __privateGet$
|
1473
|
+
await __privateGet$4(this, _cache).delete(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1474
|
+
const cacheItems = await __privateGet$4(this, _cache).getAll();
|
1373
1475
|
const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
|
1374
1476
|
for (const [key, value] of queries) {
|
1375
1477
|
const ids = getIds(value);
|
1376
1478
|
if (ids.includes(recordId))
|
1377
|
-
await __privateGet$
|
1479
|
+
await __privateGet$4(this, _cache).delete(key);
|
1378
1480
|
}
|
1379
1481
|
};
|
1380
1482
|
_setCacheRecord = new WeakSet();
|
1381
1483
|
setCacheRecord_fn = async function(record) {
|
1382
|
-
if (!__privateGet$
|
1484
|
+
if (!__privateGet$4(this, _cache).cacheRecords)
|
1383
1485
|
return;
|
1384
|
-
await __privateGet$
|
1486
|
+
await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
|
1385
1487
|
};
|
1386
1488
|
_getCacheRecord = new WeakSet();
|
1387
1489
|
getCacheRecord_fn = async function(recordId) {
|
1388
|
-
if (!__privateGet$
|
1490
|
+
if (!__privateGet$4(this, _cache).cacheRecords)
|
1389
1491
|
return null;
|
1390
|
-
return __privateGet$
|
1492
|
+
return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1391
1493
|
};
|
1392
1494
|
_setCacheQuery = new WeakSet();
|
1393
1495
|
setCacheQuery_fn = async function(query, meta, records) {
|
1394
|
-
await __privateGet$
|
1496
|
+
await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
|
1395
1497
|
};
|
1396
1498
|
_getCacheQuery = new WeakSet();
|
1397
1499
|
getCacheQuery_fn = async function(query) {
|
1398
|
-
const key = `query_${__privateGet$
|
1399
|
-
const result = await __privateGet$
|
1500
|
+
const key = `query_${__privateGet$4(this, _table)}:${query.key()}`;
|
1501
|
+
const result = await __privateGet$4(this, _cache).get(key);
|
1400
1502
|
if (!result)
|
1401
1503
|
return null;
|
1402
|
-
const { cache: ttl = __privateGet$
|
1403
|
-
if (
|
1404
|
-
return
|
1504
|
+
const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
|
1505
|
+
if (ttl < 0)
|
1506
|
+
return null;
|
1405
1507
|
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1406
1508
|
return hasExpired ? null : result;
|
1407
1509
|
};
|
1510
|
+
_getSchema$1 = new WeakSet();
|
1511
|
+
getSchema_fn$1 = async function() {
|
1512
|
+
if (__privateGet$4(this, _schema$1))
|
1513
|
+
return __privateGet$4(this, _schema$1);
|
1514
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1515
|
+
const { schema } = await getBranchDetails({
|
1516
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1517
|
+
...fetchProps
|
1518
|
+
});
|
1519
|
+
__privateSet$3(this, _schema$1, schema);
|
1520
|
+
return schema;
|
1521
|
+
};
|
1408
1522
|
const transformObjectLinks = (object) => {
|
1409
1523
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
1410
1524
|
if (key === "xata")
|
@@ -1412,15 +1526,33 @@ const transformObjectLinks = (object) => {
|
|
1412
1526
|
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1413
1527
|
}, {});
|
1414
1528
|
};
|
1415
|
-
const initObject = (db,
|
1529
|
+
const initObject = (db, schema, table, object) => {
|
1416
1530
|
const result = {};
|
1417
1531
|
Object.assign(result, object);
|
1418
|
-
const
|
1419
|
-
|
1420
|
-
|
1421
|
-
|
1422
|
-
|
1423
|
-
|
1532
|
+
const { columns } = schema.tables.find(({ name }) => name === table) ?? {};
|
1533
|
+
if (!columns)
|
1534
|
+
console.error(`Table ${table} not found in schema`);
|
1535
|
+
for (const column of columns ?? []) {
|
1536
|
+
const value = result[column.name];
|
1537
|
+
switch (column.type) {
|
1538
|
+
case "datetime": {
|
1539
|
+
const date = value !== void 0 ? new Date(value) : void 0;
|
1540
|
+
if (date && isNaN(date.getTime())) {
|
1541
|
+
console.error(`Failed to parse date ${value} for field ${column.name}`);
|
1542
|
+
} else if (date) {
|
1543
|
+
result[column.name] = date;
|
1544
|
+
}
|
1545
|
+
break;
|
1546
|
+
}
|
1547
|
+
case "link": {
|
1548
|
+
const linkTable = column.link?.table;
|
1549
|
+
if (!linkTable) {
|
1550
|
+
console.error(`Failed to parse link for field ${column.name}`);
|
1551
|
+
} else if (isObject(value)) {
|
1552
|
+
result[column.name] = initObject(db, schema, linkTable, value);
|
1553
|
+
}
|
1554
|
+
break;
|
1555
|
+
}
|
1424
1556
|
}
|
1425
1557
|
}
|
1426
1558
|
result.read = function() {
|
@@ -1452,7 +1584,7 @@ var __accessCheck$3 = (obj, member, msg) => {
|
|
1452
1584
|
if (!member.has(obj))
|
1453
1585
|
throw TypeError("Cannot " + msg);
|
1454
1586
|
};
|
1455
|
-
var __privateGet$
|
1587
|
+
var __privateGet$3 = (obj, member, getter) => {
|
1456
1588
|
__accessCheck$3(obj, member, "read from private field");
|
1457
1589
|
return getter ? getter.call(obj) : member.get(obj);
|
1458
1590
|
};
|
@@ -1461,7 +1593,7 @@ var __privateAdd$3 = (obj, member, value) => {
|
|
1461
1593
|
throw TypeError("Cannot add the same private member more than once");
|
1462
1594
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1463
1595
|
};
|
1464
|
-
var __privateSet$
|
1596
|
+
var __privateSet$2 = (obj, member, value, setter) => {
|
1465
1597
|
__accessCheck$3(obj, member, "write to private field");
|
1466
1598
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1467
1599
|
return value;
|
@@ -1470,30 +1602,30 @@ var _map;
|
|
1470
1602
|
class SimpleCache {
|
1471
1603
|
constructor(options = {}) {
|
1472
1604
|
__privateAdd$3(this, _map, void 0);
|
1473
|
-
__privateSet$
|
1605
|
+
__privateSet$2(this, _map, /* @__PURE__ */ new Map());
|
1474
1606
|
this.capacity = options.max ?? 500;
|
1475
1607
|
this.cacheRecords = options.cacheRecords ?? true;
|
1476
1608
|
this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
|
1477
1609
|
}
|
1478
1610
|
async getAll() {
|
1479
|
-
return Object.fromEntries(__privateGet$
|
1611
|
+
return Object.fromEntries(__privateGet$3(this, _map));
|
1480
1612
|
}
|
1481
1613
|
async get(key) {
|
1482
|
-
return __privateGet$
|
1614
|
+
return __privateGet$3(this, _map).get(key) ?? null;
|
1483
1615
|
}
|
1484
1616
|
async set(key, value) {
|
1485
1617
|
await this.delete(key);
|
1486
|
-
__privateGet$
|
1487
|
-
if (__privateGet$
|
1488
|
-
const leastRecentlyUsed = __privateGet$
|
1618
|
+
__privateGet$3(this, _map).set(key, value);
|
1619
|
+
if (__privateGet$3(this, _map).size > this.capacity) {
|
1620
|
+
const leastRecentlyUsed = __privateGet$3(this, _map).keys().next().value;
|
1489
1621
|
await this.delete(leastRecentlyUsed);
|
1490
1622
|
}
|
1491
1623
|
}
|
1492
1624
|
async delete(key) {
|
1493
|
-
__privateGet$
|
1625
|
+
__privateGet$3(this, _map).delete(key);
|
1494
1626
|
}
|
1495
1627
|
async clear() {
|
1496
|
-
return __privateGet$
|
1628
|
+
return __privateGet$3(this, _map).clear();
|
1497
1629
|
}
|
1498
1630
|
}
|
1499
1631
|
_map = new WeakMap();
|
@@ -1521,7 +1653,7 @@ var __accessCheck$2 = (obj, member, msg) => {
|
|
1521
1653
|
if (!member.has(obj))
|
1522
1654
|
throw TypeError("Cannot " + msg);
|
1523
1655
|
};
|
1524
|
-
var __privateGet$
|
1656
|
+
var __privateGet$2 = (obj, member, getter) => {
|
1525
1657
|
__accessCheck$2(obj, member, "read from private field");
|
1526
1658
|
return getter ? getter.call(obj) : member.get(obj);
|
1527
1659
|
};
|
@@ -1532,26 +1664,24 @@ var __privateAdd$2 = (obj, member, value) => {
|
|
1532
1664
|
};
|
1533
1665
|
var _tables;
|
1534
1666
|
class SchemaPlugin extends XataPlugin {
|
1535
|
-
constructor(
|
1667
|
+
constructor(tableNames) {
|
1536
1668
|
super();
|
1537
|
-
this.links = links;
|
1538
1669
|
this.tableNames = tableNames;
|
1539
1670
|
__privateAdd$2(this, _tables, {});
|
1540
1671
|
}
|
1541
1672
|
build(pluginOptions) {
|
1542
|
-
const links = this.links;
|
1543
1673
|
const db = new Proxy({}, {
|
1544
1674
|
get: (_target, table) => {
|
1545
1675
|
if (!isString(table))
|
1546
1676
|
throw new Error("Invalid table name");
|
1547
|
-
if (
|
1548
|
-
__privateGet$
|
1677
|
+
if (__privateGet$2(this, _tables)[table] === void 0) {
|
1678
|
+
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
|
1549
1679
|
}
|
1550
|
-
return __privateGet$
|
1680
|
+
return __privateGet$2(this, _tables)[table];
|
1551
1681
|
}
|
1552
1682
|
});
|
1553
1683
|
for (const table of this.tableNames ?? []) {
|
1554
|
-
db[table] = new RestRepository({ db, pluginOptions, table
|
1684
|
+
db[table] = new RestRepository({ db, pluginOptions, table });
|
1555
1685
|
}
|
1556
1686
|
return db;
|
1557
1687
|
}
|
@@ -1562,55 +1692,80 @@ var __accessCheck$1 = (obj, member, msg) => {
|
|
1562
1692
|
if (!member.has(obj))
|
1563
1693
|
throw TypeError("Cannot " + msg);
|
1564
1694
|
};
|
1695
|
+
var __privateGet$1 = (obj, member, getter) => {
|
1696
|
+
__accessCheck$1(obj, member, "read from private field");
|
1697
|
+
return getter ? getter.call(obj) : member.get(obj);
|
1698
|
+
};
|
1565
1699
|
var __privateAdd$1 = (obj, member, value) => {
|
1566
1700
|
if (member.has(obj))
|
1567
1701
|
throw TypeError("Cannot add the same private member more than once");
|
1568
1702
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1569
1703
|
};
|
1704
|
+
var __privateSet$1 = (obj, member, value, setter) => {
|
1705
|
+
__accessCheck$1(obj, member, "write to private field");
|
1706
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1707
|
+
return value;
|
1708
|
+
};
|
1570
1709
|
var __privateMethod$1 = (obj, member, method) => {
|
1571
1710
|
__accessCheck$1(obj, member, "access private method");
|
1572
1711
|
return method;
|
1573
1712
|
};
|
1574
|
-
var _search, search_fn;
|
1713
|
+
var _schema, _search, search_fn, _getSchema, getSchema_fn;
|
1575
1714
|
class SearchPlugin extends XataPlugin {
|
1576
|
-
constructor(db
|
1715
|
+
constructor(db) {
|
1577
1716
|
super();
|
1578
1717
|
this.db = db;
|
1579
|
-
this.links = links;
|
1580
1718
|
__privateAdd$1(this, _search);
|
1719
|
+
__privateAdd$1(this, _getSchema);
|
1720
|
+
__privateAdd$1(this, _schema, void 0);
|
1581
1721
|
}
|
1582
1722
|
build({ getFetchProps }) {
|
1583
1723
|
return {
|
1584
1724
|
all: async (query, options = {}) => {
|
1585
1725
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1726
|
+
const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
|
1586
1727
|
return records.map((record) => {
|
1587
1728
|
const { table = "orphan" } = record.xata;
|
1588
|
-
return { table, record: initObject(this.db,
|
1729
|
+
return { table, record: initObject(this.db, schema, table, record) };
|
1589
1730
|
});
|
1590
1731
|
},
|
1591
1732
|
byTable: async (query, options = {}) => {
|
1592
1733
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1734
|
+
const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
|
1593
1735
|
return records.reduce((acc, record) => {
|
1594
1736
|
const { table = "orphan" } = record.xata;
|
1595
1737
|
const items = acc[table] ?? [];
|
1596
|
-
const item = initObject(this.db,
|
1738
|
+
const item = initObject(this.db, schema, table, record);
|
1597
1739
|
return { ...acc, [table]: [...items, item] };
|
1598
1740
|
}, {});
|
1599
1741
|
}
|
1600
1742
|
};
|
1601
1743
|
}
|
1602
1744
|
}
|
1745
|
+
_schema = new WeakMap();
|
1603
1746
|
_search = new WeakSet();
|
1604
1747
|
search_fn = async function(query, options, getFetchProps) {
|
1605
1748
|
const fetchProps = await getFetchProps();
|
1606
|
-
const { tables, fuzziness } = options ?? {};
|
1749
|
+
const { tables, fuzziness, highlight } = options ?? {};
|
1607
1750
|
const { records } = await searchBranch({
|
1608
1751
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1609
|
-
body: { tables, query, fuzziness },
|
1752
|
+
body: { tables, query, fuzziness, highlight },
|
1610
1753
|
...fetchProps
|
1611
1754
|
});
|
1612
1755
|
return records;
|
1613
1756
|
};
|
1757
|
+
_getSchema = new WeakSet();
|
1758
|
+
getSchema_fn = async function(getFetchProps) {
|
1759
|
+
if (__privateGet$1(this, _schema))
|
1760
|
+
return __privateGet$1(this, _schema);
|
1761
|
+
const fetchProps = await getFetchProps();
|
1762
|
+
const { schema } = await getBranchDetails({
|
1763
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1764
|
+
...fetchProps
|
1765
|
+
});
|
1766
|
+
__privateSet$1(this, _schema, schema);
|
1767
|
+
return schema;
|
1768
|
+
};
|
1614
1769
|
|
1615
1770
|
const isBranchStrategyBuilder = (strategy) => {
|
1616
1771
|
return typeof strategy === "function";
|
@@ -1622,30 +1777,39 @@ const envBranchNames = [
|
|
1622
1777
|
"CF_PAGES_BRANCH",
|
1623
1778
|
"BRANCH"
|
1624
1779
|
];
|
1625
|
-
const defaultBranch = "main";
|
1626
1780
|
async function getCurrentBranchName(options) {
|
1627
|
-
const env =
|
1628
|
-
if (env)
|
1629
|
-
|
1630
|
-
|
1631
|
-
|
1632
|
-
|
1633
|
-
|
1634
|
-
|
1635
|
-
|
1636
|
-
return defaultBranch;
|
1781
|
+
const env = getBranchByEnvVariable();
|
1782
|
+
if (env) {
|
1783
|
+
const details = await getDatabaseBranch(env, options);
|
1784
|
+
if (details)
|
1785
|
+
return env;
|
1786
|
+
console.warn(`Branch ${env} not found in Xata. Ignoring...`);
|
1787
|
+
}
|
1788
|
+
const gitBranch = await getGitBranch();
|
1789
|
+
return resolveXataBranch(gitBranch, options);
|
1637
1790
|
}
|
1638
1791
|
async function getCurrentBranchDetails(options) {
|
1639
|
-
const
|
1640
|
-
|
1641
|
-
|
1642
|
-
|
1643
|
-
|
1644
|
-
|
1645
|
-
|
1646
|
-
|
1647
|
-
|
1648
|
-
|
1792
|
+
const branch = await getCurrentBranchName(options);
|
1793
|
+
return getDatabaseBranch(branch, options);
|
1794
|
+
}
|
1795
|
+
async function resolveXataBranch(gitBranch, options) {
|
1796
|
+
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1797
|
+
const apiKey = options?.apiKey || getAPIKey();
|
1798
|
+
if (!databaseURL)
|
1799
|
+
throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
|
1800
|
+
if (!apiKey)
|
1801
|
+
throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
|
1802
|
+
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1803
|
+
const [workspace] = host.split(".");
|
1804
|
+
const { branch } = await resolveBranch({
|
1805
|
+
apiKey,
|
1806
|
+
apiUrl: databaseURL,
|
1807
|
+
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1808
|
+
workspacesApiUrl: `${protocol}//${host}`,
|
1809
|
+
pathParams: { dbName, workspace },
|
1810
|
+
queryParams: { gitBranch, fallbackBranch: getEnvVariable("XATA_FALLBACK_BRANCH") }
|
1811
|
+
});
|
1812
|
+
return branch;
|
1649
1813
|
}
|
1650
1814
|
async function getDatabaseBranch(branch, options) {
|
1651
1815
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
@@ -1719,7 +1883,7 @@ var __privateMethod = (obj, member, method) => {
|
|
1719
1883
|
const buildClient = (plugins) => {
|
1720
1884
|
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1721
1885
|
return _a = class {
|
1722
|
-
constructor(options = {},
|
1886
|
+
constructor(options = {}, tables) {
|
1723
1887
|
__privateAdd(this, _parseOptions);
|
1724
1888
|
__privateAdd(this, _getFetchProps);
|
1725
1889
|
__privateAdd(this, _evaluateBranch);
|
@@ -1729,12 +1893,12 @@ const buildClient = (plugins) => {
|
|
1729
1893
|
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
1730
1894
|
cache: safeOptions.cache
|
1731
1895
|
};
|
1732
|
-
const db = new SchemaPlugin(
|
1733
|
-
const search = new SearchPlugin(db
|
1896
|
+
const db = new SchemaPlugin(tables).build(pluginOptions);
|
1897
|
+
const search = new SearchPlugin(db).build(pluginOptions);
|
1734
1898
|
this.db = db;
|
1735
1899
|
this.search = search;
|
1736
1900
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
1737
|
-
if (
|
1901
|
+
if (namespace === void 0)
|
1738
1902
|
continue;
|
1739
1903
|
const result = namespace.build(pluginOptions);
|
1740
1904
|
if (result instanceof Promise) {
|
@@ -1751,7 +1915,7 @@ const buildClient = (plugins) => {
|
|
1751
1915
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1752
1916
|
const apiKey = options?.apiKey || getAPIKey();
|
1753
1917
|
const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
|
1754
|
-
const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
1918
|
+
const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
1755
1919
|
if (!databaseURL || !apiKey) {
|
1756
1920
|
throw new Error("Options databaseURL and apiKey are required");
|
1757
1921
|
}
|
@@ -1778,7 +1942,7 @@ const buildClient = (plugins) => {
|
|
1778
1942
|
}, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
|
1779
1943
|
if (__privateGet(this, _branch))
|
1780
1944
|
return __privateGet(this, _branch);
|
1781
|
-
if (
|
1945
|
+
if (param === void 0)
|
1782
1946
|
return void 0;
|
1783
1947
|
const strategies = Array.isArray(param) ? [...param] : [param];
|
1784
1948
|
const evaluateBranch = async (strategy) => {
|
@@ -1803,5 +1967,5 @@ class XataError extends Error {
|
|
1803
1967
|
}
|
1804
1968
|
}
|
1805
1969
|
|
1806
|
-
export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, Repository, RestRepository, SchemaPlugin, SearchPlugin, SimpleCache, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addTableColumn, buildClient, bulkInsertTableRecords, cancelWorkspaceMemberInvite, contains, createBranch, createDatabase, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, endsWith, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseURL, getRecord, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getWorkspace, getWorkspaceMembersList, getWorkspacesList, gt, gte, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isIdentifiable, isNot, isXataRecord, le, lt, lte, notExists, operationsByTag, pattern, queryTable, removeWorkspaceMember, resendWorkspaceMemberInvite, searchBranch, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
|
1970
|
+
export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, Repository, RestRepository, SchemaPlugin, SearchPlugin, SimpleCache, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addGitBranchesEntry, addTableColumn, buildClient, bulkInsertTableRecords, cancelWorkspaceMemberInvite, contains, createBranch, createDatabase, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, endsWith, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseURL, getGitBranchesMapping, getRecord, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getWorkspace, getWorkspaceMembersList, getWorkspacesList, gt, gte, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isCursorPaginationOptions, isIdentifiable, isNot, isXataRecord, le, lt, lte, notExists, operationsByTag, pattern, queryTable, removeGitBranchesEntry, removeWorkspaceMember, resendWorkspaceMemberInvite, resolveBranch, searchBranch, searchTable, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
|
1807
1971
|
//# sourceMappingURL=index.mjs.map
|