@xata.io/client 0.0.0-alpha.vfaf51aa → 0.0.0-alpha.vfafe7e2
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 +278 -0
- package/README.md +63 -55
- package/Usage.md +194 -123
- package/dist/index.cjs +2357 -859
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +5346 -1437
- package/dist/index.mjs +2321 -859
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
- /package/{rollup.config.js → rollup.config.mjs} +0 -0
package/Usage.md
CHANGED
@@ -1,23 +1,157 @@
|
|
1
|
-
#
|
1
|
+
# Xata SDK Reference
|
2
2
|
|
3
|
-
There
|
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
4
|
|
5
|
-
- `Repository`: a table representation that can be used to create, read, update, and delete records.
|
6
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
7
|
- `XataRecord`: a row in a table.
|
8
8
|
- `Page`: a collection of records that can be paginated.
|
9
9
|
|
10
|
-
|
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
|
+
```
|
11
122
|
|
12
|
-
|
123
|
+
### Combining Queries
|
13
124
|
|
14
|
-
|
125
|
+
Queries can be stored in variables and can be combined with other queries.
|
15
126
|
|
16
|
-
|
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.
|
17
151
|
|
18
152
|
### Reading records
|
19
153
|
|
20
|
-
The `read()` method can be used to read
|
154
|
+
The `read()` method can be used to read records by their ids:
|
21
155
|
|
22
156
|
- If the object cannot be found, the method will return `null`.
|
23
157
|
- If the object can be found, the method will return a `XataRecord` object.
|
@@ -44,7 +178,14 @@ const user = await xata.db.users.read(object1);
|
|
44
178
|
const users = await xata.db.users.read([object1, object2]);
|
45
179
|
```
|
46
180
|
|
47
|
-
|
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
|
48
189
|
|
49
190
|
Both the `create()` and `createOrUpdate()` methods can be used to create a new record.
|
50
191
|
|
@@ -91,9 +232,22 @@ const users = await xata.db.users.createOrUpdate([
|
|
91
232
|
]);
|
92
233
|
```
|
93
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
|
+
|
94
248
|
### Updating records
|
95
249
|
|
96
|
-
The `update()` method can be used to update an existing record. It will throw an Error if the record cannot be found.
|
250
|
+
The `update()` method can be used to update an existing record. It will throw an `Error` if the record cannot be found.
|
97
251
|
|
98
252
|
```ts
|
99
253
|
const user = await xata.db.users.update('rec_1234abcdef', { fullName: 'John Smith' });
|
@@ -114,9 +268,15 @@ const users = await xata.db.users.update([
|
|
114
268
|
]);
|
115
269
|
```
|
116
270
|
|
117
|
-
|
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.
|
118
272
|
|
119
|
-
|
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.
|
120
280
|
|
121
281
|
```ts
|
122
282
|
const user = await xata.db.users.delete('rec_1234abcdef');
|
@@ -145,7 +305,7 @@ const object2 = { id: 'user_admin' };
|
|
145
305
|
const users = await xata.db.users.delete([object1, object2]);
|
146
306
|
```
|
147
307
|
|
148
|
-
### Searching
|
308
|
+
### Searching Records
|
149
309
|
|
150
310
|
The `search()` method can be used to search records. It returns an array of records.
|
151
311
|
|
@@ -159,105 +319,9 @@ Also you can customize the results with an `options` object that includes `fuzzi
|
|
159
319
|
const results = await xata.db.users.search('John', { fuzziness: 1, filter: { 'team.name': 'Marketing' } });
|
160
320
|
```
|
161
321
|
|
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
322
|
## Page
|
257
323
|
|
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.
|
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.
|
261
325
|
|
262
326
|
It contains:
|
263
327
|
|
@@ -265,8 +329,8 @@ It contains:
|
|
265
329
|
- `hasNextPage`: Function that returns a boolean indicating if there is a next page.
|
266
330
|
- `nextPage`: Async function that can be used to get the next page.
|
267
331
|
- `previousPage`: Async function that can be used to get the previous page.
|
268
|
-
- `
|
269
|
-
- `
|
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.
|
270
334
|
- `meta`: Information about the current page and its cursor.
|
271
335
|
|
272
336
|
```ts
|
@@ -280,14 +344,14 @@ page2.records; // Array of `XataRecord` objects.
|
|
280
344
|
const page1 = await page2.previousPage();
|
281
345
|
page1.records; // Array of `XataRecord` objects.
|
282
346
|
|
283
|
-
const
|
284
|
-
|
347
|
+
const startPage = await page1.startPage();
|
348
|
+
startPage.records; // Array of `XataRecord` objects.
|
285
349
|
```
|
286
350
|
|
287
351
|
The `Repository` class implements the `Query` interface, so you can use it to paginate the records in the table too.
|
288
352
|
|
289
353
|
```ts
|
290
|
-
const page = await xata.db.users.
|
354
|
+
const page = await xata.db.users.startPage();
|
291
355
|
page.records; // Array of `XataRecord` objects.
|
292
356
|
```
|
293
357
|
|
@@ -299,7 +363,7 @@ records.hasNextPage();
|
|
299
363
|
const { records: page2Records } = await records.nextPage();
|
300
364
|
```
|
301
365
|
|
302
|
-
Optionally you can provide `offset` and `size` parameters to the pagination and override the default values.
|
366
|
+
Optionally, you can provide `offset` and `size` parameters to the pagination and override the default values.
|
303
367
|
|
304
368
|
```ts
|
305
369
|
const page = await xata.db.users.getPaginated();
|
@@ -312,7 +376,7 @@ const page2 = await page.nextPage(50);
|
|
312
376
|
const page3 = await page2.nextPage(10, 60);
|
313
377
|
```
|
314
378
|
|
315
|
-
### Iterators and
|
379
|
+
### Iterators and Generators
|
316
380
|
|
317
381
|
The `Query` object can be used to iterate over the results as a way to paginate the results.
|
318
382
|
|
@@ -330,16 +394,16 @@ for await (const users of xata.db.users.getIterator({ batchSize: 50 })) {
|
|
330
394
|
}
|
331
395
|
```
|
332
396
|
|
333
|
-
### Helper
|
397
|
+
### Helper Variables
|
334
398
|
|
335
399
|
We expose some helper variables of the API limits when paginating:
|
336
400
|
|
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.
|
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).
|
341
405
|
|
342
|
-
You can use these variables if you implement your own pagination mechanism, as they will be updated when
|
406
|
+
You can use these variables if you implement your own pagination mechanism, as they will be updated when our API limits are updated.
|
343
407
|
|
344
408
|
## XataRecord
|
345
409
|
|
@@ -358,6 +422,13 @@ user?.update({ fullName: 'John Doe' }); // Partially updates the record
|
|
358
422
|
user?.delete(); // Deletes the record
|
359
423
|
```
|
360
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
|
+
|
361
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.
|
362
433
|
|
363
434
|
```ts
|