@xata.io/client 0.5.0 → 0.7.0
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.
- package/.eslintrc.cjs +13 -0
- package/CHANGELOG.md +33 -0
- package/dist/api/client.d.ts +2 -2
- package/dist/api/client.js +12 -9
- package/dist/api/components.d.ts +27 -31
- package/dist/api/components.js +27 -31
- package/dist/api/fetcher.d.ts +15 -0
- package/dist/api/fetcher.js +23 -22
- package/dist/api/providers.js +3 -2
- package/dist/api/responses.d.ts +6 -0
- package/dist/schema/config.d.ts +4 -0
- package/dist/schema/config.js +83 -0
- package/dist/schema/filters.d.ts +93 -17
- package/dist/schema/filters.js +0 -22
- package/dist/schema/filters.spec.d.ts +1 -0
- package/dist/schema/filters.spec.js +175 -0
- package/dist/schema/index.d.ts +1 -0
- package/dist/schema/index.js +4 -1
- package/dist/schema/operators.d.ts +17 -27
- package/dist/schema/operators.js +1 -14
- package/dist/schema/pagination.d.ts +13 -13
- package/dist/schema/pagination.js +0 -1
- package/dist/schema/query.d.ts +39 -50
- package/dist/schema/query.js +25 -37
- package/dist/schema/record.d.ts +25 -3
- package/dist/schema/record.js +11 -0
- package/dist/schema/repository.d.ts +79 -35
- package/dist/schema/repository.js +208 -111
- package/dist/schema/selection.d.ts +24 -11
- package/dist/schema/selection.spec.d.ts +1 -0
- package/dist/schema/selection.spec.js +203 -0
- package/dist/schema/sorting.d.ts +17 -0
- package/dist/schema/sorting.js +28 -0
- package/dist/schema/sorting.spec.d.ts +1 -0
- package/dist/schema/sorting.spec.js +9 -0
- package/dist/util/environment.d.ts +5 -0
- package/dist/util/environment.js +68 -0
- package/dist/util/fetch.d.ts +2 -0
- package/dist/util/fetch.js +13 -0
- package/dist/util/lang.d.ts +3 -0
- package/dist/util/lang.js +13 -1
- package/dist/util/types.d.ts +22 -1
- package/package.json +2 -2
package/dist/schema/query.d.ts
CHANGED
@@ -1,56 +1,53 @@
|
|
1
|
-
import {
|
2
|
-
import {
|
1
|
+
import { FilterExpression } from '../api/schemas';
|
2
|
+
import { NonEmptyArray, RequiredBy } from '../util/types';
|
3
|
+
import { Filter } from './filters';
|
3
4
|
import { Page, Paginable, PaginationOptions, PaginationQueryMeta } from './pagination';
|
4
5
|
import { XataRecord } from './record';
|
5
6
|
import { Repository } from './repository';
|
6
|
-
import {
|
7
|
+
import { SelectableColumn, SelectedPick, ValueAtColumn } from './selection';
|
8
|
+
import { SortDirection, SortFilter } from './sorting';
|
7
9
|
export declare type QueryOptions<T extends XataRecord> = {
|
8
10
|
page?: PaginationOptions;
|
9
|
-
columns?:
|
11
|
+
columns?: NonEmptyArray<SelectableColumn<T>>;
|
12
|
+
filter?: FilterExpression;
|
10
13
|
sort?: SortFilter<T> | SortFilter<T>[];
|
11
14
|
};
|
12
|
-
export declare type QueryTableOptions = {
|
13
|
-
filter: FilterExpression;
|
14
|
-
sort?: SortExpression;
|
15
|
-
page?: PageConfig;
|
16
|
-
columns?: ColumnsFilter;
|
17
|
-
};
|
18
15
|
/**
|
19
16
|
* Query objects contain the information of all filters, sorting, etc. to be included in the database query.
|
20
17
|
*
|
21
18
|
* Query objects are immutable. Any method that adds more constraints or options to the query will return
|
22
19
|
* a new Query object containing the both the previous and the new constraints and options.
|
23
20
|
*/
|
24
|
-
export declare class Query<
|
21
|
+
export declare class Query<Record extends XataRecord, Result extends XataRecord = Record> implements Paginable<Record, Result> {
|
25
22
|
#private;
|
26
23
|
readonly meta: PaginationQueryMeta;
|
27
|
-
readonly records:
|
28
|
-
constructor(repository: Repository<
|
29
|
-
getQueryOptions():
|
24
|
+
readonly records: Result[];
|
25
|
+
constructor(repository: Repository<Record> | null, table: string, data: Partial<QueryOptions<Record>>, parent?: Partial<QueryOptions<Record>>);
|
26
|
+
getQueryOptions(): QueryOptions<Record>;
|
30
27
|
/**
|
31
28
|
* Builds a new query object representing a logical OR between the given subqueries.
|
32
29
|
* @param queries An array of subqueries.
|
33
30
|
* @returns A new Query object.
|
34
31
|
*/
|
35
|
-
any(...queries: Query<
|
32
|
+
any(...queries: Query<Record, any>[]): Query<Record, Result>;
|
36
33
|
/**
|
37
34
|
* Builds a new query object representing a logical AND between the given subqueries.
|
38
35
|
* @param queries An array of subqueries.
|
39
36
|
* @returns A new Query object.
|
40
37
|
*/
|
41
|
-
all(...queries: Query<
|
38
|
+
all(...queries: Query<Record, any>[]): Query<Record, Result>;
|
42
39
|
/**
|
43
40
|
* Builds a new query object representing a logical OR negating each subquery. In pseudo-code: !q1 OR !q2
|
44
41
|
* @param queries An array of subqueries.
|
45
42
|
* @returns A new Query object.
|
46
43
|
*/
|
47
|
-
not(...queries: Query<
|
44
|
+
not(...queries: Query<Record, any>[]): Query<Record, Result>;
|
48
45
|
/**
|
49
46
|
* Builds a new query object representing a logical AND negating each subquery. In pseudo-code: !q1 AND !q2
|
50
47
|
* @param queries An array of subqueries.
|
51
48
|
* @returns A new Query object.
|
52
49
|
*/
|
53
|
-
none(...queries: Query<
|
50
|
+
none(...queries: Query<Record, any>[]): Query<Record, Result>;
|
54
51
|
/**
|
55
52
|
* Builds a new query object adding one or more constraints. Examples:
|
56
53
|
*
|
@@ -64,66 +61,58 @@ export declare class Query<T extends XataRecord, R extends XataRecord = T> imple
|
|
64
61
|
* })
|
65
62
|
* ```
|
66
63
|
*
|
67
|
-
* @param constraints
|
68
64
|
* @returns A new Query object.
|
69
65
|
*/
|
70
|
-
filter(
|
71
|
-
filter<F extends
|
66
|
+
filter(filters: Filter<Record>): Query<Record, Result>;
|
67
|
+
filter<F extends SelectableColumn<Record>>(column: F, value: Filter<ValueAtColumn<Record, F>>): Query<Record, Result>;
|
72
68
|
/**
|
73
69
|
* Builds a new query with a new sort option.
|
74
70
|
* @param column The column name.
|
75
71
|
* @param direction The direction. Either ascending or descending.
|
76
72
|
* @returns A new Query object.
|
77
73
|
*/
|
78
|
-
sort<F extends
|
74
|
+
sort<F extends SelectableColumn<Record>>(column: F, direction: SortDirection): Query<Record, Result>;
|
79
75
|
/**
|
80
76
|
* Builds a new query specifying the set of columns to be returned in the query response.
|
81
77
|
* @param columns Array of column names to be returned by the query.
|
82
78
|
* @returns A new Query object.
|
83
79
|
*/
|
84
|
-
select<K extends SelectableColumn<
|
85
|
-
getPaginated
|
86
|
-
|
87
|
-
|
80
|
+
select<K extends SelectableColumn<Record>>(columns: NonEmptyArray<K>): Query<Record, SelectedPick<Record, NonEmptyArray<K>>>;
|
81
|
+
getPaginated(): Promise<Page<Record, Result>>;
|
82
|
+
getPaginated(options: Omit<QueryOptions<Record>, 'columns'>): Promise<Page<Record, Result>>;
|
83
|
+
getPaginated<Options extends RequiredBy<QueryOptions<Record>, 'columns'>>(options: Options): Promise<Page<Record, SelectedPick<Record, typeof options['columns']>>>;
|
84
|
+
[Symbol.asyncIterator](): AsyncIterableIterator<Result>;
|
85
|
+
getIterator(chunk: number): AsyncGenerator<Result[]>;
|
86
|
+
getIterator(chunk: number, options: Omit<QueryOptions<Record>, 'columns' | 'page'>): AsyncGenerator<Result[]>;
|
87
|
+
getIterator<Options extends RequiredBy<Omit<QueryOptions<Record>, 'page'>, 'columns'>>(chunk: number, options: Options): AsyncGenerator<SelectedPick<Record, typeof options['columns']>[]>;
|
88
88
|
/**
|
89
89
|
* Performs the query in the database and returns a set of results.
|
90
90
|
* @param options Additional options to be used when performing the query.
|
91
91
|
* @returns An array of records from the database.
|
92
92
|
*/
|
93
|
-
getMany
|
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']>[]>;
|
94
96
|
/**
|
95
97
|
* Performs the query in the database and returns all the results.
|
96
98
|
* Warning: If there are a large number of results, this method can have performance implications.
|
97
99
|
* @param options Additional options to be used when performing the query.
|
98
100
|
* @returns An array of records from the database.
|
99
101
|
*/
|
100
|
-
getAll
|
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']>[]>;
|
101
105
|
/**
|
102
106
|
* Performs the query in the database and returns the first result.
|
103
107
|
* @param options Additional options to be used when performing the query.
|
104
108
|
* @returns The first record that matches the query, or null if no record matched the query.
|
105
109
|
*/
|
106
|
-
getOne
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
114
|
-
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>>;
|
115
117
|
hasNextPage(): boolean;
|
116
118
|
}
|
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 {};
|
package/dist/schema/query.js
CHANGED
@@ -51,7 +51,7 @@ const pagination_1 = require("./pagination");
|
|
51
51
|
*/
|
52
52
|
class Query {
|
53
53
|
constructor(repository, table, data, parent) {
|
54
|
-
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;
|
55
55
|
_Query_table.set(this, void 0);
|
56
56
|
_Query_repository.set(this, void 0);
|
57
57
|
_Query_data.set(this, { filter: {} });
|
@@ -65,12 +65,14 @@ class Query {
|
|
65
65
|
else {
|
66
66
|
__classPrivateFieldSet(this, _Query_repository, this, "f");
|
67
67
|
}
|
68
|
-
__classPrivateFieldGet(this, _Query_data, "f").filter
|
69
|
-
__classPrivateFieldGet(this, _Query_data, "f").filter.$
|
70
|
-
__classPrivateFieldGet(this, _Query_data, "f").filter.$
|
71
|
-
__classPrivateFieldGet(this, _Query_data, "f").filter.$
|
72
|
-
__classPrivateFieldGet(this, _Query_data, "f").
|
73
|
-
__classPrivateFieldGet(this, _Query_data, "f").
|
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;
|
74
76
|
this.any = this.any.bind(this);
|
75
77
|
this.all = this.all.bind(this);
|
76
78
|
this.not = this.not.bind(this);
|
@@ -89,7 +91,7 @@ class Query {
|
|
89
91
|
* @returns A new Query object.
|
90
92
|
*/
|
91
93
|
any(...queries) {
|
92
|
-
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 : {}; });
|
93
95
|
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $any } }, __classPrivateFieldGet(this, _Query_data, "f"));
|
94
96
|
}
|
95
97
|
/**
|
@@ -98,7 +100,7 @@ class Query {
|
|
98
100
|
* @returns A new Query object.
|
99
101
|
*/
|
100
102
|
all(...queries) {
|
101
|
-
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 : {}; });
|
102
104
|
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f"));
|
103
105
|
}
|
104
106
|
/**
|
@@ -107,7 +109,7 @@ class Query {
|
|
107
109
|
* @returns A new Query object.
|
108
110
|
*/
|
109
111
|
not(...queries) {
|
110
|
-
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 : {}; });
|
111
113
|
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $not } }, __classPrivateFieldGet(this, _Query_data, "f"));
|
112
114
|
}
|
113
115
|
/**
|
@@ -116,19 +118,18 @@ class Query {
|
|
116
118
|
* @returns A new Query object.
|
117
119
|
*/
|
118
120
|
none(...queries) {
|
119
|
-
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 : {}; });
|
120
122
|
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $none } }, __classPrivateFieldGet(this, _Query_data, "f"));
|
121
123
|
}
|
122
124
|
filter(a, b) {
|
125
|
+
var _a, _b;
|
123
126
|
if (arguments.length === 1) {
|
124
127
|
const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
|
125
|
-
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));
|
126
129
|
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f"));
|
127
130
|
}
|
128
131
|
else {
|
129
|
-
const
|
130
|
-
const value = b;
|
131
|
-
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 }]));
|
132
133
|
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f"));
|
133
134
|
}
|
134
135
|
}
|
@@ -139,7 +140,9 @@ class Query {
|
|
139
140
|
* @returns A new Query object.
|
140
141
|
*/
|
141
142
|
sort(column, direction) {
|
142
|
-
|
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 }];
|
143
146
|
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { sort }, __classPrivateFieldGet(this, _Query_data, "f"));
|
144
147
|
}
|
145
148
|
/**
|
@@ -151,7 +154,8 @@ class Query {
|
|
151
154
|
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { columns }, __classPrivateFieldGet(this, _Query_data, "f"));
|
152
155
|
}
|
153
156
|
getPaginated(options = {}) {
|
154
|
-
|
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);
|
155
159
|
}
|
156
160
|
[(_Query_table = new WeakMap(), _Query_repository = new WeakMap(), _Query_data = new WeakMap(), Symbol.asyncIterator)]() {
|
157
161
|
return __asyncGenerator(this, arguments, function* _a() {
|
@@ -177,29 +181,20 @@ class Query {
|
|
177
181
|
let end = false;
|
178
182
|
while (!end) {
|
179
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.
|
180
185
|
yield yield __await(records);
|
181
186
|
offset += chunk;
|
182
187
|
end = !meta.page.more;
|
183
188
|
}
|
184
189
|
});
|
185
190
|
}
|
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
|
-
*/
|
191
191
|
getMany(options = {}) {
|
192
192
|
return __awaiter(this, void 0, void 0, function* () {
|
193
193
|
const { records } = yield this.getPaginated(options);
|
194
|
+
// Method overloading does not provide type inference for the return type.
|
194
195
|
return records;
|
195
196
|
});
|
196
197
|
}
|
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
198
|
getAll(chunk = pagination_1.PAGINATION_MAX_SIZE, options = {}) {
|
204
199
|
var e_2, _a;
|
205
200
|
return __awaiter(this, void 0, void 0, function* () {
|
@@ -217,24 +212,17 @@ class Query {
|
|
217
212
|
}
|
218
213
|
finally { if (e_2) throw e_2.error; }
|
219
214
|
}
|
215
|
+
// Method overloading does not provide type inference for the return type.
|
220
216
|
return results;
|
221
217
|
});
|
222
218
|
}
|
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
|
-
*/
|
228
219
|
getOne(options = {}) {
|
229
220
|
return __awaiter(this, void 0, void 0, function* () {
|
230
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.
|
231
223
|
return records[0] || null;
|
232
224
|
});
|
233
225
|
}
|
234
|
-
/**async deleteAll(): Promise<number> {
|
235
|
-
// TODO: Return number of affected rows
|
236
|
-
return 0;
|
237
|
-
}**/
|
238
226
|
nextPage(size, offset) {
|
239
227
|
return this.firstPage(size, offset);
|
240
228
|
}
|
package/dist/schema/record.d.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import {
|
1
|
+
import { SelectedPick } from './selection';
|
2
2
|
/**
|
3
3
|
* Represents an identifiable record from the database.
|
4
4
|
*/
|
@@ -27,14 +27,14 @@ export interface XataRecord extends Identifiable {
|
|
27
27
|
/**
|
28
28
|
* Retrieves a refreshed copy of the current record from the database.
|
29
29
|
*/
|
30
|
-
read(): Promise<this>;
|
30
|
+
read(): Promise<SelectedPick<this, ['*']> | null>;
|
31
31
|
/**
|
32
32
|
* Performs a partial update of the current record. On success a new object is
|
33
33
|
* returned and the current object is not mutated.
|
34
34
|
* @param data The columns and their values that have to be updated.
|
35
35
|
* @returns A new record containing the latest values for all the columns of the current record.
|
36
36
|
*/
|
37
|
-
update(data: Partial<
|
37
|
+
update(data: Partial<EditableData<Omit<this, keyof XataRecord>>>): Promise<SelectedPick<this, ['*']>>;
|
38
38
|
/**
|
39
39
|
* Performs a deletion of the current record in the database.
|
40
40
|
*
|
@@ -42,3 +42,25 @@ export interface XataRecord extends Identifiable {
|
|
42
42
|
*/
|
43
43
|
delete(): Promise<void>;
|
44
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
|
+
};
|
package/dist/schema/record.js
CHANGED
@@ -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,77 +1,121 @@
|
|
1
1
|
import { FetchImpl } from '../api/fetcher';
|
2
2
|
import { Page } from './pagination';
|
3
|
-
import { Query
|
4
|
-
import { BaseData, XataRecord } from './record';
|
5
|
-
import {
|
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<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record
|
11
|
-
abstract create(object: Data): Promise<Record
|
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, ['*']>>;
|
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: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
|
12
19
|
/**
|
13
20
|
* Creates multiple records in the table.
|
14
21
|
* @param objects Array of objects with the column names and the values to be stored in the table.
|
15
22
|
* @returns Array of the persisted records.
|
16
23
|
*/
|
17
|
-
abstract
|
24
|
+
abstract create(objects: Array<EditableData<Data> & Partial<Identifiable>>): Promise<SelectedPick<Record, ['*']>[]>;
|
18
25
|
/**
|
19
26
|
* Queries a single record from the table given its unique id.
|
20
27
|
* @param id The unique id.
|
21
28
|
* @returns The persisted record for the given id or null if the record could not be found.
|
22
29
|
*/
|
23
|
-
abstract read(id: string): Promise<Record | null>;
|
30
|
+
abstract read(id: string): Promise<SelectedPick<Record, ['*']> | null>;
|
24
31
|
/**
|
25
|
-
*
|
26
|
-
* @param id
|
27
|
-
* @param object Object containing the column names with their values to be stored in the table.
|
32
|
+
* Partially update a single record.
|
33
|
+
* @param object An object with its id and the columns to be updated.
|
28
34
|
* @returns The full persisted record.
|
29
35
|
*/
|
30
|
-
abstract
|
36
|
+
abstract update(object: Partial<EditableData<Data>> & Identifiable): Promise<SelectedPick<Record, ['*']>>;
|
31
37
|
/**
|
32
38
|
* Partially update a single record given its unique id.
|
33
39
|
* @param id The unique id.
|
34
|
-
* @param object The column names and their values that have to be
|
40
|
+
* @param object The column names and their values that have to be updated.
|
35
41
|
* @returns The full persisted record.
|
36
42
|
*/
|
37
|
-
abstract update(id: string, object: Partial<Data
|
43
|
+
abstract update(id: string, object: Partial<EditableData<Data>>): Promise<SelectedPick<Record, ['*']>>;
|
38
44
|
/**
|
39
|
-
*
|
45
|
+
* Partially updates multiple records.
|
46
|
+
* @param objects An array of objects with their ids and columns to be updated.
|
47
|
+
* @returns Array of the persisted records.
|
48
|
+
*/
|
49
|
+
abstract update(objects: Array<Partial<EditableData<Data>> & Identifiable>): Promise<SelectedPick<Record, ['*']>[]>;
|
50
|
+
/**
|
51
|
+
* Creates or updates a single record. If a record exists with the given id,
|
52
|
+
* it will be update, otherwise a new record will be created.
|
53
|
+
* @param object Object containing the column names with their values to be persisted in the table.
|
54
|
+
* @returns The full persisted record.
|
55
|
+
*/
|
56
|
+
abstract createOrUpdate(object: EditableData<Data> & Identifiable): Promise<SelectedPick<Record, ['*']>>;
|
57
|
+
/**
|
58
|
+
* Creates or updates a single record. If a record exists with the given id,
|
40
59
|
* it will be update, otherwise a new record will be created.
|
41
60
|
* @param id A unique id.
|
42
61
|
* @param object The column names and the values to be persisted.
|
43
62
|
* @returns The full persisted record.
|
44
63
|
*/
|
45
|
-
abstract
|
64
|
+
abstract createOrUpdate(id: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
|
65
|
+
/**
|
66
|
+
* Creates or updates a single record. If a record exists with the given id,
|
67
|
+
* it will be update, otherwise a new record will be created.
|
68
|
+
* @param objects Array of objects with the column names and the values to be stored in the table.
|
69
|
+
* @returns Array of the persisted records.
|
70
|
+
*/
|
71
|
+
abstract createOrUpdate(objects: Array<EditableData<Data> & Identifiable>): Promise<SelectedPick<Record, ['*']>[]>;
|
46
72
|
/**
|
47
73
|
* Deletes a record given its unique id.
|
48
74
|
* @param id The unique id.
|
49
75
|
* @throws If the record could not be found or there was an error while performing the deletion.
|
50
76
|
*/
|
51
|
-
abstract delete(id: string): void
|
52
|
-
|
53
|
-
|
54
|
-
|
77
|
+
abstract delete(id: string): Promise<void>;
|
78
|
+
/**
|
79
|
+
* Deletes a record given its unique id.
|
80
|
+
* @param id An object with a unique id.
|
81
|
+
* @throws If the record could not be found or there was an error while performing the deletion.
|
82
|
+
*/
|
83
|
+
abstract delete(id: Identifiable): Promise<void>;
|
84
|
+
/**
|
85
|
+
* Deletes a record given a list of unique ids.
|
86
|
+
* @param ids The array of unique ids.
|
87
|
+
* @throws If the record could not be found or there was an error while performing the deletion.
|
88
|
+
*/
|
89
|
+
abstract delete(ids: string[]): Promise<void>;
|
90
|
+
/**
|
91
|
+
* Deletes a record given a list of unique ids.
|
92
|
+
* @param ids An array of objects with unique ids.
|
93
|
+
* @throws If the record could not be found or there was an error while performing the deletion.
|
94
|
+
*/
|
95
|
+
abstract delete(ids: Identifiable[]): Promise<void>;
|
96
|
+
abstract query<Result extends XataRecord>(query: Query<Record, Result>): Promise<Page<Record, Result>>;
|
55
97
|
}
|
56
|
-
export declare class RestRepository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends
|
98
|
+
export declare class RestRepository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record, SelectedPick<Record, ['*']>> {
|
57
99
|
#private;
|
58
100
|
constructor(client: BaseClient<any>, table: string);
|
59
|
-
create(object: Data): Promise<Record
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
101
|
+
create(object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
|
102
|
+
create(recordId: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
|
103
|
+
create(objects: EditableData<Data>[]): Promise<SelectedPick<Record, ['*']>[]>;
|
104
|
+
read(recordId: string): Promise<SelectedPick<Record, ['*']> | null>;
|
105
|
+
update(object: Partial<EditableData<Data>> & Identifiable): Promise<SelectedPick<Record, ['*']>>;
|
106
|
+
update(recordId: string, object: Partial<EditableData<Data>>): Promise<SelectedPick<Record, ['*']>>;
|
107
|
+
update(objects: Array<Partial<EditableData<Data>> & Identifiable>): Promise<SelectedPick<Record, ['*']>[]>;
|
108
|
+
createOrUpdate(object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
|
109
|
+
createOrUpdate(recordId: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
|
110
|
+
createOrUpdate(objects: EditableData<Data>[]): Promise<SelectedPick<Record, ['*']>[]>;
|
111
|
+
delete(recordId: string | Identifiable | Array<string | Identifiable>): Promise<void>;
|
112
|
+
query<Result extends XataRecord>(query: Query<Record, Result>): Promise<Page<Record, Result>>;
|
69
113
|
}
|
70
114
|
interface RepositoryFactory {
|
71
|
-
createRepository<Data extends BaseData>(client: BaseClient<any>, table: string): Repository<Data>;
|
115
|
+
createRepository<Data extends BaseData>(client: BaseClient<any>, table: string): Repository<Data & XataRecord>;
|
72
116
|
}
|
73
117
|
export declare class RestRespositoryFactory implements RepositoryFactory {
|
74
|
-
createRepository<Data extends BaseData>(client: BaseClient<any>, table: string): Repository<Data>;
|
118
|
+
createRepository<Data extends BaseData>(client: BaseClient<any>, table: string): Repository<Data & XataRecord>;
|
75
119
|
}
|
76
120
|
declare type BranchStrategyValue = string | undefined | null;
|
77
121
|
declare type BranchStrategyBuilder = () => BranchStrategyValue | Promise<BranchStrategyValue>;
|
@@ -86,18 +130,18 @@ export declare type XataClientOptions = {
|
|
86
130
|
*/
|
87
131
|
fetch?: FetchImpl;
|
88
132
|
databaseURL?: string;
|
89
|
-
branch
|
133
|
+
branch?: BranchStrategyOption;
|
90
134
|
/**
|
91
135
|
* API key to be used. You can create one in your account settings at https://app.xata.io/settings.
|
92
136
|
*/
|
93
|
-
apiKey
|
137
|
+
apiKey?: string;
|
94
138
|
repositoryFactory?: RepositoryFactory;
|
95
139
|
};
|
96
140
|
export declare class BaseClient<D extends Record<string, Repository<any>> = Record<string, Repository<any>>> {
|
97
141
|
#private;
|
98
142
|
options: XataClientOptions;
|
99
143
|
db: D;
|
100
|
-
constructor(options
|
144
|
+
constructor(options?: XataClientOptions, links?: Links);
|
101
145
|
initObject<T>(table: string, object: object): T;
|
102
146
|
getBranch(): Promise<string>;
|
103
147
|
}
|