@xata.io/client 0.0.0-alpha.vf73045e → 0.0.0-alpha.vf79e7d8

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/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
  }
@@ -122,13 +146,13 @@ function getFetchImplementation(userFetch) {
122
146
  const fetchImpl = userFetch ?? globalFetch;
123
147
  if (!fetchImpl) {
124
148
  throw new Error(
125
- `The \`fetch\` option passed to the Xata client is resolving to a falsy value and may not be correctly imported.`
149
+ `Couldn't find \`fetch\`. Install a fetch implementation such as \`node-fetch\` and pass it explicitly.`
126
150
  );
127
151
  }
128
152
  return fetchImpl;
129
153
  }
130
154
 
131
- const VERSION = "0.0.0-alpha.vf73045e";
155
+ const VERSION = "0.0.0-alpha.vf79e7d8";
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
- const baseUrl = buildBaseUrl({ path, workspacesApiUrl, pathParams, apiUrl });
213
- const fullUrl = resolveUrl(baseUrl, queryParams, pathParams);
214
- const url = fullUrl.includes("localhost") ? fullUrl.replace(/^[^.]+\./, "http://") : fullUrl;
215
- const response = await fetchImpl(url, {
216
- method: method.toUpperCase(),
217
- body: body ? JSON.stringify(body) : void 0,
218
- headers: {
219
- "Content-Type": "application/json",
220
- "User-Agent": `Xata client-ts/${VERSION}`,
221
- ...headers,
222
- ...hostHeader(fullUrl),
223
- Authorization: `Bearer ${apiKey}`
224
- }
225
- });
226
- if (response.status === 204) {
227
- return {};
228
- }
229
- const requestId = response.headers?.get("x-request-id") ?? void 0;
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 jsonResponse = await response.json();
232
- if (response.ok) {
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
- throw new FetcherError(response.status, error, requestId);
289
+ return {};
238
290
  }
239
291
  }
240
292
 
@@ -517,9 +569,9 @@ const operationsByTag = {
517
569
  };
518
570
 
