@xata.io/client 0.0.0-beta.bbcb88d → 0.0.0-beta.c9e08d0

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/dist/index.d.ts CHANGED
@@ -54,15 +54,51 @@ export declare const includesAll: (value: string) => Constraint<string>;
54
54
  declare type FilterConstraints<T> = {
55
55
  [key in keyof T]?: T[key] extends Record<string, any> ? FilterConstraints<T[key]> : T[key] | DeepConstraint<T[key]>;
56
56
  };
57
+ declare type CursorNavigationOptions = {
58
+ first?: string;
59
+ } | {
60
+ last?: string;
61
+ } | {
62
+ after?: string;
63
+ before?: string;
64
+ };
65
+ declare type OffsetNavigationOptions = {
66
+ size?: number;
67
+ offset?: number;
68
+ };
69
+ declare type PaginationOptions = CursorNavigationOptions & OffsetNavigationOptions;
57
70
  declare type BulkQueryOptions<T> = {
58
- filter?: FilterConstraints<T>;
59
- sort?: {
60
- column: keyof T;
61
- direction?: SortDirection;
62
- } | keyof T;
71
+ page?: PaginationOptions;
63
72
  };
64
73
  declare type QueryOrConstraint<T, R> = Query<T, R> | Constraint<T>;
65
- export declare class Query<T, R = T> {
74
+ declare type QueryMeta = {
75
+ page: {
76
+ cursor: string;
77
+ more: boolean;
78
+ };
79
+ };
80
+ interface BasePage<T, R> {
81
+ query: Query<T, R>;
82
+ meta: QueryMeta;
83
+ records: R[];
84
+ nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
85
+ previousPage(size?: number, offset?: number): Promise<Page<T, R>>;
86
+ firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
87
+ lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
88
+ hasNextPage(): boolean;
89
+ }
90
+ declare class Page<T, R> implements BasePage<T, R> {
91
+ readonly query: Query<T, R>;
92
+ readonly meta: QueryMeta;
93
+ readonly records: R[];
94
+ constructor(query: Query<T, R>, meta: QueryMeta, records?: R[]);
95
+ nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
96
+ previousPage(size?: number, offset?: number): Promise<Page<T, R>>;
97
+ firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
98
+ lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
99
+ hasNextPage(): boolean;
100
+ }
101
+ export declare class Query<T, R = T> implements BasePage<T, R> {
66
102
  table: string;
67
103
  repository: Repository<T>;
68
104
  readonly $any?: QueryOrConstraint<T, R>[];
@@ -70,6 +106,9 @@ export declare class Query<T, R = T> {
70
106
  readonly $not?: QueryOrConstraint<T, R>[];
71
107
  readonly $none?: QueryOrConstraint<T, R>[];
72
108
  readonly $sort?: Record<string, SortDirection>;
109
+ readonly query: Query<T, R>;
110
+ readonly meta: QueryMeta;
111
+ readonly records: R[];
73
112
  constructor(repository: Repository<T> | null, table: string, data: Partial<Query<T, R>>, parent?: Query<T, R>);
74
113
  any(...queries: Query<T, R>[]): Query<T, R>;
75
114
  all(...queries: Query<T, R>[]): Query<T, R>;
@@ -78,30 +117,40 @@ export declare class Query<T, R = T> {
78
117
  filter(constraints: FilterConstraints<T>): Query<T, R>;
79
118
  filter<F extends keyof T>(column: F, value: FilterConstraints<T[F]> | DeepConstraint<T[F]>): Query<T, R>;
80
119
  sort<F extends keyof T>(column: F, direction: SortDirection): Query<T, R>;
120
+ getPaginated(options?: BulkQueryOptions<T>): Promise<Page<T, R>>;
121
+ [Symbol.asyncIterator](): AsyncIterableIterator<R>;
122
+ getIterator(chunk: number, options?: Omit<BulkQueryOptions<T>, 'page'>): AsyncGenerator<R[]>;
81
123
  getMany(options?: BulkQueryOptions<T>): Promise<R[]>;
82
- getOne(options?: BulkQueryOptions<T>): Promise<R | null>;
124
+ getOne(options?: Omit<BulkQueryOptions<T>, 'page'>): Promise<R | null>;
83
125
  deleteAll(): Promise<number>;
84
126
  include(columns: Include<T>): this;
127
+ nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
128
+ previousPage(size?: number, offset?: number): Promise<Page<T, R>>;
129
+ firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
130
+ lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
131
+ hasNextPage(): boolean;
85
132
  }
86
133
  export declare abstract class Repository<T> extends Query<T, Selectable<T>> {
87
134
  select<K extends keyof Selectable<T>>(...columns: K[]): Query<T, Select<T, K>>;
88
135
  abstract create(object: Selectable<T>): Promise<T>;
136
+ abstract createMany(objects: Selectable<T>[]): Promise<T[]>;
89
137
  abstract read(id: string): Promise<T | null>;
90
138
  abstract update(id: string, object: Partial<T>): Promise<T>;
91
139
  abstract delete(id: string): void;
92
- abstract query<R>(query: Query<T, R>): Promise<R[]>;
140
+ abstract _runQuery<R>(query: Query<T, R>, options?: BulkQueryOptions<T>): Promise<Page<T, R>>;
93
141
  }
94
142
  export declare class RestRepository<T> extends Repository<T> {
95
143
  client: BaseClient<any>;
96
144
  fetch: any;
97
145
  constructor(client: BaseClient<any>, table: string);
98
- request(method: string, path: string, body?: unknown): Promise<any>;
146
+ request<T>(method: string, path: string, body?: unknown): Promise<T | undefined>;
99
147
  select<K extends keyof T>(...columns: K[]): Query<T, Select<T, K>>;
100
148
  create(object: T): Promise<T>;
149
+ createMany(objects: T[]): Promise<T[]>;
101
150
  read(id: string): Promise<T | null>;
102
151
  update(id: string, object: Partial<T>): Promise<T>;
103
152
  delete(id: string): Promise<void>;
104
- query<R>(query: Query<T, R>): Promise<R[]>;
153
+ _runQuery<R>(query: Query<T, R>, options?: BulkQueryOptions<T>): Promise<Page<T, R>>;
105
154
  }
106
155
  interface RepositoryFactory {
107
156
  createRepository<T>(client: BaseClient<any>, table: string): Repository<T>;
@@ -115,7 +164,7 @@ declare type BranchStrategy = BranchStrategyValue | BranchStrategyBuilder;
115
164
  declare type BranchStrategyOption = NonNullable<BranchStrategy | BranchStrategy[]>;
116
165
  export declare type XataClientOptions = {
117
166
  fetch?: unknown;
118
- databaseURL: string;
167
+ databaseURL?: string;
119
168
  branch: BranchStrategyOption;
120
169
  apiKey: string;
121
170
  repositoryFactory?: RepositoryFactory;
package/dist/index.js CHANGED
@@ -15,6 +15,18 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
15
15
  function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
16
16
  function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
17
17
  };
18
+ var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
19
+ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
20
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
21
+ var g = generator.apply(thisArg, _arguments || []), i, q = [];
22
+ return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
23
+ function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
24
+ function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
25
+ function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
26
+ function fulfill(value) { resume("next", value); }
27
+ function reject(value) { resume("throw", value); }
28
+ function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
29
+ };
18
30
  Object.defineProperty(exports, "__esModule", { value: true });
19
31
  exports.XataError = exports.BaseClient = exports.RestRespositoryFactory = exports.RestRepository = exports.Repository = exports.Query = exports.includesAll = exports.includesPattern = exports.includesSubstring = exports.includes = exports.contains = exports.isNot = exports.is = exports.pattern = exports.endsWith = exports.startsWith = exports.notExists = exports.exists = exports.le = exports.lte = exports.lt = exports.gte = exports.ge = exports.gt = void 0;
20
32
  const errors_1 = require("./util/errors");
@@ -55,8 +67,43 @@ const includesPattern = (value) => ({ $includesPattern: value });
55
67
  exports.includesPattern = includesPattern;
56
68
  const includesAll = (value) => ({ $includesAll: value });
57
69
  exports.includesAll = includesAll;
70
+ class Page {
71
+ constructor(query, meta, records = []) {
72
+ this.query = query;
73
+ this.meta = meta;
74
+ this.records = records;
75
+ }
76
+ nextPage(size, offset) {
77
+ return __awaiter(this, void 0, void 0, function* () {
78
+ return this.query.getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
79
+ });
80
+ }
81
+ previousPage(size, offset) {
82
+ return __awaiter(this, void 0, void 0, function* () {
83
+ return this.query.getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
84
+ });
85
+ }
86
+ firstPage(size, offset) {
87
+ return __awaiter(this, void 0, void 0, function* () {
88
+ return this.query.getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
89
+ });
90
+ }
91
+ lastPage(size, offset) {
92
+ return __awaiter(this, void 0, void 0, function* () {
93
+ return this.query.getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
94
+ });
95
+ }
96
+ // TODO: We need to add something on the backend if we want a hasPreviousPage
97
+ hasNextPage() {
98
+ return this.meta.page.more;
99
+ }
100
+ }
58
101
  class Query {
59
102
  constructor(repository, table, data, parent) {
103
+ // Cursor pagination
104
+ this.query = this;
105
+ this.meta = { page: { cursor: 'start', more: true } };
106
+ this.records = [];
60
107
  if (repository) {
61
108
  this.repository = repository;
62
109
  }
@@ -129,24 +176,56 @@ class Query {
129
176
  }, this);
130
177
  return q;
131
178
  }
132
- // TODO: pagination. Maybe implement different methods for different type of paginations
133
- // and one to simply get the first records returned by the query with no pagination.
179
+ getPaginated(options) {
180
+ return __awaiter(this, void 0, void 0, function* () {
181
+ return this.repository._runQuery(this, options);
182
+ });
183
+ }
184
+ [Symbol.asyncIterator]() {
185
+ return __asyncGenerator(this, arguments, function* _a() {
186
+ var e_1, _b;
187
+ try {
188
+ for (var _c = __asyncValues(this.getIterator(1)), _d; _d = yield __await(_c.next()), !_d.done;) {
189
+ const [record] = _d.value;
190
+ yield yield __await(record);
191
+ }
192
+ }
193
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
194
+ finally {
195
+ try {
196
+ if (_d && !_d.done && (_b = _c.return)) yield __await(_b.call(_c));
197
+ }
198
+ finally { if (e_1) throw e_1.error; }
199
+ }
200
+ });
201
+ }
202
+ getIterator(chunk, options = {}) {
203
+ return __asyncGenerator(this, arguments, function* getIterator_1() {
204
+ let offset = 0;
205
+ let end = false;
206
+ while (!end) {
207
+ const { records, meta } = yield __await(this.getPaginated(Object.assign(Object.assign({}, options), { page: { size: chunk, offset } })));
208
+ yield yield __await(records);
209
+ offset += chunk;
210
+ end = !meta.page.more;
211
+ }
212
+ });
213
+ }
134
214
  getMany(options) {
135
215
  return __awaiter(this, void 0, void 0, function* () {
136
- // TODO: use options
137
- return this.repository.query(this);
216
+ const { records } = yield this.getPaginated(options);
217
+ return records;
138
218
  });
139
219
  }
140
- getOne(options) {
220
+ getOne(options = {}) {
141
221
  return __awaiter(this, void 0, void 0, function* () {
142
- // TODO: use options
143
- const arr = yield this.getMany(); // TODO, limit to 1
144
- return arr[0] || null;
222
+ const records = yield this.getMany(Object.assign(Object.assign({}, options), { page: { size: 1 } }));
223
+ return records[0] || null;
145
224
  });
146
225
  }
147
226
  deleteAll() {
148
227
  return __awaiter(this, void 0, void 0, function* () {
149
- // Return number of affected rows
228
+ // TODO: Return number of affected rows
150
229
  return 0;
151
230
  });
152
231
  }
@@ -154,6 +233,29 @@ class Query {
154
233
  // TODO
155
234
  return this;
156
235
  }
236
+ nextPage(size, offset) {
237
+ return __awaiter(this, void 0, void 0, function* () {
238
+ return this.firstPage(size, offset);
239
+ });
240
+ }
241
+ previousPage(size, offset) {
242
+ return __awaiter(this, void 0, void 0, function* () {
243
+ return this.firstPage(size, offset);
244
+ });
245
+ }
246
+ firstPage(size, offset) {
247
+ return __awaiter(this, void 0, void 0, function* () {
248
+ return this.getPaginated({ page: { size, offset } });
249
+ });
250
+ }
251
+ lastPage(size, offset) {
252
+ return __awaiter(this, void 0, void 0, function* () {
253
+ return this.getPaginated({ page: { size, offset, before: 'end' } });
254
+ });
255
+ }
256
+ hasNextPage() {
257
+ return this.meta.page.more;
258
+ }
157
259
  }
158
260
  exports.Query = Query;
159
261
  class Repository extends Query {
@@ -211,7 +313,7 @@ class RestRepository extends Repository {
211
313
  throw new XataError(resp.statusText, resp.status);
212
314
  }
213
315
  if (resp.status === 204)
214
- return;
316
+ return undefined;
215
317
  return resp.json();
216
318
  });
217
319
  }
@@ -220,22 +322,40 @@ class RestRepository extends Repository {
220
322
  }
221
323
  create(object) {
222
324
  return __awaiter(this, void 0, void 0, function* () {
223
- const body = Object.assign({}, object);
224
- for (const key of Object.keys(body)) {
225
- const value = body[key];
226
- if (value && typeof value === 'object' && typeof value.id === 'string') {
227
- body[key] = value.id;
228
- }
325
+ const record = transformObjectLinks(object);
326
+ const response = yield this.request('POST', `/tables/${this.table}/data`, record);
327
+ if (!response) {
328
+ throw new Error("The server didn't return any data for the query");
329
+ }
330
+ const finalObject = yield this.read(response.id);
331
+ if (!finalObject) {
332
+ throw new Error('The server failed to save the record');
333
+ }
334
+ return finalObject;
335
+ });
336
+ }
337
+ createMany(objects) {
338
+ return __awaiter(this, void 0, void 0, function* () {
339
+ const records = objects.map((object) => transformObjectLinks(object));
340
+ const response = yield this.request('POST', `/tables/${this.table}/bulk`, { records });
341
+ if (!response) {
342
+ throw new Error("The server didn't return any data for the query");
343
+ }
344
+ // TODO: Use filer.$any() to get all the records
345
+ const finalObjects = yield Promise.all(response.recordIDs.map((id) => this.read(id)));
346
+ if (finalObjects.some((object) => !object)) {
347
+ throw new Error('The server failed to save the record');
229
348
  }
230
- const obj = yield this.request('POST', `/tables/${this.table}/data`, body);
231
- return this.client.initObject(this.table, obj);
349
+ return finalObjects;
232
350
  });
233
351
  }
234
352
  read(id) {
235
353
  return __awaiter(this, void 0, void 0, function* () {
236
354
  try {
237
- const obj = yield this.request('GET', `/tables/${this.table}/data/${id}`);
238
- return this.client.initObject(this.table, obj);
355
+ const response = yield this.request('GET', `/tables/${this.table}/data/${id}`);
356
+ if (!response)
357
+ return null;
358
+ return this.client.initObject(this.table, response);
239
359
  }
240
360
  catch (err) {
241
361
  if (err.status === 404)
@@ -246,8 +366,12 @@ class RestRepository extends Repository {
246
366
  }
247
367
  update(id, object) {
248
368
  return __awaiter(this, void 0, void 0, function* () {
249
- const obj = yield this.request('PUT', `/tables/${this.table}/data/${id}`, object);
250
- return this.client.initObject(this.table, obj);
369
+ const response = yield this.request('PUT', `/tables/${this.table}/data/${id}`, object);
370
+ if (!response) {
371
+ throw new Error("The server didn't return any data for the query");
372
+ }
373
+ // TODO: Review this, not sure we are properly initializing the object
374
+ return this.client.initObject(this.table, response);
251
375
  });
252
376
  }
253
377
  delete(id) {
@@ -255,7 +379,7 @@ class RestRepository extends Repository {
255
379
  yield this.request('DELETE', `/tables/${this.table}/data/${id}`);
256
380
  });
257
381
  }
258
- query(query) {
382
+ _runQuery(query, options) {
259
383
  return __awaiter(this, void 0, void 0, function* () {
260
384
  const filter = {
261
385
  $any: query.$any,
@@ -265,10 +389,16 @@ class RestRepository extends Repository {
265
389
  };
266
390
  const body = {
267
391
  filter: Object.values(filter).some(Boolean) ? filter : undefined,
268
- sort: query.$sort
392
+ sort: query.$sort,
393
+ page: options === null || options === void 0 ? void 0 : options.page
269
394
  };
270
- const result = yield this.request('POST', `/tables/${this.table}/query`, body);
271
- return result.records.map((record) => this.client.initObject(this.table, record));
395
+ const response = yield this.request('POST', `/tables/${this.table}/query`, body);
396
+ if (!response) {
397
+ throw new Error("The server didn't return any data for the query");
398
+ }
399
+ const { meta, records: objects } = response;
400
+ const records = objects.map((record) => this.client.initObject(this.table, record));
401
+ return new Page(query, meta, records);
272
402
  });
273
403
  }
274
404
  }
@@ -327,7 +457,7 @@ class BaseClient {
327
457
  return o;
328
458
  }
329
459
  getBranch() {
330
- var e_1, _a;
460
+ var e_2, _a;
331
461
  return __awaiter(this, void 0, void 0, function* () {
332
462
  if (this.branch)
333
463
  return this.branch;
@@ -346,12 +476,12 @@ class BaseClient {
346
476
  }
347
477
  }
348
478
  }
349
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
479
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
350
480
  finally {
351
481
  try {
352
482
  if (strategies_1_1 && !strategies_1_1.done && (_a = strategies_1.return)) yield _a.call(strategies_1);
353
483
  }
354
- finally { if (e_1) throw e_1.error; }
484
+ finally { if (e_2) throw e_2.error; }
355
485
  }
356
486
  throw new Error('Unable to resolve branch value');
357
487
  });
@@ -368,3 +498,12 @@ exports.XataError = XataError;
368
498
  const isBranchStrategyBuilder = (strategy) => {
369
499
  return typeof strategy === 'function';
370
500
  };
501
+ // TODO: We can find a better implementation for links
502
+ const transformObjectLinks = (object) => {
503
+ return Object.entries(object).reduce((acc, [key, value]) => {
504
+ if (value && typeof value === 'object' && typeof value.id === 'string') {
505
+ return Object.assign(Object.assign({}, acc), { [key]: value.id });
506
+ }
507
+ return Object.assign(Object.assign({}, acc), { [key]: value });
508
+ }, {});
509
+ };
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@xata.io/client",
3
- "version": "0.0.0-beta.bbcb88d",
3
+ "version": "0.0.0-beta.c9e08d0",
4
4
  "description": "Xata.io SDK for TypeScript and JavaScript",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "scripts": {
8
8
  "test": "echo \"Error: no test specified\" && exit 1",
9
- "build": "tsc",
9
+ "build": "tsc -p tsconfig.build.json",
10
10
  "prepack": "npm run build"
11
11
  },
12
12
  "repository": {
@@ -20,5 +20,5 @@
20
20
  "url": "https://github.com/xataio/client-ts/issues"
21
21
  },
22
22
  "homepage": "https://github.com/xataio/client-ts/blob/main/client/README.md",
23
- "gitHead": "bbcb88d6cb878b39c43ee6c564496f362796baa9"
23
+ "gitHead": "c9e08d07e1a7e7b07d118b2b459b9843edb341c3"
24
24
  }
@@ -1 +0,0 @@
1
- export {};