@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.mjs
CHANGED
@@ -7,14 +7,18 @@ function compact(arr) {
|
|
7
7
|
function isObject(value) {
|
8
8
|
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
9
9
|
}
|
10
|
+
function isDefined(value) {
|
11
|
+
return value !== null && value !== void 0;
|
12
|
+
}
|
10
13
|
function isString(value) {
|
11
|
-
return value
|
14
|
+
return isDefined(value) && typeof value === "string";
|
12
15
|
}
|
13
16
|
function toBase64(value) {
|
14
17
|
try {
|
15
18
|
return btoa(value);
|
16
19
|
} catch (err) {
|
17
|
-
|
20
|
+
const buf = Buffer;
|
21
|
+
return buf.from(value).toString("base64");
|
18
22
|
}
|
19
23
|
}
|
20
24
|
|
@@ -70,16 +74,28 @@ function getFetchImplementation(userFetch) {
|
|
70
74
|
return fetchImpl;
|
71
75
|
}
|
72
76
|
|
73
|
-
|
74
|
-
|
77
|
+
const VERSION = "0.0.0-alpha.vfaf51aa";
|
78
|
+
|
79
|
+
class ErrorWithCause extends Error {
|
80
|
+
constructor(message, options) {
|
81
|
+
super(message, options);
|
82
|
+
}
|
83
|
+
}
|
84
|
+
class FetcherError extends ErrorWithCause {
|
85
|
+
constructor(status, data, requestId) {
|
75
86
|
super(getMessage(data));
|
76
87
|
this.status = status;
|
77
88
|
this.errors = isBulkError(data) ? data.errors : void 0;
|
89
|
+
this.requestId = requestId;
|
78
90
|
if (data instanceof Error) {
|
79
91
|
this.stack = data.stack;
|
80
92
|
this.cause = data.cause;
|
81
93
|
}
|
82
94
|
}
|
95
|
+
toString() {
|
96
|
+
const error = super.toString();
|
97
|
+
return `[${this.status}] (${this.requestId ?? "Unknown"}): ${error}`;
|
98
|
+
}
|
83
99
|
}
|
84
100
|
function isBulkError(error) {
|
85
101
|
return isObject(error) && Array.isArray(error.errors);
|
@@ -102,7 +118,12 @@ function getMessage(data) {
|
|
102
118
|
}
|
103
119
|
|
104
120
|
const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
|
105
|
-
const
|
121
|
+
const cleanQueryParams = Object.entries(queryParams).reduce((acc, [key, value]) => {
|
122
|
+
if (value === void 0 || value === null)
|
123
|
+
return acc;
|
124
|
+
return { ...acc, [key]: value };
|
125
|
+
}, {});
|
126
|
+
const query = new URLSearchParams(cleanQueryParams).toString();
|
106
127
|
const queryString = query.length > 0 ? `?${query}` : "";
|
107
128
|
return url.replace(/\{\w*\}/g, (key) => pathParams[key.slice(1, -1)]) + queryString;
|
108
129
|
};
|
@@ -142,6 +163,7 @@ async function fetch$1({
|
|
142
163
|
body: body ? JSON.stringify(body) : void 0,
|
143
164
|
headers: {
|
144
165
|
"Content-Type": "application/json",
|
166
|
+
"User-Agent": `Xata client-ts/${VERSION}`,
|
145
167
|
...headers,
|
146
168
|
...hostHeader(fullUrl),
|
147
169
|
Authorization: `Bearer ${apiKey}`
|
@@ -150,14 +172,15 @@ async function fetch$1({
|
|
150
172
|
if (response.status === 204) {
|
151
173
|
return {};
|
152
174
|
}
|
175
|
+
const requestId = response.headers?.get("x-request-id") ?? void 0;
|
153
176
|
try {
|
154
177
|
const jsonResponse = await response.json();
|
155
178
|
if (response.ok) {
|
156
179
|
return jsonResponse;
|
157
180
|
}
|
158
|
-
throw new FetcherError(response.status, jsonResponse);
|
181
|
+
throw new FetcherError(response.status, jsonResponse, requestId);
|
159
182
|
} catch (error) {
|
160
|
-
throw new FetcherError(response.status, error);
|
183
|
+
throw new FetcherError(response.status, error, requestId);
|
161
184
|
}
|
162
185
|
}
|
163
186
|
|
@@ -366,6 +389,11 @@ const queryTable = (variables) => fetch$1({
|
|
366
389
|
method: "post",
|
367
390
|
...variables
|
368
391
|
});
|
392
|
+
const searchTable = (variables) => fetch$1({
|
393
|
+
url: "/db/{dbBranchName}/tables/{tableName}/search",
|
394
|
+
method: "post",
|
395
|
+
...variables
|
396
|
+
});
|
369
397
|
const searchBranch = (variables) => fetch$1({
|
370
398
|
url: "/db/{dbBranchName}/search",
|
371
399
|
method: "post",
|
@@ -429,6 +457,7 @@ const operationsByTag = {
|
|
429
457
|
getRecord,
|
430
458
|
bulkInsertTableRecords,
|
431
459
|
queryTable,
|
460
|
+
searchTable,
|
432
461
|
searchBranch
|
433
462
|
}
|
434
463
|
};
|
@@ -675,10 +704,10 @@ class DatabaseApi {
|
|
675
704
|
...this.extraProps
|
676
705
|
});
|
677
706
|
}
|
678
|
-
resolveBranch(workspace, dbName, gitBranch) {
|
707
|
+
resolveBranch(workspace, dbName, gitBranch, fallbackBranch) {
|
679
708
|
return operationsByTag.database.resolveBranch({
|
680
709
|
pathParams: { workspace, dbName },
|
681
|
-
queryParams: { gitBranch },
|
710
|
+
queryParams: { gitBranch, fallbackBranch },
|
682
711
|
...this.extraProps
|
683
712
|
});
|
684
713
|
}
|
@@ -884,6 +913,13 @@ class RecordsApi {
|
|
884
913
|
...this.extraProps
|
885
914
|
});
|
886
915
|
}
|
916
|
+
searchTable(workspace, database, branch, tableName, query) {
|
917
|
+
return operationsByTag.records.searchTable({
|
918
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
919
|
+
body: query,
|
920
|
+
...this.extraProps
|
921
|
+
});
|
922
|
+
}
|
887
923
|
searchBranch(workspace, database, branch, query) {
|
888
924
|
return operationsByTag.records.searchBranch({
|
889
925
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
@@ -921,13 +957,13 @@ var __privateSet$5 = (obj, member, value, setter) => {
|
|
921
957
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
922
958
|
return value;
|
923
959
|
};
|
924
|
-
var _query;
|
960
|
+
var _query, _page;
|
925
961
|
class Page {
|
926
962
|
constructor(query, meta, records = []) {
|
927
963
|
__privateAdd$6(this, _query, void 0);
|
928
964
|
__privateSet$5(this, _query, query);
|
929
965
|
this.meta = meta;
|
930
|
-
this.records = records;
|
966
|
+
this.records = new RecordArray(this, records);
|
931
967
|
}
|
932
968
|
async nextPage(size, offset) {
|
933
969
|
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
|
@@ -947,9 +983,40 @@ class Page {
|
|
947
983
|
}
|
948
984
|
_query = new WeakMap();
|
949
985
|
const PAGINATION_MAX_SIZE = 200;
|
950
|
-
const PAGINATION_DEFAULT_SIZE =
|
986
|
+
const PAGINATION_DEFAULT_SIZE = 20;
|
951
987
|
const PAGINATION_MAX_OFFSET = 800;
|
952
988
|
const PAGINATION_DEFAULT_OFFSET = 0;
|
989
|
+
function isCursorPaginationOptions(options) {
|
990
|
+
return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
|
991
|
+
}
|
992
|
+
const _RecordArray = class extends Array {
|
993
|
+
constructor(page, overrideRecords) {
|
994
|
+
super(...overrideRecords ?? page.records);
|
995
|
+
__privateAdd$6(this, _page, void 0);
|
996
|
+
__privateSet$5(this, _page, page);
|
997
|
+
}
|
998
|
+
async nextPage(size, offset) {
|
999
|
+
const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
|
1000
|
+
return new _RecordArray(newPage);
|
1001
|
+
}
|
1002
|
+
async previousPage(size, offset) {
|
1003
|
+
const newPage = await __privateGet$6(this, _page).previousPage(size, offset);
|
1004
|
+
return new _RecordArray(newPage);
|
1005
|
+
}
|
1006
|
+
async firstPage(size, offset) {
|
1007
|
+
const newPage = await __privateGet$6(this, _page).firstPage(size, offset);
|
1008
|
+
return new _RecordArray(newPage);
|
1009
|
+
}
|
1010
|
+
async lastPage(size, offset) {
|
1011
|
+
const newPage = await __privateGet$6(this, _page).lastPage(size, offset);
|
1012
|
+
return new _RecordArray(newPage);
|
1013
|
+
}
|
1014
|
+
hasNextPage() {
|
1015
|
+
return __privateGet$6(this, _page).meta.page.more;
|
1016
|
+
}
|
1017
|
+
};
|
1018
|
+
let RecordArray = _RecordArray;
|
1019
|
+
_page = new WeakMap();
|
953
1020
|
|
954
1021
|
var __accessCheck$5 = (obj, member, msg) => {
|
955
1022
|
if (!member.has(obj))
|
@@ -971,18 +1038,19 @@ var __privateSet$4 = (obj, member, value, setter) => {
|
|
971
1038
|
};
|
972
1039
|
var _table$1, _repository, _data;
|
973
1040
|
const _Query = class {
|
974
|
-
constructor(repository, table, data,
|
1041
|
+
constructor(repository, table, data, rawParent) {
|
975
1042
|
__privateAdd$5(this, _table$1, void 0);
|
976
1043
|
__privateAdd$5(this, _repository, void 0);
|
977
1044
|
__privateAdd$5(this, _data, { filter: {} });
|
978
1045
|
this.meta = { page: { cursor: "start", more: true } };
|
979
|
-
this.records = [];
|
1046
|
+
this.records = new RecordArray(this, []);
|
980
1047
|
__privateSet$4(this, _table$1, table);
|
981
1048
|
if (repository) {
|
982
1049
|
__privateSet$4(this, _repository, repository);
|
983
1050
|
} else {
|
984
1051
|
__privateSet$4(this, _repository, this);
|
985
1052
|
}
|
1053
|
+
const parent = cleanParent(data, rawParent);
|
986
1054
|
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
987
1055
|
__privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
|
988
1056
|
__privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
|
@@ -1054,18 +1122,21 @@ const _Query = class {
|
|
1054
1122
|
}
|
1055
1123
|
async *getIterator(options = {}) {
|
1056
1124
|
const { batchSize = 1 } = options;
|
1057
|
-
let
|
1058
|
-
let
|
1059
|
-
|
1060
|
-
|
1061
|
-
|
1062
|
-
|
1063
|
-
|
1125
|
+
let page = await this.getPaginated({ ...options, pagination: { size: batchSize, offset: 0 } });
|
1126
|
+
let more = page.hasNextPage();
|
1127
|
+
yield page.records;
|
1128
|
+
while (more) {
|
1129
|
+
page = await page.nextPage();
|
1130
|
+
more = page.hasNextPage();
|
1131
|
+
yield page.records;
|
1064
1132
|
}
|
1065
1133
|
}
|
1066
1134
|
async getMany(options = {}) {
|
1067
|
-
const
|
1068
|
-
|
1135
|
+
const page = await this.getPaginated(options);
|
1136
|
+
if (page.hasNextPage() && options.pagination?.size === void 0) {
|
1137
|
+
console.trace("Calling getMany does not return all results. Paginate to get all results or call getAll.");
|
1138
|
+
}
|
1139
|
+
return page.records;
|
1069
1140
|
}
|
1070
1141
|
async getAll(options = {}) {
|
1071
1142
|
const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
|
@@ -1102,12 +1173,20 @@ let Query = _Query;
|
|
1102
1173
|
_table$1 = new WeakMap();
|
1103
1174
|
_repository = new WeakMap();
|
1104
1175
|
_data = new WeakMap();
|
1176
|
+
function cleanParent(data, parent) {
|
1177
|
+
if (isCursorPaginationOptions(data.pagination)) {
|
1178
|
+
return { ...parent, sorting: void 0, filter: void 0 };
|
1179
|
+
}
|
1180
|
+
return parent;
|
1181
|
+
}
|
1105
1182
|
|
1106
1183
|
function isIdentifiable(x) {
|
1107
1184
|
return isObject(x) && isString(x?.id);
|
1108
1185
|
}
|
1109
1186
|
function isXataRecord(x) {
|
1110
|
-
|
1187
|
+
const record = x;
|
1188
|
+
const metadata = record?.getMetadata();
|
1189
|
+
return isIdentifiable(x) && isObject(metadata) && typeof metadata.version === "number";
|
1111
1190
|
}
|
1112
1191
|
|
1113
1192
|
function isSortFilterString(value) {
|
@@ -1184,9 +1263,31 @@ class RestRepository extends Query {
|
|
1184
1263
|
}
|
1185
1264
|
async create(a, b) {
|
1186
1265
|
if (Array.isArray(a)) {
|
1187
|
-
|
1188
|
-
|
1189
|
-
|
1266
|
+
if (a.length === 0)
|
1267
|
+
return [];
|
1268
|
+
const [itemsWithoutIds, itemsWithIds, order] = a.reduce(([accWithoutIds, accWithIds, accOrder], item) => {
|
1269
|
+
const condition = isString(item.id);
|
1270
|
+
accOrder.push(condition);
|
1271
|
+
if (condition) {
|
1272
|
+
accWithIds.push(item);
|
1273
|
+
} else {
|
1274
|
+
accWithoutIds.push(item);
|
1275
|
+
}
|
1276
|
+
return [accWithoutIds, accWithIds, accOrder];
|
1277
|
+
}, [[], [], []]);
|
1278
|
+
const recordsWithoutId = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, itemsWithoutIds);
|
1279
|
+
await Promise.all(recordsWithoutId.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
|
1280
|
+
if (itemsWithIds.length > 100) {
|
1281
|
+
console.warn("Bulk create operation with id is not optimized in the Xata API yet, this request might be slow");
|
1282
|
+
}
|
1283
|
+
const recordsWithId = await Promise.all(itemsWithIds.map((object) => this.create(object)));
|
1284
|
+
return order.map((condition) => {
|
1285
|
+
if (condition) {
|
1286
|
+
return recordsWithId.shift();
|
1287
|
+
} else {
|
1288
|
+
return recordsWithoutId.shift();
|
1289
|
+
}
|
1290
|
+
}).filter((record) => !!record);
|
1190
1291
|
}
|
1191
1292
|
if (isString(a) && isObject(b)) {
|
1192
1293
|
if (a === "")
|
@@ -1209,27 +1310,38 @@ class RestRepository extends Query {
|
|
1209
1310
|
}
|
1210
1311
|
throw new Error("Invalid arguments for create method");
|
1211
1312
|
}
|
1212
|
-
async read(
|
1213
|
-
|
1214
|
-
|
1215
|
-
|
1216
|
-
|
1217
|
-
|
1218
|
-
|
1219
|
-
|
1220
|
-
|
1221
|
-
|
1222
|
-
|
1223
|
-
|
1224
|
-
|
1225
|
-
|
1226
|
-
|
1313
|
+
async read(a) {
|
1314
|
+
if (Array.isArray(a)) {
|
1315
|
+
if (a.length === 0)
|
1316
|
+
return [];
|
1317
|
+
const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
|
1318
|
+
return this.getAll({ filter: { id: { $any: ids } } });
|
1319
|
+
}
|
1320
|
+
const id = isString(a) ? a : a.id;
|
1321
|
+
if (isString(id)) {
|
1322
|
+
const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, id);
|
1323
|
+
if (cacheRecord)
|
1324
|
+
return cacheRecord;
|
1325
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1326
|
+
try {
|
1327
|
+
const response = await getRecord({
|
1328
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
|
1329
|
+
...fetchProps
|
1330
|
+
});
|
1331
|
+
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1332
|
+
return initObject(this.db, schema, __privateGet$4(this, _table), response);
|
1333
|
+
} catch (e) {
|
1334
|
+
if (isObject(e) && e.status === 404) {
|
1335
|
+
return null;
|
1336
|
+
}
|
1337
|
+
throw e;
|
1227
1338
|
}
|
1228
|
-
throw e;
|
1229
1339
|
}
|
1230
1340
|
}
|
1231
1341
|
async update(a, b) {
|
1232
1342
|
if (Array.isArray(a)) {
|
1343
|
+
if (a.length === 0)
|
1344
|
+
return [];
|
1233
1345
|
if (a.length > 100) {
|
1234
1346
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1235
1347
|
}
|
@@ -1251,6 +1363,8 @@ class RestRepository extends Query {
|
|
1251
1363
|
}
|
1252
1364
|
async createOrUpdate(a, b) {
|
1253
1365
|
if (Array.isArray(a)) {
|
1366
|
+
if (a.length === 0)
|
1367
|
+
return [];
|
1254
1368
|
if (a.length > 100) {
|
1255
1369
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1256
1370
|
}
|
@@ -1272,6 +1386,8 @@ class RestRepository extends Query {
|
|
1272
1386
|
}
|
1273
1387
|
async delete(a) {
|
1274
1388
|
if (Array.isArray(a)) {
|
1389
|
+
if (a.length === 0)
|
1390
|
+
return;
|
1275
1391
|
if (a.length > 100) {
|
1276
1392
|
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
1277
1393
|
}
|
@@ -1292,9 +1408,14 @@ class RestRepository extends Query {
|
|
1292
1408
|
}
|
1293
1409
|
async search(query, options = {}) {
|
1294
1410
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1295
|
-
const { records } = await
|
1296
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1297
|
-
body: {
|
1411
|
+
const { records } = await searchTable({
|
1412
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1413
|
+
body: {
|
1414
|
+
query,
|
1415
|
+
fuzziness: options.fuzziness,
|
1416
|
+
highlight: options.highlight,
|
1417
|
+
filter: options.filter
|
1418
|
+
},
|
1298
1419
|
...fetchProps
|
1299
1420
|
});
|
1300
1421
|
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
@@ -1376,7 +1497,7 @@ bulkInsertTableRecords_fn = async function(objects) {
|
|
1376
1497
|
body: { records },
|
1377
1498
|
...fetchProps
|
1378
1499
|
});
|
1379
|
-
const finalObjects = await this.
|
1500
|
+
const finalObjects = await this.read(response.recordIDs);
|
1380
1501
|
if (finalObjects.length !== objects.length) {
|
1381
1502
|
throw new Error("The server failed to save some records");
|
1382
1503
|
}
|
@@ -1477,7 +1598,8 @@ const transformObjectLinks = (object) => {
|
|
1477
1598
|
};
|
1478
1599
|
const initObject = (db, schema, table, object) => {
|
1479
1600
|
const result = {};
|
1480
|
-
|
1601
|
+
const { xata, ...rest } = object ?? {};
|
1602
|
+
Object.assign(result, rest);
|
1481
1603
|
const { columns } = schema.tables.find(({ name }) => name === table) ?? {};
|
1482
1604
|
if (!columns)
|
1483
1605
|
console.error(`Table ${table} not found in schema`);
|
@@ -1485,10 +1607,10 @@ const initObject = (db, schema, table, object) => {
|
|
1485
1607
|
const value = result[column.name];
|
1486
1608
|
switch (column.type) {
|
1487
1609
|
case "datetime": {
|
1488
|
-
const date = new Date(value);
|
1489
|
-
if (isNaN(date.getTime())) {
|
1610
|
+
const date = value !== void 0 ? new Date(value) : void 0;
|
1611
|
+
if (date && isNaN(date.getTime())) {
|
1490
1612
|
console.error(`Failed to parse date ${value} for field ${column.name}`);
|
1491
|
-
} else {
|
1613
|
+
} else if (date) {
|
1492
1614
|
result[column.name] = date;
|
1493
1615
|
}
|
1494
1616
|
break;
|
@@ -1513,7 +1635,10 @@ const initObject = (db, schema, table, object) => {
|
|
1513
1635
|
result.delete = function() {
|
1514
1636
|
return db[table].delete(result["id"]);
|
1515
1637
|
};
|
1516
|
-
|
1638
|
+
result.getMetadata = function() {
|
1639
|
+
return xata;
|
1640
|
+
};
|
1641
|
+
for (const prop of ["read", "update", "delete", "getMetadata"]) {
|
1517
1642
|
Object.defineProperty(result, prop, { enumerable: false });
|
1518
1643
|
}
|
1519
1644
|
Object.freeze(result);
|
@@ -1695,10 +1820,10 @@ _schema = new WeakMap();
|
|
1695
1820
|
_search = new WeakSet();
|
1696
1821
|
search_fn = async function(query, options, getFetchProps) {
|
1697
1822
|
const fetchProps = await getFetchProps();
|
1698
|
-
const { tables, fuzziness } = options ?? {};
|
1823
|
+
const { tables, fuzziness, highlight } = options ?? {};
|
1699
1824
|
const { records } = await searchBranch({
|
1700
1825
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1701
|
-
body: { tables, query, fuzziness },
|
1826
|
+
body: { tables, query, fuzziness, highlight },
|
1702
1827
|
...fetchProps
|
1703
1828
|
});
|
1704
1829
|
return records;
|
@@ -1726,30 +1851,39 @@ const envBranchNames = [
|
|
1726
1851
|
"CF_PAGES_BRANCH",
|
1727
1852
|
"BRANCH"
|
1728
1853
|
];
|
1729
|
-
const defaultBranch = "main";
|
1730
1854
|
async function getCurrentBranchName(options) {
|
1731
1855
|
const env = getBranchByEnvVariable();
|
1732
|
-
if (env)
|
1733
|
-
|
1734
|
-
|
1735
|
-
|
1736
|
-
|
1737
|
-
|
1738
|
-
|
1739
|
-
|
1740
|
-
return defaultBranch;
|
1856
|
+
if (env) {
|
1857
|
+
const details = await getDatabaseBranch(env, options);
|
1858
|
+
if (details)
|
1859
|
+
return env;
|
1860
|
+
console.warn(`Branch ${env} not found in Xata. Ignoring...`);
|
1861
|
+
}
|
1862
|
+
const gitBranch = await getGitBranch();
|
1863
|
+
return resolveXataBranch(gitBranch, options);
|
1741
1864
|
}
|
1742
1865
|
async function getCurrentBranchDetails(options) {
|
1743
|
-
const
|
1744
|
-
|
1745
|
-
|
1746
|
-
|
1747
|
-
|
1748
|
-
|
1749
|
-
|
1750
|
-
|
1751
|
-
|
1752
|
-
|
1866
|
+
const branch = await getCurrentBranchName(options);
|
1867
|
+
return getDatabaseBranch(branch, options);
|
1868
|
+
}
|
1869
|
+
async function resolveXataBranch(gitBranch, options) {
|
1870
|
+
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1871
|
+
const apiKey = options?.apiKey || getAPIKey();
|
1872
|
+
if (!databaseURL)
|
1873
|
+
throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
|
1874
|
+
if (!apiKey)
|
1875
|
+
throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
|
1876
|
+
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1877
|
+
const [workspace] = host.split(".");
|
1878
|
+
const { branch } = await resolveBranch({
|
1879
|
+
apiKey,
|
1880
|
+
apiUrl: databaseURL,
|
1881
|
+
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1882
|
+
workspacesApiUrl: `${protocol}//${host}`,
|
1883
|
+
pathParams: { dbName, workspace },
|
1884
|
+
queryParams: { gitBranch, fallbackBranch: getEnvVariable("XATA_FALLBACK_BRANCH") }
|
1885
|
+
});
|
1886
|
+
return branch;
|
1753
1887
|
}
|
1754
1888
|
async function getDatabaseBranch(branch, options) {
|
1755
1889
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
@@ -1767,10 +1901,7 @@ async function getDatabaseBranch(branch, options) {
|
|
1767
1901
|
apiUrl: databaseURL,
|
1768
1902
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1769
1903
|
workspacesApiUrl: `${protocol}//${host}`,
|
1770
|
-
pathParams: {
|
1771
|
-
dbBranchName,
|
1772
|
-
workspace
|
1773
|
-
}
|
1904
|
+
pathParams: { dbBranchName, workspace }
|
1774
1905
|
});
|
1775
1906
|
} catch (err) {
|
1776
1907
|
if (isObject(err) && err.status === 404)
|
@@ -1907,5 +2038,5 @@ class XataError extends Error {
|
|
1907
2038
|
}
|
1908
2039
|
}
|
1909
2040
|
|
1910
|
-
export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, Repository, RestRepository, SchemaPlugin, SearchPlugin, SimpleCache, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addGitBranchesEntry, addTableColumn, buildClient, bulkInsertTableRecords, cancelWorkspaceMemberInvite, contains, createBranch, createDatabase, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, endsWith, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseURL, getGitBranchesMapping, getRecord, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getWorkspace, getWorkspaceMembersList, getWorkspacesList, gt, gte, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isIdentifiable, isNot, isXataRecord, le, lt, lte, notExists, operationsByTag, pattern, queryTable, removeGitBranchesEntry, removeWorkspaceMember, resendWorkspaceMemberInvite, resolveBranch, searchBranch, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
|
2041
|
+
export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, RecordArray, Repository, RestRepository, SchemaPlugin, SearchPlugin, SimpleCache, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addGitBranchesEntry, addTableColumn, buildClient, bulkInsertTableRecords, cancelWorkspaceMemberInvite, contains, createBranch, createDatabase, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, endsWith, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseURL, getGitBranchesMapping, getRecord, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getWorkspace, getWorkspaceMembersList, getWorkspacesList, gt, gte, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isCursorPaginationOptions, isIdentifiable, isNot, isXataRecord, le, lt, lte, notExists, operationsByTag, pattern, queryTable, removeGitBranchesEntry, removeWorkspaceMember, resendWorkspaceMemberInvite, resolveBranch, searchBranch, searchTable, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
|
1911
2042
|
//# sourceMappingURL=index.mjs.map
|