@xata.io/client 0.0.0-alpha.vfee45b2 → 0.0.0-alpha.vfef462e

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,125 @@
1
1
  # @xata.io/client
2
2
 
3
+ ## 0.16.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#485](https://github.com/xataio/client-ts/pull/485) [`a9cbb26`](https://github.com/xataio/client-ts/commit/a9cbb263fbca47cb91a827db252d95a5bb4079a6) Thanks [@SferaDev](https://github.com/SferaDev)! - Allow selecting columns with record operations
8
+
9
+ * [#485](https://github.com/xataio/client-ts/pull/485) [`7e04a3d`](https://github.com/xataio/client-ts/commit/7e04a3d1c51958a44f687a0036ead8bb3f5a2dfb) Thanks [@SferaDev](https://github.com/SferaDev)! - Remove record cache
10
+
11
+ ### Patch Changes
12
+
13
+ - [#503](https://github.com/xataio/client-ts/pull/503) [`6a96ea5`](https://github.com/xataio/client-ts/commit/6a96ea5da4c5b7ca9a99b57ebbce8d6766b5d4d8) Thanks [@xata-bot](https://github.com/xata-bot)! - Update API response types for create of tables and branches
14
+
15
+ * [#421](https://github.com/xataio/client-ts/pull/421) [`43f2560`](https://github.com/xataio/client-ts/commit/43f25605ddd0d2fd514a1542a14389d28955c500) Thanks [@SferaDev](https://github.com/SferaDev)! - Add search boosters and allow prefix search
16
+
17
+ ## 0.15.0
18
+
19
+ ### Patch Changes
20
+
21
+ - [#496](https://github.com/xataio/client-ts/pull/496) [`e923d11`](https://github.com/xataio/client-ts/commit/e923d11fe357519dc4ca3ae722670e6e70ccd1c6) Thanks [@gimenete](https://github.com/gimenete)! - Ignore git output in Xata client
22
+
23
+ * [#481](https://github.com/xataio/client-ts/pull/481) [`599b52c`](https://github.com/xataio/client-ts/commit/599b52c3090222eedef85d1ad1e907874cd3e801) Thanks [@xata-bot](https://github.com/xata-bot)! - Add updateWorkspaceMemberInvite API method
24
+
25
+ ## 0.14.0
26
+
27
+ ### Minor Changes
28
+
29
+ - [#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
30
+
31
+ * [#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
32
+
33
+ - [#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
34
+
35
+ ### Patch Changes
36
+
37
+ - [#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
38
+
39
+ * [#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
40
+
41
+ - [#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
42
+
43
+ ## 0.13.4
44
+
45
+ ### Patch Changes
46
+
47
+ - [#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
48
+
49
+ ## 0.13.3
50
+
51
+ ### Patch Changes
52
+
53
+ - [#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
54
+
55
+ ## 0.13.2
56
+
57
+ ### Patch Changes
58
+
59
+ - [#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
60
+
61
+ * [#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
62
+
63
+ - [#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
64
+
65
+ ## 0.13.1
66
+
67
+ ### Patch Changes
68
+
69
+ - [#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
70
+
71
+ * [#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
72
+
73
+ - [#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
74
+
75
+ * [#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
76
+
77
+ - [#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
78
+
79
+ * [#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
80
+
81
+ - [#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`
82
+
83
+ ## 0.13.0
84
+
85
+ ### Minor Changes
86
+
87
+ - [#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
88
+
89
+ * [#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
90
+
91
+ ## 0.12.0
92
+
93
+ ### Minor Changes
94
+
95
+ - [#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
96
+
97
+ ### Patch Changes
98
+
99
+ - [#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+
100
+
101
+ * [#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
102
+
103
+ ## 0.11.0
104
+
105
+ ### Minor Changes
106
+
107
+ - [#322](https://github.com/xataio/client-ts/pull/322) [`bc64c28`](https://github.com/xataio/client-ts/commit/bc64c28fbfbb000c7190ac8092e2ef6a261df86f) Thanks [@SferaDev](https://github.com/SferaDev)! - Add filter support for cross-table search operations
108
+
109
+ ### Patch Changes
110
+
111
+ - [#327](https://github.com/xataio/client-ts/pull/327) [`505257c`](https://github.com/xataio/client-ts/commit/505257c0c42ca0c8beaf5c0f638037c576dcc43c) Thanks [@SferaDev](https://github.com/SferaDev)! - Allow reading multiple uids at the same time
112
+
113
+ * [#346](https://github.com/xataio/client-ts/pull/346) [`ff7e5c6`](https://github.com/xataio/client-ts/commit/ff7e5c6f211913196d8c28600d7a7675ed261688) Thanks [@SferaDev](https://github.com/SferaDev)! - Fix compat with TS 4.8
114
+
115
+ - [#345](https://github.com/xataio/client-ts/pull/345) [`bf64cb8`](https://github.com/xataio/client-ts/commit/bf64cb885d55a0271e966314384324f02ded084e) Thanks [@SferaDev](https://github.com/SferaDev)! - Fix bug with nullable record filters inferred as never
116
+
117
+ * [#334](https://github.com/xataio/client-ts/pull/334) [`ce07601`](https://github.com/xataio/client-ts/commit/ce07601e4ddf9f75e20249d479dc04a63795ca96) Thanks [@SferaDev](https://github.com/SferaDev)! - Add support for TS 4.5
118
+
119
+ - [#325](https://github.com/xataio/client-ts/pull/325) [`12f1ce3`](https://github.com/xataio/client-ts/commit/12f1ce362f6cda27dfdb3afab0800282bddc8b5e) Thanks [@SferaDev](https://github.com/SferaDev)! - Fix offset errors with operations that affect many rows
120
+
121
+ * [#345](https://github.com/xataio/client-ts/pull/345) [`a73a2a2`](https://github.com/xataio/client-ts/commit/a73a2a2014c44cf88eaef42196ba1dba9d516b4a) Thanks [@SferaDev](https://github.com/SferaDev)! - Fix issue with Filter<T> not narrowing down type on object properties
122
+
3
123
  ## 0.10.2
4
124
 
5
125
  ### Patch Changes
package/README.md CHANGED
@@ -1 +1,271 @@
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. 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
+
5
+ ## Table of Contents
6
+
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
26
+
27
+ ```bash
28
+ npm install @xata.io/client
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ There are three ways to use the SDK:
34
+
35
+ - **Schema-generated Client**: SDK to create/read/update/delete records in a given database following a schema file (with type-safety).
36
+ - **Schema-less Client**: SDK to create/read/update/delete records in any database without schema validation (with partial type-safety).
37
+ - **API Client**: SDK to interact with the whole Xata API and all its endpoints.
38
+
39
+ ### Schema-generated Client
40
+
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).
42
+
43
+ To run it (and assuming you have configured the project with `xata init`):
44
+
45
+ ```bash
46
+ xata codegen
47
+ ```
48
+
49
+ In a TypeScript file, start using the generated code like this:
50
+
51
+ ```ts
52
+ import { XataClient } from './xata'; // or wherever you chose to generate the client
53
+
54
+ const xata = new XataClient();
55
+ ```
56
+
57
+ The import above will differ if you chose to generate the code in a different location.
58
+
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.
60
+
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).
62
+
63
+ ```ts
64
+ interface User extends Identifiable {
65
+ email?: string | null;
66
+ }
67
+
68
+ type UserRecord = User & XataRecord;
69
+
70
+ async function initializeDatabase(admin: User): Promise<UserRecord> {
71
+ return xata.db.users.create(admin);
72
+ }
73
+
74
+ const admin = await initializeDatabase({ email: 'admin@example.com' });
75
+ await admin.update({ email: 'admin@foo.bar' });
76
+ await admin.delete();
77
+ ```
78
+
79
+ You will learn more about the available operations below, under the [`API Design`](#api-design) section.
80
+
81
+ ### Schema-less Client
82
+
83
+ 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.
84
+
85
+ ```ts
86
+ import { BaseClient } from '@xata.io/client';
87
+
88
+ const xata = new BaseClient({
89
+ branch: 'branchname',
90
+ apiKey: 'xau_1234abcdef',
91
+ fetch: fetchImplementation // Required if your runtime doesn't provide a global `fetch` function.
92
+ });
93
+ ```
94
+
95
+ It works the same way as the code-generated `XataClient` but doesn't provide type-safety for your model.
96
+
97
+ You can read more on the methods available below, in the next section.
98
+
99
+ ### API Design
100
+
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]`.
102
+
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.
104
+
105
+ #### Creating Objects
106
+
107
+ Invoke the `create()` method in the repository. Example:
108
+
109
+ ```ts
110
+ const user = await xata.db.users.create({
111
+ fullName: 'John Smith'
112
+ });
113
+ ```
114
+
115
+ If you want to create a record with a specific ID, you can invoke `insert()`.
116
+
117
+ ```ts
118
+ const user = await xata.db.users.insert('user_admin', {
119
+ fullName: 'John Smith'
120
+ });
121
+ ```
122
+
123
+ And if you want to create or insert a record with a specific ID, you can invoke `updateOrInsert()`.
124
+
125
+ ```ts
126
+ const user = await client.db.users.updateOrInsert('user_admin', {
127
+ fullName: 'John Smith'
128
+ });
129
+ ```
130
+
131
+ #### Query a Single Object by its ID
132
+
133
+ ```ts
134
+ // `user` will be null if the object cannot be found
135
+ const user = await xata.db.users.read('rec_1234abcdef');
136
+ ```
137
+
138
+ #### Querying Multiple Objects
139
+
140
+ ```ts
141
+ // Query objects selecting all fields.
142
+ const page = await xata.db.users.select().getPaginated();
143
+ const user = await xata.db.users.select().getFirst();
144
+
145
+ // You can also use `xata.db.users` directly, since it's an immutable Query too!
146
+ const page = await xata.db.users.getPaginated();
147
+ const user = await xata.db.users.getFirst();
148
+
149
+ // Query objects selecting just one or more fields
150
+ const page = await xata.db.users.select('email', 'profile').getPaginated();
151
+
152
+ // Apply constraints
153
+ const page = await xata.db.users.filter('email', 'foo@example.com').getPaginated();
154
+
155
+ // Sorting
156
+ const page = await xata.db.users.sort('full_name', 'asc').getPaginated();
157
+ ```
158
+
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()`.
160
+
161
+ ```ts
162
+ // Operators that combine multiple conditions can be deconstructed
163
+ const { filter, any, all, not, sort } = xata.db.users;
164
+ const query = filter('email', 'foo@example.com');
165
+
166
+ // Single-column operators are imported directly from the package
167
+ import { gt, includes, startsWith } from '@xata.io/client';
168
+ filter('email', startsWith('username')).not(filter('created_at', gt(somePastDate)));
169
+
170
+ // Queries are immutable objects. This is useful to derive queries from other queries
171
+ const admins = filter('admin', true);
172
+ const spaniardsAdmins = admins.filter('country', 'Spain');
173
+ await admins.getAll(); // still returns all admins
174
+
175
+ // Finally fetch the results of the query
176
+ const users = await query.getAll();
177
+ const firstUser = await query.getFirst();
178
+ ```
179
+
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
190
+ ```
191
+
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:
193
+
194
+ ```ts
195
+ for await (const record of xata.db.users) {
196
+ console.log(record);
197
+ }
198
+
199
+ for await (const record of xata.db.users.filter('team.id', teamId)) {
200
+ console.log(record);
201
+ }
202
+
203
+ for await (const records of xata.db.users.getIterator({ batchSize: 100 })) {
204
+ console.log(records);
205
+ }
206
+ ```
207
+
208
+ #### Updating Objects
209
+
210
+ Updating an object leaves the existing instance unchanged, but returns a new object with the updated values.
211
+
212
+ ```ts
213
+ // Using an existing object
214
+ const updatedUser = await user.update({
215
+ fullName: 'John Smith Jr.'
216
+ });
217
+
218
+ // Using an object's id
219
+ const updatedUser = await xata.db.users.update('rec_1234abcdef', {
220
+ fullName: 'John Smith Jr.'
221
+ });
222
+ ```
223
+
224
+ #### Deleting Objects
225
+
226
+ ```ts
227
+ // Using an existing object
228
+ await user.delete();
229
+
230
+ // Using an object's id
231
+ await xata.db.users.delete('rec_1234abcdef');
232
+ ```
233
+
234
+ ### API Client
235
+
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...
237
+
238
+ To communicate with the SDK we provide a constructor called `XataApiClient` that accepts an API token and an optional fetch implementation method.
239
+
240
+ ```ts
241
+ const api = new XataApiClient({ apiKey: process.env.XATA_API_KEY });
242
+ ```
243
+
244
+ 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`...).
245
+
246
+ ```ts
247
+ const { id: workspace } = await client.workspaces.createWorkspace({ name: 'example', slug: 'example' });
248
+ const { databaseName } = await client.databases.createDatabase(workspace, 'database');
249
+
250
+ await client.branches.createBranch(workspace, databaseName, 'branch');
251
+ await client.tables.createTable(workspace, databaseName, 'branch', 'table');
252
+ await client.tables.setTableSchema(workspace, databaseName, 'branch', 'table', {
253
+ columns: [{ name: 'email', type: 'string' }]
254
+ });
255
+
256
+ const { id: recordId } = await client.records.insertRecord(workspace, databaseName, 'branch', 'table', {
257
+ email: 'example@foo.bar'
258
+ });
259
+
260
+ const record = await client.records.getRecord(workspace, databaseName, 'branch', 'table', recordId);
261
+
262
+ await client.workspaces.deleteWorkspace(workspace);
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
+ ```