@xata.io/client 0.0.0-beta.f12621e → 0.0.0-beta.f46df88

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.
Files changed (44) hide show
  1. package/.eslintrc.cjs +13 -0
  2. package/CHANGELOG.md +49 -0
  3. package/dist/api/client.d.ts +95 -0
  4. package/dist/api/client.js +236 -0
  5. package/dist/api/components.d.ts +1436 -0
  6. package/dist/api/components.js +997 -0
  7. package/dist/api/fetcher.d.ts +25 -0
  8. package/dist/api/fetcher.js +73 -0
  9. package/dist/api/index.d.ts +7 -0
  10. package/dist/api/index.js +21 -0
  11. package/dist/api/parameters.d.ts +16 -0
  12. package/dist/api/parameters.js +2 -0
  13. package/dist/api/providers.d.ts +8 -0
  14. package/dist/api/providers.js +30 -0
  15. package/dist/api/responses.d.ts +50 -0
  16. package/dist/api/responses.js +2 -0
  17. package/dist/api/schemas.d.ts +311 -0
  18. package/dist/api/schemas.js +2 -0
  19. package/dist/index.d.ts +1 -61
  20. package/dist/index.js +7 -268
  21. package/dist/schema/filters.d.ts +7 -5
  22. package/dist/schema/filters.js +2 -1
  23. package/dist/schema/index.d.ts +6 -0
  24. package/dist/schema/index.js +17 -1
  25. package/dist/schema/operators.d.ts +51 -0
  26. package/dist/schema/operators.js +51 -0
  27. package/dist/schema/pagination.d.ts +57 -16
  28. package/dist/schema/pagination.js +56 -7
  29. package/dist/schema/query.d.ts +112 -37
  30. package/dist/schema/query.js +125 -71
  31. package/dist/schema/record.d.ts +66 -0
  32. package/dist/schema/record.js +13 -0
  33. package/dist/schema/repository.d.ts +100 -0
  34. package/dist/schema/repository.js +288 -0
  35. package/dist/schema/selection.d.ts +23 -17
  36. package/dist/schema/selection.spec.d.ts +1 -0
  37. package/dist/schema/selection.spec.js +109 -0
  38. package/dist/util/lang.d.ts +5 -0
  39. package/dist/util/lang.js +22 -0
  40. package/dist/util/types.d.ts +11 -1
  41. package/package.json +2 -2
  42. package/tsconfig.json +21 -0
  43. package/dist/util/errors.d.ts +0 -3
  44. package/dist/util/errors.js +0 -9
@@ -8,37 +8,86 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
12
+ if (kind === "m") throw new TypeError("Private method is not writable");
13
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
14
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
15
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
16
+ };
17
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
18
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
19
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
20
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
21
+ };
22
+ var _Page_query;
11
23
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.Page = void 0;
24
+ exports.PAGINATION_DEFAULT_OFFSET = exports.PAGINATION_MAX_OFFSET = exports.PAGINATION_DEFAULT_SIZE = exports.PAGINATION_MAX_SIZE = exports.Page = void 0;
25
+ /**
26
+ * A Page contains a set of results from a query plus metadata about the retrieved
27
+ * set of values such as the cursor, required to retrieve additional records.
28
+ */
13
29
  class Page {
14
30
  constructor(query, meta, records = []) {
15
- this.query = query;
31
+ _Page_query.set(this, void 0);
32
+ __classPrivateFieldSet(this, _Page_query, query, "f");
16
33
  this.meta = meta;
17
34
  this.records = records;
18
35
  }
36
+ /**
37
+ * Retrieves the next page of results.
38
+ * @param size Maximum number of results to be retrieved.
39
+ * @param offset Number of results to skip when retrieving the results.
40
+ * @returns The next page or results.
41
+ */
19
42
  nextPage(size, offset) {
20
43
  return __awaiter(this, void 0, void 0, function* () {
21
- return this.query.getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
44
+ return __classPrivateFieldGet(this, _Page_query, "f").getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
22
45
  });
23
46
  }
47
+ /**
48
+ * Retrieves the previous page of results.
49
+ * @param size Maximum number of results to be retrieved.
50
+ * @param offset Number of results to skip when retrieving the results.
51
+ * @returns The previous page or results.
52
+ */
24
53
  previousPage(size, offset) {
25
54
  return __awaiter(this, void 0, void 0, function* () {
26
- return this.query.getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
55
+ return __classPrivateFieldGet(this, _Page_query, "f").getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
27
56
  });
28
57
  }
58
+ /**
59
+ * Retrieves the first page of results.
60
+ * @param size Maximum number of results to be retrieved.
61
+ * @param offset Number of results to skip when retrieving the results.
62
+ * @returns The first page or results.
63
+ */
29
64
  firstPage(size, offset) {
30
65
  return __awaiter(this, void 0, void 0, function* () {
31
- return this.query.getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
66
+ return __classPrivateFieldGet(this, _Page_query, "f").getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
32
67
  });
33
68
  }
69
+ /**
70
+ * Retrieves the last page of results.
71
+ * @param size Maximum number of results to be retrieved.
72
+ * @param offset Number of results to skip when retrieving the results.
73
+ * @returns The last page or results.
74
+ */
34
75
  lastPage(size, offset) {
35
76
  return __awaiter(this, void 0, void 0, function* () {
36
- return this.query.getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
77
+ return __classPrivateFieldGet(this, _Page_query, "f").getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
37
78
  });
38
79
  }
39
- // TODO: We need to add something on the backend if we want a hasPreviousPage
80
+ /**
81
+ * Shortcut method to check if there will be additional results if the next page of results is retrieved.
82
+ * @returns Whether or not there will be additional results in the next page of results.
83
+ */
40
84
  hasNextPage() {
41
85
  return this.meta.page.more;
42
86
  }
43
87
  }
