@xata.io/client 0.11.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,43 @@
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
+
21
+ ## 0.13.0
22
+
23
+ ### Minor Changes
24
+
25
+ - [#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
26
+
27
+ * [#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
28
+
29
+ ## 0.12.0
30
+
31
+ ### Minor Changes
32
+
33
+ - [#376](https://github.com/xataio/client-ts/pull/376) [`db3c88e`](https://github.com/xataio/client-ts/commit/db3c88e1f2bee6d308afb8d6e95b7c090a87e7a7) Thanks [@SferaDev](https://github.com/SferaDev)! - Hide xata object and expose getMetadata method
34
+
35
+ ### Patch Changes
36
+
37
+ - [#364](https://github.com/xataio/client-ts/pull/364) [`1cde95f`](https://github.com/xataio/client-ts/commit/1cde95f05a6b9fbf0564ea05400140f0cef41a3a) Thanks [@SferaDev](https://github.com/SferaDev)! - Add peer dep of TS 4.5+
38
+
39
+ * [#362](https://github.com/xataio/client-ts/pull/362) [`57bf0e2`](https://github.com/xataio/client-ts/commit/57bf0e2e049ed0498683ff42d287983f295342b7) Thanks [@SferaDev](https://github.com/SferaDev)! - Do not show error if date is not defined
40
+
3
41
  ## 0.11.0
4
42
 
5
43
  ### Minor Changes
package/README.md CHANGED
@@ -1 +1,265 @@
1
- Visit https://github.com/xataio/client-ts for more information.
1
+ # Xata SDK for TypeScript and JavaScript
2
+
3
+ This SDK has zero dependencies, so it can be used in many JavaScript runtimes including Node.js, Cloudflare workers, Deno, Electron, etc.
4
+
5
+ It also works in browsers for the same reason. But this is strongly discouraged because the API token would be leaked.
6
+
7
+ ## Installing
8
+
9
+ ```bash
10
+ npm install @xata.io/client
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ There are three ways to use the SDK:
16
+
17
+ - **Schema-generated Client**: SDK to create/read/update/delete records in a given database following a schema file (with type-safety).
18
+ - **Schema-less Client**: SDK to create/read/update/delete records in any database without schema validation (with partial type-safety).
19
+ - **API Client**: SDK to interact with the whole Xata API and all its endpoints.
20
+
21
+ ### Schema-generated Client
22
+
23
+ To use the schema-generated client, you need to run the code generator utility that comes with [our CLI](../../cli/README.md).
24
+
25
+ To run it (and assuming you have configured the project with `xata init`):
26
+
27
+ ```bash
28
+ xata codegen
29
+ ```
30
+
31
+ In a TypeScript file start using the generated code:
32
+
33
+ ```ts
34
+ import { XataClient } from './xata';
35
+
36
+ const xata = new XataClient({
37
+ fetch: fetchImplementation // Required if your runtime doesn't provide a global `fetch` function.
38
+ });
39
+ ```
40
+
41
+ The import above will differ if you chose to genreate the code in a different location.
42
+
43
+ The `fetch` paramter is required only if your runtime doesn't provide a global `fetch` function. There's also a `databaseURL` argument that by default will contain a URL pointing to your database (e.g. `https://myworkspace-123abc.xata.sh/db/databasename`), it can be specified in the constructor to overwrite that value if for whatever reason you need to connect to a different workspace or database.
44
+
45
+ The code generator will create two TypeScript types for each schema entity. The base one will be an `Identifiable` entity with the internal properties your entity has and the `Record` one will extend it with a set of operations (update, delete, etc...) and some schema metadata (xata version).
46
+
47
+ ```ts
48
+ interface User extends Identifiable {
49
+ email?: string | null;
50
+ }
51
+
52
+ type UserRecord = User & XataRecord;
53
+
54
+ async function initializeDatabase(admin: User): Promise<UserRecord> {
55
+ return xata.db.users.create(admin);
56
+ }
57
+
58
+ const admin = await initializeDatabase({ email: 'admin@example.com' });
59
+ await admin.update({ email: 'admin@foo.bar' });
60
+ await admin.delete();
61
+ ```
62
+
63
+ You will learn more about the available operations below, under the `API Design` section.
64
+
65
+ ### Schema-less Client
66
+
67
+ If you don't have a schema file, or you are building a generic way to interact with Xata, you can use the `BaseClient` class without schema validation.
68
+
69
+ ```ts
70
+ import { BaseClient } from '@xata.io/client';
71
+
72
+ const xata = new BaseClient({
73
+ branch: 'branchname',
74
+ apiKey: 'xau_1234abcdef',
75
+ fetch: fetchImplementation // Required if your runtime doesn't provide a global `fetch` function.
76
+ });
77
+ ```
78
+
79
+ It works the same way as the code-generated `XataClient` but doesn't provide type-safety for your model.
80
+
81
+ You can read more on the methods available below, under the `API Design` section.
82
+
83
+ ### API Design
84
+
85
+ The Xata SDK to create/read/update/delete records follows the repository pattern. Each table will have a repository object available at `xata.db.[table-name]`.
86
+
87
+ For example if you have a `users` table there'll be a repository at `xata.db.users`. If you're using the schema-less client, you can also use the `xata.db.[table-name]` syntax to access the repository but without TypeScript auto-completion.
88
+
89
+ **Creating objects**
90
+
91
+ Invoke the `create()` method in the repository. Example:
92
+
93
+ ```ts
94
+ const user = await xata.db.users.create({
95
+ fullName: 'John Smith'
96
+ });
97
+ ```
98
+
99
+ If you want to create a record with a specific ID, you can invoke `insert()`.
100
+
101
+ ```ts
102
+ const user = await xata.db.users.insert('user_admin', {
103
+ fullName: 'John Smith'
104
+ });
105
+ ```
106
+
107
+ And if you want to create or insert a record with a specific ID, you can invoke `updateOrInsert()`.
108
+
109
+ ```ts
110
+ const user = await client.db.users.updateOrInsert('user_admin', {
111
+ fullName: 'John Smith'
112
+ });
113
+ ```
114
+
115
+ **Query a single object by its id**
116
+
117
+ ```ts
118
+ // `user` will be null if the object cannot be found
119
+ const user = await xata.db.users.read('rec_1234abcdef');
120
+ ```
121
+
122
+ **Querying multiple objects**
123
+
124
+ ```ts
125
+ // Query objects selecting all fields.
126
+ const page = await xata.db.users.select().getPaginated();
127
+ const user = await xata.db.users.select().getFirst();
128
+
129
+ // You can also use `xata.db.users` directly, since it's an immutable Query too!
130
+ const page = await xata.db.users.getPaginated();
131
+ const user = await xata.db.users.getFirst();
132
+
133
+ // Query objects selecting just one or more fields
134
+ const page = await xata.db.users.select('email', 'profile').getPaginated();
135
+
136
+ // Apply constraints
137
+ const page = await xata.db.users.filter('email', 'foo@example.com').getPaginated();
138
+
139
+ // Sorting
140
+ const page = await xata.db.users.sort('full_name', 'asc').getPaginated();
141
+ ```
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 `getPaginated()`, `getMany()`, `getAll()`, or `getFirst()`.
144
+
145
+ ```ts
146
+ // Operators that combine multiple conditions can be deconstructed
147
+ const { filter, any, all, not, sort } = xata.db.users;
148
+ const query = filter('email', 'foo@example.com');
149
+
150
+ // Single-column operators are imported directly from the package
151
+ import { gt, includes, startsWith } from '@xata.io/client';
152
+ filter('email', startsWith('username')).not(filter('created_at', gt(somePastDate)));
153
+
154
+ // Queries are immutable objects. This is useful to derive queries from other queries
155
+ const admins = filter('admin', true);
156
+ const spaniardsAdmins = admins.filter('country', 'Spain');
157
+ await admins.getAll(); // still returns all admins
158
+
159
+ // Finally fetch the results of the query
160
+ const users = await query.getAll();
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.
165
+
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
174
+ ```
175
+
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:
177
+
178
+ ```ts
179
+ for await (const record of xata.db.users) {
180
+ console.log(record);
181
+ }
182
+
183
+ for await (const record of xata.db.users.filter('team.id', teamId)) {
184
+ console.log(record);
185
+ }
186
+
187
+ for await (const records of xata.db.users.getIterator({ batchSize: 100 })) {
188
+ console.log(records);
189
+ }
190
+ ```
191
+
192
+ **Updating objects**
193
+
194
+ Updating an object leaves the existing instance unchanged, but returns a new object with the updated values.
195
+
196
+ ```ts
197
+ // Using an existing object
198
+ const updatedUser = await user.update({
199
+ fullName: 'John Smith Jr.'
200
+ });
201
+
202
+ // Using an object's id
203
+ const updatedUser = await xata.db.users.update('rec_1234abcdef', {
204
+ fullName: 'John Smith Jr.'
205
+ });
206
+ ```
207
+
208
+ **Deleting objects**
209
+
210
+ ```ts
211
+ // Using an existing object
212
+ await user.delete();
213
+
214
+ // Using an object's id
215
+ await xata.db.users.delete('rec_1234abcdef');
216
+ ```
217
+
218
+ ### API Client
219
+
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...
221
+
222
+ To communicate with the SDK we provide a constructor called `XataApiClient` that accepts an API token and an optional fetch implementation method.
223
+
224
+ ```ts
225
+ const api = new XataApiClient({ apiKey: process.env.XATA_API_KEY });
226
+ ```
227
+
228
+ Once you have initialized the API client, the operations are organized following the same hiearchy as in the [official documentation](https://docs.xata.io). You have different namespaces for each entity (ie. `workspaces`, `databases`, `tables`, `branches`, `users`, `records`...).
229
+
230
+ ```ts
231
+ const { id: workspace } = await client.workspaces.createWorkspace({ name: 'example', slug: 'example' });
232
+
233
+ const { databaseName } = await client.databases.createDatabase(workspace, 'database');
234
+
235
+ await client.branches.createBranch(workspace, databaseName, 'branch');
236
+ await client.tables.createTable(workspace, databaseName, 'branch', 'table');
237
+ await client.tables.setTableSchema(workspace, databaseName, 'branch', 'table', {
238
+ columns: [{ name: 'email', type: 'string' }]
239
+ });
240
+
241
+ const { id: recordId } = await client.records.insertRecord(workspace, databaseName, 'branch', 'table', {
242
+ email: 'example@foo.bar'
243
+ });
244
+
245
+ const record = await client.records.getRecord(workspace, databaseName, 'branch', 'table', recordId);
246
+
247
+ await client.workspaces.deleteWorkspace(workspace);
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
+ ```