@xata.io/client 0.0.0-alpha.vf0ac0ad → 0.0.0-alpha.vf221157

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,45 @@
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
+
17
+ ## 0.10.0
18
+
19
+ ### Minor Changes
20
+
21
+ - [#272](https://github.com/xataio/client-ts/pull/272) [`6d76275`](https://github.com/xataio/client-ts/commit/6d7627555a404a4c2da42f4187df6f8300f9a46f) Thanks [@SferaDev](https://github.com/SferaDev)! - Rename page options to pagination
22
+
23
+ * [#270](https://github.com/xataio/client-ts/pull/270) [`1864742`](https://github.com/xataio/client-ts/commit/18647428d8608841de514c3784fb711c39dccc6d) Thanks [@SferaDev](https://github.com/SferaDev)! - Move chunk to options object
24
+
25
+ ### Patch Changes
26
+
27
+ - [#281](https://github.com/xataio/client-ts/pull/281) [`d1ec0df`](https://github.com/xataio/client-ts/commit/d1ec0df14834088a816919bfc68216f3f9b2d9ef) Thanks [@SferaDev](https://github.com/SferaDev)! - Do not send from param on undefined
28
+
29
+ * [#282](https://github.com/xataio/client-ts/pull/282) [`1af6f1a`](https://github.com/xataio/client-ts/commit/1af6f1aaa1123e77a895961581c87f06a88db698) Thanks [@SferaDev](https://github.com/SferaDev)! - Do not allow filter/sort with cursor navigation
30
+
31
+ - [#284](https://github.com/xataio/client-ts/pull/284) [`be4eda8`](https://github.com/xataio/client-ts/commit/be4eda8f73037d97fef7de28b56d7471dd867875) Thanks [@SferaDev](https://github.com/SferaDev)! - Fix cache ttl with 0 value
32
+
33
+ * [#265](https://github.com/xataio/client-ts/pull/265) [`99be734`](https://github.com/xataio/client-ts/commit/99be734827576d888aa12a579ed1983a0a8a8e83) Thanks [@SferaDev](https://github.com/SferaDev)! - Add datetime field type support
34
+
35
+ ## 0.9.1
36
+
37
+ ### Patch Changes
38
+
39
+ - [#250](https://github.com/xataio/client-ts/pull/250) [`5d7c9e4`](https://github.com/xataio/client-ts/commit/5d7c9e4fa2799255e2bfc8b6fb12c89dc4e1f35e) Thanks [@xata-bot](https://github.com/xata-bot)! - Add branch resolution endpoints to api client
40
+
41
+ * [#261](https://github.com/xataio/client-ts/pull/261) [`e95f20a`](https://github.com/xataio/client-ts/commit/e95f20a7bce264936680353f816065fa379448fc) Thanks [@gimenete](https://github.com/gimenete)! - Fixes a compatibility error in CloudFlare workers with latest version of wrangler
42
+
3
43
  ## 0.9.0
4
44
 
5
45
  ### Minor Changes
package/dist/index.cjs CHANGED
@@ -370,6 +370,11 @@ const queryTable = (variables) => fetch$1({
370
370
  method: "post",
371
371
  ...variables
372
372
  });
373
+ const searchTable = (variables) => fetch$1({
374
+ url: "/db/{dbBranchName}/tables/{tableName}/search",
375
+ method: "post",
376
+ ...variables
377
+ });
373
378
  const searchBranch = (variables) => fetch$1({
374
379
  url: "/db/{dbBranchName}/search",
375
380
  method: "post",
@@ -433,6 +438,7 @@ const operationsByTag = {
433
438
  getRecord,
434
439
  bulkInsertTableRecords,
435
440
  queryTable,
441
+ searchTable,
436
442
  searchBranch
437
443
  }
438
444
  };
@@ -703,10 +709,10 @@ class BranchApi {
703
709
  ...this.extraProps
704
710
  });
705
711
  }
706
- createBranch(workspace, database, branch, from = "", options = {}) {
712
+ createBranch(workspace, database, branch, from, options = {}) {
707
713
  return operationsByTag.branch.createBranch({
708
714
  pathParams: { workspace, dbBranchName: `${database}:${branch}` },
709
- queryParams: { from },
715
+ queryParams: isString(from) ? { from } : void 0,
710
716
  body: options,
711
717
  ...this.extraProps
712
718
  });
@@ -888,6 +894,13 @@ class RecordsApi {
888
894
  ...this.extraProps
889
895
  });
890
896
  }
897
+ searchTable(workspace, database, branch, tableName, query) {
898
+ return operationsByTag.records.searchTable({
899
+ pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
900
+ body: query,
901
+ ...this.extraProps
902
+ });
903
+ }
891
904
  searchBranch(workspace, database, branch, query) {
892
905
  return operationsByTag.records.searchBranch({
893
906
  pathParams: { workspace, dbBranchName: `${database}:${branch}` },
@@ -934,16 +947,16 @@ class Page {
934
947
  this.records = records;
935
948
  }
936
949
  async nextPage(size, offset) {
937
- return __privateGet$6(this, _query).getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
950
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
938
951
  }
939
952
  async previousPage(size, offset) {
940
- return __privateGet$6(this, _query).getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
953
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, before: this.meta.page.cursor } });
941
954
  }
942
955
  async firstPage(size, offset) {
943
- return __privateGet$6(this, _query).getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
956
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, first: this.meta.page.cursor } });
944
957
  }
945
958
  async lastPage(size, offset) {
946
- return __privateGet$6(this, _query).getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
959
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, last: this.meta.page.cursor } });
947
960
  }
948
961
  hasNextPage() {
949
962
  return this.meta.page.more;
@@ -994,7 +1007,7 @@ const _Query = class {
994
1007
  __privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
995
1008
  __privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
996
1009
  __privateGet$5(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
997
- __privateGet$5(this, _data).page = data.page ?? parent?.page;
1010
+ __privateGet$5(this, _data).pagination = data.pagination ?? parent?.pagination;
998
1011
  __privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
999
1012
  this.any = this.any.bind(this);
1000
1013
  this.all = this.all.bind(this);
@@ -1009,8 +1022,8 @@ const _Query = class {
1009
1022
  return __privateGet$5(this, _data);
1010
1023
  }
1011
1024
  key() {
1012
- const { columns = [], filter = {}, sort = [], page = {} } = __privateGet$5(this, _data);
1013
- const key = JSON.stringify({ columns, filter, sort, page });
1025
+ const { columns = [], filter = {}, sort = [], pagination = {} } = __privateGet$5(this, _data);
1026
+ const key = JSON.stringify({ columns, filter, sort, pagination });
1014
1027
  return toBase64(key);
1015
1028
  }
1016
1029
  any(...queries) {
@@ -1052,17 +1065,18 @@ const _Query = class {
1052
1065
  return __privateGet$5(this, _repository).query(query);
1053
1066
  }
1054
1067
  async *[Symbol.asyncIterator]() {
1055
- for await (const [record] of this.getIterator(1)) {
1068
+ for await (const [record] of this.getIterator({ batchSize: 1 })) {
1056
1069
  yield record;
1057
1070
  }
1058
1071
  }
1059
- async *getIterator(chunk, options = {}) {
1072
+ async *getIterator(options = {}) {
1073
+ const { batchSize = 1 } = options;
1060
1074
  let offset = 0;
1061
1075
  let end = false;
1062
1076
  while (!end) {
1063
- const { records, meta } = await this.getPaginated({ ...options, page: { size: chunk, offset } });
1077
+ const { records, meta } = await this.getPaginated({ ...options, pagination: { size: batchSize, offset } });
1064
1078
  yield records;
1065
- offset += chunk;
1079
+ offset += batchSize;
1066
1080
  end = !meta.page.more;
1067
1081
  }
1068
1082
  }
@@ -1070,16 +1084,17 @@ const _Query = class {
1070
1084
  const { records } = await this.getPaginated(options);
1071
1085
  return records;
1072
1086
  }
1073
- async getAll(chunk = PAGINATION_MAX_SIZE, options = {}) {
1087
+ async getAll(options = {}) {
1088
+ const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
1074
1089
  const results = [];
1075
- for await (const page of this.getIterator(chunk, options)) {
1090
+ for await (const page of this.getIterator({ ...rest, batchSize })) {
1076
1091
  results.push(...page);
1077
1092
  }
1078
1093
  return results;
1079
1094
  }
1080
1095
  async getFirst(options = {}) {
1081
- const records = await this.getMany({ ...options, page: { size: 1 } });
1082
- return records[0] || null;
1096
+ const records = await this.getMany({ ...options, pagination: { size: 1 } });
1097
+ return records[0] ?? null;
1083
1098
  }
1084
1099
  cache(ttl) {
1085
1100
  return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
@@ -1091,10 +1106,10 @@ const _Query = class {
1091
1106
  return this.firstPage(size, offset);
1092
1107
  }
1093
1108
  firstPage(size, offset) {
1094
- return this.getPaginated({ page: { size, offset } });
1109
+ return this.getPaginated({ pagination: { size, offset } });
1095
1110
  }
1096
1111
  lastPage(size, offset) {
1097
- return this.getPaginated({ page: { size, offset, before: "end" } });
1112
+ return this.getPaginated({ pagination: { size, offset, before: "end" } });
1098
1113
  }
1099
1114
  hasNextPage() {
1100
1115
  return this.meta.page.more;
@@ -1294,9 +1309,13 @@ class RestRepository extends Query {
1294
1309
  }
1295
1310
  async search(query, options = {}) {
1296
1311
  const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1297
- const { records } = await searchBranch({
1298
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1299
- body: { tables: [__privateGet$4(this, _table)], query, fuzziness: options.fuzziness },
1312
+ const { records } = await searchTable({
1313
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1314
+ body: {
1315
+ query,
1316
+ fuzziness: options.fuzziness,
1317
+ filter: options.filter
1318
+ },
1300
1319
  ...fetchProps
1301
1320
  });
1302
1321
  const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
@@ -1309,8 +1328,8 @@ class RestRepository extends Query {
1309
1328
  const data = query.getQueryOptions();
1310
1329
  const body = {
1311
1330
  filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
1312
- sort: data.sort ? buildSortFilter(data.sort) : void 0,
1313
- page: data.page,
1331
+ sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
1332
+ page: data.pagination,
1314
1333
  columns: data.columns
1315
1334
  };
1316
1335
  const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
@@ -1453,8 +1472,8 @@ getCacheQuery_fn = async function(query) {
1453
1472
  if (!result)
1454
1473
  return null;
1455
1474
  const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
1456
- if (!ttl || ttl < 0)
1457
- return result;
1475
+ if (ttl < 0)
1476
+ return null;
1458
1477
  const hasExpired = result.date.getTime() + ttl < Date.now();
1459
1478
  return hasExpired ? null : result;
1460
1479
  };
@@ -1499,7 +1518,7 @@ const initObject = (db, schema, table, object) => {
1499
1518
  const linkTable = column.link?.table;
1500
1519
  if (!linkTable) {
1501
1520
  console.error(`Failed to parse link for field ${column.name}`);
1502
- } else if (value && isObject(value)) {
1521
+ } else if (isObject(value)) {
1503
1522
  result[column.name] = initObject(db, schema, linkTable, value);
1504
1523
  }
1505
1524
  break;
@@ -1625,7 +1644,7 @@ class SchemaPlugin extends XataPlugin {
1625
1644
  get: (_target, table) => {
1626
1645
  if (!isString(table))
1627
1646
  throw new Error("Invalid table name");
1628
- if (!__privateGet$2(this, _tables)[table]) {
1647
+ if (__privateGet$2(this, _tables)[table] === void 0) {
1629
1648
  __privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
1630
1649
  }
1631
1650
  return __privateGet$2(this, _tables)[table];
@@ -1728,30 +1747,39 @@ const envBranchNames = [
1728
1747
  "CF_PAGES_BRANCH",
1729
1748
  "BRANCH"
1730
1749
  ];
1731
- const defaultBranch = "main";
1732
1750
  async function getCurrentBranchName(options) {
1733
- const env = await getBranchByEnvVariable();
1734
- if (env)
1735
- return env;
1736
- const branch = await getGitBranch();
1737
- if (!branch)
1738
- return defaultBranch;
1739
- const details = await getDatabaseBranch(branch, options);
1740
- if (details)
1741
- return branch;
1742
- return defaultBranch;
1751
+ const env = getBranchByEnvVariable();
1752
+ if (env) {
1753
+ const details = await getDatabaseBranch(env, options);
1754
+ if (details)
1755
+ return env;
1756
+ console.warn(`Branch ${env} not found in Xata. Ignoring...`);
1757
+ }
1758
+ const gitBranch = await getGitBranch();
1759
+ return resolveXataBranch(gitBranch, options);
1743
1760
  }
1744
1761
  async function getCurrentBranchDetails(options) {
1745
- const env = await getBranchByEnvVariable();
1746
- if (env)
1747
- return getDatabaseBranch(env, options);
1748
- const branch = await getGitBranch();
1749
- if (!branch)
1750
- return getDatabaseBranch(defaultBranch, options);
1751
- const details = await getDatabaseBranch(branch, options);
1752
- if (details)
1753
- return details;
1754
- return getDatabaseBranch(defaultBranch, options);
1762
+ const branch = await getCurrentBranchName(options);
1763
+ return getDatabaseBranch(branch, options);
1764
+ }
1765
+ async function resolveXataBranch(gitBranch, options) {
1766
+ const databaseURL = options?.databaseURL || getDatabaseURL();
1767
+ const apiKey = options?.apiKey || getAPIKey();
1768
+ if (!databaseURL)
1769
+ throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
1770
+ if (!apiKey)
1771
+ throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
1772
+ const [protocol, , host, , dbName] = databaseURL.split("/");
1773
+ const [workspace] = host.split(".");
1774
+ const { branch } = await resolveBranch({
1775
+ apiKey,
1776
+ apiUrl: databaseURL,
1777
+ fetchImpl: getFetchImplementation(options?.fetchImpl),
1778
+ workspacesApiUrl: `${protocol}//${host}`,
1779
+ pathParams: { dbName, workspace },
1780
+ queryParams: { gitBranch, fallbackBranch: getEnvVariable("XATA_FALLBACK_BRANCH") }
1781
+ });
1782
+ return branch;
1755
1783
  }
1756
1784
  async function getDatabaseBranch(branch, options) {
1757
1785
  const databaseURL = options?.databaseURL || getDatabaseURL();
@@ -1840,7 +1868,7 @@ const buildClient = (plugins) => {
1840
1868
  this.db = db;
1841
1869
  this.search = search;
1842
1870
  for (const [key, namespace] of Object.entries(plugins ?? {})) {
1843
- if (!namespace)
1871
+ if (namespace === void 0)
1844
1872
  continue;
1845
1873
  const result = namespace.build(pluginOptions);
1846
1874
  if (result instanceof Promise) {
@@ -1857,7 +1885,7 @@ const buildClient = (plugins) => {
1857
1885
  const databaseURL = options?.databaseURL || getDatabaseURL();
1858
1886
  const apiKey = options?.apiKey || getAPIKey();
1859
1887
  const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
1860
- const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
1888
+ const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
1861
1889
  if (!databaseURL || !apiKey) {
1862
1890
  throw new Error("Options databaseURL and apiKey are required");
1863
1891
  }
@@ -1884,7 +1912,7 @@ const buildClient = (plugins) => {
1884
1912
  }, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
1885
1913
  if (__privateGet(this, _branch))
1886
1914
  return __privateGet(this, _branch);
1887
- if (!param)
1915
+ if (param === void 0)
1888
1916
  return void 0;
1889
1917
  const strategies = Array.isArray(param) ? [...param] : [param];
1890
1918
  const evaluateBranch = async (strategy) => {
@@ -1996,6 +2024,7 @@ exports.removeWorkspaceMember = removeWorkspaceMember;
1996
2024
  exports.resendWorkspaceMemberInvite = resendWorkspaceMemberInvite;
1997
2025
  exports.resolveBranch = resolveBranch;
1998
2026
  exports.searchBranch = searchBranch;
2027
+ exports.searchTable = searchTable;
1999
2028
  exports.setTableSchema = setTableSchema;
2000
2029
  exports.startsWith = startsWith;
2001
2030
  exports.updateBranchMetadata = updateBranchMetadata;