@xata.io/client 0.0.0-beta.518ec9b → 0.0.0-beta.5f52401

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,39 +1,90 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
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;
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
74
  // TODO: these can only be applied to columns of type "multiple"
75
+ /**
76
+ * Operator to restrict results to only arrays that include the given value.
77
+ */
33
78
  const includes = (value) => ({ $includes: value });
34
79
  exports.includes = includes;
80
+ /**
81
+ * Operator to restrict results to only arrays that include a value matching the given substring.
82
+ */
35
83
  const includesSubstring = (value) => ({ $includesSubstring: value });
36
84
  exports.includesSubstring = includesSubstring;
85
+ /**
86
+ * Operator to restrict results to only arrays that include a value matching the given pattern.
87
+ */
37
88
  const includesPattern = (value) => ({ $includesPattern: value });
38
89
  exports.includesPattern = includesPattern;
39
90
  const includesAll = (value) => ({ $includesAll: value });
@@ -1,5 +1,5 @@
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;
@@ -15,15 +15,53 @@ export interface Paginable<T extends XataRecord, R extends XataRecord = T> {
15
15
  lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
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<T extends XataRecord, R extends XataRecord = T> implements Paginable<T, R> {
19
23
  #private;
24
+ /**
25
+ * Page metadata, required to retrieve additional records.
26
+ */
20
27
  readonly meta: PaginationQueryMeta;
28
+ /**
29
+ * The set of results for this page.
30
+ */
21
31
  readonly records: R[];
22
32
  constructor(query: Query<T, R>, meta: PaginationQueryMeta, records?: R[]);
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
+ */
23
39
  nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
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
+ */
24
46
  previousPage(size?: number, offset?: number): Promise<Page<T, R>>;
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
+ */
25
53
  firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
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
+ */
26
60
  lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
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,62 @@ 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
80
  // TODO: We need to add something on the backend if we want a hasPreviousPage
81
+ /**
82
+ * Shortcut method to check if there will be additional results if the next page of results is retrieved.
83
+ * @returns Whether or not there will be additional results in the next page of results.
84
+ */
53
85
  hasNextPage() {
54
86
  return this.meta.page.more;
55
87
  }
56
88
  }
57
89
  exports.Page = Page;
58
90
  _Page_query = new WeakMap();
91
+ exports.PAGINATION_MAX_SIZE = 200;
92
+ exports.PAGINATION_DEFAULT_SIZE = 200;
93
+ exports.PAGINATION_MAX_OFFSET = 800;
94
+ exports.PAGINATION_DEFAULT_OFFSET = 0;
@@ -1,8 +1,9 @@
1
- import { XataRecord, Repository } from '..';
2
- import { FilterExpression, SortExpression, PageConfig, ColumnsFilter } from '../api/schemas';
1
+ import { ColumnsFilter, FilterExpression, PageConfig, SortExpression } from '../api/schemas';
3
2
  import { DeepConstraint, FilterConstraints, SortDirection, SortFilter } from './filters';
4
- import { PaginationOptions, Page, Paginable, PaginationQueryMeta } from './pagination';
5
- import { Selectable, SelectableColumn, Select } from './selection';
3
+ import { Page, Paginable, PaginationOptions, PaginationQueryMeta } from './pagination';
4
+ import { XataRecord } from './record';
5
+ import { Repository } from './repository';
6
+ import { Select, Selectable, SelectableColumn } from './selection';
6
7
  export declare type QueryOptions<T extends XataRecord> = {
7
8
  page?: PaginationOptions;
8
9
  columns?: Extract<keyof Selectable<T>, string>[];
@@ -14,31 +15,95 @@ export declare type QueryTableOptions = {
14
15
  page?: PageConfig;
15
16
  columns?: ColumnsFilter;
16
17
  };
18
+ /**
19
+ * Query objects contain the information of all filters, sorting, etc. to be included in the database query.
20
+ *
21
+ * Query objects are immutable. Any method that adds more constraints or options to the query will return
22
+ * a new Query object containing the both the previous and the new constraints and options.
23
+ */
17
24
  export declare class Query<T extends XataRecord, R extends XataRecord = T> implements Paginable<T, R> {
18
25
  #private;
19
26
  readonly meta: PaginationQueryMeta;
20
27
  readonly records: R[];
21
28
  constructor(repository: Repository<T> | null, table: string, data: Partial<QueryTableOptions>, parent?: Partial<QueryTableOptions>);
22
29
  getQueryOptions(): QueryTableOptions;
30
+ /**
31
+ * Builds a new query object representing a logical OR between the given subqueries.
32
+ * @param queries An array of subqueries.
33
+ * @returns A new Query object.
34
+ */
23
35
  any(...queries: Query<T, R>[]): Query<T, R>;
36
+ /**
37
+ * Builds a new query object representing a logical AND between the given subqueries.
38
+ * @param queries An array of subqueries.
39
+ * @returns A new Query object.
40
+ */
24
41
  all(...queries: Query<T, R>[]): Query<T, R>;
42
+ /**
43
+ * Builds a new query object representing a logical OR negating each subquery. In pseudo-code: !q1 OR !q2
44
+ * @param queries An array of subqueries.
45
+ * @returns A new Query object.
46
+ */
25
47
  not(...queries: Query<T, R>[]): Query<T, R>;
48
+ /**
49
+ * Builds a new query object representing a logical AND negating each subquery. In pseudo-code: !q1 AND !q2
50
+ * @param queries An array of subqueries.
51
+ * @returns A new Query object.
52
+ */
26
53
  none(...queries: Query<T, R>[]): Query<T, R>;
54
+ /**
55
+ * Builds a new query object adding one or more constraints. Examples:
56
+ *
57
+ * ```
58
+ * query.filter("columnName", columnValue)
59
+ * query.filter({
60
+ * "columnName": columnValue
61
+ * })
62
+ * query.filter({
63
+ * "columnName": operator(columnValue) // Use gt, gte, lt, lte, startsWith,...
64
+ * })
65
+ * ```
66
+ *
67
+ * @param constraints
68
+ * @returns A new Query object.
69
+ */
27
70
  filter(constraints: FilterConstraints<T>): Query<T, R>;
28
71
  filter<F extends keyof Selectable<T>>(column: F, value: FilterConstraints<T[F]> | DeepConstraint<T[F]>): Query<T, R>;
72
+ /**
73
+ * Builds a new query with a new sort option.
74
+ * @param column The column name.
75
+ * @param direction The direction. Either ascending or descending.
76
+ * @returns A new Query object.
77
+ */
29
78
  sort<F extends keyof T>(column: F, direction: SortDirection): Query<T, R>;
79
+ /**
80
+ * Builds a new query specifying the set of columns to be returned in the query response.
81
+ * @param columns Array of column names to be returned by the query.
82
+ * @returns A new Query object.
83
+ */
30
84
  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 extends {
32
- columns: SelectableColumn<T>[];
33
- } ? Select<T, typeof options['columns'][number]> : R>>;
85
+ getPaginated<Options extends QueryOptions<T>>(options?: Options): Promise<Page<T, GetWithColumnOptions<T, R, typeof options>>>;
34
86
  [Symbol.asyncIterator](): AsyncIterableIterator<R>;
35
- getIterator(chunk: number, options?: Omit<QueryOptions<T>, 'page'>): AsyncGenerator<R[]>;
36
- getMany<Options extends QueryOptions<T>>(options?: Options): Promise<(typeof options extends {
37
- columns: SelectableColumn<T>[];
38
- } ? Select<T, typeof options['columns'][number]> : R)[]>;
39
- getOne<Options extends Omit<QueryOptions<T>, 'page'>>(options?: Options): Promise<(typeof options extends {
40
- columns: SelectableColumn<T>[];
41
- } ? Select<T, typeof options['columns'][number]> : R) | null>;
87
+ getIterator<Options extends QueryOptions<T>>(chunk: number, options?: Omit<Options, 'page'>): AsyncGenerator<GetWithColumnOptions<T, R, typeof options>[]>;
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<Options extends QueryOptions<T>>(options?: Options): Promise<GetWithColumnOptions<T, R, typeof options>[]>;
94
+ /**
95
+ * Performs the query in the database and returns all the results.
96
+ * Warning: If there are a large number of results, this method can have performance implications.
97
+ * @param options Additional options to be used when performing the query.
98
+ * @returns An array of records from the database.
99
+ */
100
+ getAll<Options extends QueryOptions<T>>(chunk?: number, options?: Omit<Options, 'page'>): Promise<GetWithColumnOptions<T, R, typeof options>[]>;
101
+ /**
102
+ * Performs the query in the database and returns the first result.
103
+ * @param options Additional options to be used when performing the query.
104
+ * @returns The first record that matches the query, or null if no record matched the query.
105
+ */
106
+ getOne<Options extends Omit<QueryOptions<T>, 'page'>>(options?: Options): Promise<GetWithColumnOptions<T, R, typeof options> | null>;
42
107
  /**async deleteAll(): Promise<number> {
43
108
  // TODO: Return number of affected rows
44
109
  return 0;
@@ -49,3 +114,16 @@ export declare class Query<T extends XataRecord, R extends XataRecord = T> imple
49
114
  lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
50
115
  hasNextPage(): boolean;
51
116
  }
117
+ /**
118
+ * Helper type to read options and compute the correct type for the result values
119
+ * T: Original type
120
+ * R: Default destination type
121
+ * Options: QueryOptions
122
+ *
123
+ * If the columns are overriden in the options, the result type is the pick of the original type and the columns
124
+ * If the columns are not overriden, the result type is the default destination type
125
+ */
126
+ declare type GetWithColumnOptions<T, R, Options> = Options extends {
127
+ columns: SelectableColumn<T>[];
128
+ } ? Select<T, Options['columns'][number]> : R;
129
+ export {};
@@ -42,6 +42,13 @@ 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
54
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
@@ -76,18 +83,38 @@ class Query {
76
83
  getQueryOptions() {
77
84
  return __classPrivateFieldGet(this, _Query_data, "f");
78
85
  }
86
+ /**
87
+ * Builds a new query object representing a logical OR between the given subqueries.
88
+ * @param queries An array of subqueries.
89
+ * @returns A new Query object.
90
+ */
79
91
  any(...queries) {
80
92
  const $any = queries.map((query) => query.getQueryOptions().filter);
81
93
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $any } }, __classPrivateFieldGet(this, _Query_data, "f"));
82
94
  }
95
+ /**
96
+ * Builds a new query object representing a logical AND between the given subqueries.
97
+ * @param queries An array of subqueries.
98
+ * @returns A new Query object.
99
+ */
83
100
  all(...queries) {
84
101
  const $all = queries.map((query) => query.getQueryOptions().filter);
85
102
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f"));
86
103
  }
104
+ /**
105
+ * Builds a new query object representing a logical OR negating each subquery. In pseudo-code: !q1 OR !q2
106
+ * @param queries An array of subqueries.
107
+ * @returns A new Query object.
108
+ */
87
109
  not(...queries) {
88
110
  const $not = queries.map((query) => query.getQueryOptions().filter);
89
111
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $not } }, __classPrivateFieldGet(this, _Query_data, "f"));
90
112
  }
