@xata.io/client 0.0.0-beta.26144eb → 0.0.0-beta.2ab8b47

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 (53) hide show
  1. package/.eslintrc.cjs +13 -0
  2. package/CHANGELOG.md +67 -0
  3. package/dist/api/client.d.ts +2 -2
  4. package/dist/api/client.js +34 -18
  5. package/dist/api/components.d.ts +34 -37
  6. package/dist/api/components.js +34 -37
  7. package/dist/api/fetcher.d.ts +15 -0
  8. package/dist/api/fetcher.js +23 -22
  9. package/dist/api/providers.js +3 -2
  10. package/dist/api/responses.d.ts +6 -0
  11. package/dist/client.d.ts +39 -0
  12. package/dist/client.js +124 -0
  13. package/dist/index.d.ts +3 -0
  14. package/dist/index.js +3 -0
  15. package/dist/namespace.d.ts +7 -0
  16. package/dist/namespace.js +6 -0
  17. package/dist/schema/filters.d.ts +93 -17
  18. package/dist/schema/filters.js +0 -22
  19. package/dist/schema/filters.spec.d.ts +1 -0
  20. package/dist/schema/filters.spec.js +177 -0
  21. package/dist/schema/index.d.ts +19 -3
  22. package/dist/schema/index.js +30 -6
  23. package/dist/schema/operators.d.ts +26 -24
  24. package/dist/schema/operators.js +13 -11
  25. package/dist/schema/pagination.d.ts +18 -14
  26. package/dist/schema/pagination.js +5 -2
  27. package/dist/schema/query.d.ts +48 -44
  28. package/dist/schema/query.js +46 -31
  29. package/dist/schema/record.d.ts +28 -3
  30. package/dist/schema/record.js +11 -0
  31. package/dist/schema/repository.d.ts +90 -65
  32. package/dist/schema/repository.js +206 -194
  33. package/dist/schema/selection.d.ts +24 -13
  34. package/dist/schema/selection.spec.d.ts +1 -0
  35. package/dist/schema/selection.spec.js +204 -0
  36. package/dist/schema/sorting.d.ts +17 -0
  37. package/dist/schema/sorting.js +28 -0
  38. package/dist/schema/sorting.spec.d.ts +1 -0
  39. package/dist/schema/sorting.spec.js +11 -0
  40. package/dist/search/index.d.ts +20 -0
  41. package/dist/search/index.js +30 -0
  42. package/dist/util/branches.d.ts +5 -0
  43. package/dist/util/branches.js +7 -0
  44. package/dist/util/config.d.ts +11 -0
  45. package/dist/util/config.js +121 -0
  46. package/dist/util/environment.d.ts +5 -0
  47. package/dist/util/environment.js +68 -0
  48. package/dist/util/fetch.d.ts +2 -0
  49. package/dist/util/fetch.js +13 -0
  50. package/dist/util/lang.d.ts +3 -0
  51. package/dist/util/lang.js +13 -1
  52. package/dist/util/types.d.ts +23 -1
  53. package/package.json +5 -2
@@ -1,72 +1,74 @@
1
- import { Constraint } from './filters';
2
- declare type ComparableType = number | Date;
1
+ import { ArrayFilter, ComparableType, ComparableTypeFilter, ExistanceFilter, PropertyFilter, StringTypeFilter } from './filters';
2
+ import { SelectableColumn } from './selection';
3
3
  /**
4
4
  * Operator to restrict results to only values that are greater than the given value.
5
5
  */
6
- export declare const gt: <T extends ComparableType>(value: T) => Constraint<T>;
6
+ export declare const gt: <T extends ComparableType>(value: T) => ComparableTypeFilter<T>;
7
7
  /**
8
8
  * Operator to restrict results to only values that are greater than or equal to the given value.
9
9
  */
10
- export declare const ge: <T extends ComparableType>(value: T) => Constraint<T>;
10
+ export declare const ge: <T extends ComparableType>(value: T) => ComparableTypeFilter<T>;
11
11
  /**
12
12
  * Operator to restrict results to only values that are greater than or equal to the given value.
13
13
  */
