@xata.io/client 0.10.0 → 0.10.1

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.d.ts CHANGED
@@ -1151,14 +1151,15 @@ declare type ResolveBranchVariables = {
1151
1151
  } & FetcherExtraProps;
1152
1152
  /**
1153
1153
  * In order to resolve the database branch, the following algorithm is used:
1154
- * * if the `gitBranch` is found in the [git branches mapping](), the associated Xata branch is returned
1154
+ * * if the `gitBranch` was provided and is found in the [git branches mapping](/api-reference/dbs/db_name/gitBranches), the associated Xata branch is returned
1155
1155
  * * else, if a Xata branch with the exact same name as `gitBranch` exists, return it
1156
- * * else, return the default branch of the DB (currently `main` or the first branch)
1156
+ * * else, if `fallbackBranch` is provided and a branch with that name exists, return it
1157
+ * * else, return the default branch of the DB (`main` or the first branch)
1157
1158
  *
1158
1159
  * Example call:
1159
1160
  *
1160
1161
  * ```json
1161
- * // GET https://tutorial-ng7s8c.xata.sh/dbs/demo/dbs/demo/resolveBranch?gitBranch=test?fallbackBranch=tsg
1162
+ * // GET https://tutorial-ng7s8c.xata.sh/dbs/demo/dbs/demo/resolveBranch?gitBranch=test&fallbackBranch=tsg
1162
1163
  * ```
1163
1164
  *
1164
1165
  * Example response:
@@ -1458,8 +1459,9 @@ declare type UpdateTableVariables = {
1458
1459
  *
1459
1460
  * In the example below, we rename a table from “users” to “people”:
1460
1461
  *
1461
- * ```jsx
1462
- * PATCH /db/test:main/tables/users
1462
+ * ```json
1463
+ * // PATCH /db/test:main/tables/users
1464
+ *
1463
1465
  * {
1464
1466
  * "name": "people"
1465
1467
  * }
@@ -1867,7 +1869,7 @@ declare type QueryTableVariables = {
1867
1869
  * {
1868
1870
  * "columns": [...],
1869
1871
  * "filter": {
1870
- * "$all": [...]
1872
+ * "$all": [...],
1871
1873
  * "$any": [...]
1872
1874
  * ...
1873
1875
  * },
@@ -2005,7 +2007,7 @@ declare type QueryTableVariables = {
2005
2007
  * {
2006
2008
  * "name": "Kilian",
2007
2009
  * "address": {
2008
- * "street": "New street",
2010
+ * "street": "New street"
2009
2011
  * }
2010
2012
  * }
2011
2013
  * ```
@@ -2014,10 +2016,7 @@ declare type QueryTableVariables = {
2014
2016
  *
2015
2017
  * ```json
2016
2018
  * {
2017
- * "columns": [
2018
- * "*",
2019
- * "team.name"
2020
- * ]
2019
+ * "columns": ["*", "team.name"]
2021
2020
  * }
2022
2021
  * ```
2023
2022
  *
@@ -2035,7 +2034,7 @@ declare type QueryTableVariables = {
2035
2034
  * "team": {
2036
2035
  * "id": "XX",
2037
2036
  * "xata": {
2038
- * "version": 0,
2037
+ * "version": 0
2039
2038
  * },
2040
2039
  * "name": "first team"
2041
2040
  * }
@@ -2046,10 +2045,7 @@ declare type QueryTableVariables = {
2046
2045
  *
2047
2046
  * ```json
2048
2047
  * {
2049
- * "columns": [
2050
- * "*",
2051
- * "team.*"
2052
- * ]
2048
+ * "columns": ["*", "team.*"]
2053
2049
  * }
2054
2050
  * ```
2055
2051
  *
@@ -2067,7 +2063,7 @@ declare type QueryTableVariables = {
2067
2063
  * "team": {
2068
2064
  * "id": "XX",
2069
2065
  * "xata": {
2070
- * "version": 0,
2066
+ * "version": 0
2071
2067
  * },
2072
2068
  * "name": "first team",
2073
2069
  * "code": "A1",
@@ -2117,7 +2113,7 @@ declare type QueryTableVariables = {
2117
2113
  * ```json
2118
2114
  * {
2119
2115
  * "filter": {
2120
- * "name": "r2",
2116
+ * "name": "r2"
2121
2117
  * }
2122
2118
  * }
2123
2119
  * ```
@@ -2139,7 +2135,7 @@ declare type QueryTableVariables = {
2139
2135
  * ```json
2140
2136
  * {
2141
2137
  * "filter": {
2142
- * "settings.plan": "free",
2138
+ * "settings.plan": "free"
2143
2139
  * }
2144
2140
  * }
2145
2141
  * ```
@@ -2149,8 +2145,8 @@ declare type QueryTableVariables = {
2149
2145
  * "filter": {
2150
2146
  * "settings": {
2151
2147
  * "plan": "free"
2152
- * },
2153
- * },
2148
+ * }
2149
+ * }
2154
2150
  * }
2155
2151
  * ```
2156
2152
  *
@@ -2159,8 +2155,8 @@ declare type QueryTableVariables = {
2159
2155
  * ```json
2160
2156
  * {
2161
2157
  * "filter": {
2162
- * "settings.plan": {"$any": ["free", "paid"]}
2163
- * },
2158
+ * "settings.plan": { "$any": ["free", "paid"] }
2159
+ * }
2164
2160
  * }
2165
2161
  * ```
2166
2162
  *
@@ -2169,9 +2165,9 @@ declare type QueryTableVariables = {
2169
2165
  * ```json
2170
2166
  * {
2171
2167
  * "filter": {
2172
- * "settings.dark": true,
2173
- * "settings.plan": "free",
2174
- * },
2168
+ * "settings.dark": true,
2169
+ * "settings.plan": "free"
2170
+ * }
2175
2171
  * }
2176
2172
  * ```
2177
2173
  *
@@ -2182,11 +2178,11 @@ declare type QueryTableVariables = {
2182
2178
  * ```json
2183
2179
  * {
2184
2180
  * "filter": {
2185
- * "$any": {
2186
- * "settings.dark": true,
2187
- * "settings.plan": "free"
2188
- * }
2189
- * },
2181
+ * "$any": {
2182
+ * "settings.dark": true,
2183
+ * "settings.plan": "free"
2184
+ * }
2185
+ * }
2190
2186
  * }
2191
2187
  * ```
2192
2188
  *
@@ -2197,10 +2193,10 @@ declare type QueryTableVariables = {
2197
2193
  * "filter": {
2198
2194
  * "$any": [
2199
2195
  * {
2200
- * "name": "r1",
2196
+ * "name": "r1"
2201
2197
  * },
2202
2198
  * {
2203
- * "name": "r2",
2199
+ * "name": "r2"
2204
2200
  * }
2205
2201
  * ]
2206
2202
  * }
@@ -2212,7 +2208,7 @@ declare type QueryTableVariables = {
2212
2208
  * ```json
2213
2209
  * {
2214
2210
  * "filter": {
2215
- * "$exists": "settings",
2211
+ * "$exists": "settings"
2216
2212
  * }
2217
2213
  * }
2218
2214
  * ```
@@ -2224,10 +2220,10 @@ declare type QueryTableVariables = {
2224
2220
  * "filter": {
2225
2221
  * "$all": [
2226
2222
  * {
2227
- * "$exists": "settings",
2223
+ * "$exists": "settings"
2228
2224
  * },
2229
2225
  * {
2230
- * "$exists": "name",
2226
+ * "$exists": "name"
2231
2227
  * }
2232
2228
  * ]
2233
2229
  * }
@@ -2239,7 +2235,7 @@ declare type QueryTableVariables = {
2239
2235
  * ```json
2240
2236
  * {
2241
2237
  * "filter": {
2242
- * "$notExists": "settings",
2238
+ * "$notExists": "settings"
2243
2239
  * }
2244
2240
  * }
2245
2241
  * ```
@@ -2266,7 +2262,7 @@ declare type QueryTableVariables = {
2266
2262
  * {
2267
2263
  * "filter": {
2268
2264
  * "<column_name>": {
2269
- * "$pattern": "v*alue*"
2265
+ * "$pattern": "v*alue*"
2270
2266
  * }
2271
2267
  * }
2272
2268
  * }
@@ -2278,10 +2274,10 @@ declare type QueryTableVariables = {
2278
2274
  * {
2279
2275
  * "filter": {
2280
2276
  * "<column_name>": {
2281
- * "$endsWith": ".gz"
2277
+ * "$endsWith": ".gz"
2282
2278
  * },
2283
2279
  * "<column_name>": {
2284
- * "$startsWith": "tmp-"
2280
+ * "$startsWith": "tmp-"
2285
2281
  * }
2286
2282
  * }
2287
2283
  * }
@@ -2292,10 +2288,10 @@ declare type QueryTableVariables = {
2292
2288
  * ```json
2293
2289
  * {
2294
2290
  * "filter": {
2295
- * "<column_name>": {
2296
- * "$ge": 0,
2297
- * "$lt": 100
2298
- * }
2291
+ * "<column_name>": {
2292
+ * "$ge": 0,
2293
+ * "$lt": 100
2294
+ * }
2299
2295
  * }
2300
2296
  * }
2301
2297
  * ```
@@ -2313,7 +2309,6 @@ declare type QueryTableVariables = {
2313
2309
  * ```
2314
2310
  * The supported operators are `$gt`, `$lt`, `$ge`, `$le`.
2315
2311
  *
2316
- *
2317
2312
  * #### Negations
2318
2313
  *
2319
2314
  * A general `$not` operator can inverse any operation.
@@ -2338,15 +2333,21 @@ declare type QueryTableVariables = {
2338
2333
  * {
2339
2334
  * "filter": {
2340
2335
  * "$not": {
2341
- * "$any": [{
2342
- * "<column_name1>": "value1"
2343
- * }, {
2344
- * "$all": [{
2345
- * "<column_name2>": "value2"
2346
- * }, {
2347
- * "<column_name3>": "value3"
2348
- * }]
2349
- * }]
2336
+ * "$any": [
2337
+ * {
2338
+ * "<column_name1>": "value1"
2339
+ * },
2340
+ * {
2341
+ * "$all": [
2342
+ * {
2343
+ * "<column_name2>": "value2"
2344
+ * },
2345
+ * {
2346
+ * "<column_name3>": "value3"
2347
+ * }
2348
+ * ]
2349
+ * }
2350
+ * ]
2350
2351
  * }
2351
2352
  * }
2352
2353
  * }
@@ -2401,8 +2402,8 @@ declare type QueryTableVariables = {
2401
2402
  * "<array name>": {
2402
2403
  * "$includes": {
2403
2404
  * "$all": [
2404
- * {"$contains": "label"},
2405
- * {"$not": {"$endsWith": "-debug"}}
2405
+ * { "$contains": "label" },
2406
+ * { "$not": { "$endsWith": "-debug" } }
2406
2407
  * ]
2407
2408
  * }
2408
2409
  * }
@@ -2422,9 +2423,7 @@ declare type QueryTableVariables = {
2422
2423
  * {
2423
2424
  * "filter": {
2424
2425
  * "settings.labels": {
2425
- * "$includesAll": [
2426
- * {"$contains": "label"},
2427
- * ]
2426
+ * "$includesAll": [{ "$contains": "label" }]
2428
2427
  * }
2429
2428
  * }
2430
2429
  * }
@@ -2472,7 +2471,6 @@ declare type QueryTableVariables = {
2472
2471
  * }
2473
2472
  * ```
2474
2473
  *
2475
- *
2476
2474
  * ### Pagination
2477
2475
  *
2478
2476
  * We offer cursor pagination and offset pagination. The offset pagination is limited
@@ -2538,8 +2536,8 @@ declare type QueryTableVariables = {
2538
2536
  * can be queried by update `page.after` to the returned cursor while keeping the
2539
2537
  * `page.before` cursor from the first range query.
2540
2538
  *
2541
- * The `filter` , `columns`, `sort` , and `page.size` configuration will be
2542
- * encoded with the cursor. The pagination request will be invalid if
2539
+ * The `filter` , `columns`, `sort` , and `page.size` configuration will be
2540
+ * encoded with the cursor. The pagination request will be invalid if
2543
2541
  * `filter` or `sort` is set. The columns returned and page size can be changed
2544
2542
  * anytime by passing the `columns` or `page.size` settings to the next query.
2545
2543
  *
package/dist/index.mjs CHANGED
@@ -1077,7 +1077,7 @@ const _Query = class {
1077
1077
  }
1078
1078
  async getFirst(options = {}) {
1079
1079
  const records = await this.getMany({ ...options, pagination: { size: 1 } });
1080
- return records[0] || null;
1080
+ return records[0] ?? null;
1081
1081
  }
1082
1082
  cache(ttl) {
1083
1083
  return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
@@ -1307,7 +1307,7 @@ class RestRepository extends Query {
1307
1307
  const data = query.getQueryOptions();
1308
1308
  const body = {
1309
1309
  filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
1310
- sort: data.sort ? buildSortFilter(data.sort) : void 0,
1310
+ sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
1311
1311
  page: data.pagination,
1312
1312
  columns: data.columns
1313
1313
  };
@@ -1497,7 +1497,7 @@ const initObject = (db, schema, table, object) => {
1497
1497
  const linkTable = column.link?.table;
1498
1498
  if (!linkTable) {
1499
1499
  console.error(`Failed to parse link for field ${column.name}`);
1500
- } else if (value && isObject(value)) {
1500
+ } else if (isObject(value)) {
1501
1501
  result[column.name] = initObject(db, schema, linkTable, value);
1502
1502
  }
1503
1503
  break;
@@ -1623,7 +1623,7 @@ class SchemaPlugin extends XataPlugin {
1623
1623
  get: (_target, table) => {
1624
1624
  if (!isString(table))
1625
1625
  throw new Error("Invalid table name");
1626
- if (!__privateGet$2(this, _tables)[table]) {
1626
+ if (__privateGet$2(this, _tables)[table] === void 0) {
1627
1627
  __privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
1628
1628
  }
1629
1629
  return __privateGet$2(this, _tables)[table];
@@ -1726,30 +1726,39 @@ const envBranchNames = [
1726
1726
  "CF_PAGES_BRANCH",
1727
1727
  "BRANCH"
1728
1728
  ];
1729
- const defaultBranch = "main";
1730
1729
  async function getCurrentBranchName(options) {
1731
- const env = await getBranchByEnvVariable();
1732
- if (env)
1733
- return env;
1734
- const branch = await getGitBranch();
1735
- if (!branch)
1736
- return defaultBranch;
1737
- const details = await getDatabaseBranch(branch, options);
1738
- if (details)
1739
- return branch;
1740
- return defaultBranch;
1730
+ const env = getBranchByEnvVariable();
1731
+ if (env) {
1732
+ const details = await getDatabaseBranch(env, options);
1733
+ if (details)
1734
+ return env;
1735
+ console.warn(`Branch ${env} not found in Xata. Ignoring...`);
1736
+ }
1737
+ const gitBranch = await getGitBranch();
1738
+ return resolveXataBranch(gitBranch, options);
1741
1739
  }
1742
1740
  async function getCurrentBranchDetails(options) {
1743
- const env = await getBranchByEnvVariable();
1744
- if (env)
1745
- return getDatabaseBranch(env, options);
1746
- const branch = await getGitBranch();
1747
- if (!branch)
1748
- return getDatabaseBranch(defaultBranch, options);
1749
- const details = await getDatabaseBranch(branch, options);
1750
- if (details)
1751
- return details;
1752
- return getDatabaseBranch(defaultBranch, options);
1741
+ const branch = await getCurrentBranchName(options);
1742
+ return getDatabaseBranch(branch, options);
1743
+ }
1744
+ async function resolveXataBranch(gitBranch, options) {
1745
+ const databaseURL = options?.databaseURL || getDatabaseURL();
1746
+ const apiKey = options?.apiKey || getAPIKey();
1747
+ if (!databaseURL)
1748
+ throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
1749
+ if (!apiKey)
1750
+ throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
1751
+ const [protocol, , host, , dbName] = databaseURL.split("/");
1752
+ const [workspace] = host.split(".");
1753
+ const { branch } = await resolveBranch({
1754
+ apiKey,
1755
+ apiUrl: databaseURL,
1756
+ fetchImpl: getFetchImplementation(options?.fetchImpl),
1757
+ workspacesApiUrl: `${protocol}//${host}`,
1758
+ pathParams: { dbName, workspace },
1759
+ queryParams: { gitBranch, fallbackBranch: getEnvVariable("XATA_FALLBACK_BRANCH") }
1760
+ });
1761
+ return branch;
1753
1762
  }
1754
1763
  async function getDatabaseBranch(branch, options) {
1755
1764
  const databaseURL = options?.databaseURL || getDatabaseURL();
@@ -1838,7 +1847,7 @@ const buildClient = (plugins) => {
1838
1847
  this.db = db;
1839
1848
  this.search = search;
1840
1849
  for (const [key, namespace] of Object.entries(plugins ?? {})) {
1841
- if (!namespace)
1850
+ if (namespace === void 0)
1842
1851
  continue;
1843
1852
  const result = namespace.build(pluginOptions);
1844
1853
  if (result instanceof Promise) {
@@ -1855,7 +1864,7 @@ const buildClient = (plugins) => {
1855
1864
  const databaseURL = options?.databaseURL || getDatabaseURL();
1856
1865
  const apiKey = options?.apiKey || getAPIKey();
1857
1866
  const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
1858
- const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
1867
+ const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
1859
1868
  if (!databaseURL || !apiKey) {
1860
1869
  throw new Error("Options databaseURL and apiKey are required");
1861
1870
  }
@@ -1882,7 +1891,7 @@ const buildClient = (plugins) => {
1882
1891
  }, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
1883
1892
  if (__privateGet(this, _branch))
1884
1893
  return __privateGet(this, _branch);
1885
- if (!param)
1894
+ if (param === void 0)
1886
1895
  return void 0;
1887
1896
  const strategies = Array.isArray(param) ? [...param] : [param];
1888
1897
  const evaluateBranch = async (strategy) => {