@xata.io/client 0.10.1 → 0.12.0

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
@@ -7,8 +7,11 @@ function compact(arr) {
7
7
  function isObject(value) {
8
8
  return Boolean(value) && typeof value === "object" && !Array.isArray(value);
9
9
  }
10
+ function isDefined(value) {
11
+ return value !== null && value !== void 0;
12
+ }
10
13
  function isString(value) {
11
- return value !== void 0 && value !== null && typeof value === "string";
14
+ return isDefined(value) && typeof value === "string";
12
15
  }
13
16
  function toBase64(value) {
14
17
  try {
@@ -70,7 +73,12 @@ function getFetchImplementation(userFetch) {
70
73
  return fetchImpl;
71
74
  }
72
75
 
73
- class FetcherError extends Error {
76
+ class ErrorWithCause extends Error {
77
+ constructor(message, options) {
78
+ super(message, options);
79
+ }
80
+ }
81
+ class FetcherError extends ErrorWithCause {
74
82
  constructor(status, data) {
75
83
  super(getMessage(data));
76
84
  this.status = status;
@@ -366,6 +374,11 @@ const queryTable = (variables) => fetch$1({
366
374
  method: "post",
367
375
  ...variables
368
376
  });
377
+ const searchTable = (variables) => fetch$1({
378
+ url: "/db/{dbBranchName}/tables/{tableName}/search",
379
+ method: "post",
380
+ ...variables
381
+ });
369
382
  const searchBranch = (variables) => fetch$1({
370
383
  url: "/db/{dbBranchName}/search",
371
384
  method: "post",
@@ -429,6 +442,7 @@ const operationsByTag = {
429
442
  getRecord,
430
443
  bulkInsertTableRecords,
431
444
  queryTable,
445
+ searchTable,
432
446
  searchBranch
433
447
  }
434
448
  };
@@ -884,6 +898,13 @@ class RecordsApi {
884
898
  ...this.extraProps
885
899
  });
886
900
  }
901
+ searchTable(workspace, database, branch, tableName, query) {
902
+ return operationsByTag.records.searchTable({
903
+ pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
904
+ body: query,
905
+ ...this.extraProps
906
+ });
907
+ }
887
908
  searchBranch(workspace, database, branch, query) {
888
909
  return operationsByTag.records.searchBranch({
889
910
  pathParams: { workspace, dbBranchName: `${database}:${branch}` },
@@ -950,6 +971,9 @@ const PAGINATION_MAX_SIZE = 200;
950
971
  const PAGINATION_DEFAULT_SIZE = 200;
951
972
  const PAGINATION_MAX_OFFSET = 800;
952
973
  const PAGINATION_DEFAULT_OFFSET = 0;
974
+ function isCursorPaginationOptions(options) {
975
+ return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
976
+ }
953
977
 
954
978
  var __accessCheck$5 = (obj, member, msg) => {
955
979
  if (!member.has(obj))
@@ -971,7 +995,7 @@ var __privateSet$4 = (obj, member, value, setter) => {
971
995
  };
972
996
  var _table$1, _repository, _data;
973
997
  const _Query = class {
974
- constructor(repository, table, data, parent) {
998
+ constructor(repository, table, data, rawParent) {
975
999
  __privateAdd$5(this, _table$1, void 0);
976
1000
  __privateAdd$5(this, _repository, void 0);
977
1001
  __privateAdd$5(this, _data, { filter: {} });
@@ -983,6 +1007,7 @@ const _Query = class {
983
1007
  } else {
984
1008
  __privateSet$4(this, _repository, this);
985
1009
  }
1010
+ const parent = cleanParent(data, rawParent);
986
1011
  __privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
987
1012
  __privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
988
1013
  __privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
@@ -1054,13 +1079,13 @@ const _Query = class {
1054
1079
  }
1055
1080
  async *getIterator(options = {}) {
1056
1081
  const { batchSize = 1 } = options;
1057
- let offset = 0;
1058
- let end = false;
1059
- while (!end) {
1060
- const { records, meta } = await this.getPaginated({ ...options, pagination: { size: batchSize, offset } });
1061
- yield records;
1062
- offset += batchSize;
1063
- end = !meta.page.more;
1082
+ let page = await this.getPaginated({ ...options, pagination: { size: batchSize, offset: 0 } });
1083
+ let more = page.hasNextPage();
1084
+ yield page.records;
1085
+ while (more) {
1086
+ page = await page.nextPage();
1087
+ more = page.hasNextPage();
1088
+ yield page.records;
1064
1089
  }
1065
1090
  }
1066
1091
  async getMany(options = {}) {
@@ -1102,12 +1127,20 @@ let Query = _Query;
1102
1127
  _table$1 = new WeakMap();
1103
1128
  _repository = new WeakMap();
1104
1129
  _data = new WeakMap();
1130
+ function cleanParent(data, parent) {
1131
+ if (isCursorPaginationOptions(data.pagination)) {
1132
+ return { ...parent, sorting: void 0, filter: void 0 };
1133
+ }
1134
+ return parent;
1135
+ }
1105
1136
 
1106
1137
  function isIdentifiable(x) {
1107
1138
  return isObject(x) && isString(x?.id);
1108
1139
  }
1109
1140
  function isXataRecord(x) {
1110
- return isIdentifiable(x) && typeof x?.xata === "object" && typeof x?.xata?.version === "number";
1141
+ const record = x;
1142
+ const metadata = record?.getMetadata();
1143
+ return isIdentifiable(x) && isObject(metadata) && typeof metadata.version === "number";
1111
1144
  }
1112
1145
 
1113
1146
  function isSortFilterString(value) {
@@ -1184,6 +1217,8 @@ class RestRepository extends Query {
1184
1217
  }
1185
1218
  async create(a, b) {
1186
1219
  if (Array.isArray(a)) {
1220
+ if (a.length === 0)
1221
+ return [];
1187
1222
  const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
1188
1223
  await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
1189
1224
  return records;
@@ -1209,27 +1244,36 @@ class RestRepository extends Query {
1209
1244
  }
1210
1245
  throw new Error("Invalid arguments for create method");
1211
1246
  }
1212
- async read(recordId) {
1213
- const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, recordId);
1214
- if (cacheRecord)
1215
- return cacheRecord;
1216
- const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1217
- try {
1218
- const response = await getRecord({
1219
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1220
- ...fetchProps
1221
- });
1222
- const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1223
- return initObject(this.db, schema, __privateGet$4(this, _table), response);
1224
- } catch (e) {
1225
- if (isObject(e) && e.status === 404) {
1226
- return null;
1247
+ async read(a) {
1248
+ if (Array.isArray(a)) {
1249
+ if (a.length === 0)
1250
+ return [];
1251
+ return this.getAll({ filter: { id: { $any: a } } });
1252
+ }
1253
+ if (isString(a)) {
1254
+ const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, a);
1255
+ if (cacheRecord)
1256
+ return cacheRecord;
1257
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1258
+ try {
1259
+ const response = await getRecord({
1260
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: a },
1261
+ ...fetchProps
1262
+ });
1263
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1264
+ return initObject(this.db, schema, __privateGet$4(this, _table), response);
1265
+ } catch (e) {
1266
+ if (isObject(e) && e.status === 404) {
1267
+ return null;
1268
+ }
1269
+ throw e;
1227
1270
  }
1228
- throw e;
1229
1271
  }
1230
1272
  }
1231
1273
  async update(a, b) {
1232
1274
  if (Array.isArray(a)) {
1275
+ if (a.length === 0)
1276
+ return [];
1233
1277
  if (a.length > 100) {
1234
1278
  console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
1235
1279
  }
@@ -1251,6 +1295,8 @@ class RestRepository extends Query {
1251
1295
  }
1252
1296
  async createOrUpdate(a, b) {
1253
1297
  if (Array.isArray(a)) {
1298
+ if (a.length === 0)
1299
+ return [];
1254
1300
  if (a.length > 100) {
1255
1301
  console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
1256
1302
  }
@@ -1272,6 +1318,8 @@ class RestRepository extends Query {
1272
1318
  }
1273
1319
  async delete(a) {
1274
1320
  if (Array.isArray(a)) {
1321
+ if (a.length === 0)
1322
+ return;
1275
1323
  if (a.length > 100) {
1276
1324
  console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
1277
1325
  }
@@ -1292,9 +1340,14 @@ class RestRepository extends Query {
1292
1340
  }
1293
1341
  async search(query, options = {}) {
1294
1342
  const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1295
- const { records } = await searchBranch({
1296
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1297
- body: { tables: [__privateGet$4(this, _table)], query, fuzziness: options.fuzziness },
1343
+ const { records } = await searchTable({
1344
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1345
+ body: {
1346
+ query,
1347
+ fuzziness: options.fuzziness,
1348
+ highlight: options.highlight,
1349
+ filter: options.filter
1350
+ },
1298
1351
  ...fetchProps
1299
1352
  });
1300
1353
  const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
@@ -1477,7 +1530,8 @@ const transformObjectLinks = (object) => {
1477
1530
  };
1478
1531
  const initObject = (db, schema, table, object) => {
1479
1532
  const result = {};
1480
- Object.assign(result, object);
1533
+ const { xata, ...rest } = object ?? {};
1534
+ Object.assign(result, rest);
1481
1535
  const { columns } = schema.tables.find(({ name }) => name === table) ?? {};
1482
1536
  if (!columns)
1483
1537
  console.error(`Table ${table} not found in schema`);
@@ -1485,10 +1539,10 @@ const initObject = (db, schema, table, object) => {
1485
1539
  const value = result[column.name];
1486
1540
  switch (column.type) {
1487
1541
  case "datetime": {
1488
- const date = new Date(value);
1489
- if (isNaN(date.getTime())) {
1542
+ const date = value !== void 0 ? new Date(value) : void 0;
1543
+ if (date && isNaN(date.getTime())) {
1490
1544
  console.error(`Failed to parse date ${value} for field ${column.name}`);
1491
- } else {
1545
+ } else if (date) {
1492
1546
  result[column.name] = date;
1493
1547
  }
1494
1548
  break;
@@ -1513,7 +1567,10 @@ const initObject = (db, schema, table, object) => {
1513
1567
  result.delete = function() {
1514
1568
  return db[table].delete(result["id"]);
1515
1569
  };
1516
- for (const prop of ["read", "update", "delete"]) {
1570
+ result.getMetadata = function() {
1571
+ return xata;
1572
+ };
1573
+ for (const prop of ["read", "update", "delete", "getMetadata"]) {
1517
1574
  Object.defineProperty(result, prop, { enumerable: false });
1518
1575
  }
1519
1576
  Object.freeze(result);
@@ -1695,10 +1752,10 @@ _schema = new WeakMap();
1695
1752
  _search = new WeakSet();
1696
1753
  search_fn = async function(query, options, getFetchProps) {
1697
1754
  const fetchProps = await getFetchProps();
1698
- const { tables, fuzziness } = options ?? {};
1755
+ const { tables, fuzziness, highlight } = options ?? {};
1699
1756
  const { records } = await searchBranch({
1700
1757
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1701
- body: { tables, query, fuzziness },
1758
+ body: { tables, query, fuzziness, highlight },
1702
1759
  ...fetchProps
1703
1760
  });
1704
1761
  return records;
@@ -1916,5 +1973,5 @@ class XataError extends Error {
1916
1973
  }
1917
1974
  }
1918
1975
 
1919
- export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, Repository, RestRepository, SchemaPlugin, SearchPlugin, SimpleCache, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addGitBranchesEntry, addTableColumn, buildClient, bulkInsertTableRecords, cancelWorkspaceMemberInvite, contains, createBranch, createDatabase, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, endsWith, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseURL, getGitBranchesMapping, getRecord, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getWorkspace, getWorkspaceMembersList, getWorkspacesList, gt, gte, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isIdentifiable, isNot, isXataRecord, le, lt, lte, notExists, operationsByTag, pattern, queryTable, removeGitBranchesEntry, removeWorkspaceMember, resendWorkspaceMemberInvite, resolveBranch, searchBranch, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
1976
+ export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, Repository, RestRepository, SchemaPlugin, SearchPlugin, SimpleCache, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addGitBranchesEntry, addTableColumn, buildClient, bulkInsertTableRecords, cancelWorkspaceMemberInvite, contains, createBranch, createDatabase, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, endsWith, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseURL, getGitBranchesMapping, getRecord, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getWorkspace, getWorkspaceMembersList, getWorkspacesList, gt, gte, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isCursorPaginationOptions, isIdentifiable, isNot, isXataRecord, le, lt, lte, notExists, operationsByTag, pattern, queryTable, removeGitBranchesEntry, removeWorkspaceMember, resendWorkspaceMemberInvite, resolveBranch, searchBranch, searchTable, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
1920
1977
  //# sourceMappingURL=index.mjs.map