@xata.io/client 0.15.0 → 0.16.2

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.cjs CHANGED
@@ -20,6 +20,30 @@ function _interopNamespace(e) {
20
20
  return Object.freeze(n);
21
21
  }
22
22
 
23
+ const defaultTrace = async (_name, fn, _options) => {
24
+ return await fn({
25
+ setAttributes: () => {
26
+ return;
27
+ },
28
+ onError: () => {
29
+ return;
30
+ }
31
+ });
32
+ };
33
+ const TraceAttributes = {
34
+ VERSION: "xata.sdk.version",
35
+ TABLE: "xata.table",
36
+ HTTP_REQUEST_ID: "http.request_id",
37
+ HTTP_STATUS_CODE: "http.status_code",
38
+ HTTP_HOST: "http.host",
39
+ HTTP_SCHEME: "http.scheme",
40
+ HTTP_USER_AGENT: "http.user_agent",
41
+ HTTP_METHOD: "http.method",
42
+ HTTP_URL: "http.url",
43
+ HTTP_ROUTE: "http.route",
44
+ HTTP_TARGET: "http.target"
45
+ };
46
+
23
47
  function notEmpty(value) {
24
48
  return value !== null && value !== void 0;
25
49
  }
@@ -35,6 +59,9 @@ function isDefined(value) {
35
59
  function isString(value) {
36
60
  return isDefined(value) && typeof value === "string";
37
61
  }
62
+ function isStringArray(value) {
63
+ return isDefined(value) && Array.isArray(value) && value.every(isString);
64
+ }
38
65
  function toBase64(value) {
39
66
  try {
40
67
  return btoa(value);
@@ -140,12 +167,14 @@ function getFetchImplementation(userFetch) {
140
167
  const globalFetch = typeof fetch !== "undefined" ? fetch : void 0;
141
168
  const fetchImpl = userFetch ?? globalFetch;
142
169
  if (!fetchImpl) {
143
- throw new Error(`The \`fetch\` option passed to the Xata client is resolving to a falsy value and may not be correctly imported.`);
170
+ throw new Error(
171
+ `Couldn't find \`fetch\`. Install a fetch implementation such as \`node-fetch\` and pass it explicitly.`
172
+ );
144
173
  }
145
174
  return fetchImpl;
146
175
  }
147
176
 
148
- const VERSION = "0.15.0";
177
+ const VERSION = "0.16.2";
149
178
 
150
179
  class ErrorWithCause extends Error {
151
180
  constructor(message, options) {
@@ -224,34 +253,62 @@ async function fetch$1({
224
253
  fetchImpl,
225
254
  apiKey,
226
255
  apiUrl,
227
- workspacesApiUrl
256
+ workspacesApiUrl,
257
+ trace
228
258
  }) {
229
- const baseUrl = buildBaseUrl({ path, workspacesApiUrl, pathParams, apiUrl });
230
- const fullUrl = resolveUrl(baseUrl, queryParams, pathParams);
231
- const url = fullUrl.includes("localhost") ? fullUrl.replace(/^[^.]+\./, "http://") : fullUrl;
232
- const response = await fetchImpl(url, {
233
- method: method.toUpperCase(),
234
- body: body ? JSON.stringify(body) : void 0,
235
- headers: {
236
- "Content-Type": "application/json",
237
- "User-Agent": `Xata client-ts/${VERSION}`,
238
- ...headers,
239
- ...hostHeader(fullUrl),
240
- Authorization: `Bearer ${apiKey}`
241
- }
242
- });
243
- if (response.status === 204) {
244
- return {};
245
- }
246
- const requestId = response.headers?.get("x-request-id") ?? void 0;
259
+ return trace(
260
+ `${method.toUpperCase()} ${path}`,
261
+ async ({ setAttributes, onError }) => {
262
+ const baseUrl = buildBaseUrl({ path, workspacesApiUrl, pathParams, apiUrl });
263
+ const fullUrl = resolveUrl(baseUrl, queryParams, pathParams);
264
+ const url = fullUrl.includes("localhost") ? fullUrl.replace(/^[^.]+\./, "http://") : fullUrl;
265
+ setAttributes({
266
+ [TraceAttributes.HTTP_URL]: url,
267
+ [TraceAttributes.HTTP_TARGET]: resolveUrl(path, queryParams, pathParams)
268
+ });
269
+ const response = await fetchImpl(url, {
270
+ method: method.toUpperCase(),
271
+ body: body ? JSON.stringify(body) : void 0,
272
+ headers: {
273
+ "Content-Type": "application/json",
274
+ "User-Agent": `Xata client-ts/${VERSION}`,
275
+ ...headers,
276
+ ...hostHeader(fullUrl),
277
+ Authorization: `Bearer ${apiKey}`
278
+ }
279
+ });
280
+ if (response.status === 204) {
281
+ return {};
282
+ }
283
+ const { host, protocol } = parseUrl(response.url);
284
+ const requestId = response.headers?.get("x-request-id") ?? void 0;
285
+ setAttributes({
286
+ [TraceAttributes.HTTP_REQUEST_ID]: requestId,
287
+ [TraceAttributes.HTTP_STATUS_CODE]: response.status,
288
+ [TraceAttributes.HTTP_HOST]: host,
289
+ [TraceAttributes.HTTP_SCHEME]: protocol?.replace(":", "")
290
+ });
291
+ try {
292
+ const jsonResponse = await response.json();
293
+ if (response.ok) {
294
+ return jsonResponse;
295
+ }
296
+ throw new FetcherError(response.status, jsonResponse, requestId);
297
+ } catch (error) {
298
+ const fetcherError = new FetcherError(response.status, error, requestId);
299
+ onError(fetcherError.message);
300
+ throw fetcherError;
301
+ }
302
+ },
303
+ { [TraceAttributes.HTTP_METHOD]: method.toUpperCase(), [TraceAttributes.HTTP_ROUTE]: path }
304
+ );
305
+ }
306
+ function parseUrl(url) {
247
307
  try {
248
- const jsonResponse = await response.json();
249
- if (response.ok) {
250
- return jsonResponse;
251
- }
252
- throw new FetcherError(response.status, jsonResponse, requestId);
308
+ const { host, protocol } = new URL(url);
309
+ return { host, protocol };
253
310
  } catch (error) {
254
- throw new FetcherError(response.status, error, requestId);
311
+ return {};
255
312
  }
256
313
  }
257
314
 
@@ -346,6 +403,11 @@ const deleteDatabase = (variables) => fetch$1({
346
403
  method: "delete",
347
404
  ...variables
348
405
  });
406
+ const getDatabaseMetadata = (variables) => fetch$1({
407
+ url: "/dbs/{dbName}/metadata",
408
+ method: "get",
409
+ ...variables
410
+ });
349
411
  const getGitBranchesMapping = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "get", ...variables });
350
412
  const addGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "post", ...variables });
351
413
  const removeGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "delete", ...variables });