14
- export declare const gte: <T extends ComparableType>(value: T) => Constraint<T>;
14
+ export declare const gte: <T extends ComparableType>(value: T) => ComparableTypeFilter<T>;
15
15
  /**
16
16
  * Operator to restrict results to only values that are lower than the given value.
17
17
  */
18
- export declare const lt: <T extends ComparableType>(value: T) => Constraint<T>;
18
+ export declare const lt: <T extends ComparableType>(value: T) => ComparableTypeFilter<T>;
19
19
  /**
20
20
  * Operator to restrict results to only values that are lower than or equal to the given value.
21
21
  */
22
- export declare const lte: <T extends ComparableType>(value: T) => Constraint<T>;
22
+ export declare const lte: <T extends ComparableType>(value: T) => ComparableTypeFilter<T>;
23
23
  /**
24
24
  * Operator to restrict results to only values that are lower than or equal to the given value.
25
25
  */
26
- export declare const le: <T extends ComparableType>(value: T) => Constraint<T>;
26
+ export declare const le: <T extends ComparableType>(value: T) => ComparableTypeFilter<T>;
27
27
  /**
28
28
  * Operator to restrict results to only values that are not null.
29
29
  */
30
- export declare const exists: (column: string) => Constraint<string>;
30
+ export declare const exists: <T>(column: SelectableColumn<T, []>) => ExistanceFilter<T>;
31
31
  /**
32
32
  * Operator to restrict results to only values that are null.
33
33
  */
34
- export declare const notExists: (column: string) => Constraint<string>;
34
+ export declare const notExists: <T>(column: SelectableColumn<T, []>) => ExistanceFilter<T>;
35
35
  /**
36
36
  * Operator to restrict results to only values that start with the given prefix.
37
37
  */
38
- export declare const startsWith: (value: string) => Constraint<string>;
38
+ export declare const startsWith: (value: string) => StringTypeFilter;
39
39
  /**
40
40
  * Operator to restrict results to only values that end with the given suffix.
41
41
  */
42
- export declare const endsWith: (value: string) => Constraint<string>;
42
+ export declare const endsWith: (value: string) => StringTypeFilter;
43
43
  /**
44
44
  * Operator to restrict results to only values that match the given pattern.
45
45
  */
46
- export declare const pattern: (value: string) => Constraint<string>;
46
+ export declare const pattern: (value: string) => StringTypeFilter;
47
47
  /**
48
48
  * Operator to restrict results to only values that are equal to the given value.
49
49
  */
50
- export declare const is: <T>(value: T) => Constraint<T>;
50
+ export declare const is: <T>(value: T) => PropertyFilter<T>;
51
51
  /**
52
52
  * Operator to restrict results to only values that are not equal to the given value.
53
53
  */
54
- export declare const isNot: <T>(value: T) => Constraint<T>;
54
+ export declare const isNot: <T>(value: T) => PropertyFilter<T>;
55
55
  /**
56
56
  * Operator to restrict results to only values that contain the given value.
57
57
  */
58
- export declare const contains: <T>(value: T) => Constraint<T>;
58
+ export declare const contains: (value: string) => StringTypeFilter;
59
59
  /**
60
- * Operator to restrict results to only arrays that include the given value.
60
+ * Operator to restrict results if some array items match the predicate.
61
61
  */
62
- export declare const includes: (value: string) => Constraint<string>;
62
+ export declare const includes: <T>(value: T) => ArrayFilter<T>;
63
63
  /**
64
- * Operator to restrict results to only arrays that include a value matching the given substring.
64
+ * Operator to restrict results if all array items match the predicate.
65
65
  */
66
- export declare const includesSubstring: (value: string) => Constraint<string>;
66
+ export declare const includesAll: <T>(value: T) => ArrayFilter<T>;
67
67
  /**
68
- * Operator to restrict results to only arrays that include a value matching the given pattern.
68
+ * Operator to restrict results if none array items match the predicate.
69
69
  */