113
+ /**
114
+ * Builds a new query object representing a logical AND negating each subquery. In pseudo-code: !q1 AND !q2
115
+ * @param queries An array of subqueries.
116
+ * @returns A new Query object.
117
+ */
91
118
  none(...queries) {
92
119
  const $none = queries.map((query) => query.getQueryOptions().filter);
93
120
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $none } }, __classPrivateFieldGet(this, _Query_data, "f"));
@@ -105,10 +132,21 @@ class Query {
105
132
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f"));
106
133
  }
107
134
  }
135
+ /**
136
+ * Builds a new query with a new sort option.
137
+ * @param column The column name.
138
+ * @param direction The direction. Either ascending or descending.
139
+ * @returns A new Query object.
140
+ */
108
141
  sort(column, direction) {
109
142
  const sort = Object.assign(Object.assign({}, __classPrivateFieldGet(this, _Query_data, "f").sort), { [column]: direction });
110
143
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { sort }, __classPrivateFieldGet(this, _Query_data, "f"));
111
144
  }
145
+ /**
146
+ * Builds a new query specifying the set of columns to be returned in the query response.
147
+ * @param columns Array of column names to be returned by the query.
148
+ * @returns A new Query object.
149
+ */
112
150
  select(columns) {
113
151
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { columns }, __classPrivateFieldGet(this, _Query_data, "f"));
114
152
  }
