@xata.io/client 0.22.0 → 0.22.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xata.io/client",
3
- "version": "0.22.0",
3
+ "version": "0.22.1",
4
4
  "description": "Xata.io SDK for TypeScript and JavaScript",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",
package/Usage.md DELETED
@@ -1,451 +0,0 @@
1
- # Xata SDK Reference
2
-
3
- There are four types of objects in the Xata TypeScript SDK. In this document, we will understand them deeper to better work with the Xata SDK.
4
-
5
- - `Query`: a combination of filters and other parameters to retrieve a collection of records.
6
- - `Repository`: a table representation that can be used to create, read, update, and delete records.
7
- - `XataRecord`: a row in a table.
8
- - `Page`: a collection of records that can be paginated.
9
-
10
- Let's explore them below.
11
-
12
- ## Table of Contents
13
-
14
- <!-- START doctoc generated TOC please keep comment here to allow auto update -->
15
- <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
16
-
17
- - [Query](#query)
18
- - [Column selection](#column-selection)
19
- - [Sorting](#sorting)
20
- - [Filtering](#filtering)
21
- - [Combining queries](#combining-queries)
22
- - [Repository](#repository)
23
- - [Reading records](#reading-records)
24
- - [Creating records](#creating-records)
25
- - [Updating records](#updating-records)
26
- - [Deleting records](#deleting-records)
27
- - [Searching records](#searching-records)
28
- - [Page](#page)
29
- - [Iterators and generators](#iterators-and-generators)
30
- - [Helper variables](#helper-variables)
31
- - [XataRecord](#xatarecord)
32
-
33
- <!-- END doctoc generated TOC please keep comment here to allow auto update -->
34
-
35
- ## Query
36
-
37
- To get a collection of records, you can use the `Query` object. It provides the following methods:
38
-
39
- - `getFirst()`: returns the first record in the query results.
40
- - `getPaginated()`: returns a page of records in the query results.
41
- - `getAll()`: returns all the records in the query results by making multiple requests to iterate over all the pages which exist. If the query is not filtered and the table is a large dataset, this operation can affect the performance.
42
- - `getMany()`: returns an array with a subset of the first results in the query. The default [pagination](#page) size (20) is used and can be customised by passing a different `{ pagination: { size: number } }` in its options. To learn more about default values, see [helper variables](#helper-variables).
43
-
44
- Both the `getAll()` and `getMany()` will produce multiple requests to the server if the query should return more than the maximum page size. We perform the minimum number of requests to get the desired number of records.
45
-
46
- All these methods allow customising its filters, column selection, column ordering, pagination or cache TTL. For example:
47
-
48
- ```ts
49
- // First item sorting by name
50
- const user = await xata.db.users.getFirst({ sort: 'name' });
51
-
52
- // Get first 50 items but ignore the first one
53
- const users = await xata.db.users.getMany({ pagination: { size: 50, offset: 1 } });
54
-
55
- // Get page of 100 items where name contains "foo"
56
- const page = await xata.db.users.getPaginated({ filter: { name: { $contains: 'foo' } }, pagination: { size: 100 } });
57
-
58
- // Get all admin users and cache the result for 5 minutes
59
- const user = await xata.db.users.filter('role', 'admin').getAll({ cache: 5 * 60 * 1000 });
60
-
61
- // Overwrite values set in a query
62
- const query = xata.db.users.filter('role', 'admin').select(['name']);
63
- const adminUsers = await query.getAll();
64
- const firstAdminUserWithEmail = await query.getFirst({ columns: ['name', 'email'] });
65
- ```
66
-
67
- Since the [`Repository`](#repository) class implements the `Query` interface, you can use it to query and paginate the records in the table too.
68
-
69
- ```ts
70
- const user = xata.db.users.getFirst();
71
- ```
72
-
73
- ### Column selection
74
-
75
- The `Query` object can be used to select the columns that will be returned in the results. You can pick multiple columns by providing an array of column names, or you can pick all the columns by providing `*`.
76
-
77
- The dot notation is supported to select columns from nested objects.
78
-
79
- ```ts
80
- const user = xata.db.users.select(['*', 'team.*']).getFirst();
81
- ```
82
-
83
- ### Sorting
84
-
85
- The `Query` object can be used to sort the order of the results. You can sort the results by providing a column name and an `asc` or `desc` string.
86
-
87
- ```ts
88
- const user = xata.db.users.orderBy('fullName', 'asc').getFirst();
89
- ```
90
-
91
- ### Filtering
92
-
93
- You can filter the results by providing the column and the value to filter.
94
-
95
- ```ts
96
- const user = xata.db.users.filter('fullName', 'John').getFirst();
97
- ```
98
-
99
- To combine multiple filters in an 'AND' clause, you can pipe the filters together.
100
-
101
- ```ts
102
- const user = xata.db.users.filter('fullName', 'John').filter('team.name', 'Marketing').getFirst();
103
- ```
104
-
105
- Also you can filter the results by providing a `filter` object.
106
-
107
- ```ts
108
- const user = xata.db.users.filter({ fullName: 'John', 'team.name': 'Marketing' }).getFirst();
109
- ```
110
-
111
- 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.
112
-
113
- ```ts
114
- const user = xata.db.users.filter('name', startsWith('Bar')).getFirst();
115
- ```
116
-
117
- If you prefer to directly use the filter operators as in the API, you can add them in the `filter` object.
118
-
119
- ```ts
120
- xata.db.users.filter({ full_name: { $startsWith: 'foo' } }).getFirst();
121
- ```
122
-
123
- ### Combining Queries
124
-
125
- Queries can be stored in variables and can be combined with other queries.
126
-
127
- ```ts
128
- const johnQuery = xata.db.users.filter('fullName', 'John');
129
- const janeQuery = xata.db.users.filter('fullName', 'Jane');
130
-
131
- const johns = await johnQuery.getAll();
132
- const janes = await janeQuery.getAll();
133
-
134
- const users = await xata.db.users.any(johnQuery, janeQuery).getAll();
135
- ```
136
-
137
- We offer the following helper methods to combine queries:
138
-
139
- - `any()`: returns the records that match any of the queries.
140
- - `all()`: returns the records that match all of the queries.
141
- - `none()`: returns the records that match none of the queries.
142
- - `not()`: returns the records that don't match the given query.
143
-
144
- You can read more about the query operators in the API section for the query table endpoint.
145
-
146
- ## Repository
147
-
148
- Any table in the database can be represented by a `Repository` object. A repository is an object that can be used to create, read, update, and delete records in the table it represents. It also implements the `Query` and `Page` interfaces, so you can use it to query and paginate the records in the table too.
149
-
150
- We'll see how to use these objects in the next section.
151
-
152
- ### Reading records
153
-
154
- The `read()` method can be used to read records by their ids:
155
-
156
- - If the object cannot be found, the method will return `null`.
157
- - If the object can be found, the method will return a `XataRecord` object.
158
-
159
- You can read a single record by its id.
160
-
161
- ```ts
162
- const user = await xata.db.users.read('rec_1234abcdef');
163
- ```
164
-
165
- You can also read multiple records by their ids.
166
-
167
- ```ts
168
- const users = await xata.db.users.read(['rec_1234abcdef', 'rec_5678defgh']);
169
- ```
170
-
171
- And you can read records coming from an object that contains an `id` property.
172
-
173
- ```ts
174
- const object1 = { id: 'rec_1234abcdef' };
175
- const object2 = { id: 'rec_5678defgh' };
176
-
177
- const user = await xata.db.users.read(object1);
178
- const users = await xata.db.users.read([object1, object2]);
179
- ```
180
-
181
- By default an object with the first level properties is returned. If you want to reduce or expand its columns, you can pass an array of columns to the `read()` method.
182
-
183
- ```ts
184
- const user = await xata.db.users.read('rec_1234abcdef', ['fullName', 'team.name']);
185
- const users = await xata.db.users.read(['rec_1234abcdef', 'rec_5678defgh'], ['fullName', 'team.name']);
186
- ```
187
-
188
- ### Creating Records
189
-
190
- Both the `create()` and `createOrUpdate()` methods can be used to create a new record.
191
-
192
- - The `create()` method will create a new record and fail if the provided id already exists.
193
- - The `createOrUpdate()` method will create a new record if the provided id doesn't exist, or update an existing record if it does.
194
-
195
- You can create a record without providing an id, and the id will be generated automatically.
196
-
197
- ```ts
198
- const user = await xata.db.users.create({ fullName: 'John Smith' });
199
- user.id; // 'rec_1234abcdef'
200
- ```
201
-
202
- You can create a record with an id as parameter, and the id will be used.
203
-
204
- ```ts
205
- const user = await xata.db.users.create('user_admin', { fullName: 'John Smith' });
206
- user.id; // 'user_admin'
207
- ```
208
-
209
- You can create a record with the id provided in the object, and the id will be used.
210
-
211
- ```ts
212
- const user = await xata.db.users.create({ id: 'user_admin', fullName: 'John Smith' });
213
- user.id; // 'user_admin'
214
- ```
215
-
216
- You can create multiple records at once by providing an array of objects.
217
-
218
- ```ts
219
- const users = await xata.db.users.create([{ fullName: 'John Smith' }, { id: 'user_admin', fullName: 'Jane Doe' }]);
220
- users[0].id; // 'rec_1234abcdef'
221
- users[1].id; // 'user_admin'
222
- ```
223
-
224
- The `createOrUpdate()` method beaves the same way as `create()` but you will always need to provide an id.
225
-
226
- ```ts
227
- const user1 = await xata.db.users.createOrUpdate('user_admin', { fullName: 'John Smith' });
228
- const user2 = await xata.db.users.createOrUpdate({ id: 'user_manager', fullName: 'Jane Doe' });
229
- const users = await xata.db.users.createOrUpdate([
230
- { id: 'user_admin', fullName: 'John Smith' },
231
- { id: 'user_manager', fullName: 'Jane Doe' }
232
- ]);
233
- ```
234
-
235
- By default, the `create` and `createOrUpdate` methods will return the created record with the first level properties. If you want to reduce or expand its columns, you can pass an array of columns to the `create` or `createOrUpdate` method.
236
-
237
- ```ts
238
- const user = await xata.db.users.create('user_admin', { fullName: 'John Smith' }, ['fullName', 'team.name']);
239
- const users = await xata.db.users.createOrUpdate(
240
- [
241
- { id: 'user_admin', fullName: 'John Smith' },
242
- { id: 'user_manager', fullName: 'Jane Doe' }
243
- ],
244
- ['fullName', 'team.name']
245
- );
246
- ```
247
-
248
- ### Updating records
249
-
250
- The `update()` method can be used to update an existing record. It will throw an `Error` if the record cannot be found.
251
-
252
- ```ts
253
- const user = await xata.db.users.update('rec_1234abcdef', { fullName: 'John Smith' });
254
- ```
255
-
256
- The `id` property can also be sent in the object update.
257
-
258
- ```ts
259
- const user = await xata.db.users.update({ id: 'user_admin', fullName: 'John Smith' });
260
- ```
261
-
262
- You can update multiple records at once by providing an array of objects.
263
-
264
- ```ts
265
- const users = await xata.db.users.update([
266
- { id: 'rec_1234abcdef', fullName: 'John Smith' },
267
- { id: 'user_admin', fullName: 'Jane Doe' }
268
- ]);
269
- ```
270
-
271
- By default, the `update` method will return the updated record with the first level properties. If you want to reduce or expand its columns, you can pass an array of columns to the `update` method.
272
-
273
- ```ts
274
- const user = await xata.db.users.update('rec_1234abcdef', { fullName: 'John Smith' }, ['fullName', 'team.name']);
275
- ```
276
-
277
- ### Deleting Records
278
-
279
- The `delete()` method can be used to delete an existing record. It will throw an `Error` if the record cannot be found.
280
-
281
- ```ts
282
- const user = await xata.db.users.delete('rec_1234abcdef');
283
- ```
284
-
285
- You can delete multiple records at once by providing an array of ids.
286
-
287
- ```ts
288
- const users = await xata.db.users.delete(['rec_1234abcdef', 'user_admin']);
289
- ```
290
-
291
- You can delete records coming from an object that contains an `id` property.
292
-
293
- ```ts
294
- const object1 = { id: 'rec_1234abcdef' };
295
-
296
- const user = await xata.db.users.delete(object1);
297
- ```
298
-
299
- You can delete records coming from an array of objects that contain an `id` property.
300
-
301
- ```ts
302
- const object1 = { id: 'rec_1234abcdef' };
303
- const object2 = { id: 'user_admin' };
304
-
305
- const users = await xata.db.users.delete([object1, object2]);
306
- ```
307
-
308
- ### Searching Records
309
-
310
- The `search()` method can be used to search records. It returns an array of records.
311
-
312
- ```ts
313
- const results = await xata.db.users.search('John');
314
- ```
315
-
316
- Also you can customize the results with an `options` object that includes `fuzziness`, `filter` and all the other options the API supports.
317
-
318
- ```ts
319
- const results = await xata.db.users.search('John', { fuzziness: 1, filter: { 'team.name': 'Marketing' } });
320
- ```
321
-
322
- ## Page
323
-
324
- Some methods of the `Query` interface provide a `Page` object as a return value that can be used to paginate the results. The `Page` object can be used to get the queried records of a table in pages. It is an abstraction of cursor-based pagination.
325
-
326
- It contains:
327
-
328
- - `records`: Array of `XataRecord` objects.
329
- - `hasNextPage`: Function that returns a boolean indicating if there is a next page.
330
- - `nextPage`: Async function that can be used to get the next page.
331
- - `previousPage`: Async function that can be used to get the previous page.
332
- - `startPage`: Async function that can be used to get the start page.
333
- - `endPage`: Async function that can be used to get the end page.
334
- - `meta`: Information about the current page and its cursor.
335
-
336
- ```ts
337
- const page = await xata.db.users.getPaginated();
338
- page.records; // Array of `XataRecord` objects.
339
- page.hasNextPage();
340
-
341
- const page2 = await page.nextPage();
342
- page2.records; // Array of `XataRecord` objects.
343
-
344
- const page1 = await page2.previousPage();
345
- page1.records; // Array of `XataRecord` objects.
346
-
347
- const startPage = await page1.startPage();
348
- startPage.records; // Array of `XataRecord` objects.
349
- ```
350
-
351
- The `Repository` class implements the `Query` interface, so you can use it to paginate the records in the table too.
352
-
353
- ```ts
354
- const page = await xata.db.users.startPage();
355
- page.records; // Array of `XataRecord` objects.
356
- ```
357
-
358
- The array returned in `records` also implements the `Page` interface.
359
-
360
- ```ts
361
- const { records } = await xata.db.users.getPaginated();
362
- records.hasNextPage();
363
- const { records: page2Records } = await records.nextPage();
364
- ```
365
-
366
- Optionally, you can provide `offset` and `size` parameters to the pagination and override the default values.
367
-
368
- ```ts
369
- const page = await xata.db.users.getPaginated();
370
- page.records; // Array of `XataRecord` objects.
371
-
372
- // A second page with size 50
373
- const page2 = await page.nextPage(50);
374
-
375
- // A third page with size 10 but an offset of 60
376
- const page3 = await page2.nextPage(10, 60);
377
- ```
378
-
379
- ### Iterators and Generators
380
-
381
- The `Query` object can be used to iterate over the results as a way to paginate the results.
382
-
383
- ```ts
384
- for await (const user of xata.db.users) {
385
- await user.update({ full_name: 'John Doe' });
386
- }
387
- ```
388
-
389
- Also if you want to retrieve more than one record at a time in the iterator, you can use the `getIterator()` method.
390
-
391
- ```ts
392
- for await (const users of xata.db.users.getIterator({ batchSize: 50 })) {
393
- console.log(users);
394
- }
395
- ```
396
-
397
- ### Helper Variables
398
-
399
- We expose some helper variables of the API limits when paginating:
400
-
401
- - `PAGINATION_MAX_SIZE`: Maximum page size (200).
402
- - `PAGINATION_DEFAULT_SIZE`: Default page size (20).
403
- - `PAGINATION_MAX_OFFSET`: Maximum offset (800).
404
- - `PAGINATION_DEFAULT_OFFSET`: Default offset (0).
405
-
406
- You can use these variables if you implement your own pagination mechanism, as they will be updated when our API limits are updated.
407
-
408
- ## XataRecord
409
-
410
- Every row in a table is represented by an `XataRecord` object.
411
-
412
- It contains an `id` property that represents the unique identifier of the record and all the fields of the table.
413
-
414
- Also it provides some methods to read again, update or delete the record.
415
-
416
- ```ts
417
- const user = await xata.db.users.read('rec_1234abcdef');
418
- user?.id; // 'rec_1234abcdef'
419
- user?.fullName; // 'John Smith'
420
- user?.read(); // Reads the record again
421
- user?.update({ fullName: 'John Doe' }); // Partially updates the record
422
- user?.delete(); // Deletes the record
423
- ```
424
-
425
- The `read` and `update` methods return the updated record with the first level properties. If you want to reduce or expand its columns, you can pass an array of columns to the `read` and `update` methods.
426
-
427
- ```ts
428
- const user = await xata.db.users.read('rec_1234abcdef');
429
- user?.read(['fullName', 'team.name']); // Reads the record again with the `fullName` and `team.name` columns
430
- ```
431
-
432
- 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.
433
-
434
- ```ts
435
- const user = await xata.db.users.read('rec_1234abcdef');
436
- user?.team?.id; // 'rec_5678defgh'
437
- user?.team?.read(); // Reads the linked record properties
438
- user?.team?.update({ name: 'A team' }); // Partially updates the linked record
439
- ```
440
-
441
- When working with `queries` you can expand the properties returned in a `XataRecord` object.
442
-
443
- By default only the current table fields are returned, and the id of any linked record.
444
-
445
- ```ts
446
- const user = await xata.db.users.filter('id', 'rec_1234abcdef').select(['*', 'team.*']).getFirst();
447
- user?.id; // 'rec_1234abcdef'
448
- user?.fullName; // 'John Smith'
449
- user?.team?.id; // 'rec_5678defgh'
450
- user?.team?.name; // 'A team'
451
- ```