@xata.io/client 0.9.1 → 0.10.2
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 +260 -175
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +150 -88
- package/dist/index.mjs +260 -176
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/tsconfig.json +1 -0
package/dist/index.cjs
CHANGED
@@ -370,6 +370,11 @@ const queryTable = (variables) => fetch$1({
|
|
370
370
|
method: "post",
|
371
371
|
...variables
|
372
372
|
});
|
373
|
+
const searchTable = (variables) => fetch$1({
|
374
|
+
url: "/db/{dbBranchName}/tables/{tableName}/search",
|
375
|
+
method: "post",
|
376
|
+
...variables
|
377
|
+
});
|
373
378
|
const searchBranch = (variables) => fetch$1({
|
374
379
|
url: "/db/{dbBranchName}/search",
|
375
380
|
method: "post",
|
@@ -433,6 +438,7 @@ const operationsByTag = {
|
|
433
438
|
getRecord,
|
434
439
|
bulkInsertTableRecords,
|
435
440
|
queryTable,
|
441
|
+
searchTable,
|
436
442
|
searchBranch
|
437
443
|
}
|
438
444
|
};
|
@@ -466,7 +472,7 @@ var __accessCheck$7 = (obj, member, msg) => {
|
|
466
472
|
if (!member.has(obj))
|
467
473
|
throw TypeError("Cannot " + msg);
|
468
474
|
};
|
469
|
-
var __privateGet$
|
475
|
+
var __privateGet$7 = (obj, member, getter) => {
|
470
476
|
__accessCheck$7(obj, member, "read from private field");
|
471
477
|
return getter ? getter.call(obj) : member.get(obj);
|
472
478
|
};
|
@@ -475,7 +481,7 @@ var __privateAdd$7 = (obj, member, value) => {
|
|
475
481
|
throw TypeError("Cannot add the same private member more than once");
|
476
482
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
477
483
|
};
|
478
|
-
var __privateSet$
|
484
|
+
var __privateSet$6 = (obj, member, value, setter) => {
|
479
485
|
__accessCheck$7(obj, member, "write to private field");
|
480
486
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
481
487
|
return value;
|
@@ -490,7 +496,7 @@ class XataApiClient {
|
|
490
496
|
if (!apiKey) {
|
491
497
|
throw new Error("Could not resolve a valid apiKey");
|
492
498
|
}
|
493
|
-
__privateSet$
|
499
|
+
__privateSet$6(this, _extraProps, {
|
494
500
|
apiUrl: getHostUrl(provider, "main"),
|
495
501
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
496
502
|
fetchImpl: getFetchImplementation(options.fetch),
|
@@ -498,34 +504,34 @@ class XataApiClient {
|
|
498
504
|
});
|
499
505
|
}
|
500
506
|
get user() {
|
501
|
-
if (!__privateGet$
|
502
|
-
__privateGet$
|
503
|
-
return __privateGet$
|
507
|
+
if (!__privateGet$7(this, _namespaces).user)
|
508
|
+
__privateGet$7(this, _namespaces).user = new UserApi(__privateGet$7(this, _extraProps));
|
509
|
+
return __privateGet$7(this, _namespaces).user;
|
504
510
|
}
|
505
511
|
get workspaces() {
|
506
|
-
if (!__privateGet$
|
507
|
-
__privateGet$
|
508
|
-
return __privateGet$
|
512
|
+
if (!__privateGet$7(this, _namespaces).workspaces)
|
513
|
+
__privateGet$7(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$7(this, _extraProps));
|
514
|
+
return __privateGet$7(this, _namespaces).workspaces;
|
509
515
|
}
|
510
516
|
get databases() {
|
511
|
-
if (!__privateGet$
|
512
|
-
__privateGet$
|
513
|
-
return __privateGet$
|
517
|
+
if (!__privateGet$7(this, _namespaces).databases)
|
518
|
+
__privateGet$7(this, _namespaces).databases = new DatabaseApi(__privateGet$7(this, _extraProps));
|
519
|
+
return __privateGet$7(this, _namespaces).databases;
|
514
520
|
}
|
515
521
|
get branches() {
|
516
|
-
if (!__privateGet$
|
517
|
-
__privateGet$
|
518
|
-
return __privateGet$
|
522
|
+
if (!__privateGet$7(this, _namespaces).branches)
|
523
|
+
__privateGet$7(this, _namespaces).branches = new BranchApi(__privateGet$7(this, _extraProps));
|
524
|
+
return __privateGet$7(this, _namespaces).branches;
|
519
525
|
}
|
520
526
|
get tables() {
|
521
|
-
if (!__privateGet$
|
522
|
-
__privateGet$
|
523
|
-
return __privateGet$
|
527
|
+
if (!__privateGet$7(this, _namespaces).tables)
|
528
|
+
__privateGet$7(this, _namespaces).tables = new TableApi(__privateGet$7(this, _extraProps));
|
529
|
+
return __privateGet$7(this, _namespaces).tables;
|
524
530
|
}
|
525
531
|
get records() {
|
526
|
-
if (!__privateGet$
|
527
|
-
__privateGet$
|
528
|
-
return __privateGet$
|
532
|
+
if (!__privateGet$7(this, _namespaces).records)
|
533
|
+
__privateGet$7(this, _namespaces).records = new RecordsApi(__privateGet$7(this, _extraProps));
|
534
|
+
return __privateGet$7(this, _namespaces).records;
|
529
535
|
}
|
530
536
|
}
|
531
537
|
_extraProps = new WeakMap();
|
@@ -703,10 +709,10 @@ class BranchApi {
|
|
703
709
|
...this.extraProps
|
704
710
|
});
|
705
711
|
}
|
706
|
-
createBranch(workspace, database, branch, from
|
712
|
+
createBranch(workspace, database, branch, from, options = {}) {
|
707
713
|
return operationsByTag.branch.createBranch({
|
708
714
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
709
|
-
queryParams: { from },
|
715
|
+
queryParams: isString(from) ? { from } : void 0,
|
710
716
|
body: options,
|
711
717
|
...this.extraProps
|
712
718
|
});
|
@@ -888,6 +894,13 @@ class RecordsApi {
|
|
888
894
|
...this.extraProps
|
889
895
|
});
|
890
896
|
}
|
897
|
+
searchTable(workspace, database, branch, tableName, query) {
|
898
|
+
return operationsByTag.records.searchTable({
|
899
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
900
|
+
body: query,
|
901
|
+
...this.extraProps
|
902
|
+
});
|
903
|
+
}
|
891
904
|
searchBranch(workspace, database, branch, query) {
|
892
905
|
return operationsByTag.records.searchBranch({
|
893
906
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
@@ -911,7 +924,7 @@ var __accessCheck$6 = (obj, member, msg) => {
|
|
911
924
|
if (!member.has(obj))
|
912
925
|
throw TypeError("Cannot " + msg);
|
913
926
|
};
|
914
|
-
var __privateGet$
|
927
|
+
var __privateGet$6 = (obj, member, getter) => {
|
915
928
|
__accessCheck$6(obj, member, "read from private field");
|
916
929
|
return getter ? getter.call(obj) : member.get(obj);
|
917
930
|
};
|
@@ -920,7 +933,7 @@ var __privateAdd$6 = (obj, member, value) => {
|
|
920
933
|
throw TypeError("Cannot add the same private member more than once");
|
921
934
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
922
935
|
};
|
923
|
-
var __privateSet$
|
936
|
+
var __privateSet$5 = (obj, member, value, setter) => {
|
924
937
|
__accessCheck$6(obj, member, "write to private field");
|
925
938
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
926
939
|
return value;
|
@@ -929,21 +942,21 @@ var _query;
|
|
929
942
|
class Page {
|
930
943
|
constructor(query, meta, records = []) {
|
931
944
|
__privateAdd$6(this, _query, void 0);
|
932
|
-
__privateSet$
|
945
|
+
__privateSet$5(this, _query, query);
|
933
946
|
this.meta = meta;
|
934
947
|
this.records = records;
|
935
948
|
}
|
936
949
|
async nextPage(size, offset) {
|
937
|
-
return __privateGet$
|
950
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
|
938
951
|
}
|
939
952
|
async previousPage(size, offset) {
|
940
|
-
return __privateGet$
|
953
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, before: this.meta.page.cursor } });
|
941
954
|
}
|
942
955
|
async firstPage(size, offset) {
|
943
|
-
return __privateGet$
|
956
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, first: this.meta.page.cursor } });
|
944
957
|
}
|
945
958
|
async lastPage(size, offset) {
|
946
|
-
return __privateGet$
|
959
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, last: this.meta.page.cursor } });
|
947
960
|
}
|
948
961
|
hasNextPage() {
|
949
962
|
return this.meta.page.more;
|
@@ -959,7 +972,7 @@ var __accessCheck$5 = (obj, member, msg) => {
|
|
959
972
|
if (!member.has(obj))
|
960
973
|
throw TypeError("Cannot " + msg);
|
961
974
|
};
|
962
|
-
var __privateGet$
|
975
|
+
var __privateGet$5 = (obj, member, getter) => {
|
963
976
|
__accessCheck$5(obj, member, "read from private field");
|
964
977
|
return getter ? getter.call(obj) : member.get(obj);
|
965
978
|
};
|
@@ -968,7 +981,7 @@ var __privateAdd$5 = (obj, member, value) => {
|
|
968
981
|
throw TypeError("Cannot add the same private member more than once");
|
969
982
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
970
983
|
};
|
971
|
-
var __privateSet$
|
984
|
+
var __privateSet$4 = (obj, member, value, setter) => {
|
972
985
|
__accessCheck$5(obj, member, "write to private field");
|
973
986
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
974
987
|
return value;
|
@@ -981,21 +994,21 @@ const _Query = class {
|
|
981
994
|
__privateAdd$5(this, _data, { filter: {} });
|
982
995
|
this.meta = { page: { cursor: "start", more: true } };
|
983
996
|
this.records = [];
|
984
|
-
__privateSet$
|
997
|
+
__privateSet$4(this, _table$1, table);
|
985
998
|
if (repository) {
|
986
|
-
__privateSet$
|
999
|
+
__privateSet$4(this, _repository, repository);
|
987
1000
|
} else {
|
988
|
-
__privateSet$
|
1001
|
+
__privateSet$4(this, _repository, this);
|
989
1002
|
}
|
990
|
-
__privateGet$
|
991
|
-
__privateGet$
|
992
|
-
__privateGet$
|
993
|
-
__privateGet$
|
994
|
-
__privateGet$
|
995
|
-
__privateGet$
|
996
|
-
__privateGet$
|
997
|
-
__privateGet$
|
998
|
-
__privateGet$
|
1003
|
+
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
1004
|
+
__privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
|
1005
|
+
__privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
|
1006
|
+
__privateGet$5(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
|
1007
|
+
__privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
|
1008
|
+
__privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
|
1009
|
+
__privateGet$5(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
|
1010
|
+
__privateGet$5(this, _data).pagination = data.pagination ?? parent?.pagination;
|
1011
|
+
__privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
|
999
1012
|
this.any = this.any.bind(this);
|
1000
1013
|
this.all = this.all.bind(this);
|
1001
1014
|
this.not = this.not.bind(this);
|
@@ -1006,63 +1019,64 @@ const _Query = class {
|
|
1006
1019
|
Object.defineProperty(this, "repository", { enumerable: false });
|
1007
1020
|
}
|
1008
1021
|
getQueryOptions() {
|
1009
|
-
return __privateGet$
|
1022
|
+
return __privateGet$5(this, _data);
|
1010
1023
|
}
|
1011
1024
|
key() {
|
1012
|
-
const { columns = [], filter = {}, sort = [],
|
1013
|
-
const key = JSON.stringify({ columns, filter, sort,
|
1025
|
+
const { columns = [], filter = {}, sort = [], pagination = {} } = __privateGet$5(this, _data);
|
1026
|
+
const key = JSON.stringify({ columns, filter, sort, pagination });
|
1014
1027
|
return toBase64(key);
|
1015
1028
|
}
|
1016
1029
|
any(...queries) {
|
1017
1030
|
const $any = queries.map((query) => query.getQueryOptions().filter ?? {});
|
1018
|
-
return new _Query(__privateGet$
|
1031
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $any } }, __privateGet$5(this, _data));
|
1019
1032
|
}
|
1020
1033
|
all(...queries) {
|
1021
1034
|
const $all = queries.map((query) => query.getQueryOptions().filter ?? {});
|
1022
|
-
return new _Query(__privateGet$
|
1035
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
1023
1036
|
}
|
1024
1037
|
not(...queries) {
|
1025
1038
|
const $not = queries.map((query) => query.getQueryOptions().filter ?? {});
|
1026
|
-
return new _Query(__privateGet$
|
1039
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $not } }, __privateGet$5(this, _data));
|
1027
1040
|
}
|
1028
1041
|
none(...queries) {
|
1029
1042
|
const $none = queries.map((query) => query.getQueryOptions().filter ?? {});
|
1030
|
-
return new _Query(__privateGet$
|
1043
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $none } }, __privateGet$5(this, _data));
|
1031
1044
|
}
|
1032
1045
|
filter(a, b) {
|
1033
1046
|
if (arguments.length === 1) {
|
1034
1047
|
const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
|
1035
|
-
const $all = compact([__privateGet$
|
1036
|
-
return new _Query(__privateGet$
|
1048
|
+
const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat(constraints));
|
1049
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
1037
1050
|
} else {
|
1038
|
-
const $all = compact([__privateGet$
|
1039
|
-
return new _Query(__privateGet$
|
1051
|
+
const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
|
1052
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
1040
1053
|
}
|
1041
1054
|
}
|
1042
1055
|
sort(column, direction) {
|
1043
|
-
const originalSort = [__privateGet$
|
1056
|
+
const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
|
1044
1057
|
const sort = [...originalSort, { column, direction }];
|
1045
|
-
return new _Query(__privateGet$
|
1058
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
|
1046
1059
|
}
|
1047
1060
|
select(columns) {
|
1048
|
-
return new _Query(__privateGet$
|
1061
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { columns }, __privateGet$5(this, _data));
|
1049
1062
|
}
|
1050
1063
|
getPaginated(options = {}) {
|
1051
|
-
const query = new _Query(__privateGet$
|
1052
|
-
return __privateGet$
|
1064
|
+
const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
|
1065
|
+
return __privateGet$5(this, _repository).query(query);
|
1053
1066
|
}
|
1054
1067
|
async *[Symbol.asyncIterator]() {
|
1055
|
-
for await (const [record] of this.getIterator(1)) {
|
1068
|
+
for await (const [record] of this.getIterator({ batchSize: 1 })) {
|
1056
1069
|
yield record;
|
1057
1070
|
}
|
1058
1071
|
}
|
1059
|
-
async *getIterator(
|
1072
|
+
async *getIterator(options = {}) {
|
1073
|
+
const { batchSize = 1 } = options;
|
1060
1074
|
let offset = 0;
|
1061
1075
|
let end = false;
|
1062
1076
|
while (!end) {
|
1063
|
-
const { records, meta } = await this.getPaginated({ ...options,
|
1077
|
+
const { records, meta } = await this.getPaginated({ ...options, pagination: { size: batchSize, offset } });
|
1064
1078
|
yield records;
|
1065
|
-
offset +=
|
1079
|
+
offset += batchSize;
|
1066
1080
|
end = !meta.page.more;
|
1067
1081
|
}
|
1068
1082
|
}
|
@@ -1070,19 +1084,20 @@ const _Query = class {
|
|
1070
1084
|
const { records } = await this.getPaginated(options);
|
1071
1085
|
return records;
|
1072
1086
|
}
|
1073
|
-
async getAll(
|
1087
|
+
async getAll(options = {}) {
|
1088
|
+
const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
|
1074
1089
|
const results = [];
|
1075
|
-
for await (const page of this.getIterator(
|
1090
|
+
for await (const page of this.getIterator({ ...rest, batchSize })) {
|
1076
1091
|
results.push(...page);
|
1077
1092
|
}
|
1078
1093
|
return results;
|
1079
1094
|
}
|
1080
1095
|
async getFirst(options = {}) {
|
1081
|
-
const records = await this.getMany({ ...options,
|
1082
|
-
return records[0]
|
1096
|
+
const records = await this.getMany({ ...options, pagination: { size: 1 } });
|
1097
|
+
return records[0] ?? null;
|
1083
1098
|
}
|
1084
1099
|
cache(ttl) {
|
1085
|
-
return new _Query(__privateGet$
|
1100
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
|
1086
1101
|
}
|
1087
1102
|
nextPage(size, offset) {
|
1088
1103
|
return this.firstPage(size, offset);
|
@@ -1091,10 +1106,10 @@ const _Query = class {
|
|
1091
1106
|
return this.firstPage(size, offset);
|
1092
1107
|
}
|
1093
1108
|
firstPage(size, offset) {
|
1094
|
-
return this.getPaginated({
|
1109
|
+
return this.getPaginated({ pagination: { size, offset } });
|
1095
1110
|
}
|
1096
1111
|
lastPage(size, offset) {
|
1097
|
-
return this.getPaginated({
|
1112
|
+
return this.getPaginated({ pagination: { size, offset, before: "end" } });
|
1098
1113
|
}
|
1099
1114
|
hasNextPage() {
|
1100
1115
|
return this.meta.page.more;
|
@@ -1139,7 +1154,7 @@ var __accessCheck$4 = (obj, member, msg) => {
|
|
1139
1154
|
if (!member.has(obj))
|
1140
1155
|
throw TypeError("Cannot " + msg);
|
1141
1156
|
};
|
1142
|
-
var __privateGet$
|
1157
|
+
var __privateGet$4 = (obj, member, getter) => {
|
1143
1158
|
__accessCheck$4(obj, member, "read from private field");
|
1144
1159
|
return getter ? getter.call(obj) : member.get(obj);
|
1145
1160
|
};
|
@@ -1148,7 +1163,7 @@ var __privateAdd$4 = (obj, member, value) => {
|
|
1148
1163
|
throw TypeError("Cannot add the same private member more than once");
|
1149
1164
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1150
1165
|
};
|
1151
|
-
var __privateSet$
|
1166
|
+
var __privateSet$3 = (obj, member, value, setter) => {
|
1152
1167
|
__accessCheck$4(obj, member, "write to private field");
|
1153
1168
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1154
1169
|
return value;
|
@@ -1157,7 +1172,7 @@ var __privateMethod$2 = (obj, member, method) => {
|
|
1157
1172
|
__accessCheck$4(obj, member, "access private method");
|
1158
1173
|
return method;
|
1159
1174
|
};
|
1160
|
-
var _table,
|
1175
|
+
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;
|
1161
1176
|
class Repository extends Query {
|
1162
1177
|
}
|
1163
1178
|
class RestRepository extends Query {
|
@@ -1174,15 +1189,15 @@ class RestRepository extends Query {
|
|
1174
1189
|
__privateAdd$4(this, _getCacheRecord);
|
1175
1190
|
__privateAdd$4(this, _setCacheQuery);
|
1176
1191
|
__privateAdd$4(this, _getCacheQuery);
|
1192
|
+
__privateAdd$4(this, _getSchema$1);
|
1177
1193
|
__privateAdd$4(this, _table, void 0);
|
1178
|
-
__privateAdd$4(this, _links, void 0);
|
1179
1194
|
__privateAdd$4(this, _getFetchProps, void 0);
|
1180
1195
|
__privateAdd$4(this, _cache, void 0);
|
1181
|
-
|
1182
|
-
__privateSet$
|
1183
|
-
__privateSet$
|
1196
|
+
__privateAdd$4(this, _schema$1, void 0);
|
1197
|
+
__privateSet$3(this, _table, options.table);
|
1198
|
+
__privateSet$3(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
1184
1199
|
this.db = options.db;
|
1185
|
-
__privateSet$
|
1200
|
+
__privateSet$3(this, _cache, options.pluginOptions.cache);
|
1186
1201
|
}
|
1187
1202
|
async create(a, b) {
|
1188
1203
|
if (Array.isArray(a)) {
|
@@ -1215,13 +1230,14 @@ class RestRepository extends Query {
|
|
1215
1230
|
const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, recordId);
|
1216
1231
|
if (cacheRecord)
|
1217
1232
|
return cacheRecord;
|
1218
|
-
const fetchProps = await __privateGet$
|
1233
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1219
1234
|
try {
|
1220
1235
|
const response = await getRecord({
|
1221
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1236
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1222
1237
|
...fetchProps
|
1223
1238
|
});
|
1224
|
-
|
1239
|
+
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1240
|
+
return initObject(this.db, schema, __privateGet$4(this, _table), response);
|
1225
1241
|
} catch (e) {
|
1226
1242
|
if (isObject(e) && e.status === 404) {
|
1227
1243
|
return null;
|
@@ -1292,13 +1308,18 @@ class RestRepository extends Query {
|
|
1292
1308
|
throw new Error("Invalid arguments for delete method");
|
1293
1309
|
}
|
1294
1310
|
async search(query, options = {}) {
|
1295
|
-
const fetchProps = await __privateGet$
|
1296
|
-
const { records } = await
|
1297
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1298
|
-
body: {
|
1311
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1312
|
+
const { records } = await searchTable({
|
1313
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1314
|
+
body: {
|
1315
|
+
query,
|
1316
|
+
fuzziness: options.fuzziness,
|
1317
|
+
filter: options.filter
|
1318
|
+
},
|
1299
1319
|
...fetchProps
|
1300
1320
|
});
|
1301
|
-
|
1321
|
+
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1322
|
+
return records.map((item) => initObject(this.db, schema, __privateGet$4(this, _table), item));
|
1302
1323
|
}
|
1303
1324
|
async query(query) {
|
1304
1325
|
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
@@ -1307,34 +1328,35 @@ class RestRepository extends Query {
|
|
1307
1328
|
const data = query.getQueryOptions();
|
1308
1329
|
const body = {
|
1309
1330
|
filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
|
1310
|
-
sort: data.sort ? buildSortFilter(data.sort) : void 0,
|
1311
|
-
page: data.
|
1331
|
+
sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
|
1332
|
+
page: data.pagination,
|
1312
1333
|
columns: data.columns
|
1313
1334
|
};
|
1314
|
-
const fetchProps = await __privateGet$
|
1335
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1315
1336
|
const { meta, records: objects } = await queryTable({
|
1316
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1337
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1317
1338
|
body,
|
1318
1339
|
...fetchProps
|
1319
1340
|
});
|
1320
|
-
const
|
1341
|
+
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1342
|
+
const records = objects.map((record) => initObject(this.db, schema, __privateGet$4(this, _table), record));
|
1321
1343
|
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1322
1344
|
return new Page(query, meta, records);
|
1323
1345
|
}
|
1324
1346
|
}
|
1325
1347
|
_table = new WeakMap();
|
1326
|
-
_links = new WeakMap();
|
1327
1348
|
_getFetchProps = new WeakMap();
|
1328
1349
|
_cache = new WeakMap();
|
1350
|
+
_schema$1 = new WeakMap();
|
1329
1351
|
_insertRecordWithoutId = new WeakSet();
|
1330
1352
|
insertRecordWithoutId_fn = async function(object) {
|
1331
|
-
const fetchProps = await __privateGet$
|
1353
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1332
1354
|
const record = transformObjectLinks(object);
|
1333
1355
|
const response = await insertRecord({
|
1334
1356
|
pathParams: {
|
1335
1357
|
workspace: "{workspaceId}",
|
1336
1358
|
dbBranchName: "{dbBranch}",
|
1337
|
-
tableName: __privateGet$
|
1359
|
+
tableName: __privateGet$4(this, _table)
|
1338
1360
|
},
|
1339
1361
|
body: record,
|
1340
1362
|
...fetchProps
|
@@ -1347,13 +1369,13 @@ insertRecordWithoutId_fn = async function(object) {
|
|
1347
1369
|
};
|
1348
1370
|
_insertRecordWithId = new WeakSet();
|
1349
1371
|
insertRecordWithId_fn = async function(recordId, object) {
|
1350
|
-
const fetchProps = await __privateGet$
|
1372
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1351
1373
|
const record = transformObjectLinks(object);
|
1352
1374
|
const response = await insertRecordWithID({
|
1353
1375
|
pathParams: {
|
1354
1376
|
workspace: "{workspaceId}",
|
1355
1377
|
dbBranchName: "{dbBranch}",
|
1356
|
-
tableName: __privateGet$
|
1378
|
+
tableName: __privateGet$4(this, _table),
|
1357
1379
|
recordId
|
1358
1380
|
},
|
1359
1381
|
body: record,
|
@@ -1368,10 +1390,10 @@ insertRecordWithId_fn = async function(recordId, object) {
|
|
1368
1390
|
};
|
1369
1391
|
_bulkInsertTableRecords = new WeakSet();
|
1370
1392
|
bulkInsertTableRecords_fn = async function(objects) {
|
1371
|
-
const fetchProps = await __privateGet$
|
1393
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1372
1394
|
const records = objects.map((object) => transformObjectLinks(object));
|
1373
1395
|
const response = await bulkInsertTableRecords({
|
1374
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1396
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1375
1397
|
body: { records },
|
1376
1398
|
...fetchProps
|
1377
1399
|
});
|
@@ -1383,10 +1405,10 @@ bulkInsertTableRecords_fn = async function(objects) {
|
|
1383
1405
|
};
|
1384
1406
|
_updateRecordWithID = new WeakSet();
|
1385
1407
|
updateRecordWithID_fn = async function(recordId, object) {
|
1386
|
-
const fetchProps = await __privateGet$
|
1408
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1387
1409
|
const record = transformObjectLinks(object);
|
1388
1410
|
const response = await updateRecordWithID({
|
1389
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1411
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1390
1412
|
body: record,
|
1391
1413
|
...fetchProps
|
1392
1414
|
});
|
@@ -1397,9 +1419,9 @@ updateRecordWithID_fn = async function(recordId, object) {
|
|
1397
1419
|
};
|
1398
1420
|
_upsertRecordWithID = new WeakSet();
|
1399
1421
|
upsertRecordWithID_fn = async function(recordId, object) {
|
1400
|
-
const fetchProps = await __privateGet$
|
1422
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1401
1423
|
const response = await upsertRecordWithID({
|
1402
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1424
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1403
1425
|
body: object,
|
1404
1426
|
...fetchProps
|
1405
1427
|
});
|
@@ -1410,51 +1432,63 @@ upsertRecordWithID_fn = async function(recordId, object) {
|
|
1410
1432
|
};
|
1411
1433
|
_deleteRecord = new WeakSet();
|
1412
1434
|
deleteRecord_fn = async function(recordId) {
|
1413
|
-
const fetchProps = await __privateGet$
|
1435
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1414
1436
|
await deleteRecord({
|
1415
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1437
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1416
1438
|
...fetchProps
|
1417
1439
|
});
|
1418
1440
|
};
|
1419
1441
|
_invalidateCache = new WeakSet();
|
1420
1442
|
invalidateCache_fn = async function(recordId) {
|
1421
|
-
await __privateGet$
|
1422
|
-
const cacheItems = await __privateGet$
|
1443
|
+
await __privateGet$4(this, _cache).delete(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1444
|
+
const cacheItems = await __privateGet$4(this, _cache).getAll();
|
1423
1445
|
const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
|
1424
1446
|
for (const [key, value] of queries) {
|
1425
1447
|
const ids = getIds(value);
|
1426
1448
|
if (ids.includes(recordId))
|
1427
|
-
await __privateGet$
|
1449
|
+
await __privateGet$4(this, _cache).delete(key);
|
1428
1450
|
}
|
1429
1451
|
};
|
1430
1452
|
_setCacheRecord = new WeakSet();
|
1431
1453
|
setCacheRecord_fn = async function(record) {
|
1432
|
-
if (!__privateGet$
|
1454
|
+
if (!__privateGet$4(this, _cache).cacheRecords)
|
1433
1455
|
return;
|
1434
|
-
await __privateGet$
|
1456
|
+
await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
|
1435
1457
|
};
|
1436
1458
|
_getCacheRecord = new WeakSet();
|
1437
1459
|
getCacheRecord_fn = async function(recordId) {
|
1438
|
-
if (!__privateGet$
|
1460
|
+
if (!__privateGet$4(this, _cache).cacheRecords)
|
1439
1461
|
return null;
|
1440
|
-
return __privateGet$
|
1462
|
+
return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1441
1463
|
};
|
1442
1464
|
_setCacheQuery = new WeakSet();
|
1443
1465
|
setCacheQuery_fn = async function(query, meta, records) {
|
1444
|
-
await __privateGet$
|
1466
|
+
await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
|
1445
1467
|
};
|
1446
1468
|
_getCacheQuery = new WeakSet();
|
1447
1469
|
getCacheQuery_fn = async function(query) {
|
1448
|
-
const key = `query_${__privateGet$
|
1449
|
-
const result = await __privateGet$
|
1470
|
+
const key = `query_${__privateGet$4(this, _table)}:${query.key()}`;
|
1471
|
+
const result = await __privateGet$4(this, _cache).get(key);
|
1450
1472
|
if (!result)
|
1451
1473
|
return null;
|
1452
|
-
const { cache: ttl = __privateGet$
|
1453
|
-
if (
|
1454
|
-
return
|
1474
|
+
const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
|
1475
|
+
if (ttl < 0)
|
1476
|
+
return null;
|
1455
1477
|
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1456
1478
|
return hasExpired ? null : result;
|
1457
1479
|
};
|
1480
|
+
_getSchema$1 = new WeakSet();
|
1481
|
+
getSchema_fn$1 = async function() {
|
1482
|
+
if (__privateGet$4(this, _schema$1))
|
1483
|
+
return __privateGet$4(this, _schema$1);
|
1484
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1485
|
+
const { schema } = await getBranchDetails({
|
1486
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1487
|
+
...fetchProps
|
1488
|
+
});
|
1489
|
+
__privateSet$3(this, _schema$1, schema);
|
1490
|
+
return schema;
|
1491
|
+
};
|
1458
1492
|
const transformObjectLinks = (object) => {
|
1459
1493
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
1460
1494
|
if (key === "xata")
|
@@ -1462,15 +1496,33 @@ const transformObjectLinks = (object) => {
|
|
1462
1496
|
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1463
1497
|
}, {});
|
1464
1498
|
};
|
1465
|
-
const initObject = (db,
|
1499
|
+
const initObject = (db, schema, table, object) => {
|
1466
1500
|
const result = {};
|
1467
1501
|
Object.assign(result, object);
|
1468
|
-
const
|
1469
|
-
|
1470
|
-
|
1471
|
-
|
1472
|
-
|
1473
|
-
|
1502
|
+
const { columns } = schema.tables.find(({ name }) => name === table) ?? {};
|
1503
|
+
if (!columns)
|
1504
|
+
console.error(`Table ${table} not found in schema`);
|
1505
|
+
for (const column of columns ?? []) {
|
1506
|
+
const value = result[column.name];
|
1507
|
+
switch (column.type) {
|
1508
|
+
case "datetime": {
|
1509
|
+
const date = new Date(value);
|
1510
|
+
if (isNaN(date.getTime())) {
|
1511
|
+
console.error(`Failed to parse date ${value} for field ${column.name}`);
|
1512
|
+
} else {
|
1513
|
+
result[column.name] = date;
|
1514
|
+
}
|
1515
|
+
break;
|
1516
|
+
}
|
1517
|
+
case "link": {
|
1518
|
+
const linkTable = column.link?.table;
|
1519
|
+
if (!linkTable) {
|
1520
|
+
console.error(`Failed to parse link for field ${column.name}`);
|
1521
|
+
} else if (isObject(value)) {
|
1522
|
+
result[column.name] = initObject(db, schema, linkTable, value);
|
1523
|
+
}
|
1524
|
+
break;
|
1525
|
+
}
|
1474
1526
|
}
|
1475
1527
|
}
|
1476
1528
|
result.read = function() {
|
@@ -1502,7 +1554,7 @@ var __accessCheck$3 = (obj, member, msg) => {
|
|
1502
1554
|
if (!member.has(obj))
|
1503
1555
|
throw TypeError("Cannot " + msg);
|
1504
1556
|
};
|
1505
|
-
var __privateGet$
|
1557
|
+
var __privateGet$3 = (obj, member, getter) => {
|
1506
1558
|
__accessCheck$3(obj, member, "read from private field");
|
1507
1559
|
return getter ? getter.call(obj) : member.get(obj);
|
1508
1560
|
};
|
@@ -1511,7 +1563,7 @@ var __privateAdd$3 = (obj, member, value) => {
|
|
1511
1563
|
throw TypeError("Cannot add the same private member more than once");
|
1512
1564
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1513
1565
|
};
|
1514
|
-
var __privateSet$
|
1566
|
+
var __privateSet$2 = (obj, member, value, setter) => {
|
1515
1567
|
__accessCheck$3(obj, member, "write to private field");
|
1516
1568
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1517
1569
|
return value;
|
@@ -1520,30 +1572,30 @@ var _map;
|
|
1520
1572
|
class SimpleCache {
|
1521
1573
|
constructor(options = {}) {
|
1522
1574
|
__privateAdd$3(this, _map, void 0);
|
1523
|
-
__privateSet$
|
1575
|
+
__privateSet$2(this, _map, /* @__PURE__ */ new Map());
|
1524
1576
|
this.capacity = options.max ?? 500;
|
1525
1577
|
this.cacheRecords = options.cacheRecords ?? true;
|
1526
1578
|
this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
|
1527
1579
|
}
|
1528
1580
|
async getAll() {
|
1529
|
-
return Object.fromEntries(__privateGet$
|
1581
|
+
return Object.fromEntries(__privateGet$3(this, _map));
|
1530
1582
|
}
|
1531
1583
|
async get(key) {
|
1532
|
-
return __privateGet$
|
1584
|
+
return __privateGet$3(this, _map).get(key) ?? null;
|
1533
1585
|
}
|
1534
1586
|
async set(key, value) {
|
1535
1587
|
await this.delete(key);
|
1536
|
-
__privateGet$
|
1537
|
-
if (__privateGet$
|
1538
|
-
const leastRecentlyUsed = __privateGet$
|
1588
|
+
__privateGet$3(this, _map).set(key, value);
|
1589
|
+
if (__privateGet$3(this, _map).size > this.capacity) {
|
1590
|
+
const leastRecentlyUsed = __privateGet$3(this, _map).keys().next().value;
|
1539
1591
|
await this.delete(leastRecentlyUsed);
|
1540
1592
|
}
|
1541
1593
|
}
|
1542
1594
|
async delete(key) {
|
1543
|
-
__privateGet$
|
1595
|
+
__privateGet$3(this, _map).delete(key);
|
1544
1596
|
}
|
1545
1597
|
async clear() {
|
1546
|
-
return __privateGet$
|
1598
|
+
return __privateGet$3(this, _map).clear();
|
1547
1599
|
}
|
1548
1600
|
}
|
1549
1601
|
_map = new WeakMap();
|
@@ -1571,7 +1623,7 @@ var __accessCheck$2 = (obj, member, msg) => {
|
|
1571
1623
|
if (!member.has(obj))
|
1572
1624
|
throw TypeError("Cannot " + msg);
|
1573
1625
|
};
|
1574
|
-
var __privateGet$
|
1626
|
+
var __privateGet$2 = (obj, member, getter) => {
|
1575
1627
|
__accessCheck$2(obj, member, "read from private field");
|
1576
1628
|
return getter ? getter.call(obj) : member.get(obj);
|
1577
1629
|
};
|
@@ -1582,26 +1634,24 @@ var __privateAdd$2 = (obj, member, value) => {
|
|
1582
1634
|
};
|
1583
1635
|
var _tables;
|
1584
1636
|
class SchemaPlugin extends XataPlugin {
|
1585
|
-
constructor(
|
1637
|
+
constructor(tableNames) {
|
1586
1638
|
super();
|
1587
|
-
this.links = links;
|
1588
1639
|
this.tableNames = tableNames;
|
1589
1640
|
__privateAdd$2(this, _tables, {});
|
1590
1641
|
}
|
1591
1642
|
build(pluginOptions) {
|
1592
|
-
const links = this.links;
|
1593
1643
|
const db = new Proxy({}, {
|
1594
1644
|
get: (_target, table) => {
|
1595
1645
|
if (!isString(table))
|
1596
1646
|
throw new Error("Invalid table name");
|
1597
|
-
if (
|
1598
|
-
__privateGet$
|
1647
|
+
if (__privateGet$2(this, _tables)[table] === void 0) {
|
1648
|
+
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
|
1599
1649
|
}
|
1600
|
-
return __privateGet$
|
1650
|
+
return __privateGet$2(this, _tables)[table];
|
1601
1651
|
}
|
1602
1652
|
});
|
1603
1653
|
for (const table of this.tableNames ?? []) {
|
1604
|
-
db[table] = new RestRepository({ db, pluginOptions, table
|
1654
|
+
db[table] = new RestRepository({ db, pluginOptions, table });
|
1605
1655
|
}
|
1606
1656
|
return db;
|
1607
1657
|
}
|
@@ -1612,44 +1662,57 @@ var __accessCheck$1 = (obj, member, msg) => {
|
|
1612
1662
|
if (!member.has(obj))
|
1613
1663
|
throw TypeError("Cannot " + msg);
|
1614
1664
|
};
|
1665
|
+
var __privateGet$1 = (obj, member, getter) => {
|
1666
|
+
__accessCheck$1(obj, member, "read from private field");
|
1667
|
+
return getter ? getter.call(obj) : member.get(obj);
|
1668
|
+
};
|
1615
1669
|
var __privateAdd$1 = (obj, member, value) => {
|
1616
1670
|
if (member.has(obj))
|
1617
1671
|
throw TypeError("Cannot add the same private member more than once");
|
1618
1672
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1619
1673
|
};
|
1674
|
+
var __privateSet$1 = (obj, member, value, setter) => {
|
1675
|
+
__accessCheck$1(obj, member, "write to private field");
|
1676
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1677
|
+
return value;
|
1678
|
+
};
|
1620
1679
|
var __privateMethod$1 = (obj, member, method) => {
|
1621
1680
|
__accessCheck$1(obj, member, "access private method");
|
1622
1681
|
return method;
|
1623
1682
|
};
|
1624
|
-
var _search, search_fn;
|
1683
|
+
var _schema, _search, search_fn, _getSchema, getSchema_fn;
|
1625
1684
|
class SearchPlugin extends XataPlugin {
|
1626
|
-
constructor(db
|
1685
|
+
constructor(db) {
|
1627
1686
|
super();
|
1628
1687
|
this.db = db;
|
1629
|
-
this.links = links;
|
1630
1688
|
__privateAdd$1(this, _search);
|
1689
|
+
__privateAdd$1(this, _getSchema);
|
1690
|
+
__privateAdd$1(this, _schema, void 0);
|
1631
1691
|
}
|
1632
1692
|
build({ getFetchProps }) {
|
1633
1693
|
return {
|
1634
1694
|
all: async (query, options = {}) => {
|
1635
1695
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1696
|
+
const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
|
1636
1697
|
return records.map((record) => {
|
1637
1698
|
const { table = "orphan" } = record.xata;
|
1638
|
-
return { table, record: initObject(this.db,
|
1699
|
+
return { table, record: initObject(this.db, schema, table, record) };
|
1639
1700
|
});
|
1640
1701
|
},
|
1641
1702
|
byTable: async (query, options = {}) => {
|
1642
1703
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1704
|
+
const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
|
1643
1705
|
return records.reduce((acc, record) => {
|
1644
1706
|
const { table = "orphan" } = record.xata;
|
1645
1707
|
const items = acc[table] ?? [];
|
1646
|
-
const item = initObject(this.db,
|
1708
|
+
const item = initObject(this.db, schema, table, record);
|
1647
1709
|
return { ...acc, [table]: [...items, item] };
|
1648
1710
|
}, {});
|
1649
1711
|
}
|
1650
1712
|
};
|
1651
1713
|
}
|
1652
1714
|
}
|
1715
|
+
_schema = new WeakMap();
|
1653
1716
|
_search = new WeakSet();
|
1654
1717
|
search_fn = async function(query, options, getFetchProps) {
|
1655
1718
|
const fetchProps = await getFetchProps();
|
@@ -1661,6 +1724,18 @@ search_fn = async function(query, options, getFetchProps) {
|
|
1661
1724
|
});
|
1662
1725
|
return records;
|
1663
1726
|
};
|
1727
|
+
_getSchema = new WeakSet();
|
1728
|
+
getSchema_fn = async function(getFetchProps) {
|
1729
|
+
if (__privateGet$1(this, _schema))
|
1730
|
+
return __privateGet$1(this, _schema);
|
1731
|
+
const fetchProps = await getFetchProps();
|
1732
|
+
const { schema } = await getBranchDetails({
|
1733
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1734
|
+
...fetchProps
|
1735
|
+
});
|
1736
|
+
__privateSet$1(this, _schema, schema);
|
1737
|
+
return schema;
|
1738
|
+
};
|
1664
1739
|
|
1665
1740
|
const isBranchStrategyBuilder = (strategy) => {
|
1666
1741
|
return typeof strategy === "function";
|
@@ -1672,30 +1747,39 @@ const envBranchNames = [
|
|
1672
1747
|
"CF_PAGES_BRANCH",
|
1673
1748
|
"BRANCH"
|
1674
1749
|
];
|
1675
|
-
const defaultBranch = "main";
|
1676
1750
|
async function getCurrentBranchName(options) {
|
1677
|
-
const env =
|
1678
|
-
if (env)
|
1679
|
-
|
1680
|
-
|
1681
|
-
|
1682
|
-
|
1683
|
-
|
1684
|
-
|
1685
|
-
|
1686
|
-
return defaultBranch;
|
1751
|
+
const env = getBranchByEnvVariable();
|
1752
|
+
if (env) {
|
1753
|
+
const details = await getDatabaseBranch(env, options);
|
1754
|
+
if (details)
|
1755
|
+
return env;
|
1756
|
+
console.warn(`Branch ${env} not found in Xata. Ignoring...`);
|
1757
|
+
}
|
1758
|
+
const gitBranch = await getGitBranch();
|
1759
|
+
return resolveXataBranch(gitBranch, options);
|
1687
1760
|
}
|
1688
1761
|
async function getCurrentBranchDetails(options) {
|
1689
|
-
const
|
1690
|
-
|
1691
|
-
|
1692
|
-
|
1693
|
-
|
1694
|
-
|
1695
|
-
|
1696
|
-
|
1697
|
-
|
1698
|
-
|
1762
|
+
const branch = await getCurrentBranchName(options);
|
1763
|
+
return getDatabaseBranch(branch, options);
|
1764
|
+
}
|
1765
|
+
async function resolveXataBranch(gitBranch, options) {
|
1766
|
+
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1767
|
+
const apiKey = options?.apiKey || getAPIKey();
|
1768
|
+
if (!databaseURL)
|
1769
|
+
throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
|
1770
|
+
if (!apiKey)
|
1771
|
+
throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
|
1772
|
+
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1773
|
+
const [workspace] = host.split(".");
|
1774
|
+
const { branch } = await resolveBranch({
|
1775
|
+
apiKey,
|
1776
|
+
apiUrl: databaseURL,
|
1777
|
+
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1778
|
+
workspacesApiUrl: `${protocol}//${host}`,
|
1779
|
+
pathParams: { dbName, workspace },
|
1780
|
+
queryParams: { gitBranch, fallbackBranch: getEnvVariable("XATA_FALLBACK_BRANCH") }
|
1781
|
+
});
|
1782
|
+
return branch;
|
1699
1783
|
}
|
1700
1784
|
async function getDatabaseBranch(branch, options) {
|
1701
1785
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
@@ -1769,7 +1853,7 @@ var __privateMethod = (obj, member, method) => {
|
|
1769
1853
|
const buildClient = (plugins) => {
|
1770
1854
|
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1771
1855
|
return _a = class {
|
1772
|
-
constructor(options = {},
|
1856
|
+
constructor(options = {}, tables) {
|
1773
1857
|
__privateAdd(this, _parseOptions);
|
1774
1858
|
__privateAdd(this, _getFetchProps);
|
1775
1859
|
__privateAdd(this, _evaluateBranch);
|
@@ -1779,12 +1863,12 @@ const buildClient = (plugins) => {
|
|
1779
1863
|
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
1780
1864
|
cache: safeOptions.cache
|
1781
1865
|
};
|
1782
|
-
const db = new SchemaPlugin(
|
1783
|
-
const search = new SearchPlugin(db
|
1866
|
+
const db = new SchemaPlugin(tables).build(pluginOptions);
|
1867
|
+
const search = new SearchPlugin(db).build(pluginOptions);
|
1784
1868
|
this.db = db;
|
1785
1869
|
this.search = search;
|
1786
1870
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
1787
|
-
if (
|
1871
|
+
if (namespace === void 0)
|
1788
1872
|
continue;
|
1789
1873
|
const result = namespace.build(pluginOptions);
|
1790
1874
|
if (result instanceof Promise) {
|
@@ -1801,7 +1885,7 @@ const buildClient = (plugins) => {
|
|
1801
1885
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1802
1886
|
const apiKey = options?.apiKey || getAPIKey();
|
1803
1887
|
const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
|
1804
|
-
const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
1888
|
+
const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
1805
1889
|
if (!databaseURL || !apiKey) {
|
1806
1890
|
throw new Error("Options databaseURL and apiKey are required");
|
1807
1891
|
}
|
@@ -1828,7 +1912,7 @@ const buildClient = (plugins) => {
|
|
1828
1912
|
}, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
|
1829
1913
|
if (__privateGet(this, _branch))
|
1830
1914
|
return __privateGet(this, _branch);
|
1831
|
-
if (
|
1915
|
+
if (param === void 0)
|
1832
1916
|
return void 0;
|
1833
1917
|
const strategies = Array.isArray(param) ? [...param] : [param];
|
1834
1918
|
const evaluateBranch = async (strategy) => {
|
@@ -1940,6 +2024,7 @@ exports.removeWorkspaceMember = removeWorkspaceMember;
|
|
1940
2024
|
exports.resendWorkspaceMemberInvite = resendWorkspaceMemberInvite;
|
1941
2025
|
exports.resolveBranch = resolveBranch;
|
1942
2026
|
exports.searchBranch = searchBranch;
|
2027
|
+
exports.searchTable = searchTable;
|
1943
2028
|
exports.setTableSchema = setTableSchema;
|
1944
2029
|
exports.startsWith = startsWith;
|
1945
2030
|
exports.updateBranchMetadata = updateBranchMetadata;
|