@xata.io/client 0.0.0-alpha.vf350c0a → 0.0.0-alpha.vf45a2df
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 +120 -0
- package/README.md +271 -1
- package/Usage.md +395 -0
- package/dist/index.cjs +260 -134
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +397 -127
- package/dist/index.mjs +258 -135
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -3
- package/tsconfig.json +1 -0
package/dist/index.cjs
CHANGED
@@ -11,14 +11,18 @@ function compact(arr) {
|
|
11
11
|
function isObject(value) {
|
12
12
|
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
13
13
|
}
|
14
|
+
function isDefined(value) {
|
15
|
+
return value !== null && value !== void 0;
|
16
|
+
}
|
14
17
|
function isString(value) {
|
15
|
-
return value
|
18
|
+
return isDefined(value) && typeof value === "string";
|
16
19
|
}
|
17
20
|
function toBase64(value) {
|
18
21
|
try {
|
19
22
|
return btoa(value);
|
20
23
|
} catch (err) {
|
21
|
-
|
24
|
+
const buf = Buffer;
|
25
|
+
return buf.from(value).toString("base64");
|
22
26
|
}
|
23
27
|
}
|
24
28
|
|
@@ -74,16 +78,28 @@ function getFetchImplementation(userFetch) {
|
|
74
78
|
return fetchImpl;
|
75
79
|
}
|
76
80
|
|
77
|
-
|
78
|
-
|
81
|
+
const VERSION = "0.0.0-alpha.vf45a2df";
|
82
|
+
|
83
|
+
class ErrorWithCause extends Error {
|
84
|
+
constructor(message, options) {
|
85
|
+
super(message, options);
|
86
|
+
}
|
87
|
+
}
|
88
|
+
class FetcherError extends ErrorWithCause {
|
89
|
+
constructor(status, data, requestId) {
|
79
90
|
super(getMessage(data));
|
80
91
|
this.status = status;
|
81
92
|
this.errors = isBulkError(data) ? data.errors : void 0;
|
93
|
+
this.requestId = requestId;
|
82
94
|
if (data instanceof Error) {
|
83
95
|
this.stack = data.stack;
|
84
96
|
this.cause = data.cause;
|
85
97
|
}
|
86
98
|
}
|
99
|
+
toString() {
|
100
|
+
const error = super.toString();
|
101
|
+
return `[${this.status}] (${this.requestId ?? "Unknown"}): ${error}`;
|
102
|
+
}
|
87
103
|
}
|
88
104
|
function isBulkError(error) {
|
89
105
|
return isObject(error) && Array.isArray(error.errors);
|
@@ -106,7 +122,12 @@ function getMessage(data) {
|
|
106
122
|
}
|
107
123
|
|
108
124
|
const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
|
109
|
-
const
|
125
|
+
const cleanQueryParams = Object.entries(queryParams).reduce((acc, [key, value]) => {
|
126
|
+
if (value === void 0 || value === null)
|
127
|
+
return acc;
|
128
|
+
return { ...acc, [key]: value };
|
129
|
+
}, {});
|
130
|
+
const query = new URLSearchParams(cleanQueryParams).toString();
|
110
131
|
const queryString = query.length > 0 ? `?${query}` : "";
|
111
132
|
return url.replace(/\{\w*\}/g, (key) => pathParams[key.slice(1, -1)]) + queryString;
|
112
133
|
};
|
@@ -146,6 +167,7 @@ async function fetch$1({
|
|
146
167
|
body: body ? JSON.stringify(body) : void 0,
|
147
168
|
headers: {
|
148
169
|
"Content-Type": "application/json",
|
170
|
+
"User-Agent": `Xata client-ts/${VERSION}`,
|
149
171
|
...headers,
|
150
172
|
...hostHeader(fullUrl),
|
151
173
|
Authorization: `Bearer ${apiKey}`
|
@@ -154,14 +176,15 @@ async function fetch$1({
|
|
154
176
|
if (response.status === 204) {
|
155
177
|
return {};
|
156
178
|
}
|
179
|
+
const requestId = response.headers?.get("x-request-id") ?? void 0;
|
157
180
|
try {
|
158
181
|
const jsonResponse = await response.json();
|
159
182
|
if (response.ok) {
|
160
183
|
return jsonResponse;
|
161
184
|
}
|
162
|
-
throw new FetcherError(response.status, jsonResponse);
|
185
|
+
throw new FetcherError(response.status, jsonResponse, requestId);
|
163
186
|
} catch (error) {
|
164
|
-
throw new FetcherError(response.status, error);
|
187
|
+
throw new FetcherError(response.status, error, requestId);
|
165
188
|
}
|
166
189
|
}
|
167
190
|
|
@@ -370,6 +393,11 @@ const queryTable = (variables) => fetch$1({
|
|
370
393
|
method: "post",
|
371
394
|
...variables
|
372
395
|
});
|
396
|
+
const searchTable = (variables) => fetch$1({
|
397
|
+
url: "/db/{dbBranchName}/tables/{tableName}/search",
|
398
|
+
method: "post",
|
399
|
+
...variables
|
400
|
+
});
|
373
401
|
const searchBranch = (variables) => fetch$1({
|
374
402
|
url: "/db/{dbBranchName}/search",
|
375
403
|
method: "post",
|
@@ -433,6 +461,7 @@ const operationsByTag = {
|
|
433
461
|
getRecord,
|
434
462
|
bulkInsertTableRecords,
|
435
463
|
queryTable,
|
464
|
+
searchTable,
|
436
465
|
searchBranch
|
437
466
|
}
|
438
467
|
};
|
@@ -679,10 +708,10 @@ class DatabaseApi {
|
|
679
708
|
...this.extraProps
|
680
709
|
});
|
681
710
|
}
|
682
|
-
resolveBranch(workspace, dbName, gitBranch) {
|
711
|
+
resolveBranch(workspace, dbName, gitBranch, fallbackBranch) {
|
683
712
|
return operationsByTag.database.resolveBranch({
|
684
713
|
pathParams: { workspace, dbName },
|
685
|
-
queryParams: { gitBranch },
|
714
|
+
queryParams: { gitBranch, fallbackBranch },
|
686
715
|
...this.extraProps
|
687
716
|
});
|
688
717
|
}
|
@@ -703,10 +732,10 @@ class BranchApi {
|
|
703
732
|
...this.extraProps
|
704
733
|
});
|
705
734
|
}
|
706
|
-
createBranch(workspace, database, branch, from
|
735
|
+
createBranch(workspace, database, branch, from, options = {}) {
|
707
736
|
return operationsByTag.branch.createBranch({
|
708
737
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
709
|
-
queryParams: { from },
|
738
|
+
queryParams: isString(from) ? { from } : void 0,
|
710
739
|
body: options,
|
711
740
|
...this.extraProps
|
712
741
|
});
|
@@ -888,6 +917,13 @@ class RecordsApi {
|
|
888
917
|
...this.extraProps
|
889
918
|
});
|
890
919
|
}
|
920
|
+
searchTable(workspace, database, branch, tableName, query) {
|
921
|
+
return operationsByTag.records.searchTable({
|
922
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
923
|
+
body: query,
|
924
|
+
...this.extraProps
|
925
|
+
});
|
926
|
+
}
|
891
927
|
searchBranch(workspace, database, branch, query) {
|
892
928
|
return operationsByTag.records.searchBranch({
|
893
929
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
@@ -925,25 +961,25 @@ var __privateSet$5 = (obj, member, value, setter) => {
|
|
925
961
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
926
962
|
return value;
|
927
963
|
};
|
928
|
-
var _query;
|
964
|
+
var _query, _page;
|
929
965
|
class Page {
|
930
966
|
constructor(query, meta, records = []) {
|
931
967
|
__privateAdd$6(this, _query, void 0);
|
932
968
|
__privateSet$5(this, _query, query);
|
933
969
|
this.meta = meta;
|
934
|
-
this.records = records;
|
970
|
+
this.records = new RecordArray(this, records);
|
935
971
|
}
|
936
972
|
async nextPage(size, offset) {
|
937
|
-
return __privateGet$6(this, _query).getPaginated({
|
973
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
|
938
974
|
}
|
939
975
|
async previousPage(size, offset) {
|
940
|
-
return __privateGet$6(this, _query).getPaginated({
|
976
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, before: this.meta.page.cursor } });
|
941
977
|
}
|
942
978
|
async firstPage(size, offset) {
|
943
|
-
return __privateGet$6(this, _query).getPaginated({
|
979
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, first: this.meta.page.cursor } });
|
944
980
|
}
|
945
981
|
async lastPage(size, offset) {
|
946
|
-
return __privateGet$6(this, _query).getPaginated({
|
982
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, last: this.meta.page.cursor } });
|
947
983
|
}
|
948
984
|
hasNextPage() {
|
949
985
|
return this.meta.page.more;
|
@@ -951,9 +987,50 @@ class Page {
|
|
951
987
|
}
|
952
988
|
_query = new WeakMap();
|
953
989
|
const PAGINATION_MAX_SIZE = 200;
|
954
|
-
const PAGINATION_DEFAULT_SIZE =
|
990
|
+
const PAGINATION_DEFAULT_SIZE = 20;
|
955
991
|
const PAGINATION_MAX_OFFSET = 800;
|
956
992
|
const PAGINATION_DEFAULT_OFFSET = 0;
|
993
|
+
function isCursorPaginationOptions(options) {
|
994
|
+
return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
|
995
|
+
}
|
996
|
+
const _RecordArray = class extends Array {
|
997
|
+
constructor(page, overrideRecords) {
|
998
|
+
super(..._RecordArray.parseConstructorParams(page, overrideRecords));
|
999
|
+
__privateAdd$6(this, _page, void 0);
|
1000
|
+
__privateSet$5(this, _page, page);
|
1001
|
+
}
|
1002
|
+
static parseConstructorParams(...args) {
|
1003
|
+
if (args.length === 1 && typeof args[0] === "number") {
|
1004
|
+
return new Array(args[0]);
|
1005
|
+
}
|
1006
|
+
if (args.length <= 2 && isObject(args[0]?.meta) && Array.isArray(args[1] ?? [])) {
|
1007
|
+
const result = args[1] ?? args[0].records ?? [];
|
1008
|
+
return new Array(...result);
|
1009
|
+
}
|
1010
|
+
return new Array(...args);
|
1011
|
+
}
|
1012
|
+
async nextPage(size, offset) {
|
1013
|
+
const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
|
1014
|
+
return new _RecordArray(newPage);
|
1015
|
+
}
|
1016
|
+
async previousPage(size, offset) {
|
1017
|
+
const newPage = await __privateGet$6(this, _page).previousPage(size, offset);
|
1018
|
+
return new _RecordArray(newPage);
|
1019
|
+
}
|
1020
|
+
async firstPage(size, offset) {
|
1021
|
+
const newPage = await __privateGet$6(this, _page).firstPage(size, offset);
|
1022
|
+
return new _RecordArray(newPage);
|
1023
|
+
}
|
1024
|
+
async lastPage(size, offset) {
|
1025
|
+
const newPage = await __privateGet$6(this, _page).lastPage(size, offset);
|
1026
|
+
return new _RecordArray(newPage);
|
1027
|
+
}
|
1028
|
+
hasNextPage() {
|
1029
|
+
return __privateGet$6(this, _page).meta.page.more;
|
1030
|
+
}
|
1031
|
+
};
|
1032
|
+
let RecordArray = _RecordArray;
|
1033
|
+
_page = new WeakMap();
|
957
1034
|
|
958
1035
|
var __accessCheck$5 = (obj, member, msg) => {
|
959
1036
|
if (!member.has(obj))
|
@@ -975,18 +1052,19 @@ var __privateSet$4 = (obj, member, value, setter) => {
|
|
975
1052
|
};
|
976
1053
|
var _table$1, _repository, _data;
|
977
1054
|
const _Query = class {
|
978
|
-
constructor(repository, table, data,
|
1055
|
+
constructor(repository, table, data, rawParent) {
|
979
1056
|
__privateAdd$5(this, _table$1, void 0);
|
980
1057
|
__privateAdd$5(this, _repository, void 0);
|
981
1058
|
__privateAdd$5(this, _data, { filter: {} });
|
982
1059
|
this.meta = { page: { cursor: "start", more: true } };
|
983
|
-
this.records = [];
|
1060
|
+
this.records = new RecordArray(this, []);
|
984
1061
|
__privateSet$4(this, _table$1, table);
|
985
1062
|
if (repository) {
|
986
1063
|
__privateSet$4(this, _repository, repository);
|
987
1064
|
} else {
|
988
1065
|
__privateSet$4(this, _repository, this);
|
989
1066
|
}
|
1067
|
+
const parent = cleanParent(data, rawParent);
|
990
1068
|
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
991
1069
|
__privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
|
992
1070
|
__privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
|
@@ -994,7 +1072,7 @@ const _Query = class {
|
|
994
1072
|
__privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
|
995
1073
|
__privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
|
996
1074
|
__privateGet$5(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
|
997
|
-
__privateGet$5(this, _data).
|
1075
|
+
__privateGet$5(this, _data).pagination = data.pagination ?? parent?.pagination;
|
998
1076
|
__privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
|
999
1077
|
this.any = this.any.bind(this);
|
1000
1078
|
this.all = this.all.bind(this);
|
@@ -1009,8 +1087,8 @@ const _Query = class {
|
|
1009
1087
|
return __privateGet$5(this, _data);
|
1010
1088
|
}
|
1011
1089
|
key() {
|
1012
|
-
const { columns = [], filter = {}, sort = [],
|
1013
|
-
const key = JSON.stringify({ columns, filter, sort,
|
1090
|
+
const { columns = [], filter = {}, sort = [], pagination = {} } = __privateGet$5(this, _data);
|
1091
|
+
const key = JSON.stringify({ columns, filter, sort, pagination });
|
1014
1092
|
return toBase64(key);
|
1015
1093
|
}
|
1016
1094
|
any(...queries) {
|
@@ -1052,34 +1130,39 @@ const _Query = class {
|
|
1052
1130
|
return __privateGet$5(this, _repository).query(query);
|
1053
1131
|
}
|
1054
1132
|
async *[Symbol.asyncIterator]() {
|
1055
|
-
for await (const [record] of this.getIterator(1)) {
|
1133
|
+
for await (const [record] of this.getIterator({ batchSize: 1 })) {
|
1056
1134
|
yield record;
|
1057
1135
|
}
|
1058
1136
|
}
|
1059
|
-
async *getIterator(
|
1060
|
-
|
1061
|
-
let
|
1062
|
-
|
1063
|
-
|
1064
|
-
|
1065
|
-
|
1066
|
-
|
1137
|
+
async *getIterator(options = {}) {
|
1138
|
+
const { batchSize = 1 } = options;
|
1139
|
+
let page = await this.getPaginated({ ...options, pagination: { size: batchSize, offset: 0 } });
|
1140
|
+
let more = page.hasNextPage();
|
1141
|
+
yield page.records;
|
1142
|
+
while (more) {
|
1143
|
+
page = await page.nextPage();
|
1144
|
+
more = page.hasNextPage();
|
1145
|
+
yield page.records;
|
1067
1146
|
}
|
1068
1147
|
}
|
1069
1148
|
async getMany(options = {}) {
|
1070
|
-
const
|
1071
|
-
|
1149
|
+
const page = await this.getPaginated(options);
|
1150
|
+
if (page.hasNextPage() && options.pagination?.size === void 0) {
|
1151
|
+
console.trace("Calling getMany does not return all results. Paginate to get all results or call getAll.");
|
1152
|
+
}
|
1153
|
+
return page.records;
|
1072
1154
|
}
|
1073
|
-
async getAll(
|
1155
|
+
async getAll(options = {}) {
|
1156
|
+
const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
|
1074
1157
|
const results = [];
|
1075
|
-
for await (const page of this.getIterator(
|
1158
|
+
for await (const page of this.getIterator({ ...rest, batchSize })) {
|
1076
1159
|
results.push(...page);
|
1077
1160
|
}
|
1078
1161
|
return results;
|
1079
1162
|
}
|
1080
1163
|
async getFirst(options = {}) {
|
1081
|
-
const records = await this.getMany({ ...options,
|
1082
|
-
return records[0]
|
1164
|
+
const records = await this.getMany({ ...options, pagination: { size: 1 } });
|
1165
|
+
return records[0] ?? null;
|
1083
1166
|
}
|
1084
1167
|
cache(ttl) {
|
1085
1168
|
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
|
@@ -1091,10 +1174,10 @@ const _Query = class {
|
|
1091
1174
|
return this.firstPage(size, offset);
|
1092
1175
|
}
|
1093
1176
|
firstPage(size, offset) {
|
1094
|
-
return this.getPaginated({
|
1177
|
+
return this.getPaginated({ pagination: { size, offset } });
|
1095
1178
|
}
|
1096
1179
|
lastPage(size, offset) {
|
1097
|
-
return this.getPaginated({
|
1180
|
+
return this.getPaginated({ pagination: { size, offset, before: "end" } });
|
1098
1181
|
}
|
1099
1182
|
hasNextPage() {
|
1100
1183
|
return this.meta.page.more;
|
@@ -1104,12 +1187,20 @@ let Query = _Query;
|
|
1104
1187
|
_table$1 = new WeakMap();
|
1105
1188
|
_repository = new WeakMap();
|
1106
1189
|
_data = new WeakMap();
|
1190
|
+
function cleanParent(data, parent) {
|
1191
|
+
if (isCursorPaginationOptions(data.pagination)) {
|
1192
|
+
return { ...parent, sorting: void 0, filter: void 0 };
|
1193
|
+
}
|
1194
|
+
return parent;
|
1195
|
+
}
|
1107
1196
|
|
1108
1197
|
function isIdentifiable(x) {
|
1109
1198
|
return isObject(x) && isString(x?.id);
|
1110
1199
|
}
|
1111
1200
|
function isXataRecord(x) {
|
1112
|
-
|
1201
|
+
const record = x;
|
1202
|
+
const metadata = record?.getMetadata();
|
1203
|
+
return isIdentifiable(x) && isObject(metadata) && typeof metadata.version === "number";
|
1113
1204
|
}
|
1114
1205
|
|
1115
1206
|
function isSortFilterString(value) {
|
@@ -1157,7 +1248,7 @@ var __privateMethod$2 = (obj, member, method) => {
|
|
1157
1248
|
__accessCheck$4(obj, member, "access private method");
|
1158
1249
|
return method;
|
1159
1250
|
};
|
1160
|
-
var _table, _getFetchProps, _cache,
|
1251
|
+
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
1252
|
class Repository extends Query {
|
1162
1253
|
}
|
1163
1254
|
class RestRepository extends Query {
|
@@ -1174,11 +1265,11 @@ class RestRepository extends Query {
|
|
1174
1265
|
__privateAdd$4(this, _getCacheRecord);
|
1175
1266
|
__privateAdd$4(this, _setCacheQuery);
|
1176
1267
|
__privateAdd$4(this, _getCacheQuery);
|
1177
|
-
__privateAdd$4(this,
|
1268
|
+
__privateAdd$4(this, _getSchema$1);
|
1178
1269
|
__privateAdd$4(this, _table, void 0);
|
1179
1270
|
__privateAdd$4(this, _getFetchProps, void 0);
|
1180
1271
|
__privateAdd$4(this, _cache, void 0);
|
1181
|
-
__privateAdd$4(this,
|
1272
|
+
__privateAdd$4(this, _schema$1, void 0);
|
1182
1273
|
__privateSet$3(this, _table, options.table);
|
1183
1274
|
__privateSet$3(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
1184
1275
|
this.db = options.db;
|
@@ -1186,6 +1277,8 @@ class RestRepository extends Query {
|
|
1186
1277
|
}
|
1187
1278
|
async create(a, b) {
|
1188
1279
|
if (Array.isArray(a)) {
|
1280
|
+
if (a.length === 0)
|
1281
|
+
return [];
|
1189
1282
|
const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
|
1190
1283
|
await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
|
1191
1284
|
return records;
|
@@ -1211,27 +1304,38 @@ class RestRepository extends Query {
|
|
1211
1304
|
}
|
1212
1305
|
throw new Error("Invalid arguments for create method");
|
1213
1306
|
}
|
1214
|
-
async read(
|
1215
|
-
|
1216
|
-
|
1217
|
-
|
1218
|
-
|
1219
|
-
|
1220
|
-
|
1221
|
-
|
1222
|
-
|
1223
|
-
|
1224
|
-
|
1225
|
-
|
1226
|
-
|
1227
|
-
|
1228
|
-
|
1307
|
+
async read(a) {
|
1308
|
+
if (Array.isArray(a)) {
|
1309
|
+
if (a.length === 0)
|
1310
|
+
return [];
|
1311
|
+
const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
|
1312
|
+
return this.getAll({ filter: { id: { $any: ids } } });
|
1313
|
+
}
|
1314
|
+
const id = isString(a) ? a : a.id;
|
1315
|
+
if (isString(id)) {
|
1316
|
+
const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, id);
|
1317
|
+
if (cacheRecord)
|
1318
|
+
return cacheRecord;
|
1319
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1320
|
+
try {
|
1321
|
+
const response = await getRecord({
|
1322
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
|
1323
|
+
...fetchProps
|
1324
|
+
});
|
1325
|
+
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1326
|
+
return initObject(this.db, schema, __privateGet$4(this, _table), response);
|
1327
|
+
} catch (e) {
|
1328
|
+
if (isObject(e) && e.status === 404) {
|
1329
|
+
return null;
|
1330
|
+
}
|
1331
|
+
throw e;
|
1229
1332
|
}
|
1230
|
-
throw e;
|
1231
1333
|
}
|
1232
1334
|
}
|
1233
1335
|
async update(a, b) {
|
1234
1336
|
if (Array.isArray(a)) {
|
1337
|
+
if (a.length === 0)
|
1338
|
+
return [];
|
1235
1339
|
if (a.length > 100) {
|
1236
1340
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1237
1341
|
}
|
@@ -1253,6 +1357,8 @@ class RestRepository extends Query {
|
|
1253
1357
|
}
|
1254
1358
|
async createOrUpdate(a, b) {
|
1255
1359
|
if (Array.isArray(a)) {
|
1360
|
+
if (a.length === 0)
|
1361
|
+
return [];
|
1256
1362
|
if (a.length > 100) {
|
1257
1363
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1258
1364
|
}
|
@@ -1274,6 +1380,8 @@ class RestRepository extends Query {
|
|
1274
1380
|
}
|
1275
1381
|
async delete(a) {
|
1276
1382
|
if (Array.isArray(a)) {
|
1383
|
+
if (a.length === 0)
|
1384
|
+
return;
|
1277
1385
|
if (a.length > 100) {
|
1278
1386
|
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
1279
1387
|
}
|
@@ -1294,13 +1402,18 @@ class RestRepository extends Query {
|
|
1294
1402
|
}
|
1295
1403
|
async search(query, options = {}) {
|
1296
1404
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1297
|
-
const { records } = await
|
1298
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1299
|
-
body: {
|
1405
|
+
const { records } = await searchTable({
|
1406
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1407
|
+
body: {
|
1408
|
+
query,
|
1409
|
+
fuzziness: options.fuzziness,
|
1410
|
+
highlight: options.highlight,
|
1411
|
+
filter: options.filter
|
1412
|
+
},
|
1300
1413
|
...fetchProps
|
1301
1414
|
});
|
1302
|
-
const
|
1303
|
-
return records.map((item) => initObject(this.db,
|
1415
|
+
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1416
|
+
return records.map((item) => initObject(this.db, schema, __privateGet$4(this, _table), item));
|
1304
1417
|
}
|
1305
1418
|
async query(query) {
|
1306
1419
|
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
@@ -1309,8 +1422,8 @@ class RestRepository extends Query {
|
|
1309
1422
|
const data = query.getQueryOptions();
|
1310
1423
|
const body = {
|
1311
1424
|
filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
|
1312
|
-
sort: data.sort ? buildSortFilter(data.sort) : void 0,
|
1313
|
-
page: data.
|
1425
|
+
sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
|
1426
|
+
page: data.pagination,
|
1314
1427
|
columns: data.columns
|
1315
1428
|
};
|
1316
1429
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
@@ -1319,8 +1432,8 @@ class RestRepository extends Query {
|
|
1319
1432
|
body,
|
1320
1433
|
...fetchProps
|
1321
1434
|
});
|
1322
|
-
const
|
1323
|
-
const records = objects.map((record) => initObject(this.db,
|
1435
|
+
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1436
|
+
const records = objects.map((record) => initObject(this.db, schema, __privateGet$4(this, _table), record));
|
1324
1437
|
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1325
1438
|
return new Page(query, meta, records);
|
1326
1439
|
}
|
@@ -1328,7 +1441,7 @@ class RestRepository extends Query {
|
|
1328
1441
|
_table = new WeakMap();
|
1329
1442
|
_getFetchProps = new WeakMap();
|
1330
1443
|
_cache = new WeakMap();
|
1331
|
-
|
1444
|
+
_schema$1 = new WeakMap();
|
1332
1445
|
_insertRecordWithoutId = new WeakSet();
|
1333
1446
|
insertRecordWithoutId_fn = async function(object) {
|
1334
1447
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
@@ -1373,16 +1486,20 @@ _bulkInsertTableRecords = new WeakSet();
|
|
1373
1486
|
bulkInsertTableRecords_fn = async function(objects) {
|
1374
1487
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1375
1488
|
const records = objects.map((object) => transformObjectLinks(object));
|
1376
|
-
const
|
1489
|
+
const { recordIDs } = await bulkInsertTableRecords({
|
1377
1490
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1378
1491
|
body: { records },
|
1379
1492
|
...fetchProps
|
1380
1493
|
});
|
1381
|
-
const finalObjects = await this.
|
1494
|
+
const finalObjects = await this.read(recordIDs);
|
1382
1495
|
if (finalObjects.length !== objects.length) {
|
1383
1496
|
throw new Error("The server failed to save some records");
|
1384
1497
|
}
|
1385
|
-
|
1498
|
+
const dictionary = finalObjects.reduce((acc, object) => {
|
1499
|
+
acc[object.id] = object;
|
1500
|
+
return acc;
|
1501
|
+
}, {});
|
1502
|
+
return recordIDs.map((id) => dictionary[id]);
|
1386
1503
|
};
|
1387
1504
|
_updateRecordWithID = new WeakSet();
|
1388
1505
|
updateRecordWithID_fn = async function(recordId, object) {
|
@@ -1453,21 +1570,22 @@ getCacheQuery_fn = async function(query) {
|
|
1453
1570
|
if (!result)
|
1454
1571
|
return null;
|
1455
1572
|
const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
|
1456
|
-
if (
|
1457
|
-
return
|
1573
|
+
if (ttl < 0)
|
1574
|
+
return null;
|
1458
1575
|
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1459
1576
|
return hasExpired ? null : result;
|
1460
1577
|
};
|
1461
|
-
|
1462
|
-
|
1463
|
-
if (__privateGet$4(this,
|
1464
|
-
return __privateGet$4(this,
|
1465
|
-
const
|
1466
|
-
|
1467
|
-
|
1578
|
+
_getSchema$1 = new WeakSet();
|
1579
|
+
getSchema_fn$1 = async function() {
|
1580
|
+
if (__privateGet$4(this, _schema$1))
|
1581
|
+
return __privateGet$4(this, _schema$1);
|
1582
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1583
|
+
const { schema } = await getBranchDetails({
|
1584
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1585
|
+
...fetchProps
|
1468
1586
|
});
|
1469
|
-
__privateSet$3(this,
|
1470
|
-
return
|
1587
|
+
__privateSet$3(this, _schema$1, schema);
|
1588
|
+
return schema;
|
1471
1589
|
};
|
1472
1590
|
const transformObjectLinks = (object) => {
|
1473
1591
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
@@ -1476,17 +1594,21 @@ const transformObjectLinks = (object) => {
|
|
1476
1594
|
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1477
1595
|
}, {});
|
1478
1596
|
};
|
1479
|
-
const initObject = (db,
|
1597
|
+
const initObject = (db, schema, table, object) => {
|
1480
1598
|
const result = {};
|
1481
|
-
|
1482
|
-
|
1599
|
+
const { xata, ...rest } = object ?? {};
|
1600
|
+
Object.assign(result, rest);
|
1601
|
+
const { columns } = schema.tables.find(({ name }) => name === table) ?? {};
|
1602
|
+
if (!columns)
|
1603
|
+
console.error(`Table ${table} not found in schema`);
|
1604
|
+
for (const column of columns ?? []) {
|
1483
1605
|
const value = result[column.name];
|
1484
1606
|
switch (column.type) {
|
1485
1607
|
case "datetime": {
|
1486
|
-
const date = new Date(value);
|
1487
|
-
if (isNaN(date.getTime())) {
|
1608
|
+
const date = value !== void 0 ? new Date(value) : void 0;
|
1609
|
+
if (date && isNaN(date.getTime())) {
|
1488
1610
|
console.error(`Failed to parse date ${value} for field ${column.name}`);
|
1489
|
-
} else {
|
1611
|
+
} else if (date) {
|
1490
1612
|
result[column.name] = date;
|
1491
1613
|
}
|
1492
1614
|
break;
|
@@ -1495,8 +1617,8 @@ const initObject = (db, columns, table, object) => {
|
|
1495
1617
|
const linkTable = column.link?.table;
|
1496
1618
|
if (!linkTable) {
|
1497
1619
|
console.error(`Failed to parse link for field ${column.name}`);
|
1498
|
-
} else if (
|
1499
|
-
result[column.name] = initObject(db,
|
1620
|
+
} else if (isObject(value)) {
|
1621
|
+
result[column.name] = initObject(db, schema, linkTable, value);
|
1500
1622
|
}
|
1501
1623
|
break;
|
1502
1624
|
}
|
@@ -1511,7 +1633,10 @@ const initObject = (db, columns, table, object) => {
|
|
1511
1633
|
result.delete = function() {
|
1512
1634
|
return db[table].delete(result["id"]);
|
1513
1635
|
};
|
1514
|
-
|
1636
|
+
result.getMetadata = function() {
|
1637
|
+
return xata;
|
1638
|
+
};
|
1639
|
+
for (const prop of ["read", "update", "delete", "getMetadata"]) {
|
1515
1640
|
Object.defineProperty(result, prop, { enumerable: false });
|
1516
1641
|
}
|
1517
1642
|
Object.freeze(result);
|
@@ -1621,7 +1746,7 @@ class SchemaPlugin extends XataPlugin {
|
|
1621
1746
|
get: (_target, table) => {
|
1622
1747
|
if (!isString(table))
|
1623
1748
|
throw new Error("Invalid table name");
|
1624
|
-
if (
|
1749
|
+
if (__privateGet$2(this, _tables)[table] === void 0) {
|
1625
1750
|
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
|
1626
1751
|
}
|
1627
1752
|
return __privateGet$2(this, _tables)[table];
|
@@ -1673,11 +1798,7 @@ class SearchPlugin extends XataPlugin {
|
|
1673
1798
|
const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
|
1674
1799
|
return records.map((record) => {
|
1675
1800
|
const { table = "orphan" } = record.xata;
|
1676
|
-
|
1677
|
-
if (!columns) {
|
1678
|
-
console.error(`No schema columns found for table ${table}`);
|
1679
|
-
}
|
1680
|
-
return { table, record: initObject(this.db, columns ?? [], table, record) };
|
1801
|
+
return { table, record: initObject(this.db, schema, table, record) };
|
1681
1802
|
});
|
1682
1803
|
},
|
1683
1804
|
byTable: async (query, options = {}) => {
|
@@ -1685,12 +1806,8 @@ class SearchPlugin extends XataPlugin {
|
|
1685
1806
|
const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
|
1686
1807
|
return records.reduce((acc, record) => {
|
1687
1808
|
const { table = "orphan" } = record.xata;
|
1688
|
-
const columns = schema.tables.find((t) => t.name === table)?.columns;
|
1689
|
-
if (!columns) {
|
1690
|
-
console.error(`No schema columns found for table ${table}`);
|
1691
|
-
}
|
1692
1809
|
const items = acc[table] ?? [];
|
1693
|
-
const item = initObject(this.db,
|
1810
|
+
const item = initObject(this.db, schema, table, record);
|
1694
1811
|
return { ...acc, [table]: [...items, item] };
|
1695
1812
|
}, {});
|
1696
1813
|
}
|
@@ -1701,10 +1818,10 @@ _schema = new WeakMap();
|
|
1701
1818
|
_search = new WeakSet();
|
1702
1819
|
search_fn = async function(query, options, getFetchProps) {
|
1703
1820
|
const fetchProps = await getFetchProps();
|
1704
|
-
const { tables, fuzziness } = options ?? {};
|
1821
|
+
const { tables, fuzziness, highlight } = options ?? {};
|
1705
1822
|
const { records } = await searchBranch({
|
1706
1823
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1707
|
-
body: { tables, query, fuzziness },
|
1824
|
+
body: { tables, query, fuzziness, highlight },
|
1708
1825
|
...fetchProps
|
1709
1826
|
});
|
1710
1827
|
return records;
|
@@ -1732,30 +1849,39 @@ const envBranchNames = [
|
|
1732
1849
|
"CF_PAGES_BRANCH",
|
1733
1850
|
"BRANCH"
|
1734
1851
|
];
|
1735
|
-
const defaultBranch = "main";
|
1736
1852
|
async function getCurrentBranchName(options) {
|
1737
|
-
const env =
|
1738
|
-
if (env)
|
1739
|
-
|
1740
|
-
|
1741
|
-
|
1742
|
-
|
1743
|
-
|
1744
|
-
|
1745
|
-
|
1746
|
-
return defaultBranch;
|
1853
|
+
const env = getBranchByEnvVariable();
|
1854
|
+
if (env) {
|
1855
|
+
const details = await getDatabaseBranch(env, options);
|
1856
|
+
if (details)
|
1857
|
+
return env;
|
1858
|
+
console.warn(`Branch ${env} not found in Xata. Ignoring...`);
|
1859
|
+
}
|
1860
|
+
const gitBranch = await getGitBranch();
|
1861
|
+
return resolveXataBranch(gitBranch, options);
|
1747
1862
|
}
|
1748
1863
|
async function getCurrentBranchDetails(options) {
|
1749
|
-
const
|
1750
|
-
|
1751
|
-
|
1752
|
-
|
1753
|
-
|
1754
|
-
|
1755
|
-
|
1756
|
-
|
1757
|
-
|
1758
|
-
|
1864
|
+
const branch = await getCurrentBranchName(options);
|
1865
|
+
return getDatabaseBranch(branch, options);
|
1866
|
+
}
|
1867
|
+
async function resolveXataBranch(gitBranch, options) {
|
1868
|
+
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1869
|
+
const apiKey = options?.apiKey || getAPIKey();
|
1870
|
+
if (!databaseURL)
|
1871
|
+
throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
|
1872
|
+
if (!apiKey)
|
1873
|
+
throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
|
1874
|
+
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1875
|
+
const [workspace] = host.split(".");
|
1876
|
+
const { branch } = await resolveBranch({
|
1877
|
+
apiKey,
|
1878
|
+
apiUrl: databaseURL,
|
1879
|
+
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1880
|
+
workspacesApiUrl: `${protocol}//${host}`,
|
1881
|
+
pathParams: { dbName, workspace },
|
1882
|
+
queryParams: { gitBranch, fallbackBranch: getEnvVariable("XATA_FALLBACK_BRANCH") }
|
1883
|
+
});
|
1884
|
+
return branch;
|
1759
1885
|
}
|
1760
1886
|
async function getDatabaseBranch(branch, options) {
|
1761
1887
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
@@ -1773,10 +1899,7 @@ async function getDatabaseBranch(branch, options) {
|
|
1773
1899
|
apiUrl: databaseURL,
|
1774
1900
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1775
1901
|
workspacesApiUrl: `${protocol}//${host}`,
|
1776
|
-
pathParams: {
|
1777
|
-
dbBranchName,
|
1778
|
-
workspace
|
1779
|
-
}
|
1902
|
+
pathParams: { dbBranchName, workspace }
|
1780
1903
|
});
|
1781
1904
|
} catch (err) {
|
1782
1905
|
if (isObject(err) && err.status === 404)
|
@@ -1844,7 +1967,7 @@ const buildClient = (plugins) => {
|
|
1844
1967
|
this.db = db;
|
1845
1968
|
this.search = search;
|
1846
1969
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
1847
|
-
if (
|
1970
|
+
if (namespace === void 0)
|
1848
1971
|
continue;
|
1849
1972
|
const result = namespace.build(pluginOptions);
|
1850
1973
|
if (result instanceof Promise) {
|
@@ -1861,7 +1984,7 @@ const buildClient = (plugins) => {
|
|
1861
1984
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1862
1985
|
const apiKey = options?.apiKey || getAPIKey();
|
1863
1986
|
const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
|
1864
|
-
const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
1987
|
+
const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
1865
1988
|
if (!databaseURL || !apiKey) {
|
1866
1989
|
throw new Error("Options databaseURL and apiKey are required");
|
1867
1990
|
}
|
@@ -1888,7 +2011,7 @@ const buildClient = (plugins) => {
|
|
1888
2011
|
}, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
|
1889
2012
|
if (__privateGet(this, _branch))
|
1890
2013
|
return __privateGet(this, _branch);
|
1891
|
-
if (
|
2014
|
+
if (param === void 0)
|
1892
2015
|
return void 0;
|
1893
2016
|
const strategies = Array.isArray(param) ? [...param] : [param];
|
1894
2017
|
const evaluateBranch = async (strategy) => {
|
@@ -1921,6 +2044,7 @@ exports.PAGINATION_MAX_OFFSET = PAGINATION_MAX_OFFSET;
|
|
1921
2044
|
exports.PAGINATION_MAX_SIZE = PAGINATION_MAX_SIZE;
|
1922
2045
|
exports.Page = Page;
|
1923
2046
|
exports.Query = Query;
|
2047
|
+
exports.RecordArray = RecordArray;
|
1924
2048
|
exports.Repository = Repository;
|
1925
2049
|
exports.RestRepository = RestRepository;
|
1926
2050
|
exports.SchemaPlugin = SchemaPlugin;
|
@@ -1985,6 +2109,7 @@ exports.insertRecord = insertRecord;
|
|
1985
2109
|
exports.insertRecordWithID = insertRecordWithID;
|
1986
2110
|
exports.inviteWorkspaceMember = inviteWorkspaceMember;
|
1987
2111
|
exports.is = is;
|
2112
|
+
exports.isCursorPaginationOptions = isCursorPaginationOptions;
|
1988
2113
|
exports.isIdentifiable = isIdentifiable;
|
1989
2114
|
exports.isNot = isNot;
|
1990
2115
|
exports.isXataRecord = isXataRecord;
|
@@ -2000,6 +2125,7 @@ exports.removeWorkspaceMember = removeWorkspaceMember;
|
|
2000
2125
|
exports.resendWorkspaceMemberInvite = resendWorkspaceMemberInvite;
|
2001
2126
|
exports.resolveBranch = resolveBranch;
|
2002
2127
|
exports.searchBranch = searchBranch;
|
2128
|
+
exports.searchTable = searchTable;
|
2003
2129
|
exports.setTableSchema = setTableSchema;
|
2004
2130
|
exports.startsWith = startsWith;
|
2005
2131
|
exports.updateBranchMetadata = updateBranchMetadata;
|