@xata.io/client 0.0.0-beta.914c21b → 0.0.0-beta.9559ec0

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.
@@ -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,52 @@
1
- import { XataRecord, Repository } from '..';
2
- import { FilterExpression, SortExpression, PageConfig, ColumnsFilter } from '../api/schemas';
1
+ import { FilterExpression } from '../api/schemas';
2
+ import { NonEmptyArray, RequiredBy } from '../util/types';
3
3
  import { DeepConstraint, FilterConstraints, SortDirection, SortFilter } from './filters';
4
- import { PaginationOptions, Page, Paginable, PaginationQueryMeta } from './pagination';
5
- import { Selectable, SelectableColumn, Select } from './selection';
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';
6
8
  export declare type QueryOptions<T extends XataRecord> = {
7
9
  page?: PaginationOptions;
8
- columns?: Extract<keyof Selectable<T>, string>[];
10
+ columns?: NonEmptyArray<SelectableColumn<T>>;
11
+ filter?: FilterExpression;
9
12
  sort?: SortFilter<T> | SortFilter<T>[];
10
13
  };
11
- export declare type QueryTableOptions = {
12
- filter: FilterExpression;
13
- sort?: SortExpression;
14
- page?: PageConfig;
15
- columns?: ColumnsFilter;
16
- };
17
14
  /**
18
15
  * Query objects contain the information of all filters, sorting, etc. to be included in the database query.
19
16
  *
20
17
  * Query objects are immutable. Any method that adds more constraints or options to the query will return
21
18
  * a new Query object containing the both the previous and the new constraints and options.
22
19
  */