519
571
  function getHostUrl(provider, type) {
520
- if (isValidAlias(provider)) {
572
+ if (isHostProviderAlias(provider)) {
521
573
  return providers[provider][type];
522
- } else if (isValidBuilder(provider)) {
574
+ } else if (isHostProviderBuilder(provider)) {
523
575
  return provider[type];
524
576
  }
525
577
  throw new Error("Invalid API provider");
@@ -534,10 +586,10 @@ const providers = {
534
586
  workspaces: "https://{workspaceId}.staging.xatabase.co"
535
587
  }
536
588
  };
537
- function isValidAlias(alias) {
589
+ function isHostProviderAlias(alias) {
538
590
  return isString(alias) && Object.keys(providers).includes(alias);
539
591
  }
540
- function isValidBuilder(builder) {
592
+ function isHostProviderBuilder(builder) {
541
593
  return isObject(builder) && isString(builder.main) && isString(builder.workspaces);
542
594
  }
543
595
 
@@ -565,7 +617,8 @@ class XataApiClient {
565
617
  __privateAdd$7(this, _extraProps, void 0);
566
618
  __privateAdd$7(this, _namespaces, {});
567
619
  const provider = options.host ?? "production";
568
- const apiKey = options?.apiKey ?? getAPIKey();
620
+ const apiKey = options.apiKey ?? getAPIKey();
621
+ const trace = options.trace ?? defaultTrace;
569
622
  if (!apiKey) {
570
623
  throw new Error("Could not resolve a valid apiKey");
571
624
  }
@@ -573,7 +626,8 @@ class XataApiClient {
573
626
  apiUrl: getHostUrl(provider, "main"),
574
627
  workspacesApiUrl: getHostUrl(provider, "workspaces"),
575
628
  fetchImpl: getFetchImplementation(options.fetch),
576
- apiKey
629
+ apiKey,
630
+ trace
577
631
  });
578
632
  }
579
633
  get user() {
@@ -1182,11 +1236,12 @@ const _Query = class {
1182
1236
  }
1183
1237
  filter(a, b) {
1184
1238
  if (arguments.length === 1) {
1185
- const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
1239
+ const constraints = Object.entries(a ?? {}).map(([column, constraint]) => ({ [column]: constraint }));
1186
1240
  const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat(constraints));
1187
1241
  return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
1188
1242
  } else {
1189
- const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
1243
+ const constraints = isDefined(a) && isDefined(b) ? [{ [a]: b }] : void 0;
1244
+ const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat(constraints));
1190
1245
  return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
1191
1246
  }
1192
1247
  }
@@ -1326,7 +1381,7 @@ var __privateMethod$2 = (obj, member, method) => {
1326
1381
  __accessCheck$4(obj, member, "access private method");
1327
1382
  return method;
1328
1383
  };
1329
- 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;
1384
+ 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;
1330
1385
  class Repository extends Query {
1331
1386
  }
1332
1387
  class RestRepository extends Query {
@@ -1346,168 +1401,193 @@ class RestRepository extends Query {
1346
1401
  __privateAdd$4(this, _db, void 0);
1347
1402
  __privateAdd$4(this, _cache, void 0);
1348
1403
  __privateAdd$4(this, _schemaTables$2, void 0);
1404
+ __privateAdd$4(this, _trace, void 0);
1349
1405
  __privateSet$4(this, _table, options.table);
1350
1406
  __privateSet$4(this, _getFetchProps, options.pluginOptions.getFetchProps);
1351
1407
  __privateSet$4(this, _db, options.db);
1352
1408
  __privateSet$4(this, _cache, options.pluginOptions.cache);
1353
1409
  __privateSet$4(this, _schemaTables$2, options.schemaTables);
1410
+ const trace = options.pluginOptions.trace ?? defaultTrace;
1411
+ __privateSet$4(this, _trace, async (name, fn, options2 = {}) => {
1412
+ return trace(name, fn, {
1413
+ ...options2,
1414
+ [TraceAttributes.TABLE]: __privateGet$4(this, _table),
1415
+ [TraceAttributes.VERSION]: VERSION
1416
+ });
1417
+ });
1354
1418
  }
1355
1419
  async create(a, b, c) {
1356
- if (Array.isArray(a)) {
1357
- if (a.length === 0)
1358
- return [];
1359
- const columns = isStringArray(b) ? b : void 0;
1360
- return __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a, columns);
1361
- }
1362
- if (isString(a) && isObject(b)) {
1363
- if (a === "")
1364
- throw new Error("The id can't be empty");
1365
- const columns = isStringArray(c) ? c : void 0;
1366
- return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b, columns);
1367
- }
1368
- if (isObject(a) && isString(a.id)) {
1369
- if (a.id === "")
1370
- throw new Error("The id can't be empty");
1371
- const columns = isStringArray(b) ? b : void 0;
1372
- return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 }, columns);
1373
- }
1374
- if (isObject(a)) {
1375
- const columns = isStringArray(b) ? b : void 0;
1376
- return __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a, columns);
1377
- }
1378
- throw new Error("Invalid arguments for create method");
1420
+ return __privateGet$4(this, _trace).call(this, "create", async () => {
1421
+ if (Array.isArray(a)) {
1422
+ if (a.length === 0)
1423
+ return [];
1424
+ const columns = isStringArray(b) ? b : void 0;
1425
+ return __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a, columns);
1426
+ }
1427
+ if (isString(a) && isObject(b)) {
1428
+ if (a === "")
1429
+ throw new Error("The id can't be empty");
1430
+ const columns = isStringArray(c) ? c : void 0;
1431
+ return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b, columns);
1432
+ }
1433
+ if (isObject(a) && isString(a.id)) {
1434
+ if (a.id === "")
1435
+ throw new Error("The id can't be empty");
1436
+ const columns = isStringArray(b) ? b : void 0;
1437
+ return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 }, columns);
1438
+ }
1439
+ if (isObject(a)) {
1440
+ const columns = isStringArray(b) ? b : void 0;
1441
+ return __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a, columns);
1442
+ }
1443
+ throw new Error("Invalid arguments for create method");
1444
+ });
1379
1445
  }
