@xata.io/client 0.0.0-alpha.vf2696e7 → 0.0.0-alpha.vf344dbc

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/.eslintrc.cjs CHANGED
@@ -6,8 +6,7 @@ module.exports = {
6
6
  project: 'packages/client/tsconfig.json'
7
7
  },
8
8
  rules: {
9
- '@typescript-eslint/no-explicit-any': 'off',
10
- '@typescript-eslint/ban-types': 'off',
11
9
  '@typescript-eslint/no-floating-promises': 'error',
10
+ "@typescript-eslint/strict-boolean-expressions": ["error", { allowNullableString: true, allowNullableObject: true }],
12
11
  }
13
12
  };
package/CHANGELOG.md CHANGED
@@ -1,5 +1,101 @@
1
1
  # @xata.io/client
2
2
 
3
+ ## 0.13.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#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
8
+
9
+ * [#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
10
+
11
+ ## 0.12.0
12
+
13
+ ### Minor Changes
14
+
15
+ - [#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
16
+
17
+ ### Patch Changes
18
+
19
+ - [#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+
20
+
21
+ * [#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
22
+
23
+ ## 0.11.0
24
+
25
+ ### Minor Changes
26
+
27
+ - [#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
28
+
29
+ ### Patch Changes
30
+
31
+ - [#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
32
+
33
+ * [#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
34
+
35
+ - [#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
36
+
37
+ * [#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
38
+
39
+ - [#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
40
+
41
+ * [#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
42
+
43
+ ## 0.10.2
44
+
45
+ ### Patch Changes
46
+
47
+ - [#312](https://github.com/xataio/client-ts/pull/312) [`0edf1af`](https://github.com/xataio/client-ts/commit/0edf1af2205c4761d53a02c74ddaab3168d69775) Thanks [@SferaDev](https://github.com/SferaDev)! - Add filtering to search by table
48
+
49
+ * [#312](https://github.com/xataio/client-ts/pull/312) [`66ad7cc`](https://github.com/xataio/client-ts/commit/66ad7cc0365046c5d039c37117feac04428d8373) Thanks [@SferaDev](https://github.com/SferaDev)! - Add new API method for searching in a given table
50
+
51
+ ## 0.10.1
52
+
53
+ ### Patch Changes
54
+
55
+ - [#271](https://github.com/xataio/client-ts/pull/271) [`0bb17b8`](https://github.com/xataio/client-ts/commit/0bb17b88d49f1c8be32d2d6b0b3a5918890876cb) Thanks [@SferaDev](https://github.com/SferaDev)! - Link and resolve branches from git
56
+
57
+ ## 0.10.0
58
+
59
+ ### Minor Changes
60
+
61
+ - [#272](https://github.com/xataio/client-ts/pull/272) [`6d76275`](https://github.com/xataio/client-ts/commit/6d7627555a404a4c2da42f4187df6f8300f9a46f) Thanks [@SferaDev](https://github.com/SferaDev)! - Rename page options to pagination
62
+
63
+ * [#270](https://github.com/xataio/client-ts/pull/270) [`1864742`](https://github.com/xataio/client-ts/commit/18647428d8608841de514c3784fb711c39dccc6d) Thanks [@SferaDev](https://github.com/SferaDev)! - Move chunk to options object
64
+
65
+ ### Patch Changes
66
+
67
+ - [#281](https://github.com/xataio/client-ts/pull/281) [`d1ec0df`](https://github.com/xataio/client-ts/commit/d1ec0df14834088a816919bfc68216f3f9b2d9ef) Thanks [@SferaDev](https://github.com/SferaDev)! - Do not send from param on undefined
68
+
69
+ * [#282](https://github.com/xataio/client-ts/pull/282) [`1af6f1a`](https://github.com/xataio/client-ts/commit/1af6f1aaa1123e77a895961581c87f06a88db698) Thanks [@SferaDev](https://github.com/SferaDev)! - Do not allow filter/sort with cursor navigation
70
+
71
+ - [#284](https://github.com/xataio/client-ts/pull/284) [`be4eda8`](https://github.com/xataio/client-ts/commit/be4eda8f73037d97fef7de28b56d7471dd867875) Thanks [@SferaDev](https://github.com/SferaDev)! - Fix cache ttl with 0 value
72
+
73
+ * [#265](https://github.com/xataio/client-ts/pull/265) [`99be734`](https://github.com/xataio/client-ts/commit/99be734827576d888aa12a579ed1983a0a8a8e83) Thanks [@SferaDev](https://github.com/SferaDev)! - Add datetime field type support
74
+
75
+ ## 0.9.1
76
+
77
+ ### Patch Changes
78
+
79
+ - [#250](https://github.com/xataio/client-ts/pull/250) [`5d7c9e4`](https://github.com/xataio/client-ts/commit/5d7c9e4fa2799255e2bfc8b6fb12c89dc4e1f35e) Thanks [@xata-bot](https://github.com/xata-bot)! - Add branch resolution endpoints to api client
80
+
81
+ * [#261](https://github.com/xataio/client-ts/pull/261) [`e95f20a`](https://github.com/xataio/client-ts/commit/e95f20a7bce264936680353f816065fa379448fc) Thanks [@gimenete](https://github.com/gimenete)! - Fixes a compatibility error in CloudFlare workers with latest version of wrangler
82
+
83
+ ## 0.9.0
84
+
85
+ ### Minor Changes
86
+
87
+ - [#246](https://github.com/xataio/client-ts/pull/246) [`2848894`](https://github.com/xataio/client-ts/commit/284889446bbac5d6737086bf01a588d97b841730) Thanks [@SferaDev](https://github.com/SferaDev)! - Rename getOne to getFirst
88
+
89
+ ### Patch Changes
90
+
91
+ - [#254](https://github.com/xataio/client-ts/pull/254) [`2fc2788`](https://github.com/xataio/client-ts/commit/2fc2788e583c047ffb2cd693f053f60ce608149c) Thanks [@SferaDev](https://github.com/SferaDev)! - Deprecate XataApiClient
92
+
93
+ * [#230](https://github.com/xataio/client-ts/pull/230) [`a96da7c`](https://github.com/xataio/client-ts/commit/a96da7c8b548604ed25001390992531537675a44) Thanks [@SferaDev](https://github.com/SferaDev)! - Include tables in Proxy target for object introspection (shell)
94
+
95
+ - [#222](https://github.com/xataio/client-ts/pull/222) [`e8d595f`](https://github.com/xataio/client-ts/commit/e8d595f54efe126b39c78cc771a5d69c551f4fba) Thanks [@SferaDev](https://github.com/SferaDev)! - Add cache strategies
96
+
97
+ * [#244](https://github.com/xataio/client-ts/pull/244) [`c4dcd11`](https://github.com/xataio/client-ts/commit/c4dcd110d8f9dc3a7e4510f2f00257c9109e51fa) Thanks [@gimenete](https://github.com/gimenete)! - getCurrentBranchName never returns a Promise that resolves to undefined
98
+
3
99
  ## 0.8.4
4
100
 
5
101
  ### Patch 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
+ ```