@xata.io/client 0.0.0-alpha.vf2043e7 → 0.0.0-alpha.vf350c0a
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/CHANGELOG.md +16 -0
- package/dist/index.cjs +428 -163
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +247 -24
- package/dist/index.mjs +424 -164
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
@@ -10,6 +10,13 @@ function isObject(value) {
|
|
10
10
|
function isString(value) {
|
11
11
|
return value !== void 0 && value !== null && typeof value === "string";
|
12
12
|
}
|
13
|
+
function toBase64(value) {
|
14
|
+
try {
|
15
|
+
return btoa(value);
|
16
|
+
} catch (err) {
|
17
|
+
return Buffer.from(value).toString("base64");
|
18
|
+
}
|
19
|
+
}
|
13
20
|
|
14
21
|
function getEnvVariable(name) {
|
15
22
|
try {
|
@@ -27,7 +34,10 @@ function getEnvVariable(name) {
|
|
27
34
|
}
|
28
35
|
async function getGitBranch() {
|
29
36
|
try {
|
30
|
-
|
37
|
+
if (typeof require === "function") {
|
38
|
+
const req = require;
|
39
|
+
return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
|
40
|
+
}
|
31
41
|
} catch (err) {
|
32
42
|
}
|
33
43
|
try {
|
@@ -241,6 +251,14 @@ const deleteDatabase = (variables) => fetch$1({
|
|
241
251
|
method: "delete",
|
242
252
|
...variables
|
243
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
|
+
});
|
244
262
|
const getBranchDetails = (variables) => fetch$1({
|
245
263
|
url: "/db/{dbBranchName}",
|
246
264
|
method: "get",
|
@@ -369,7 +387,15 @@ const operationsByTag = {
|
|
369
387
|
resendWorkspaceMemberInvite,
|
370
388
|
acceptWorkspaceMemberInvite
|
371
389
|
},
|
372
|
-
database: {
|
390
|
+
database: {
|
391
|
+
getDatabaseList,
|
392
|
+
createDatabase,
|
393
|
+
deleteDatabase,
|
394
|
+
getGitBranchesMapping,
|
395
|
+
addGitBranchesEntry,
|
396
|
+
removeGitBranchesEntry,
|
397
|
+
resolveBranch
|
398
|
+
},
|
373
399
|
branch: {
|
374
400
|
getBranchList,
|
375
401
|
getBranchDetails,
|
@@ -432,35 +458,35 @@ function isValidBuilder(builder) {
|
|
432
458
|
return isObject(builder) && isString(builder.main) && isString(builder.workspaces);
|
433
459
|
}
|
434
460
|
|
435
|
-
var __accessCheck$
|
461
|
+
var __accessCheck$7 = (obj, member, msg) => {
|
436
462
|
if (!member.has(obj))
|
437
463
|
throw TypeError("Cannot " + msg);
|
438
464
|
};
|
439
|
-
var __privateGet$
|
440
|
-
__accessCheck$
|
465
|
+
var __privateGet$7 = (obj, member, getter) => {
|
466
|
+
__accessCheck$7(obj, member, "read from private field");
|
441
467
|
return getter ? getter.call(obj) : member.get(obj);
|
442
468
|
};
|
443
|
-
var __privateAdd$
|
469
|
+
var __privateAdd$7 = (obj, member, value) => {
|
444
470
|
if (member.has(obj))
|
445
471
|
throw TypeError("Cannot add the same private member more than once");
|
446
472
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
447
473
|
};
|
448
|
-
var __privateSet$
|
449
|
-
__accessCheck$
|
474
|
+
var __privateSet$6 = (obj, member, value, setter) => {
|
475
|
+
__accessCheck$7(obj, member, "write to private field");
|
450
476
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
451
477
|
return value;
|
452
478
|
};
|
453
479
|
var _extraProps, _namespaces;
|
454
480
|
class XataApiClient {
|
455
481
|
constructor(options = {}) {
|
456
|
-
__privateAdd$
|
457
|
-
__privateAdd$
|
482
|
+
__privateAdd$7(this, _extraProps, void 0);
|
483
|
+
__privateAdd$7(this, _namespaces, {});
|
458
484
|
const provider = options.host ?? "production";
|
459
485
|
const apiKey = options?.apiKey ?? getAPIKey();
|
460
486
|
if (!apiKey) {
|
461
487
|
throw new Error("Could not resolve a valid apiKey");
|
462
488
|
}
|
463
|
-
__privateSet$
|
489
|
+
__privateSet$6(this, _extraProps, {
|
464
490
|
apiUrl: getHostUrl(provider, "main"),
|
465
491
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
466
492
|
fetchImpl: getFetchImplementation(options.fetch),
|
@@ -468,34 +494,34 @@ class XataApiClient {
|
|
468
494
|
});
|
469
495
|
}
|
470
496
|
get user() {
|
471
|
-
if (!__privateGet$
|
472
|
-
__privateGet$
|
473
|
-
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;
|
474
500
|
}
|
475
501
|
get workspaces() {
|
476
|
-
if (!__privateGet$
|
477
|
-
__privateGet$
|
478
|
-
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;
|
479
505
|
}
|
480
506
|
get databases() {
|
481
|
-
if (!__privateGet$
|
482
|
-
__privateGet$
|
483
|
-
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;
|
484
510
|
}
|
485
511
|
get branches() {
|
486
|
-
if (!__privateGet$
|
487
|
-
__privateGet$
|
488
|
-
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;
|
489
515
|
}
|
490
516
|
get tables() {
|
491
|
-
if (!__privateGet$
|
492
|
-
__privateGet$
|
493
|
-
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;
|
494
520
|
}
|
495
521
|
get records() {
|
496
|
-
if (!__privateGet$
|
497
|
-
__privateGet$
|
498
|
-
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;
|
499
525
|
}
|
500
526
|
}
|
501
527
|
_extraProps = new WeakMap();
|
@@ -629,6 +655,33 @@ class DatabaseApi {
|
|
629
655
|
...this.extraProps
|
630
656
|
});
|
631
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
|
+
}
|
632
685
|
}
|
633
686
|
class BranchApi {
|
634
687
|
constructor(extraProps) {
|
@@ -850,43 +903,43 @@ class XataApiPlugin {
|
|
850
903
|
class XataPlugin {
|
851
904
|
}
|
852
905
|
|
853
|
-
var __accessCheck$
|
906
|
+
var __accessCheck$6 = (obj, member, msg) => {
|
854
907
|
if (!member.has(obj))
|
855
908
|
throw TypeError("Cannot " + msg);
|
856
909
|
};
|
857
|
-
var __privateGet$
|
858
|
-
__accessCheck$
|
910
|
+
var __privateGet$6 = (obj, member, getter) => {
|
911
|
+
__accessCheck$6(obj, member, "read from private field");
|
859
912
|
return getter ? getter.call(obj) : member.get(obj);
|
860
913
|
};
|
861
|
-
var __privateAdd$
|
914
|
+
var __privateAdd$6 = (obj, member, value) => {
|
862
915
|
if (member.has(obj))
|
863
916
|
throw TypeError("Cannot add the same private member more than once");
|
864
917
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
865
918
|
};
|
866
|
-
var __privateSet$
|
867
|
-
__accessCheck$
|
919
|
+
var __privateSet$5 = (obj, member, value, setter) => {
|
920
|
+
__accessCheck$6(obj, member, "write to private field");
|
868
921
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
869
922
|
return value;
|
870
923
|
};
|
871
924
|
var _query;
|
872
925
|
class Page {
|
873
926
|
constructor(query, meta, records = []) {
|
874
|
-
__privateAdd$
|
875
|
-
__privateSet$
|
927
|
+
__privateAdd$6(this, _query, void 0);
|
928
|
+
__privateSet$5(this, _query, query);
|
876
929
|
this.meta = meta;
|
877
930
|
this.records = records;
|
878
931
|
}
|
879
932
|
async nextPage(size, offset) {
|
880
|
-
return __privateGet$
|
933
|
+
return __privateGet$6(this, _query).getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
|
881
934
|
}
|
882
935
|
async previousPage(size, offset) {
|
883
|
-
return __privateGet$
|
936
|
+
return __privateGet$6(this, _query).getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
|
884
937
|
}
|
885
938
|
async firstPage(size, offset) {
|
886
|
-
return __privateGet$
|
939
|
+
return __privateGet$6(this, _query).getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
|
887
940
|
}
|
888
941
|
async lastPage(size, offset) {
|
889
|
-
return __privateGet$
|
942
|
+
return __privateGet$6(this, _query).getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
|
890
943
|
}
|
891
944
|
hasNextPage() {
|
892
945
|
return this.meta.page.more;
|
@@ -898,46 +951,47 @@ const PAGINATION_DEFAULT_SIZE = 200;
|
|
898
951
|
const PAGINATION_MAX_OFFSET = 800;
|
899
952
|
const PAGINATION_DEFAULT_OFFSET = 0;
|
900
953
|
|
901
|
-
var __accessCheck$
|
954
|
+
var __accessCheck$5 = (obj, member, msg) => {
|
902
955
|
if (!member.has(obj))
|
903
956
|
throw TypeError("Cannot " + msg);
|
904
957
|
};
|
905
|
-
var __privateGet$
|
906
|
-
__accessCheck$
|
958
|
+
var __privateGet$5 = (obj, member, getter) => {
|
959
|
+
__accessCheck$5(obj, member, "read from private field");
|
907
960
|
return getter ? getter.call(obj) : member.get(obj);
|
908
961
|
};
|
909
|
-
var __privateAdd$
|
962
|
+
var __privateAdd$5 = (obj, member, value) => {
|
910
963
|
if (member.has(obj))
|
911
964
|
throw TypeError("Cannot add the same private member more than once");
|
912
965
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
913
966
|
};
|
914
|
-
var __privateSet$
|
915
|
-
__accessCheck$
|
967
|
+
var __privateSet$4 = (obj, member, value, setter) => {
|
968
|
+
__accessCheck$5(obj, member, "write to private field");
|
916
969
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
917
970
|
return value;
|
918
971
|
};
|
919
972
|
var _table$1, _repository, _data;
|
920
973
|
const _Query = class {
|
921
974
|
constructor(repository, table, data, parent) {
|
922
|
-
__privateAdd$
|
923
|
-
__privateAdd$
|
924
|
-
__privateAdd$
|
975
|
+
__privateAdd$5(this, _table$1, void 0);
|
976
|
+
__privateAdd$5(this, _repository, void 0);
|
977
|
+
__privateAdd$5(this, _data, { filter: {} });
|
925
978
|
this.meta = { page: { cursor: "start", more: true } };
|
926
979
|
this.records = [];
|
927
|
-
__privateSet$
|
980
|
+
__privateSet$4(this, _table$1, table);
|
928
981
|
if (repository) {
|
929
|
-
__privateSet$
|
982
|
+
__privateSet$4(this, _repository, repository);
|
930
983
|
} else {
|
931
|
-
__privateSet$
|
984
|
+
__privateSet$4(this, _repository, this);
|
932
985
|
}
|
933
|
-
__privateGet$
|
934
|
-
__privateGet$
|
935
|
-
__privateGet$
|
936
|
-
__privateGet$
|
937
|
-
__privateGet$
|
938
|
-
__privateGet$
|
939
|
-
__privateGet$
|
940
|
-
__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).page = data.page ?? parent?.page;
|
994
|
+
__privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
|
941
995
|
this.any = this.any.bind(this);
|
942
996
|
this.all = this.all.bind(this);
|
943
997
|
this.not = this.not.bind(this);
|
@@ -948,45 +1002,50 @@ const _Query = class {
|
|
948
1002
|
Object.defineProperty(this, "repository", { enumerable: false });
|
949
1003
|
}
|
950
1004
|
getQueryOptions() {
|
951
|
-
return __privateGet$
|
1005
|
+
return __privateGet$5(this, _data);
|
1006
|
+
}
|
1007
|
+
key() {
|
1008
|
+
const { columns = [], filter = {}, sort = [], page = {} } = __privateGet$5(this, _data);
|
1009
|
+
const key = JSON.stringify({ columns, filter, sort, page });
|
1010
|
+
return toBase64(key);
|
952
1011
|
}
|
953
1012
|
any(...queries) {
|
954
1013
|
const $any = queries.map((query) => query.getQueryOptions().filter ?? {});
|
955
|
-
return new _Query(__privateGet$
|
1014
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $any } }, __privateGet$5(this, _data));
|
956
1015
|
}
|
957
1016
|
all(...queries) {
|
958
1017
|
const $all = queries.map((query) => query.getQueryOptions().filter ?? {});
|
959
|
-
return new _Query(__privateGet$
|
1018
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
960
1019
|
}
|
961
1020
|
not(...queries) {
|
962
1021
|
const $not = queries.map((query) => query.getQueryOptions().filter ?? {});
|
963
|
-
return new _Query(__privateGet$
|
1022
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $not } }, __privateGet$5(this, _data));
|
964
1023
|
}
|
965
1024
|
none(...queries) {
|
966
1025
|
const $none = queries.map((query) => query.getQueryOptions().filter ?? {});
|
967
|
-
return new _Query(__privateGet$
|
1026
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $none } }, __privateGet$5(this, _data));
|
968
1027
|
}
|
969
1028
|
filter(a, b) {
|
970
1029
|
if (arguments.length === 1) {
|
971
1030
|
const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
|
972
|
-
const $all = compact([__privateGet$
|
973
|
-
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));
|
974
1033
|
} else {
|
975
|
-
const $all = compact([__privateGet$
|
976
|
-
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));
|
977
1036
|
}
|
978
1037
|
}
|
979
1038
|
sort(column, direction) {
|
980
|
-
const originalSort = [__privateGet$
|
1039
|
+
const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
|
981
1040
|
const sort = [...originalSort, { column, direction }];
|
982
|
-
return new _Query(__privateGet$
|
1041
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
|
983
1042
|
}
|
984
1043
|
select(columns) {
|
985
|
-
return new _Query(__privateGet$
|
1044
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { columns }, __privateGet$5(this, _data));
|
986
1045
|
}
|
987
1046
|
getPaginated(options = {}) {
|
988
|
-
const query = new _Query(__privateGet$
|
989
|
-
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);
|
990
1049
|
}
|
991
1050
|
async *[Symbol.asyncIterator]() {
|
992
1051
|
for await (const [record] of this.getIterator(1)) {
|
@@ -1014,10 +1073,13 @@ const _Query = class {
|
|
1014
1073
|
}
|
1015
1074
|
return results;
|
1016
1075
|
}
|
1017
|
-
async
|
1076
|
+
async getFirst(options = {}) {
|
1018
1077
|
const records = await this.getMany({ ...options, page: { size: 1 } });
|
1019
1078
|
return records[0] || null;
|
1020
1079
|
}
|
1080
|
+
cache(ttl) {
|
1081
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
|
1082
|
+
}
|
1021
1083
|
nextPage(size, offset) {
|
1022
1084
|
return this.firstPage(size, offset);
|
1023
1085
|
}
|
@@ -1069,75 +1131,94 @@ function buildSortFilter(filter) {
|
|
1069
1131
|
}
|
1070
1132
|
}
|
1071
1133
|
|
1072
|
-
var __accessCheck$
|
1134
|
+
var __accessCheck$4 = (obj, member, msg) => {
|
1073
1135
|
if (!member.has(obj))
|
1074
1136
|
throw TypeError("Cannot " + msg);
|
1075
1137
|
};
|
1076
|
-
var __privateGet$
|
1077
|
-
__accessCheck$
|
1138
|
+
var __privateGet$4 = (obj, member, getter) => {
|
1139
|
+
__accessCheck$4(obj, member, "read from private field");
|
1078
1140
|
return getter ? getter.call(obj) : member.get(obj);
|
1079
1141
|
};
|
1080
|
-
var __privateAdd$
|
1142
|
+
var __privateAdd$4 = (obj, member, value) => {
|
1081
1143
|
if (member.has(obj))
|
1082
1144
|
throw TypeError("Cannot add the same private member more than once");
|
1083
1145
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1084
1146
|
};
|
1085
|
-
var __privateSet$
|
1086
|
-
__accessCheck$
|
1147
|
+
var __privateSet$3 = (obj, member, value, setter) => {
|
1148
|
+
__accessCheck$4(obj, member, "write to private field");
|
1087
1149
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1088
1150
|
return value;
|
1089
1151
|
};
|
1090
1152
|
var __privateMethod$2 = (obj, member, method) => {
|
1091
|
-
__accessCheck$
|
1153
|
+
__accessCheck$4(obj, member, "access private method");
|
1092
1154
|
return method;
|
1093
1155
|
};
|
1094
|
-
var _table,
|
1156
|
+
var _table, _getFetchProps, _cache, _schemaColumns, _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, _getSchemaColumns, getSchemaColumns_fn;
|
1095
1157
|
class Repository extends Query {
|
1096
1158
|
}
|
1097
1159
|
class RestRepository extends Query {
|
1098
1160
|
constructor(options) {
|
1099
1161
|
super(null, options.table, {});
|
1100
|
-
__privateAdd$
|
1101
|
-
__privateAdd$
|
1102
|
-
__privateAdd$
|
1103
|
-
__privateAdd$
|
1104
|
-
__privateAdd$
|
1105
|
-
__privateAdd$
|
1106
|
-
__privateAdd$
|
1107
|
-
__privateAdd$
|
1108
|
-
__privateAdd$
|
1109
|
-
|
1110
|
-
|
1111
|
-
|
1162
|
+
__privateAdd$4(this, _insertRecordWithoutId);
|
1163
|
+
__privateAdd$4(this, _insertRecordWithId);
|
1164
|
+
__privateAdd$4(this, _bulkInsertTableRecords);
|
1165
|
+
__privateAdd$4(this, _updateRecordWithID);
|
1166
|
+
__privateAdd$4(this, _upsertRecordWithID);
|
1167
|
+
__privateAdd$4(this, _deleteRecord);
|
1168
|
+
__privateAdd$4(this, _invalidateCache);
|
1169
|
+
__privateAdd$4(this, _setCacheRecord);
|
1170
|
+
__privateAdd$4(this, _getCacheRecord);
|
1171
|
+
__privateAdd$4(this, _setCacheQuery);
|
1172
|
+
__privateAdd$4(this, _getCacheQuery);
|
1173
|
+
__privateAdd$4(this, _getSchemaColumns);
|
1174
|
+
__privateAdd$4(this, _table, void 0);
|
1175
|
+
__privateAdd$4(this, _getFetchProps, void 0);
|
1176
|
+
__privateAdd$4(this, _cache, void 0);
|
1177
|
+
__privateAdd$4(this, _schemaColumns, void 0);
|
1178
|
+
__privateSet$3(this, _table, options.table);
|
1179
|
+
__privateSet$3(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
1112
1180
|
this.db = options.db;
|
1181
|
+
__privateSet$3(this, _cache, options.pluginOptions.cache);
|
1113
1182
|
}
|
1114
1183
|
async create(a, b) {
|
1115
1184
|
if (Array.isArray(a)) {
|
1116
|
-
|
1185
|
+
const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
|
1186
|
+
await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
|
1187
|
+
return records;
|
1117
1188
|
}
|
1118
1189
|
if (isString(a) && isObject(b)) {
|
1119
1190
|
if (a === "")
|
1120
1191
|
throw new Error("The id can't be empty");
|
1121
|
-
|
1192
|
+
const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b);
|
1193
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1194
|
+
return record;
|
1122
1195
|
}
|
1123
1196
|
if (isObject(a) && isString(a.id)) {
|
1124
1197
|
if (a.id === "")
|
1125
1198
|
throw new Error("The id can't be empty");
|
1126
|
-
|
1199
|
+
const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 });
|
1200
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1201
|
+
return record;
|
1127
1202
|
}
|
1128
1203
|
if (isObject(a)) {
|
1129
|
-
|
1204
|
+
const record = await __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a);
|
1205
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1206
|
+
return record;
|
1130
1207
|
}
|
1131
1208
|
throw new Error("Invalid arguments for create method");
|
1132
1209
|
}
|
1133
1210
|
async read(recordId) {
|
1134
|
-
const
|
1211
|
+
const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, recordId);
|
1212
|
+
if (cacheRecord)
|
1213
|
+
return cacheRecord;
|
1214
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1135
1215
|
try {
|
1136
1216
|
const response = await getRecord({
|
1137
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1217
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1138
1218
|
...fetchProps
|
1139
1219
|
});
|
1140
|
-
|
1220
|
+
const columns = await __privateMethod$2(this, _getSchemaColumns, getSchemaColumns_fn).call(this);
|
1221
|
+
return initObject(this.db, columns, __privateGet$4(this, _table), response);
|
1141
1222
|
} catch (e) {
|
1142
1223
|
if (isObject(e) && e.status === 404) {
|
1143
1224
|
return null;
|
@@ -1153,10 +1234,16 @@ class RestRepository extends Query {
|
|
1153
1234
|
return Promise.all(a.map((object) => this.update(object)));
|
1154
1235
|
}
|
1155
1236
|
if (isString(a) && isObject(b)) {
|
1156
|
-
|
1237
|
+
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
|
1238
|
+
const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b);
|
1239
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1240
|
+
return record;
|
1157
1241
|
}
|
1158
1242
|
if (isObject(a) && isString(a.id)) {
|
1159
|
-
|
1243
|
+
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
|
1244
|
+
const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
|
1245
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1246
|
+
return record;
|
1160
1247
|
}
|
1161
1248
|
throw new Error("Invalid arguments for update method");
|
1162
1249
|
}
|
@@ -1168,41 +1255,53 @@ class RestRepository extends Query {
|
|
1168
1255
|
return Promise.all(a.map((object) => this.createOrUpdate(object)));
|
1169
1256
|
}
|
1170
1257
|
if (isString(a) && isObject(b)) {
|
1171
|
-
|
1258
|
+
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
|
1259
|
+
const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b);
|
1260
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1261
|
+
return record;
|
1172
1262
|
}
|
1173
1263
|
if (isObject(a) && isString(a.id)) {
|
1174
|
-
|
1264
|
+
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
|
1265
|
+
const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
|
1266
|
+
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1267
|
+
return record;
|
1175
1268
|
}
|
1176
1269
|
throw new Error("Invalid arguments for createOrUpdate method");
|
1177
1270
|
}
|
1178
|
-
async delete(
|
1179
|
-
if (Array.isArray(
|
1180
|
-
if (
|
1271
|
+
async delete(a) {
|
1272
|
+
if (Array.isArray(a)) {
|
1273
|
+
if (a.length > 100) {
|
1181
1274
|
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
1182
1275
|
}
|
1183
|
-
await Promise.all(
|
1276
|
+
await Promise.all(a.map((id) => this.delete(id)));
|
1184
1277
|
return;
|
1185
1278
|
}
|
1186
|
-
if (isString(
|
1187
|
-
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this,
|
1279
|
+
if (isString(a)) {
|
1280
|
+
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a);
|
1281
|
+
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
|
1188
1282
|
return;
|
1189
1283
|
}
|
1190
|
-
if (isObject(
|
1191
|
-
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this,
|
1284
|
+
if (isObject(a) && isString(a.id)) {
|
1285
|
+
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id);
|
1286
|
+
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
|
1192
1287
|
return;
|
1193
1288
|
}
|
1194
1289
|
throw new Error("Invalid arguments for delete method");
|
1195
1290
|
}
|
1196
1291
|
async search(query, options = {}) {
|
1197
|
-
const fetchProps = await __privateGet$
|
1292
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1198
1293
|
const { records } = await searchBranch({
|
1199
1294
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1200
|
-
body: { tables: [__privateGet$
|
1295
|
+
body: { tables: [__privateGet$4(this, _table)], query, fuzziness: options.fuzziness },
|
1201
1296
|
...fetchProps
|
1202
1297
|
});
|
1203
|
-
|
1298
|
+
const columns = await __privateMethod$2(this, _getSchemaColumns, getSchemaColumns_fn).call(this);
|
1299
|
+
return records.map((item) => initObject(this.db, columns, __privateGet$4(this, _table), item));
|
1204
1300
|
}
|
1205
1301
|
async query(query) {
|
1302
|
+
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
1303
|
+
if (cacheQuery)
|
1304
|
+
return new Page(query, cacheQuery.meta, cacheQuery.records);
|
1206
1305
|
const data = query.getQueryOptions();
|
1207
1306
|
const body = {
|
1208
1307
|
filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
|
@@ -1210,28 +1309,31 @@ class RestRepository extends Query {
|
|
1210
1309
|
page: data.page,
|
1211
1310
|
columns: data.columns
|
1212
1311
|
};
|
1213
|
-
const fetchProps = await __privateGet$
|
1312
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1214
1313
|
const { meta, records: objects } = await queryTable({
|
1215
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1314
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1216
1315
|
body,
|
1217
1316
|
...fetchProps
|
1218
1317
|
});
|
1219
|
-
const
|
1318
|
+
const columns = await __privateMethod$2(this, _getSchemaColumns, getSchemaColumns_fn).call(this);
|
1319
|
+
const records = objects.map((record) => initObject(this.db, columns, __privateGet$4(this, _table), record));
|
1320
|
+
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1220
1321
|
return new Page(query, meta, records);
|
1221
1322
|
}
|
1222
1323
|
}
|
1223
1324
|
_table = new WeakMap();
|
1224
|
-
_links = new WeakMap();
|
1225
1325
|
_getFetchProps = new WeakMap();
|
1326
|
+
_cache = new WeakMap();
|
1327
|
+
_schemaColumns = new WeakMap();
|
1226
1328
|
_insertRecordWithoutId = new WeakSet();
|
1227
1329
|
insertRecordWithoutId_fn = async function(object) {
|
1228
|
-
const fetchProps = await __privateGet$
|
1330
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1229
1331
|
const record = transformObjectLinks(object);
|
1230
1332
|
const response = await insertRecord({
|
1231
1333
|
pathParams: {
|
1232
1334
|
workspace: "{workspaceId}",
|
1233
1335
|
dbBranchName: "{dbBranch}",
|
1234
|
-
tableName: __privateGet$
|
1336
|
+
tableName: __privateGet$4(this, _table)
|
1235
1337
|
},
|
1236
1338
|
body: record,
|
1237
1339
|
...fetchProps
|
@@ -1244,13 +1346,13 @@ insertRecordWithoutId_fn = async function(object) {
|
|
1244
1346
|
};
|
1245
1347
|
_insertRecordWithId = new WeakSet();
|
1246
1348
|
insertRecordWithId_fn = async function(recordId, object) {
|
1247
|
-
const fetchProps = await __privateGet$
|
1349
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1248
1350
|
const record = transformObjectLinks(object);
|
1249
1351
|
const response = await insertRecordWithID({
|
1250
1352
|
pathParams: {
|
1251
1353
|
workspace: "{workspaceId}",
|
1252
1354
|
dbBranchName: "{dbBranch}",
|
1253
|
-
tableName: __privateGet$
|
1355
|
+
tableName: __privateGet$4(this, _table),
|
1254
1356
|
recordId
|
1255
1357
|
},
|
1256
1358
|
body: record,
|
@@ -1265,10 +1367,10 @@ insertRecordWithId_fn = async function(recordId, object) {
|
|
1265
1367
|
};
|
1266
1368
|
_bulkInsertTableRecords = new WeakSet();
|
1267
1369
|
bulkInsertTableRecords_fn = async function(objects) {
|
1268
|
-
const fetchProps = await __privateGet$
|
1370
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1269
1371
|
const records = objects.map((object) => transformObjectLinks(object));
|
1270
1372
|
const response = await bulkInsertTableRecords({
|
1271
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1373
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1272
1374
|
body: { records },
|
1273
1375
|
...fetchProps
|
1274
1376
|
});
|
@@ -1280,10 +1382,10 @@ bulkInsertTableRecords_fn = async function(objects) {
|
|
1280
1382
|
};
|
1281
1383
|
_updateRecordWithID = new WeakSet();
|
1282
1384
|
updateRecordWithID_fn = async function(recordId, object) {
|
1283
|
-
const fetchProps = await __privateGet$
|
1385
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1284
1386
|
const record = transformObjectLinks(object);
|
1285
1387
|
const response = await updateRecordWithID({
|
1286
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1388
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1287
1389
|
body: record,
|
1288
1390
|
...fetchProps
|
1289
1391
|
});
|
@@ -1294,9 +1396,9 @@ updateRecordWithID_fn = async function(recordId, object) {
|
|
1294
1396
|
};
|
1295
1397
|
_upsertRecordWithID = new WeakSet();
|
1296
1398
|
upsertRecordWithID_fn = async function(recordId, object) {
|
1297
|
-
const fetchProps = await __privateGet$
|
1399
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1298
1400
|
const response = await upsertRecordWithID({
|
1299
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1401
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1300
1402
|
body: object,
|
1301
1403
|
...fetchProps
|
1302
1404
|
});
|
@@ -1307,12 +1409,62 @@ upsertRecordWithID_fn = async function(recordId, object) {
|
|
1307
1409
|
};
|
1308
1410
|
_deleteRecord = new WeakSet();
|
1309
1411
|
deleteRecord_fn = async function(recordId) {
|
1310
|
-
const fetchProps = await __privateGet$
|
1412
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1311
1413
|
await deleteRecord({
|
1312
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1414
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1313
1415
|
...fetchProps
|
1314
1416
|
});
|
1315
1417
|
};
|
1418
|
+
_invalidateCache = new WeakSet();
|
1419
|
+
invalidateCache_fn = async function(recordId) {
|
1420
|
+
await __privateGet$4(this, _cache).delete(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1421
|
+
const cacheItems = await __privateGet$4(this, _cache).getAll();
|
1422
|
+
const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
|
1423
|
+
for (const [key, value] of queries) {
|
1424
|
+
const ids = getIds(value);
|
1425
|
+
if (ids.includes(recordId))
|
1426
|
+
await __privateGet$4(this, _cache).delete(key);
|
1427
|
+
}
|
1428
|
+
};
|
1429
|
+
_setCacheRecord = new WeakSet();
|
1430
|
+
setCacheRecord_fn = async function(record) {
|
1431
|
+
if (!__privateGet$4(this, _cache).cacheRecords)
|
1432
|
+
return;
|
1433
|
+
await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
|
1434
|
+
};
|
1435
|
+
_getCacheRecord = new WeakSet();
|
1436
|
+
getCacheRecord_fn = async function(recordId) {
|
1437
|
+
if (!__privateGet$4(this, _cache).cacheRecords)
|
1438
|
+
return null;
|
1439
|
+
return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1440
|
+
};
|
1441
|
+
_setCacheQuery = new WeakSet();
|
1442
|
+
setCacheQuery_fn = async function(query, meta, records) {
|
1443
|
+
await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
|
1444
|
+
};
|
1445
|
+
_getCacheQuery = new WeakSet();
|
1446
|
+
getCacheQuery_fn = async function(query) {
|
1447
|
+
const key = `query_${__privateGet$4(this, _table)}:${query.key()}`;
|
1448
|
+
const result = await __privateGet$4(this, _cache).get(key);
|
1449
|
+
if (!result)
|
1450
|
+
return null;
|
1451
|
+
const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
|
1452
|
+
if (!ttl || ttl < 0)
|
1453
|
+
return result;
|
1454
|
+
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1455
|
+
return hasExpired ? null : result;
|
1456
|
+
};
|
1457
|
+
_getSchemaColumns = new WeakSet();
|
1458
|
+
getSchemaColumns_fn = async function() {
|
1459
|
+
if (__privateGet$4(this, _schemaColumns))
|
1460
|
+
return __privateGet$4(this, _schemaColumns);
|
1461
|
+
const { columns } = await getTableSchema({
|
1462
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1463
|
+
...await __privateGet$4(this, _getFetchProps).call(this)
|
1464
|
+
});
|
1465
|
+
__privateSet$3(this, _schemaColumns, columns);
|
1466
|
+
return columns;
|
1467
|
+
};
|
1316
1468
|
const transformObjectLinks = (object) => {
|
1317
1469
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
1318
1470
|
if (key === "xata")
|
@@ -1320,15 +1472,30 @@ const transformObjectLinks = (object) => {
|
|
1320
1472
|
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1321
1473
|
}, {});
|
1322
1474
|
};
|
1323
|
-
const initObject = (db,
|
1475
|
+
const initObject = (db, columns, table, object) => {
|
1324
1476
|
const result = {};
|
1325
1477
|
Object.assign(result, object);
|
1326
|
-
const
|
1327
|
-
|
1328
|
-
|
1329
|
-
|
1330
|
-
|
1331
|
-
|
1478
|
+
for (const column of columns) {
|
1479
|
+
const value = result[column.name];
|
1480
|
+
switch (column.type) {
|
1481
|
+
case "datetime": {
|
1482
|
+
const date = new Date(value);
|
1483
|
+
if (isNaN(date.getTime())) {
|
1484
|
+
console.error(`Failed to parse date ${value} for field ${column.name}`);
|
1485
|
+
} else {
|
1486
|
+
result[column.name] = date;
|
1487
|
+
}
|
1488
|
+
break;
|
1489
|
+
}
|
1490
|
+
case "link": {
|
1491
|
+
const linkTable = column.link?.table;
|
1492
|
+
if (!linkTable) {
|
1493
|
+
console.error(`Failed to parse link for field ${column.name}`);
|
1494
|
+
} else if (value && isObject(value)) {
|
1495
|
+
result[column.name] = initObject(db, columns, linkTable, value);
|
1496
|
+
}
|
1497
|
+
break;
|
1498
|
+
}
|
1332
1499
|
}
|
1333
1500
|
}
|
1334
1501
|
result.read = function() {
|
@@ -1346,6 +1513,65 @@ const initObject = (db, links, table, object) => {
|
|
1346
1513
|
Object.freeze(result);
|
1347
1514
|
return result;
|
1348
1515
|
};
|
1516
|
+
function getIds(value) {
|
1517
|
+
if (Array.isArray(value)) {
|
1518
|
+
return value.map((item) => getIds(item)).flat();
|
1519
|
+
}
|
1520
|
+
if (!isObject(value))
|
1521
|
+
return [];
|
1522
|
+
const nestedIds = Object.values(value).map((item) => getIds(item)).flat();
|
1523
|
+
return isString(value.id) ? [value.id, ...nestedIds] : nestedIds;
|
1524
|
+
}
|
1525
|
+
|
1526
|
+
var __accessCheck$3 = (obj, member, msg) => {
|
1527
|
+
if (!member.has(obj))
|
1528
|
+
throw TypeError("Cannot " + msg);
|
1529
|
+
};
|
1530
|
+
var __privateGet$3 = (obj, member, getter) => {
|
1531
|
+
__accessCheck$3(obj, member, "read from private field");
|
1532
|
+
return getter ? getter.call(obj) : member.get(obj);
|
1533
|
+
};
|
1534
|
+
var __privateAdd$3 = (obj, member, value) => {
|
1535
|
+
if (member.has(obj))
|
1536
|
+
throw TypeError("Cannot add the same private member more than once");
|
1537
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1538
|
+
};
|
1539
|
+
var __privateSet$2 = (obj, member, value, setter) => {
|
1540
|
+
__accessCheck$3(obj, member, "write to private field");
|
1541
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1542
|
+
return value;
|
1543
|
+
};
|
1544
|
+
var _map;
|
1545
|
+
class SimpleCache {
|
1546
|
+
constructor(options = {}) {
|
1547
|
+
__privateAdd$3(this, _map, void 0);
|
1548
|
+
__privateSet$2(this, _map, /* @__PURE__ */ new Map());
|
1549
|
+
this.capacity = options.max ?? 500;
|
1550
|
+
this.cacheRecords = options.cacheRecords ?? true;
|
1551
|
+
this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
|
1552
|
+
}
|
1553
|
+
async getAll() {
|
1554
|
+
return Object.fromEntries(__privateGet$3(this, _map));
|
1555
|
+
}
|
1556
|
+
async get(key) {
|
1557
|
+
return __privateGet$3(this, _map).get(key) ?? null;
|
1558
|
+
}
|
1559
|
+
async set(key, value) {
|
1560
|
+
await this.delete(key);
|
1561
|
+
__privateGet$3(this, _map).set(key, value);
|
1562
|
+
if (__privateGet$3(this, _map).size > this.capacity) {
|
1563
|
+
const leastRecentlyUsed = __privateGet$3(this, _map).keys().next().value;
|
1564
|
+
await this.delete(leastRecentlyUsed);
|
1565
|
+
}
|
1566
|
+
}
|
1567
|
+
async delete(key) {
|
1568
|
+
__privateGet$3(this, _map).delete(key);
|
1569
|
+
}
|
1570
|
+
async clear() {
|
1571
|
+
return __privateGet$3(this, _map).clear();
|
1572
|
+
}
|
1573
|
+
}
|
1574
|
+
_map = new WeakMap();
|
1349
1575
|
|
1350
1576
|
const gt = (value) => ({ $gt: value });
|
1351
1577
|
const ge = (value) => ({ $ge: value });
|
@@ -1370,7 +1596,7 @@ var __accessCheck$2 = (obj, member, msg) => {
|
|
1370
1596
|
if (!member.has(obj))
|
1371
1597
|
throw TypeError("Cannot " + msg);
|
1372
1598
|
};
|
1373
|
-
var __privateGet$
|
1599
|
+
var __privateGet$2 = (obj, member, getter) => {
|
1374
1600
|
__accessCheck$2(obj, member, "read from private field");
|
1375
1601
|
return getter ? getter.call(obj) : member.get(obj);
|
1376
1602
|
};
|
@@ -1381,26 +1607,24 @@ var __privateAdd$2 = (obj, member, value) => {
|
|
1381
1607
|
};
|
1382
1608
|
var _tables;
|
1383
1609
|
class SchemaPlugin extends XataPlugin {
|
1384
|
-
constructor(
|
1610
|
+
constructor(tableNames) {
|
1385
1611
|
super();
|
1386
|
-
this.links = links;
|
1387
1612
|
this.tableNames = tableNames;
|
1388
1613
|
__privateAdd$2(this, _tables, {});
|
1389
1614
|
}
|
1390
|
-
build(
|
1391
|
-
const { getFetchProps } = options;
|
1392
|
-
const links = this.links;
|
1615
|
+
build(pluginOptions) {
|
1393
1616
|
const db = new Proxy({}, {
|
1394
1617
|
get: (_target, table) => {
|
1395
1618
|
if (!isString(table))
|
1396
1619
|
throw new Error("Invalid table name");
|
1397
|
-
if (!__privateGet$
|
1398
|
-
__privateGet$
|
1399
|
-
|
1620
|
+
if (!__privateGet$2(this, _tables)[table]) {
|
1621
|
+
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
|
1622
|
+
}
|
1623
|
+
return __privateGet$2(this, _tables)[table];
|
1400
1624
|
}
|
1401
1625
|
});
|
1402
1626
|
for (const table of this.tableNames ?? []) {
|
1403
|
-
db[table] = new RestRepository({ db,
|
1627
|
+
db[table] = new RestRepository({ db, pluginOptions, table });
|
1404
1628
|
}
|
1405
1629
|
return db;
|
1406
1630
|
}
|
@@ -1411,44 +1635,65 @@ var __accessCheck$1 = (obj, member, msg) => {
|
|
1411
1635
|
if (!member.has(obj))
|
1412
1636
|
throw TypeError("Cannot " + msg);
|
1413
1637
|
};
|
1638
|
+
var __privateGet$1 = (obj, member, getter) => {
|
1639
|
+
__accessCheck$1(obj, member, "read from private field");
|
1640
|
+
return getter ? getter.call(obj) : member.get(obj);
|
1641
|
+
};
|
1414
1642
|
var __privateAdd$1 = (obj, member, value) => {
|
1415
1643
|
if (member.has(obj))
|
1416
1644
|
throw TypeError("Cannot add the same private member more than once");
|
1417
1645
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1418
1646
|
};
|
1647
|
+
var __privateSet$1 = (obj, member, value, setter) => {
|
1648
|
+
__accessCheck$1(obj, member, "write to private field");
|
1649
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1650
|
+
return value;
|
1651
|
+
};
|
1419
1652
|
var __privateMethod$1 = (obj, member, method) => {
|
1420
1653
|
__accessCheck$1(obj, member, "access private method");
|
1421
1654
|
return method;
|
1422
1655
|
};
|
1423
|
-
var _search, search_fn;
|
1656
|
+
var _schema, _search, search_fn, _getSchema, getSchema_fn;
|
1424
1657
|
class SearchPlugin extends XataPlugin {
|
1425
|
-
constructor(db
|
1658
|
+
constructor(db) {
|
1426
1659
|
super();
|
1427
1660
|
this.db = db;
|
1428
|
-
this.links = links;
|
1429
1661
|
__privateAdd$1(this, _search);
|
1662
|
+
__privateAdd$1(this, _getSchema);
|
1663
|
+
__privateAdd$1(this, _schema, void 0);
|
1430
1664
|
}
|
1431
1665
|
build({ getFetchProps }) {
|
1432
1666
|
return {
|
1433
1667
|
all: async (query, options = {}) => {
|
1434
1668
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1669
|
+
const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
|
1435
1670
|
return records.map((record) => {
|
1436
1671
|
const { table = "orphan" } = record.xata;
|
1437
|
-
|
1672
|
+
const columns = schema.tables.find((t) => t.name === table)?.columns;
|
1673
|
+
if (!columns) {
|
1674
|
+
console.error(`No schema columns found for table ${table}`);
|
1675
|
+
}
|
1676
|
+
return { table, record: initObject(this.db, columns ?? [], table, record) };
|
1438
1677
|
});
|
1439
1678
|
},
|
1440
1679
|
byTable: async (query, options = {}) => {
|
1441
1680
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1681
|
+
const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
|
1442
1682
|
return records.reduce((acc, record) => {
|
1443
1683
|
const { table = "orphan" } = record.xata;
|
1684
|
+
const columns = schema.tables.find((t) => t.name === table)?.columns;
|
1685
|
+
if (!columns) {
|
1686
|
+
console.error(`No schema columns found for table ${table}`);
|
1687
|
+
}
|
1444
1688
|
const items = acc[table] ?? [];
|
1445
|
-
const item = initObject(this.db,
|
1689
|
+
const item = initObject(this.db, columns ?? [], table, record);
|
1446
1690
|
return { ...acc, [table]: [...items, item] };
|
1447
1691
|
}, {});
|
1448
1692
|
}
|
1449
1693
|
};
|
1450
1694
|
}
|
1451
1695
|
}
|
1696
|
+
_schema = new WeakMap();
|
1452
1697
|
_search = new WeakSet();
|
1453
1698
|
search_fn = async function(query, options, getFetchProps) {
|
1454
1699
|
const fetchProps = await getFetchProps();
|
@@ -1460,6 +1705,18 @@ search_fn = async function(query, options, getFetchProps) {
|
|
1460
1705
|
});
|
1461
1706
|
return records;
|
1462
1707
|
};
|
1708
|
+
_getSchema = new WeakSet();
|
1709
|
+
getSchema_fn = async function(getFetchProps) {
|
1710
|
+
if (__privateGet$1(this, _schema))
|
1711
|
+
return __privateGet$1(this, _schema);
|
1712
|
+
const fetchProps = await getFetchProps();
|
1713
|
+
const { schema } = await getBranchDetails({
|
1714
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1715
|
+
...fetchProps
|
1716
|
+
});
|
1717
|
+
__privateSet$1(this, _schema, schema);
|
1718
|
+
return schema;
|
1719
|
+
};
|
1463
1720
|
|
1464
1721
|
const isBranchStrategyBuilder = (strategy) => {
|
1465
1722
|
return typeof strategy === "function";
|
@@ -1568,22 +1825,24 @@ var __privateMethod = (obj, member, method) => {
|
|
1568
1825
|
const buildClient = (plugins) => {
|
1569
1826
|
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1570
1827
|
return _a = class {
|
1571
|
-
constructor(options = {},
|
1828
|
+
constructor(options = {}, tables) {
|
1572
1829
|
__privateAdd(this, _parseOptions);
|
1573
1830
|
__privateAdd(this, _getFetchProps);
|
1574
1831
|
__privateAdd(this, _evaluateBranch);
|
1575
1832
|
__privateAdd(this, _branch, void 0);
|
1576
1833
|
const safeOptions = __privateMethod(this, _parseOptions, parseOptions_fn).call(this, options);
|
1577
|
-
const
|
1578
|
-
|
1579
|
-
|
1580
|
-
}
|
1834
|
+
const pluginOptions = {
|
1835
|
+
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
1836
|
+
cache: safeOptions.cache
|
1837
|
+
};
|
1838
|
+
const db = new SchemaPlugin(tables).build(pluginOptions);
|
1839
|
+
const search = new SearchPlugin(db).build(pluginOptions);
|
1581
1840
|
this.db = db;
|
1582
1841
|
this.search = search;
|
1583
1842
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
1584
1843
|
if (!namespace)
|
1585
1844
|
continue;
|
1586
|
-
const result = namespace.build(
|
1845
|
+
const result = namespace.build(pluginOptions);
|
1587
1846
|
if (result instanceof Promise) {
|
1588
1847
|
void result.then((namespace2) => {
|
1589
1848
|
this[key] = namespace2;
|
@@ -1597,11 +1856,12 @@ const buildClient = (plugins) => {
|
|
1597
1856
|
const fetch = getFetchImplementation(options?.fetch);
|
1598
1857
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1599
1858
|
const apiKey = options?.apiKey || getAPIKey();
|
1859
|
+
const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
|
1600
1860
|
const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
1601
1861
|
if (!databaseURL || !apiKey) {
|
1602
1862
|
throw new Error("Options databaseURL and apiKey are required");
|
1603
1863
|
}
|
1604
|
-
return { fetch, databaseURL, apiKey, branch };
|
1864
|
+
return { fetch, databaseURL, apiKey, branch, cache };
|
1605
1865
|
}, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({
|
1606
1866
|
fetch,
|
1607
1867
|
apiKey,
|
@@ -1649,5 +1909,5 @@ class XataError extends Error {
|
|
1649
1909
|
}
|
1650
1910
|
}
|
1651
1911
|
|
1652
|
-
export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, Repository, RestRepository, SchemaPlugin, SearchPlugin, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addTableColumn, buildClient, bulkInsertTableRecords, cancelWorkspaceMemberInvite, contains, createBranch, createDatabase, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, endsWith, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseURL, getRecord, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getWorkspace, getWorkspaceMembersList, getWorkspacesList, gt, gte, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isIdentifiable, isNot, isXataRecord, le, lt, lte, notExists, operationsByTag, pattern, queryTable, removeWorkspaceMember, resendWorkspaceMemberInvite, searchBranch, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
|
1912
|
+
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 };
|
1653
1913
|
//# sourceMappingURL=index.mjs.map
|