@xata.io/client 0.0.0-alpha.vfaca77b → 0.0.0-alpha.vfb22b4f

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,53 @@
1
1
  # @xata.io/client
2
2
 
3
+ ## 0.13.4
4
+
5
+ ### Patch Changes
6
+
7
+ - [#444](https://github.com/xataio/client-ts/pull/444) [`3c3a5af`](https://github.com/xataio/client-ts/commit/3c3a5afb1d5fb3295fd8cf6c2b66709a5c047507) Thanks [@SferaDev](https://github.com/SferaDev)! - Publish xata client on deno.land
8
+
9
+ ## 0.13.3
10
+
11
+ ### Patch Changes
12
+
13
+ - [#434](https://github.com/xataio/client-ts/pull/434) [`b82383d`](https://github.com/xataio/client-ts/commit/b82383d7541d19ae71ad7e047fd100901981f28b) Thanks [@SferaDev](https://github.com/SferaDev)! - Fix problem with SSR `RecordArray` in Next.js
14
+
15
+ ## 0.13.2
16
+
17
+ ### Patch Changes
18
+
19
+ - [#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
20
+
21
+ * [#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
22
+
23
+ - [#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
24
+
25
+ ## 0.13.1
26
+
27
+ ### Patch Changes
28
+
29
+ - [#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
30
+
31
+ * [#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
32
+
33
+ - [#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
34
+
35
+ * [#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
36
+
37
+ - [#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
38
+
39
+ * [#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
40
+
41
+ - [#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`
42
+
43
+ ## 0.13.0
44
+
45
+ ### Minor Changes
46
+
47
+ - [#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
48
+
49
+ * [#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
50
+
3
51
  ## 0.12.0
4
52
 
5
53
  ### Minor Changes
package/README.md CHANGED
@@ -1,10 +1,28 @@
1
1
  # Xata SDK for TypeScript and JavaScript
2
2
 
3
- This SDK has zero dependencies, so it can be used in many JavaScript runtimes including Node.js, Cloudflare workers, Deno, Electron, etc.
3
+ This SDK has zero dependencies, so it can be used in many JavaScript runtimes including Node.js, Cloudflare workers, Deno, Electron, etc. It also works in browsers for the same reason. But this is strongly discouraged because you can easily leak your API keys from browsers.
4
4
 
5
- It also works in browsers for the same reason. But this is strongly discouraged because the API token would be leaked.
5
+ ## Table of Contents
6
6
 
7
- ## Installing
7
+ <!-- START doctoc generated TOC please keep comment here to allow auto update -->
8
+ <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
9
+
10
+ - [Installation](#installation)
11
+ - [Usage](#usage)
12
+ - [Schema-generated Client](#schema-generated-client)
13
+ - [Schema-less Client](#schema-less-client)
14
+ - [API Design](#api-design)
15
+ - [Creating Objects](#creating-objects)
16
+ - [Query a Single Object by its ID](#query-a-single-object-by-its-id)
17
+ - [Querying Multiple Objects](#querying-multiple-objects)
18
+ - [Updating Objects](#updating-objects)
19
+ - [Deleting Objects](#deleting-objects)
20
+ - [API Client](#api-client)
21
+ - [Deno support](#deno-support)
22
+
23
+ <!-- END doctoc generated TOC please keep comment here to allow auto update -->
24
+
25
+ ## Installation
8
26
 
9
27
  ```bash
10
28
  npm install @xata.io/client
@@ -20,7 +38,7 @@ There are three ways to use the SDK:
20
38
 
21
39
  ### Schema-generated Client
22
40
 
23
- To use the schema-generated client, you need to run the code generator utility that comes with [our CLI](../../cli/README.md).
41
+ To use the schema-generated client, you need to run the code generator utility that comes with [our CLI](https://docs.xata.io/cli/getting-started).
24
42
 
25
43
  To run it (and assuming you have configured the project with `xata init`):
26
44
 
@@ -28,19 +46,17 @@ To run it (and assuming you have configured the project with `xata init`):
28
46
  xata codegen
29
47
  ```
30
48
 
31
- In a TypeScript file start using the generated code:
49
+ In a TypeScript file, start using the generated code like this:
32
50
 
33
51
  ```ts
34
- import { XataClient } from './xata';
52
+ import { XataClient } from './xata'; // or wherever you chose to generate the client
35
53
 
36
- const xata = new XataClient({
37
- fetch: fetchImplementation // Required if your runtime doesn't provide a global `fetch` function.
38
- });
54
+ const xata = new XataClient();
39
55
  ```
40
56
 
41
- The import above will differ if you chose to genreate the code in a different location.
57
+ The import above will differ if you chose to generate the code in a different location.
42
58
 
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.
59
+ The `XataClient` constructor accepts an object with configuration options, like the `fetch` parameter, which 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
60
 
45
61
  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
62
 
@@ -60,7 +76,7 @@ await admin.update({ email: 'admin@foo.bar' });
60
76
  await admin.delete();
61
77
  ```
62
78
 
63
- You will learn more about the available operations below, under the `API Design` section.
79
+ You will learn more about the available operations below, under the [`API Design`](#api-design) section.
64
80
 
65
81
  ### Schema-less Client
66
82
 
@@ -78,15 +94,15 @@ const xata = new BaseClient({
78
94
 
79
95
  It works the same way as the code-generated `XataClient` but doesn't provide type-safety for your model.
80
96
 
81
- You can read more on the methods available below, under the `API Design` section.
97
+ You can read more on the methods available below, in the next section.
82
98
 
83
99
  ### API Design
84
100
 
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]`.
101
+ The Xata SDK to create/read/update/delete records follows the [repository pattern](https://lyz-code.github.io/blue-book/architecture/repository_pattern/). Each table will have a repository object available at `xata.db.[table-name]`.
86
102
 
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.
103
+ 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
104
 
89
- **Creating objects**
105
+ #### Creating Objects
90
106
 
91
107
  Invoke the `create()` method in the repository. Example:
92
108
 
@@ -112,35 +128,35 @@ const user = await client.db.users.updateOrInsert('user_admin', {
112
128
  });
113
129
  ```
114
130
 
115
- **Query a single object by its id**
131
+ #### Query a Single Object by its ID
116
132
 
117
133
  ```ts
118
134
  // `user` will be null if the object cannot be found
119
135
  const user = await xata.db.users.read('rec_1234abcdef');
120
136
  ```
121
137
 
122
- **Querying multiple objects**
138
+ #### Querying Multiple Objects
123
139
 
124
140
  ```ts
125
141
  // Query objects selecting all fields.
126
- const users = await xata.db.users.select().getMany();
142
+ const page = await xata.db.users.select().getPaginated();
127
143
  const user = await xata.db.users.select().getFirst();
128
144
 
129
145
  // You can also use `xata.db.users` directly, since it's an immutable Query too!
130
- const users = await xata.db.users.getMany();
146
+ const page = await xata.db.users.getPaginated();
131
147
  const user = await xata.db.users.getFirst();
132
148
 
133
149
  // Query objects selecting just one or more fields
134
- const users = await xata.db.users.select('email', 'profile').getMany();
150
+ const page = await xata.db.users.select('email', 'profile').getPaginated();
135
151
 
136
152
  // Apply constraints
137
- const users = await xata.db.users.filter('email', 'foo@example.com').getMany();
153
+ const page = await xata.db.users.filter('email', 'foo@example.com').getPaginated();
138
154
 
139
155
  // Sorting
140
- const users = await xata.db.users.sort('fullName', 'asc').getMany();
156
+ const page = await xata.db.users.sort('full_name', 'asc').getPaginated();
141
157
  ```
142
158
 
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 `getAll()`, `getFirst()`, `getMany()` or `getPaginated()`.
159
+ 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
160
 
145
161
  ```ts
146
162
  // Operators that combine multiple conditions can be deconstructed
@@ -154,19 +170,26 @@ filter('email', startsWith('username')).not(filter('created_at', gt(somePastDate
154
170
  // Queries are immutable objects. This is useful to derive queries from other queries
155
171
  const admins = filter('admin', true);
156
172
  const spaniardsAdmins = admins.filter('country', 'Spain');
157
- await admins.getMany(); // still returns all admins
173
+ await admins.getAll(); // still returns all admins
158
174
 
159
175
  // Finally fetch the results of the query
160
- const users = await query.getMany();
176
+ const users = await query.getAll();
161
177
  const firstUser = await query.getFirst();
178
+ ```
162
179
 
163
- // Also you can paginate the results
164
- const page = await query.getPaginated();
165
- const hasPage2 = page.hasNextPage();
166
- const page2 = await page.nextPage();
180
+ The `getPaginated()` method will return a `Page` object. It's a wrapper that internally uses cursor based pagination.
181
+
182
+ ```ts
183
+ page.records; // Array of records
184
+ page.hasNextPage(); // Boolean
185
+
186
+ const nextPage = await page.nextPage(); // Page object
187
+ const previousPage = await page.previousPage(); // Page object
188
+ const firstPage = await page.firstPage(); // Page object
189
+ const lastPage = await page.lastPage(); // Page object
167
190
  ```
168
191
 
169
- 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:
192
+ 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:
170
193
 
171
194
  ```ts
172
195
  for await (const record of xata.db.users) {
@@ -182,7 +205,7 @@ for await (const records of xata.db.users.getIterator({ batchSize: 100 })) {
182
205
  }
183
206
  ```
184
207
 
185
- **Updating objects**
208
+ #### Updating Objects
186
209
 
187
210
  Updating an object leaves the existing instance unchanged, but returns a new object with the updated values.
188
211
 
@@ -198,7 +221,7 @@ const updatedUser = await xata.db.users.update('rec_1234abcdef', {
198
221
  });
199
222
  ```
200
223
 
201
- **Deleting objects**
224
+ #### Deleting Objects
202
225
 
203
226
  ```ts
204
227
  // Using an existing object
@@ -208,26 +231,9 @@ await user.delete();
208
231
  await xata.db.users.delete('rec_1234abcdef');
209
232
  ```
210
233
 
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
234
  ### API Client
229
235
 
230
- 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...
236
+ 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](https://docs.xata.io/concepts/workspaces), databases, tables, branches...
231
237
 
232
238
  To communicate with the SDK we provide a constructor called `XataApiClient` that accepts an API token and an optional fetch implementation method.
233
239
 
@@ -239,7 +245,6 @@ Once you have initialized the API client, the operations are organized following
239
245
 
240
246
  ```ts
241
247
  const { id: workspace } = await client.workspaces.createWorkspace({ name: 'example', slug: 'example' });
242
-
243
248
  const { databaseName } = await client.databases.createDatabase(workspace, 'database');
244
249
 
245
250
  await client.branches.createBranch(workspace, databaseName, 'branch');
@@ -256,3 +261,11 @@ const record = await client.records.getRecord(workspace, databaseName, 'branch',
256
261
 
257
262
  await client.workspaces.deleteWorkspace(workspace);
258
263
  ```
264
+
265
+ ## Deno support
266
+
267
+ We publish the client on [deno.land](https://deno.land/x/xata). You can use it by changing the import in the auto-generated `xata.ts` file:
268
+
269
+ ```ts
270
+ import { buildClient, BaseClientOptions, XataRecord } from 'https://deno.land/x/xata/mod.ts';
271
+ ```
package/Usage.md ADDED
@@ -0,0 +1,395 @@
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.
42
+ - `getMany()`: returns an array of some records in the query results.
43
+
44
+ Since the [`Repository`](#repository) class implements the `Query` interface, you can use it to query and paginate the records in the table too.
45
+
46
+ ```ts
47
+ const user = xata.db.users.getFirst();
48
+ ```
49
+
50
+ ### Column selection
51
+
52
+ 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 `*`.
53
+
54
+ The dot notation is supported to select columns from nested objects.
55
+
56
+ ```ts
57
+ const user = xata.db.users.select(['*', 'team.*']).getFirst();
58
+ ```
59
+
60
+ ### Sorting
61
+
62
+ 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.
63
+
64
+ ```ts
65
+ const user = xata.db.users.orderBy('fullName', 'asc').getFirst();
66
+ ```
67
+
68
+ ### Filtering
69
+
70
+ You can filter the results by providing the column and the value to filter.
71
+
72
+ ```ts
73
+ const user = xata.db.users.filter('fullName', 'John').getFirst();
74
+ ```
75
+
76
+ To combine multiple filters in an 'AND' clause, you can pipe the filters together.
77
+
78
+ ```ts
79
+ const user = xata.db.users.filter('fullName', 'John').filter('team.name', 'Marketing').getFirst();
80
+ ```
81
+
82
+ Also you can filter the results by providing a `filter` object.
83
+
84
+ ```ts
85
+ const user = xata.db.users.filter({ fullName: 'John', 'team.name': 'Marketing' }).getFirst();
86
+ ```
87
+
88
+ 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.
89
+
90
+ ```ts
91
+ const user = xata.db.users.filter('name', startsWith('Bar')).getFirst();
92
+ ```
93
+
94
+ If you prefer to directly use the filter operators as in the API, you can add them in the `filter` object.
95
+
96
+ ```ts
97
+ xata.db.users.filter({ full_name: { $startsWith: 'foo' } }).getFirst();
98
+ ```
99
+
100
+ ### Combining Queries
101
+
102
+ Queries can be stored in variables and can be combined with other queries.
103
+
104
+ ```ts
105
+ const johnQuery = xata.db.users.filter('fullName', 'John');
106
+ const janeQuery = xata.db.users.filter('fullName', 'Jane');
107
+
108
+ const johns = await johnQuery.getAll();
109
+ const janes = await janeQuery.getAll();
110
+
111
+ const users = await xata.db.users.any(johnQuery, janeQuery).getAll();
112
+ ```
113
+
114
+ We offer the following helper methods to combine queries:
115
+
116
+ - `any()`: returns the records that match any of the queries.
117
+ - `all()`: returns the records that match all of the queries.
118
+ - `none()`: returns the records that match none of the queries.
119
+ - `not()`: returns the records that don't match the given query.
120
+
121
+ You can read more about the query operators in the API section for the query table endpoint.
122
+
123
+ ## Repository
124
+
125
+ 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.
126
+
127
+ We'll see how to use these objects in the next section.
128
+
129
+ ### Reading records
130
+
131
+ The `read()` method can be used to read records by their ids:
132
+
133
+ - If the object cannot be found, the method will return `null`.
134
+ - If the object can be found, the method will return a `XataRecord` object.
135
+
136
+ You can read a single record by its id.
137
+
138
+ ```ts
139
+ const user = await xata.db.users.read('rec_1234abcdef');
140
+ ```
141
+
142
+ You can also read multiple records by their ids.
143
+
144
+ ```ts
145
+ const users = await xata.db.users.read(['rec_1234abcdef', 'rec_5678defgh']);
146
+ ```
147
+
148
+ And you can read records coming from an object that contains an `id` property.
149
+
150
+ ```ts
151
+ const object1 = { id: 'rec_1234abcdef' };
152
+ const object2 = { id: 'rec_5678defgh' };
153
+
154
+ const user = await xata.db.users.read(object1);
155
+ const users = await xata.db.users.read([object1, object2]);
156
+ ```
157
+
158
+ ### Creating Records
159
+
160
+ Both the `create()` and `createOrUpdate()` methods can be used to create a new record.
161
+
162
+ - The `create()` method will create a new record and fail if the provided id already exists.
163
+ - The `createOrUpdate()` method will create a new record if the provided id doesn't exist, or update an existing record if it does.
164
+
165
+ You can create a record without providing an id, and the id will be generated automatically.
166
+
167
+ ```ts
168
+ const user = await xata.db.users.create({ fullName: 'John Smith' });
169
+ user.id; // 'rec_1234abcdef'
170
+ ```
171
+
172
+ You can create a record with an id as parameter, and the id will be used.
173
+
174
+ ```ts
175
+ const user = await xata.db.users.create('user_admin', { fullName: 'John Smith' });
176
+ user.id; // 'user_admin'
177
+ ```
178
+
179
+ You can create a record with the id provided in the object, and the id will be used.
180
+
181
+ ```ts
182
+ const user = await xata.db.users.create({ id: 'user_admin', fullName: 'John Smith' });
183
+ user.id; // 'user_admin'
184
+ ```
185
+
186
+ You can create multiple records at once by providing an array of objects.
187
+
188
+ ```ts
189
+ const users = await xata.db.users.create([{ fullName: 'John Smith' }, { id: 'user_admin', fullName: 'Jane Doe' }]);
190
+ users[0].id; // 'rec_1234abcdef'
191
+ users[1].id; // 'user_admin'
192
+ ```
193
+
194
+ The `createOrUpdate()` method beaves the same way as `create()` but you will always need to provide an id.
195
+
196
+ ```ts
197
+ const user1 = await xata.db.users.createOrUpdate('user_admin', { fullName: 'John Smith' });
198
+ const user2 = await xata.db.users.createOrUpdate({ id: 'user_manager', fullName: 'Jane Doe' });
199
+ const users = await xata.db.users.createOrUpdate([
200
+ { id: 'user_admin', fullName: 'John Smith' },
201
+ { id: 'user_manager', fullName: 'Jane Doe' }
202
+ ]);
203
+ ```
204
+
205
+ ### Updating records
206
+
207
+ The `update()` method can be used to update an existing record. It will throw an `Error` if the record cannot be found.
208
+
209
+ ```ts
210
+ const user = await xata.db.users.update('rec_1234abcdef', { fullName: 'John Smith' });
211
+ ```
212
+
213
+ The `id` property can also be sent in the object update.
214
+
215
+ ```ts
216
+ const user = await xata.db.users.update({ id: 'user_admin', fullName: 'John Smith' });
217
+ ```
218
+
219
+ You can update multiple records at once by providing an array of objects.
220
+
221
+ ```ts
222
+ const users = await xata.db.users.update([
223
+ { id: 'rec_1234abcdef', fullName: 'John Smith' },
224
+ { id: 'user_admin', fullName: 'Jane Doe' }
225
+ ]);
226
+ ```
227
+
228
+ ### Deleting Records
229
+
230
+ The `delete()` method can be used to delete an existing record. It will throw an `Error` if the record cannot be found.
231
+
232
+ ```ts
233
+ const user = await xata.db.users.delete('rec_1234abcdef');
234
+ ```
235
+
236
+ You can delete multiple records at once by providing an array of ids.
237
+
238
+ ```ts
239
+ const users = await xata.db.users.delete(['rec_1234abcdef', 'user_admin']);
240
+ ```
241
+
242
+ You can delete records coming from an object that contains an `id` property.
243
+
244
+ ```ts
245
+ const object1 = { id: 'rec_1234abcdef' };
246
+
247
+ const user = await xata.db.users.delete(object1);
248
+ ```
249
+
250
+ You can delete records coming from an array of objects that contain an `id` property.
251
+
252
+ ```ts
253
+ const object1 = { id: 'rec_1234abcdef' };
254
+ const object2 = { id: 'user_admin' };
255
+
256
+ const users = await xata.db.users.delete([object1, object2]);
257
+ ```
258
+
259
+ ### Searching Records
260
+
261
+ The `search()` method can be used to search records. It returns an array of records.
262
+
263
+ ```ts
264
+ const results = await xata.db.users.search('John');
265
+ ```
266
+
267
+ Also you can customize the results with an `options` object that includes `fuzziness`, `filter` and all the other options the API supports.
268
+
269
+ ```ts
270
+ const results = await xata.db.users.search('John', { fuzziness: 1, filter: { 'team.name': 'Marketing' } });
271
+ ```
272
+
273
+ ## Page
274
+
275
+ 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.
276
+
277
+ It contains:
278
+
279
+ - `records`: Array of `XataRecord` objects.
280
+ - `hasNextPage`: Function that returns a boolean indicating if there is a next page.
281
+ - `nextPage`: Async function that can be used to get the next page.
282
+ - `previousPage`: Async function that can be used to get the previous page.
283
+ - `firstPage`: Async function that can be used to get the first page.
284
+ - `lastPage`: Async function that can be used to get the last page.
285
+ - `meta`: Information about the current page and its cursor.
286
+
287
+ ```ts
288
+ const page = await xata.db.users.getPaginated();
289
+ page.records; // Array of `XataRecord` objects.
290
+ page.hasNextPage();
291
+
292
+ const page2 = await page.nextPage();
293
+ page2.records; // Array of `XataRecord` objects.
294
+
295
+ const page1 = await page2.previousPage();
296
+ page1.records; // Array of `XataRecord` objects.
297
+
298
+ const firstPage = await page1.firstPage();
299
+ firstPage.records; // Array of `XataRecord` objects.
300
+ ```
301
+
302
+ The `Repository` class implements the `Query` interface, so you can use it to paginate the records in the table too.
303
+
304
+ ```ts
305
+ const page = await xata.db.users.firstPage();
306
+ page.records; // Array of `XataRecord` objects.
307
+ ```
308
+
309
+ The array returned in `records` also implements the `Page` interface.
310
+
311
+ ```ts
312
+ const { records } = await xata.db.users.getPaginated();
313
+ records.hasNextPage();
314
+ const { records: page2Records } = await records.nextPage();
315
+ ```
316
+
317
+ Optionally, you can provide `offset` and `size` parameters to the pagination and override the default values.
318
+
319
+ ```ts
320
+ const page = await xata.db.users.getPaginated();
321
+ page.records; // Array of `XataRecord` objects.
322
+
323
+ // A second page with size 50
324
+ const page2 = await page.nextPage(50);
325
+
326
+ // A third page with size 10 but an offset of 60
327
+ const page3 = await page2.nextPage(10, 60);
328
+ ```
329
+
330
+ ### Iterators and Generators
331
+
332
+ The `Query` object can be used to iterate over the results as a way to paginate the results.
333
+
334
+ ```ts
335
+ for await (const user of xata.db.users) {
336
+ await user.update({ full_name: 'John Doe' });
337
+ }
338
+ ```
339
+
340
+ Also if you want to retrieve more than one record at a time in the iterator, you can use the `getIterator()` method.
341
+
342
+ ```ts
343
+ for await (const users of xata.db.users.getIterator({ batchSize: 50 })) {
344
+ console.log(users);
345
+ }
346
+ ```
347
+
348
+ ### Helper Variables
349
+
350
+ We expose some helper variables of the API limits when paginating:
351
+
352
+ - `PAGINATION_MAX_SIZE`: Maximum page size.
353
+ - `PAGINATION_DEFAULT_SIZE`: Default page size.
354
+ - `PAGINATION_MAX_OFFSET`: Maximum offset.
355
+ - `PAGINATION_DEFAULT_OFFSET`: Default offset.
356
+
357
+ You can use these variables if you implement your own pagination mechanism, as they will be updated when our API limits are updated.
358
+
359
+ ## XataRecord
360
+
361
+ Every row in a table is represented by an `XataRecord` object.
362
+
363
+ It contains an `id` property that represents the unique identifier of the record and all the fields of the table.
364
+
365
+ Also it provides some methods to read again, update or delete the record.
366
+
367
+ ```ts
368
+ const user = await xata.db.users.read('rec_1234abcdef');
369
+ user?.id; // 'rec_1234abcdef'
370
+ user?.fullName; // 'John Smith'
371
+ user?.read(); // Reads the record again
372
+ user?.update({ fullName: 'John Doe' }); // Partially updates the record
373
+ user?.delete(); // Deletes the record
374
+ ```
375
+
376
+ 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.
377
+
378
+ ```ts
379
+ const user = await xata.db.users.read('rec_1234abcdef');
380
+ user?.team?.id; // 'rec_5678defgh'
381
+ user?.team?.read(); // Reads the linked record properties
382
+ user?.team?.update({ name: 'A team' }); // Partially updates the linked record
383
+ ```
384
+
385
+ When working with `queries` you can expand the properties returned in a `XataRecord` object.
386
+
387
+ By default only the current table fields are returned, and the id of any linked record.
388
+
389
+ ```ts
390
+ const user = await xata.db.users.filter('id', 'rec_1234abcdef').select(['*', 'team.*']).getFirst();
391
+ user?.id; // 'rec_1234abcdef'
392
+ user?.fullName; // 'John Smith'
393
+ user?.team?.id; // 'rec_5678defgh'
394
+ user?.team?.name; // 'A team'
395
+ ```