@xata.io/client 0.10.2 → 0.13.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 +40 -0
- package/README.md +265 -1
- package/dist/index.cjs +113 -40
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +226 -49
- package/dist/index.mjs +112 -41
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -2
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,45 @@
|
|
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
|
+
|
3
43
|
## 0.10.2
|
4
44
|
|
5
45
|
### Patch Changes
|
package/README.md
CHANGED
@@ -1 +1,265 @@
|
|
1
|
-
|
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
|
+
```
|
package/dist/index.cjs
CHANGED
@@ -11,8 +11,11 @@ function compact(arr) {
|
|
11
11
|
function isObject(value) {
|
12
12
|
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
13
13
|
}
|
14
|
+
function isDefined(value) {
|
15
|
+
return value !== null && value !== void 0;
|
16
|
+
}
|
14
17
|
function isString(value) {
|
15
|
-
return value
|
18
|
+
return isDefined(value) && typeof value === "string";
|
16
19
|
}
|
17
20
|
function toBase64(value) {
|
18
21
|
try {
|
@@ -74,7 +77,12 @@ function getFetchImplementation(userFetch) {
|
|
74
77
|
return fetchImpl;
|
75
78
|
}
|
76
79
|
|
77
|
-
class
|
80
|
+
class ErrorWithCause extends Error {
|
81
|
+
constructor(message, options) {
|
82
|
+
super(message, options);
|
83
|
+
}
|
84
|
+
}
|
85
|
+
class FetcherError extends ErrorWithCause {
|
78
86
|
constructor(status, data) {
|
79
87
|
super(getMessage(data));
|
80
88
|
this.status = status;
|
@@ -938,13 +946,13 @@ var __privateSet$5 = (obj, member, value, setter) => {
|
|
938
946
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
939
947
|
return value;
|
940
948
|
};
|
941
|
-
var _query;
|
949
|
+
var _query, _page;
|
942
950
|
class Page {
|
943
951
|
constructor(query, meta, records = []) {
|
944
952
|
__privateAdd$6(this, _query, void 0);
|
945
953
|
__privateSet$5(this, _query, query);
|
946
954
|
this.meta = meta;
|
947
|
-
this.records = records;
|
955
|
+
this.records = new RecordArray(this, records);
|
948
956
|
}
|
949
957
|
async nextPage(size, offset) {
|
950
958
|
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
|
@@ -964,9 +972,40 @@ class Page {
|
|
964
972
|
}
|
965
973
|
_query = new WeakMap();
|
966
974
|
const PAGINATION_MAX_SIZE = 200;
|
967
|
-
const PAGINATION_DEFAULT_SIZE =
|
975
|
+
const PAGINATION_DEFAULT_SIZE = 20;
|
968
976
|
const PAGINATION_MAX_OFFSET = 800;
|
969
977
|
const PAGINATION_DEFAULT_OFFSET = 0;
|
978
|
+
function isCursorPaginationOptions(options) {
|
979
|
+
return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
|
980
|
+
}
|
981
|
+
const _RecordArray = class extends Array {
|
982
|
+
constructor(page, overrideRecords) {
|
983
|
+
super(...overrideRecords ?? page.records);
|
984
|
+
__privateAdd$6(this, _page, void 0);
|
985
|
+
__privateSet$5(this, _page, page);
|
986
|
+
}
|
987
|
+
async nextPage(size, offset) {
|
988
|
+
const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
|
989
|
+
return new _RecordArray(newPage);
|
990
|
+
}
|
991
|
+
async previousPage(size, offset) {
|
992
|
+
const newPage = await __privateGet$6(this, _page).previousPage(size, offset);
|
993
|
+
return new _RecordArray(newPage);
|
994
|
+
}
|
995
|
+
async firstPage(size, offset) {
|
996
|
+
const newPage = await __privateGet$6(this, _page).firstPage(size, offset);
|
997
|
+
return new _RecordArray(newPage);
|
998
|
+
}
|
999
|
+
async lastPage(size, offset) {
|
1000
|
+
const newPage = await __privateGet$6(this, _page).lastPage(size, offset);
|
1001
|
+
return new _RecordArray(newPage);
|
1002
|
+
}
|
1003
|
+
hasNextPage() {
|
1004
|
+
return __privateGet$6(this, _page).meta.page.more;
|
1005
|
+
}
|
1006
|
+
};
|
1007
|
+
let RecordArray = _RecordArray;
|
1008
|
+
_page = new WeakMap();
|
970
1009
|
|
971
1010
|
var __accessCheck$5 = (obj, member, msg) => {
|
972
1011
|
if (!member.has(obj))
|
@@ -988,18 +1027,19 @@ var __privateSet$4 = (obj, member, value, setter) => {
|
|
988
1027
|
};
|
989
1028
|
var _table$1, _repository, _data;
|
990
1029
|
const _Query = class {
|
991
|
-
constructor(repository, table, data,
|
1030
|
+
constructor(repository, table, data, rawParent) {
|
992
1031
|
__privateAdd$5(this, _table$1, void 0);
|
993
1032
|
__privateAdd$5(this, _repository, void 0);
|
994
1033
|
__privateAdd$5(this, _data, { filter: {} });
|
995
1034
|
this.meta = { page: { cursor: "start", more: true } };
|
996
|
-
this.records = [];
|
1035
|
+
this.records = new RecordArray(this, []);
|
997
1036
|
__privateSet$4(this, _table$1, table);
|
998
1037
|
if (repository) {
|
999
1038
|
__privateSet$4(this, _repository, repository);
|
1000
1039
|
} else {
|
1001
1040
|
__privateSet$4(this, _repository, this);
|
1002
1041
|
}
|
1042
|
+
const parent = cleanParent(data, rawParent);
|
1003
1043
|
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
1004
1044
|
__privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
|
1005
1045
|
__privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
|
@@ -1071,18 +1111,21 @@ const _Query = class {
|
|
1071
1111
|
}
|
1072
1112
|
async *getIterator(options = {}) {
|
1073
1113
|
const { batchSize = 1 } = options;
|
1074
|
-
let
|
1075
|
-
let
|
1076
|
-
|
1077
|
-
|
1078
|
-
|
1079
|
-
|
1080
|
-
|
1114
|
+
let page = await this.getPaginated({ ...options, pagination: { size: batchSize, offset: 0 } });
|
1115
|
+
let more = page.hasNextPage();
|
1116
|
+
yield page.records;
|
1117
|
+
while (more) {
|
1118
|
+
page = await page.nextPage();
|
1119
|
+
more = page.hasNextPage();
|
1120
|
+
yield page.records;
|
1081
1121
|
}
|
1082
1122
|
}
|
1083
1123
|
async getMany(options = {}) {
|
1084
|
-
const
|
1085
|
-
|
1124
|
+
const page = await this.getPaginated(options);
|
1125
|
+
if (page.hasNextPage() && options.pagination?.size === void 0) {
|
1126
|
+
console.trace("Calling getMany does not return all results. Paginate to get all results or call getAll.");
|
1127
|
+
}
|
1128
|
+
return page.records;
|
1086
1129
|
}
|
1087
1130
|
async getAll(options = {}) {
|
1088
1131
|
const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
|
@@ -1119,12 +1162,20 @@ let Query = _Query;
|
|
1119
1162
|
_table$1 = new WeakMap();
|
1120
1163
|
_repository = new WeakMap();
|
1121
1164
|
_data = new WeakMap();
|
1165
|
+
function cleanParent(data, parent) {
|
1166
|
+
if (isCursorPaginationOptions(data.pagination)) {
|
1167
|
+
return { ...parent, sorting: void 0, filter: void 0 };
|
1168
|
+
}
|
1169
|
+
return parent;
|
1170
|
+
}
|
1122
1171
|
|
1123
1172
|
function isIdentifiable(x) {
|
1124
1173
|
return isObject(x) && isString(x?.id);
|
1125
1174
|
}
|
1126
1175
|
function isXataRecord(x) {
|
1127
|
-
|
1176
|
+
const record = x;
|
1177
|
+
const metadata = record?.getMetadata();
|
1178
|
+
return isIdentifiable(x) && isObject(metadata) && typeof metadata.version === "number";
|
1128
1179
|
}
|
1129
1180
|
|
1130
1181
|
function isSortFilterString(value) {
|
@@ -1201,6 +1252,8 @@ class RestRepository extends Query {
|
|
1201
1252
|
}
|
1202
1253
|
async create(a, b) {
|
1203
1254
|
if (Array.isArray(a)) {
|
1255
|
+
if (a.length === 0)
|
1256
|
+
return [];
|
1204
1257
|
const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
|
1205
1258
|
await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
|
1206
1259
|
return records;
|
@@ -1226,27 +1279,36 @@ class RestRepository extends Query {
|
|
1226
1279
|
}
|
1227
1280
|
throw new Error("Invalid arguments for create method");
|
1228
1281
|
}
|
1229
|
-
async read(
|
1230
|
-
|
1231
|
-
|
1232
|
-
|
1233
|
-
|
1234
|
-
|
1235
|
-
|
1236
|
-
|
1237
|
-
|
1238
|
-
|
1239
|
-
const
|
1240
|
-
|
1241
|
-
|
1242
|
-
|
1243
|
-
|
1282
|
+
async read(a) {
|
1283
|
+
if (Array.isArray(a)) {
|
1284
|
+
if (a.length === 0)
|
1285
|
+
return [];
|
1286
|
+
return this.getAll({ filter: { id: { $any: a } } });
|
1287
|
+
}
|
1288
|
+
if (isString(a)) {
|
1289
|
+
const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, a);
|
1290
|
+
if (cacheRecord)
|
1291
|
+
return cacheRecord;
|
1292
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1293
|
+
try {
|
1294
|
+
const response = await getRecord({
|
1295
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: a },
|
1296
|
+
...fetchProps
|
1297
|
+
});
|
1298
|
+
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1299
|
+
return initObject(this.db, schema, __privateGet$4(this, _table), response);
|
1300
|
+
} catch (e) {
|
1301
|
+
if (isObject(e) && e.status === 404) {
|
1302
|
+
return null;
|
1303
|
+
}
|
1304
|
+
throw e;
|
1244
1305
|
}
|
1245
|
-
throw e;
|
1246
1306
|
}
|
1247
1307
|
}
|
1248
1308
|
async update(a, b) {
|
1249
1309
|
if (Array.isArray(a)) {
|
1310
|
+
if (a.length === 0)
|
1311
|
+
return [];
|
1250
1312
|
if (a.length > 100) {
|
1251
1313
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1252
1314
|
}
|
@@ -1268,6 +1330,8 @@ class RestRepository extends Query {
|
|
1268
1330
|
}
|
1269
1331
|
async createOrUpdate(a, b) {
|
1270
1332
|
if (Array.isArray(a)) {
|
1333
|
+
if (a.length === 0)
|
1334
|
+
return [];
|
1271
1335
|
if (a.length > 100) {
|
1272
1336
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1273
1337
|
}
|
@@ -1289,6 +1353,8 @@ class RestRepository extends Query {
|
|
1289
1353
|
}
|
1290
1354
|
async delete(a) {
|
1291
1355
|
if (Array.isArray(a)) {
|
1356
|
+
if (a.length === 0)
|
1357
|
+
return;
|
1292
1358
|
if (a.length > 100) {
|
1293
1359
|
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
1294
1360
|
}
|
@@ -1314,6 +1380,7 @@ class RestRepository extends Query {
|
|
1314
1380
|
body: {
|
1315
1381
|
query,
|
1316
1382
|
fuzziness: options.fuzziness,
|
1383
|
+
highlight: options.highlight,
|
1317
1384
|
filter: options.filter
|
1318
1385
|
},
|
1319
1386
|
...fetchProps
|
@@ -1498,7 +1565,8 @@ const transformObjectLinks = (object) => {
|
|
1498
1565
|
};
|
1499
1566
|
const initObject = (db, schema, table, object) => {
|
1500
1567
|
const result = {};
|
1501
|
-
|
1568
|
+
const { xata, ...rest } = object ?? {};
|
1569
|
+
Object.assign(result, rest);
|
1502
1570
|
const { columns } = schema.tables.find(({ name }) => name === table) ?? {};
|
1503
1571
|
if (!columns)
|
1504
1572
|
console.error(`Table ${table} not found in schema`);
|
@@ -1506,10 +1574,10 @@ const initObject = (db, schema, table, object) => {
|
|
1506
1574
|
const value = result[column.name];
|
1507
1575
|
switch (column.type) {
|
1508
1576
|
case "datetime": {
|
1509
|
-
const date = new Date(value);
|
1510
|
-
if (isNaN(date.getTime())) {
|
1577
|
+
const date = value !== void 0 ? new Date(value) : void 0;
|
1578
|
+
if (date && isNaN(date.getTime())) {
|
1511
1579
|
console.error(`Failed to parse date ${value} for field ${column.name}`);
|
1512
|
-
} else {
|
1580
|
+
} else if (date) {
|
1513
1581
|
result[column.name] = date;
|
1514
1582
|
}
|
1515
1583
|
break;
|
@@ -1534,7 +1602,10 @@ const initObject = (db, schema, table, object) => {
|
|
1534
1602
|
result.delete = function() {
|
1535
1603
|
return db[table].delete(result["id"]);
|
1536
1604
|
};
|
1537
|
-
|
1605
|
+
result.getMetadata = function() {
|
1606
|
+
return xata;
|
1607
|
+
};
|
1608
|
+
for (const prop of ["read", "update", "delete", "getMetadata"]) {
|
1538
1609
|
Object.defineProperty(result, prop, { enumerable: false });
|
1539
1610
|
}
|
1540
1611
|
Object.freeze(result);
|
@@ -1716,10 +1787,10 @@ _schema = new WeakMap();
|
|
1716
1787
|
_search = new WeakSet();
|
1717
1788
|
search_fn = async function(query, options, getFetchProps) {
|
1718
1789
|
const fetchProps = await getFetchProps();
|
1719
|
-
const { tables, fuzziness } = options ?? {};
|
1790
|
+
const { tables, fuzziness, highlight } = options ?? {};
|
1720
1791
|
const { records } = await searchBranch({
|
1721
1792
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1722
|
-
body: { tables, query, fuzziness },
|
1793
|
+
body: { tables, query, fuzziness, highlight },
|
1723
1794
|
...fetchProps
|
1724
1795
|
});
|
1725
1796
|
return records;
|
@@ -1945,6 +2016,7 @@ exports.PAGINATION_MAX_OFFSET = PAGINATION_MAX_OFFSET;
|
|
1945
2016
|
exports.PAGINATION_MAX_SIZE = PAGINATION_MAX_SIZE;
|
1946
2017
|
exports.Page = Page;
|
1947
2018
|
exports.Query = Query;
|
2019
|
+
exports.RecordArray = RecordArray;
|
1948
2020
|
exports.Repository = Repository;
|
1949
2021
|
exports.RestRepository = RestRepository;
|
1950
2022
|
exports.SchemaPlugin = SchemaPlugin;
|
@@ -2009,6 +2081,7 @@ exports.insertRecord = insertRecord;
|
|
2009
2081
|
exports.insertRecordWithID = insertRecordWithID;
|
2010
2082
|
exports.inviteWorkspaceMember = inviteWorkspaceMember;
|
2011
2083
|
exports.is = is;
|
2084
|
+
exports.isCursorPaginationOptions = isCursorPaginationOptions;
|
2012
2085
|
exports.isIdentifiable = isIdentifiable;
|
2013
2086
|
exports.isNot = isNot;
|
2014
2087
|
exports.isXataRecord = isXataRecord;
|