44
88
  exports.Page = Page;
89
+ _Page_query = new WeakMap();
90
+ exports.PAGINATION_MAX_SIZE = 200;
91
+ exports.PAGINATION_DEFAULT_SIZE = 200;
92
+ exports.PAGINATION_MAX_OFFSET = 800;
93
+ exports.PAGINATION_DEFAULT_OFFSET = 0;
@@ -1,43 +1,118 @@
1
- import { XataRecord, Repository } from '..';
2
- import { Constraint, DeepConstraint, FilterConstraints, SortDirection, SortFilter } from './filters';
3
- import { PaginationOptions, BasePage, PaginationQueryMeta, Page } from './pagination';
4
- import { Selectable, SelectableColumn, Select } from './selection';
5
- export declare type QueryOptions<T> = {
1
+ import { FilterExpression } from '../api/schemas';
2
+ import { NonEmptyArray, RequiredBy } from '../util/types';
3
+ import { DeepConstraint, FilterConstraints, SortDirection, SortFilter } from './filters';
4
+ import { Page, Paginable, PaginationOptions, PaginationQueryMeta } from './pagination';
5
+ import { XataRecord } from './record';
6
+ import { Repository } from './repository';
7
+ import { SelectableColumn, SelectedPick, ValueAtColumn } from './selection';
8
+ export declare type QueryOptions<T extends XataRecord> = {
6
9
  page?: PaginationOptions;
7
- columns?: Array<keyof Selectable<T>>;
10
+ columns?: NonEmptyArray<SelectableColumn<T>>;
11
+ filter?: FilterExpression;
8
12
  sort?: SortFilter<T> | SortFilter<T>[];
9
13
  };
10
- declare type QueryOrConstraint<T extends XataRecord, R extends XataRecord> = Query<T, R> | Constraint<T>;
11
- export declare class Query<T extends XataRecord, R extends XataRecord = T> implements BasePage<T, R> {
12
- table: string;
13
- repository: Repository<T>;
14
- readonly $any?: QueryOrConstraint<T, R>[];
15
- readonly $all?: QueryOrConstraint<T, R>[];
16
- readonly $not?: QueryOrConstraint<T, R>[];
17
- readonly $none?: QueryOrConstraint<T, R>[];
18
- readonly $sort?: Record<string, SortDirection>;
19
- readonly columns: SelectableColumn<T>[];
20
- readonly query: Query<T, R>;
14
+ /**
15
+ * Query objects contain the information of all filters, sorting, etc. to be included in the database query.
16
+ *
17
+ * Query objects are immutable. Any method that adds more constraints or options to the query will return
18
+ * a new Query object containing the both the previous and the new constraints and options.
19
+ */
20
+ export declare class Query<Record extends XataRecord, Result extends XataRecord = Record> implements Paginable<Record, Result> {
21
+ #private;
21
22
  readonly meta: PaginationQueryMeta;
22
- readonly records: R[];
23
- constructor(repository: Repository<T> | null, table: string, data: Partial<Query<T, R>>, parent?: Query<T, R>);
24
- any(...queries: Query<T, R>[]): Query<T, R>;
25
- all(...queries: Query<T, R>[]): Query<T, R>;
26
- not(...queries: Query<T, R>[]): Query<T, R>;
27
- none(...queries: Query<T, R>[]): Query<T, R>;
28
- filter(constraints: FilterConstraints<T>): Query<T, R>;
29
- filter<F extends keyof T>(column: F, value: FilterConstraints<T[F]> | DeepConstraint<T[F]>): Query<T, R>;
30
- sort<F extends keyof T>(column: F, direction: SortDirection): Query<T, R>;
31
- getPaginated<Options extends QueryOptions<T>>(options?: Options): Promise<Page<T, typeof options['columns'] extends SelectableColumn<T>[] ? Select<T, typeof options['columns'][number]> : R>>;
32
- [Symbol.asyncIterator](): AsyncIterableIterator<R>;
33
- getIterator(chunk: number, options?: Omit<QueryOptions<T>, 'page'>): AsyncGenerator<R[]>;
34
- getMany<Options extends QueryOptions<T>>(options?: Options): Promise<(typeof options['columns'] extends SelectableColumn<T>[] ? Select<T, typeof options['columns'][number]> : R)[]>;
35
- getOne<Options extends Omit<QueryOptions<T>, 'page'>>(options?: Options): Promise<(typeof options['columns'] extends SelectableColumn<T>[] ? Select<T, typeof options['columns'][number]> : R) | null>;
36
- deleteAll(): Promise<number>;
37
- nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
38
- previousPage(size?: number, offset?: number): Promise<Page<T, R>>;
39
- firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
40
- lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
23
+ readonly records: Result[];
24
+ constructor(repository: Repository<Record> | null, table: string, data: Partial<QueryOptions<Record>>, parent?: Partial<QueryOptions<Record>>);
25
+ getQueryOptions(): QueryOptions<Record>;
26
+ /**
27
+ * Builds a new query object representing a logical OR between the given subqueries.
28
+ * @param queries An array of subqueries.
29
+ * @returns A new Query object.
30
+ */
31
+ any(...queries: Query<Record, Result>[]): Query<Record, Result>;
32
+ /**
33
+ * Builds a new query object representing a logical AND between the given subqueries.
34
+ * @param queries An array of subqueries.
35
+ * @returns A new Query object.
36
+ */
37
+ all(...queries: Query<Record, Result>[]): Query<Record, Result>;
38
+ /**
39
+ * Builds a new query object representing a logical OR negating each subquery. In pseudo-code: !q1 OR !q2
40
+ * @param queries An array of subqueries.
41
+ * @returns A new Query object.
42
+ */
43
+ not(...queries: Query<Record, Result>[]): Query<Record, Result>;
44
+ /**
45
+ * Builds a new query object representing a logical AND negating each subquery. In pseudo-code: !q1 AND !q2
46
+ * @param queries An array of subqueries.
47
+ * @returns A new Query object.
48
+ */
49
+ none(...queries: Query<Record, Result>[]): Query<Record, Result>;
50
+ /**
51
+ * Builds a new query object adding one or more constraints. Examples:
52
+ *
53
+ * ```
54
+ * query.filter("columnName", columnValue)
55
+ * query.filter({
56
+ * "columnName": columnValue
57
+ * })
58
+ * query.filter({
59
+ * "columnName": operator(columnValue) // Use gt, gte, lt, lte, startsWith,...
60
+ * })
61
+ * ```
62
+ *
63
+ * @param constraints
64
+ * @returns A new Query object.
65
+ */
66
+ filter(constraints: FilterConstraints<Record>): Query<Record, Result>;
67
+ filter<F extends SelectableColumn<Record>>(column: F, value: FilterConstraints<ValueAtColumn<Record, F>> | DeepConstraint<ValueAtColumn<Record, F>>): Query<Record, Result>;
68
+ /**
69
+ * Builds a new query with a new sort option.
70
+ * @param column The column name.
71
+ * @param direction The direction. Either ascending or descending.
72
+ * @returns A new Query object.
73
+ */
74
+ sort<F extends SelectableColumn<Record>>(column: F, direction: SortDirection): Query<Record, Result>;
75
+ /**
76
+ * Builds a new query specifying the set of columns to be returned in the query response.
77
+ * @param columns Array of column names to be returned by the query.
78
+ * @returns A new Query object.
79
+ */
80
+ select<K extends SelectableColumn<Record>>(columns: NonEmptyArray<K>): Query<Record, SelectedPick<Record, NonEmptyArray<K>>>;
81
+ getPaginated(): Promise<Page<Record, Result>>;
82
+ getPaginated(options: Omit<QueryOptions<Record>, 'columns'>): Promise<Page<Record, Result>>;
83
+ getPaginated<Options extends RequiredBy<QueryOptions<Record>, 'columns'>>(options: Options): Promise<Page<Record, SelectedPick<Record, typeof options['columns']>>>;
84
+ [Symbol.asyncIterator](): AsyncIterableIterator<Result>;
85
+ getIterator(chunk: number): AsyncGenerator<Result[]>;
86
+ getIterator(chunk: number, options: Omit<QueryOptions<Record>, 'columns' | 'page'>): AsyncGenerator<Result[]>;
87
+ getIterator<Options extends RequiredBy<Omit<QueryOptions<Record>, 'page'>, 'columns'>>(chunk: number, options: Options): AsyncGenerator<SelectedPick<Record, typeof options['columns']>[]>;
88
+ /**
89
+ * Performs the query in the database and returns a set of results.
90
+ * @param options Additional options to be used when performing the query.
91
+ * @returns An array of records from the database.
92
+ */
93
+ getMany(): Promise<Result[]>;
94
+ getMany(options: Omit<QueryOptions<Record>, 'columns'>): Promise<Result[]>;
95
+ getMany<Options extends RequiredBy<QueryOptions<Record>, 'columns'>>(options: Options): Promise<SelectedPick<Record, typeof options['columns']>[]>;
96
+ /**
97
+ * Performs the query in the database and returns all the results.
98
+ * Warning: If there are a large number of results, this method can have performance implications.
99
+ * @param options Additional options to be used when performing the query.
100
+ * @returns An array of records from the database.
101
+ */
102
+ getAll(chunk?: number): Promise<Result[]>;
103
+ getAll(chunk: number | undefined, options: Omit<QueryOptions<Record>, 'columns' | 'page'>): Promise<Result[]>;
104
+ getAll<Options extends RequiredBy<Omit<QueryOptions<Record>, 'page'>, 'columns'>>(chunk: number | undefined, options: Options): Promise<SelectedPick<Record, typeof options['columns']>[]>;
105
+ /**
106
+ * Performs the query in the database and returns the first result.
107
+ * @param options Additional options to be used when performing the query.
108
+ * @returns The first record that matches the query, or null if no record matched the query.
109
+ */
110
+ getOne(): Promise<Result | null>;
111
+ getOne(options: Omit<QueryOptions<Record>, 'columns' | 'page'>): Promise<Result | null>;
112
+ getOne<Options extends RequiredBy<Omit<QueryOptions<Record>, 'page'>, 'columns'>>(options: Options): Promise<SelectedPick<Record, typeof options['columns']> | null>;
113
+ nextPage(size?: number, offset?: number): Promise<Page<Record, Result>>;
114
+ previousPage(size?: number, offset?: number): Promise<Page<Record, Result>>;
115
+ firstPage(size?: number, offset?: number): Promise<Page<Record, Result>>;
116
+ lastPage(size?: number, offset?: number): Promise<Page<Record, Result>>;
41
117
  hasNextPage(): boolean;
42
118
  }
43
- export {};
@@ -8,6 +8,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
12
+ if (kind === "m") throw new TypeError("Private method is not writable");
13
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
14
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
15
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
16
+ };
17
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
18
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
19
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
20
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
21
+ };
11
22
  var __asyncValues = (this && this.__asyncValues) || function (o) {
12
23
  if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
13
24
  var m = o[Symbol.asyncIterator], i;
@@ -27,32 +38,41 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
27
38
  function reject(value) { resume("throw", value); }
28
39
  function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
29
40
  };
