@xata.io/client 0.0.0-alpha.vf9d4e41 → 0.0.0-alpha.vfaf51aa
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 +82 -0
- package/README.md +265 -1
- package/Usage.md +380 -0
- package/dist/index.cjs +212 -78
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +299 -51
- package/dist/index.mjs +210 -79
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -3
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.vfaf51aa";
|
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
|
}
|
@@ -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,13 +961,13 @@ 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
973
|
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
|
@@ -951,9 +987,40 @@ 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(...overrideRecords ?? page.records);
|
999
|
+
__privateAdd$6(this, _page, void 0);
|
1000
|
+
__privateSet$5(this, _page, page);
|
1001
|
+
}
|
1002
|
+
async nextPage(size, offset) {
|
1003
|
+
const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
|
1004
|
+
return new _RecordArray(newPage);
|
1005
|
+
}
|
1006
|
+
async previousPage(size, offset) {
|
1007
|
+
const newPage = await __privateGet$6(this, _page).previousPage(size, offset);
|
1008
|
+
return new _RecordArray(newPage);
|
1009
|
+
}
|
1010
|
+
async firstPage(size, offset) {
|
1011
|
+
const newPage = await __privateGet$6(this, _page).firstPage(size, offset);
|
1012
|
+
return new _RecordArray(newPage);
|
1013
|
+
}
|
1014
|
+
async lastPage(size, offset) {
|
1015
|
+
const newPage = await __privateGet$6(this, _page).lastPage(size, offset);
|
1016
|
+
return new _RecordArray(newPage);
|
1017
|
+
}
|
1018
|
+
hasNextPage() {
|
1019
|
+
return __privateGet$6(this, _page).meta.page.more;
|
1020
|
+
}
|
1021
|
+
};
|
1022
|
+
let RecordArray = _RecordArray;
|
1023
|
+
_page = new WeakMap();
|
957
1024
|
|
958
1025
|
var __accessCheck$5 = (obj, member, msg) => {
|
959
1026
|
if (!member.has(obj))
|
@@ -975,18 +1042,19 @@ var __privateSet$4 = (obj, member, value, setter) => {
|
|
975
1042
|
};
|
976
1043
|
var _table$1, _repository, _data;
|
977
1044
|
const _Query = class {
|
978
|
-
constructor(repository, table, data,
|
1045
|
+
constructor(repository, table, data, rawParent) {
|
979
1046
|
__privateAdd$5(this, _table$1, void 0);
|
980
1047
|
__privateAdd$5(this, _repository, void 0);
|
981
1048
|
__privateAdd$5(this, _data, { filter: {} });
|
982
1049
|
this.meta = { page: { cursor: "start", more: true } };
|
983
|
-
this.records = [];
|
1050
|
+
this.records = new RecordArray(this, []);
|
984
1051
|
__privateSet$4(this, _table$1, table);
|
985
1052
|
if (repository) {
|
986
1053
|
__privateSet$4(this, _repository, repository);
|
987
1054
|
} else {
|
988
1055
|
__privateSet$4(this, _repository, this);
|
989
1056
|
}
|
1057
|
+
const parent = cleanParent(data, rawParent);
|
990
1058
|
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
991
1059
|
__privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
|
992
1060
|
__privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
|
@@ -1058,18 +1126,21 @@ const _Query = class {
|
|
1058
1126
|
}
|
1059
1127
|
async *getIterator(options = {}) {
|
1060
1128
|
const { batchSize = 1 } = options;
|
1061
|
-
let
|
1062
|
-
let
|
1063
|
-
|
1064
|
-
|
1065
|
-
|
1066
|
-
|
1067
|
-
|
1129
|
+
let page = await this.getPaginated({ ...options, pagination: { size: batchSize, offset: 0 } });
|
1130
|
+
let more = page.hasNextPage();
|
1131
|
+
yield page.records;
|
1132
|
+
while (more) {
|
1133
|
+
page = await page.nextPage();
|
1134
|
+
more = page.hasNextPage();
|
1135
|
+
yield page.records;
|
1068
1136
|
}
|
1069
1137
|
}
|
1070
1138
|
async getMany(options = {}) {
|
1071
|
-
const
|
1072
|
-
|
1139
|
+
const page = await this.getPaginated(options);
|
1140
|
+
if (page.hasNextPage() && options.pagination?.size === void 0) {
|
1141
|
+
console.trace("Calling getMany does not return all results. Paginate to get all results or call getAll.");
|
1142
|
+
}
|
1143
|
+
return page.records;
|
1073
1144
|
}
|
1074
1145
|
async getAll(options = {}) {
|
1075
1146
|
const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
|
@@ -1106,12 +1177,20 @@ let Query = _Query;
|
|
1106
1177
|
_table$1 = new WeakMap();
|
1107
1178
|
_repository = new WeakMap();
|
1108
1179
|
_data = new WeakMap();
|
1180
|
+
function cleanParent(data, parent) {
|
1181
|
+
if (isCursorPaginationOptions(data.pagination)) {
|
1182
|
+
return { ...parent, sorting: void 0, filter: void 0 };
|
1183
|
+
}
|
1184
|
+
return parent;
|
1185
|
+
}
|
1109
1186
|
|
1110
1187
|
function isIdentifiable(x) {
|
1111
1188
|
return isObject(x) && isString(x?.id);
|
1112
1189
|
}
|
1113
1190
|
function isXataRecord(x) {
|
1114
|
-
|
1191
|
+
const record = x;
|
1192
|
+
const metadata = record?.getMetadata();
|
1193
|
+
return isIdentifiable(x) && isObject(metadata) && typeof metadata.version === "number";
|
1115
1194
|
}
|
1116
1195
|
|
1117
1196
|
function isSortFilterString(value) {
|
@@ -1188,9 +1267,31 @@ class RestRepository extends Query {
|
|
1188
1267
|
}
|
1189
1268
|
async create(a, b) {
|
1190
1269
|
if (Array.isArray(a)) {
|
1191
|
-
|
1192
|
-
|
1193
|
-
|
1270
|
+
if (a.length === 0)
|
1271
|
+
return [];
|
1272
|
+
const [itemsWithoutIds, itemsWithIds, order] = a.reduce(([accWithoutIds, accWithIds, accOrder], item) => {
|
1273
|
+
const condition = isString(item.id);
|
1274
|
+
accOrder.push(condition);
|
1275
|
+
if (condition) {
|
1276
|
+
accWithIds.push(item);
|
1277
|
+
} else {
|
1278
|
+
accWithoutIds.push(item);
|
1279
|
+
}
|
1280
|
+
return [accWithoutIds, accWithIds, accOrder];
|
1281
|
+
}, [[], [], []]);
|
1282
|
+
const recordsWithoutId = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, itemsWithoutIds);
|
1283
|
+
await Promise.all(recordsWithoutId.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
|
1284
|
+
if (itemsWithIds.length > 100) {
|
1285
|
+
console.warn("Bulk create operation with id is not optimized in the Xata API yet, this request might be slow");
|
1286
|
+
}
|
1287
|
+
const recordsWithId = await Promise.all(itemsWithIds.map((object) => this.create(object)));
|
1288
|
+
return order.map((condition) => {
|
1289
|
+
if (condition) {
|
1290
|
+
return recordsWithId.shift();
|
1291
|
+
} else {
|
1292
|
+
return recordsWithoutId.shift();
|
1293
|
+
}
|
1294
|
+
}).filter((record) => !!record);
|
1194
1295
|
}
|
1195
1296
|
if (isString(a) && isObject(b)) {
|
1196
1297
|
if (a === "")
|
@@ -1213,27 +1314,38 @@ class RestRepository extends Query {
|
|
1213
1314
|
}
|
1214
1315
|
throw new Error("Invalid arguments for create method");
|
1215
1316
|
}
|
1216
|
-
async read(
|
1217
|
-
|
1218
|
-
|
1219
|
-
|
1220
|
-
|
1221
|
-
|
1222
|
-
|
1223
|
-
|
1224
|
-
|
1225
|
-
|
1226
|
-
|
1227
|
-
|
1228
|
-
|
1229
|
-
|
1230
|
-
|
1317
|
+
async read(a) {
|
1318
|
+
if (Array.isArray(a)) {
|
1319
|
+
if (a.length === 0)
|
1320
|
+
return [];
|
1321
|
+
const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
|
1322
|
+
return this.getAll({ filter: { id: { $any: ids } } });
|
1323
|
+
}
|
1324
|
+
const id = isString(a) ? a : a.id;
|
1325
|
+
if (isString(id)) {
|
1326
|
+
const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, id);
|
1327
|
+
if (cacheRecord)
|
1328
|
+
return cacheRecord;
|
1329
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1330
|
+
try {
|
1331
|
+
const response = await getRecord({
|
1332
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
|
1333
|
+
...fetchProps
|
1334
|
+
});
|
1335
|
+
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1336
|
+
return initObject(this.db, schema, __privateGet$4(this, _table), response);
|
1337
|
+
} catch (e) {
|
1338
|
+
if (isObject(e) && e.status === 404) {
|
1339
|
+
return null;
|
1340
|
+
}
|
1341
|
+
throw e;
|
1231
1342
|
}
|
1232
|
-
throw e;
|
1233
1343
|
}
|
1234
1344
|
}
|
1235
1345
|
async update(a, b) {
|
1236
1346
|
if (Array.isArray(a)) {
|
1347
|
+
if (a.length === 0)
|
1348
|
+
return [];
|
1237
1349
|
if (a.length > 100) {
|
1238
1350
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1239
1351
|
}
|
@@ -1255,6 +1367,8 @@ class RestRepository extends Query {
|
|
1255
1367
|
}
|
1256
1368
|
async createOrUpdate(a, b) {
|
1257
1369
|
if (Array.isArray(a)) {
|
1370
|
+
if (a.length === 0)
|
1371
|
+
return [];
|
1258
1372
|
if (a.length > 100) {
|
1259
1373
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1260
1374
|
}
|
@@ -1276,6 +1390,8 @@ class RestRepository extends Query {
|
|
1276
1390
|
}
|
1277
1391
|
async delete(a) {
|
1278
1392
|
if (Array.isArray(a)) {
|
1393
|
+
if (a.length === 0)
|
1394
|
+
return;
|
1279
1395
|
if (a.length > 100) {
|
1280
1396
|
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
1281
1397
|
}
|
@@ -1296,9 +1412,14 @@ class RestRepository extends Query {
|
|
1296
1412
|
}
|
1297
1413
|
async search(query, options = {}) {
|
1298
1414
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1299
|
-
const { records } = await
|
1300
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1301
|
-
body: {
|
1415
|
+
const { records } = await searchTable({
|
1416
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1417
|
+
body: {
|
1418
|
+
query,
|
1419
|
+
fuzziness: options.fuzziness,
|
1420
|
+
highlight: options.highlight,
|
1421
|
+
filter: options.filter
|
1422
|
+
},
|
1302
1423
|
...fetchProps
|
1303
1424
|
});
|
1304
1425
|
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
@@ -1380,7 +1501,7 @@ bulkInsertTableRecords_fn = async function(objects) {
|
|
1380
1501
|
body: { records },
|
1381
1502
|
...fetchProps
|
1382
1503
|
});
|
1383
|
-
const finalObjects = await this.
|
1504
|
+
const finalObjects = await this.read(response.recordIDs);
|
1384
1505
|
if (finalObjects.length !== objects.length) {
|
1385
1506
|
throw new Error("The server failed to save some records");
|
1386
1507
|
}
|
@@ -1481,7 +1602,8 @@ const transformObjectLinks = (object) => {
|
|
1481
1602
|
};
|
1482
1603
|
const initObject = (db, schema, table, object) => {
|
1483
1604
|
const result = {};
|
1484
|
-
|
1605
|
+
const { xata, ...rest } = object ?? {};
|
1606
|
+
Object.assign(result, rest);
|
1485
1607
|
const { columns } = schema.tables.find(({ name }) => name === table) ?? {};
|
1486
1608
|
if (!columns)
|
1487
1609
|
console.error(`Table ${table} not found in schema`);
|
@@ -1489,10 +1611,10 @@ const initObject = (db, schema, table, object) => {
|
|
1489
1611
|
const value = result[column.name];
|
1490
1612
|
switch (column.type) {
|
1491
1613
|
case "datetime": {
|
1492
|
-
const date = new Date(value);
|
1493
|
-
if (isNaN(date.getTime())) {
|
1614
|
+
const date = value !== void 0 ? new Date(value) : void 0;
|
1615
|
+
if (date && isNaN(date.getTime())) {
|
1494
1616
|
console.error(`Failed to parse date ${value} for field ${column.name}`);
|
1495
|
-
} else {
|
1617
|
+
} else if (date) {
|
1496
1618
|
result[column.name] = date;
|
1497
1619
|
}
|
1498
1620
|
break;
|
@@ -1517,7 +1639,10 @@ const initObject = (db, schema, table, object) => {
|
|
1517
1639
|
result.delete = function() {
|
1518
1640
|
return db[table].delete(result["id"]);
|
1519
1641
|
};
|
1520
|
-
|
1642
|
+
result.getMetadata = function() {
|
1643
|
+
return xata;
|
1644
|
+
};
|
1645
|
+
for (const prop of ["read", "update", "delete", "getMetadata"]) {
|
1521
1646
|
Object.defineProperty(result, prop, { enumerable: false });
|
1522
1647
|
}
|
1523
1648
|
Object.freeze(result);
|
@@ -1699,10 +1824,10 @@ _schema = new WeakMap();
|
|
1699
1824
|
_search = new WeakSet();
|
1700
1825
|
search_fn = async function(query, options, getFetchProps) {
|
1701
1826
|
const fetchProps = await getFetchProps();
|
1702
|
-
const { tables, fuzziness } = options ?? {};
|
1827
|
+
const { tables, fuzziness, highlight } = options ?? {};
|
1703
1828
|
const { records } = await searchBranch({
|
1704
1829
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1705
|
-
body: { tables, query, fuzziness },
|
1830
|
+
body: { tables, query, fuzziness, highlight },
|
1706
1831
|
...fetchProps
|
1707
1832
|
});
|
1708
1833
|
return records;
|
@@ -1730,30 +1855,39 @@ const envBranchNames = [
|
|
1730
1855
|
"CF_PAGES_BRANCH",
|
1731
1856
|
"BRANCH"
|
1732
1857
|
];
|
1733
|
-
const defaultBranch = "main";
|
1734
1858
|
async function getCurrentBranchName(options) {
|
1735
1859
|
const env = getBranchByEnvVariable();
|
1736
|
-
if (env)
|
1737
|
-
|
1738
|
-
|
1739
|
-
|
1740
|
-
|
1741
|
-
|
1742
|
-
|
1743
|
-
|
1744
|
-
return defaultBranch;
|
1860
|
+
if (env) {
|
1861
|
+
const details = await getDatabaseBranch(env, options);
|
1862
|
+
if (details)
|
1863
|
+
return env;
|
1864
|
+
console.warn(`Branch ${env} not found in Xata. Ignoring...`);
|
1865
|
+
}
|
1866
|
+
const gitBranch = await getGitBranch();
|
1867
|
+
return resolveXataBranch(gitBranch, options);
|
1745
1868
|
}
|
1746
1869
|
async function getCurrentBranchDetails(options) {
|
1747
|
-
const
|
1748
|
-
|
1749
|
-
|
1750
|
-
|
1751
|
-
|
1752
|
-
|
1753
|
-
|
1754
|
-
|
1755
|
-
|
1756
|
-
|
1870
|
+
const branch = await getCurrentBranchName(options);
|
1871
|
+
return getDatabaseBranch(branch, options);
|
1872
|
+
}
|
1873
|
+
async function resolveXataBranch(gitBranch, options) {
|
1874
|
+
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1875
|
+
const apiKey = options?.apiKey || getAPIKey();
|
1876
|
+
if (!databaseURL)
|
1877
|
+
throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
|
1878
|
+
if (!apiKey)
|
1879
|
+
throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
|
1880
|
+
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1881
|
+
const [workspace] = host.split(".");
|
1882
|
+
const { branch } = await resolveBranch({
|
1883
|
+
apiKey,
|
1884
|
+
apiUrl: databaseURL,
|
1885
|
+
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1886
|
+
workspacesApiUrl: `${protocol}//${host}`,
|
1887
|
+
pathParams: { dbName, workspace },
|
1888
|
+
queryParams: { gitBranch, fallbackBranch: getEnvVariable("XATA_FALLBACK_BRANCH") }
|
1889
|
+
});
|
1890
|
+
return branch;
|
1757
1891
|
}
|
1758
1892
|
async function getDatabaseBranch(branch, options) {
|
1759
1893
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
@@ -1771,10 +1905,7 @@ async function getDatabaseBranch(branch, options) {
|
|
1771
1905
|
apiUrl: databaseURL,
|
1772
1906
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1773
1907
|
workspacesApiUrl: `${protocol}//${host}`,
|
1774
|
-
pathParams: {
|
1775
|
-
dbBranchName,
|
1776
|
-
workspace
|
1777
|
-
}
|
1908
|
+
pathParams: { dbBranchName, workspace }
|
1778
1909
|
});
|
1779
1910
|
} catch (err) {
|
1780
1911
|
if (isObject(err) && err.status === 404)
|
@@ -1919,6 +2050,7 @@ exports.PAGINATION_MAX_OFFSET = PAGINATION_MAX_OFFSET;
|
|
1919
2050
|
exports.PAGINATION_MAX_SIZE = PAGINATION_MAX_SIZE;
|
1920
2051
|
exports.Page = Page;
|
1921
2052
|
exports.Query = Query;
|
2053
|
+
exports.RecordArray = RecordArray;
|
1922
2054
|
exports.Repository = Repository;
|
1923
2055
|
exports.RestRepository = RestRepository;
|
1924
2056
|
exports.SchemaPlugin = SchemaPlugin;
|
@@ -1983,6 +2115,7 @@ exports.insertRecord = insertRecord;
|
|
1983
2115
|
exports.insertRecordWithID = insertRecordWithID;
|
1984
2116
|
exports.inviteWorkspaceMember = inviteWorkspaceMember;
|
1985
2117
|
exports.is = is;
|
2118
|
+
exports.isCursorPaginationOptions = isCursorPaginationOptions;
|
1986
2119
|
exports.isIdentifiable = isIdentifiable;
|
1987
2120
|
exports.isNot = isNot;
|
1988
2121
|
exports.isXataRecord = isXataRecord;
|
@@ -1998,6 +2131,7 @@ exports.removeWorkspaceMember = removeWorkspaceMember;
|
|
1998
2131
|
exports.resendWorkspaceMemberInvite = resendWorkspaceMemberInvite;
|
1999
2132
|
exports.resolveBranch = resolveBranch;
|
2000
2133
|
exports.searchBranch = searchBranch;
|
2134
|
+
exports.searchTable = searchTable;
|
2001
2135
|
exports.setTableSchema = setTableSchema;
|
2002
2136
|
exports.startsWith = startsWith;
|
2003
2137
|
exports.updateBranchMetadata = updateBranchMetadata;
|