@@ -359,11 +421,7 @@ const getBranchDetails = (variables) => fetch$1({
359
421
  method: "get",
360
422
  ...variables
361
423
  });
362
- const createBranch = (variables) => fetch$1({
363
- url: "/db/{dbBranchName}",
364
- method: "put",
365
- ...variables
366
- });
424
+ const createBranch = (variables) => fetch$1({ url: "/db/{dbBranchName}", method: "put", ...variables });
367
425
  const deleteBranch = (variables) => fetch$1({
368
426
  url: "/db/{dbBranchName}",
369
427
  method: "delete",
@@ -437,11 +495,7 @@ const updateColumn = (variables) => fetch$1({
437
495
  method: "patch",
438
496
  ...variables
439
497
  });
440
- const insertRecord = (variables) => fetch$1({
441
- url: "/db/{dbBranchName}/tables/{tableName}/data",
442
- method: "post",
443
- ...variables
444
- });
498
+ const insertRecord = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data", method: "post", ...variables });
445
499
  const insertRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "put", ...variables });
446
500
  const updateRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "patch", ...variables });
447
501
  const upsertRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "post", ...variables });
@@ -492,6 +546,7 @@ const operationsByTag = {
492
546
  getDatabaseList,
493
547
  createDatabase,
494
548
  deleteDatabase,
549
+ getDatabaseMetadata,
495
550
  getGitBranchesMapping,
496
551
  addGitBranchesEntry,
497
552
  removeGitBranchesEntry,
@@ -584,7 +639,8 @@ class XataApiClient {
584
639
  __privateAdd$7(this, _extraProps, void 0);
585
640
  __privateAdd$7(this, _namespaces, {});
586
641
  const provider = options.host ?? "production";
587
- const apiKey = options?.apiKey ?? getAPIKey();
642
+ const apiKey = options.apiKey ?? getAPIKey();
643
+ const trace = options.trace ?? defaultTrace;
588
644
  if (!apiKey) {
589
645
  throw new Error("Could not resolve a valid apiKey");
590
646
  }
@@ -592,7 +648,8 @@ class XataApiClient {
592
648
  apiUrl: getHostUrl(provider, "main"),
593
649
  workspacesApiUrl: getHostUrl(provider, "workspaces"),
594
650
  fetchImpl: getFetchImplementation(options.fetch),
595
- apiKey
651
+ apiKey,
652
+ trace
596
653
  });
597
654
  }
598
655
  get user() {
@@ -764,6 +821,12 @@ class DatabaseApi {
764
821
  ...this.extraProps
765
822
  });
766
823
  }