41
+ var _Query_table, _Query_repository, _Query_data;
30
42
  Object.defineProperty(exports, "__esModule", { value: true });
31
43
  exports.Query = void 0;
44
+ const lang_1 = require("../util/lang");
45
+ const pagination_1 = require("./pagination");
46
+ /**
47
+ * Query objects contain the information of all filters, sorting, etc. to be included in the database query.
48
+ *
49
+ * Query objects are immutable. Any method that adds more constraints or options to the query will return
50
+ * a new Query object containing the both the previous and the new constraints and options.
51
+ */
32
52
  class Query {
33
53
  constructor(repository, table, data, parent) {
34
- this.columns = ['*'];
35
- // Cursor pagination
36
- this.query = this;
54
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
55
+ _Query_table.set(this, void 0);
56
+ _Query_repository.set(this, void 0);
57
+ _Query_data.set(this, { filter: {} });
58
+ // Implements pagination
37
59
  this.meta = { page: { cursor: 'start', more: true } };
38
60
  this.records = [];
61
+ __classPrivateFieldSet(this, _Query_table, table, "f");
39
62
  if (repository) {
40
- this.repository = repository;
63
+ __classPrivateFieldSet(this, _Query_repository, repository, "f");
41
64
  }
42
65
  else {
43
- this.repository = this;
66
+ __classPrivateFieldSet(this, _Query_repository, this, "f");
44
67
  }
45
- this.table = table;
46
- // For some reason Object.assign(this, parent) didn't work in this case
47
- // so doing all this manually:
48
- this.$any = parent === null || parent === void 0 ? void 0 : parent.$any;
49
- this.$all = parent === null || parent === void 0 ? void 0 : parent.$all;
50
- this.$not = parent === null || parent === void 0 ? void 0 : parent.$not;
51
- this.$none = parent === null || parent === void 0 ? void 0 : parent.$none;
52
- this.$sort = parent === null || parent === void 0 ? void 0 : parent.$sort;
53
- Object.assign(this, data);
54
- // These bindings are used to support deconstructing
55
- // const { any, not, filter, sort } = xata.users.query()
68
+ __classPrivateFieldGet(this, _Query_data, "f").filter = (_b = (_a = data.filter) !== null && _a !== void 0 ? _a : parent === null || parent === void 0 ? void 0 : parent.filter) !== null && _b !== void 0 ? _b : {};
69
+ __classPrivateFieldGet(this, _Query_data, "f").filter.$any = (_d = (_c = data.filter) === null || _c === void 0 ? void 0 : _c.$any) !== null && _d !== void 0 ? _d : (_e = parent === null || parent === void 0 ? void 0 : parent.filter) === null || _e === void 0 ? void 0 : _e.$any;
70
+ __classPrivateFieldGet(this, _Query_data, "f").filter.$all = (_g = (_f = data.filter) === null || _f === void 0 ? void 0 : _f.$all) !== null && _g !== void 0 ? _g : (_h = parent === null || parent === void 0 ? void 0 : parent.filter) === null || _h === void 0 ? void 0 : _h.$all;
71
+ __classPrivateFieldGet(this, _Query_data, "f").filter.$not = (_k = (_j = data.filter) === null || _j === void 0 ? void 0 : _j.$not) !== null && _k !== void 0 ? _k : (_l = parent === null || parent === void 0 ? void 0 : parent.filter) === null || _l === void 0 ? void 0 : _l.$not;
72
+ __classPrivateFieldGet(this, _Query_data, "f").filter.$none = (_o = (_m = data.filter) === null || _m === void 0 ? void 0 : _m.$none) !== null && _o !== void 0 ? _o : (_p = parent === null || parent === void 0 ? void 0 : parent.filter) === null || _p === void 0 ? void 0 : _p.$none;
73
+ __classPrivateFieldGet(this, _Query_data, "f").sort = (_q = data.sort) !== null && _q !== void 0 ? _q : parent === null || parent === void 0 ? void 0 : parent.sort;
74
+ __classPrivateFieldGet(this, _Query_data, "f").columns = (_s = (_r = data.columns) !== null && _r !== void 0 ? _r : parent === null || parent === void 0 ? void 0 : parent.columns) !== null && _s !== void 0 ? _s : ['*'];
75
+ __classPrivateFieldGet(this, _Query_data, "f").page = (_t = data.page) !== null && _t !== void 0 ? _t : parent === null || parent === void 0 ? void 0 : parent.page;
56
76
  this.any = this.any.bind(this);
57
77
  this.all = this.all.bind(this);
58
78
  this.not = this.not.bind(this);
@@ -62,58 +82,82 @@ class Query {
62
82
  Object.defineProperty(this, 'table', { enumerable: false });
63
83
  Object.defineProperty(this, 'repository', { enumerable: false });
64
84
  }
65
- any(...queries) {
66
- return new Query(this.repository, this.table, {
67
- $any: (this.$any || []).concat(queries)
68
- }, this);
85
+ getQueryOptions() {
86
+ return __classPrivateFieldGet(this, _Query_data, "f");
69
87
  }
88
+ /**
89
+ * Builds a new query object representing a logical OR between the given subqueries.
90
+ * @param queries An array of subqueries.
91
+ * @returns A new Query object.
92
+ */
93
+ any(...queries) {
94
+ const $any = queries.map((query) => { var _a; return (_a = query.getQueryOptions().filter) !== null && _a !== void 0 ? _a : {}; });
95
+ return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $any } }, __classPrivateFieldGet(this, _Query_data, "f"));
96
+ }
97
+ /**
98
+ * Builds a new query object representing a logical AND between the given subqueries.
99
+ * @param queries An array of subqueries.
100
+ * @returns A new Query object.
101
+ */
70
102
  all(...queries) {
71
- return new Query(this.repository, this.table, {
72
- $all: (this.$all || []).concat(queries)
73
- }, this);
74
- }
103
+ const $all = queries.map((query) => { var _a; return (_a = query.getQueryOptions().filter) !== null && _a !== void 0 ? _a : {}; });
104
+ return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f"));
105
+ }
106
+ /**
107
+ * Builds a new query object representing a logical OR negating each subquery. In pseudo-code: !q1 OR !q2
108
+ * @param queries An array of subqueries.
109
+ * @returns A new Query object.
110
+ */
75
111
  not(...queries) {
76
- return new Query(this.repository, this.table, {
77
- $not: (this.$not || []).concat(queries)
78
- }, this);
79
- }
112
+ const $not = queries.map((query) => { var _a; return (_a = query.getQueryOptions().filter) !== null && _a !== void 0 ? _a : {}; });
113
+ return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $not } }, __classPrivateFieldGet(this, _Query_data, "f"));
114
+ }
115
+ /**
116
+ * Builds a new query object representing a logical AND negating each subquery. In pseudo-code: !q1 AND !q2
117
+ * @param queries An array of subqueries.
118
+ * @returns A new Query object.
119
+ */
80
120
  none(...queries) {
81
- return new Query(this.repository, this.table, {
82
- $none: (this.$none || []).concat(queries)
83
- }, this);
121
+ const $none = queries.map((query) => { var _a; return (_a = query.getQueryOptions().filter) !== null && _a !== void 0 ? _a : {}; });
122
+ return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $none } }, __classPrivateFieldGet(this, _Query_data, "f"));
84
123
  }
