@xata.io/client 0.0.0-beta.4e4e7fc → 0.0.0-beta.53c906e

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 (48) hide show
  1. package/.eslintrc.cjs +13 -0
  2. package/CHANGELOG.md +71 -0
  3. package/dist/api/client.d.ts +4 -2
  4. package/dist/api/client.js +40 -18
  5. package/dist/api/components.d.ts +58 -37
  6. package/dist/api/components.js +48 -38
  7. package/dist/api/fetcher.d.ts +15 -0
  8. package/dist/api/fetcher.js +23 -22
  9. package/dist/api/parameters.d.ts +1 -0
  10. package/dist/api/providers.js +3 -2
  11. package/dist/api/responses.d.ts +6 -0
  12. package/dist/api/schemas.d.ts +1 -1
  13. package/dist/index.d.ts +1 -0
  14. package/dist/index.js +1 -0
  15. package/dist/schema/filters.d.ts +93 -17
  16. package/dist/schema/filters.js +0 -22
  17. package/dist/schema/filters.spec.d.ts +1 -0
  18. package/dist/schema/filters.spec.js +175 -0
  19. package/dist/schema/index.d.ts +5 -3
  20. package/dist/schema/index.js +8 -4
  21. package/dist/schema/operators.d.ts +74 -21
  22. package/dist/schema/operators.js +59 -6
  23. package/dist/schema/pagination.d.ts +56 -14
  24. package/dist/schema/pagination.js +37 -2
  25. package/dist/schema/query.d.ts +110 -37
  26. package/dist/schema/query.js +87 -35
  27. package/dist/schema/record.d.ts +60 -4
  28. package/dist/schema/record.js +11 -0
  29. package/dist/schema/repository.d.ts +140 -25
  30. package/dist/schema/repository.js +232 -100
  31. package/dist/schema/selection.d.ts +24 -13
  32. package/dist/schema/selection.spec.d.ts +1 -0
  33. package/dist/schema/selection.spec.js +203 -0
  34. package/dist/schema/sorting.d.ts +17 -0
  35. package/dist/schema/sorting.js +28 -0
  36. package/dist/schema/sorting.spec.d.ts +1 -0
  37. package/dist/schema/sorting.spec.js +9 -0
  38. package/dist/util/config.d.ts +11 -0
  39. package/dist/util/config.js +121 -0
  40. package/dist/util/environment.d.ts +5 -0
  41. package/dist/util/environment.js +68 -0
  42. package/dist/util/fetch.d.ts +2 -0
  43. package/dist/util/fetch.js +13 -0
  44. package/dist/util/lang.d.ts +3 -0
  45. package/dist/util/lang.js +13 -1
  46. package/dist/util/types.d.ts +22 -1
  47. package/package.json +5 -2
  48. package/tsconfig.json +21 -0
@@ -1,40 +1,93 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- 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;
3
+ exports.includesAny = exports.includesNone = exports.includesAll = 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;
4
+ /**
5
+ * Operator to restrict results to only values that are greater than the given value.
6
+ */
4
7
  const gt = (value) => ({ $gt: value });
5
8
  exports.gt = gt;
9
+ /**
10
+ * Operator to restrict results to only values that are greater than or equal to the given value.
11
+ */
6
12
  const ge = (value) => ({ $ge: value });
7
13
  exports.ge = ge;
14
+ /**
15
+ * Operator to restrict results to only values that are greater than or equal to the given value.
16
+ */
8
17
  const gte = (value) => ({ $ge: value });
9
18
  exports.gte = gte;
19
+ /**
20
+ * Operator to restrict results to only values that are lower than the given value.
21
+ */
10
22
  const lt = (value) => ({ $lt: value });
11
23
  exports.lt = lt;
24
+ /**
25
+ * Operator to restrict results to only values that are lower than or equal to the given value.
26
+ */
12
27
  const lte = (value) => ({ $le: value });
13
28
  exports.lte = lte;
29
+ /**
30
+ * Operator to restrict results to only values that are lower than or equal to the given value.
31
+ */
14
32
  const le = (value) => ({ $le: value });
15
33
  exports.le = le;
34
+ /**
35
+ * Operator to restrict results to only values that are not null.
36
+ */
16
37
  const exists = (column) => ({ $exists: column });