1380
1446
  async read(a, b) {
1381
- const columns = isStringArray(b) ? b : ["*"];
1382
- if (Array.isArray(a)) {
1383
- if (a.length === 0)
1384
- return [];
1385
- const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
1386
- const finalObjects = await this.getAll({ filter: { id: { $any: ids } }, columns });
1387
- const dictionary = finalObjects.reduce((acc, object) => {
1388
- acc[object.id] = object;
1389
- return acc;
1390
- }, {});
1391
- return ids.map((id2) => dictionary[id2] ?? null);
1392
- }
1393
- const id = isString(a) ? a : a.id;
1394
- if (isString(id)) {
1395
- const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1396
- try {
1397
- const response = await getRecord({
1398
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
1399
- queryParams: { columns },
1400
- ...fetchProps
1401
- });
1402
- const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1403
- return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
1404
- } catch (e) {
1405
- if (isObject(e) && e.status === 404) {
1406
- return null;
1447
+ return __privateGet$4(this, _trace).call(this, "read", async () => {
1448
+ const columns = isStringArray(b) ? b : ["*"];
1449
+ if (Array.isArray(a)) {
1450
+ if (a.length === 0)
1451
+ return [];
1452
+ const ids = a.map((item) => extractId(item));
1453
+ const finalObjects = await this.getAll({ filter: { id: { $any: compact(ids) } }, columns });
1454
+ const dictionary = finalObjects.reduce((acc, object) => {
1455
+ acc[object.id] = object;
1456
+ return acc;
1457
+ }, {});
1458
+ return ids.map((id2) => dictionary[id2 ?? ""] ?? null);
1459
+ }
1460
+ const id = extractId(a);
1461
+ if (id) {
1462
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1463
+ try {
1464
+ const response = await getRecord({
1465
+ pathParams: {
1466
+ workspace: "{workspaceId}",
1467
+ dbBranchName: "{dbBranch}",
1468
+ tableName: __privateGet$4(this, _table),
1469
+ recordId: id
1470
+ },
1471
+ queryParams: { columns },
1472
+ ...fetchProps
1473
+ });
1474
+ const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1475
+ return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
1476
+ } catch (e) {
1477
+ if (isObject(e) && e.status === 404) {
1478
+ return null;
1479
+ }
1480
+ throw e;
1407
1481
  }
1408
- throw e;
1409
1482
  }
1410
- }
1411
- return null;
1483
+ return null;
1484
+ });
1412
1485
  }
1413
1486
  async update(a, b, c) {
1414
- if (Array.isArray(a)) {
1415
- if (a.length === 0)
1416
- return [];
1417
- if (a.length > 100) {
1418
- console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
1487
+ return __privateGet$4(this, _trace).call(this, "update", async () => {
1488
+ if (Array.isArray(a)) {
1489
+ if (a.length === 0)
1490
+ return [];
1491
+ if (a.length > 100) {
1492
+ console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
1493
+ }
1494
+ const columns = isStringArray(b) ? b : ["*"];
1495
+ return Promise.all(a.map((object) => this.update(object, columns)));
1419
1496
  }
1420
- const columns = isStringArray(b) ? b : ["*"];
1421
- return Promise.all(a.map((object) => this.update(object, columns)));
1422
- }
1423
- if (isString(a) && isObject(b)) {
1424
- const columns = isStringArray(c) ? c : void 0;
1425
- return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b, columns);
1426
- }
1427
- if (isObject(a) && isString(a.id)) {
1428
- const columns = isStringArray(b) ? b : void 0;
1429
- return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
1430
- }
1431
- throw new Error("Invalid arguments for update method");
1497
+ if (isString(a) && isObject(b)) {
1498
+ const columns = isStringArray(c) ? c : void 0;
1499
+ return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b, columns);
1500
+ }
1501
+ if (isObject(a) && isString(a.id)) {
1502
+ const columns = isStringArray(b) ? b : void 0;
1503
+ return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
1504
+ }
1505
+ throw new Error("Invalid arguments for update method");
1506
+ });
1432
1507
  }
1433
1508
  async createOrUpdate(a, b, c) {
1434
- if (Array.isArray(a)) {
1435
- if (a.length === 0)
1436
- return [];
1437
- if (a.length > 100) {
1438
- console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
1509
+ return __privateGet$4(this, _trace).call(this, "createOrUpdate", async () => {
1510
+ if (Array.isArray(a)) {
1511
+ if (a.length === 0)
1512
+ return [];
1513
+ if (a.length > 100) {
1514
+ console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
1515
+ }
1516
+ const columns = isStringArray(b) ? b : ["*"];
1517
+ return Promise.all(a.map((object) => this.createOrUpdate(object, columns)));
1439
1518
  }
1440
- const columns = isStringArray(b) ? b : ["*"];
1441
- return Promise.all(a.map((object) => this.createOrUpdate(object, columns)));
1442
- }
1443
- if (isString(a) && isObject(b)) {
1444
- const columns = isStringArray(c) ? c : void 0;
1445
- return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b, columns);
1446
- }
1447
- if (isObject(a) && isString(a.id)) {
1448
- const columns = isStringArray(c) ? c : void 0;
1449
- return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
1450
- }
1451
- throw new Error("Invalid arguments for createOrUpdate method");
1452
- }
1453
- async delete(a) {
1454
- if (Array.isArray(a)) {
1455
- if (a.length === 0)
1456
- return;
1457
- if (a.length > 100) {
1458
- console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
1519
+ if (isString(a) && isObject(b)) {
1520
+ const columns = isStringArray(c) ? c : void 0;
1521
+ return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b, columns);
1459
1522
  }
1460
- await Promise.all(a.map((id) => this.delete(id)));
1461
- return;
1462
- }
1463
- if (isString(a)) {
1464
- await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a);
1465
- return;
1466
- }
1467
- if (isObject(a) && isString(a.id)) {
1468
- await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id);
1469
- return;
1470
- }
1471
- throw new Error("Invalid arguments for delete method");
1523
+ if (isObject(a) && isString(a.id)) {
1524
+ const columns = isStringArray(c) ? c : void 0;
1525
+ return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
1526
+ }
1527
+ throw new Error("Invalid arguments for createOrUpdate method");
1528
+ });
1529
+ }
1530
+ async delete(a, b) {
1531
+ return __privateGet$4(this, _trace).call(this, "delete", async () => {
1532
+ if (Array.isArray(a)) {
1533
+ if (a.length === 0)
1534
+ return [];
1535
+ if (a.length > 100) {
1536
+ console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
1537
+ }
1538
+ return Promise.all(a.map((id) => this.delete(id, b)));
1539
+ }
1540
+ if (isString(a)) {
1541
+ return __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a, b);
1542
+ }
1543
+ if (isObject(a) && isString(a.id)) {
1544
+ return __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id, b);
1545
+ }
1546
+ throw new Error("Invalid arguments for delete method");
1547
+ });
1472
1548
  }