85
124
  filter(a, b) {
125
+ var _a, _b;
86
126
  if (arguments.length === 1) {
87
- const constraints = a;
88
- const queries = [];
89
- for (const [column, constraint] of Object.entries(constraints)) {
90
- queries.push({ [column]: constraint });
91
- }
92
- return new Query(this.repository, this.table, {
93
- $all: (this.$all || []).concat(queries)
94
- }, this);
127
+ const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
128
+ const $all = (0, lang_1.compact)([(_a = __classPrivateFieldGet(this, _Query_data, "f").filter) === null || _a === void 0 ? void 0 : _a.$all].flat().concat(constraints));
129
+ return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f"));
95
130
  }
96
131
  else {
97
- const column = a;
98
- const value = b;
99
- return new Query(this.repository, this.table, {
100
- $all: (this.$all || []).concat({ [column]: value })
101
- }, this);
132
+ const $all = (0, lang_1.compact)([(_b = __classPrivateFieldGet(this, _Query_data, "f").filter) === null || _b === void 0 ? void 0 : _b.$all].flat().concat([{ [a]: b }]));
133
+ return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f"));
102
134
  }
103
135
  }
136
+ /**
137
+ * Builds a new query with a new sort option.
138
+ * @param column The column name.
139
+ * @param direction The direction. Either ascending or descending.
140
+ * @returns A new Query object.
141
+ */
104
142
  sort(column, direction) {
105
- const sort = Object.assign(Object.assign({}, this.$sort), { [column]: direction });
106
- const q = new Query(this.repository, this.table, {
107
- $sort: sort
108
- }, this);
109
- return q;
143
+ var _a;
144
+ const originalSort = [(_a = __classPrivateFieldGet(this, _Query_data, "f").sort) !== null && _a !== void 0 ? _a : []].flat();
145
+ const sort = [...originalSort, { column, direction }];
146
+ return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { sort }, __classPrivateFieldGet(this, _Query_data, "f"));
147
+ }
148
+ /**
149
+ * Builds a new query specifying the set of columns to be returned in the query response.
150
+ * @param columns Array of column names to be returned by the query.
151
+ * @returns A new Query object.
152
+ */
153
+ select(columns) {
154
+ return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { columns }, __classPrivateFieldGet(this, _Query_data, "f"));
110
155
  }