17
38
  exports.exists = exists;
39
+ /**
40
+ * Operator to restrict results to only values that are null.
41
+ */
18
42
  const notExists = (column) => ({ $notExists: column });
19
43
  exports.notExists = notExists;
44
+ /**
45
+ * Operator to restrict results to only values that start with the given prefix.
46
+ */
20
47
  const startsWith = (value) => ({ $startsWith: value });
21
48
  exports.startsWith = startsWith;
49
+ /**
50
+ * Operator to restrict results to only values that end with the given suffix.
51
+ */
22
52
  const endsWith = (value) => ({ $endsWith: value });
23
53
  exports.endsWith = endsWith;
54
+ /**
55
+ * Operator to restrict results to only values that match the given pattern.
56
+ */
24
57
  const pattern = (value) => ({ $pattern: value });
25
58
  exports.pattern = pattern;
59
+ /**
60
+ * Operator to restrict results to only values that are equal to the given value.
61
+ */
26
62
  const is = (value) => ({ $is: value });
27
63
  exports.is = is;
64
+ /**
65
+ * Operator to restrict results to only values that are not equal to the given value.
66
+ */
28
67
  const isNot = (value) => ({ $isNot: value });
29
68
  exports.isNot = isNot;
69
+ /**
70
+ * Operator to restrict results to only values that contain the given value.
71
+ */
30
72
  const contains = (value) => ({ $contains: value });
31
73
  exports.contains = contains;
32
- // TODO: these can only be applied to columns of type "multiple"
74
+ /**
75
+ * Operator to restrict results if some array items match the predicate.
76
+ */
33
77
  const includes = (value) => ({ $includes: value });
34
78
  exports.includes = includes;
35
- const includesSubstring = (value) => ({ $includesSubstring: value });
36
- exports.includesSubstring = includesSubstring;
37
- const includesPattern = (value) => ({ $includesPattern: value });
38
- exports.includesPattern = includesPattern;
79
+ /**
80
+ * Operator to restrict results if all array items match the predicate.
81
+ */
39
82
  const includesAll = (value) => ({ $includesAll: value });
40
83
  exports.includesAll = includesAll;
84
+ /**
85
+ * Operator to restrict results if none array items match the predicate.
86
+ */
87
+ const includesNone = (value) => ({ $includesNone: value });
88
+ exports.includesNone = includesNone;
89
+ /**
90
+ * Operator to restrict results if some array items match the predicate.
91
+ */
92
+ const includesAny = (value) => ({ $includesAny: value });
93
+ exports.includesAny = includesAny;
@@ -1,29 +1,67 @@
1
- import { XataRecord } from '..';
2
1
  import { Query } from './query';
2
+ import { XataRecord } from './record';
3
3
  export declare type PaginationQueryMeta = {
4
4
  page: {
5
5
  cursor: string;
6
6
  more: boolean;
7
7
  };
8
8
  };