1473
1549
  async search(query, options = {}) {
1474
- const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1475
- const { records } = await searchTable({
1476
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1477
- body: {
1478
- query,
1479
- fuzziness: options.fuzziness,
1480
- prefix: options.prefix,
1481
- highlight: options.highlight,
1482
- filter: options.filter,
1483
- boosters: options.boosters
1484
- },
1485
- ...fetchProps
1550
+ return __privateGet$4(this, _trace).call(this, "search", async () => {
1551
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1552
+ const { records } = await searchTable({
1553
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1554
+ body: {
1555
+ query,
1556
+ fuzziness: options.fuzziness,
1557
+ prefix: options.prefix,
1558
+ highlight: options.highlight,
1559
+ filter: options.filter,
1560
+ boosters: options.boosters
1561
+ },
1562
+ ...fetchProps
1563
+ });
1564
+ const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1565
+ return records.map((item) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), item));
1486
1566
  });
1487
- const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1488
- return records.map((item) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), item));
1489
1567
  }
1490
1568
  async query(query) {
1491
- const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
1492
- if (cacheQuery)
1493
- return new Page(query, cacheQuery.meta, cacheQuery.records);
1494
- const data = query.getQueryOptions();
1495
- const body = {
1496
- filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
1497
- sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
1498
- page: data.pagination,
1499
- columns: data.columns
1500
- };
1501
- const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1502
- const { meta, records: objects } = await queryTable({
1503
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1504
- body,
1505
- ...fetchProps
1569
+ return __privateGet$4(this, _trace).call(this, "query", async () => {
1570
+ const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
1571
+ if (cacheQuery)
1572
+ return new Page(query, cacheQuery.meta, cacheQuery.records);
1573
+ const data = query.getQueryOptions();
1574
+ const body = {
1575
+ filter: cleanFilter(data.filter),
1576
+ sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
1577
+ page: data.pagination,
1578
+ columns: data.columns
1579
+ };
1580
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1581
+ const { meta, records: objects } = await queryTable({
1582
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1583
+ body,
1584
+ ...fetchProps
1585
+ });
1586
+ const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1587
+ const records = objects.map((record) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), record));
1588
+ await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
1589
+ return new Page(query, meta, records);
1506
1590
  });
1507
- const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1508
- const records = objects.map((record) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), record));
1509
- await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
1510
- return new Page(query, meta, records);
1511
1591
  }
1512
1592
  }
1513
1593
  _table = new WeakMap();
