@xata.io/client 0.0.0-alpha.vec0bff6 → 0.0.0-alpha.vec88a57
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 +24 -0
- package/README.md +2 -0
- package/Usage.md +27 -6
- package/dist/index.cjs +383 -179
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +171 -17
- package/dist/index.mjs +372 -180
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,27 @@
|
|
|
1
|
+
const defaultTrace = async (_name, fn, _options) => {
|
|
2
|
+
return await fn({
|
|
3
|
+
setAttributes: () => {
|
|
4
|
+
return;
|
|
5
|
+
},
|
|
6
|
+
onError: () => {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
const TraceAttributes = {
|
|
12
|
+
VERSION: "xata.sdk.version",
|
|
13
|
+
TABLE: "xata.table",
|
|
14
|
+
HTTP_REQUEST_ID: "http.request_id",
|
|
15
|
+
HTTP_STATUS_CODE: "http.status_code",
|
|
16
|
+
HTTP_HOST: "http.host",
|
|
17
|
+
HTTP_SCHEME: "http.scheme",
|
|
18
|
+
HTTP_USER_AGENT: "http.user_agent",
|
|
19
|
+
HTTP_METHOD: "http.method",
|
|
20
|
+
HTTP_URL: "http.url",
|
|
21
|
+
HTTP_ROUTE: "http.route",
|
|
22
|
+
HTTP_TARGET: "http.target"
|
|
23
|
+
};
|
|
24
|
+
|
|
1
25
|
function notEmpty(value) {
|
|
2
26
|
return value !== null && value !== void 0;
|
|
3
27
|
}
|
|
@@ -128,7 +152,7 @@ function getFetchImplementation(userFetch) {
|
|
|
128
152
|
return fetchImpl;
|
|
129
153
|
}
|
|
130
154
|
|
|
131
|
-
const VERSION = "0.0.0-alpha.
|
|
155
|
+
const VERSION = "0.0.0-alpha.vec88a57";
|
|
132
156
|
|
|
133
157
|
class ErrorWithCause extends Error {
|
|
134
158
|
constructor(message, options) {
|
|
@@ -207,34 +231,62 @@ async function fetch$1({
|
|
|
207
231
|
fetchImpl,
|
|
208
232
|
apiKey,
|
|
209
233
|
apiUrl,
|
|
210
|
-
workspacesApiUrl
|
|
234
|
+
workspacesApiUrl,
|
|
235
|
+
trace
|
|
211
236
|
}) {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
237
|
+
return trace(
|
|
238
|
+
`${method.toUpperCase()} ${path}`,
|
|
239
|
+
async ({ setAttributes, onError }) => {
|
|
240
|
+
const baseUrl = buildBaseUrl({ path, workspacesApiUrl, pathParams, apiUrl });
|
|
241
|
+
const fullUrl = resolveUrl(baseUrl, queryParams, pathParams);
|
|
242
|
+
const url = fullUrl.includes("localhost") ? fullUrl.replace(/^[^.]+\./, "http://") : fullUrl;
|
|
243
|
+
setAttributes({
|
|
244
|
+
[TraceAttributes.HTTP_URL]: url,
|
|
245
|
+
[TraceAttributes.HTTP_TARGET]: resolveUrl(path, queryParams, pathParams)
|
|
246
|
+
});
|
|
247
|
+
const response = await fetchImpl(url, {
|
|
248
|
+
method: method.toUpperCase(),
|
|
249
|
+
body: body ? JSON.stringify(body) : void 0,
|
|
250
|
+
headers: {
|
|
251
|
+
"Content-Type": "application/json",
|
|
252
|
+
"User-Agent": `Xata client-ts/${VERSION}`,
|
|
253
|
+
...headers,
|
|
254
|
+
...hostHeader(fullUrl),
|
|
255
|
+
Authorization: `Bearer ${apiKey}`
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
if (response.status === 204) {
|
|
259
|
+
return {};
|
|
260
|
+
}
|
|
261
|
+
const { host, protocol } = parseUrl(response.url);
|
|
262
|
+
const requestId = response.headers?.get("x-request-id") ?? void 0;
|
|
263
|
+
setAttributes({
|
|
264
|
+
[TraceAttributes.HTTP_REQUEST_ID]: requestId,
|
|
265
|
+
[TraceAttributes.HTTP_STATUS_CODE]: response.status,
|
|
266
|
+
[TraceAttributes.HTTP_HOST]: host,
|
|
267
|
+
[TraceAttributes.HTTP_SCHEME]: protocol?.replace(":", "")
|
|
268
|
+
});
|
|
269
|
+
try {
|
|
270
|
+
const jsonResponse = await response.json();
|
|
271
|
+
if (response.ok) {
|
|
272
|
+
return jsonResponse;
|
|
273
|
+
}
|
|
274
|
+
throw new FetcherError(response.status, jsonResponse, requestId);
|
|
275
|
+
} catch (error) {
|
|
276
|
+
const fetcherError = new FetcherError(response.status, error, requestId);
|
|
277
|
+
onError(fetcherError.message);
|
|
278
|
+
throw fetcherError;
|
|
279
|
+
}
|
|
280
|
+
},
|
|
281
|
+
{ [TraceAttributes.HTTP_METHOD]: method.toUpperCase(), [TraceAttributes.HTTP_ROUTE]: path }
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
function parseUrl(url) {
|
|
230
285
|
try {
|
|
231
|
-
const
|
|
232
|
-
|
|
233
|
-
return jsonResponse;
|
|
234
|
-
}
|
|
235
|
-
throw new FetcherError(response.status, jsonResponse, requestId);
|
|
286
|
+
const { host, protocol } = new URL(url);
|
|
287
|
+
return { host, protocol };
|
|
236
288
|
} catch (error) {
|
|
237
|
-
|
|
289
|
+
return {};
|
|
238
290
|
}
|
|
239
291
|
}
|
|
240
292
|
|
|
@@ -329,6 +381,11 @@ const deleteDatabase = (variables) => fetch$1({
|
|
|
329
381
|
method: "delete",
|
|
330
382
|
...variables
|
|
331
383
|
});
|
|
384
|
+
const getDatabaseMetadata = (variables) => fetch$1({
|
|
385
|
+
url: "/dbs/{dbName}/metadata",
|
|
386
|
+
method: "get",
|
|
387
|
+
...variables
|
|
388
|
+
});
|
|
332
389
|
const getGitBranchesMapping = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "get", ...variables });
|
|
333
390
|
const addGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "post", ...variables });
|
|
334
391
|
const removeGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "delete", ...variables });
|
|
@@ -467,6 +524,7 @@ const operationsByTag = {
|
|
|
467
524
|
getDatabaseList,
|
|
468
525
|
createDatabase,
|
|
469
526
|
deleteDatabase,
|
|
527
|
+
getDatabaseMetadata,
|
|
470
528
|
getGitBranchesMapping,
|
|
471
529
|
addGitBranchesEntry,
|
|
472
530
|
removeGitBranchesEntry,
|
|
@@ -559,7 +617,8 @@ class XataApiClient {
|
|
|
559
617
|
__privateAdd$7(this, _extraProps, void 0);
|
|
560
618
|
__privateAdd$7(this, _namespaces, {});
|
|
561
619
|
const provider = options.host ?? "production";
|
|
562
|
-
const apiKey = options
|
|
620
|
+
const apiKey = options.apiKey ?? getAPIKey();
|
|
621
|
+
const trace = options.trace ?? defaultTrace;
|
|
563
622
|
if (!apiKey) {
|
|
564
623
|
throw new Error("Could not resolve a valid apiKey");
|
|
565
624
|
}
|
|
@@ -567,7 +626,8 @@ class XataApiClient {
|
|
|
567
626
|
apiUrl: getHostUrl(provider, "main"),
|
|
568
627
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
|
569
628
|
fetchImpl: getFetchImplementation(options.fetch),
|
|
570
|
-
apiKey
|
|
629
|
+
apiKey,
|
|
630
|
+
trace
|
|
571
631
|
});
|
|
572
632
|
}
|
|
573
633
|
get user() {
|
|
@@ -739,6 +799,12 @@ class DatabaseApi {
|
|
|
739
799
|
...this.extraProps
|
|
740
800
|
});
|
|
741
801
|
}
|
|
802
|
+
getDatabaseMetadata(workspace, dbName) {
|
|
803
|
+
return operationsByTag.database.getDatabaseMetadata({
|
|
804
|
+
pathParams: { workspace, dbName },
|
|
805
|
+
...this.extraProps
|
|
806
|
+
});
|
|
807
|
+
}
|
|
742
808
|
getGitBranchesMapping(workspace, dbName) {
|
|
743
809
|
return operationsByTag.database.getGitBranchesMapping({
|
|
744
810
|
pathParams: { workspace, dbName },
|
|
@@ -1178,7 +1244,7 @@ const _Query = class {
|
|
|
1178
1244
|
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
|
1179
1245
|
}
|
|
1180
1246
|
}
|
|
1181
|
-
sort(column, direction) {
|
|
1247
|
+
sort(column, direction = "asc") {
|
|
1182
1248
|
const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
|
|
1183
1249
|
const sort = [...originalSort, { column, direction }];
|
|
1184
1250
|
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
|
|
@@ -1314,7 +1380,7 @@ var __privateMethod$2 = (obj, member, method) => {
|
|
|
1314
1380
|
__accessCheck$4(obj, member, "access private method");
|
|
1315
1381
|
return method;
|
|
1316
1382
|
};
|
|
1317
|
-
var _table, _getFetchProps, _db, _cache, _schemaTables$2, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _bulkInsertTableRecords, bulkInsertTableRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn, _setCacheQuery, setCacheQuery_fn, _getCacheQuery, getCacheQuery_fn, _getSchemaTables$1, getSchemaTables_fn$1;
|
|
1383
|
+
var _table, _getFetchProps, _db, _cache, _schemaTables$2, _trace, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _bulkInsertTableRecords, bulkInsertTableRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn, _setCacheQuery, setCacheQuery_fn, _getCacheQuery, getCacheQuery_fn, _getSchemaTables$1, getSchemaTables_fn$1;
|
|
1318
1384
|
class Repository extends Query {
|
|
1319
1385
|
}
|
|
1320
1386
|
class RestRepository extends Query {
|
|
@@ -1334,168 +1400,196 @@ class RestRepository extends Query {
|
|
|
1334
1400
|
__privateAdd$4(this, _db, void 0);
|
|
1335
1401
|
__privateAdd$4(this, _cache, void 0);
|
|
1336
1402
|
__privateAdd$4(this, _schemaTables$2, void 0);
|
|
1403
|
+
__privateAdd$4(this, _trace, void 0);
|
|
1337
1404
|
__privateSet$4(this, _table, options.table);
|
|
1338
1405
|
__privateSet$4(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
|
1339
1406
|
__privateSet$4(this, _db, options.db);
|
|
1340
1407
|
__privateSet$4(this, _cache, options.pluginOptions.cache);
|
|
1341
1408
|
__privateSet$4(this, _schemaTables$2, options.schemaTables);
|
|
1409
|
+
const trace = options.pluginOptions.trace ?? defaultTrace;
|
|
1410
|
+
__privateSet$4(this, _trace, async (name, fn, options2 = {}) => {
|
|
1411
|
+
return trace(name, fn, {
|
|
1412
|
+
...options2,
|
|
1413
|
+
[TraceAttributes.TABLE]: __privateGet$4(this, _table),
|
|
1414
|
+
[TraceAttributes.VERSION]: VERSION
|
|
1415
|
+
});
|
|
1416
|
+
});
|
|
1342
1417
|
}
|
|
1343
1418
|
async create(a, b, c) {
|
|
1344
|
-
|
|
1345
|
-
if (a
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
if (a
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
if (a.id
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1419
|
+
return __privateGet$4(this, _trace).call(this, "create", async () => {
|
|
1420
|
+
if (Array.isArray(a)) {
|
|
1421
|
+
if (a.length === 0)
|
|
1422
|
+
return [];
|
|
1423
|
+
const columns = isStringArray(b) ? b : void 0;
|
|
1424
|
+
return __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a, columns);
|
|
1425
|
+
}
|
|
1426
|
+
if (isString(a) && isObject(b)) {
|
|
1427
|
+
if (a === "")
|
|
1428
|
+
throw new Error("The id can't be empty");
|
|
1429
|
+
const columns = isStringArray(c) ? c : void 0;
|
|
1430
|
+
return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b, columns);
|
|
1431
|
+
}
|
|
1432
|
+
if (isObject(a) && isString(a.id)) {
|
|
1433
|
+
if (a.id === "")
|
|
1434
|
+
throw new Error("The id can't be empty");
|
|
1435
|
+
const columns = isStringArray(b) ? b : void 0;
|
|
1436
|
+
return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
|
1437
|
+
}
|
|
1438
|
+
if (isObject(a)) {
|
|
1439
|
+
const columns = isStringArray(b) ? b : void 0;
|
|
1440
|
+
return __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a, columns);
|
|
1441
|
+
}
|
|
1442
|
+
throw new Error("Invalid arguments for create method");
|
|
1443
|
+
});
|
|
1367
1444
|
}
|
|
1368
1445
|
async read(a, b) {
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
if (a
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
acc
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1446
|
+
return __privateGet$4(this, _trace).call(this, "read", async () => {
|
|
1447
|
+
const columns = isStringArray(b) ? b : ["*"];
|
|
1448
|
+
if (Array.isArray(a)) {
|
|
1449
|
+
if (a.length === 0)
|
|
1450
|
+
return [];
|
|
1451
|
+
const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
|
|
1452
|
+
const finalObjects = await this.getAll({ filter: { id: { $any: ids } }, columns });
|
|
1453
|
+
const dictionary = finalObjects.reduce((acc, object) => {
|
|
1454
|
+
acc[object.id] = object;
|
|
1455
|
+
return acc;
|
|
1456
|
+
}, {});
|
|
1457
|
+
return ids.map((id2) => dictionary[id2] ?? null);
|
|
1458
|
+
}
|
|
1459
|
+
const id = isString(a) ? a : a.id;
|
|
1460
|
+
if (isString(id)) {
|
|
1461
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
|
1462
|
+
try {
|
|
1463
|
+
const response = await getRecord({
|
|
1464
|
+
pathParams: {
|
|
1465
|
+
workspace: "{workspaceId}",
|
|
1466
|
+
dbBranchName: "{dbBranch}",
|
|
1467
|
+
tableName: __privateGet$4(this, _table),
|
|
1468
|
+
recordId: id
|
|
1469
|
+
},
|
|
1470
|
+
queryParams: { columns },
|
|
1471
|
+
...fetchProps
|
|
1472
|
+
});
|
|
1473
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
|
1474
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
|
1475
|
+
} catch (e) {
|
|
1476
|
+
if (isObject(e) && e.status === 404) {
|
|
1477
|
+
return null;
|
|
1478
|
+
}
|
|
1479
|
+
throw e;
|
|
1395
1480
|
}
|
|
1396
|
-
throw e;
|
|
1397
1481
|
}
|
|
1398
|
-
|
|
1399
|
-
|
|
1482
|
+
return null;
|
|
1483
|
+
});
|
|
1400
1484
|
}
|
|
1401
1485
|
async update(a, b, c) {
|
|
1402
|
-
|
|
1403
|
-
if (a
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1486
|
+
return __privateGet$4(this, _trace).call(this, "update", async () => {
|
|
1487
|
+
if (Array.isArray(a)) {
|
|
1488
|
+
if (a.length === 0)
|
|
1489
|
+
return [];
|
|
1490
|
+
if (a.length > 100) {
|
|
1491
|
+
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
|
1492
|
+
}
|
|
1493
|
+
const columns = isStringArray(b) ? b : ["*"];
|
|
1494
|
+
return Promise.all(a.map((object) => this.update(object, columns)));
|
|
1407
1495
|
}
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
}
|
|
1419
|
-
throw new Error("Invalid arguments for update method");
|
|
1496
|
+
if (isString(a) && isObject(b)) {
|
|
1497
|
+
const columns = isStringArray(c) ? c : void 0;
|
|
1498
|
+
return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b, columns);
|
|
1499
|
+
}
|
|
1500
|
+
if (isObject(a) && isString(a.id)) {
|
|
1501
|
+
const columns = isStringArray(b) ? b : void 0;
|
|
1502
|
+
return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
|
1503
|
+
}
|
|
1504
|
+
throw new Error("Invalid arguments for update method");
|
|
1505
|
+
});
|
|
1420
1506
|
}
|
|
1421
1507
|
async createOrUpdate(a, b, c) {
|
|
1422
|
-
|
|
1423
|
-
if (a
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1508
|
+
return __privateGet$4(this, _trace).call(this, "createOrUpdate", async () => {
|
|
1509
|
+
if (Array.isArray(a)) {
|
|
1510
|
+
if (a.length === 0)
|
|
1511
|
+
return [];
|
|
1512
|
+
if (a.length > 100) {
|
|
1513
|
+
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
|
1514
|
+
}
|
|
1515
|
+
const columns = isStringArray(b) ? b : ["*"];
|
|
1516
|
+
return Promise.all(a.map((object) => this.createOrUpdate(object, columns)));
|
|
1427
1517
|
}
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
}
|
|
1439
|
-
throw new Error("Invalid arguments for createOrUpdate method");
|
|
1518
|
+
if (isString(a) && isObject(b)) {
|
|
1519
|
+
const columns = isStringArray(c) ? c : void 0;
|
|
1520
|
+
return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b, columns);
|
|
1521
|
+
}
|
|
1522
|
+
if (isObject(a) && isString(a.id)) {
|
|
1523
|
+
const columns = isStringArray(c) ? c : void 0;
|
|
1524
|
+
return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
|
1525
|
+
}
|
|
1526
|
+
throw new Error("Invalid arguments for createOrUpdate method");
|
|
1527
|
+
});
|
|
1440
1528
|
}
|
|
1441
1529
|
async delete(a) {
|
|
1442
|
-
|
|
1443
|
-
if (a
|
|
1530
|
+
return __privateGet$4(this, _trace).call(this, "delete", async () => {
|
|
1531
|
+
if (Array.isArray(a)) {
|
|
1532
|
+
if (a.length === 0)
|
|
1533
|
+
return;
|
|
1534
|
+
if (a.length > 100) {
|
|
1535
|
+
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
|
1536
|
+
}
|
|
1537
|
+
await Promise.all(a.map((id) => this.delete(id)));
|
|
1444
1538
|
return;
|
|
1445
|
-
if (a.length > 100) {
|
|
1446
|
-
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
|
1447
1539
|
}
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
}
|
|
1459
|
-
throw new Error("Invalid arguments for delete method");
|
|
1540
|
+
if (isString(a)) {
|
|
1541
|
+
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a);
|
|
1542
|
+
return;
|
|
1543
|
+
}
|
|
1544
|
+
if (isObject(a) && isString(a.id)) {
|
|
1545
|
+
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id);
|
|
1546
|
+
return;
|
|
1547
|
+
}
|
|
1548
|
+
throw new Error("Invalid arguments for delete method");
|
|
1549
|
+
});
|
|
1460
1550
|
}
|
|
1461
1551
|
async search(query, options = {}) {
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1552
|
+
return __privateGet$4(this, _trace).call(this, "search", async () => {
|
|
1553
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
|
1554
|
+
const { records } = await searchTable({
|
|
1555
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
|
1556
|
+
body: {
|
|
1557
|
+
query,
|
|
1558
|
+
fuzziness: options.fuzziness,
|
|
1559
|
+
prefix: options.prefix,
|
|
1560
|
+
highlight: options.highlight,
|
|
1561
|
+
filter: options.filter,
|
|
1562
|
+
boosters: options.boosters
|
|
1563
|
+
},
|
|
1564
|
+
...fetchProps
|
|
1565
|
+
});
|
|
1566
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
|
1567
|
+
return records.map((item) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), item));
|
|
1474
1568
|
});
|
|
1475
|
-
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
|
1476
|
-
return records.map((item) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), item));
|
|
1477
1569
|
}
|
|
1478
1570
|
async query(query) {
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1571
|
+
return __privateGet$4(this, _trace).call(this, "query", async () => {
|
|
1572
|
+
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
|
1573
|
+
if (cacheQuery)
|
|
1574
|
+
return new Page(query, cacheQuery.meta, cacheQuery.records);
|
|
1575
|
+
const data = query.getQueryOptions();
|
|
1576
|
+
const body = {
|
|
1577
|
+
filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
|
|
1578
|
+
sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
|
|
1579
|
+
page: data.pagination,
|
|
1580
|
+
columns: data.columns
|
|
1581
|
+
};
|
|
1582
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
|
1583
|
+
const { meta, records: objects } = await queryTable({
|
|
1584
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
|
1585
|
+
body,
|
|
1586
|
+
...fetchProps
|
|
1587
|
+
});
|
|
1588
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
|
1589
|
+
const records = objects.map((record) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), record));
|
|
1590
|
+
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
|
1591
|
+
return new Page(query, meta, records);
|
|
1494
1592
|
});
|
|
1495
|
-
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
|
1496
|
-
const records = objects.map((record) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), record));
|
|
1497
|
-
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
|
1498
|
-
return new Page(query, meta, records);
|
|
1499
1593
|
}
|
|
1500
1594
|
}
|
|
1501
1595
|
_table = new WeakMap();
|
|
@@ -1503,6 +1597,7 @@ _getFetchProps = new WeakMap();
|
|
|
1503
1597
|
_db = new WeakMap();
|
|
1504
1598
|
_cache = new WeakMap();
|
|
1505
1599
|
_schemaTables$2 = new WeakMap();
|
|
1600
|
+
_trace = new WeakMap();
|
|
1506
1601
|
_insertRecordWithoutId = new WeakSet();
|
|
1507
1602
|
insertRecordWithoutId_fn = async function(object, columns = ["*"]) {
|
|
1508
1603
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
|
@@ -1723,18 +1818,25 @@ class SimpleCache {
|
|
|
1723
1818
|
}
|
|
1724
1819
|
_map = new WeakMap();
|
|
1725
1820
|
|
|
1726
|
-
const
|
|
1727
|
-
const
|
|
1728
|
-
const
|
|
1729
|
-
const
|
|
1730
|
-
const
|
|
1731
|
-
const
|
|
1821
|
+
const greaterThan = (value) => ({ $gt: value });
|
|
1822
|
+
const gt = greaterThan;
|
|
1823
|
+
const greaterThanEquals = (value) => ({ $ge: value });
|
|
1824
|
+
const greaterEquals = greaterThanEquals;
|
|
1825
|
+
const gte = greaterThanEquals;
|
|
1826
|
+
const ge = greaterThanEquals;
|
|
1827
|
+
const lessThan = (value) => ({ $lt: value });
|
|
1828
|
+
const lt = lessThan;
|
|
1829
|
+
const lessThanEquals = (value) => ({ $le: value });
|
|
1830
|
+
const lessEquals = lessThanEquals;
|
|
1831
|
+
const lte = lessThanEquals;
|
|
1832
|
+
const le = lessThanEquals;
|
|
1732
1833
|
const exists = (column) => ({ $exists: column });
|
|
1733
1834
|
const notExists = (column) => ({ $notExists: column });
|
|
1734
1835
|
const startsWith = (value) => ({ $startsWith: value });
|
|
1735
1836
|
const endsWith = (value) => ({ $endsWith: value });
|
|
1736
1837
|
const pattern = (value) => ({ $pattern: value });
|
|
1737
1838
|
const is = (value) => ({ $is: value });
|
|
1839
|
+
const equals = is;
|
|
1738
1840
|
const isNot = (value) => ({ $isNot: value });
|
|
1739
1841
|
const contains = (value) => ({ $contains: value });
|
|
1740
1842
|
const includes = (value) => ({ $includes: value });
|
|
@@ -1911,7 +2013,8 @@ async function resolveXataBranch(gitBranch, options) {
|
|
|
1911
2013
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
|
1912
2014
|
workspacesApiUrl: `${protocol}//${host}`,
|
|
1913
2015
|
pathParams: { dbName, workspace },
|
|
1914
|
-
queryParams: { gitBranch, fallbackBranch }
|
|
2016
|
+
queryParams: { gitBranch, fallbackBranch },
|
|
2017
|
+
trace: defaultTrace
|
|
1915
2018
|
});
|
|
1916
2019
|
return branch;
|
|
1917
2020
|
}
|
|
@@ -1935,7 +2038,8 @@ async function getDatabaseBranch(branch, options) {
|
|
|
1935
2038
|
apiUrl: databaseURL,
|
|
1936
2039
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
|
1937
2040
|
workspacesApiUrl: `${protocol}//${host}`,
|
|
1938
|
-
pathParams: { dbBranchName, workspace }
|
|
2041
|
+
pathParams: { dbBranchName, workspace },
|
|
2042
|
+
trace: defaultTrace
|
|
1939
2043
|
});
|
|
1940
2044
|
} catch (err) {
|
|
1941
2045
|
if (isObject(err) && err.status === 404)
|
|
@@ -1987,7 +2091,8 @@ const buildClient = (plugins) => {
|
|
|
1987
2091
|
__privateSet(this, _options, safeOptions);
|
|
1988
2092
|
const pluginOptions = {
|
|
1989
2093
|
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
|
1990
|
-
cache: safeOptions.cache
|
|
2094
|
+
cache: safeOptions.cache,
|
|
2095
|
+
trace: safeOptions.trace
|
|
1991
2096
|
};
|
|
1992
2097
|
const db = new SchemaPlugin(schemaTables).build(pluginOptions);
|
|
1993
2098
|
const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
|
|
@@ -2016,12 +2121,16 @@ const buildClient = (plugins) => {
|
|
|
2016
2121
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
|
2017
2122
|
const apiKey = options?.apiKey || getAPIKey();
|
|
2018
2123
|
const cache = options?.cache ?? new SimpleCache({ defaultQueryTTL: 0 });
|
|
2124
|
+
const trace = options?.trace ?? defaultTrace;
|
|
2019
2125
|
const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
|
2020
|
-
if (!
|
|
2021
|
-
throw new Error("
|
|
2126
|
+
if (!apiKey) {
|
|
2127
|
+
throw new Error("Option apiKey is required");
|
|
2128
|
+
}
|
|
2129
|
+
if (!databaseURL) {
|
|
2130
|
+
throw new Error("Option databaseURL is required");
|
|
2022
2131
|
}
|
|
2023
|
-
return { fetch, databaseURL, apiKey, branch, cache };
|
|
2024
|
-
}, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({ fetch, apiKey, databaseURL, branch }) {
|
|
2132
|
+
return { fetch, databaseURL, apiKey, branch, cache, trace };
|
|
2133
|
+
}, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({ fetch, apiKey, databaseURL, branch, trace }) {
|
|
2025
2134
|
const branchValue = await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, branch);
|
|
2026
2135
|
if (!branchValue)
|
|
2027
2136
|
throw new Error("Unable to resolve branch value");
|
|
@@ -2033,7 +2142,8 @@ const buildClient = (plugins) => {
|
|
|
2033
2142
|
const hasBranch = params.dbBranchName ?? params.branch;
|
|
2034
2143
|
const newPath = path.replace(/^\/db\/[^/]+/, hasBranch ? `:${branchValue}` : "");
|
|
2035
2144
|
return databaseURL + newPath;
|
|
2036
|
-
}
|
|
2145
|
+
},
|
|
2146
|
+
trace
|
|
2037
2147
|
};
|
|
2038
2148
|
}, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
|
|
2039
2149
|
if (__privateGet(this, _branch))
|
|
@@ -2056,6 +2166,88 @@ const buildClient = (plugins) => {
|
|
|
2056
2166
|
class BaseClient extends buildClient() {
|
|
2057
2167
|
}
|
|
2058
2168
|
|
|
2169
|
+
const META = "__";
|
|
2170
|
+
const VALUE = "___";
|
|
2171
|
+
class Serializer {
|
|
2172
|
+
constructor() {
|
|
2173
|
+
this.classes = {};
|
|
2174
|
+
}
|
|
2175
|
+
add(clazz) {
|
|
2176
|
+
this.classes[clazz.name] = clazz;
|
|
2177
|
+
}
|
|
2178
|
+
toJSON(data) {
|
|
2179
|
+
function visit(obj) {
|
|
2180
|
+
if (Array.isArray(obj))
|
|
2181
|
+
return obj.map(visit);
|
|
2182
|
+
const type = typeof obj;
|
|
2183
|
+
if (type === "undefined")
|
|
2184
|
+
return { [META]: "undefined" };
|
|
2185
|
+
if (type === "bigint")
|
|
2186
|
+
return { [META]: "bigint", [VALUE]: obj.toString() };
|
|
2187
|
+
if (obj === null || type !== "object")
|
|
2188
|
+
return obj;
|
|
2189
|
+
const constructor = obj.constructor;
|
|
2190
|
+
const o = { [META]: constructor.name };
|
|
2191
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
2192
|
+
o[key] = visit(value);
|
|
2193
|
+
}
|
|
2194
|
+
if (constructor === Date)
|
|
2195
|
+
o[VALUE] = obj.toISOString();
|
|
2196
|
+
if (constructor === Map)
|
|
2197
|
+
o[VALUE] = Object.fromEntries(obj);
|
|
2198
|
+
if (constructor === Set)
|
|
2199
|
+
o[VALUE] = [...obj];
|
|
2200
|
+
return o;
|
|
2201
|
+
}
|
|
2202
|
+
return JSON.stringify(visit(data));
|
|
2203
|
+
}
|
|
2204
|
+
fromJSON(json) {
|
|
2205
|
+
return JSON.parse(json, (key, value) => {
|
|
2206
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
2207
|
+
const { [META]: clazz, [VALUE]: val, ...rest } = value;
|
|
2208
|
+
const constructor = this.classes[clazz];
|
|
2209
|
+
if (constructor) {
|
|
2210
|
+
return Object.assign(Object.create(constructor.prototype), rest);
|
|
2211
|
+
}
|
|
2212
|
+
if (clazz === "Date")
|
|
2213
|
+
return new Date(val);
|
|
2214
|
+
if (clazz === "Set")
|
|
2215
|
+
return new Set(val);
|
|
2216
|
+
if (clazz === "Map")
|
|
2217
|
+
return new Map(Object.entries(val));
|
|
2218
|
+
if (clazz === "bigint")
|
|
2219
|
+
return BigInt(val);
|
|
2220
|
+
if (clazz === "undefined")
|
|
2221
|
+
return void 0;
|
|
2222
|
+
return rest;
|
|
2223
|
+
}
|
|
2224
|
+
return value;
|
|
2225
|
+
});
|
|
2226
|
+
}
|
|
2227
|
+
}
|
|
2228
|
+
const defaultSerializer = new Serializer();
|
|
2229
|
+
const serialize = (data) => {
|
|
2230
|
+
return defaultSerializer.toJSON(data);
|
|
2231
|
+
};
|
|
2232
|
+
const deserialize = (json) => {
|
|
2233
|
+
return defaultSerializer.fromJSON(json);
|
|
2234
|
+
};
|
|
2235
|
+
|
|
2236
|
+
function buildWorkerRunner(config) {
|
|
2237
|
+
return function xataWorker(name, _worker) {
|
|
2238
|
+
return async (...args) => {
|
|
2239
|
+
const url = process.env.NODE_ENV === "development" ? `http://localhost:64749/${name}` : `https://dispatcher.xata.workers.dev/${config.workspace}/${config.worker}/${name}`;
|
|
2240
|
+
const result = await fetch(url, {
|
|
2241
|
+
method: "POST",
|
|
2242
|
+
headers: { "Content-Type": "application/json" },
|
|
2243
|
+
body: serialize({ args })
|
|
2244
|
+
});
|
|
2245
|
+
const text = await result.text();
|
|
2246
|
+
return deserialize(text);
|
|
2247
|
+
};
|
|
2248
|
+
};
|
|
2249
|
+
}
|
|
2250
|
+
|
|
2059
2251
|
class XataError extends Error {
|
|
2060
2252
|
constructor(message, status) {
|
|
2061
2253
|
super(message);
|
|
@@ -2063,5 +2255,5 @@ class XataError extends Error {
|
|
|
2063
2255
|
}
|
|
2064
2256
|
}
|
|
2065
2257
|
|
|
2066
|
-
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, updateWorkspaceMemberInvite, updateWorkspaceMemberRole, upsertRecordWithID };
|
|
2258
|
+
export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, RecordArray, Repository, RestRepository, SchemaPlugin, SearchPlugin, Serializer, SimpleCache, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addGitBranchesEntry, addTableColumn, buildClient, buildWorkerRunner, bulkInsertTableRecords, cancelWorkspaceMemberInvite, contains, createBranch, createDatabase, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, deserialize, endsWith, equals, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseMetadata, getDatabaseURL, getGitBranchesMapping, getRecord, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getWorkspace, getWorkspaceMembersList, getWorkspacesList, greaterEquals, greaterThan, greaterThanEquals, gt, gte, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isCursorPaginationOptions, isIdentifiable, isNot, isXataRecord, le, lessEquals, lessThan, lessThanEquals, lt, lte, notExists, operationsByTag, pattern, queryTable, removeGitBranchesEntry, removeWorkspaceMember, resendWorkspaceMemberInvite, resolveBranch, searchBranch, searchTable, serialize, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberInvite, updateWorkspaceMemberRole, upsertRecordWithID };
|
|
2067
2259
|
//# sourceMappingURL=index.mjs.map
|