@xata.io/client 0.13.2 → 0.14.0
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 +30 -0
- package/README.md +41 -35
- package/Usage.md +129 -114
- package/dist/index.cjs +185 -126
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +93 -23
- package/dist/index.mjs +167 -126
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -4
- package/LICENSE +0 -201
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,35 @@
|
|
1
1
|
# @xata.io/client
|
2
2
|
|
3
|
+
## 0.14.0
|
4
|
+
|
5
|
+
### Minor Changes
|
6
|
+
|
7
|
+
- [#409](https://github.com/xataio/client-ts/pull/409) [`8812380`](https://github.com/xataio/client-ts/commit/881238062b5eeac2dc8b9ba156720e0acc22c5c5) Thanks [@SferaDev](https://github.com/SferaDev)! - Infer types from schema in codegen
|
8
|
+
|
9
|
+
* [#457](https://github.com/xataio/client-ts/pull/457) [`0584a5b`](https://github.com/xataio/client-ts/commit/0584a5b207a21dbc36ddc1d44b276f1d5bb60dc5) Thanks [@SferaDev](https://github.com/SferaDev)! - Load env variables so that code analysis detects them
|
10
|
+
|
11
|
+
- [#469](https://github.com/xataio/client-ts/pull/469) [`8d8a912`](https://github.com/xataio/client-ts/commit/8d8a9129e36452266c4c12fe35b421f66e572498) Thanks [@gimenete](https://github.com/gimenete)! - Treat branch name specified with third party env variables as git branches in the resolution algorithm
|
12
|
+
|
13
|
+
### Patch Changes
|
14
|
+
|
15
|
+
- [#462](https://github.com/xataio/client-ts/pull/462) [`7547b7e`](https://github.com/xataio/client-ts/commit/7547b7edbc9a95c6620784cc5348316f27502c73) Thanks [@SferaDev](https://github.com/SferaDev)! - Fix bug with RecordArray.map
|
16
|
+
|
17
|
+
* [#472](https://github.com/xataio/client-ts/pull/472) [`e99010c`](https://github.com/xataio/client-ts/commit/e99010c9ab9d355abadcfbcf98b5a3fcc80c307a) Thanks [@SferaDev](https://github.com/SferaDev)! - Add id as entity property
|
18
|
+
|
19
|
+
- [#443](https://github.com/xataio/client-ts/pull/443) [`c4be404`](https://github.com/xataio/client-ts/commit/c4be404a3ecb34df9b1ef4501c92f5bdc221f19c) Thanks [@SferaDev](https://github.com/SferaDev)! - Improve performance with `create([])` operation
|
20
|
+
|
21
|
+
## 0.13.4
|
22
|
+
|
23
|
+
### Patch Changes
|
24
|
+
|
25
|
+
- [#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
|
26
|
+
|
27
|
+
## 0.13.3
|
28
|
+
|
29
|
+
### Patch Changes
|
30
|
+
|
31
|
+
- [#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
|
32
|
+
|
3
33
|
## 0.13.2
|
4
34
|
|
5
35
|
### Patch 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
|
-
|
5
|
+
## Table of Contents
|
6
6
|
|
7
|
-
|
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](
|
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
|
57
|
+
The import above will differ if you chose to generate the code in a different location.
|
42
58
|
|
43
|
-
The `fetch`
|
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,
|
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
|
-
|
105
|
+
#### Creating Objects
|
90
106
|
|
91
107
|
Invoke the `create()` method in the repository. Example:
|
92
108
|
|
@@ -112,14 +128,14 @@ const user = await client.db.users.updateOrInsert('user_admin', {
|
|
112
128
|
});
|
113
129
|
```
|
114
130
|
|
115
|
-
|
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
|
-
|
138
|
+
#### Querying Multiple Objects
|
123
139
|
|
124
140
|
```ts
|
125
141
|
// Query objects selecting all fields.
|
@@ -140,7 +156,7 @@ const page = await xata.db.users.filter('email', 'foo@example.com').getPaginated
|
|
140
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
|
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
|
@@ -173,7 +189,7 @@ const firstPage = await page.firstPage(); // Page object
|
|
173
189
|
const lastPage = await page.lastPage(); // Page object
|
174
190
|
```
|
175
191
|
|
176
|
-
If you want to use an iterator, both the Repository and the Query classes implement an AsyncIterable
|
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:
|
177
193
|
|
178
194
|
```ts
|
179
195
|
for await (const record of xata.db.users) {
|
@@ -189,7 +205,7 @@ for await (const records of xata.db.users.getIterator({ batchSize: 100 })) {
|
|
189
205
|
}
|
190
206
|
```
|
191
207
|
|
192
|
-
|
208
|
+
#### Updating Objects
|
193
209
|
|
194
210
|
Updating an object leaves the existing instance unchanged, but returns a new object with the updated values.
|
195
211
|
|
@@ -205,7 +221,7 @@ const updatedUser = await xata.db.users.update('rec_1234abcdef', {
|
|
205
221
|
});
|
206
222
|
```
|
207
223
|
|
208
|
-
|
224
|
+
#### Deleting Objects
|
209
225
|
|
210
226
|
```ts
|
211
227
|
// Using an existing object
|
@@ -217,7 +233,7 @@ await xata.db.users.delete('rec_1234abcdef');
|
|
217
233
|
|
218
234
|
### API Client
|
219
235
|
|
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...
|
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...
|
221
237
|
|
222
238
|
To communicate with the SDK we provide a constructor called `XataApiClient` that accepts an API token and an optional fetch implementation method.
|
223
239
|
|
@@ -229,7 +245,6 @@ Once you have initialized the API client, the operations are organized following
|
|
229
245
|
|
230
246
|
```ts
|
231
247
|
const { id: workspace } = await client.workspaces.createWorkspace({ name: 'example', slug: 'example' });
|
232
|
-
|
233
248
|
const { databaseName } = await client.databases.createDatabase(workspace, 'database');
|
234
249
|
|
235
250
|
await client.branches.createBranch(workspace, databaseName, 'branch');
|
@@ -249,17 +264,8 @@ await client.workspaces.deleteWorkspace(workspace);
|
|
249
264
|
|
250
265
|
## Deno support
|
251
266
|
|
252
|
-
|
253
|
-
|
254
|
-
However you can already use it with your preferred node CDN with the following import in the auto-generated `xata.ts` file:
|
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:
|
255
268
|
|
256
269
|
```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';
|
270
|
+
import { buildClient, BaseClientOptions, XataRecord } from 'https://deno.land/x/xata/mod.ts';
|
265
271
|
```
|
package/Usage.md
CHANGED
@@ -1,23 +1,134 @@
|
|
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.
|
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.
|
11
122
|
|
12
|
-
|
123
|
+
## Repository
|
13
124
|
|
14
|
-
A repository is an object that can be used to create, read, update, and delete records in the table it represents.
|
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.
|
15
126
|
|
16
|
-
|
127
|
+
We'll see how to use these objects in the next section.
|
17
128
|
|
18
129
|
### Reading records
|
19
130
|
|
20
|
-
The `read()` method can be used to read
|
131
|
+
The `read()` method can be used to read records by their ids:
|
21
132
|
|
22
133
|
- If the object cannot be found, the method will return `null`.
|
23
134
|
- If the object can be found, the method will return a `XataRecord` object.
|
@@ -44,7 +155,7 @@ const user = await xata.db.users.read(object1);
|
|
44
155
|
const users = await xata.db.users.read([object1, object2]);
|
45
156
|
```
|
46
157
|
|
47
|
-
### Creating
|
158
|
+
### Creating Records
|
48
159
|
|
49
160
|
Both the `create()` and `createOrUpdate()` methods can be used to create a new record.
|
50
161
|
|
@@ -93,7 +204,7 @@ const users = await xata.db.users.createOrUpdate([
|
|
93
204
|
|
94
205
|
### Updating records
|
95
206
|
|
96
|
-
The `update()` method can be used to update an existing record. It will throw an Error if the record cannot be found.
|
207
|
+
The `update()` method can be used to update an existing record. It will throw an `Error` if the record cannot be found.
|
97
208
|
|
98
209
|
```ts
|
99
210
|
const user = await xata.db.users.update('rec_1234abcdef', { fullName: 'John Smith' });
|
@@ -114,9 +225,9 @@ const users = await xata.db.users.update([
|
|
114
225
|
]);
|
115
226
|
```
|
116
227
|
|
117
|
-
### Deleting
|
228
|
+
### Deleting Records
|
118
229
|
|
119
|
-
The `delete()` method can be used to delete an existing record. It will throw an Error if the record cannot be found.
|
230
|
+
The `delete()` method can be used to delete an existing record. It will throw an `Error` if the record cannot be found.
|
120
231
|
|
121
232
|
```ts
|
122
233
|
const user = await xata.db.users.delete('rec_1234abcdef');
|
@@ -145,7 +256,7 @@ const object2 = { id: 'user_admin' };
|
|
145
256
|
const users = await xata.db.users.delete([object1, object2]);
|
146
257
|
```
|
147
258
|
|
148
|
-
### Searching
|
259
|
+
### Searching Records
|
149
260
|
|
150
261
|
The `search()` method can be used to search records. It returns an array of records.
|
151
262
|
|
@@ -159,105 +270,9 @@ Also you can customize the results with an `options` object that includes `fuzzi
|
|
159
270
|
const results = await xata.db.users.search('John', { fuzziness: 1, filter: { 'team.name': 'Marketing' } });
|
160
271
|
```
|
161
272
|
|
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
273
|
## Page
|
257
274
|
|
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.
|
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.
|
261
276
|
|
262
277
|
It contains:
|
263
278
|
|
@@ -299,7 +314,7 @@ records.hasNextPage();
|
|
299
314
|
const { records: page2Records } = await records.nextPage();
|
300
315
|
```
|
301
316
|
|
302
|
-
Optionally you can provide `offset` and `size` parameters to the pagination and override the default values.
|
317
|
+
Optionally, you can provide `offset` and `size` parameters to the pagination and override the default values.
|
303
318
|
|
304
319
|
```ts
|
305
320
|
const page = await xata.db.users.getPaginated();
|
@@ -312,7 +327,7 @@ const page2 = await page.nextPage(50);
|
|
312
327
|
const page3 = await page2.nextPage(10, 60);
|
313
328
|
```
|
314
329
|
|
315
|
-
### Iterators and
|
330
|
+
### Iterators and Generators
|
316
331
|
|
317
332
|
The `Query` object can be used to iterate over the results as a way to paginate the results.
|
318
333
|
|
@@ -330,7 +345,7 @@ for await (const users of xata.db.users.getIterator({ batchSize: 50 })) {
|
|
330
345
|
}
|
331
346
|
```
|
332
347
|
|
333
|
-
### Helper
|
348
|
+
### Helper Variables
|
334
349
|
|
335
350
|
We expose some helper variables of the API limits when paginating:
|
336
351
|
|
@@ -339,7 +354,7 @@ We expose some helper variables of the API limits when paginating:
|
|
339
354
|
- `PAGINATION_MAX_OFFSET`: Maximum offset.
|
340
355
|
- `PAGINATION_DEFAULT_OFFSET`: Default offset.
|
341
356
|
|
342
|
-
You can use these variables if you implement your own pagination mechanism, as they will be updated when
|
357
|
+
You can use these variables if you implement your own pagination mechanism, as they will be updated when our API limits are updated.
|
343
358
|
|
344
359
|
## XataRecord
|
345
360
|
|