@xata.io/client 0.0.0-alpha.vf95371f → 0.0.0-alpha.vf957886

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/.eslintrc.cjs CHANGED
@@ -6,8 +6,7 @@ module.exports = {
6
6
  project: 'packages/client/tsconfig.json'
7
7
  },
8
8
  rules: {
9
- '@typescript-eslint/no-explicit-any': 'off',
10
- '@typescript-eslint/ban-types': 'off',
11
9
  '@typescript-eslint/no-floating-promises': 'error',
10
+ "@typescript-eslint/strict-boolean-expressions": ["error", { allowNullableString: true, allowNullableObject: true }],
12
11
  }
13
12
  };
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @xata.io/client
2
2
 
3
+ ## 0.10.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [#312](https://github.com/xataio/client-ts/pull/312) [`0edf1af`](https://github.com/xataio/client-ts/commit/0edf1af2205c4761d53a02c74ddaab3168d69775) Thanks [@SferaDev](https://github.com/SferaDev)! - Add filtering to search by table
8
+
9
+ * [#312](https://github.com/xataio/client-ts/pull/312) [`66ad7cc`](https://github.com/xataio/client-ts/commit/66ad7cc0365046c5d039c37117feac04428d8373) Thanks [@SferaDev](https://github.com/SferaDev)! - Add new API method for searching in a given table
10
+
11
+ ## 0.10.1
12
+
13
+ ### Patch Changes
14
+
15
+ - [#271](https://github.com/xataio/client-ts/pull/271) [`0bb17b8`](https://github.com/xataio/client-ts/commit/0bb17b88d49f1c8be32d2d6b0b3a5918890876cb) Thanks [@SferaDev](https://github.com/SferaDev)! - Link and resolve branches from git
16
+
3
17
  ## 0.10.0
4
18
 
5
19
  ### Minor Changes
package/dist/index.cjs CHANGED
@@ -11,8 +11,11 @@ function compact(arr) {
11
11
  function isObject(value) {
12
12
  return Boolean(value) && typeof value === "object" && !Array.isArray(value);
13
13
  }
14
+ function isDefined(value) {
15
+ return value !== null && value !== void 0;
16
+ }
14
17
  function isString(value) {
15
- return value !== void 0 && value !== null && typeof value === "string";
18
+ return isDefined(value) && typeof value === "string";
16
19
  }
17
20
  function toBase64(value) {
18
21
  try {
@@ -74,7 +77,12 @@ function getFetchImplementation(userFetch) {
74
77
  return fetchImpl;
75
78
  }
76
79
 
77
- class FetcherError extends Error {
80
+ class ErrorWithCause extends Error {
81
+ constructor(message, options) {
82
+ super(message, options);
83
+ }
84
+ }
85
+ class FetcherError extends ErrorWithCause {
78
86
  constructor(status, data) {
79
87
  super(getMessage(data));
80
88
  this.status = status;
@@ -370,6 +378,11 @@ const queryTable = (variables) => fetch$1({
370
378
  method: "post",
371
379
  ...variables
372
380
  });
381
+ const searchTable = (variables) => fetch$1({
382
+ url: "/db/{dbBranchName}/tables/{tableName}/search",
383
+ method: "post",
384
+ ...variables
385
+ });
373
386
  const searchBranch = (variables) => fetch$1({
374
387
  url: "/db/{dbBranchName}/search",
375
388
  method: "post",
@@ -433,6 +446,7 @@ const operationsByTag = {
433
446
  getRecord,
434
447
  bulkInsertTableRecords,
435
448
  queryTable,
449
+ searchTable,
436
450
  searchBranch
437
451
  }
438
452
  };
@@ -888,6 +902,13 @@ class RecordsApi {
888
902
  ...this.extraProps
889
903
  });
890
904
  }
905
+ searchTable(workspace, database, branch, tableName, query) {
906
+ return operationsByTag.records.searchTable({
907
+ pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
908
+ body: query,
909
+ ...this.extraProps
910
+ });
911
+ }
891
912
  searchBranch(workspace, database, branch, query) {
892
913
  return operationsByTag.records.searchBranch({
893
914
  pathParams: { workspace, dbBranchName: `${database}:${branch}` },
@@ -954,6 +975,9 @@ const PAGINATION_MAX_SIZE = 200;
954
975
  const PAGINATION_DEFAULT_SIZE = 200;
955
976
  const PAGINATION_MAX_OFFSET = 800;
956
977
  const PAGINATION_DEFAULT_OFFSET = 0;
978
+ function isCursorPaginationOptions(options) {
979
+ return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
980
+ }
957
981
 
958
982
  var __accessCheck$5 = (obj, member, msg) => {
959
983
  if (!member.has(obj))
@@ -975,7 +999,7 @@ var __privateSet$4 = (obj, member, value, setter) => {
975
999
  };
976
1000
  var _table$1, _repository, _data;
977
1001
  const _Query = class {
978
- constructor(repository, table, data, parent) {
1002
+ constructor(repository, table, data, rawParent) {
979
1003
  __privateAdd$5(this, _table$1, void 0);
980
1004
  __privateAdd$5(this, _repository, void 0);
981
1005
  __privateAdd$5(this, _data, { filter: {} });
@@ -987,6 +1011,7 @@ const _Query = class {
987
1011
  } else {
988
1012
  __privateSet$4(this, _repository, this);
989
1013
  }
1014
+ const parent = cleanParent(data, rawParent);
990
1015
  __privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
991
1016
  __privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
992
1017
  __privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
@@ -1058,13 +1083,13 @@ const _Query = class {
1058
1083
  }
1059
1084
  async *getIterator(options = {}) {
1060
1085
  const { batchSize = 1 } = options;
1061
- let offset = 0;
1062
- let end = false;
1063
- while (!end) {
1064
- const { records, meta } = await this.getPaginated({ ...options, pagination: { size: batchSize, offset } });
1065
- yield records;
1066
- offset += batchSize;
1067
- end = !meta.page.more;
1086
+ let page = await this.getPaginated({ ...options, pagination: { size: batchSize, offset: 0 } });
1087
+ let more = page.hasNextPage();
1088
+ yield page.records;
1089
+ while (more) {
1090
+ page = await page.nextPage();
1091
+ more = page.hasNextPage();
1092
+ yield page.records;
1068
1093
  }
1069
1094
  }
1070
1095
  async getMany(options = {}) {
@@ -1081,7 +1106,7 @@ const _Query = class {
1081
1106
  }
1082
1107
  async getFirst(options = {}) {
1083
1108
  const records = await this.getMany({ ...options, pagination: { size: 1 } });
1084
- return records[0] || null;
1109
+ return records[0] ?? null;
1085
1110
  }
1086
1111
  cache(ttl) {
1087
1112
  return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
@@ -1106,6 +1131,12 @@ let Query = _Query;
1106
1131
  _table$1 = new WeakMap();
1107
1132
  _repository = new WeakMap();
1108
1133
  _data = new WeakMap();
1134
+ function cleanParent(data, parent) {
1135
+ if (isCursorPaginationOptions(data.pagination)) {
1136
+ return { ...parent, sorting: void 0, filter: void 0 };
1137
+ }
1138
+ return parent;
1139
+ }
1109
1140
 
1110
1141
  function isIdentifiable(x) {
1111
1142
  return isObject(x) && isString(x?.id);
@@ -1296,9 +1327,13 @@ class RestRepository extends Query {
1296
1327
  }
1297
1328
  async search(query, options = {}) {
1298
1329
  const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1299
- const { records } = await searchBranch({
1300
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1301
- body: { tables: [__privateGet$4(this, _table)], query, fuzziness: options.fuzziness },
1330
+ const { records } = await searchTable({
1331
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1332
+ body: {
1333
+ query,
1334
+ fuzziness: options.fuzziness,
1335
+ filter: options.filter
1336
+ },
1302
1337
  ...fetchProps
1303
1338
  });
1304
1339
  const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
@@ -1311,7 +1346,7 @@ class RestRepository extends Query {
1311
1346
  const data = query.getQueryOptions();
1312
1347
  const body = {
1313
1348
  filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
1314
- sort: data.sort ? buildSortFilter(data.sort) : void 0,
1349
+ sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
1315
1350
  page: data.pagination,
1316
1351
  columns: data.columns
1317
1352
  };
@@ -1501,7 +1536,7 @@ const initObject = (db, schema, table, object) => {
1501
1536
  const linkTable = column.link?.table;
1502
1537
  if (!linkTable) {
1503
1538
  console.error(`Failed to parse link for field ${column.name}`);
1504
- } else if (value && isObject(value)) {
1539
+ } else if (isObject(value)) {
1505
1540
  result[column.name] = initObject(db, schema, linkTable, value);
1506
1541
  }
1507
1542
  break;
@@ -1627,7 +1662,7 @@ class SchemaPlugin extends XataPlugin {
1627
1662
  get: (_target, table) => {
1628
1663
  if (!isString(table))
1629
1664
  throw new Error("Invalid table name");
1630
- if (!__privateGet$2(this, _tables)[table]) {
1665
+ if (__privateGet$2(this, _tables)[table] === void 0) {
1631
1666
  __privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
1632
1667
  }
1633
1668
  return __privateGet$2(this, _tables)[table];
@@ -1730,30 +1765,39 @@ const envBranchNames = [
1730
1765
  "CF_PAGES_BRANCH",
1731
1766
  "BRANCH"
1732
1767
  ];
1733
- const defaultBranch = "main";
1734
1768
  async function getCurrentBranchName(options) {
1735
1769
  const env = getBranchByEnvVariable();
1736
- if (env)
1737
- return env;
1738
- const branch = await getGitBranch();
1739
- if (!branch)
1740
- return defaultBranch;
1741
- const details = await getDatabaseBranch(branch, options);
1742
- if (details)
1743
- return branch;
1744
- return defaultBranch;
1770
+ if (env) {
1771
+ const details = await getDatabaseBranch(env, options);
1772
+ if (details)
1773
+ return env;
1774
+ console.warn(`Branch ${env} not found in Xata. Ignoring...`);
1775
+ }
1776
+ const gitBranch = await getGitBranch();
1777
+ return resolveXataBranch(gitBranch, options);
1745
1778
  }
1746
1779
  async function getCurrentBranchDetails(options) {
1747
- const env = getBranchByEnvVariable();
1748
- if (env)
1749
- return getDatabaseBranch(env, options);
1750
- const branch = await getGitBranch();
1751
- if (!branch)
1752
- return getDatabaseBranch(defaultBranch, options);
1753
- const details = await getDatabaseBranch(branch, options);
1754
- if (details)
1755
- return details;
1756
- return getDatabaseBranch(defaultBranch, options);
1780
+ const branch = await getCurrentBranchName(options);
1781
+ return getDatabaseBranch(branch, options);
1782
+ }
1783
+ async function resolveXataBranch(gitBranch, options) {
1784
+ const databaseURL = options?.databaseURL || getDatabaseURL();
1785
+ const apiKey = options?.apiKey || getAPIKey();
1786
+ if (!databaseURL)
1787
+ throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
1788
+ if (!apiKey)
1789
+ throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
1790
+ const [protocol, , host, , dbName] = databaseURL.split("/");
1791
+ const [workspace] = host.split(".");
1792
+ const { branch } = await resolveBranch({
1793
+ apiKey,
1794
+ apiUrl: databaseURL,
1795
+ fetchImpl: getFetchImplementation(options?.fetchImpl),
1796
+ workspacesApiUrl: `${protocol}//${host}`,
1797
+ pathParams: { dbName, workspace },
1798
+ queryParams: { gitBranch, fallbackBranch: getEnvVariable("XATA_FALLBACK_BRANCH") }
1799
+ });
1800
+ return branch;
1757
1801
  }
1758
1802
  async function getDatabaseBranch(branch, options) {
1759
1803
  const databaseURL = options?.databaseURL || getDatabaseURL();
@@ -1842,7 +1886,7 @@ const buildClient = (plugins) => {
1842
1886
  this.db = db;
1843
1887
  this.search = search;
1844
1888
  for (const [key, namespace] of Object.entries(plugins ?? {})) {
1845
- if (!namespace)
1889
+ if (namespace === void 0)
1846
1890
  continue;
1847
1891
  const result = namespace.build(pluginOptions);
1848
1892
  if (result instanceof Promise) {
@@ -1859,7 +1903,7 @@ const buildClient = (plugins) => {
1859
1903
  const databaseURL = options?.databaseURL || getDatabaseURL();
1860
1904
  const apiKey = options?.apiKey || getAPIKey();
1861
1905
  const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
1862
- const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
1906
+ const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
1863
1907
  if (!databaseURL || !apiKey) {
1864
1908
  throw new Error("Options databaseURL and apiKey are required");
1865
1909
  }
@@ -1886,7 +1930,7 @@ const buildClient = (plugins) => {
1886
1930
  }, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
1887
1931
  if (__privateGet(this, _branch))
1888
1932
  return __privateGet(this, _branch);
1889
- if (!param)
1933
+ if (param === void 0)
1890
1934
  return void 0;
1891
1935
  const strategies = Array.isArray(param) ? [...param] : [param];
1892
1936
  const evaluateBranch = async (strategy) => {
@@ -1983,6 +2027,7 @@ exports.insertRecord = insertRecord;
1983
2027
  exports.insertRecordWithID = insertRecordWithID;
1984
2028
  exports.inviteWorkspaceMember = inviteWorkspaceMember;
1985
2029
  exports.is = is;
2030
+ exports.isCursorPaginationOptions = isCursorPaginationOptions;
1986
2031
  exports.isIdentifiable = isIdentifiable;
1987
2032
  exports.isNot = isNot;
1988
2033
  exports.isXataRecord = isXataRecord;
@@ -1998,6 +2043,7 @@ exports.removeWorkspaceMember = removeWorkspaceMember;
1998
2043
  exports.resendWorkspaceMemberInvite = resendWorkspaceMemberInvite;
1999
2044
  exports.resolveBranch = resolveBranch;
2000
2045
  exports.searchBranch = searchBranch;
2046
+ exports.searchTable = searchTable;
2001
2047
  exports.setTableSchema = setTableSchema;
2002
2048
  exports.startsWith = startsWith;
2003
2049
  exports.updateBranchMetadata = updateBranchMetadata;