824
+ getDatabaseMetadata(workspace, dbName) {
825
+ return operationsByTag.database.getDatabaseMetadata({
826
+ pathParams: { workspace, dbName },
827
+ ...this.extraProps
828
+ });
829
+ }
767
830
  getGitBranchesMapping(workspace, dbName) {
768
831
  return operationsByTag.database.getGitBranchesMapping({
769
832
  pathParams: { workspace, dbName },
@@ -936,9 +999,10 @@ class RecordsApi {
936
999
  constructor(extraProps) {
937
1000
  this.extraProps = extraProps;
938
1001
  }
939
- insertRecord(workspace, database, branch, tableName, record) {
1002
+ insertRecord(workspace, database, branch, tableName, record, options = {}) {
940
1003
  return operationsByTag.records.insertRecord({
941
1004
  pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
1005
+ queryParams: options,
942
1006
  body: record,
943
1007
  ...this.extraProps
944
1008
  });
@@ -967,21 +1031,24 @@ class RecordsApi {
967
1031
  ...this.extraProps
968
1032
  });
969
1033
  }
970
- deleteRecord(workspace, database, branch, tableName, recordId) {
1034
+ deleteRecord(workspace, database, branch, tableName, recordId, options = {}) {
971
1035
  return operationsByTag.records.deleteRecord({
972
1036
  pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
1037
+ queryParams: options,
973
1038
  ...this.extraProps
974
1039
  });
975
1040
  }
976
1041
  getRecord(workspace, database, branch, tableName, recordId, options = {}) {
977
1042
  return operationsByTag.records.getRecord({
978
1043
  pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
1044
+ queryParams: options,
979
1045
  ...this.extraProps
980
1046
  });
981
1047
  }
982
- bulkInsertTableRecords(workspace, database, branch, tableName, records) {
1048
+ bulkInsertTableRecords(workspace, database, branch, tableName, records, options = {}) {
983
1049
  return operationsByTag.records.bulkInsertTableRecords({
984
1050
  pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
1051
+ queryParams: options,
985
1052
  body: { records },
986
1053
  ...this.extraProps
987
1054
  });
@@ -1199,13 +1266,18 @@ const _Query = class {
1199
1266
  return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
1200
1267
  }
1201
1268
  }
1202
- sort(column, direction) {
1269
+ sort(column, direction = "asc") {
1203
1270
  const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
1204
1271
  const sort = [...originalSort, { column, direction }];
1205
1272
  return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
1206
1273
  }
1207
1274
  select(columns) {
1208
- return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { columns }, __privateGet$5(this, _data));
1275
+ return new _Query(
1276
+ __privateGet$5(this, _repository),
1277
+ __privateGet$5(this, _table$1),
1278
+ { columns },
1279
+ __privateGet$5(this, _data)
1280
+ );
1209
1281
  }
1210
1282
  getPaginated(options = {}) {
1211
1283
  const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
@@ -1330,7 +1402,7 @@ var __privateMethod$2 = (obj, member, method) => {
1330
1402
  __accessCheck$4(obj, member, "access private method");
1331
1403
  return method;
1332
1404
  };
1333
- var _table, _getFetchProps, _cache, _schemaTables$2, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _bulkInsertTableRecords, bulkInsertTableRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn, _invalidateCache, invalidateCache_fn, _setCacheRecord, setCacheRecord_fn, _getCacheRecord, getCacheRecord_fn, _setCacheQuery, setCacheQuery_fn, _getCacheQuery, getCacheQuery_fn, _getSchemaTables$1, getSchemaTables_fn$1;
1405
+ 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;
1334
1406
  class Repository extends Query {
1335
1407
  }
1336
1408
  class RestRepository extends Query {
@@ -1342,191 +1414,214 @@ class RestRepository extends Query {
1342
1414
  __privateAdd$4(this, _updateRecordWithID);
1343
1415
  __privateAdd$4(this, _upsertRecordWithID);
1344
1416
  __privateAdd$4(this, _deleteRecord);
1345
- __privateAdd$4(this, _invalidateCache);
1346
- __privateAdd$4(this, _setCacheRecord);
1347
- __privateAdd$4(this, _getCacheRecord);
1348
1417
  __privateAdd$4(this, _setCacheQuery);
1349
1418
  __privateAdd$4(this, _getCacheQuery);
1350
1419
  __privateAdd$4(this, _getSchemaTables$1);
1351
1420
  __privateAdd$4(this, _table, void 0);
1352
1421
  __privateAdd$4(this, _getFetchProps, void 0);
1422
+ __privateAdd$4(this, _db, void 0);
1353
1423
  __privateAdd$4(this, _cache, void 0);
1354
1424
  __privateAdd$4(this, _schemaTables$2, void 0);
1425
+ __privateAdd$4(this, _trace, void 0);
1355
1426
  __privateSet$4(this, _table, options.table);
1356
1427
  __privateSet$4(this, _getFetchProps, options.pluginOptions.getFetchProps);
1357
- this.db = options.db;
1428
+ __privateSet$4(this, _db, options.db);
1358
1429
  __privateSet$4(this, _cache, options.pluginOptions.cache);
1359
1430
  __privateSet$4(this, _schemaTables$2, options.schemaTables);
1431
+ const trace = options.pluginOptions.trace ?? defaultTrace;
1432
+ __privateSet$4(this, _trace, async (name, fn, options2 = {}) => {
1433
+ return trace(name, fn, {
1434
+ ...options2,
1435
+ [TraceAttributes.TABLE]: __privateGet$4(this, _table),
1436
+ [TraceAttributes.VERSION]: VERSION
1437
+ });
1438
+ });
1360
1439
  }
1361
- async create(a, b) {
1362
- if (Array.isArray(a)) {
1363
- if (a.length === 0)
1364
- return [];
1365
- const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
1366
- await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
1367
- return records;
1368
- }
1369
- if (isString(a) && isObject(b)) {
1370
- if (a === "")
1371
- throw new Error("The id can't be empty");
1372
- const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b);
1373
- await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1374
- return record;
1375
- }
1376
- if (isObject(a) && isString(a.id)) {
1377
- if (a.id === "")
1378
- throw new Error("The id can't be empty");
1379
- const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 });
1380
- await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1381
- return record;
1382
- }
1383
- if (isObject(a)) {
1384
- const record = await __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a);
1385
- await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1386
- return record;
1387
- }
1388
- throw new Error("Invalid arguments for create method");
1389
- }
1390
- async read(a) {
1391
- if (Array.isArray(a)) {
1392
- if (a.length === 0)
1393
- return [];
1394
- const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
1395
- return this.getAll({ filter: { id: { $any: ids } } });
1396
- }
1397
- const id = isString(a) ? a : a.id;
1398
- if (isString(id)) {
1399
- const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, id);
1400
- if (cacheRecord)
1401
- return cacheRecord;
1402
- const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1403
- try {
1404
- const response = await getRecord({
1405
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
1406
- ...fetchProps
1407
- });
1408
- const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1409
- return initObject(this.db, schemaTables, __privateGet$4(this, _table), response);
1410
- } catch (e) {
1411
- if (isObject(e) && e.status === 404) {
1412
- return null;
1440
+ async create(a, b, c) {
1441
+ return __privateGet$4(this, _trace).call(this, "create", async () => {
1442
+ if (Array.isArray(a)) {
1443
+ if (a.length === 0)
1444
+ return [];
1445
+ const columns = isStringArray(b) ? b : void 0;
1446
+ return __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a, columns);
1447
+ }
1448
+ if (isString(a) && isObject(b)) {
1449
+ if (a === "")
1450
+ throw new Error("The id can't be empty");
1451
+ const columns = isStringArray(c) ? c : void 0;
1452
+ return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b, columns);
1453
+ }
1454
+ if (isObject(a) && isString(a.id)) {
1455
+ if (a.id === "")
1456
+ throw new Error("The id can't be empty");
1457
+ const columns = isStringArray(b) ? b : void 0;
1458
+ return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 }, columns);
1459
+ }
1460
+ if (isObject(a)) {
1461
+ const columns = isStringArray(b) ? b : void 0;
1462
+ return __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a, columns);
1463
+ }
1464
+ throw new Error("Invalid arguments for create method");
1465
+ });
1466
+ }
1467
+ async read(a, b) {
1468
+ return __privateGet$4(this, _trace).call(this, "read", async () => {
1469
+ const columns = isStringArray(b) ? b : ["*"];
1470
+ if (Array.isArray(a)) {
1471
+ if (a.length === 0)
1472
+ return [];
1473
+ const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
1474
+ const finalObjects = await this.getAll({ filter: { id: { $any: ids } }, columns });
1475
+ const dictionary = finalObjects.reduce((acc, object) => {
1476
+ acc[object.id] = object;
1477
+ return acc;
1478
+ }, {});
1479
+ return ids.map((id2) => dictionary[id2] ?? null);
1480
+ }
1481
+ const id = isString(a) ? a : a.id;
1482
+ if (isString(id)) {
1483
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1484
+ try {
1485
+ const response = await getRecord({
1486
+ pathParams: {
1487
+ workspace: "{workspaceId}",
1488
+ dbBranchName: "{dbBranch}",
1489
+ tableName: __privateGet$4(this, _table),
1490
+ recordId: id
1491
+ },
1492
+ queryParams: { columns },
1493
+ ...fetchProps
1494
+ });
1495
+ const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1496
+ return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
1497
+ } catch (e) {
1498
+ if (isObject(e) && e.status === 404) {
1499
+ return null;
1500
+ }
1501
+ throw e;
1413
1502
  }
1414
- throw e;
1415
1503
  }
1416
- }
1504
+ return null;
1505
+ });
1417
1506
  }
1418
- async update(a, b) {
1419
- if (Array.isArray(a)) {
1420
- if (a.length === 0)
1421
- return [];
1422
- if (a.length > 100) {
1423
- console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
1507
+ async update(a, b, c) {
1508
+ return __privateGet$4(this, _trace).call(this, "update", 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.update(object, columns)));
1424
1517
  }
1425
- return Promise.all(a.map((object) => this.update(object)));
1426
- }
1427
- if (isString(a) && isObject(b)) {
1428
- await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
1429
- const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b);
1430
- await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1431
- return record;
1432
- }
1433
- if (isObject(a) && isString(a.id)) {
1434
- await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
1435
- const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1436
- await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1437
- return record;
1438
- }
1439
- throw new Error("Invalid arguments for update method");
1440
- }
1441
- async createOrUpdate(a, b) {
1442
- if (Array.isArray(a)) {
1443
- if (a.length === 0)
1444
- return [];
1445
- if (a.length > 100) {
1446
- console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
1518
+ if (isString(a) && isObject(b)) {
1519
+ const columns = isStringArray(c) ? c : void 0;
1520
+ return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b, columns);
1447
1521
  }
1448
- return Promise.all(a.map((object) => this.createOrUpdate(object)));
1449
- }
1450
- if (isString(a) && isObject(b)) {
1451
- await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
1452
- const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b);
1453
- await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1454
- return record;
1455
- }
1456
- if (isObject(a) && isString(a.id)) {
1457
- await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
1458
- const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1459
- await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1460
- return record;
1461
- }
1462
- throw new Error("Invalid arguments for createOrUpdate method");
1522
+ if (isObject(a) && isString(a.id)) {
1523
+ const columns = isStringArray(b) ? b : void 0;
1524
+ return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
1525
+ }
1526
+ throw new Error("Invalid arguments for update method");
1527
+ });
1528
+ }
1529
+ async createOrUpdate(a, b, c) {
1530
+ return __privateGet$4(this, _trace).call(this, "createOrUpdate", async () => {
1531
+ if (Array.isArray(a)) {
1532
+ if (a.length === 0)
1533
+ return [];
1534
+ if (a.length > 100) {
1535
+ console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
1536
+ }
1537
+ const columns = isStringArray(b) ? b : ["*"];
1538
+ return Promise.all(a.map((object) => this.createOrUpdate(object, columns)));
1539
+ }
1540
+ if (isString(a) && isObject(b)) {
1541
+ const columns = isStringArray(c) ? c : void 0;
1542
+ return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b, columns);
1543
+ }
1544
+ if (isObject(a) && isString(a.id)) {
1545
+ const columns = isStringArray(c) ? c : void 0;
1546
+ return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
1547
+ }
1548
+ throw new Error("Invalid arguments for createOrUpdate method");
1549
+ });
1463
1550
  }
