@xata.io/client 0.12.0 → 0.13.2
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 +36 -0
- package/README.md +36 -29
- package/Usage.md +380 -0
- package/dist/index.cjs +91 -25
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +70 -22
- package/dist/index.mjs +91 -26
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -3
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,41 @@
|
|
1
1
|
# @xata.io/client
|
2
2
|
|
3
|
+
## 0.13.2
|
4
|
+
|
5
|
+
### Patch Changes
|
6
|
+
|
7
|
+
- [#431](https://github.com/xataio/client-ts/pull/431) [`8f62024`](https://github.com/xataio/client-ts/commit/8f62024101028b981dc31a68fb258e89110d45dc) Thanks [@SferaDev](https://github.com/SferaDev)! - Include request ids in the error response
|
8
|
+
|
9
|
+
* [#429](https://github.com/xataio/client-ts/pull/429) [`bb102b4`](https://github.com/xataio/client-ts/commit/bb102b46b722d0a61996c42cda991c9f0080e464) Thanks [@SferaDev](https://github.com/SferaDev)! - Avoid detection of `Buffer` in edge runtime middleware
|
10
|
+
|
11
|
+
- [#428](https://github.com/xataio/client-ts/pull/428) [`06740ca`](https://github.com/xataio/client-ts/commit/06740cad216831216f0be8cf9de7e354c0ef9191) Thanks [@SferaDev](https://github.com/SferaDev)! - Improve selection types to make them more readable
|
12
|
+
|
13
|
+
## 0.13.1
|
14
|
+
|
15
|
+
### Patch Changes
|
16
|
+
|
17
|
+
- [#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
|
18
|
+
|
19
|
+
* [#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
|
20
|
+
|
21
|
+
- [#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
|
22
|
+
|
23
|
+
* [#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
|
24
|
+
|
25
|
+
- [#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
|
26
|
+
|
27
|
+
* [#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
|
28
|
+
|
29
|
+
- [#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`
|
30
|
+
|
31
|
+
## 0.13.0
|
32
|
+
|
33
|
+
### Minor Changes
|
34
|
+
|
35
|
+
- [#375](https://github.com/xataio/client-ts/pull/375) [`c9f34ad`](https://github.com/xataio/client-ts/commit/c9f34ad37d75203083a1dec2fac2b03e096521af) Thanks [@SferaDev](https://github.com/SferaDev)! - Change default pagination size to 20
|
36
|
+
|
37
|
+
* [#375](https://github.com/xataio/client-ts/pull/375) [`5f82e43`](https://github.com/xataio/client-ts/commit/5f82e4394010f40dcbf3faf2d0bdb58a6fc1c37a) Thanks [@SferaDev](https://github.com/SferaDev)! - Return a paginable object in getPaginated
|
38
|
+
|
3
39
|
## 0.12.0
|
4
40
|
|
5
41
|
### Minor Changes
|
package/README.md
CHANGED
@@ -123,24 +123,24 @@ const user = await xata.db.users.read('rec_1234abcdef');
|
|
123
123
|
|
124
124
|
```ts
|
125
125
|
// Query objects selecting all fields.
|
126
|
-
const
|
126
|
+
const page = await xata.db.users.select().getPaginated();
|
127
127
|
const user = await xata.db.users.select().getFirst();
|
128
128
|
|
129
129
|
// You can also use `xata.db.users` directly, since it's an immutable Query too!
|
130
|
-
const
|
130
|
+
const page = await xata.db.users.getPaginated();
|
131
131
|
const user = await xata.db.users.getFirst();
|
132
132
|
|
133
133
|
// Query objects selecting just one or more fields
|
134
|
-
const
|
134
|
+
const page = await xata.db.users.select('email', 'profile').getPaginated();
|
135
135
|
|
136
136
|
// Apply constraints
|
137
|
-
const
|
137
|
+
const page = await xata.db.users.filter('email', 'foo@example.com').getPaginated();
|
138
138
|
|
139
139
|
// Sorting
|
140
|
-
const
|
140
|
+
const page = await xata.db.users.sort('full_name', 'asc').getPaginated();
|
141
141
|
```
|
142
142
|
|
143
|
-
Query operations (`select()`, `filter()`, `sort()`) return a `Query` object. These objects are immutable. You can add additional constraints, sort, etc. by calling their methods, and a new query will be returned. In order to finally make a query to the database you'll invoke `
|
143
|
+
Query operations (`select()`, `filter()`, `sort()`) return a `Query` object. These objects are immutable. You can add additional constraints, sort, etc. by calling their methods, and a new query will be returned. In order to finally make a query to the database you'll invoke `getPaginated()`, `getMany()`, `getAll()`, or `getFirst()`.
|
144
144
|
|
145
145
|
```ts
|
146
146
|
// Operators that combine multiple conditions can be deconstructed
|
@@ -154,16 +154,23 @@ filter('email', startsWith('username')).not(filter('created_at', gt(somePastDate
|
|
154
154
|
// Queries are immutable objects. This is useful to derive queries from other queries
|
155
155
|
const admins = filter('admin', true);
|
156
156
|
const spaniardsAdmins = admins.filter('country', 'Spain');
|
157
|
-
await admins.
|
157
|
+
await admins.getAll(); // still returns all admins
|
158
158
|
|
159
159
|
// Finally fetch the results of the query
|
160
|
-
const users = await query.
|
160
|
+
const users = await query.getAll();
|
161
161
|
const firstUser = await query.getFirst();
|
162
|
+
```
|
163
|
+
|
164
|
+
The `getPaginated()` method will return a `Page` object. It's a wrapper that internally uses cursor based pagination.
|
162
165
|
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
166
|
+
```ts
|
167
|
+
page.records; // Array of records
|
168
|
+
page.hasNextPage(); // Boolean
|
169
|
+
|
170
|
+
const nextPage = await page.nextPage(); // Page object
|
171
|
+
const previousPage = await page.previousPage(); // Page object
|
172
|
+
const firstPage = await page.firstPage(); // Page object
|
173
|
+
const lastPage = await page.lastPage(); // Page object
|
167
174
|
```
|
168
175
|
|
169
176
|
If you want to use an iterator, both the Repository and the Query classes implement an AsyncIterable. Alternatively you can use `getIterator()` and customize the batch size of the iterator:
|
@@ -208,23 +215,6 @@ await user.delete();
|
|
208
215
|
await xata.db.users.delete('rec_1234abcdef');
|
209
216
|
```
|
210
217
|
|
211
|
-
#### Deno support
|
212
|
-
|
213
|
-
Right now we are still not publishing the client on deno.land or have support for deno in the codegen.
|
214
|
-
|
215
|
-
However you can already use it with your preferred node CDN with the following import in the auto-generated `xata.ts` file:
|
216
|
-
|
217
|
-
```ts
|
218
|
-
import {
|
219
|
-
BaseClient,
|
220
|
-
Query,
|
221
|
-
Repository,
|
222
|
-
RestRespositoryFactory,
|
223
|
-
XataClientOptions,
|
224
|
-
XataRecord
|
225
|
-
} from 'https://esm.sh/@xata.io/client@<version>/dist/schema?target=deno';
|
226
|
-
```
|
227
|
-
|
228
218
|
### API Client
|
229
219
|
|
230
220
|
One of the main features of the SDK is the ability to interact with the whole Xata API and perform administrative operations such as creating/reading/updating/deleting workspaces, databases, tables, branches...
|
@@ -256,3 +246,20 @@ const record = await client.records.getRecord(workspace, databaseName, 'branch',
|
|
256
246
|
|
257
247
|
await client.workspaces.deleteWorkspace(workspace);
|
258
248
|
```
|
249
|
+
|
250
|
+
## Deno support
|
251
|
+
|
252
|
+
Right now we are still not publishing the client on deno.land or have support for deno in the codegen.
|
253
|
+
|
254
|
+
However you can already use it with your preferred node CDN with the following import in the auto-generated `xata.ts` file:
|
255
|
+
|
256
|
+
```ts
|
257
|
+
import {
|
258
|
+
BaseClient,
|
259
|
+
Query,
|
260
|
+
Repository,
|
261
|
+
RestRespositoryFactory,
|
262
|
+
XataClientOptions,
|
263
|
+
XataRecord
|
264
|
+
} from 'https://esm.sh/@xata.io/client@<version>/dist/schema?target=deno';
|
265
|
+
```
|
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
|
+
```
|