9
- export interface Paginable<T extends XataRecord, R extends XataRecord = T> {
9
+ export interface Paginable<Record extends XataRecord, Result extends XataRecord = Record> {
10
10
  meta: PaginationQueryMeta;
11
- records: R[];
12
- nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
13
- previousPage(size?: number, offset?: number): Promise<Page<T, R>>;
14
- firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
15
- lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
11
+ records: Result[];
12
+ nextPage(size?: number, offset?: number): Promise<Page<Record, Result>>;
13
+ previousPage(size?: number, offset?: number): Promise<Page<Record, Result>>;
14
+ firstPage(size?: number, offset?: number): Promise<Page<Record, Result>>;
15
+ lastPage(size?: number, offset?: number): Promise<Page<Record, Result>>;
16
16
  hasNextPage(): boolean;
17
17
  }
18
- export declare class Page<T extends XataRecord, R extends XataRecord> implements Paginable<T, R> {
18
+ /**
19
+ * A Page contains a set of results from a query plus metadata about the retrieved
20
+ * set of values such as the cursor, required to retrieve additional records.
21
+ */
22
+ export declare class Page<Record extends XataRecord, Result extends XataRecord = Record> implements Paginable<Record, Result> {
19
23
  #private;
24
+ /**
25
+ * Page metadata, required to retrieve additional records.
26
+ */
20
27
  readonly meta: PaginationQueryMeta;
21
- readonly records: R[];
22
- constructor(query: Query<T, R>, meta: PaginationQueryMeta, records?: R[]);
23
- nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
24
- previousPage(size?: number, offset?: number): Promise<Page<T, R>>;
25
- firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
26
- lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
28
+ /**
29
+ * The set of results for this page.
30
+ */
31
+ readonly records: Result[];
32
+ constructor(query: Query<Record, Result>, meta: PaginationQueryMeta, records?: Result[]);
33
+ /**
34
+ * Retrieves the next page of results.
35
+ * @param size Maximum number of results to be retrieved.
36
+ * @param offset Number of results to skip when retrieving the results.
37
+ * @returns The next page or results.
38
+ */
39
+ nextPage(size?: number, offset?: number): Promise<Page<Record, Result>>;
40
+ /**
41
+ * Retrieves the previous page of results.
42
+ * @param size Maximum number of results to be retrieved.
43
+ * @param offset Number of results to skip when retrieving the results.
44
+ * @returns The previous page or results.
45
+ */
46
+ previousPage(size?: number, offset?: number): Promise<Page<Record, Result>>;
47
+ /**
48
+ * Retrieves the first 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 first page or results.
52
+ */
53
+ firstPage(size?: number, offset?: number): Promise<Page<Record, Result>>;
54
+ /**
55
+ * Retrieves the last page of results.
56
+ * @param size Maximum number of results to be retrieved.
57
+ * @param offset Number of results to skip when retrieving the results.
58
+ * @returns The last page or results.
59
+ */
60
+ lastPage(size?: number, offset?: number): Promise<Page<Record, Result>>;
61
+ /**
62
+ * Shortcut method to check if there will be additional results if the next page of results is retrieved.
63
+ * @returns Whether or not there will be additional results in the next page of results.
64
+ */
27
65
  hasNextPage(): boolean;
28
66
  }
29
67
  export declare type CursorNavigationOptions = {
@@ -39,3 +77,7 @@ export declare type OffsetNavigationOptions = {
39
77
  offset?: number;
40
78
  };
41
79
  export declare type PaginationOptions = CursorNavigationOptions & OffsetNavigationOptions;
80
+ export declare const PAGINATION_MAX_SIZE = 200;
81
+ export declare const PAGINATION_DEFAULT_SIZE = 200;
82
+ export declare const PAGINATION_MAX_OFFSET = 800;
83
+ export declare const PAGINATION_DEFAULT_OFFSET = 0;
@@ -21,7 +21,11 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
21
21
  };
22
22
  var _Page_query;
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
- 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
+ */
25
29
  class Page {
26
30
  constructor(query, meta, records = []) {
27
31
  _Page_query.set(this, void 0);
@@ -29,30 +33,61 @@ class Page {
29
33
  this.meta = meta;
30
34
  this.records = records;
31
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
+ */
32
42
  nextPage(size, offset) {
33
43
  return __awaiter(this, void 0, void 0, function* () {
34
44
  return __classPrivateFieldGet(this, _Page_query, "f").getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
35
45
  });
36
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
+ */
37
53
  previousPage(size, offset) {
38
54
  return __awaiter(this, void 0, void 0, function* () {
39
55
  return __classPrivateFieldGet(this, _Page_query, "f").getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
40
56
  });
41
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
+ */
42
64
  firstPage(size, offset) {
43
65
  return __awaiter(this, void 0, void 0, function* () {
44
66
  return __classPrivateFieldGet(this, _Page_query, "f").getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
45
67
  });
46
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
+ */
47
75
  lastPage(size, offset) {
48
76
  return __awaiter(this, void 0, void 0, function* () {
49
77
  return __classPrivateFieldGet(this, _Page_query, "f").getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
50
78
  });
51
79
  }
52
- // 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
+ */
53
84
  hasNextPage() {
54
85
  return this.meta.page.more;
55
86
  }
56
87
  }
57
88
  exports.Page = Page;
58
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,45 +1,118 @@
1
- import { XataRecord, Repository } from '..';
2
- import { FilterExpression, SortExpression, PageConfig, ColumnsFilter } from '../api/schemas';
3
- import { DeepConstraint, FilterConstraints, SortDirection, SortFilter } from './filters';
4
- import { PaginationOptions, Page, Paginable, PaginationQueryMeta } from './pagination';
5
- import { Selectable, SelectableColumn, Select } from './selection';
1
+ import { FilterExpression } from '../api/schemas';
2
+ import { NonEmptyArray, RequiredBy } from '../util/types';
3
+ import { Filter } 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
+ import { SortDirection, SortFilter } from './sorting';
6
9
  export declare type QueryOptions<T extends XataRecord> = {
7
10
  page?: PaginationOptions;
8
- columns?: Extract<keyof Selectable<T>, string>[];
11
+ columns?: NonEmptyArray<SelectableColumn<T>>;
12
+ filter?: FilterExpression;
9
13
  sort?: SortFilter<T> | SortFilter<T>[];
10
14
  };
11
- export declare type QueryTableOptions = {
12
- filter: FilterExpression;
13
- sort?: SortExpression;
14
- page?: PageConfig;
15
- columns?: ColumnsFilter;
16
- };
17
- export declare class Query<T extends XataRecord, R extends XataRecord = T> implements Paginable<T, R> {
15
+ /**
16
+ * Query objects contain the information of all filters, sorting, etc. to be included in the database query.
17
+ *
18
+ * Query objects are immutable. Any method that adds more constraints or options to the query will return
19
+ * a new Query object containing the both the previous and the new constraints and options.
20
+ */
21
+ export declare class Query<Record extends XataRecord, Result extends XataRecord = Record> implements Paginable<Record, Result> {
18
22
  #private;
19
23
  readonly meta: PaginationQueryMeta;
20
- readonly records: R[];
21
- constructor(repository: Repository<T> | null, table: string, data: Partial<QueryTableOptions>, parent?: Partial<QueryTableOptions>);
22
- getQueryOptions(): QueryTableOptions;
23
- any(...queries: Query<T, R>[]): Query<T, R>;
24
- all(...queries: Query<T, R>[]): Query<T, R>;
25
- not(...queries: Query<T, R>[]): Query<T, R>;
26
- none(...queries: Query<T, R>[]): Query<T, R>;
27
- filter(constraints: FilterConstraints<T>): Query<T, R>;
28
- filter<F extends keyof T>(column: F, value: FilterConstraints<T[F]> | DeepConstraint<T[F]>): Query<T, R>;
29
- sort<F extends keyof T>(column: F, direction: SortDirection): Query<T, R>;
30
- select<K extends SelectableColumn<T>>(columns: K[]): Query<T, Select<T, K>>;
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
- /**async deleteAll(): Promise<number> {
37
- // TODO: Return number of affected rows
38
- return 0;
39
- }**/
40
- nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
41
- previousPage(size?: number, offset?: number): Promise<Page<T, R>>;
42
- firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
43
- lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
24
+ readonly records: Result[];
25
+ constructor(repository: Repository<Record> | null, table: string, data: Partial<QueryOptions<Record>>, parent?: Partial<QueryOptions<Record>>);
26
+ getQueryOptions(): QueryOptions<Record>;
27
+ /**
28
+ * Builds a new query object representing a logical OR between the given subqueries.
29
+ * @param queries An array of subqueries.
30
+ * @returns A new Query object.
31
+ */
32
+ any(...queries: Query<Record, any>[]): Query<Record, Result>;
33
+ /**
34
+ * Builds a new query object representing a logical AND between the given subqueries.
35
+ * @param queries An array of subqueries.
36
+ * @returns A new Query object.
37
+ */
38
+ all(...queries: Query<Record, any>[]): Query<Record, Result>;
39
+ /**
40
+ * Builds a new query object representing a logical OR negating each subquery. In pseudo-code: !q1 OR !q2
41
+ * @param queries An array of subqueries.
42
+ * @returns A new Query object.
43
+ */
44
+ not(...queries: Query<Record, any>[]): Query<Record, Result>;
45
+ /**
46
+ * Builds a new query object representing a logical AND negating each subquery. In pseudo-code: !q1 AND !q2
47
+ * @param queries An array of subqueries.
48
+ * @returns A new Query object.
49
+ */
50
+ none(...queries: Query<Record, any>[]): Query<Record, Result>;
51
+ /**
52
+ * Builds a new query object adding one or more constraints. Examples:
53
+ *
54
+ * ```
55
+ * query.filter("columnName", columnValue)
56
+ * query.filter({
57
+ * "columnName": columnValue
58
+ * })
59
+ * query.filter({
60
+ * "columnName": operator(columnValue) // Use gt, gte, lt, lte, startsWith,...
61
+ * })
62
+ * ```
63
+ *
64
+ * @returns A new Query object.
65
+ */
66
+ filter(filters: Filter<Record>): Query<Record, Result>;
67
+ filter<F extends SelectableColumn<Record>>(column: F, value: Filter<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>>;
44
117
  hasNextPage(): boolean;
45
118
  }
@@ -42,9 +42,16 @@ var _Query_table, _Query_repository, _Query_data;
42
42
  Object.defineProperty(exports, "__esModule", { value: true });
43
43
  exports.Query = void 0;
44
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
+ */
45
52
  class Query {
46
53
  constructor(repository, table, data, parent) {
47
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
54
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
48
55
  _Query_table.set(this, void 0);
49
56
  _Query_repository.set(this, void 0);
50
57
  _Query_data.set(this, { filter: {} });
@@ -58,12 +65,14 @@ class Query {
58
65
  else {
59
66
  __classPrivateFieldSet(this, _Query_repository, this, "f");
60
67
  }
61
- __classPrivateFieldGet(this, _Query_data, "f").filter.$any = (_b = (_a = data.filter) === null || _a === void 0 ? void 0 : _a.$any) !== null && _b !== void 0 ? _b : (_c = parent === null || parent === void 0 ? void 0 : parent.filter) === null || _c === void 0 ? void 0 : _c.$any;
62
- __classPrivateFieldGet(this, _Query_data, "f").filter.$all = (_e = (_d = data.filter) === null || _d === void 0 ? void 0 : _d.$all) !== null && _e !== void 0 ? _e : (_f = parent === null || parent === void 0 ? void 0 : parent.filter) === null || _f === void 0 ? void 0 : _f.$all;
63
- __classPrivateFieldGet(this, _Query_data, "f").filter.$not = (_h = (_g = data.filter) === null || _g === void 0 ? void 0 : _g.$not) !== null && _h !== void 0 ? _h : (_j = parent === null || parent === void 0 ? void 0 : parent.filter) === null || _j === void 0 ? void 0 : _j.$not;
64
- __classPrivateFieldGet(this, _Query_data, "f").filter.$none = (_l = (_k = data.filter) === null || _k === void 0 ? void 0 : _k.$none) !== null && _l !== void 0 ? _l : (_m = parent === null || parent === void 0 ? void 0 : parent.filter) === null || _m === void 0 ? void 0 : _m.$none;
65
- __classPrivateFieldGet(this, _Query_data, "f").sort = (_o = data.sort) !== null && _o !== void 0 ? _o : parent === null || parent === void 0 ? void 0 : parent.sort;
66
- __classPrivateFieldGet(this, _Query_data, "f").columns = (_q = (_p = data.columns) !== null && _p !== void 0 ? _p : parent === null || parent === void 0 ? void 0 : parent.columns) !== null && _q !== void 0 ? _q : ['*'];
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;
67
76
  this.any = this.any.bind(this);
68
77
  this.all = this.all.bind(this);
69
78
  this.not = this.not.bind(this);
@@ -76,46 +85,77 @@ class Query {
76
85
  getQueryOptions() {
77
86
  return __classPrivateFieldGet(this, _Query_data, "f");
78
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
+ */
79
93
  any(...queries) {
80
- const $any = queries.map((query) => query.getQueryOptions().filter);
94
+ const $any = queries.map((query) => { var _a; return (_a = query.getQueryOptions().filter) !== null && _a !== void 0 ? _a : {}; });
81
95
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $any } }, __classPrivateFieldGet(this, _Query_data, "f"));
82
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
+ */
83
102
  all(...queries) {
84
- const $all = queries.map((query) => query.getQueryOptions().filter);
103
+ const $all = queries.map((query) => { var _a; return (_a = query.getQueryOptions().filter) !== null && _a !== void 0 ? _a : {}; });
85
104
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f"));
86
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
+ */
87
111
  not(...queries) {
88
- const $not = queries.map((query) => query.getQueryOptions().filter);
112
+ const $not = queries.map((query) => { var _a; return (_a = query.getQueryOptions().filter) !== null && _a !== void 0 ? _a : {}; });
89
113
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $not } }, __classPrivateFieldGet(this, _Query_data, "f"));
90
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
+ */
91
120
  none(...queries) {
92
- const $none = queries.map((query) => query.getQueryOptions().filter);
121
+ const $none = queries.map((query) => { var _a; return (_a = query.getQueryOptions().filter) !== null && _a !== void 0 ? _a : {}; });
93
122
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $none } }, __classPrivateFieldGet(this, _Query_data, "f"));
94
123
  }
95
124
  filter(a, b) {
125
+ var _a, _b;
96
126
  if (arguments.length === 1) {
97
127
  const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
98
- const $all = (0, lang_1.compact)([__classPrivateFieldGet(this, _Query_data, "f").filter.$all].flat().concat(constraints));
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));
99
129
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f"));
100
130
  }
101
131
  else {
102
- const column = a;
103
- const value = b;
104
- const $all = (0, lang_1.compact)([__classPrivateFieldGet(this, _Query_data, "f").filter.$all].flat().concat({ [column]: value }));
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 }]));
105
133
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f"));
106
134
  }
107
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
+ */
108
142
  sort(column, direction) {
109
- const sort = Object.assign(Object.assign({}, __classPrivateFieldGet(this, _Query_data, "f").sort), { [column]: direction });
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 }];
110
146
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { sort }, __classPrivateFieldGet(this, _Query_data, "f"));
111
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
+ */
112
153
  select(columns) {
113
154
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { columns }, __classPrivateFieldGet(this, _Query_data, "f"));
114
155
  }
