@xata.io/client 0.0.0-beta.642c07f → 0.0.0-beta.645c53a

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,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,82 +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>;
29
- /**
30
- * Insert a single record with a unique id.
31
- * @param id The unique id.
32
- * @param object Object containing the column names with their values to be stored in the table.
33
- * @returns The full persisted record.
34
- */
35
- abstract insert(id: string, object: Selectable<T>): Promise<T>;
30
+ abstract read(id: string): Promise<SelectedPick<Record, ['*']> | null>;
36
31
  /**
37
32
  * Partially update a single record given its unique id.
38
33
  * @param id The unique id.
39
34
  * @param object The column names and their values that have to be updatd.
40
35
  * @returns The full persisted record.
41
36
  */
42
- abstract update(id: string, object: Partial<Selectable<T>>): Promise<T>;
37
+ abstract update(id: string, object: Partial<EditableData<Data>>): Promise<SelectedPick<Record, ['*']>>;
43
38
  /**
44
- * Updates or inserts 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,
45
40
  * it will be update, otherwise a new record will be created.
46
41
  * @param id A unique id.
47
42
  * @param object The column names and the values to be persisted.
48
43
  * @returns The full persisted record.
49
44
  */
50
- abstract updateOrInsert(id: string, object: Selectable<T>): Promise<T>;
45
+ abstract createOrUpdate(id: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
51
46
  /**
52
47
  * Deletes a record given its unique id.
53
48
  * @param id The unique id.
54
49
  * @throws If the record could not be found or there was an error while performing the deletion.
55
50
  */
56
51
  abstract delete(id: string): void;
57
- abstract query<R extends XataRecord, Options extends QueryOptions<T>>(query: Query<T, R>, options: Options): Promise<Page<T, typeof options extends {
58
- columns: SelectableColumn<T>[];
59
- } ? Select<T, typeof options['columns'][number]> : R>>;
52
+ abstract query<Result extends XataRecord>(query: Query<Record, Result>): Promise<Page<Record, Result>>;
60
53
  }
61
- 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, ['*']>> {
62
55
  #private;
63
56
  constructor(client: BaseClient<any>, table: string);
64
- create(object: Selectable<T>): Promise<T>;
65
- createMany(objects: T[]): Promise<T[]>;
66
- read(recordId: string): Promise<T | null>;
67
- update(recordId: string, object: Partial<Selectable<T>>): Promise<T>;
68
- insert(recordId: string, object: Selectable<T>): Promise<T>;
69
- updateOrInsert(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, ['*']>>;
70
63
  delete(recordId: string): Promise<void>;
71
- query<R extends XataRecord, Options extends QueryOptions<T>>(query: Query<T, R>, options?: Options): Promise<Page<T, typeof options extends {
72
- columns: SelectableColumn<T>[];
73
- } ? Select<T, typeof options['columns'][number]> : R>>;
64
+ query<Result extends XataRecord>(query: Query<Record, Result>): Promise<Page<Record, Result>>;
74
65
  }
75
66
  interface RepositoryFactory {
76
- createRepository<T extends XataRecord>(client: BaseClient<any>, table: string): Repository<T>;
67
+ createRepository<Data extends BaseData>(client: BaseClient<any>, table: string): Repository<Data & XataRecord>;
77
68
  }
78
69
  export declare class RestRespositoryFactory implements RepositoryFactory {
79
- createRepository<T extends XataRecord>(client: BaseClient<any>, table: string): Repository<T>;
70
+ createRepository<Data extends BaseData>(client: BaseClient<any>, table: string): Repository<Data & XataRecord>;
80
71
  }
81
72
  declare type BranchStrategyValue = string | undefined | null;
82
73
  declare type BranchStrategyBuilder = () => BranchStrategyValue | Promise<BranchStrategyValue>;
@@ -98,7 +89,7 @@ export declare type XataClientOptions = {
98
89
  apiKey: string;
99
90
  repositoryFactory?: RepositoryFactory;
100
91
  };
101
- 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>>> {
102
93
  #private;
103
94
  options: XataClientOptions;
104
95
  db: D;