@xata.io/client 0.9.0 → 0.10.1
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 +32 -0
- package/dist/index.cjs +292 -175
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +229 -31
- package/dist/index.mjs +289 -176
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/tsconfig.json +1 -0
package/dist/index.mjs
CHANGED
@@ -34,7 +34,10 @@ function getEnvVariable(name) {
|
|
34
34
|
}
|
35
35
|
async function getGitBranch() {
|
36
36
|
try {
|
37
|
-
|
37
|
+
if (typeof require === "function") {
|
38
|
+
const req = require;
|
39
|
+
return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
|
40
|
+
}
|
38
41
|
} catch (err) {
|
39
42
|
}
|
40
43
|
try {
|
@@ -248,6 +251,14 @@ const deleteDatabase = (variables) => fetch$1({
|
|
248
251
|
method: "delete",
|
249
252
|
...variables
|
250
253
|
});
|
254
|
+
const getGitBranchesMapping = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "get", ...variables });
|
255
|
+
const addGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "post", ...variables });
|
256
|
+
const removeGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "delete", ...variables });
|
257
|
+
const resolveBranch = (variables) => fetch$1({
|
258
|
+
url: "/dbs/{dbName}/resolveBranch",
|
259
|
+
method: "get",
|
260
|
+
...variables
|
261
|
+
});
|
251
262
|
const getBranchDetails = (variables) => fetch$1({
|
252
263
|
url: "/db/{dbBranchName}",
|
253
264
|
method: "get",
|
@@ -376,7 +387,15 @@ const operationsByTag = {
|
|
376
387
|
resendWorkspaceMemberInvite,
|
377
388
|
acceptWorkspaceMemberInvite
|
378
389
|
},
|
379
|
-
database: {
|
390
|
+
database: {
|
391
|
+
getDatabaseList,
|
392
|
+
createDatabase,
|
393
|
+
deleteDatabase,
|
394
|
+
getGitBranchesMapping,
|
395
|
+
addGitBranchesEntry,
|
396
|
+
removeGitBranchesEntry,
|
397
|
+
resolveBranch
|
398
|
+
},
|
380
399
|
branch: {
|
381
400
|
getBranchList,
|
382
401
|
getBranchDetails,
|
@@ -443,7 +462,7 @@ var __accessCheck$7 = (obj, member, msg) => {
|
|
443
462
|
if (!member.has(obj))
|
444
463
|
throw TypeError("Cannot " + msg);
|
445
464
|
};
|
446
|
-
var __privateGet$
|
465
|
+
var __privateGet$7 = (obj, member, getter) => {
|
447
466
|
__accessCheck$7(obj, member, "read from private field");
|
448
467
|
return getter ? getter.call(obj) : member.get(obj);
|
449
468
|
};
|
@@ -452,7 +471,7 @@ var __privateAdd$7 = (obj, member, value) => {
|
|
452
471
|
throw TypeError("Cannot add the same private member more than once");
|
453
472
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
454
473
|
};
|
455
|
-
var __privateSet$
|
474
|
+
var __privateSet$6 = (obj, member, value, setter) => {
|
456
475
|
__accessCheck$7(obj, member, "write to private field");
|
457
476
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
458
477
|
return value;
|
@@ -467,7 +486,7 @@ class XataApiClient {
|
|
467
486
|
if (!apiKey) {
|
468
487
|
throw new Error("Could not resolve a valid apiKey");
|
469
488
|
}
|
470
|
-
__privateSet$
|
489
|
+
__privateSet$6(this, _extraProps, {
|
471
490
|
apiUrl: getHostUrl(provider, "main"),
|
472
491
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
473
492
|
fetchImpl: getFetchImplementation(options.fetch),
|
@@ -475,34 +494,34 @@ class XataApiClient {
|
|
475
494
|
});
|
476
495
|
}
|
477
496
|
get user() {
|
478
|
-
if (!__privateGet$
|
479
|
-
__privateGet$
|
480
|
-
return __privateGet$
|
497
|
+
if (!__privateGet$7(this, _namespaces).user)
|
498
|
+
__privateGet$7(this, _namespaces).user = new UserApi(__privateGet$7(this, _extraProps));
|
499
|
+
return __privateGet$7(this, _namespaces).user;
|
481
500
|
}
|
482
501
|
get workspaces() {
|
483
|
-
if (!__privateGet$
|
484
|
-
__privateGet$
|
485
|
-
return __privateGet$
|
502
|
+
if (!__privateGet$7(this, _namespaces).workspaces)
|
503
|
+
__privateGet$7(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$7(this, _extraProps));
|
504
|
+
return __privateGet$7(this, _namespaces).workspaces;
|
486
505
|
}
|
487
506
|
get databases() {
|
488
|
-
if (!__privateGet$
|
489
|
-
__privateGet$
|
490
|
-
return __privateGet$
|
507
|
+
if (!__privateGet$7(this, _namespaces).databases)
|
508
|
+
__privateGet$7(this, _namespaces).databases = new DatabaseApi(__privateGet$7(this, _extraProps));
|
509
|
+
return __privateGet$7(this, _namespaces).databases;
|
491
510
|
}
|
492
511
|
get branches() {
|
493
|
-
if (!__privateGet$
|
494
|
-
__privateGet$
|
495
|
-
return __privateGet$
|
512
|
+
if (!__privateGet$7(this, _namespaces).branches)
|
513
|
+
__privateGet$7(this, _namespaces).branches = new BranchApi(__privateGet$7(this, _extraProps));
|
514
|
+
return __privateGet$7(this, _namespaces).branches;
|
496
515
|
}
|
497
516
|
get tables() {
|
498
|
-
if (!__privateGet$
|
499
|
-
__privateGet$
|
500
|
-
return __privateGet$
|
517
|
+
if (!__privateGet$7(this, _namespaces).tables)
|
518
|
+
__privateGet$7(this, _namespaces).tables = new TableApi(__privateGet$7(this, _extraProps));
|
519
|
+
return __privateGet$7(this, _namespaces).tables;
|
501
520
|
}
|
502
521
|
get records() {
|
503
|
-
if (!__privateGet$
|
504
|
-
__privateGet$
|
505
|
-
return __privateGet$
|
522
|
+
if (!__privateGet$7(this, _namespaces).records)
|
523
|
+
__privateGet$7(this, _namespaces).records = new RecordsApi(__privateGet$7(this, _extraProps));
|
524
|
+
return __privateGet$7(this, _namespaces).records;
|
506
525
|
}
|
507
526
|
}
|
508
527
|
_extraProps = new WeakMap();
|
@@ -636,6 +655,33 @@ class DatabaseApi {
|
|
636
655
|
...this.extraProps
|
637
656
|
});
|
638
657
|
}
|
658
|
+
getGitBranchesMapping(workspace, dbName) {
|
659
|
+
return operationsByTag.database.getGitBranchesMapping({
|
660
|
+
pathParams: { workspace, dbName },
|
661
|
+
...this.extraProps
|
662
|
+
});
|
663
|
+
}
|
664
|
+
addGitBranchesEntry(workspace, dbName, body) {
|
665
|
+
return operationsByTag.database.addGitBranchesEntry({
|
666
|
+
pathParams: { workspace, dbName },
|
667
|
+
body,
|
668
|
+
...this.extraProps
|
669
|
+
});
|
670
|
+
}
|
671
|
+
removeGitBranchesEntry(workspace, dbName, gitBranch) {
|
672
|
+
return operationsByTag.database.removeGitBranchesEntry({
|
673
|
+
pathParams: { workspace, dbName },
|
674
|
+
queryParams: { gitBranch },
|
675
|
+
...this.extraProps
|
676
|
+
});
|
677
|
+
}
|
678
|
+
resolveBranch(workspace, dbName, gitBranch) {
|
679
|
+
return operationsByTag.database.resolveBranch({
|
680
|
+
pathParams: { workspace, dbName },
|
681
|
+
queryParams: { gitBranch },
|
682
|
+
...this.extraProps
|
683
|
+
});
|
684
|
+
}
|
639
685
|
}
|
640
686
|
class BranchApi {
|
641
687
|
constructor(extraProps) {
|
@@ -653,10 +699,10 @@ class BranchApi {
|
|
653
699
|
...this.extraProps
|
654
700
|
});
|
655
701
|
}
|
656
|
-
createBranch(workspace, database, branch, from
|
702
|
+
createBranch(workspace, database, branch, from, options = {}) {
|
657
703
|
return operationsByTag.branch.createBranch({
|
658
704
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
659
|
-
queryParams: { from },
|
705
|
+
queryParams: isString(from) ? { from } : void 0,
|
660
706
|
body: options,
|
661
707
|
...this.extraProps
|
662
708
|
});
|
@@ -861,7 +907,7 @@ var __accessCheck$6 = (obj, member, msg) => {
|
|
861
907
|
if (!member.has(obj))
|
862
908
|
throw TypeError("Cannot " + msg);
|
863
909
|
};
|
864
|
-
var __privateGet$
|
910
|
+
var __privateGet$6 = (obj, member, getter) => {
|
865
911
|
__accessCheck$6(obj, member, "read from private field");
|
866
912
|
return getter ? getter.call(obj) : member.get(obj);
|
867
913
|
};
|
@@ -870,7 +916,7 @@ var __privateAdd$6 = (obj, member, value) => {
|
|
870
916
|
throw TypeError("Cannot add the same private member more than once");
|
871
917
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
872
918
|
};
|
873
|
-
var __privateSet$
|
919
|
+
var __privateSet$5 = (obj, member, value, setter) => {
|
874
920
|
__accessCheck$6(obj, member, "write to private field");
|
875
921
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
876
922
|
return value;
|
@@ -879,21 +925,21 @@ var _query;
|
|
879
925
|
class Page {
|
880
926
|
constructor(query, meta, records = []) {
|
881
927
|
__privateAdd$6(this, _query, void 0);
|
882
|
-
__privateSet$
|
928
|
+
__privateSet$5(this, _query, query);
|
883
929
|
this.meta = meta;
|
884
930
|
this.records = records;
|
885
931
|
}
|
886
932
|
async nextPage(size, offset) {
|
887
|
-
return __privateGet$
|
933
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
|
888
934
|
}
|
889
935
|
async previousPage(size, offset) {
|
890
|
-
return __privateGet$
|
936
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, before: this.meta.page.cursor } });
|
891
937
|
}
|
892
938
|
async firstPage(size, offset) {
|
893
|
-
return __privateGet$
|
939
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, first: this.meta.page.cursor } });
|
894
940
|
}
|
895
941
|
async lastPage(size, offset) {
|
896
|
-
return __privateGet$
|
942
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, last: this.meta.page.cursor } });
|
897
943
|
}
|
898
944
|
hasNextPage() {
|
899
945
|
return this.meta.page.more;
|
@@ -909,7 +955,7 @@ var __accessCheck$5 = (obj, member, msg) => {
|
|
909
955
|
if (!member.has(obj))
|
910
956
|
throw TypeError("Cannot " + msg);
|
911
957
|
};
|
912
|
-
var __privateGet$
|
958
|
+
var __privateGet$5 = (obj, member, getter) => {
|
913
959
|
__accessCheck$5(obj, member, "read from private field");
|
914
960
|
return getter ? getter.call(obj) : member.get(obj);
|
915
961
|
};
|
@@ -918,7 +964,7 @@ var __privateAdd$5 = (obj, member, value) => {
|
|
918
964
|
throw TypeError("Cannot add the same private member more than once");
|
919
965
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
920
966
|
};
|
921
|
-
var __privateSet$
|
967
|
+
var __privateSet$4 = (obj, member, value, setter) => {
|
922
968
|
__accessCheck$5(obj, member, "write to private field");
|
923
969
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
924
970
|
return value;
|
@@ -931,21 +977,21 @@ const _Query = class {
|
|
931
977
|
__privateAdd$5(this, _data, { filter: {} });
|
932
978
|
this.meta = { page: { cursor: "start", more: true } };
|
933
979
|
this.records = [];
|
934
|
-
__privateSet$
|
980
|
+
__privateSet$4(this, _table$1, table);
|
935
981
|
if (repository) {
|
936
|
-
__privateSet$
|
982
|
+
__privateSet$4(this, _repository, repository);
|
937
983
|
} else {
|
938
|
-
__privateSet$
|
984
|
+
__privateSet$4(this, _repository, this);
|
939
985
|
}
|
940
|
-
__privateGet$
|
941
|
-
__privateGet$
|
942
|
-
__privateGet$
|
943
|
-
__privateGet$
|
944
|
-
__privateGet$
|
945
|
-
__privateGet$
|
946
|
-
__privateGet$
|
947
|
-
__privateGet$
|
948
|
-
__privateGet$
|
986
|
+
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
987
|
+
__privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
|
988
|
+
__privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
|
989
|
+
__privateGet$5(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
|
990
|
+
__privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
|
991
|
+
__privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
|
992
|
+
__privateGet$5(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
|
993
|
+
__privateGet$5(this, _data).pagination = data.pagination ?? parent?.pagination;
|
994
|
+
__privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
|
949
995
|
this.any = this.any.bind(this);
|
950
996
|
this.all = this.all.bind(this);
|
951
997
|
this.not = this.not.bind(this);
|
@@ -956,63 +1002,64 @@ const _Query = class {
|
|
956
1002
|
Object.defineProperty(this, "repository", { enumerable: false });
|
957
1003
|
}
|
958
1004
|
getQueryOptions() {
|
959
|
-
return __privateGet$
|
1005
|
+
return __privateGet$5(this, _data);
|
960
1006
|
}
|
961
1007
|
key() {
|
962
|
-
const { columns = [], filter = {}, sort = [],
|
963
|
-
const key = JSON.stringify({ columns, filter, sort,
|
1008
|
+
const { columns = [], filter = {}, sort = [], pagination = {} } = __privateGet$5(this, _data);
|
1009
|
+
const key = JSON.stringify({ columns, filter, sort, pagination });
|
964
1010
|
return toBase64(key);
|
965
1011
|
}
|
966
1012
|
any(...queries) {
|
967
1013
|
const $any = queries.map((query) => query.getQueryOptions().filter ?? {});
|
968
|
-
return new _Query(__privateGet$
|
1014
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $any } }, __privateGet$5(this, _data));
|
969
1015
|
}
|
970
1016
|
all(...queries) {
|
971
1017
|
const $all = queries.map((query) => query.getQueryOptions().filter ?? {});
|
972
|
-
return new _Query(__privateGet$
|
1018
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
973
1019
|
}
|
974
1020
|
not(...queries) {
|
975
1021
|
const $not = queries.map((query) => query.getQueryOptions().filter ?? {});
|
976
|
-
return new _Query(__privateGet$
|
1022
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $not } }, __privateGet$5(this, _data));
|
977
1023
|
}
|
978
1024
|
none(...queries) {
|
979
1025
|
const $none = queries.map((query) => query.getQueryOptions().filter ?? {});
|
980
|
-
return new _Query(__privateGet$
|
1026
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $none } }, __privateGet$5(this, _data));
|
981
1027
|
}
|
982
1028
|
filter(a, b) {
|
983
1029
|
if (arguments.length === 1) {
|
984
1030
|
const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
|
985
|
-
const $all = compact([__privateGet$
|
986
|
-
return new _Query(__privateGet$
|
1031
|
+
const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat(constraints));
|
1032
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
987
1033
|
} else {
|
988
|
-
const $all = compact([__privateGet$
|
989
|
-
return new _Query(__privateGet$
|
1034
|
+
const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
|
1035
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
990
1036
|
}
|
991
1037
|
}
|
992
1038
|
sort(column, direction) {
|
993
|
-
const originalSort = [__privateGet$
|
1039
|
+
const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
|
994
1040
|
const sort = [...originalSort, { column, direction }];
|
995
|
-
return new _Query(__privateGet$
|
1041
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
|
996
1042
|
}
|
997
1043
|
select(columns) {
|
998
|
-
return new _Query(__privateGet$
|
1044
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { columns }, __privateGet$5(this, _data));
|
999
1045
|
}
|
1000
1046
|
getPaginated(options = {}) {
|
1001
|
-
const query = new _Query(__privateGet$
|
1002
|
-
return __privateGet$
|
1047
|
+
const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
|
1048
|
+
return __privateGet$5(this, _repository).query(query);
|
1003
1049
|
}
|
1004
1050
|
async *[Symbol.asyncIterator]() {
|
1005
|
-
for await (const [record] of this.getIterator(1)) {
|
1051
|
+
for await (const [record] of this.getIterator({ batchSize: 1 })) {
|
1006
1052
|
yield record;
|
1007
1053
|
}
|
1008
1054
|
}
|
1009
|
-
async *getIterator(
|
1055
|
+
async *getIterator(options = {}) {
|
1056
|
+
const { batchSize = 1 } = options;
|
1010
1057
|
let offset = 0;
|
1011
1058
|
let end = false;
|
1012
1059
|
while (!end) {
|
1013
|
-
const { records, meta } = await this.getPaginated({ ...options,
|
1060
|
+
const { records, meta } = await this.getPaginated({ ...options, pagination: { size: batchSize, offset } });
|
1014
1061
|
yield records;
|
1015
|
-
offset +=
|
1062
|
+
offset += batchSize;
|
1016
1063
|
end = !meta.page.more;
|
1017
1064
|
}
|
1018
1065
|
}
|
@@ -1020,19 +1067,20 @@ const _Query = class {
|
|
1020
1067
|
const { records } = await this.getPaginated(options);
|
1021
1068
|
return records;
|
1022
1069
|
}
|
1023
|
-
async getAll(
|
1070
|
+
async getAll(options = {}) {
|
1071
|
+
const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
|
1024
1072
|
const results = [];
|
1025
|
-
for await (const page of this.getIterator(
|
1073
|
+
for await (const page of this.getIterator({ ...rest, batchSize })) {
|
1026
1074
|
results.push(...page);
|
1027
1075
|
}
|
1028
1076
|
return results;
|
1029
1077
|
}
|
1030
1078
|
async getFirst(options = {}) {
|
1031
|
-
const records = await this.getMany({ ...options,
|
1032
|
-
return records[0]
|
1079
|
+
const records = await this.getMany({ ...options, pagination: { size: 1 } });
|
1080
|
+
return records[0] ?? null;
|
1033
1081
|
}
|
1034
1082
|
cache(ttl) {
|
1035
|
-
return new _Query(__privateGet$
|
1083
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
|
1036
1084
|
}
|
1037
1085
|
nextPage(size, offset) {
|
1038
1086
|
return this.firstPage(size, offset);
|
@@ -1041,10 +1089,10 @@ const _Query = class {
|
|
1041
1089
|
return this.firstPage(size, offset);
|
1042
1090
|
}
|
1043
1091
|
firstPage(size, offset) {
|
1044
|
-
return this.getPaginated({
|
1092
|
+
return this.getPaginated({ pagination: { size, offset } });
|
1045
1093
|
}
|
1046
1094
|
lastPage(size, offset) {
|
1047
|
-
return this.getPaginated({
|
1095
|
+
return this.getPaginated({ pagination: { size, offset, before: "end" } });
|
1048
1096
|
}
|
1049
1097
|
hasNextPage() {
|
1050
1098
|
return this.meta.page.more;
|
@@ -1089,7 +1137,7 @@ var __accessCheck$4 = (obj, member, msg) => {
|
|
1089
1137
|
if (!member.has(obj))
|
1090
1138
|
throw TypeError("Cannot " + msg);
|
1091
1139
|
};
|
1092
|
-
var __privateGet$
|
1140
|
+
var __privateGet$4 = (obj, member, getter) => {
|
1093
1141
|
__accessCheck$4(obj, member, "read from private field");
|
1094
1142
|
return getter ? getter.call(obj) : member.get(obj);
|
1095
1143
|
};
|
@@ -1098,7 +1146,7 @@ var __privateAdd$4 = (obj, member, value) => {
|
|
1098
1146
|
throw TypeError("Cannot add the same private member more than once");
|
1099
1147
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1100
1148
|
};
|
1101
|
-
var __privateSet$
|
1149
|
+
var __privateSet$3 = (obj, member, value, setter) => {
|
1102
1150
|
__accessCheck$4(obj, member, "write to private field");
|
1103
1151
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1104
1152
|
return value;
|
@@ -1107,7 +1155,7 @@ var __privateMethod$2 = (obj, member, method) => {
|
|
1107
1155
|
__accessCheck$4(obj, member, "access private method");
|
1108
1156
|
return method;
|
1109
1157
|
};
|
1110
|
-
var _table,
|
1158
|
+
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
1159
|
class Repository extends Query {
|
1112
1160
|
}
|
1113
1161
|
class RestRepository extends Query {
|
@@ -1124,15 +1172,15 @@ class RestRepository extends Query {
|
|
1124
1172
|
__privateAdd$4(this, _getCacheRecord);
|
1125
1173
|
__privateAdd$4(this, _setCacheQuery);
|
1126
1174
|
__privateAdd$4(this, _getCacheQuery);
|
1175
|
+
__privateAdd$4(this, _getSchema$1);
|
1127
1176
|
__privateAdd$4(this, _table, void 0);
|
1128
|
-
__privateAdd$4(this, _links, void 0);
|
1129
1177
|
__privateAdd$4(this, _getFetchProps, void 0);
|
1130
1178
|
__privateAdd$4(this, _cache, void 0);
|
1131
|
-
|
1132
|
-
__privateSet$
|
1133
|
-
__privateSet$
|
1179
|
+
__privateAdd$4(this, _schema$1, void 0);
|
1180
|
+
__privateSet$3(this, _table, options.table);
|
1181
|
+
__privateSet$3(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
1134
1182
|
this.db = options.db;
|
1135
|
-
__privateSet$
|
1183
|
+
__privateSet$3(this, _cache, options.pluginOptions.cache);
|
1136
1184
|
}
|
1137
1185
|
async create(a, b) {
|
1138
1186
|
if (Array.isArray(a)) {
|
@@ -1165,13 +1213,14 @@ class RestRepository extends Query {
|
|
1165
1213
|
const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, recordId);
|
1166
1214
|
if (cacheRecord)
|
1167
1215
|
return cacheRecord;
|
1168
|
-
const fetchProps = await __privateGet$
|
1216
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1169
1217
|
try {
|
1170
1218
|
const response = await getRecord({
|
1171
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1219
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1172
1220
|
...fetchProps
|
1173
1221
|
});
|
1174
|
-
|
1222
|
+
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1223
|
+
return initObject(this.db, schema, __privateGet$4(this, _table), response);
|
1175
1224
|
} catch (e) {
|
1176
1225
|
if (isObject(e) && e.status === 404) {
|
1177
1226
|
return null;
|
@@ -1242,13 +1291,14 @@ class RestRepository extends Query {
|
|
1242
1291
|
throw new Error("Invalid arguments for delete method");
|
1243
1292
|
}
|
1244
1293
|
async search(query, options = {}) {
|
1245
|
-
const fetchProps = await __privateGet$
|
1294
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1246
1295
|
const { records } = await searchBranch({
|
1247
1296
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1248
|
-
body: { tables: [__privateGet$
|
1297
|
+
body: { tables: [__privateGet$4(this, _table)], query, fuzziness: options.fuzziness },
|
1249
1298
|
...fetchProps
|
1250
1299
|
});
|
1251
|
-
|
1300
|
+
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1301
|
+
return records.map((item) => initObject(this.db, schema, __privateGet$4(this, _table), item));
|
1252
1302
|
}
|
1253
1303
|
async query(query) {
|
1254
1304
|
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
@@ -1257,34 +1307,35 @@ class RestRepository extends Query {
|
|
1257
1307
|
const data = query.getQueryOptions();
|
1258
1308
|
const body = {
|
1259
1309
|
filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
|
1260
|
-
sort: data.sort ? buildSortFilter(data.sort) : void 0,
|
1261
|
-
page: data.
|
1310
|
+
sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
|
1311
|
+
page: data.pagination,
|
1262
1312
|
columns: data.columns
|
1263
1313
|
};
|
1264
|
-
const fetchProps = await __privateGet$
|
1314
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1265
1315
|
const { meta, records: objects } = await queryTable({
|
1266
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1316
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1267
1317
|
body,
|
1268
1318
|
...fetchProps
|
1269
1319
|
});
|
1270
|
-
const
|
1320
|
+
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1321
|
+
const records = objects.map((record) => initObject(this.db, schema, __privateGet$4(this, _table), record));
|
1271
1322
|
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1272
1323
|
return new Page(query, meta, records);
|
1273
1324
|
}
|
1274
1325
|
}
|
1275
1326
|
_table = new WeakMap();
|
1276
|
-
_links = new WeakMap();
|
1277
1327
|
_getFetchProps = new WeakMap();
|
1278
1328
|
_cache = new WeakMap();
|
1329
|
+
_schema$1 = new WeakMap();
|
1279
1330
|
_insertRecordWithoutId = new WeakSet();
|
1280
1331
|
insertRecordWithoutId_fn = async function(object) {
|
1281
|
-
const fetchProps = await __privateGet$
|
1332
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1282
1333
|
const record = transformObjectLinks(object);
|
1283
1334
|
const response = await insertRecord({
|
1284
1335
|
pathParams: {
|
1285
1336
|
workspace: "{workspaceId}",
|
1286
1337
|
dbBranchName: "{dbBranch}",
|
1287
|
-
tableName: __privateGet$
|
1338
|
+
tableName: __privateGet$4(this, _table)
|
1288
1339
|
},
|
1289
1340
|
body: record,
|
1290
1341
|
...fetchProps
|
@@ -1297,13 +1348,13 @@ insertRecordWithoutId_fn = async function(object) {
|
|
1297
1348
|
};
|
1298
1349
|
_insertRecordWithId = new WeakSet();
|
1299
1350
|
insertRecordWithId_fn = async function(recordId, object) {
|
1300
|
-
const fetchProps = await __privateGet$
|
1351
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1301
1352
|
const record = transformObjectLinks(object);
|
1302
1353
|
const response = await insertRecordWithID({
|
1303
1354
|
pathParams: {
|
1304
1355
|
workspace: "{workspaceId}",
|
1305
1356
|
dbBranchName: "{dbBranch}",
|
1306
|
-
tableName: __privateGet$
|
1357
|
+
tableName: __privateGet$4(this, _table),
|
1307
1358
|
recordId
|
1308
1359
|
},
|
1309
1360
|
body: record,
|
@@ -1318,10 +1369,10 @@ insertRecordWithId_fn = async function(recordId, object) {
|
|
1318
1369
|
};
|
1319
1370
|
_bulkInsertTableRecords = new WeakSet();
|
1320
1371
|
bulkInsertTableRecords_fn = async function(objects) {
|
1321
|
-
const fetchProps = await __privateGet$
|
1372
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1322
1373
|
const records = objects.map((object) => transformObjectLinks(object));
|
1323
1374
|
const response = await bulkInsertTableRecords({
|
1324
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1375
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1325
1376
|
body: { records },
|
1326
1377
|
...fetchProps
|
1327
1378
|
});
|
@@ -1333,10 +1384,10 @@ bulkInsertTableRecords_fn = async function(objects) {
|
|
1333
1384
|
};
|
1334
1385
|
_updateRecordWithID = new WeakSet();
|
1335
1386
|
updateRecordWithID_fn = async function(recordId, object) {
|
1336
|
-
const fetchProps = await __privateGet$
|
1387
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1337
1388
|
const record = transformObjectLinks(object);
|
1338
1389
|
const response = await updateRecordWithID({
|
1339
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1390
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1340
1391
|
body: record,
|
1341
1392
|
...fetchProps
|
1342
1393
|
});
|
@@ -1347,9 +1398,9 @@ updateRecordWithID_fn = async function(recordId, object) {
|
|
1347
1398
|
};
|
1348
1399
|
_upsertRecordWithID = new WeakSet();
|
1349
1400
|
upsertRecordWithID_fn = async function(recordId, object) {
|
1350
|
-
const fetchProps = await __privateGet$
|
1401
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1351
1402
|
const response = await upsertRecordWithID({
|
1352
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1403
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1353
1404
|
body: object,
|
1354
1405
|
...fetchProps
|
1355
1406
|
});
|
@@ -1360,51 +1411,63 @@ upsertRecordWithID_fn = async function(recordId, object) {
|
|
1360
1411
|
};
|
1361
1412
|
_deleteRecord = new WeakSet();
|
1362
1413
|
deleteRecord_fn = async function(recordId) {
|
1363
|
-
const fetchProps = await __privateGet$
|
1414
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1364
1415
|
await deleteRecord({
|
1365
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1416
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1366
1417
|
...fetchProps
|
1367
1418
|
});
|
1368
1419
|
};
|
1369
1420
|
_invalidateCache = new WeakSet();
|
1370
1421
|
invalidateCache_fn = async function(recordId) {
|
1371
|
-
await __privateGet$
|
1372
|
-
const cacheItems = await __privateGet$
|
1422
|
+
await __privateGet$4(this, _cache).delete(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1423
|
+
const cacheItems = await __privateGet$4(this, _cache).getAll();
|
1373
1424
|
const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
|
1374
1425
|
for (const [key, value] of queries) {
|
1375
1426
|
const ids = getIds(value);
|
1376
1427
|
if (ids.includes(recordId))
|
1377
|
-
await __privateGet$
|
1428
|
+
await __privateGet$4(this, _cache).delete(key);
|
1378
1429
|
}
|
1379
1430
|
};
|
1380
1431
|
_setCacheRecord = new WeakSet();
|
1381
1432
|
setCacheRecord_fn = async function(record) {
|
1382
|
-
if (!__privateGet$
|
1433
|
+
if (!__privateGet$4(this, _cache).cacheRecords)
|
1383
1434
|
return;
|
1384
|
-
await __privateGet$
|
1435
|
+
await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
|
1385
1436
|
};
|
1386
1437
|
_getCacheRecord = new WeakSet();
|
1387
1438
|
getCacheRecord_fn = async function(recordId) {
|
1388
|
-
if (!__privateGet$
|
1439
|
+
if (!__privateGet$4(this, _cache).cacheRecords)
|
1389
1440
|
return null;
|
1390
|
-
return __privateGet$
|
1441
|
+
return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1391
1442
|
};
|
1392
1443
|
_setCacheQuery = new WeakSet();
|
1393
1444
|
setCacheQuery_fn = async function(query, meta, records) {
|
1394
|
-
await __privateGet$
|
1445
|
+
await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
|
1395
1446
|
};
|
1396
1447
|
_getCacheQuery = new WeakSet();
|
1397
1448
|
getCacheQuery_fn = async function(query) {
|
1398
|
-
const key = `query_${__privateGet$
|
1399
|
-
const result = await __privateGet$
|
1449
|
+
const key = `query_${__privateGet$4(this, _table)}:${query.key()}`;
|
1450
|
+
const result = await __privateGet$4(this, _cache).get(key);
|
1400
1451
|
if (!result)
|
1401
1452
|
return null;
|
1402
|
-
const { cache: ttl = __privateGet$
|
1403
|
-
if (
|
1404
|
-
return
|
1453
|
+
const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
|
1454
|
+
if (ttl < 0)
|
1455
|
+
return null;
|
1405
1456
|
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1406
1457
|
return hasExpired ? null : result;
|
1407
1458
|
};
|
1459
|
+
_getSchema$1 = new WeakSet();
|
1460
|
+
getSchema_fn$1 = async function() {
|
1461
|
+
if (__privateGet$4(this, _schema$1))
|
1462
|
+
return __privateGet$4(this, _schema$1);
|
1463
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1464
|
+
const { schema } = await getBranchDetails({
|
1465
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1466
|
+
...fetchProps
|
1467
|
+
});
|
1468
|
+
__privateSet$3(this, _schema$1, schema);
|
1469
|
+
return schema;
|
1470
|
+
};
|
1408
1471
|
const transformObjectLinks = (object) => {
|
1409
1472
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
1410
1473
|
if (key === "xata")
|
@@ -1412,15 +1475,33 @@ const transformObjectLinks = (object) => {
|
|
1412
1475
|
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1413
1476
|
}, {});
|
1414
1477
|
};
|
1415
|
-
const initObject = (db,
|
1478
|
+
const initObject = (db, schema, table, object) => {
|
1416
1479
|
const result = {};
|
1417
1480
|
Object.assign(result, object);
|
1418
|
-
const
|
1419
|
-
|
1420
|
-
|
1421
|
-
|
1422
|
-
|
1423
|
-
|
1481
|
+
const { columns } = schema.tables.find(({ name }) => name === table) ?? {};
|
1482
|
+
if (!columns)
|
1483
|
+
console.error(`Table ${table} not found in schema`);
|
1484
|
+
for (const column of columns ?? []) {
|
1485
|
+
const value = result[column.name];
|
1486
|
+
switch (column.type) {
|
1487
|
+
case "datetime": {
|
1488
|
+
const date = new Date(value);
|
1489
|
+
if (isNaN(date.getTime())) {
|
1490
|
+
console.error(`Failed to parse date ${value} for field ${column.name}`);
|
1491
|
+
} else {
|
1492
|
+
result[column.name] = date;
|
1493
|
+
}
|
1494
|
+
break;
|
1495
|
+
}
|
1496
|
+
case "link": {
|
1497
|
+
const linkTable = column.link?.table;
|
1498
|
+
if (!linkTable) {
|
1499
|
+
console.error(`Failed to parse link for field ${column.name}`);
|
1500
|
+
} else if (isObject(value)) {
|
1501
|
+
result[column.name] = initObject(db, schema, linkTable, value);
|
1502
|
+
}
|
1503
|
+
break;
|
1504
|
+
}
|
1424
1505
|
}
|
1425
1506
|
}
|
1426
1507
|
result.read = function() {
|
@@ -1452,7 +1533,7 @@ var __accessCheck$3 = (obj, member, msg) => {
|
|
1452
1533
|
if (!member.has(obj))
|
1453
1534
|
throw TypeError("Cannot " + msg);
|
1454
1535
|
};
|
1455
|
-
var __privateGet$
|
1536
|
+
var __privateGet$3 = (obj, member, getter) => {
|
1456
1537
|
__accessCheck$3(obj, member, "read from private field");
|
1457
1538
|
return getter ? getter.call(obj) : member.get(obj);
|
1458
1539
|
};
|
@@ -1461,7 +1542,7 @@ var __privateAdd$3 = (obj, member, value) => {
|
|
1461
1542
|
throw TypeError("Cannot add the same private member more than once");
|
1462
1543
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1463
1544
|
};
|
1464
|
-
var __privateSet$
|
1545
|
+
var __privateSet$2 = (obj, member, value, setter) => {
|
1465
1546
|
__accessCheck$3(obj, member, "write to private field");
|
1466
1547
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1467
1548
|
return value;
|
@@ -1470,30 +1551,30 @@ var _map;
|
|
1470
1551
|
class SimpleCache {
|
1471
1552
|
constructor(options = {}) {
|
1472
1553
|
__privateAdd$3(this, _map, void 0);
|
1473
|
-
__privateSet$
|
1554
|
+
__privateSet$2(this, _map, /* @__PURE__ */ new Map());
|
1474
1555
|
this.capacity = options.max ?? 500;
|
1475
1556
|
this.cacheRecords = options.cacheRecords ?? true;
|
1476
1557
|
this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
|
1477
1558
|
}
|
1478
1559
|
async getAll() {
|
1479
|
-
return Object.fromEntries(__privateGet$
|
1560
|
+
return Object.fromEntries(__privateGet$3(this, _map));
|
1480
1561
|
}
|
1481
1562
|
async get(key) {
|
1482
|
-
return __privateGet$
|
1563
|
+
return __privateGet$3(this, _map).get(key) ?? null;
|
1483
1564
|
}
|
1484
1565
|
async set(key, value) {
|
1485
1566
|
await this.delete(key);
|
1486
|
-
__privateGet$
|
1487
|
-
if (__privateGet$
|
1488
|
-
const leastRecentlyUsed = __privateGet$
|
1567
|
+
__privateGet$3(this, _map).set(key, value);
|
1568
|
+
if (__privateGet$3(this, _map).size > this.capacity) {
|
1569
|
+
const leastRecentlyUsed = __privateGet$3(this, _map).keys().next().value;
|
1489
1570
|
await this.delete(leastRecentlyUsed);
|
1490
1571
|
}
|
1491
1572
|
}
|
1492
1573
|
async delete(key) {
|
1493
|
-
__privateGet$
|
1574
|
+
__privateGet$3(this, _map).delete(key);
|
1494
1575
|
}
|
1495
1576
|
async clear() {
|
1496
|
-
return __privateGet$
|
1577
|
+
return __privateGet$3(this, _map).clear();
|
1497
1578
|
}
|
1498
1579
|
}
|
1499
1580
|
_map = new WeakMap();
|
@@ -1521,7 +1602,7 @@ var __accessCheck$2 = (obj, member, msg) => {
|
|
1521
1602
|
if (!member.has(obj))
|
1522
1603
|
throw TypeError("Cannot " + msg);
|
1523
1604
|
};
|
1524
|
-
var __privateGet$
|
1605
|
+
var __privateGet$2 = (obj, member, getter) => {
|
1525
1606
|
__accessCheck$2(obj, member, "read from private field");
|
1526
1607
|
return getter ? getter.call(obj) : member.get(obj);
|
1527
1608
|
};
|
@@ -1532,26 +1613,24 @@ var __privateAdd$2 = (obj, member, value) => {
|
|
1532
1613
|
};
|
1533
1614
|
var _tables;
|
1534
1615
|
class SchemaPlugin extends XataPlugin {
|
1535
|
-
constructor(
|
1616
|
+
constructor(tableNames) {
|
1536
1617
|
super();
|
1537
|
-
this.links = links;
|
1538
1618
|
this.tableNames = tableNames;
|
1539
1619
|
__privateAdd$2(this, _tables, {});
|
1540
1620
|
}
|
1541
1621
|
build(pluginOptions) {
|
1542
|
-
const links = this.links;
|
1543
1622
|
const db = new Proxy({}, {
|
1544
1623
|
get: (_target, table) => {
|
1545
1624
|
if (!isString(table))
|
1546
1625
|
throw new Error("Invalid table name");
|
1547
|
-
if (
|
1548
|
-
__privateGet$
|
1626
|
+
if (__privateGet$2(this, _tables)[table] === void 0) {
|
1627
|
+
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
|
1549
1628
|
}
|
1550
|
-
return __privateGet$
|
1629
|
+
return __privateGet$2(this, _tables)[table];
|
1551
1630
|
}
|
1552
1631
|
});
|
1553
1632
|
for (const table of this.tableNames ?? []) {
|
1554
|
-
db[table] = new RestRepository({ db, pluginOptions, table
|
1633
|
+
db[table] = new RestRepository({ db, pluginOptions, table });
|
1555
1634
|
}
|
1556
1635
|
return db;
|
1557
1636
|
}
|
@@ -1562,44 +1641,57 @@ var __accessCheck$1 = (obj, member, msg) => {
|
|
1562
1641
|
if (!member.has(obj))
|
1563
1642
|
throw TypeError("Cannot " + msg);
|
1564
1643
|
};
|
1644
|
+
var __privateGet$1 = (obj, member, getter) => {
|
1645
|
+
__accessCheck$1(obj, member, "read from private field");
|
1646
|
+
return getter ? getter.call(obj) : member.get(obj);
|
1647
|
+
};
|
1565
1648
|
var __privateAdd$1 = (obj, member, value) => {
|
1566
1649
|
if (member.has(obj))
|
1567
1650
|
throw TypeError("Cannot add the same private member more than once");
|
1568
1651
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1569
1652
|
};
|
1653
|
+
var __privateSet$1 = (obj, member, value, setter) => {
|
1654
|
+
__accessCheck$1(obj, member, "write to private field");
|
1655
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1656
|
+
return value;
|
1657
|
+
};
|
1570
1658
|
var __privateMethod$1 = (obj, member, method) => {
|
1571
1659
|
__accessCheck$1(obj, member, "access private method");
|
1572
1660
|
return method;
|
1573
1661
|
};
|
1574
|
-
var _search, search_fn;
|
1662
|
+
var _schema, _search, search_fn, _getSchema, getSchema_fn;
|
1575
1663
|
class SearchPlugin extends XataPlugin {
|
1576
|
-
constructor(db
|
1664
|
+
constructor(db) {
|
1577
1665
|
super();
|
1578
1666
|
this.db = db;
|
1579
|
-
this.links = links;
|
1580
1667
|
__privateAdd$1(this, _search);
|
1668
|
+
__privateAdd$1(this, _getSchema);
|
1669
|
+
__privateAdd$1(this, _schema, void 0);
|
1581
1670
|
}
|
1582
1671
|
build({ getFetchProps }) {
|
1583
1672
|
return {
|
1584
1673
|
all: async (query, options = {}) => {
|
1585
1674
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1675
|
+
const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
|
1586
1676
|
return records.map((record) => {
|
1587
1677
|
const { table = "orphan" } = record.xata;
|
1588
|
-
return { table, record: initObject(this.db,
|
1678
|
+
return { table, record: initObject(this.db, schema, table, record) };
|
1589
1679
|
});
|
1590
1680
|
},
|
1591
1681
|
byTable: async (query, options = {}) => {
|
1592
1682
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1683
|
+
const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
|
1593
1684
|
return records.reduce((acc, record) => {
|
1594
1685
|
const { table = "orphan" } = record.xata;
|
1595
1686
|
const items = acc[table] ?? [];
|
1596
|
-
const item = initObject(this.db,
|
1687
|
+
const item = initObject(this.db, schema, table, record);
|
1597
1688
|
return { ...acc, [table]: [...items, item] };
|
1598
1689
|
}, {});
|
1599
1690
|
}
|
1600
1691
|
};
|
1601
1692
|
}
|
1602
1693
|
}
|
1694
|
+
_schema = new WeakMap();
|
1603
1695
|
_search = new WeakSet();
|
1604
1696
|
search_fn = async function(query, options, getFetchProps) {
|
1605
1697
|
const fetchProps = await getFetchProps();
|
@@ -1611,6 +1703,18 @@ search_fn = async function(query, options, getFetchProps) {
|
|
1611
1703
|
});
|
1612
1704
|
return records;
|
1613
1705
|
};
|
1706
|
+
_getSchema = new WeakSet();
|
1707
|
+
getSchema_fn = async function(getFetchProps) {
|
1708
|
+
if (__privateGet$1(this, _schema))
|
1709
|
+
return __privateGet$1(this, _schema);
|
1710
|
+
const fetchProps = await getFetchProps();
|
1711
|
+
const { schema } = await getBranchDetails({
|
1712
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1713
|
+
...fetchProps
|
1714
|
+
});
|
1715
|
+
__privateSet$1(this, _schema, schema);
|
1716
|
+
return schema;
|
1717
|
+
};
|
1614
1718
|
|
1615
1719
|
const isBranchStrategyBuilder = (strategy) => {
|
1616
1720
|
return typeof strategy === "function";
|
@@ -1622,30 +1726,39 @@ const envBranchNames = [
|
|
1622
1726
|
"CF_PAGES_BRANCH",
|
1623
1727
|
"BRANCH"
|
1624
1728
|
];
|
1625
|
-
const defaultBranch = "main";
|
1626
1729
|
async function getCurrentBranchName(options) {
|
1627
|
-
const env =
|
1628
|
-
if (env)
|
1629
|
-
|
1630
|
-
|
1631
|
-
|
1632
|
-
|
1633
|
-
|
1634
|
-
|
1635
|
-
|
1636
|
-
return defaultBranch;
|
1730
|
+
const env = getBranchByEnvVariable();
|
1731
|
+
if (env) {
|
1732
|
+
const details = await getDatabaseBranch(env, options);
|
1733
|
+
if (details)
|
1734
|
+
return env;
|
1735
|
+
console.warn(`Branch ${env} not found in Xata. Ignoring...`);
|
1736
|
+
}
|
1737
|
+
const gitBranch = await getGitBranch();
|
1738
|
+
return resolveXataBranch(gitBranch, options);
|
1637
1739
|
}
|
1638
1740
|
async function getCurrentBranchDetails(options) {
|
1639
|
-
const
|
1640
|
-
|
1641
|
-
|
1642
|
-
|
1643
|
-
|
1644
|
-
|
1645
|
-
|
1646
|
-
|
1647
|
-
|
1648
|
-
|
1741
|
+
const branch = await getCurrentBranchName(options);
|
1742
|
+
return getDatabaseBranch(branch, options);
|
1743
|
+
}
|
1744
|
+
async function resolveXataBranch(gitBranch, options) {
|
1745
|
+
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1746
|
+
const apiKey = options?.apiKey || getAPIKey();
|
1747
|
+
if (!databaseURL)
|
1748
|
+
throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
|
1749
|
+
if (!apiKey)
|
1750
|
+
throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
|
1751
|
+
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1752
|
+
const [workspace] = host.split(".");
|
1753
|
+
const { branch } = await resolveBranch({
|
1754
|
+
apiKey,
|
1755
|
+
apiUrl: databaseURL,
|
1756
|
+
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1757
|
+
workspacesApiUrl: `${protocol}//${host}`,
|
1758
|
+
pathParams: { dbName, workspace },
|
1759
|
+
queryParams: { gitBranch, fallbackBranch: getEnvVariable("XATA_FALLBACK_BRANCH") }
|
1760
|
+
});
|
1761
|
+
return branch;
|
1649
1762
|
}
|
1650
1763
|
async function getDatabaseBranch(branch, options) {
|
1651
1764
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
@@ -1719,7 +1832,7 @@ var __privateMethod = (obj, member, method) => {
|
|
1719
1832
|
const buildClient = (plugins) => {
|
1720
1833
|
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1721
1834
|
return _a = class {
|
1722
|
-
constructor(options = {},
|
1835
|
+
constructor(options = {}, tables) {
|
1723
1836
|
__privateAdd(this, _parseOptions);
|
1724
1837
|
__privateAdd(this, _getFetchProps);
|
1725
1838
|
__privateAdd(this, _evaluateBranch);
|
@@ -1729,12 +1842,12 @@ const buildClient = (plugins) => {
|
|
1729
1842
|
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
1730
1843
|
cache: safeOptions.cache
|
1731
1844
|
};
|
1732
|
-
const db = new SchemaPlugin(
|
1733
|
-
const search = new SearchPlugin(db
|
1845
|
+
const db = new SchemaPlugin(tables).build(pluginOptions);
|
1846
|
+
const search = new SearchPlugin(db).build(pluginOptions);
|
1734
1847
|
this.db = db;
|
1735
1848
|
this.search = search;
|
1736
1849
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
1737
|
-
if (
|
1850
|
+
if (namespace === void 0)
|
1738
1851
|
continue;
|
1739
1852
|
const result = namespace.build(pluginOptions);
|
1740
1853
|
if (result instanceof Promise) {
|
@@ -1751,7 +1864,7 @@ const buildClient = (plugins) => {
|
|
1751
1864
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1752
1865
|
const apiKey = options?.apiKey || getAPIKey();
|
1753
1866
|
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 });
|
1867
|
+
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
1868
|
if (!databaseURL || !apiKey) {
|
1756
1869
|
throw new Error("Options databaseURL and apiKey are required");
|
1757
1870
|
}
|
@@ -1778,7 +1891,7 @@ const buildClient = (plugins) => {
|
|
1778
1891
|
}, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
|
1779
1892
|
if (__privateGet(this, _branch))
|
1780
1893
|
return __privateGet(this, _branch);
|
1781
|
-
if (
|
1894
|
+
if (param === void 0)
|
1782
1895
|
return void 0;
|
1783
1896
|
const strategies = Array.isArray(param) ? [...param] : [param];
|
1784
1897
|
const evaluateBranch = async (strategy) => {
|
@@ -1803,5 +1916,5 @@ class XataError extends Error {
|
|
1803
1916
|
}
|
1804
1917
|
}
|
1805
1918
|
|
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 };
|
1919
|
+
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, isIdentifiable, isNot, isXataRecord, le, lt, lte, notExists, operationsByTag, pattern, queryTable, removeGitBranchesEntry, removeWorkspaceMember, resendWorkspaceMemberInvite, resolveBranch, searchBranch, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
|
1807
1920
|
//# sourceMappingURL=index.mjs.map
|