70
- export declare const includesPattern: (value: string) => Constraint<string>;
71
- export declare const includesAll: (value: string) => Constraint<string>;
72
- export {};
70
+ export declare const includesNone: <T>(value: T) => ArrayFilter<T>;
71
+ /**
72
+ * Operator to restrict results if some array items match the predicate.
73
+ */
74
+ export declare const includesAny: <T>(value: T) => ArrayFilter<T>;
@@ -1,6 +1,6 @@
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
4
  /**
5
5
  * Operator to restrict results to only values that are greater than the given value.
6
6
  */
@@ -71,21 +71,23 @@ exports.isNot = isNot;
71
71
  */
72
72
  const contains = (value) => ({ $contains: value });
73
73
  exports.contains = contains;
74
- // TODO: these can only be applied to columns of type "multiple"
75
74
  /**
76
- * Operator to restrict results to only arrays that include the given value.
75
+ * Operator to restrict results if some array items match the predicate.
77
76
  */
78
77
  const includes = (value) => ({ $includes: value });
79
78
  exports.includes = includes;
80
79
  /**
81
- * Operator to restrict results to only arrays that include a value matching the given substring.
80
+ * Operator to restrict results if all array items match the predicate.
82
81
  */
83
- const includesSubstring = (value) => ({ $includesSubstring: value });
84
- exports.includesSubstring = includesSubstring;
85
- /**
86
- * Operator to restrict results to only arrays that include a value matching the given pattern.
87
- */
88
- const includesPattern = (value) => ({ $includesPattern: value });
89
- exports.includesPattern = includesPattern;
90
82
  const includesAll = (value) => ({ $includesAll: value });
91
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,25 +1,25 @@
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
18
  /**
19
19
  * A Page contains a set of results from a query plus metadata about the retrieved
20
20
  * set of values such as the cursor, required to retrieve additional records.
21
21
  */