@@ -1515,6 +1595,7 @@ _getFetchProps = new WeakMap();
1515
1595
  _db = new WeakMap();
1516
1596
  _cache = new WeakMap();
1517
1597
  _schemaTables$2 = new WeakMap();
1598
+ _trace = new WeakMap();
1518
1599
  _insertRecordWithoutId = new WeakSet();
1519
1600
  insertRecordWithoutId_fn = async function(object, columns = ["*"]) {
1520
1601
  const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
@@ -1570,14 +1651,21 @@ _updateRecordWithID = new WeakSet();
1570
1651
  updateRecordWithID_fn = async function(recordId, object, columns = ["*"]) {
1571
1652
  const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1572
1653
  const record = transformObjectLinks(object);
1573
- const response = await updateRecordWithID({
1574
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1575
- queryParams: { columns },
1576
- body: record,
1577
- ...fetchProps
1578
- });
1579
- const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1580
- return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
1654
+ try {
1655
+ const response = await updateRecordWithID({
1656
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1657
+ queryParams: { columns },
1658
+ body: record,
1659
+ ...fetchProps
1660
+ });
1661
+ const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1662
+ return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
1663
+ } catch (e) {
1664
+ if (isObject(e) && e.status === 404) {
1665
+ return null;
1666
+ }
1667
+ throw e;
1668
+ }
1581
1669
  };
1582
1670
  _upsertRecordWithID = new WeakSet();
1583
1671
  upsertRecordWithID_fn = async function(recordId, object, columns = ["*"]) {
@@ -1592,12 +1680,22 @@ upsertRecordWithID_fn = async function(recordId, object, columns = ["*"]) {
1592
1680
  return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
1593
1681
  };
1594
1682
  _deleteRecord = new WeakSet();
1595
- deleteRecord_fn = async function(recordId) {
1683
+ deleteRecord_fn = async function(recordId, columns = ["*"]) {
1596
1684
  const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1597
- await deleteRecord({
1598
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1599
- ...fetchProps
1600
- });
1685
+ try {
1686
+ const response = await deleteRecord({
1687
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1688
+ queryParams: { columns },
1689
+ ...fetchProps
1690
+ });
1691
+ const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1692
+ return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
1693
+ } catch (e) {
1694
+ if (isObject(e) && e.status === 404) {
1695
+ return null;
1696
+ }
1697
+ throw e;
1698
+ }
1601
1699
  };
1602
1700
  _setCacheQuery = new WeakSet();
1603
1701
  setCacheQuery_fn = async function(query, meta, records) {
@@ -1685,6 +1783,19 @@ const initObject = (db, schemaTables, table, object) => {
1685
1783
  function isResponseWithRecords(value) {
1686
1784
  return isObject(value) && Array.isArray(value.records);
1687
1785
  }
1786
+ function extractId(value) {
1787
+ if (isString(value))
1788
+ return value;
1789
+ if (isObject(value) && isString(value.id))
1790
+ return value.id;
1791
+ return void 0;
1792
+ }
1793
+ function cleanFilter(filter) {
1794
+ if (!filter)
1795
+ return void 0;
1796
+ const values = Object.values(filter).filter(Boolean).filter((value) => Array.isArray(value) ? value.length > 0 : true);
1797
+ return values.length > 0 ? filter : void 0;
1798
+ }
1688
1799
 
1689
1800
  var __accessCheck$3 = (obj, member, msg) => {
1690
1801
  if (!member.has(obj))
@@ -1735,18 +1846,25 @@ class SimpleCache {
1735
1846
  }
1736
1847
  _map = new WeakMap();
1737
1848
 
1738
- const gt = (value) => ({ $gt: value });
1739
- const ge = (value) => ({ $ge: value });
1740
- const gte = (value) => ({ $ge: value });
1741
- const lt = (value) => ({ $lt: value });
1742
- const lte = (value) => ({ $le: value });
1743
- const le = (value) => ({ $le: value });
1849
+ const greaterThan = (value) => ({ $gt: value });
1850
+ const gt = greaterThan;
1851
+ const greaterThanEquals = (value) => ({ $ge: value });
1852
+ const greaterEquals = greaterThanEquals;
1853
+ const gte = greaterThanEquals;
1854
+ const ge = greaterThanEquals;
1855
+ const lessThan = (value) => ({ $lt: value });
1856
+ const lt = lessThan;
1857
+ const lessThanEquals = (value) => ({ $le: value });
1858
+ const lessEquals = lessThanEquals;
1859
+ const lte = lessThanEquals;
1860
+ const le = lessThanEquals;
1744
1861
  const exists = (column) => ({ $exists: column });
1745
1862
  const notExists = (column) => ({ $notExists: column });
1746
1863
  const startsWith = (value) => ({ $startsWith: value });
1747
1864
  const endsWith = (value) => ({ $endsWith: value });
1748
1865
  const pattern = (value) => ({ $pattern: value });
1749
1866
  const is = (value) => ({ $is: value });
1867
+ const equals = is;
1750
1868
  const isNot = (value) => ({ $isNot: value });
1751
1869
  const contains = (value) => ({ $contains: value });
1752
1870
  const includes = (value) => ({ $includes: value });
@@ -1923,7 +2041,8 @@ async function resolveXataBranch(gitBranch, options) {
1923
2041
  fetchImpl: getFetchImplementation(options?.fetchImpl),
1924
2042
  workspacesApiUrl: `${protocol}//${host}`,
1925
2043
  pathParams: { dbName, workspace },
1926
- queryParams: { gitBranch, fallbackBranch }
2044
+ queryParams: { gitBranch, fallbackBranch },
2045
+ trace: defaultTrace
1927
2046
  });
1928
2047
  return branch;
1929
2048
  }
@@ -1947,7 +2066,8 @@ async function getDatabaseBranch(branch, options) {
1947
2066
  apiUrl: databaseURL,
1948
2067
  fetchImpl: getFetchImplementation(options?.fetchImpl),
1949
2068
  workspacesApiUrl: `${protocol}//${host}`,
1950
- pathParams: { dbBranchName, workspace }
2069
+ pathParams: { dbBranchName, workspace },
2070
+ trace: defaultTrace
1951
2071
  });
1952
2072
  } catch (err) {
1953
2073
  if (isObject(err) && err.status === 404)
@@ -1999,7 +2119,8 @@ const buildClient = (plugins) => {
1999
2119
  __privateSet(this, _options, safeOptions);
2000
2120
  const pluginOptions = {
2001
2121
  getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
2002
- cache: safeOptions.cache
2122
+ cache: safeOptions.cache,
2123
+ trace: safeOptions.trace
2003
2124
  };
2004
2125
  const db = new SchemaPlugin(schemaTables).build(pluginOptions);
2005
2126
  const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
@@ -2028,12 +2149,16 @@ const buildClient = (plugins) => {
2028
2149
  const databaseURL = options?.databaseURL || getDatabaseURL();
2029
2150
  const apiKey = options?.apiKey || getAPIKey();
2030
2151
  const cache = options?.cache ?? new SimpleCache({ defaultQueryTTL: 0 });
2152
+ const trace = options?.trace ?? defaultTrace;
2031
2153
  const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
2032
- if (!databaseURL || !apiKey) {
2033
- throw new Error("Options databaseURL and apiKey are required");
2154
+ if (!apiKey) {
2155
+ throw new Error("Option apiKey is required");
2034
2156
  }
2035
- return { fetch, databaseURL, apiKey, branch, cache };
2036
- }, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({ fetch, apiKey, databaseURL, branch }) {
2157
+ if (!databaseURL) {
2158
+ throw new Error("Option databaseURL is required");
2159
+ }
2160
+ return { fetch, databaseURL, apiKey, branch, cache, trace };
2161
+ }, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({ fetch, apiKey, databaseURL, branch, trace }) {
2037
2162
  const branchValue = await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, branch);
2038
2163
  if (!branchValue)
2039
2164
  throw new Error("Unable to resolve branch value");
@@ -2045,7 +2170,8 @@ const buildClient = (plugins) => {
2045
2170
  const hasBranch = params.dbBranchName ?? params.branch;
2046
2171
  const newPath = path.replace(/^\/db\/[^/]+/, hasBranch ? `:${branchValue}` : "");
2047
2172
  return databaseURL + newPath;
2048
- }
2173
+ },
2174
+ trace
2049
2175
  };
2050
2176
  }, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
2051
2177
  if (__privateGet(this, _branch))
@@ -2157,5 +2283,5 @@ class XataError extends Error {
2157
2283
  }
2158
2284
  }
2159
2285
 
2160
- 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, 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, 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, serialize, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberInvite, updateWorkspaceMemberRole, upsertRecordWithID };
2286
+ 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 };
2161
2287
  //# sourceMappingURL=index.mjs.map