115
156
  getPaginated(options = {}) {
116
- return __awaiter(this, void 0, void 0, function* () {
117
- return __classPrivateFieldGet(this, _Query_repository, "f").query(this, options);
118
- });
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);
119
159
  }
120
160
  [(_Query_table = new WeakMap(), _Query_repository = new WeakMap(), _Query_data = new WeakMap(), Symbol.asyncIterator)]() {
121
161
  return __asyncGenerator(this, arguments, function* _a() {
@@ -141,6 +181,7 @@ class Query {
141
181
  let end = false;
142
182
  while (!end) {
143
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.
144
185
  yield yield __await(records);
145
186
  offset += chunk;
146
187
  end = !meta.page.more;
@@ -150,38 +191,49 @@ class Query {
150
191
  getMany(options = {}) {
151
192
  return __awaiter(this, void 0, void 0, function* () {
152
193
  const { records } = yield this.getPaginated(options);
194
+ // Method overloading does not provide type inference for the return type.
153
195
  return records;
154
196
  });
155
197
  }
198
+ getAll(chunk = pagination_1.PAGINATION_MAX_SIZE, options = {}) {
199
+ var e_2, _a;
200
+ return __awaiter(this, void 0, void 0, function* () {
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;
217
+ });
218
+ }
156
219
  getOne(options = {}) {
157
220
  return __awaiter(this, void 0, void 0, function* () {
158
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.
159
223
  return records[0] || null;
160
224
  });
161
225
  }
162
- /**async deleteAll(): Promise<number> {
163
- // TODO: Return number of affected rows
164
- return 0;
165
- }**/
166
226
  nextPage(size, offset) {
167
- return __awaiter(this, void 0, void 0, function* () {
168
- return this.firstPage(size, offset);
169
- });
227
+ return this.firstPage(size, offset);
170
228
  }
171
229
  previousPage(size, offset) {
172
- return __awaiter(this, void 0, void 0, function* () {
173
- return this.firstPage(size, offset);
174
- });
230
+ return this.firstPage(size, offset);
175
231
  }
176
232
  firstPage(size, offset) {
177
- return __awaiter(this, void 0, void 0, function* () {
178
- return this.getPaginated({ page: { size, offset } });
179
- });
233
+ return this.getPaginated({ page: { size, offset } });
180
234
  }
181
235
  lastPage(size, offset) {
182
- return __awaiter(this, void 0, void 0, function* () {
183
- return this.getPaginated({ page: { size, offset, before: 'end' } });
184
- });
236
+ return this.getPaginated({ page: { size, offset, before: 'end' } });
185
237
  }
186
238
  hasNextPage() {
187
239
  return this.meta.page.more;