1464
1551
  async delete(a) {
1465
- if (Array.isArray(a)) {
1466
- if (a.length === 0)
1552
+ return __privateGet$4(this, _trace).call(this, "delete", async () => {
1553
+ if (Array.isArray(a)) {
1554
+ if (a.length === 0)
1555
+ return;
1556
+ if (a.length > 100) {
1557
+ console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
1558
+ }
1559
+ await Promise.all(a.map((id) => this.delete(id)));
1467
1560
  return;
1468
- if (a.length > 100) {
1469
- console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
1470
1561
  }
1471
- await Promise.all(a.map((id) => this.delete(id)));
1472
- return;
1473
- }
1474
- if (isString(a)) {
1475
- await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a);
1476
- await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
1477
- return;
1478
- }
1479
- if (isObject(a) && isString(a.id)) {
1480
- await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id);
1481
- await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
1482
- return;
1483
- }
1484
- throw new Error("Invalid arguments for delete method");
1562
+ if (isString(a)) {
1563
+ await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a);
1564
+ return;
1565
+ }
1566
+ if (isObject(a) && isString(a.id)) {
1567
+ await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id);
1568
+ return;
1569
+ }
1570
+ throw new Error("Invalid arguments for delete method");
1571
+ });
1485
1572
  }
1486
1573
  async search(query, options = {}) {
1487
- const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1488
- const { records } = await searchTable({
1489
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1490
- body: {
1491
- query,
1492
- fuzziness: options.fuzziness,
1493
- highlight: options.highlight,
1494
- filter: options.filter
1495
- },
1496
- ...fetchProps
1574
+ return __privateGet$4(this, _trace).call(this, "search", async () => {
1575
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1576
+ const { records } = await searchTable({
1577
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1578
+ body: {
1579
+ query,
1580
+ fuzziness: options.fuzziness,
1581
+ prefix: options.prefix,
1582
+ highlight: options.highlight,
1583
+ filter: options.filter,
1584
+ boosters: options.boosters
1585
+ },
1586
+ ...fetchProps
1587
+ });
1588
+ const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1589
+ return records.map((item) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), item));
1497
1590
  });
1498
- const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1499
- return records.map((item) => initObject(this.db, schemaTables, __privateGet$4(this, _table), item));
1500
1591
  }
1501
1592
  async query(query) {
1502
- const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
1503
- if (cacheQuery)
1504
- return new Page(query, cacheQuery.meta, cacheQuery.records);
1505
- const data = query.getQueryOptions();
1506
- const body = {
1507
- filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
1508
- sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
1509
- page: data.pagination,
1510
- columns: data.columns
1511
- };
1512
- const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1513
- const { meta, records: objects } = await queryTable({
1514
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1515
- body,
1516
- ...fetchProps
1593
+ return __privateGet$4(this, _trace).call(this, "query", async () => {
1594
+ const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
1595
+ if (cacheQuery)
1596
+ return new Page(query, cacheQuery.meta, cacheQuery.records);
1597
+ const data = query.getQueryOptions();
1598
+ const body = {
1599
+ filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
1600
+ sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
1601
+ page: data.pagination,
1602
+ columns: data.columns
1603
+ };
1604
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1605
+ const { meta, records: objects } = await queryTable({
1606
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1607
+ body,
1608
+ ...fetchProps
1609
+ });
1610
+ const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1611
+ const records = objects.map((record) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), record));
1612
+ await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
1613
+ return new Page(query, meta, records);
1517
1614
  });
1518
- const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1519
- const records = objects.map((record) => initObject(this.db, schemaTables, __privateGet$4(this, _table), record));
1520
- await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
1521
- return new Page(query, meta, records);
1522
1615
  }