22
- export declare class Page<T extends XataRecord, R extends XataRecord> implements Paginable<T, R> {
22
+ export declare class Page<Record extends XataRecord, Result extends XataRecord = Record> implements Paginable<Record, Result> {
23
23
  #private;
24
24
  /**
25
25
  * Page metadata, required to retrieve additional records.
@@ -28,36 +28,36 @@ export declare class Page<T extends XataRecord, R extends XataRecord> implements
28
28
  /**
29
29
  * The set of results for this page.
30
30
  */
31
- readonly records: R[];
32
- constructor(query: Query<T, R>, meta: PaginationQueryMeta, records?: R[]);
31
+ readonly records: Result[];
32
+ constructor(query: Query<Record, Result>, meta: PaginationQueryMeta, records?: Result[]);
33
33
  /**
34
34
  * Retrieves the next page of results.
35
35
  * @param size Maximum number of results to be retrieved.
36
36
  * @param offset Number of results to skip when retrieving the results.
37
37
  * @returns The next page or results.
38
38
  */
39
- nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
39
+ nextPage(size?: number, offset?: number): Promise<Page<Record, Result>>;
40
40
  /**
41
41
  * Retrieves the previous page of results.
42
42
  * @param size Maximum number of results to be retrieved.
43
43
  * @param offset Number of results to skip when retrieving the results.
44
44
  * @returns The previous page or results.
45
45
  */
46
- previousPage(size?: number, offset?: number): Promise<Page<T, R>>;
46
+ previousPage(size?: number, offset?: number): Promise<Page<Record, Result>>;
47
47
  /**
48
48
  * Retrieves the first page of results.
49
49
  * @param size Maximum number of results to be retrieved.
50
50
  * @param offset Number of results to skip when retrieving the results.
51
51
  * @returns The first page or results.
52
52
  */
53
- firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
53
+ firstPage(size?: number, offset?: number): Promise<Page<Record, Result>>;
54
54
  /**
55
55
  * Retrieves the last page of results.
56
56
  * @param size Maximum number of results to be retrieved.
57
57
  * @param offset Number of results to skip when retrieving the results.
58
58
  * @returns The last page or results.
59
59
  */
60
- lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
60
+ lastPage(size?: number, offset?: number): Promise<Page<Record, Result>>;
61
61
  /**
62
62
  * Shortcut method to check if there will be additional results if the next page of results is retrieved.
63
63
  * @returns Whether or not there will be additional results in the next page of results.
@@ -77,3 +77,7 @@ export declare type OffsetNavigationOptions = {
77
77
  offset?: number;
78
78
  };
79
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,7 @@ 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
25
  /**
26
26
  * A Page contains a set of results from a query plus metadata about the retrieved
27
27
  * set of values such as the cursor, required to retrieve additional records.
@@ -77,7 +77,6 @@ class Page {
77
77
  return __classPrivateFieldGet(this, _Page_query, "f").getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
78
78
  });
79
79
  }
80
- // TODO: We need to add something on the backend if we want a hasPreviousPage
81
80
  /**
82
81
  * Shortcut method to check if there will be additional results if the next page of results is retrieved.
83
82
  * @returns Whether or not there will be additional results in the next page of results.
@@ -88,3 +87,7 @@ class Page {
88
87
  }
89
88
  exports.Page = Page;
90
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,55 +1,53 @@
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
15
  /**
18
16
  * Query objects contain the information of all filters, sorting, etc. to be included in the database query.
19
17
  *
20
18
  * Query objects are immutable. Any method that adds more constraints or options to the query will return
21
19
  * a new Query object containing the both the previous and the new constraints and options.
22
20
  */
23
- export declare class Query<T extends XataRecord, R extends XataRecord = T> implements Paginable<T, R> {
21
+ export declare class Query<Record extends XataRecord, Result extends XataRecord = Record> implements Paginable<Record, Result> {
24
22
  #private;
25
23
  readonly meta: PaginationQueryMeta;
26
- readonly records: R[];
27
- constructor(repository: Repository<T> | null, table: string, data: Partial<QueryTableOptions>, parent?: Partial<QueryTableOptions>);
28
- getQueryOptions(): QueryTableOptions;
24
+ readonly records: Result[];
25
+ constructor(repository: Repository<Record> | null, table: string, data: Partial<QueryOptions<Record>>, parent?: Partial<QueryOptions<Record>>);
26
+ getQueryOptions(): QueryOptions<Record>;
29
27
  /**
30
28
  * Builds a new query object representing a logical OR between the given subqueries.
31
29
  * @param queries An array of subqueries.
32
30
  * @returns A new Query object.
33
31
  */
34
- any(...queries: Query<T, R>[]): Query<T, R>;
32
+ any(...queries: Query<Record, any>[]): Query<Record, Result>;
35
33
  /**
36
34
  * Builds a new query object representing a logical AND between the given subqueries.
37
35
  * @param queries An array of subqueries.
38
36
  * @returns A new Query object.
39
37
  */
40
- all(...queries: Query<T, R>[]): Query<T, R>;
38
+ all(...queries: Query<Record, any>[]): Query<Record, Result>;
41
39
  /**
42
40
  * Builds a new query object representing a logical OR negating each subquery. In pseudo-code: !q1 OR !q2
43
41
  * @param queries An array of subqueries.
44
42
  * @returns A new Query object.
45
43
  */
46
- not(...queries: Query<T, R>[]): Query<T, R>;
44
+ not(...queries: Query<Record, any>[]): Query<Record, Result>;
47
45
  /**
48
46
  * Builds a new query object representing a logical AND negating each subquery. In pseudo-code: !q1 AND !q2
49
47
  * @param queries An array of subqueries.
50
48
  * @returns A new Query object.
51
49
  */
52
- none(...queries: Query<T, R>[]): Query<T, R>;
50
+ none(...queries: Query<Record, any>[]): Query<Record, Result>;
53
51
  /**
54
52
  * Builds a new query object adding one or more constraints. Examples:
55
53
  *
@@ -63,52 +61,58 @@ export declare class Query<T extends XataRecord, R extends XataRecord = T> imple
63
61
  * })
64
62
  * ```
65
63
  *
66
- * @param constraints
67
64
  * @returns A new Query object.
68
65
  */
69
- filter(constraints: FilterConstraints<T>): Query<T, R>;
70
- filter<F extends keyof Selectable<T>>(column: F, value: FilterConstraints<T[F]> | DeepConstraint<T[F]>): Query<T, R>;
66
+ filter(filters: Filter<Record>): Query<Record, Result>;
67
+ filter<F extends SelectableColumn<Record>>(column: F, value: Filter<ValueAtColumn<Record, F>>): Query<Record, Result>;
71
68
  /**
72
69
  * Builds a new query with a new sort option.
73
70
  * @param column The column name.
74
71
  * @param direction The direction. Either ascending or descending.
75
72
  * @returns A new Query object.
76
73
  */
77
- sort<F extends keyof T>(column: F, direction: SortDirection): Query<T, R>;
74
+ sort<F extends SelectableColumn<Record>>(column: F, direction: SortDirection): Query<Record, Result>;
78
75
  /**
79
76
  * Builds a new query specifying the set of columns to be returned in the query response.
80
77
  * @param columns Array of column names to be returned by the query.
81
78
  * @returns A new Query object.
82
79
  */
83
- select<K extends SelectableColumn<T>>(columns: K[]): Query<T, Select<T, K>>;
84
- getPaginated<Options extends QueryOptions<T>>(options?: Options): Promise<Page<T, typeof options extends {
85
- columns: SelectableColumn<T>[];
86
- } ? Select<T, typeof options['columns'][number]> : R>>;
87
- [Symbol.asyncIterator](): AsyncIterableIterator<R>;
88
- getIterator(chunk: number, options?: Omit<QueryOptions<T>, 'page'>): AsyncGenerator<R[]>;
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']>[]>;
89
88
  /**
90
89
  * Performs the query in the database and returns a set of results.
91
90
  * @param options Additional options to be used when performing the query.
92
91
  * @returns An array of records from the database.
93
92
  */
94
- getMany<Options extends QueryOptions<T>>(options?: Options): Promise<(typeof options extends {
95
- columns: SelectableColumn<T>[];
96
- } ? Select<T, typeof options['columns'][number]> : R)[]>;
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']>[]>;
97
105
  /**
98
106
  * Performs the query in the database and returns the first result.
99
107
  * @param options Additional options to be used when performing the query.
100
108
  * @returns The first record that matches the query, or null if no record matched the query.
101
109
  */
102
- getOne<Options extends Omit<QueryOptions<T>, 'page'>>(options?: Options): Promise<(typeof options extends {
103
- columns: SelectableColumn<T>[];
104
- } ? Select<T, typeof options['columns'][number]> : R) | null>;
105
- /**async deleteAll(): Promise<number> {
106
- // TODO: Return number of affected rows
107
- return 0;
108
- }**/
109
- nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
110
- previousPage(size?: number, offset?: number): Promise<Page<T, R>>;
111
- firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
112
- lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
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>>;
113
117
  hasNextPage(): boolean;
114
118
  }
@@ -42,6 +42,7 @@ 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");
45
46
  /**
46
47
  * Query objects contain the information of all filters, sorting, etc. to be included in the database query.
47
48
  *
@@ -50,7 +51,7 @@ const lang_1 = require("../util/lang");
50
51
  */
51
52
  class Query {
52
53
  constructor(repository, table, data, parent) {
53
- 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;
54
55
  _Query_table.set(this, void 0);
55
56
  _Query_repository.set(this, void 0);
56
57
  _Query_data.set(this, { filter: {} });
@@ -64,12 +65,14 @@ class Query {
64
65
  else {
65
66
  __classPrivateFieldSet(this, _Query_repository, this, "f");
66
67
  }
67
- __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;
68
- __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;
69
- __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;
70
- __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;
71
- __classPrivateFieldGet(this, _Query_data, "f").sort = (_o = data.sort) !== null && _o !== void 0 ? _o : parent === null || parent === void 0 ? void 0 : parent.sort;
72
- __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;
73
76
  this.any = this.any.bind(this);
74
77
  this.all = this.all.bind(this);
75
78
  this.not = this.not.bind(this);
@@ -88,7 +91,7 @@ class Query {
88
91
  * @returns A new Query object.
89
92
  */
90
93
  any(...queries) {
91
- 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 : {}; });
92
95
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $any } }, __classPrivateFieldGet(this, _Query_data, "f"));
93
96
  }
94
97
  /**
@@ -97,7 +100,7 @@ class Query {
97
100
  * @returns A new Query object.
98
101
  */
99
102
  all(...queries) {
100
- 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 : {}; });
101
104
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f"));
102
105
  }