111
156
  getPaginated(options = {}) {
112
- return __awaiter(this, void 0, void 0, function* () {
113
- return this.repository._runQuery(this, options);
114
- });
157
+ const query = new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), options, __classPrivateFieldGet(this, _Query_data, "f"));
158
+ return __classPrivateFieldGet(this, _Query_repository, "f").query(query);
115
159
  }
116
- [Symbol.asyncIterator]() {
160
+ [(_Query_table = new WeakMap(), _Query_repository = new WeakMap(), _Query_data = new WeakMap(), Symbol.asyncIterator)]() {
117
161
  return __asyncGenerator(this, arguments, function* _a() {
118
162
  var e_1, _b;
119
163
  try {
@@ -137,6 +181,7 @@ class Query {
137
181
  let end = false;
138
182
  while (!end) {
139
183
  const { records, meta } = yield __await(this.getPaginated(Object.assign(Object.assign({}, options), { page: { size: chunk, offset } })));
184
+ // Method overloading does not provide type inference for the return type.
140
185
  yield yield __await(records);
141
186
  offset += chunk;
142
187
  end = !meta.page.more;
@@ -146,40 +191,49 @@ class Query {
146
191
  getMany(options = {}) {
147
192
  return __awaiter(this, void 0, void 0, function* () {
148
193
  const { records } = yield this.getPaginated(options);
194
+ // Method overloading does not provide type inference for the return type.
149
195
  return records;
150
196
  });
151
197
  }
152
- getOne(options = {}) {
198
+ getAll(chunk = pagination_1.PAGINATION_MAX_SIZE, options = {}) {
199
+ var e_2, _a;
153
200
  return __awaiter(this, void 0, void 0, function* () {
154
- const records = yield this.getMany(Object.assign(Object.assign({}, options), { page: { size: 1 } }));
155
- return records[0] || null;
201
+ const results = [];
202
+ try {
203
+ for (var _b = __asyncValues(this.getIterator(chunk, options)), _c; _c = yield _b.next(), !_c.done;) {
204
+ const page = _c.value;
205
+ results.push(...page);
206
+ }
207
+ }
208
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
209
+ finally {
210
+ try {
211
+ if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
212
+ }
213
+ finally { if (e_2) throw e_2.error; }
214
+ }
215
+ // Method overloading does not provide type inference for the return type.
216
+ return results;
156
217
  });
157
218
  }
158
- deleteAll() {
219
+ getOne(options = {}) {
159
220
  return __awaiter(this, void 0, void 0, function* () {
160
- // TODO: Return number of affected rows
161
- return 0;
221
+ const records = yield this.getMany(Object.assign(Object.assign({}, options), { page: { size: 1 } }));
222
+ // Method overloading does not provide type inference for the return type.
223
+ return records[0] || null;
162
224
  });
163
225
  }
164
226
  nextPage(size, offset) {
165
- return __awaiter(this, void 0, void 0, function* () {
166
- return this.firstPage(size, offset);
167
- });
227
+ return this.firstPage(size, offset);
168
228
  }
169
229
  previousPage(size, offset) {
170
- return __awaiter(this, void 0, void 0, function* () {
171
- return this.firstPage(size, offset);
172
- });
230
+ return this.firstPage(size, offset);
173
231
  }
174
232
  firstPage(size, offset) {
175
- return __awaiter(this, void 0, void 0, function* () {
176
- return this.getPaginated({ page: { size, offset } });
177
- });
233
+ return this.getPaginated({ page: { size, offset } });
178
234
  }
179
235
  lastPage(size, offset) {
180
- return __awaiter(this, void 0, void 0, function* () {
181
- return this.getPaginated({ page: { size, offset, before: 'end' } });
182
- });
236
+ return this.getPaginated({ page: { size, offset, before: 'end' } });
183
237
  }
184
238
  hasNextPage() {
185
239
  return this.meta.page.more;
@@ -0,0 +1,66 @@
1
+ import { SelectedPick } from './selection';
2
+ /**
3
+ * Represents an identifiable record from the database.
4
+ */
5
+ export interface Identifiable {
6
+ /**
7
+ * Unique id of this record.
8
+ */
9
+ id: string;
10
+ }
11
+ export interface BaseData {
12
+ [key: string]: any;
13
+ }
14
+ /**
15
+ * Represents a persisted record from the database.
16
+ */
17
+ export interface XataRecord extends Identifiable {
18
+ /**
19
+ * Metadata of this record.
20
+ */
21
+ xata: {
22
+ /**
23
+ * Number that is increased every time the record is updated.
24
+ */
25
+ version: number;
26
+ };
27
+ /**
28
+ * Retrieves a refreshed copy of the current record from the database.
29
+ */
30
+ read(): Promise<SelectedPick<this, ['*']> | null>;
31
+ /**
32
+ * Performs a partial update of the current record. On success a new object is
33
+ * returned and the current object is not mutated.
34
+ * @param data The columns and their values that have to be updated.
35
+ * @returns A new record containing the latest values for all the columns of the current record.
36
+ */
37
+ update(data: Partial<EditableData<Omit<this, keyof XataRecord>>>): Promise<SelectedPick<this, ['*']>>;
38
+ /**
39
+ * Performs a deletion of the current record in the database.
40
+ *
41
+ * @throws If the record was already deleted or if an error happened while performing the deletion.
42
+ */
43
+ delete(): Promise<void>;
44
+ }
45
+ export declare type Link<Record extends XataRecord> = Omit<XataRecord, 'read' | 'update'> & {
46
+ /**
47
+ * Retrieves a refreshed copy of the current record from the database.
48
+ */
49
+ read(): Promise<SelectedPick<Record, ['*']> | null>;
50
+ /**
51
+ * Performs a partial update of the current record. On success a new object is
52
+ * returned and the current object is not mutated.
53
+ * @param data The columns and their values that have to be updated.
54
+ * @returns A new record containing the latest values for all the columns of the current record.
55
+ */
56
+ update(data: Partial<EditableData<Omit<Record, keyof XataRecord>>>): Promise<SelectedPick<Record, ['*']>>;
57
+ };
58
+ export declare function isIdentifiable(x: any): x is Identifiable & Record<string, unknown>;
59
+ export declare function isXataRecord(x: any): x is XataRecord & Record<string, unknown>;
60
+ export declare type EditableData<O extends BaseData> = {
61
+ [K in keyof O]: O[K] extends XataRecord ? {
62
+ id: string;
63
+ } : NonNullable<O[K]> extends XataRecord ? {
64
+ id: string;
65
+ } | undefined : O[K];
66
+ };
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isXataRecord = exports.isIdentifiable = void 0;
4
+ const lang_1 = require("../util/lang");
5
+ function isIdentifiable(x) {
6
+ return (0, lang_1.isObject)(x) && (0, lang_1.isString)(x === null || x === void 0 ? void 0 : x.id);
7
+ }
8
+ exports.isIdentifiable = isIdentifiable;
9
+ function isXataRecord(x) {
10
+ var _a;
11
+ return (isIdentifiable(x) && typeof (x === null || x === void 0 ? void 0 : x.xata) === 'object' && typeof ((_a = x === null || x === void 0 ? void 0 : x.xata) === null || _a === void 0 ? void 0 : _a.version) === 'number');
12
+ }
13
+ exports.isXataRecord = isXataRecord;