@xata.io/client 0.13.0 → 0.13.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/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # @xata.io/client
2
2
 
3
+ ## 0.13.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#417](https://github.com/xataio/client-ts/pull/417) [`86a14ec`](https://github.com/xataio/client-ts/commit/86a14eccbca94f572252327c9c0306577a1c3ebd) Thanks [@SferaDev](https://github.com/SferaDev)! - Update User-Agent
8
+
9
+ * [#422](https://github.com/xataio/client-ts/pull/422) [`2896418`](https://github.com/xataio/client-ts/commit/289641844e5b2752197dbbbf3a93ef6068b684e4) Thanks [@SferaDev](https://github.com/SferaDev)! - Allow sending link as string id
10
+
11
+ - [#420](https://github.com/xataio/client-ts/pull/420) [`301b21f`](https://github.com/xataio/client-ts/commit/301b21f4784f755a8694ca21a0f2fd48e1b16df4) Thanks [@SferaDev](https://github.com/SferaDev)! - Exclude date internal columns in selection
12
+
13
+ * [#399](https://github.com/xataio/client-ts/pull/399) [`dd4b2ef`](https://github.com/xataio/client-ts/commit/dd4b2effed2251ac8afbfb2909c21a9deb35bef1) Thanks [@SferaDev](https://github.com/SferaDev)! - Allow create many objects mixed some with ids others without
14
+
15
+ - [#425](https://github.com/xataio/client-ts/pull/425) [`00279ff`](https://github.com/xataio/client-ts/commit/00279ff985793020237f8098cba97dfec7738f82) Thanks [@SferaDev](https://github.com/SferaDev)! - Do not send falsy values to query string
16
+
17
+ * [#399](https://github.com/xataio/client-ts/pull/399) [`8e66998`](https://github.com/xataio/client-ts/commit/8e6699867a1aa1968d12db4ced80c13d4b951f88) Thanks [@SferaDev](https://github.com/SferaDev)! - Allow passing identifiable objects to `read()` operations
18
+
19
+ - [#425](https://github.com/xataio/client-ts/pull/425) [`8a4e019`](https://github.com/xataio/client-ts/commit/8a4e019031e678d946cf9dfb0e4803906fad9b5f) Thanks [@SferaDev](https://github.com/SferaDev)! - Add fallback branch to `api.databases.resolveBranch`
20
+
3
21
  ## 0.13.0
4
22
 
5
23
  ### Minor Changes
package/Usage.md ADDED
@@ -0,0 +1,380 @@
1
+ # TypeScript SDK
2
+
3
+ There're four types of objects in the Xata TypeScript SDK:
4
+
5
+ - `Repository`: a table representation that can be used to create, read, update, and delete records.
6
+ - `Query`: a combination of filters and other parameters to retrieve a collection of records.
7
+ - `XataRecord`: a row in a table.
8
+ - `Page`: a collection of records that can be paginated.
9
+
10
+ ## Repository
11
+
12
+ Any table in the database can be represented by a `Repository` object.
13
+
14
+ A repository is an object that can be used to create, read, update, and delete records in the table it represents.
15
+
16
+ It also implements the `Query` and `Page` interfaces, so you can use it to query and paginate the records in the table too. We'll see how to use these objects in the next section.
17
+
18
+ ### Reading records
19
+
20
+ The `read()` method can be used to read a records by their ids:
21
+
22
+ - If the object cannot be found, the method will return `null`.
23
+ - If the object can be found, the method will return a `XataRecord` object.
24
+
25
+ You can read a single record by its id.
26
+
27
+ ```ts
28
+ const user = await xata.db.users.read('rec_1234abcdef');
29
+ ```
30
+
31
+ You can also read multiple records by their ids.
32
+
33
+ ```ts
34
+ const users = await xata.db.users.read(['rec_1234abcdef', 'rec_5678defgh']);
35
+ ```
36
+
37
+ And you can read records coming from an object that contains an `id` property.
38
+
39
+ ```ts
40
+ const object1 = { id: 'rec_1234abcdef' };
41
+ const object2 = { id: 'rec_5678defgh' };
42
+
43
+ const user = await xata.db.users.read(object1);
44
+ const users = await xata.db.users.read([object1, object2]);
45
+ ```
46
+
47
+ ### Creating records
48
+
49
+ Both the `create()` and `createOrUpdate()` methods can be used to create a new record.
50
+
51
+ - The `create()` method will create a new record and fail if the provided id already exists.
52
+ - The `createOrUpdate()` method will create a new record if the provided id doesn't exist, or update an existing record if it does.
53
+
54
+ You can create a record without providing an id, and the id will be generated automatically.
55
+
56
+ ```ts
57
+ const user = await xata.db.users.create({ fullName: 'John Smith' });
58
+ user.id; // 'rec_1234abcdef'
59
+ ```
60
+
61
+ You can create a record with an id as parameter, and the id will be used.
62
+
63
+ ```ts
64
+ const user = await xata.db.users.create('user_admin', { fullName: 'John Smith' });
65
+ user.id; // 'user_admin'
66
+ ```
67
+
68
+ You can create a record with the id provided in the object, and the id will be used.
69
+
70
+ ```ts
71
+ const user = await xata.db.users.create({ id: 'user_admin', fullName: 'John Smith' });
72
+ user.id; // 'user_admin'
73
+ ```
74
+
75
+ You can create multiple records at once by providing an array of objects.
76
+
77
+ ```ts
78
+ const users = await xata.db.users.create([{ fullName: 'John Smith' }, { id: 'user_admin', fullName: 'Jane Doe' }]);
79
+ users[0].id; // 'rec_1234abcdef'
80
+ users[1].id; // 'user_admin'
81
+ ```
82
+
83
+ The `createOrUpdate()` method beaves the same way as `create()` but you will always need to provide an id.
84
+
85
+ ```ts
86
+ const user1 = await xata.db.users.createOrUpdate('user_admin', { fullName: 'John Smith' });
87
+ const user2 = await xata.db.users.createOrUpdate({ id: 'user_manager', fullName: 'Jane Doe' });
88
+ const users = await xata.db.users.createOrUpdate([
89
+ { id: 'user_admin', fullName: 'John Smith' },
90
+ { id: 'user_manager', fullName: 'Jane Doe' }
91
+ ]);
92
+ ```
93
+
94
+ ### Updating records
95
+
96
+ The `update()` method can be used to update an existing record. It will throw an Error if the record cannot be found.
97
+
98
+ ```ts
99
+ const user = await xata.db.users.update('rec_1234abcdef', { fullName: 'John Smith' });
100
+ ```
101
+
102
+ The `id` property can also be sent in the object update.
103
+
104
+ ```ts
105
+ const user = await xata.db.users.update({ id: 'user_admin', fullName: 'John Smith' });
106
+ ```
107
+
108
+ You can update multiple records at once by providing an array of objects.
109
+
110
+ ```ts
111
+ const users = await xata.db.users.update([
112
+ { id: 'rec_1234abcdef', fullName: 'John Smith' },
113
+ { id: 'user_admin', fullName: 'Jane Doe' }
114
+ ]);
115
+ ```
116
+
117
+ ### Deleting records
118
+
119
+ The `delete()` method can be used to delete an existing record. It will throw an Error if the record cannot be found.
120
+
121
+ ```ts
122
+ const user = await xata.db.users.delete('rec_1234abcdef');
123
+ ```
124
+
125
+ You can delete multiple records at once by providing an array of ids.
126
+
127
+ ```ts
128
+ const users = await xata.db.users.delete(['rec_1234abcdef', 'user_admin']);
129
+ ```
130
+
131
+ You can delete records coming from an object that contains an `id` property.
132
+
133
+ ```ts
134
+ const object1 = { id: 'rec_1234abcdef' };
135
+
136
+ const user = await xata.db.users.delete(object1);
137
+ ```
138
+
139
+ You can delete records coming from an array of objects that contain an `id` property.
140
+
141
+ ```ts
142
+ const object1 = { id: 'rec_1234abcdef' };
143
+ const object2 = { id: 'user_admin' };
144
+
145
+ const users = await xata.db.users.delete([object1, object2]);
146
+ ```
147
+
148
+ ### Searching records
149
+
150
+ The `search()` method can be used to search records. It returns an array of records.
151
+
152
+ ```ts
153
+ const results = await xata.db.users.search('John');
154
+ ```
155
+
156
+ Also you can customize the results with an `options` object that includes `fuzziness`, `filter` and all the other options the API supports.
157
+
158
+ ```ts
159
+ const results = await xata.db.users.search('John', { fuzziness: 1, filter: { 'team.name': 'Marketing' } });
160
+ ```
161
+
162
+ ## Query
163
+
164
+ To get a collection of records, you can use the `Query` object.
165
+
166
+ It provides the following methods:
167
+
168
+ - `getFirst()`: returns the first record in the query results.
169
+ - `getPaginated()`: returns a page of records in the query results.
170
+ - `getAll()`: returns all the records in the query results.
171
+ - `getMany()`: returns an array of some records in the query results.
172
+
173
+ Since the `Repository` class implements the `Query` interface, you can use it to query and paginate the records in the table too.
174
+
175
+ ```ts
176
+ const user = xata.db.users.getFirst();
177
+ ```
178
+
179
+ ### Column selection
180
+
181
+ The `Query` object can be used to select the columns that will be returned in the results.
182
+
183
+ You can pick multiple columns by providing an array of column names, or you can pick all the columns by providing `*`.
184
+
185
+ The dot notation is supported to select columns from nested objects.
186
+
187
+ ```ts
188
+ const user = xata.db.users.select(['*', 'team.*']).getFirst();
189
+ ```
190
+
191
+ ### Sorting
192
+
193
+ The `Query` object can be used to sort the order of the results.
194
+
195
+ You can sort the results by providing a column name and an `asc` or `desc` string.
196
+
197
+ ```ts
198
+ const user = xata.db.users.orderBy('fullName', 'asc').getFirst();
199
+ ```
200
+
201
+ ### Filtering
202
+
203
+ You can filter the results by providing the column and the value to filter.
204
+
205
+ ```ts
206
+ const user = xata.db.users.filter('fullName', 'John').getFirst();
207
+ ```
208
+
209
+ To combine multiple filters in an 'AND' clause, you can pipe the filters together.
210
+
211
+ ```ts
212
+ const user = xata.db.users.filter('fullName', 'John').filter('team.name', 'Marketing').getFirst();
213
+ ```
214
+
215
+ Also you can filter the results by providing a `filter` object.
216
+
217
+ ```ts
218
+ const user = xata.db.users.filter({ fullName: 'John', 'team.name': 'Marketing' }).getFirst();
219
+ ```
220
+
221
+ We offer some helper functions to build the filter values, like: `gt`, `ge`, `gte`, `lt`, `le`, `lte`, `exists`, `notExists`, `startsWith`, `endsWith`, `pattern`, `is`, `isNot`, `contains`, `includes`, and others specific to the type of the column.
222
+
223
+ ```ts
224
+ const user = xata.db.users.filter('name', startsWith('Bar')).getFirst();
225
+ ```
226
+
227
+ If you prefer to directly use the filter operators as in the API, you can add them in the `filter` object.
228
+
229
+ ```ts
230
+ xata.db.users.filter({ full_name: { $startsWith: 'foo' } }).getFirst();
231
+ ```
232
+
233
+ ### Combining queries
234
+
235
+ Queries can be stored in variables and can be combined with other queries.
236
+
237
+ ```ts
238
+ const johnQuery = xata.db.users.filter('fullName', 'John');
239
+ const janeQuery = xata.db.users.filter('fullName', 'Jane');
240
+
241
+ const johns = await johnQuery.getAll();
242
+ const janes = await janeQuery.getAll();
243
+
244
+ const users = await xata.db.users.any(johnQuery, janeQuery).getAll();
245
+ ```
246
+
247
+ We offer the following helper methods to combine queries:
248
+
249
+ - `any()`: returns the records that match any of the queries.
250
+ - `all()`: returns the records that match all of the queries.
251
+ - `none()`: returns the records that match none of the queries.
252
+ - `not()`: returns the records that don't match the given query.
253
+
254
+ You can read more about the query operators in the API section for the query table endpoint.
255
+
256
+ ## Page
257
+
258
+ Some methods of the `Query` interface provide a `Page` object as a return value that can be used to paginate the results.
259
+
260
+ The `Page` object can be used to get the queried records of a table in pages. It is an abstraction of cursor-based pagination.
261
+
262
+ It contains:
263
+
264
+ - `records`: Array of `XataRecord` objects.
265
+ - `hasNextPage`: Function that returns a boolean indicating if there is a next page.
266
+ - `nextPage`: Async function that can be used to get the next page.
267
+ - `previousPage`: Async function that can be used to get the previous page.
268
+ - `firstPage`: Async function that can be used to get the first page.
269
+ - `lastPage`: Async function that can be used to get the last page.
270
+ - `meta`: Information about the current page and its cursor.
271
+
272
+ ```ts
273
+ const page = await xata.db.users.getPaginated();
274
+ page.records; // Array of `XataRecord` objects.
275
+ page.hasNextPage();
276
+
277
+ const page2 = await page.nextPage();
278
+ page2.records; // Array of `XataRecord` objects.
279
+
280
+ const page1 = await page2.previousPage();
281
+ page1.records; // Array of `XataRecord` objects.
282
+
283
+ const firstPage = await page1.firstPage();
284
+ firstPage.records; // Array of `XataRecord` objects.
285
+ ```
286
+
287
+ The `Repository` class implements the `Query` interface, so you can use it to paginate the records in the table too.
288
+
289
+ ```ts
290
+ const page = await xata.db.users.firstPage();
291
+ page.records; // Array of `XataRecord` objects.
292
+ ```
293
+
294
+ The array returned in `records` also implements the `Page` interface.
295
+
296
+ ```ts
297
+ const { records } = await xata.db.users.getPaginated();
298
+ records.hasNextPage();
299
+ const { records: page2Records } = await records.nextPage();
300
+ ```
301
+
302
+ Optionally you can provide `offset` and `size` parameters to the pagination and override the default values.
303
+
304
+ ```ts
305
+ const page = await xata.db.users.getPaginated();
306
+ page.records; // Array of `XataRecord` objects.
307
+
308
+ // A second page with size 50
309
+ const page2 = await page.nextPage(50);
310
+
311
+ // A third page with size 10 but an offset of 60
312
+ const page3 = await page2.nextPage(10, 60);
313
+ ```
314
+
315
+ ### Iterators and generators
316
+
317
+ The `Query` object can be used to iterate over the results as a way to paginate the results.
318
+
319
+ ```ts
320
+ for await (const user of xata.db.users) {
321
+ await user.update({ full_name: 'John Doe' });
322
+ }
323
+ ```
324
+
325
+ Also if you want to retrieve more than one record at a time in the iterator, you can use the `getIterator()` method.
326
+
327
+ ```ts
328
+ for await (const users of xata.db.users.getIterator({ batchSize: 50 })) {
329
+ console.log(users);
330
+ }
331
+ ```
332
+
333
+ ### Helper variables
334
+
335
+ We expose some helper variables of the API limits when paginating:
336
+
337
+ - `PAGINATION_MAX_SIZE`: Maximum page size.
338
+ - `PAGINATION_DEFAULT_SIZE`: Default page size.
339
+ - `PAGINATION_MAX_OFFSET`: Maximum offset.
340
+ - `PAGINATION_DEFAULT_OFFSET`: Default offset.
341
+
342
+ You can use these variables if you implement your own pagination mechanism, as they will be updated when the API limits are updated.
343
+
344
+ ## XataRecord
345
+
346
+ Every row in a table is represented by an `XataRecord` object.
347
+
348
+ It contains an `id` property that represents the unique identifier of the record and all the fields of the table.
349
+
350
+ Also it provides some methods to read again, update or delete the record.
351
+
352
+ ```ts
353
+ const user = await xata.db.users.read('rec_1234abcdef');
354
+ user?.id; // 'rec_1234abcdef'
355
+ user?.fullName; // 'John Smith'
356
+ user?.read(); // Reads the record again
357
+ user?.update({ fullName: 'John Doe' }); // Partially updates the record
358
+ user?.delete(); // Deletes the record
359
+ ```
360
+
361
+ If the table contains a link property, it will be represented as a `Link` object containing its `id` property and methods to read again or update the linked record.
362
+
363
+ ```ts
364
+ const user = await xata.db.users.read('rec_1234abcdef');
365
+ user?.team?.id; // 'rec_5678defgh'
366
+ user?.team?.read(); // Reads the linked record properties
367
+ user?.team?.update({ name: 'A team' }); // Partially updates the linked record
368
+ ```
369
+
370
+ When working with `queries` you can expand the properties returned in a `XataRecord` object.
371
+
372
+ By default only the current table fields are returned, and the id of any linked record.
373
+
374
+ ```ts
375
+ const user = await xata.db.users.filter('id', 'rec_1234abcdef').select(['*', 'team.*']).getFirst();
376
+ user?.id; // 'rec_1234abcdef'
377
+ user?.fullName; // 'John Smith'
378
+ user?.team?.id; // 'rec_5678defgh'
379
+ user?.team?.name; // 'A team'
380
+ ```
package/dist/index.cjs CHANGED
@@ -77,6 +77,8 @@ function getFetchImplementation(userFetch) {
77
77
  return fetchImpl;
78
78
  }
79
79
 
80
+ const VERSION = "0.13.1";
81
+
80
82
  class ErrorWithCause extends Error {
81
83
  constructor(message, options) {
82
84
  super(message, options);
@@ -114,7 +116,12 @@ function getMessage(data) {
114
116
  }
115
117
 
116
118
  const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
117
- const query = new URLSearchParams(queryParams).toString();
119
+ const cleanQueryParams = Object.entries(queryParams).reduce((acc, [key, value]) => {
120
+ if (value === void 0 || value === null)
121
+ return acc;
122
+ return { ...acc, [key]: value };
123
+ }, {});
124
+ const query = new URLSearchParams(cleanQueryParams).toString();
118
125
  const queryString = query.length > 0 ? `?${query}` : "";
119
126
  return url.replace(/\{\w*\}/g, (key) => pathParams[key.slice(1, -1)]) + queryString;
120
127
  };
@@ -154,6 +161,7 @@ async function fetch$1({
154
161
  body: body ? JSON.stringify(body) : void 0,
155
162
  headers: {
156
163
  "Content-Type": "application/json",
164
+ "User-Agent": `Xata client-ts/${VERSION}`,
157
165
  ...headers,
158
166
  ...hostHeader(fullUrl),
159
167
  Authorization: `Bearer ${apiKey}`
@@ -693,10 +701,10 @@ class DatabaseApi {
693
701
  ...this.extraProps
694
702
  });
695
703
  }
696
- resolveBranch(workspace, dbName, gitBranch) {
704
+ resolveBranch(workspace, dbName, gitBranch, fallbackBranch) {
697
705
  return operationsByTag.database.resolveBranch({
698
706
  pathParams: { workspace, dbName },
699
- queryParams: { gitBranch },
707
+ queryParams: { gitBranch, fallbackBranch },
700
708
  ...this.extraProps
701
709
  });
702
710
  }
@@ -1254,9 +1262,29 @@ class RestRepository extends Query {
1254
1262
  if (Array.isArray(a)) {
1255
1263
  if (a.length === 0)
1256
1264
  return [];
1257
- const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
1258
- await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
1259
- return records;
1265
+ const [itemsWithoutIds, itemsWithIds, order] = a.reduce(([accWithoutIds, accWithIds, accOrder], item) => {
1266
+ const condition = isString(item.id);
1267
+ accOrder.push(condition);
1268
+ if (condition) {
1269
+ accWithIds.push(item);
1270
+ } else {
1271
+ accWithoutIds.push(item);
1272
+ }
1273
+ return [accWithoutIds, accWithIds, accOrder];
1274
+ }, [[], [], []]);
1275
+ const recordsWithoutId = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, itemsWithoutIds);
1276
+ await Promise.all(recordsWithoutId.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
1277
+ if (itemsWithIds.length > 100) {
1278
+ console.warn("Bulk create operation with id is not optimized in the Xata API yet, this request might be slow");
1279
+ }
1280
+ const recordsWithId = await Promise.all(itemsWithIds.map((object) => this.create(object)));
1281
+ return order.map((condition) => {
1282
+ if (condition) {
1283
+ return recordsWithId.shift();
1284
+ } else {
1285
+ return recordsWithoutId.shift();
1286
+ }
1287
+ }).filter((record) => !!record);
1260
1288
  }
1261
1289
  if (isString(a) && isObject(b)) {
1262
1290
  if (a === "")
@@ -1283,16 +1311,18 @@ class RestRepository extends Query {
1283
1311
  if (Array.isArray(a)) {
1284
1312
  if (a.length === 0)
1285
1313
  return [];
1286
- return this.getAll({ filter: { id: { $any: a } } });
1314
+ const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
1315
+ return this.getAll({ filter: { id: { $any: ids } } });
1287
1316
  }
1288
- if (isString(a)) {
1289
- const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, a);
1317
+ const id = isString(a) ? a : a.id;
1318
+ if (isString(id)) {
1319
+ const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, id);
1290
1320
  if (cacheRecord)
1291
1321
  return cacheRecord;
1292
1322
  const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1293
1323
  try {
1294
1324
  const response = await getRecord({
1295
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: a },
1325
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
1296
1326
  ...fetchProps
1297
1327
  });
1298
1328
  const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
@@ -1464,7 +1494,7 @@ bulkInsertTableRecords_fn = async function(objects) {
1464
1494
  body: { records },
1465
1495
  ...fetchProps
1466
1496
  });
1467
- const finalObjects = await this.any(...response.recordIDs.map((id) => this.filter("id", id))).getAll();
1497
+ const finalObjects = await this.read(response.recordIDs);
1468
1498
  if (finalObjects.length !== objects.length) {
1469
1499
  throw new Error("The server failed to save some records");
1470
1500
  }
@@ -1868,10 +1898,7 @@ async function getDatabaseBranch(branch, options) {
1868
1898
  apiUrl: databaseURL,
1869
1899
  fetchImpl: getFetchImplementation(options?.fetchImpl),
1870
1900
  workspacesApiUrl: `${protocol}//${host}`,
1871
- pathParams: {
1872
- dbBranchName,
1873
- workspace
1874
- }
1901
+ pathParams: { dbBranchName, workspace }
1875
1902
  });
1876
1903
  } catch (err) {
1877
1904
  if (isObject(err) && err.status === 404)