23
- export declare class Query<T extends XataRecord, R extends XataRecord = T> implements Paginable<T, R> {
20
+ export declare class Query<Record extends XataRecord, Result extends XataRecord = Record> implements Paginable<Record, Result> {
24
21
  #private;
25
22
  readonly meta: PaginationQueryMeta;
26
- readonly records: R[];
27
- constructor(repository: Repository<T> | null, table: string, data: Partial<QueryTableOptions>, parent?: Partial<QueryTableOptions>);
28
- getQueryOptions(): QueryTableOptions;
23
+ readonly records: Result[];
24
+ constructor(repository: Repository<Record> | null, table: string, data: Partial<QueryOptions<Record>>, parent?: Partial<QueryOptions<Record>>);
25
+ getQueryOptions(): QueryOptions<Record>;
29
26
  /**
30
27
  * Builds a new query object representing a logical OR between the given subqueries.
31
28
  * @param queries An array of subqueries.
32
29
  * @returns A new Query object.
33
30
  */
34
- any(...queries: Query<T, R>[]): Query<T, R>;
31
+ any(...queries: Query<Record, Result>[]): Query<Record, Result>;
35
32
  /**
36
33
  * Builds a new query object representing a logical AND between the given subqueries.
37
34
  * @param queries An array of subqueries.
38
35
  * @returns A new Query object.
39
36
  */
40
- all(...queries: Query<T, R>[]): Query<T, R>;
37
+ all(...queries: Query<Record, Result>[]): Query<Record, Result>;
41
38
  /**
42
39
  * Builds a new query object representing a logical OR negating each subquery. In pseudo-code: !q1 OR !q2
43
40
  * @param queries An array of subqueries.
44
41
  * @returns A new Query object.
45
42
  */
46
- not(...queries: Query<T, R>[]): Query<T, R>;
43
+ not(...queries: Query<Record, Result>[]): Query<Record, Result>;
47
44
  /**
48
45
  * Builds a new query object representing a logical AND negating each subquery. In pseudo-code: !q1 AND !q2
49
46
  * @param queries An array of subqueries.
50
47
  * @returns A new Query object.
51
48
  */
52
- none(...queries: Query<T, R>[]): Query<T, R>;
49
+ none(...queries: Query<Record, Result>[]): Query<Record, Result>;
53
50
  /**
54
51
  * Builds a new query object adding one or more constraints. Examples:
55
52
  *
@@ -66,49 +63,56 @@ export declare class Query<T extends XataRecord, R extends XataRecord = T> imple
66
63
  * @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(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>;
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
  }
@@ -1,12 +1,20 @@
1
- import { Selectable } from './selection';
1
+ import { SelectedPick } from './selection';
2
2
  /**
3
- * Represents a persisted record from the database.
3
+ * Represents an identifiable record from the database.
4
4
  */
5
- export interface XataRecord {
5
+ export interface Identifiable {
6
6
  /**
7
7
  * Unique id of this record.
8
8
  */
9
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 {
10
18
  /**
11
19
  * Metadata of this record.
12
20
  */
@@ -19,14 +27,14 @@ export interface XataRecord {
19
27
  /**
20
28
  * Retrieves a refreshed copy of the current record from the database.
21
29
  */
22
- read(): Promise<this>;
30
+ read(): Promise<SelectedPick<this, ['*']> | null>;
23
31
  /**
24
32
  * Performs a partial update of the current record. On success a new object is
25
33
  * returned and the current object is not mutated.
26
34
  * @param data The columns and their values that have to be updated.
27
35
  * @returns A new record containing the latest values for all the columns of the current record.
28
36
  */
29
- update(data: Partial<Selectable<this>>): Promise<this>;
37
+ update(data: Partial<EditableData<Omit<this, keyof XataRecord>>>): Promise<SelectedPick<this, ['*']>>;
30
38
  /**
31
39
  * Performs a deletion of the current record in the database.
32
40
  *
@@ -34,3 +42,25 @@ export interface XataRecord {
34
42
  */
35
43
  delete(): Promise<void>;
36
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
+ };
@@ -1,2 +1,13 @@
1
1
  "use strict";
2
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;
@@ -1,74 +1,73 @@
1
1
  import { FetchImpl } from '../api/fetcher';
2
2
  import { Page } from './pagination';
3
- import { Query, QueryOptions } from './query';
4
- import { XataRecord } from './record';
5
- import { Selectable, SelectableColumn, Select } from './selection';
3
+ import { Query } from './query';
4
+ import { BaseData, EditableData, Identifiable, XataRecord } from './record';
5
+ import { SelectedPick } from './selection';
6
6
  export declare type Links = Record<string, Array<string[]>>;
7
7
  /**
8
8
  * Common interface for performing operations on a table.
9
9
  */
10
- export declare abstract class Repository<T extends XataRecord> extends Query<T> {
10
+ export declare abstract class Repository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record, SelectedPick<Record, ['*']>> {
11
+ abstract create(object: EditableData<Data> & Partial<Identifiable>): Promise<SelectedPick<Record, ['*']>>;
11
12
  /**
12
- * Creates a record in the table.
13
+ * Creates a single record in the table with a unique id.
14
+ * @param id The unique id.
13
15
  * @param object Object containing the column names with their values to be stored in the table.
14
16
  * @returns The full persisted record.
15
17
  */
16
- abstract create(object: Selectable<T>): Promise<T>;
18
+ abstract create(id: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
17
19
  /**
18
20
  * Creates multiple records in the table.
19
21
  * @param objects Array of objects with the column names and the values to be stored in the table.
20
22
  * @returns Array of the persisted records.
21
23
  */
22
- abstract createMany(objects: Selectable<T>[]): Promise<T[]>;
24
+ abstract createMany(objects: EditableData<Data>[]): Promise<SelectedPick<Record, ['*']>[]>;
23
25
  /**
24
26
  * Queries a single record from the table given its unique id.
25
27
  * @param id The unique id.
26
28
  * @returns The persisted record for the given id or null if the record could not be found.
27
29
  */
28
- abstract read(id: string): Promise<T | null>;
30
+ abstract read(id: string): Promise<SelectedPick<Record, ['*']> | null>;
29
31
  /**
30
32
  * Partially update a single record given its unique id.
31
33
  * @param id The unique id.
32
34
  * @param object The column names and their values that have to be updatd.
33
35
  * @returns The full persisted record.
34
36
  */
35
- abstract update(id: string, object: Partial<Selectable<T>>): Promise<T>;
37
+ abstract update(id: string, object: Partial<EditableData<Data>>): Promise<SelectedPick<Record, ['*']>>;
36
38
  /**
37
- * Updates or creates a single record. If a record exists with the given id,
39
+ * Creates or updates a single record. If a record exists with the given id,
38
40
  * it will be update, otherwise a new record will be created.
39
41
  * @param id A unique id.
40
42
  * @param object The column names and the values to be persisted.
41
43
  * @returns The full persisted record.
42
44
  */
43
- abstract upsert(id: string, object: Selectable<T>): Promise<T>;
45
+ abstract createOrUpdate(id: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
44
46
  /**
45
47
  * Deletes a record given its unique id.
46
48
  * @param id The unique id.
47
49
  * @throws If the record could not be found or there was an error while performing the deletion.
48
50
  */
49
51
  abstract delete(id: string): void;
50
- abstract query<R extends XataRecord, Options extends QueryOptions<T>>(query: Query<T, R>, options: Options): Promise<Page<T, typeof options extends {
51
- columns: SelectableColumn<T>[];
52
- } ? Select<T, typeof options['columns'][number]> : R>>;
52
+ abstract query<Result extends XataRecord>(query: Query<Record, Result>): Promise<Page<Record, Result>>;
53
53
  }
54
- export declare class RestRepository<T extends XataRecord> extends Repository<T> {
54
+ export declare class RestRepository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record, SelectedPick<Record, ['*']>> {
55
55
  #private;
56
56
  constructor(client: BaseClient<any>, table: string);
57
- create(object: Selectable<T>): Promise<T>;
58
- createMany(objects: T[]): Promise<T[]>;
59
- read(recordId: string): Promise<T | null>;
60
- update(recordId: string, object: Partial<Selectable<T>>): Promise<T>;
61
- upsert(recordId: string, object: Selectable<T>): Promise<T>;
57
+ create(object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
58
+ create(recordId: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
59
+ createMany(objects: EditableData<Data>[]): Promise<SelectedPick<Record, ['*']>[]>;
60
+ read(recordId: string): Promise<SelectedPick<Record, ['*']> | null>;
61
+ update(recordId: string, object: Partial<EditableData<Data>>): Promise<SelectedPick<Record, ['*']>>;
62
+ createOrUpdate(recordId: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
62
63
  delete(recordId: string): Promise<void>;
63
- query<R extends XataRecord, Options extends QueryOptions<T>>(query: Query<T, R>, options?: Options): Promise<Page<T, typeof options extends {
64
- columns: SelectableColumn<T>[];
65
- } ? Select<T, typeof options['columns'][number]> : R>>;
64
+ query<Result extends XataRecord>(query: Query<Record, Result>): Promise<Page<Record, Result>>;
66
65
  }
67
66
  interface RepositoryFactory {
68
- createRepository<T extends XataRecord>(client: BaseClient<any>, table: string): Repository<T>;
67
+ createRepository<Data extends BaseData>(client: BaseClient<any>, table: string): Repository<Data & XataRecord>;
69
68
  }
70
69
  export declare class RestRespositoryFactory implements RepositoryFactory {
71
- createRepository<T extends XataRecord>(client: BaseClient<any>, table: string): Repository<T>;
70
+ createRepository<Data extends BaseData>(client: BaseClient<any>, table: string): Repository<Data & XataRecord>;
72
71
  }
73
72
  declare type BranchStrategyValue = string | undefined | null;
74
73
  declare type BranchStrategyBuilder = () => BranchStrategyValue | Promise<BranchStrategyValue>;
@@ -90,7 +89,7 @@ export declare type XataClientOptions = {
90
89
  apiKey: string;
91
90
  repositoryFactory?: RepositoryFactory;
92
91
  };
93
- export declare class BaseClient<D extends Record<string, Repository<any>>> {
92
+ export declare class BaseClient<D extends Record<string, Repository<any>> = Record<string, Repository<any>>> {
94
93
  #private;
95
94
  options: XataClientOptions;
96
95
  db: D;