@@ -145,12 +183,48 @@ class Query {
145
183
  }
146
184
  });
147
185
  }
186
+ /**
187
+ * Performs the query in the database and returns a set of results.
188
+ * @param options Additional options to be used when performing the query.
189
+ * @returns An array of records from the database.
190
+ */
148
191
  getMany(options = {}) {
149
192
  return __awaiter(this, void 0, void 0, function* () {
150
193
  const { records } = yield this.getPaginated(options);
151
194
  return records;
152
195
  });
153
196
  }
197
+ /**
198
+ * Performs the query in the database and returns all the results.
199
+ * Warning: If there are a large number of results, this method can have performance implications.
200
+ * @param options Additional options to be used when performing the query.
201
+ * @returns An array of records from the database.
202
+ */
203
+ getAll(chunk = pagination_1.PAGINATION_MAX_SIZE, options = {}) {
204
+ var e_2, _a;
205
+ return __awaiter(this, void 0, void 0, function* () {
206
+ const results = [];
207
+ try {
208
+ for (var _b = __asyncValues(this.getIterator(chunk, options)), _c; _c = yield _b.next(), !_c.done;) {
209
+ const page = _c.value;
210
+ results.push(...page);
211
+ }
212
+ }
213
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
214
+ finally {
215
+ try {
216
+ if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
217
+ }
218
+ finally { if (e_2) throw e_2.error; }
219
+ }
220
+ return results;
221
+ });
222
+ }
223
+ /**
224
+ * Performs the query in the database and returns the first result.
225
+ * @param options Additional options to be used when performing the query.
226
+ * @returns The first record that matches the query, or null if no record matched the query.
227
+ */
154
228
  getOne(options = {}) {
155
229
  return __awaiter(this, void 0, void 0, function* () {
156
230
  const records = yield this.getMany(Object.assign(Object.assign({}, options), { page: { size: 1 } }));
@@ -1,10 +1,44 @@
1
1
  import { Selectable } from './selection';
2
- export interface XataRecord {
2
+ /**
3
+ * Represents an identifiable record from the database.
4
+ */
5
+ export interface Identifiable {
6
+ /**
7
+ * Unique id of this record.
8
+ */
3
9
  id: string;
10
+ }
11
+ export interface BaseData {
12
+ [key: string]: any;
13
+ }
14
+ /**
15
+ * Represents a persisted record from the database.
16
+ */
17
+ export interface XataRecord extends Identifiable {
18
+ /**
19
+ * Metadata of this record.
20
+ */
4
21
  xata: {
22
+ /**
23
+ * Number that is increased every time the record is updated.
24
+ */
5
25
  version: number;
6
26
  };
27
+ /**
28
+ * Retrieves a refreshed copy of the current record from the database.
29
+ */
7
30
  read(): Promise<this>;
31
+ /**
32
+ * Performs a partial update of the current record. On success a new object is
33
+ * returned and the current object is not mutated.
34
+ * @param data The columns and their values that have to be updated.
35
+ * @returns A new record containing the latest values for all the columns of the current record.
36
+ */
8
37
  update(data: Partial<Selectable<this>>): Promise<this>;
38
+ /**
39
+ * Performs a deletion of the current record in the database.
40
+ *
41
+ * @throws If the record was already deleted or if an error happened while performing the deletion.
42
+ */
9
43
  delete(): Promise<void>;
10
44
  }
@@ -1,51 +1,99 @@
1
1
  import { FetchImpl } from '../api/fetcher';
2
2
  import { Page } from './pagination';
3
3
  import { Query, QueryOptions } from './query';
4
- import { XataRecord } from './record';
5
- import { Selectable, SelectableColumn, Select } from './selection';
4
+ import { BaseData, Identifiable, XataRecord } from './record';
5
+ import { Select, SelectableColumn } from './selection';
6
6
  export declare type Links = Record<string, Array<string[]>>;
7
- export declare abstract class Repository<T extends XataRecord> extends Query<T> {
8
- abstract create(object: Selectable<T>): Promise<T>;
9
- abstract createMany(objects: Selectable<T>[]): Promise<T[]>;
10
- abstract read(id: string): Promise<T | null>;
11
- abstract update(id: string, object: Partial<Selectable<T>>): Promise<T>;
12
- abstract upsert(id: string, object: Selectable<T>): Promise<T>;
7
+ /**
8
+ * Common interface for performing operations on a table.
9
+ */
10
+ export declare abstract class Repository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record> {
11
+ abstract create(object: Data & Partial<Identifiable>): Promise<Record>;
12
+ /**
13
+ * Creates a single record in the table with a unique id.
14
+ * @param id The unique id.
15
+ * @param object Object containing the column names with their values to be stored in the table.
16
+ * @returns The full persisted record.
17
+ */
18
+ abstract create(id: string, object: Data): Promise<Record>;
19
+ /**
20
+ * Creates multiple records in the table.
21
+ * @param objects Array of objects with the column names and the values to be stored in the table.
22
+ * @returns Array of the persisted records.
23
+ */
24
+ abstract createMany(objects: Data[]): Promise<Record[]>;
25
+ /**
26
+ * Queries a single record from the table given its unique id.
27
+ * @param id The unique id.
28
+ * @returns The persisted record for the given id or null if the record could not be found.
29
+ */
30
+ abstract read(id: string): Promise<Record | null>;
31
+ /**
32
+ * Partially update a single record given its unique id.
33
+ * @param id The unique id.
34
+ * @param object The column names and their values that have to be updatd.
35
+ * @returns The full persisted record.
36
+ */
37
+ abstract update(id: string, object: Partial<Data>): Promise<Record>;
38
+ /**
39
+ * Creates or updates a single record. If a record exists with the given id,
40
+ * it will be update, otherwise a new record will be created.
41
+ * @param id A unique id.
42
+ * @param object The column names and the values to be persisted.
43
+ * @returns The full persisted record.
44
+ */
45
+ abstract createOrUpdate(id: string, object: Data): Promise<Record>;
46
+ /**
47
+ * Deletes a record given its unique id.
48
+ * @param id The unique id.
49
+ * @throws If the record could not be found or there was an error while performing the deletion.
50
+ */
13
51
  abstract delete(id: string): void;
14
- abstract query<R extends XataRecord, Options extends QueryOptions<T>>(query: Query<T, R>, options: Options): Promise<Page<T, typeof options extends {
15
- columns: SelectableColumn<T>[];
16
- } ? Select<T, typeof options['columns'][number]> : R>>;
52
+ abstract query<Result extends XataRecord, Options extends QueryOptions<Record>>(query: Query<Record, Result>, options: Options): Promise<Page<Record, typeof options extends {
53
+ columns: SelectableColumn<Data>[];
54
+ } ? Select<Data, typeof options['columns'][number]> : Result>>;
17
55
  }
18
- export declare class RestRepository<T extends XataRecord> extends Repository<T> {
56
+ export declare class RestRepository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Repository<Data, Record> {
19
57
  #private;
20
58
  constructor(client: BaseClient<any>, table: string);
21
- create(object: Selectable<T>): Promise<T>;
22
- createMany(objects: T[]): Promise<T[]>;
23
- read(recordId: string): Promise<T | null>;
24
- update(recordId: string, object: Partial<Selectable<T>>): Promise<T>;
25
- upsert(recordId: string, object: Selectable<T>): Promise<T>;
59
+ create(object: Data): Promise<Record>;
60
+ create(recordId: string, object: Data): Promise<Record>;
61
+ createMany(objects: Data[]): Promise<Record[]>;
62
+ read(recordId: string): Promise<Record | null>;
63
+ update(recordId: string, object: Partial<Data>): Promise<Record>;
64
+ createOrUpdate(recordId: string, object: Data): Promise<Record>;
26
65
  delete(recordId: string): Promise<void>;
27
- query<R extends XataRecord, Options extends QueryOptions<T>>(query: Query<T, R>, options?: Options): Promise<Page<T, typeof options extends {
28
- columns: SelectableColumn<T>[];
29
- } ? Select<T, typeof options['columns'][number]> : R>>;
66
+ query<Result extends XataRecord, Options extends QueryOptions<Record>>(query: Query<Record, Result>, options?: Options): Promise<Page<Record, typeof options extends {
67
+ columns: SelectableColumn<Data>[];
68
+ } ? Select<Data, typeof options['columns'][number]> : Result>>;
30
69
  }
31
70
  interface RepositoryFactory {
32
- createRepository<T extends XataRecord>(client: BaseClient<any>, table: string): Repository<T>;
71
+ createRepository<Data extends BaseData>(client: BaseClient<any>, table: string): Repository<Data>;
33
72
  }
34
73
  export declare class RestRespositoryFactory implements RepositoryFactory {
35
- createRepository<T extends XataRecord>(client: BaseClient<any>, table: string): Repository<T>;
74
+ createRepository<Data extends BaseData>(client: BaseClient<any>, table: string): Repository<Data>;
36
75
  }
37
76
  declare type BranchStrategyValue = string | undefined | null;
38
77
  declare type BranchStrategyBuilder = () => BranchStrategyValue | Promise<BranchStrategyValue>;
39
78
  declare type BranchStrategy = BranchStrategyValue | BranchStrategyBuilder;
40
79
  declare type BranchStrategyOption = NonNullable<BranchStrategy | BranchStrategy[]>;
41
80
  export declare type XataClientOptions = {
81
+ /**
82
+ * Fetch implementation. This option is only required if the runtime does not include a fetch implementation
83
+ * available in the global scope. If you are running your code on Deno or Cloudflare workers for example,
84
+ * you won't need to provide a specific fetch implementation. But for most versions of Node.js you'll need
85
+ * to provide one. Such as cross-fetch, node-fetch or isomorphic-fetch.
86
+ */
42
87
  fetch?: FetchImpl;
43
88
  databaseURL?: string;
44
89
  branch: BranchStrategyOption;
90
+ /**
91
+ * API key to be used. You can create one in your account settings at https://app.xata.io/settings.
92
+ */
45
93
  apiKey: string;
46
94
  repositoryFactory?: RepositoryFactory;
47
95
  };
48
- export declare class BaseClient<D extends Record<string, Repository<any>>> {
96
+ export declare class BaseClient<D extends Record<string, Repository<any>> = Record<string, Repository<any>>> {
49
97
  #private;
50
98
  options: XataClientOptions;
51
99
  db: D;