@xata.io/client 0.13.0 → 0.13.3
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 +34 -0
- package/Usage.md +380 -0
- package/dist/index.cjs +64 -20
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +31 -14
- package/dist/index.mjs +64 -20
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -3
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,39 @@
|
|
1
1
|
# @xata.io/client
|
2
2
|
|
3
|
+
## 0.13.3
|
4
|
+
|
5
|
+
### Patch Changes
|
6
|
+
|
7
|
+
- [#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
|
8
|
+
|
9
|
+
## 0.13.2
|
10
|
+
|
11
|
+
### Patch Changes
|
12
|
+
|
13
|
+
- [#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
|
14
|
+
|
15
|
+
* [#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
|
16
|
+
|
17
|
+
- [#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
|
18
|
+
|
19
|
+
## 0.13.1
|
20
|
+
|
21
|
+
### Patch Changes
|
22
|
+
|
23
|
+
- [#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
|
24
|
+
|
25
|
+
* [#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
|
26
|
+
|
27
|
+
- [#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
|
28
|
+
|
29
|
+
* [#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
|
30
|
+
|
31
|
+
- [#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
|
32
|
+
|
33
|
+
* [#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
|
34
|
+
|
35
|
+
- [#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`
|
36
|
+
|
3
37
|
## 0.13.0
|
4
38
|
|
5
39
|
### Minor Changes
|
package/Usage.md
ADDED
@@ -0,0 +1,380 @@
|
|
1
|
+
# TypeScript SDK
|
2
|
+
|
3
|
+
There're four types of objects in the Xata TypeScript SDK:
|
4
|
+
|
5
|
+
- `Repository`: a table representation that can be used to create, read, update, and delete records.
|
6
|
+
- `Query`: a combination of filters and other parameters to retrieve a collection of records.
|
7
|
+
- `XataRecord`: a row in a table.
|
8
|
+
- `Page`: a collection of records that can be paginated.
|
9
|
+
|
10
|
+
## Repository
|
11
|
+
|
12
|
+
Any table in the database can be represented by a `Repository` object.
|
13
|
+
|
14
|
+
A repository is an object that can be used to create, read, update, and delete records in the table it represents.
|
15
|
+
|
16
|
+
It also implements the `Query` and `Page` interfaces, so you can use it to query and paginate the records in the table too. We'll see how to use these objects in the next section.
|
17
|
+
|
18
|
+
### Reading records
|
19
|
+
|
20
|
+
The `read()` method can be used to read a records by their ids:
|
21
|
+
|
22
|
+
- If the object cannot be found, the method will return `null`.
|
23
|
+
- If the object can be found, the method will return a `XataRecord` object.
|
24
|
+
|
25
|
+
You can read a single record by its id.
|
26
|
+
|
27
|
+
```ts
|
28
|
+
const user = await xata.db.users.read('rec_1234abcdef');
|
29
|
+
```
|
30
|
+
|
31
|
+
You can also read multiple records by their ids.
|
32
|
+
|
33
|
+
```ts
|
34
|
+
const users = await xata.db.users.read(['rec_1234abcdef', 'rec_5678defgh']);
|
35
|
+
```
|
36
|
+
|
37
|
+
And you can read records coming from an object that contains an `id` property.
|
38
|
+
|
39
|
+
```ts
|
40
|
+
const object1 = { id: 'rec_1234abcdef' };
|
41
|
+
const object2 = { id: 'rec_5678defgh' };
|
42
|
+
|
43
|
+
const user = await xata.db.users.read(object1);
|
44
|
+
const users = await xata.db.users.read([object1, object2]);
|
45
|
+
```
|
46
|
+
|
47
|
+
### Creating records
|
48
|
+
|
49
|
+
Both the `create()` and `createOrUpdate()` methods can be used to create a new record.
|
50
|
+
|
51
|
+
- The `create()` method will create a new record and fail if the provided id already exists.
|
52
|
+
- The `createOrUpdate()` method will create a new record if the provided id doesn't exist, or update an existing record if it does.
|
53
|
+
|
54
|
+
You can create a record without providing an id, and the id will be generated automatically.
|
55
|
+
|
56
|
+
```ts
|
57
|
+
const user = await xata.db.users.create({ fullName: 'John Smith' });
|
58
|
+
user.id; // 'rec_1234abcdef'
|
59
|
+
```
|
60
|
+
|
61
|
+
You can create a record with an id as parameter, and the id will be used.
|
62
|
+
|
63
|
+
```ts
|
64
|
+
const user = await xata.db.users.create('user_admin', { fullName: 'John Smith' });
|
65
|
+
user.id; // 'user_admin'
|
66
|
+
```
|
67
|
+
|
68
|
+
You can create a record with the id provided in the object, and the id will be used.
|
69
|
+
|
70
|
+
```ts
|
71
|
+
const user = await xata.db.users.create({ id: 'user_admin', fullName: 'John Smith' });
|
72
|
+
user.id; // 'user_admin'
|
73
|
+
```
|
74
|
+
|
75
|
+
You can create multiple records at once by providing an array of objects.
|
76
|
+
|
77
|
+
```ts
|
78
|
+
const users = await xata.db.users.create([{ fullName: 'John Smith' }, { id: 'user_admin', fullName: 'Jane Doe' }]);
|
79
|
+
users[0].id; // 'rec_1234abcdef'
|
80
|
+
users[1].id; // 'user_admin'
|
81
|
+
```
|
82
|
+
|
83
|
+
The `createOrUpdate()` method beaves the same way as `create()` but you will always need to provide an id.
|
84
|
+
|
85
|
+
```ts
|
86
|
+
const user1 = await xata.db.users.createOrUpdate('user_admin', { fullName: 'John Smith' });
|
87
|
+
const user2 = await xata.db.users.createOrUpdate({ id: 'user_manager', fullName: 'Jane Doe' });
|
88
|
+
const users = await xata.db.users.createOrUpdate([
|
89
|
+
{ id: 'user_admin', fullName: 'John Smith' },
|
90
|
+
{ id: 'user_manager', fullName: 'Jane Doe' }
|
91
|
+
]);
|
92
|
+
```
|
93
|
+
|
94
|
+
### Updating records
|
95
|
+
|
96
|
+
The `update()` method can be used to update an existing record. It will throw an Error if the record cannot be found.
|
97
|
+
|
98
|
+
```ts
|
99
|
+
const user = await xata.db.users.update('rec_1234abcdef', { fullName: 'John Smith' });
|
100
|
+
```
|
101
|
+
|
102
|
+
The `id` property can also be sent in the object update.
|
103
|
+
|
104
|
+
```ts
|
105
|
+
const user = await xata.db.users.update({ id: 'user_admin', fullName: 'John Smith' });
|
106
|
+
```
|
107
|
+
|
108
|
+
You can update multiple records at once by providing an array of objects.
|
109
|
+
|
110
|
+
```ts
|
111
|
+
const users = await xata.db.users.update([
|
112
|
+
{ id: 'rec_1234abcdef', fullName: 'John Smith' },
|
113
|
+
{ id: 'user_admin', fullName: 'Jane Doe' }
|
114
|
+
]);
|
115
|
+
```
|
116
|
+
|
117
|
+
### Deleting records
|
118
|
+
|
119
|
+
The `delete()` method can be used to delete an existing record. It will throw an Error if the record cannot be found.
|
120
|
+
|
121
|
+
```ts
|
122
|
+
const user = await xata.db.users.delete('rec_1234abcdef');
|
123
|
+
```
|
124
|
+
|
125
|
+
You can delete multiple records at once by providing an array of ids.
|
126
|
+
|
127
|
+
```ts
|
128
|
+
const users = await xata.db.users.delete(['rec_1234abcdef', 'user_admin']);
|
129
|
+
```
|
130
|
+
|
131
|
+
You can delete records coming from an object that contains an `id` property.
|
132
|
+
|
133
|
+
```ts
|
134
|
+
const object1 = { id: 'rec_1234abcdef' };
|
135
|
+
|
136
|
+
const user = await xata.db.users.delete(object1);
|
137
|
+
```
|
138
|
+
|
139
|
+
You can delete records coming from an array of objects that contain an `id` property.
|
140
|
+
|
141
|
+
```ts
|
142
|
+
const object1 = { id: 'rec_1234abcdef' };
|
143
|
+
const object2 = { id: 'user_admin' };
|
144
|
+
|
145
|
+
const users = await xata.db.users.delete([object1, object2]);
|
146
|
+
```
|
147
|
+
|
148
|
+
### Searching records
|
149
|
+
|
150
|
+
The `search()` method can be used to search records. It returns an array of records.
|
151
|
+
|
152
|
+
```ts
|
153
|
+
const results = await xata.db.users.search('John');
|
154
|
+
```
|
155
|
+
|
156
|
+
Also you can customize the results with an `options` object that includes `fuzziness`, `filter` and all the other options the API supports.
|
157
|
+
|
158
|
+
```ts
|
159
|
+
const results = await xata.db.users.search('John', { fuzziness: 1, filter: { 'team.name': 'Marketing' } });
|
160
|
+
```
|
161
|
+
|
162
|
+
## Query
|
163
|
+
|
164
|
+
To get a collection of records, you can use the `Query` object.
|
165
|
+
|
166
|
+
It provides the following methods:
|
167
|
+
|
168
|
+
- `getFirst()`: returns the first record in the query results.
|
169
|
+
- `getPaginated()`: returns a page of records in the query results.
|
170
|
+
- `getAll()`: returns all the records in the query results.
|
171
|
+
- `getMany()`: returns an array of some records in the query results.
|
172
|
+
|
173
|
+
Since the `Repository` class implements the `Query` interface, you can use it to query and paginate the records in the table too.
|
174
|
+
|
175
|
+
```ts
|
176
|
+
const user = xata.db.users.getFirst();
|
177
|
+
```
|
178
|
+
|
179
|
+
### Column selection
|
180
|
+
|
181
|
+
The `Query` object can be used to select the columns that will be returned in the results.
|
182
|
+
|
183
|
+
You can pick multiple columns by providing an array of column names, or you can pick all the columns by providing `*`.
|
184
|
+
|
185
|
+
The dot notation is supported to select columns from nested objects.
|
186
|
+
|
187
|
+
```ts
|
188
|
+
const user = xata.db.users.select(['*', 'team.*']).getFirst();
|
189
|
+
```
|
190
|
+
|
191
|
+
### Sorting
|
192
|
+
|
193
|
+
The `Query` object can be used to sort the order of the results.
|
194
|
+
|
195
|
+
You can sort the results by providing a column name and an `asc` or `desc` string.
|
196
|
+
|
197
|
+
```ts
|
198
|
+
const user = xata.db.users.orderBy('fullName', 'asc').getFirst();
|
199
|
+
```
|
200
|
+
|
201
|
+
### Filtering
|
202
|
+
|
203
|
+
You can filter the results by providing the column and the value to filter.
|
204
|
+
|
205
|
+
```ts
|
206
|
+
const user = xata.db.users.filter('fullName', 'John').getFirst();
|
207
|
+
```
|
208
|
+
|
209
|
+
To combine multiple filters in an 'AND' clause, you can pipe the filters together.
|
210
|
+
|
211
|
+
```ts
|
212
|
+
const user = xata.db.users.filter('fullName', 'John').filter('team.name', 'Marketing').getFirst();
|
213
|
+
```
|
214
|
+
|
215
|
+
Also you can filter the results by providing a `filter` object.
|
216
|
+
|
217
|
+
```ts
|
218
|
+
const user = xata.db.users.filter({ fullName: 'John', 'team.name': 'Marketing' }).getFirst();
|
219
|
+
```
|
220
|
+
|
221
|
+
We offer some helper functions to build the filter values, like: `gt`, `ge`, `gte`, `lt`, `le`, `lte`, `exists`, `notExists`, `startsWith`, `endsWith`, `pattern`, `is`, `isNot`, `contains`, `includes`, and others specific to the type of the column.
|
222
|
+
|
223
|
+
```ts
|
224
|
+
const user = xata.db.users.filter('name', startsWith('Bar')).getFirst();
|
225
|
+
```
|
226
|
+
|
227
|
+
If you prefer to directly use the filter operators as in the API, you can add them in the `filter` object.
|
228
|
+
|
229
|
+
```ts
|
230
|
+
xata.db.users.filter({ full_name: { $startsWith: 'foo' } }).getFirst();
|
231
|
+
```
|
232
|
+
|
233
|
+
### Combining queries
|
234
|
+
|
235
|
+
Queries can be stored in variables and can be combined with other queries.
|
236
|
+
|
237
|
+
```ts
|
238
|
+
const johnQuery = xata.db.users.filter('fullName', 'John');
|
239
|
+
const janeQuery = xata.db.users.filter('fullName', 'Jane');
|
240
|
+
|
241
|
+
const johns = await johnQuery.getAll();
|
242
|
+
const janes = await janeQuery.getAll();
|
243
|
+
|
244
|
+
const users = await xata.db.users.any(johnQuery, janeQuery).getAll();
|
245
|
+
```
|
246
|
+
|
247
|
+
We offer the following helper methods to combine queries:
|
248
|
+
|
249
|
+
- `any()`: returns the records that match any of the queries.
|
250
|
+
- `all()`: returns the records that match all of the queries.
|
251
|
+
- `none()`: returns the records that match none of the queries.
|
252
|
+
- `not()`: returns the records that don't match the given query.
|
253
|
+
|
254
|
+
You can read more about the query operators in the API section for the query table endpoint.
|
255
|
+
|
256
|
+
## Page
|
257
|
+
|
258
|
+
Some methods of the `Query` interface provide a `Page` object as a return value that can be used to paginate the results.
|
259
|
+
|
260
|
+
The `Page` object can be used to get the queried records of a table in pages. It is an abstraction of cursor-based pagination.
|
261
|
+
|
262
|
+
It contains:
|
263
|
+
|
264
|
+
- `records`: Array of `XataRecord` objects.
|
265
|
+
- `hasNextPage`: Function that returns a boolean indicating if there is a next page.
|
266
|
+
- `nextPage`: Async function that can be used to get the next page.
|
267
|
+
- `previousPage`: Async function that can be used to get the previous page.
|
268
|
+
- `firstPage`: Async function that can be used to get the first page.
|
269
|
+
- `lastPage`: Async function that can be used to get the last page.
|
270
|
+
- `meta`: Information about the current page and its cursor.
|
271
|
+
|
272
|
+
```ts
|
273
|
+
const page = await xata.db.users.getPaginated();
|
274
|
+
page.records; // Array of `XataRecord` objects.
|
275
|
+
page.hasNextPage();
|
276
|
+
|
277
|
+
const page2 = await page.nextPage();
|
278
|
+
page2.records; // Array of `XataRecord` objects.
|
279
|
+
|
280
|
+
const page1 = await page2.previousPage();
|
281
|
+
page1.records; // Array of `XataRecord` objects.
|
282
|
+
|
283
|
+
const firstPage = await page1.firstPage();
|
284
|
+
firstPage.records; // Array of `XataRecord` objects.
|
285
|
+
```
|
286
|
+
|
287
|
+
The `Repository` class implements the `Query` interface, so you can use it to paginate the records in the table too.
|
288
|
+
|
289
|
+
```ts
|
290
|
+
const page = await xata.db.users.firstPage();
|
291
|
+
page.records; // Array of `XataRecord` objects.
|
292
|
+
```
|
293
|
+
|
294
|
+
The array returned in `records` also implements the `Page` interface.
|
295
|
+
|
296
|
+
```ts
|
297
|
+
const { records } = await xata.db.users.getPaginated();
|
298
|
+
records.hasNextPage();
|
299
|
+
const { records: page2Records } = await records.nextPage();
|
300
|
+
```
|
301
|
+
|
302
|
+
Optionally you can provide `offset` and `size` parameters to the pagination and override the default values.
|
303
|
+
|
304
|
+
```ts
|
305
|
+
const page = await xata.db.users.getPaginated();
|
306
|
+
page.records; // Array of `XataRecord` objects.
|
307
|
+
|
308
|
+
// A second page with size 50
|
309
|
+
const page2 = await page.nextPage(50);
|
310
|
+
|
311
|
+
// A third page with size 10 but an offset of 60
|
312
|
+
const page3 = await page2.nextPage(10, 60);
|
313
|
+
```
|
314
|
+
|
315
|
+
### Iterators and generators
|
316
|
+
|
317
|
+
The `Query` object can be used to iterate over the results as a way to paginate the results.
|
318
|
+
|
319
|
+
```ts
|
320
|
+
for await (const user of xata.db.users) {
|
321
|
+
await user.update({ full_name: 'John Doe' });
|
322
|
+
}
|
323
|
+
```
|
324
|
+
|
325
|
+
Also if you want to retrieve more than one record at a time in the iterator, you can use the `getIterator()` method.
|
326
|
+
|
327
|
+
```ts
|
328
|
+
for await (const users of xata.db.users.getIterator({ batchSize: 50 })) {
|
329
|
+
console.log(users);
|
330
|
+
}
|
331
|
+
```
|
332
|
+
|
333
|
+
### Helper variables
|
334
|
+
|
335
|
+
We expose some helper variables of the API limits when paginating:
|
336
|
+
|
337
|
+
- `PAGINATION_MAX_SIZE`: Maximum page size.
|
338
|
+
- `PAGINATION_DEFAULT_SIZE`: Default page size.
|
339
|
+
- `PAGINATION_MAX_OFFSET`: Maximum offset.
|
340
|
+
- `PAGINATION_DEFAULT_OFFSET`: Default offset.
|
341
|
+
|
342
|
+
You can use these variables if you implement your own pagination mechanism, as they will be updated when the API limits are updated.
|
343
|
+
|
344
|
+
## XataRecord
|
345
|
+
|
346
|
+
Every row in a table is represented by an `XataRecord` object.
|
347
|
+
|
348
|
+
It contains an `id` property that represents the unique identifier of the record and all the fields of the table.
|
349
|
+
|
350
|
+
Also it provides some methods to read again, update or delete the record.
|
351
|
+
|
352
|
+
```ts
|
353
|
+
const user = await xata.db.users.read('rec_1234abcdef');
|
354
|
+
user?.id; // 'rec_1234abcdef'
|
355
|
+
user?.fullName; // 'John Smith'
|
356
|
+
user?.read(); // Reads the record again
|
357
|
+
user?.update({ fullName: 'John Doe' }); // Partially updates the record
|
358
|
+
user?.delete(); // Deletes the record
|
359
|
+
```
|
360
|
+
|
361
|
+
If the table contains a link property, it will be represented as a `Link` object containing its `id` property and methods to read again or update the linked record.
|
362
|
+
|
363
|
+
```ts
|
364
|
+
const user = await xata.db.users.read('rec_1234abcdef');
|
365
|
+
user?.team?.id; // 'rec_5678defgh'
|
366
|
+
user?.team?.read(); // Reads the linked record properties
|
367
|
+
user?.team?.update({ name: 'A team' }); // Partially updates the linked record
|
368
|
+
```
|
369
|
+
|
370
|
+
When working with `queries` you can expand the properties returned in a `XataRecord` object.
|
371
|
+
|
372
|
+
By default only the current table fields are returned, and the id of any linked record.
|
373
|
+
|
374
|
+
```ts
|
375
|
+
const user = await xata.db.users.filter('id', 'rec_1234abcdef').select(['*', 'team.*']).getFirst();
|
376
|
+
user?.id; // 'rec_1234abcdef'
|
377
|
+
user?.fullName; // 'John Smith'
|
378
|
+
user?.team?.id; // 'rec_5678defgh'
|
379
|
+
user?.team?.name; // 'A team'
|
380
|
+
```
|
package/dist/index.cjs
CHANGED
@@ -21,7 +21,8 @@ function toBase64(value) {
|
|
21
21
|
try {
|
22
22
|
return btoa(value);
|
23
23
|
} catch (err) {
|
24
|
-
|
24
|
+
const buf = Buffer;
|
25
|
+
return buf.from(value).toString("base64");
|
25
26
|
}
|
26
27
|
}
|
27
28
|
|
@@ -77,21 +78,28 @@ function getFetchImplementation(userFetch) {
|
|
77
78
|
return fetchImpl;
|
78
79
|
}
|
79
80
|
|
81
|
+
const VERSION = "0.13.3";
|
82
|
+
|
80
83
|
class ErrorWithCause extends Error {
|
81
84
|
constructor(message, options) {
|
82
85
|
super(message, options);
|
83
86
|
}
|
84
87
|
}
|
85
88
|
class FetcherError extends ErrorWithCause {
|
86
|
-
constructor(status, data) {
|
89
|
+
constructor(status, data, requestId) {
|
87
90
|
super(getMessage(data));
|
88
91
|
this.status = status;
|
89
92
|
this.errors = isBulkError(data) ? data.errors : void 0;
|
93
|
+
this.requestId = requestId;
|
90
94
|
if (data instanceof Error) {
|
91
95
|
this.stack = data.stack;
|
92
96
|
this.cause = data.cause;
|
93
97
|
}
|
94
98
|
}
|
99
|
+
toString() {
|
100
|
+
const error = super.toString();
|
101
|
+
return `[${this.status}] (${this.requestId ?? "Unknown"}): ${error}`;
|
102
|
+
}
|
95
103
|
}
|
96
104
|
function isBulkError(error) {
|
97
105
|
return isObject(error) && Array.isArray(error.errors);
|
@@ -114,7 +122,12 @@ function getMessage(data) {
|
|
114
122
|
}
|
115
123
|
|
116
124
|
const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
|
117
|
-
const
|
125
|
+
const cleanQueryParams = Object.entries(queryParams).reduce((acc, [key, value]) => {
|
126
|
+
if (value === void 0 || value === null)
|
127
|
+
return acc;
|
128
|
+
return { ...acc, [key]: value };
|
129
|
+
}, {});
|
130
|
+
const query = new URLSearchParams(cleanQueryParams).toString();
|
118
131
|
const queryString = query.length > 0 ? `?${query}` : "";
|
119
132
|
return url.replace(/\{\w*\}/g, (key) => pathParams[key.slice(1, -1)]) + queryString;
|
120
133
|
};
|
@@ -154,6 +167,7 @@ async function fetch$1({
|
|
154
167
|
body: body ? JSON.stringify(body) : void 0,
|
155
168
|
headers: {
|
156
169
|
"Content-Type": "application/json",
|
170
|
+
"User-Agent": `Xata client-ts/${VERSION}`,
|
157
171
|
...headers,
|
158
172
|
...hostHeader(fullUrl),
|
159
173
|
Authorization: `Bearer ${apiKey}`
|
@@ -162,14 +176,15 @@ async function fetch$1({
|
|
162
176
|
if (response.status === 204) {
|
163
177
|
return {};
|
164
178
|
}
|
179
|
+
const requestId = response.headers?.get("x-request-id") ?? void 0;
|
165
180
|
try {
|
166
181
|
const jsonResponse = await response.json();
|
167
182
|
if (response.ok) {
|
168
183
|
return jsonResponse;
|
169
184
|
}
|
170
|
-
throw new FetcherError(response.status, jsonResponse);
|
185
|
+
throw new FetcherError(response.status, jsonResponse, requestId);
|
171
186
|
} catch (error) {
|
172
|
-
throw new FetcherError(response.status, error);
|
187
|
+
throw new FetcherError(response.status, error, requestId);
|
173
188
|
}
|
174
189
|
}
|
175
190
|
|
@@ -693,10 +708,10 @@ class DatabaseApi {
|
|
693
708
|
...this.extraProps
|
694
709
|
});
|
695
710
|
}
|
696
|
-
resolveBranch(workspace, dbName, gitBranch) {
|
711
|
+
resolveBranch(workspace, dbName, gitBranch, fallbackBranch) {
|
697
712
|
return operationsByTag.database.resolveBranch({
|
698
713
|
pathParams: { workspace, dbName },
|
699
|
-
queryParams: { gitBranch },
|
714
|
+
queryParams: { gitBranch, fallbackBranch },
|
700
715
|
...this.extraProps
|
701
716
|
});
|
702
717
|
}
|
@@ -980,10 +995,20 @@ function isCursorPaginationOptions(options) {
|
|
980
995
|
}
|
981
996
|
const _RecordArray = class extends Array {
|
982
997
|
constructor(page, overrideRecords) {
|
983
|
-
super(...overrideRecords
|
998
|
+
super(..._RecordArray.parseConstructorParams(page, overrideRecords));
|
984
999
|
__privateAdd$6(this, _page, void 0);
|
985
1000
|
__privateSet$5(this, _page, page);
|
986
1001
|
}
|
1002
|
+
static parseConstructorParams(...args) {
|
1003
|
+
if (args.length === 1 && typeof args[0] === "number") {
|
1004
|
+
return new Array(args[0]);
|
1005
|
+
}
|
1006
|
+
if (args.length <= 2 && isObject(args[0]?.meta) && Array.isArray(args[1] ?? [])) {
|
1007
|
+
const result = args[1] ?? args[0].records ?? [];
|
1008
|
+
return new Array(...result);
|
1009
|
+
}
|
1010
|
+
return new Array(...args);
|
1011
|
+
}
|
987
1012
|
async nextPage(size, offset) {
|
988
1013
|
const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
|
989
1014
|
return new _RecordArray(newPage);
|
@@ -1254,9 +1279,29 @@ class RestRepository extends Query {
|
|
1254
1279
|
if (Array.isArray(a)) {
|
1255
1280
|
if (a.length === 0)
|
1256
1281
|
return [];
|
1257
|
-
const
|
1258
|
-
|
1259
|
-
|
1282
|
+
const [itemsWithoutIds, itemsWithIds, order] = a.reduce(([accWithoutIds, accWithIds, accOrder], item) => {
|
1283
|
+
const condition = isString(item.id);
|
1284
|
+
accOrder.push(condition);
|
1285
|
+
if (condition) {
|
1286
|
+
accWithIds.push(item);
|
1287
|
+
} else {
|
1288
|
+
accWithoutIds.push(item);
|
1289
|
+
}
|
1290
|
+
return [accWithoutIds, accWithIds, accOrder];
|
1291
|
+
}, [[], [], []]);
|
1292
|
+
const recordsWithoutId = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, itemsWithoutIds);
|
1293
|
+
await Promise.all(recordsWithoutId.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
|
1294
|
+
if (itemsWithIds.length > 100) {
|
1295
|
+
console.warn("Bulk create operation with id is not optimized in the Xata API yet, this request might be slow");
|
1296
|
+
}
|
1297
|
+
const recordsWithId = await Promise.all(itemsWithIds.map((object) => this.create(object)));
|
1298
|
+
return order.map((condition) => {
|
1299
|
+
if (condition) {
|
1300
|
+
return recordsWithId.shift();
|
1301
|
+
} else {
|
1302
|
+
return recordsWithoutId.shift();
|
1303
|
+
}
|
1304
|
+
}).filter((record) => !!record);
|
1260
1305
|
}
|
1261
1306
|
if (isString(a) && isObject(b)) {
|
1262
1307
|
if (a === "")
|
@@ -1283,16 +1328,18 @@ class RestRepository extends Query {
|
|
1283
1328
|
if (Array.isArray(a)) {
|
1284
1329
|
if (a.length === 0)
|
1285
1330
|
return [];
|
1286
|
-
|
1331
|
+
const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
|
1332
|
+
return this.getAll({ filter: { id: { $any: ids } } });
|
1287
1333
|
}
|
1288
|
-
|
1289
|
-
|
1334
|
+
const id = isString(a) ? a : a.id;
|
1335
|
+
if (isString(id)) {
|
1336
|
+
const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, id);
|
1290
1337
|
if (cacheRecord)
|
1291
1338
|
return cacheRecord;
|
1292
1339
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1293
1340
|
try {
|
1294
1341
|
const response = await getRecord({
|
1295
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId:
|
1342
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
|
1296
1343
|
...fetchProps
|
1297
1344
|
});
|
1298
1345
|
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
@@ -1464,7 +1511,7 @@ bulkInsertTableRecords_fn = async function(objects) {
|
|
1464
1511
|
body: { records },
|
1465
1512
|
...fetchProps
|
1466
1513
|
});
|
1467
|
-
const finalObjects = await this.
|
1514
|
+
const finalObjects = await this.read(response.recordIDs);
|
1468
1515
|
if (finalObjects.length !== objects.length) {
|
1469
1516
|
throw new Error("The server failed to save some records");
|
1470
1517
|
}
|
@@ -1868,10 +1915,7 @@ async function getDatabaseBranch(branch, options) {
|
|
1868
1915
|
apiUrl: databaseURL,
|
1869
1916
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1870
1917
|
workspacesApiUrl: `${protocol}//${host}`,
|
1871
|
-
pathParams: {
|
1872
|
-
dbBranchName,
|
1873
|
-
workspace
|
1874
|
-
}
|
1918
|
+
pathParams: { dbBranchName, workspace }
|
1875
1919
|
});
|
1876
1920
|
} catch (err) {
|
1877
1921
|
if (isObject(err) && err.status === 404)
|