@xata.io/client 0.12.0 → 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 CHANGED
@@ -1,5 +1,13 @@
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
+
3
11
  ## 0.12.0
4
12
 
5
13
  ### Minor Changes
package/README.md CHANGED
@@ -123,24 +123,24 @@ const user = await xata.db.users.read('rec_1234abcdef');
123
123
 
124
124
  ```ts
125
125
  // Query objects selecting all fields.
126
- const users = await xata.db.users.select().getMany();
126
+ const page = await xata.db.users.select().getPaginated();
127
127
  const user = await xata.db.users.select().getFirst();
128
128
 
129
129
  // You can also use `xata.db.users` directly, since it's an immutable Query too!
130
- const users = await xata.db.users.getMany();
130
+ const page = await xata.db.users.getPaginated();
131
131
  const user = await xata.db.users.getFirst();
132
132
 
133
133
  // Query objects selecting just one or more fields
134
- const users = await xata.db.users.select('email', 'profile').getMany();
134
+ const page = await xata.db.users.select('email', 'profile').getPaginated();
135
135
 
136
136
  // Apply constraints
137
- const users = await xata.db.users.filter('email', 'foo@example.com').getMany();
137
+ const page = await xata.db.users.filter('email', 'foo@example.com').getPaginated();
138
138
 
139
139
  // Sorting
140
- const users = await xata.db.users.sort('fullName', 'asc').getMany();
140
+ const page = await xata.db.users.sort('full_name', 'asc').getPaginated();
141
141
  ```
142
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 `getAll()`, `getFirst()`, `getMany()` or `getPaginated()`.
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
144
 
145
145
  ```ts
146
146
  // Operators that combine multiple conditions can be deconstructed
@@ -154,16 +154,23 @@ filter('email', startsWith('username')).not(filter('created_at', gt(somePastDate
154
154
  // Queries are immutable objects. This is useful to derive queries from other queries
155
155
  const admins = filter('admin', true);
156
156
  const spaniardsAdmins = admins.filter('country', 'Spain');
157
- await admins.getMany(); // still returns all admins
157
+ await admins.getAll(); // still returns all admins
158
158
 
159
159
  // Finally fetch the results of the query
160
- const users = await query.getMany();
160
+ const users = await query.getAll();
161
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.
162
165
 
163
- // Also you can paginate the results
164
- const page = await query.getPaginated();
165
- const hasPage2 = page.hasNextPage();
166
- const page2 = await page.nextPage();
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
167
174
  ```
168
175
 
169
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:
@@ -208,23 +215,6 @@ await user.delete();
208
215
  await xata.db.users.delete('rec_1234abcdef');
209
216
  ```
210
217
 
211
- #### Deno support
212
-
213
- Right now we are still not publishing the client on deno.land or have support for deno in the codegen.
214
-
215
- However you can already use it with your preferred node CDN with the following import in the auto-generated `xata.ts` file:
216
-
217
- ```ts
218
- import {
219
- BaseClient,
220
- Query,
221
- Repository,
222
- RestRespositoryFactory,
223
- XataClientOptions,
224
- XataRecord
225
- } from 'https://esm.sh/@xata.io/client@<version>/dist/schema?target=deno';
226
- ```
227
-
228
218
  ### API Client
229
219
 
230
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...
@@ -256,3 +246,20 @@ const record = await client.records.getRecord(workspace, databaseName, 'branch',
256
246
 
257
247
  await client.workspaces.deleteWorkspace(workspace);
258
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
@@ -946,13 +946,13 @@ var __privateSet$5 = (obj, member, value, setter) => {
946
946
  setter ? setter.call(obj, value) : member.set(obj, value);
947
947
  return value;
948
948
  };
949
- var _query;
949
+ var _query, _page;
950
950
  class Page {
951
951
  constructor(query, meta, records = []) {
952
952
  __privateAdd$6(this, _query, void 0);
953
953
  __privateSet$5(this, _query, query);
954
954
  this.meta = meta;
955
- this.records = records;
955
+ this.records = new RecordArray(this, records);
956
956
  }
957
957
  async nextPage(size, offset) {
958
958
  return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
@@ -972,12 +972,40 @@ class Page {
972
972
  }
973
973
  _query = new WeakMap();
974
974
  const PAGINATION_MAX_SIZE = 200;
975
- const PAGINATION_DEFAULT_SIZE = 200;
975
+ const PAGINATION_DEFAULT_SIZE = 20;
976
976
  const PAGINATION_MAX_OFFSET = 800;
977
977
  const PAGINATION_DEFAULT_OFFSET = 0;
978
978
  function isCursorPaginationOptions(options) {
979
979
  return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
980
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();
981
1009
 
982
1010
  var __accessCheck$5 = (obj, member, msg) => {
983
1011
  if (!member.has(obj))
@@ -1004,7 +1032,7 @@ const _Query = class {
1004
1032
  __privateAdd$5(this, _repository, void 0);
1005
1033
  __privateAdd$5(this, _data, { filter: {} });
1006
1034
  this.meta = { page: { cursor: "start", more: true } };
1007
- this.records = [];
1035
+ this.records = new RecordArray(this, []);
1008
1036
  __privateSet$4(this, _table$1, table);
1009
1037
  if (repository) {
1010
1038
  __privateSet$4(this, _repository, repository);
@@ -1093,8 +1121,11 @@ const _Query = class {
1093
1121
  }
1094
1122
  }
1095
1123
  async getMany(options = {}) {
1096
- const { records } = await this.getPaginated(options);
1097
- return records;
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;
1098
1129
  }
1099
1130
  async getAll(options = {}) {
1100
1131
  const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
@@ -1985,6 +2016,7 @@ exports.PAGINATION_MAX_OFFSET = PAGINATION_MAX_OFFSET;
1985
2016
  exports.PAGINATION_MAX_SIZE = PAGINATION_MAX_SIZE;
1986
2017
  exports.Page = Page;
1987
2018
  exports.Query = Query;
2019
+ exports.RecordArray = RecordArray;
1988
2020
  exports.Repository = Repository;
1989
2021
  exports.RestRepository = RestRepository;
1990
2022
  exports.SchemaPlugin = SchemaPlugin;