1523
1616
  }
1524
1617
  _table = new WeakMap();
1525
1618
  _getFetchProps = new WeakMap();
1619
+ _db = new WeakMap();
1526
1620
  _cache = new WeakMap();
1527
1621
  _schemaTables$2 = new WeakMap();
1622
+ _trace = new WeakMap();
1528
1623
  _insertRecordWithoutId = new WeakSet();
1529
- insertRecordWithoutId_fn = async function(object) {
1624
+ insertRecordWithoutId_fn = async function(object, columns = ["*"]) {
1530
1625
  const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1531
1626
  const record = transformObjectLinks(object);
1532
1627
  const response = await insertRecord({
@@ -1535,17 +1630,15 @@ insertRecordWithoutId_fn = async function(object) {
1535
1630
  dbBranchName: "{dbBranch}",
1536
1631
  tableName: __privateGet$4(this, _table)
1537
1632
  },
1633
+ queryParams: { columns },
1538
1634
  body: record,
1539
1635
  ...fetchProps
1540
1636
  });
1541
- const finalObject = await this.read(response.id);
1542
- if (!finalObject) {
1543
- throw new Error("The server failed to save the record");
1544
- }
1545
- return finalObject;
1637
+ const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1638
+ return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
1546
1639
  };
1547
1640
  _insertRecordWithId = new WeakSet();
1548
- insertRecordWithId_fn = async function(recordId, object) {
1641
+ insertRecordWithId_fn = async function(recordId, object, columns = ["*"]) {
1549
1642
  const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1550
1643
  const record = transformObjectLinks(object);
1551
1644
  const response = await insertRecordWithID({
@@ -1556,60 +1649,52 @@ insertRecordWithId_fn = async function(recordId, object) {
1556
1649
  recordId
1557
1650
  },
1558
1651
  body: record,
1559
- queryParams: { createOnly: true },
1652
+ queryParams: { createOnly: true, columns },
1560
1653
  ...fetchProps
1561
1654
  });
1562
- const finalObject = await this.read(response.id);
1563
- if (!finalObject) {
1564
- throw new Error("The server failed to save the record");
1565
- }
1566
- return finalObject;
1655
+ const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1656
+ return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
1567
1657
  };
1568
1658
  _bulkInsertTableRecords = new WeakSet();
1569
- bulkInsertTableRecords_fn = async function(objects) {
1659
+ bulkInsertTableRecords_fn = async function(objects, columns = ["*"]) {
1570
1660
  const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1571
1661
  const records = objects.map((object) => transformObjectLinks(object));
1572
- const { recordIDs } = await bulkInsertTableRecords({
1662
+ const response = await bulkInsertTableRecords({
1573
1663
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1664
+ queryParams: { columns },
1574
1665
  body: { records },
1575
1666
  ...fetchProps
1576
1667
  });
1577
- const finalObjects = await this.read(recordIDs);
1578
- if (finalObjects.length !== objects.length) {
1579
- throw new Error("The server failed to save some records");
1668
+ if (!isResponseWithRecords(response)) {
1669
+ throw new Error("Request included columns but server didn't include them");
1580
1670
  }
1581
- const dictionary = finalObjects.reduce((acc, object) => {
1582
- acc[object.id] = object;
1583
- return acc;
1584
- }, {});
1585
- return recordIDs.map((id) => dictionary[id]);
1671
+ const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1672
+ return response.records?.map((item) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), item));
1586
1673
  };
1587
1674
  _updateRecordWithID = new WeakSet();
1588
- updateRecordWithID_fn = async function(recordId, object) {
1675
+ updateRecordWithID_fn = async function(recordId, object, columns = ["*"]) {
1589
1676
  const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1590
1677
  const record = transformObjectLinks(object);
1591
1678
  const response = await updateRecordWithID({
1592
1679
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1680
+ queryParams: { columns },
1593
1681
  body: record,
1594
1682
  ...fetchProps
1595
1683
  });
1596
- const item = await this.read(response.id);
1597
- if (!item)
1598
- throw new Error("The server failed to save the record");
1599
- return item;
1684
+ const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1685
+ return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
1600
1686
  };
1601
1687
  _upsertRecordWithID = new WeakSet();
1602
- upsertRecordWithID_fn = async function(recordId, object) {
1688
+ upsertRecordWithID_fn = async function(recordId, object, columns = ["*"]) {
1603
1689
  const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1604
1690
  const response = await upsertRecordWithID({
1605
1691
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1692
+ queryParams: { columns },
1606
1693
  body: object,
1607
1694
  ...fetchProps
1608
1695
  });
1609
- const item = await this.read(response.id);
1610
- if (!item)
1611
- throw new Error("The server failed to save the record");
1612
- return item;
1696
+ const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1697
+ return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
1613
1698
  };
1614
1699
  _deleteRecord = new WeakSet();
1615
1700
  deleteRecord_fn = async function(recordId) {
@@ -1619,29 +1704,6 @@ deleteRecord_fn = async function(recordId) {
1619
1704
  ...fetchProps
1620
1705
  });
1621
1706
  };
1622
- _invalidateCache = new WeakSet();
1623
- invalidateCache_fn = async function(recordId) {
1624
- await __privateGet$4(this, _cache).delete(`rec_${__privateGet$4(this, _table)}:${recordId}`);
1625
- const cacheItems = await __privateGet$4(this, _cache).getAll();
1626
- const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
1627
- for (const [key, value] of queries) {
1628
- const ids = getIds(value);
1629
- if (ids.includes(recordId))
1630
- await __privateGet$4(this, _cache).delete(key);
1631
- }
1632
- };
1633
- _setCacheRecord = new WeakSet();
1634
- setCacheRecord_fn = async function(record) {
1635
- if (!__privateGet$4(this, _cache).cacheRecords)
1636
- return;
1637
- await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
1638
- };
1639
- _getCacheRecord = new WeakSet();
1640
- getCacheRecord_fn = async function(recordId) {
1641
- if (!__privateGet$4(this, _cache).cacheRecords)
1642
- return null;
1643
- return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
1644
- };
1645
1707
  _setCacheQuery = new WeakSet();
1646
1708
  setCacheQuery_fn = async function(query, meta, records) {
1647
1709
  await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
@@ -1707,11 +1769,11 @@ const initObject = (db, schemaTables, table, object) => {
1707
1769
  }
1708
1770
  }
1709
1771
  }
1710
- result.read = function() {
1711
- return db[table].read(result["id"]);
1772
+ result.read = function(columns2) {
1773
+ return db[table].read(result["id"], columns2);
1712
1774
  };
1713
- result.update = function(data) {
1714
- return db[table].update(result["id"], data);
1775
+ result.update = function(data, columns2) {
1776
+ return db[table].update(result["id"], data, columns2);
1715
1777
  };
1716
1778
  result.delete = function() {
1717
1779
  return db[table].delete(result["id"]);
@@ -1725,14 +1787,8 @@ const initObject = (db, schemaTables, table, object) => {
1725
1787
  Object.freeze(result);
1726
1788
  return result;
1727
1789
  };
1728
- function getIds(value) {
1729
- if (Array.isArray(value)) {
1730
- return value.map((item) => getIds(item)).flat();
1731
- }
1732
- if (!isObject(value))
1733
- return [];
1734
- const nestedIds = Object.values(value).map((item) => getIds(item)).flat();
1735
- return isString(value.id) ? [value.id, ...nestedIds] : nestedIds;
1790
+ function isResponseWithRecords(value) {
1791
+ return isObject(value) && Array.isArray(value.records);
1736
1792
  }
1737
1793
 
1738
1794
  var __accessCheck$3 = (obj, member, msg) => {
@@ -1759,7 +1815,6 @@ class SimpleCache {
1759
1815
  __privateAdd$3(this, _map, void 0);
1760
1816
  __privateSet$3(this, _map, /* @__PURE__ */ new Map());
1761
1817
  this.capacity = options.max ?? 500;
1762
- this.cacheRecords = options.cacheRecords ?? true;
1763
1818
  this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
1764
1819
  }
1765
1820
  async getAll() {
@@ -1785,18 +1840,25 @@ class SimpleCache {
1785
1840
  }
1786
1841
  _map = new WeakMap();
1787
1842
 
1788
- const gt = (value) => ({ $gt: value });
1789
- const ge = (value) => ({ $ge: value });
1790
- const gte = (value) => ({ $ge: value });
1791
- const lt = (value) => ({ $lt: value });
1792
- const lte = (value) => ({ $le: value });
1793
- const le = (value) => ({ $le: value });
1843
+ const greaterThan = (value) => ({ $gt: value });
1844
+ const gt = greaterThan;
1845
+ const greaterThanEquals = (value) => ({ $ge: value });
1846
+ const greaterEquals = greaterThanEquals;
1847
+ const gte = greaterThanEquals;
1848
+ const ge = greaterThanEquals;
1849
+ const lessThan = (value) => ({ $lt: value });
1850
+ const lt = lessThan;
1851
+ const lessThanEquals = (value) => ({ $le: value });
1852
+ const lessEquals = lessThanEquals;
1853
+ const lte = lessThanEquals;
1854
+ const le = lessThanEquals;
1794
1855
  const exists = (column) => ({ $exists: column });
1795
1856
  const notExists = (column) => ({ $notExists: column });
1796
1857
  const startsWith = (value) => ({ $startsWith: value });
1797
1858
  const endsWith = (value) => ({ $endsWith: value });
1798
1859
  const pattern = (value) => ({ $pattern: value });
1799
1860
  const is = (value) => ({ $is: value });
1861
+ const equals = is;
1800
1862
  const isNot = (value) => ({ $isNot: value });
1801
1863
  const contains = (value) => ({ $contains: value });
1802
1864
  const includes = (value) => ({ $includes: value });
@@ -1831,16 +1893,19 @@ class SchemaPlugin extends XataPlugin {
1831
1893
  __privateSet$2(this, _schemaTables$1, schemaTables);
1832
1894
  }
1833
1895
  build(pluginOptions) {
1834
- const db = new Proxy({}, {
1835
- get: (_target, table) => {
1836
- if (!isString(table))
1837
- throw new Error("Invalid table name");
1838
- if (__privateGet$2(this, _tables)[table] === void 0) {
1839
- __privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
1896
+ const db = new Proxy(
1897
+ {},
1898
+ {
1899
+ get: (_target, table) => {
1900
+ if (!isString(table))
1901
+ throw new Error("Invalid table name");
1902
+ if (__privateGet$2(this, _tables)[table] === void 0) {
1903
+ __privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
1904
+ }
1905
+ return __privateGet$2(this, _tables)[table];
1840
1906
  }
1841
- return __privateGet$2(this, _tables)[table];
1842
1907
  }
1843
- });
1908
+ );
1844
1909
  const tableNames = __privateGet$2(this, _schemaTables$1)?.map(({ name }) => name) ?? [];
1845
1910
  for (const table of tableNames) {
1846
1911
  db[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
@@ -1910,10 +1975,10 @@ _schemaTables = new WeakMap();
1910
1975
  _search = new WeakSet();
1911
1976
  search_fn = async function(query, options, getFetchProps) {
1912
1977
  const fetchProps = await getFetchProps();
1913
- const { tables, fuzziness, highlight } = options ?? {};
1978
+ const { tables, fuzziness, highlight, prefix } = options ?? {};
1914
1979
  const { records } = await searchBranch({
1915
1980
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1916
- body: { tables, query, fuzziness, highlight },
1981
+ body: { tables, query, fuzziness, prefix, highlight },
1917
1982
  ...fetchProps
1918
1983
  });
1919
1984
  return records;
@@ -1954,9 +2019,13 @@ async function resolveXataBranch(gitBranch, options) {
1954
2019
  const databaseURL = options?.databaseURL || getDatabaseURL();
1955
2020
  const apiKey = options?.apiKey || getAPIKey();
1956
2021
  if (!databaseURL)
1957
- throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
2022
+ throw new Error(
2023
+ "A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely"
2024
+ );
1958
2025
  if (!apiKey)
1959
- throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
2026
+ throw new Error(
2027
+ "An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely"
2028
+ );
1960
2029
  const [protocol, , host, , dbName] = databaseURL.split("/");
1961
2030
  const [workspace] = host.split(".");
1962
2031
  const { fallbackBranch } = getEnvironment();
@@ -1966,7 +2035,8 @@ async function resolveXataBranch(gitBranch, options) {
1966
2035
  fetchImpl: getFetchImplementation(options?.fetchImpl),
1967
2036
  workspacesApiUrl: `${protocol}//${host}`,
1968
2037
  pathParams: { dbName, workspace },
1969
- queryParams: { gitBranch, fallbackBranch }
2038
+ queryParams: { gitBranch, fallbackBranch },
2039
+ trace: defaultTrace
1970
2040
  });
1971
2041
  return branch;
1972
2042
  }
@@ -1974,9 +2044,13 @@ async function getDatabaseBranch(branch, options) {
1974
2044
  const databaseURL = options?.databaseURL || getDatabaseURL();
1975
2045
  const apiKey = options?.apiKey || getAPIKey();
1976
2046
  if (!databaseURL)
1977
- throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
2047
+ throw new Error(
2048
+ "A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely"
2049
+ );
1978
2050
  if (!apiKey)
1979
- throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
2051
+ throw new Error(
2052
+ "An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely"
2053
+ );
1980
2054
  const [protocol, , host, , database] = databaseURL.split("/");
1981
2055
  const [workspace] = host.split(".");
1982
2056
  const dbBranchName = `${database}:${branch}`;
@@ -1986,7 +2060,8 @@ async function getDatabaseBranch(branch, options) {
1986
2060
  apiUrl: databaseURL,
1987
2061
  fetchImpl: getFetchImplementation(options?.fetchImpl),
1988
2062
  workspacesApiUrl: `${protocol}//${host}`,
1989
- pathParams: { dbBranchName, workspace }
2063
+ pathParams: { dbBranchName, workspace },
2064
+ trace: defaultTrace
1990
2065
  });
1991
2066
  } catch (err) {
1992
2067
  if (isObject(err) && err.status === 404)
@@ -2026,17 +2101,20 @@ var __privateMethod = (obj, member, method) => {
2026
2101
  return method;
2027
2102
  };
2028
2103
  const buildClient = (plugins) => {
2029
- var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
2104
+ var _branch, _options, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
2030
2105
  return _a = class {
2031
2106
  constructor(options = {}, schemaTables) {
2032
2107
  __privateAdd(this, _parseOptions);
2033
2108
  __privateAdd(this, _getFetchProps);
2034
2109
  __privateAdd(this, _evaluateBranch);
2035
2110
  __privateAdd(this, _branch, void 0);
2111
+ __privateAdd(this, _options, void 0);
2036
2112
  const safeOptions = __privateMethod(this, _parseOptions, parseOptions_fn).call(this, options);
2113
+ __privateSet(this, _options, safeOptions);
2037
2114
  const pluginOptions = {
2038
2115
  getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
2039
- cache: safeOptions.cache
2116
+ cache: safeOptions.cache,
2117
+ trace: safeOptions.trace
2040
2118
  };
2041
2119
  const db = new SchemaPlugin(schemaTables).build(pluginOptions);
2042
2120
  const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
@@ -2055,22 +2133,26 @@ const buildClient = (plugins) => {
2055
2133
  }
2056
2134
  }
2057
2135
  }
2058
- }, _branch = new WeakMap(), _parseOptions = new WeakSet(), parseOptions_fn = function(options) {
2136
+ async getConfig() {
2137
+ const databaseURL = __privateGet(this, _options).databaseURL;
2138
+ const branch = await __privateGet(this, _options).branch();
2139
+ return { databaseURL, branch };
2140
+ }
2141
+ }, _branch = new WeakMap(), _options = new WeakMap(), _parseOptions = new WeakSet(), parseOptions_fn = function(options) {
2059
2142
  const fetch = getFetchImplementation(options?.fetch);
2060
2143
  const databaseURL = options?.databaseURL || getDatabaseURL();
2061
2144
  const apiKey = options?.apiKey || getAPIKey();
2062
- const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
2145
+ const cache = options?.cache ?? new SimpleCache({ defaultQueryTTL: 0 });
2146
+ const trace = options?.trace ?? defaultTrace;
2063
2147
  const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
2064
- if (!databaseURL || !apiKey) {
2065
- throw new Error("Options databaseURL and apiKey are required");
2148
+ if (!apiKey) {
2149
+ throw new Error("Option apiKey is required");
2066
2150
  }
2067
- return { fetch, databaseURL, apiKey, branch, cache };
2068
- }, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({
2069
- fetch,
2070
- apiKey,
2071
- databaseURL,
2072
- branch
2073
- }) {
2151
+ if (!databaseURL) {
2152
+ throw new Error("Option databaseURL is required");
2153
+ }
2154
+ return { fetch, databaseURL, apiKey, branch, cache, trace };
2155
+ }, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({ fetch, apiKey, databaseURL, branch, trace }) {
2074
2156
  const branchValue = await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, branch);
2075
2157
  if (!branchValue)
2076
2158
  throw new Error("Unable to resolve branch value");
@@ -2082,7 +2164,8 @@ const buildClient = (plugins) => {
2082
2164
  const hasBranch = params.dbBranchName ?? params.branch;
2083
2165
  const newPath = path.replace(/^\/db\/[^/]+/, hasBranch ? `:${branchValue}` : "");
2084
2166
  return databaseURL + newPath;
2085
- }
2167
+ },
2168
+ trace
2086
2169
  };
2087
2170
  }, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
2088
2171
  if (__privateGet(this, _branch))
@@ -2105,6 +2188,88 @@ const buildClient = (plugins) => {
2105
2188
  class BaseClient extends buildClient() {
2106
2189
  }
2107
2190
 
2191
+ const META = "__";
2192
+ const VALUE = "___";
2193
+ class Serializer {
2194
+ constructor() {
2195
+ this.classes = {};
2196
+ }
2197
+ add(clazz) {
2198
+ this.classes[clazz.name] = clazz;
2199
+ }
2200
+ toJSON(data) {
2201
+ function visit(obj) {
2202
+ if (Array.isArray(obj))
2203
+ return obj.map(visit);
2204
+ const type = typeof obj;
2205
+ if (type === "undefined")
2206
+ return { [META]: "undefined" };
2207
+ if (type === "bigint")
2208
+ return { [META]: "bigint", [VALUE]: obj.toString() };
2209
+ if (obj === null || type !== "object")
2210
+ return obj;
2211
+ const constructor = obj.constructor;
2212
+ const o = { [META]: constructor.name };
2213
+ for (const [key, value] of Object.entries(obj)) {
2214
+ o[key] = visit(value);
2215
+ }
2216
+ if (constructor === Date)
2217
+ o[VALUE] = obj.toISOString();
2218
+ if (constructor === Map)
2219
+ o[VALUE] = Object.fromEntries(obj);
2220
+ if (constructor === Set)
2221
+ o[VALUE] = [...obj];
2222
+ return o;
2223
+ }
2224
+ return JSON.stringify(visit(data));
2225
+ }
2226
+ fromJSON(json) {
2227
+ return JSON.parse(json, (key, value) => {
2228
+ if (value && typeof value === "object" && !Array.isArray(value)) {
2229
+ const { [META]: clazz, [VALUE]: val, ...rest } = value;
2230
+ const constructor = this.classes[clazz];
2231
+ if (constructor) {
2232
+ return Object.assign(Object.create(constructor.prototype), rest);
2233
+ }
2234
+ if (clazz === "Date")
2235
+ return new Date(val);
2236
+ if (clazz === "Set")
2237
+ return new Set(val);
2238
+ if (clazz === "Map")
2239
+ return new Map(Object.entries(val));
2240
+ if (clazz === "bigint")
2241
+ return BigInt(val);
2242
+ if (clazz === "undefined")
2243
+ return void 0;
2244
+ return rest;
2245
+ }
2246
+ return value;
2247
+ });
2248
+ }
2249
+ }
2250
+ const defaultSerializer = new Serializer();
2251
+ const serialize = (data) => {
2252
+ return defaultSerializer.toJSON(data);
2253
+ };
2254
+ const deserialize = (json) => {
2255
+ return defaultSerializer.fromJSON(json);
2256
+ };
2257
+
2258
+ function buildWorkerRunner(config) {
2259
+ return function xataWorker(name, _worker) {
2260
+ return async (...args) => {
2261
+ const url = process.env.NODE_ENV === "development" ? `http://localhost:64749/${name}` : `https://dispatcher.xata.workers.dev/${config.workspace}/${config.worker}/${name}`;
2262
+ const result = await fetch(url, {
2263
+ method: "POST",
2264
+ headers: { "Content-Type": "application/json" },
2265
+ body: serialize({ args })
2266
+ });
2267
+ const text = await result.text();
2268
+ return deserialize(text);
2269
+ };
2270
+ };
2271
+ }
2272
+
2108
2273
  class XataError extends Error {
2109
2274
  constructor(message, status) {
2110
2275
  super(message);
@@ -2125,6 +2290,7 @@ exports.Repository = Repository;
2125
2290
  exports.RestRepository = RestRepository;
2126
2291
  exports.SchemaPlugin = SchemaPlugin;
2127
2292
  exports.SearchPlugin = SearchPlugin;
2293
+ exports.Serializer = Serializer;
2128
2294
  exports.SimpleCache = SimpleCache;
2129
2295
  exports.XataApiClient = XataApiClient;
2130
2296
  exports.XataApiPlugin = XataApiPlugin;
@@ -2134,6 +2300,7 @@ exports.acceptWorkspaceMemberInvite = acceptWorkspaceMemberInvite;
2134
2300
  exports.addGitBranchesEntry = addGitBranchesEntry;
2135
2301
  exports.addTableColumn = addTableColumn;
2136
2302
  exports.buildClient = buildClient;
2303
+ exports.buildWorkerRunner = buildWorkerRunner;
2137
2304
  exports.bulkInsertTableRecords = bulkInsertTableRecords;
2138
2305
  exports.cancelWorkspaceMemberInvite = cancelWorkspaceMemberInvite;
2139
2306
  exports.contains = contains;
@@ -2150,7 +2317,9 @@ exports.deleteTable = deleteTable;
2150
2317
  exports.deleteUser = deleteUser;
2151
2318
  exports.deleteUserAPIKey = deleteUserAPIKey;
2152
2319
  exports.deleteWorkspace = deleteWorkspace;
2320
+ exports.deserialize = deserialize;
2153
2321
  exports.endsWith = endsWith;
2322
+ exports.equals = equals;
2154
2323
  exports.executeBranchMigrationPlan = executeBranchMigrationPlan;
2155
2324
  exports.exists = exists;
2156
2325
  exports.ge = ge;
@@ -2165,6 +2334,7 @@ exports.getColumn = getColumn;
2165
2334
  exports.getCurrentBranchDetails = getCurrentBranchDetails;
2166
2335
  exports.getCurrentBranchName = getCurrentBranchName;
2167
2336
  exports.getDatabaseList = getDatabaseList;
2337
+ exports.getDatabaseMetadata = getDatabaseMetadata;
2168
2338
  exports.getDatabaseURL = getDatabaseURL;
2169
2339
  exports.getGitBranchesMapping = getGitBranchesMapping;
2170
2340
  exports.getRecord = getRecord;
@@ -2175,6 +2345,9 @@ exports.getUserAPIKeys = getUserAPIKeys;
2175
2345
  exports.getWorkspace = getWorkspace;
2176
2346
  exports.getWorkspaceMembersList = getWorkspaceMembersList;
2177
2347
  exports.getWorkspacesList = getWorkspacesList;
2348
+ exports.greaterEquals = greaterEquals;
2349
+ exports.greaterThan = greaterThan;
2350
+ exports.greaterThanEquals = greaterThanEquals;
2178
2351
  exports.gt = gt;
2179
2352
  exports.gte = gte;
2180
2353
  exports.includes = includes;
@@ -2190,6 +2363,9 @@ exports.isIdentifiable = isIdentifiable;
2190
2363
  exports.isNot = isNot;
2191
2364
  exports.isXataRecord = isXataRecord;
2192
2365
  exports.le = le;
2366
+ exports.lessEquals = lessEquals;
2367
+ exports.lessThan = lessThan;
2368
+ exports.lessThanEquals = lessThanEquals;
2193
2369
  exports.lt = lt;
2194
2370
  exports.lte = lte;
2195
2371
  exports.notExists = notExists;
@@ -2202,6 +2378,7 @@ exports.resendWorkspaceMemberInvite = resendWorkspaceMemberInvite;
2202
2378
  exports.resolveBranch = resolveBranch;
2203
2379
  exports.searchBranch = searchBranch;
2204
2380
  exports.searchTable = searchTable;
2381
+ exports.serialize = serialize;
2205
2382
  exports.setTableSchema = setTableSchema;
2206
2383
  exports.startsWith = startsWith;
2207
2384
  exports.updateBranchMetadata = updateBranchMetadata;