103
106
  /**
@@ -106,7 +109,7 @@ class Query {
106
109
  * @returns A new Query object.
107
110
  */
108
111
  not(...queries) {
109
- 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 : {}; });
110
113
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $not } }, __classPrivateFieldGet(this, _Query_data, "f"));
111
114
  }
112
115
  /**
@@ -115,19 +118,18 @@ class Query {
115
118
  * @returns A new Query object.
116
119
  */
117
120
  none(...queries) {
118
- 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 : {}; });
119
122
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $none } }, __classPrivateFieldGet(this, _Query_data, "f"));
120
123
  }
121
124
  filter(a, b) {
125
+ var _a, _b;
122
126
  if (arguments.length === 1) {
123
127
  const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
124
- 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));
125
129
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f"));
126
130
  }
127
131
  else {
128
- const column = a;
129
- const value = b;
130
- 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 }]));
131
133
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f"));
132
134
  }
133
135
  }
@@ -138,7 +140,9 @@ class Query {
138
140
  * @returns A new Query object.
139
141
  */
140
142
  sort(column, direction) {
141
- 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 }];
142
146
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { sort }, __classPrivateFieldGet(this, _Query_data, "f"));
143
147
  }
144
148
  /**
@@ -150,7 +154,8 @@ class Query {
150
154
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { columns }, __classPrivateFieldGet(this, _Query_data, "f"));
151
155
  }
152
156
  getPaginated(options = {}) {
153
- return __classPrivateFieldGet(this, _Query_repository, "f").query(this, options);
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);
154
159
  }
155
160
  [(_Query_table = new WeakMap(), _Query_repository = new WeakMap(), _Query_data = new WeakMap(), Symbol.asyncIterator)]() {
156
161
  return __asyncGenerator(this, arguments, function* _a() {
@@ -176,38 +181,48 @@ class Query {
176
181
  let end = false;
177
182
  while (!end) {
178
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.
179
185
  yield yield __await(records);
180
186
  offset += chunk;
181
187
  end = !meta.page.more;
182
188
  }
183
189
  });
184
190
  }
185
- /**
186
- * Performs the query in the database and returns a set of results.
187
- * @param options Additional options to be used when performing the query.
188
- * @returns An array of records from the database.
189
- */
190
191
  getMany(options = {}) {
191
192
  return __awaiter(this, void 0, void 0, function* () {
192
193
  const { records } = yield this.getPaginated(options);
194
+ // Method overloading does not provide type inference for the return type.
193
195
  return records;
194
196
  });
195
197
  }
196
- /**
197
- * Performs the query in the database and returns the first result.
198
- * @param options Additional options to be used when performing the query.
199
- * @returns The first record that matches the query, or null if no record matched the query.
200
- */
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
+ }
201
219
  getOne(options = {}) {
202
220
  return __awaiter(this, void 0, void 0, function* () {
203
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.
204
223
  return records[0] || null;
205
224
  });
206
225
  }
207
- /**async deleteAll(): Promise<number> {
208
- // TODO: Return number of affected rows
209
- return 0;
210
- }**/
211
226
  nextPage(size, offset) {
212
227
  return this.firstPage(size, offset);
213
228
  }