@xata.io/client 0.0.0-alpha.fc0a730 → 0.0.0-beta.036d273
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 +67 -0
- package/dist/api/client.d.ts +2 -2
- package/dist/api/client.js +33 -17
- package/dist/api/components.d.ts +34 -37
- package/dist/api/components.js +34 -37
- 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/index.d.ts +1 -0
- package/dist/index.js +1 -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 +4 -2
- package/dist/schema/index.js +8 -4
- package/dist/schema/operators.d.ts +26 -24
- package/dist/schema/operators.js +13 -11
- package/dist/schema/pagination.d.ts +18 -14
- package/dist/schema/pagination.js +5 -2
- package/dist/schema/query.d.ts +42 -52
- package/dist/schema/query.js +28 -39
- package/dist/schema/record.d.ts +28 -3
- package/dist/schema/record.js +11 -0
- package/dist/schema/repository.d.ts +98 -42
- package/dist/schema/repository.js +273 -156
- package/dist/schema/selection.d.ts +24 -13
- 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/config.d.ts +11 -0
- package/dist/util/config.js +121 -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 +23 -1
- package/package.json +5 -2
package/dist/schema/query.d.ts
CHANGED
@@ -1,55 +1,53 @@
|
|
1
|
-
import {
|
2
|
-
import {
|
3
|
-
import {
|
4
|
-
import {
|
5
|
-
import {
|
1
|
+
import { FilterExpression } from '../api/schemas';
|
2
|
+
import { NonEmptyArray, RequiredBy } from '../util/types';
|
3
|
+
import { Filter } from './filters';
|
4
|
+
import { Page, Paginable, PaginationOptions, PaginationQueryMeta } from './pagination';
|
5
|
+
import { XataRecord } from './record';
|
6
|
+
import { Repository } from './repository';
|
7
|
+
import { SelectableColumn, SelectedPick, ValueAtColumn } from './selection';
|
8
|
+
import { SortDirection, SortFilter } from './sorting';
|
6
9
|
export declare type QueryOptions<T extends XataRecord> = {
|
7
10
|
page?: PaginationOptions;
|
8
|
-
columns?:
|
11
|
+
columns?: NonEmptyArray<SelectableColumn<T>>;
|
12
|
+
filter?: FilterExpression;
|
9
13
|
sort?: SortFilter<T> | SortFilter<T>[];
|
10
14
|
};
|
11
|
-
export declare type QueryTableOptions = {
|
12
|
-
filter: FilterExpression;
|
13
|
-
sort?: SortExpression;
|
14
|
-
page?: PageConfig;
|
15
|
-
columns?: ColumnsFilter;
|
16
|
-
};
|
17
15
|
/**
|
18
16
|
* Query objects contain the information of all filters, sorting, etc. to be included in the database query.
|
19
17
|
*
|
20
18
|
* Query objects are immutable. Any method that adds more constraints or options to the query will return
|
21
19
|
* a new Query object containing the both the previous and the new constraints and options.
|
22
20
|
*/
|
23
|
-
export declare class Query<
|
21
|
+
export declare class Query<Record extends XataRecord, Result extends XataRecord = Record> implements Paginable<Record, Result> {
|
24
22
|
#private;
|
25
23
|
readonly meta: PaginationQueryMeta;
|
26
|
-
readonly records:
|
27
|
-
constructor(repository: Repository<
|
28
|
-
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>;
|
29
27
|
/**
|
30
28
|
* Builds a new query object representing a logical OR between the given subqueries.
|
31
29
|
* @param queries An array of subqueries.
|
32
30
|
* @returns A new Query object.
|
33
31
|
*/
|
34
|
-
any(...queries: Query<
|
32
|
+
any(...queries: Query<Record, any>[]): Query<Record, Result>;
|
35
33
|
/**
|
36
34
|
* Builds a new query object representing a logical AND between the given subqueries.
|
37
35
|
* @param queries An array of subqueries.
|
38
36
|
* @returns A new Query object.
|
39
37
|
*/
|
40
|
-
all(...queries: Query<
|
38
|
+
all(...queries: Query<Record, any>[]): Query<Record, Result>;
|
41
39
|
/**
|
42
40
|
* Builds a new query object representing a logical OR negating each subquery. In pseudo-code: !q1 OR !q2
|
43
41
|
* @param queries An array of subqueries.
|
44
42
|
* @returns A new Query object.
|
45
43
|
*/
|
46
|
-
not(...queries: Query<
|
44
|
+
not(...queries: Query<Record, any>[]): Query<Record, Result>;
|
47
45
|
/**
|
48
46
|
* Builds a new query object representing a logical AND negating each subquery. In pseudo-code: !q1 AND !q2
|
49
47
|
* @param queries An array of subqueries.
|
50
48
|
* @returns A new Query object.
|
51
49
|
*/
|
52
|
-
none(...queries: Query<
|
50
|
+
none(...queries: Query<Record, any>[]): Query<Record, Result>;
|
53
51
|
/**
|
54
52
|
* Builds a new query object adding one or more constraints. Examples:
|
55
53
|
*
|
@@ -63,66 +61,58 @@ export declare class Query<T extends XataRecord, R extends XataRecord = T> imple
|
|
63
61
|
* })
|
64
62
|
* ```
|
65
63
|
*
|
66
|
-
* @param constraints
|
67
64
|
* @returns A new Query object.
|
68
65
|
*/
|
69
|
-
filter(
|
70
|
-
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>;
|
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
|
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<
|
84
|
-
getPaginated
|
85
|
-
|
86
|
-
|
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']>[]>;
|
87
88
|
/**
|
88
89
|
* Performs the query in the database and returns a set of results.
|
89
90
|
* @param options Additional options to be used when performing the query.
|
90
91
|
* @returns An array of records from the database.
|
91
92
|
*/
|
92
|
-
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']>[]>;
|
93
96
|
/**
|
94
97
|
* Performs the query in the database and returns all the results.
|
95
98
|
* Warning: If there are a large number of results, this method can have performance implications.
|
96
99
|
* @param options Additional options to be used when performing the query.
|
97
100
|
* @returns An array of records from the database.
|
98
101
|
*/
|
99
|
-
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']>[]>;
|
100
105
|
/**
|
101
106
|
* Performs the query in the database and returns the first result.
|
102
107
|
* @param options Additional options to be used when performing the query.
|
103
108
|
* @returns The first record that matches the query, or null if no record matched the query.
|
104
109
|
*/
|
105
|
-
getOne
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
113
|
-
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>>;
|
114
117
|
hasNextPage(): boolean;
|
115
118
|
}
|
116
|
-
/**
|
117
|
-
* Helper type to read options and compute the correct type for the result values
|
118
|
-
* T: Original type
|
119
|
-
* R: Default destination type
|
120
|
-
* Options: QueryOptions
|
121
|
-
*
|
122
|
-
* If the columns are overriden in the options, the result type is the pick of the original type and the columns
|
123
|
-
* If the columns are not overriden, the result type is the default destination type
|
124
|
-
*/
|
125
|
-
declare type GetWithColumnOptions<T, R, Options> = Options extends {
|
126
|
-
columns: SelectableColumn<T>[];
|
127
|
-
} ? Select<T, Options['columns'][number]> : R;
|
128
|
-
export {};
|
package/dist/schema/query.js
CHANGED
@@ -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
|
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").
|
72
|
-
__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;
|
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
|
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
|
-
|
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
|
-
|
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,35 +181,26 @@ 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 all the results.
|
198
|
-
* Warning: If there are a large number of results, this method can have performance implications.
|
199
|
-
* @param options Additional options to be used when performing the query.
|
200
|
-
* @returns An array of records from the database.
|
201
|
-
*/
|
202
|
-
getAll(options = {}) {
|
198
|
+
getAll(chunk = pagination_1.PAGINATION_MAX_SIZE, options = {}) {
|
203
199
|
var e_2, _a;
|
204
200
|
return __awaiter(this, void 0, void 0, function* () {
|
205
201
|
const results = [];
|
206
202
|
try {
|
207
|
-
for (var _b = __asyncValues(this.getIterator(
|
203
|
+
for (var _b = __asyncValues(this.getIterator(chunk, options)), _c; _c = yield _b.next(), !_c.done;) {
|
208
204
|
const page = _c.value;
|
209
205
|
results.push(...page);
|
210
206
|
}
|
@@ -216,24 +212,17 @@ class Query {
|
|
216
212
|
}
|
217
213
|
finally { if (e_2) throw e_2.error; }
|
218
214
|
}
|
215
|
+
// Method overloading does not provide type inference for the return type.
|
219
216
|
return results;
|
220
217
|
});
|
221
218
|
}
|
222
|
-
/**
|
223
|
-
* Performs the query in the database and returns the first result.
|
224
|
-
* @param options Additional options to be used when performing the query.
|
225
|
-
* @returns The first record that matches the query, or null if no record matched the query.
|
226
|
-
*/
|
227
219
|
getOne(options = {}) {
|
228
220
|
return __awaiter(this, void 0, void 0, function* () {
|
229
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.
|
230
223
|
return records[0] || null;
|
231
224
|
});
|
232
225
|
}
|
233
|
-
/**async deleteAll(): Promise<number> {
|
234
|
-
// TODO: Return number of affected rows
|
235
|
-
return 0;
|
236
|
-
}**/
|
237
226
|
nextPage(size, offset) {
|
238
227
|
return this.firstPage(size, offset);
|
239
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
|
*/
|
@@ -8,6 +8,9 @@ export interface Identifiable {
|
|
8
8
|
*/
|
9
9
|
id: string;
|
10
10
|
}
|
11
|
+
export interface BaseData {
|
12
|
+
[key: string]: any;
|
13
|
+
}
|
11
14
|
/**
|
12
15
|
* Represents a persisted record from the database.
|
13
16
|
*/
|
@@ -24,14 +27,14 @@ export interface XataRecord extends Identifiable {
|
|
24
27
|
/**
|
25
28
|
* Retrieves a refreshed copy of the current record from the database.
|
26
29
|
*/
|
27
|
-
read(): Promise<this>;
|
30
|
+
read(): Promise<SelectedPick<this, ['*']> | null>;
|
28
31
|
/**
|
29
32
|
* Performs a partial update of the current record. On success a new object is
|
30
33
|
* returned and the current object is not mutated.
|
31
34
|
* @param data The columns and their values that have to be updated.
|
32
35
|
* @returns A new record containing the latest values for all the columns of the current record.
|
33
36
|
*/
|
34
|
-
update(data: Partial<
|
37
|
+
update(data: Partial<EditableData<Omit<this, keyof XataRecord>>>): Promise<SelectedPick<this, ['*']>>;
|
35
38
|
/**
|
36
39
|
* Performs a deletion of the current record in the database.
|
37
40
|
*
|
@@ -39,3 +42,25 @@ export interface XataRecord extends Identifiable {
|
|
39
42
|
*/
|
40
43
|
delete(): Promise<void>;
|
41
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
|
+
} | null | 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,82 +1,135 @@
|
|
1
1
|
import { FetchImpl } from '../api/fetcher';
|
2
|
+
import { Dictionary, GetArrayInnerType, StringKeys } from '../util/types';
|
2
3
|
import { Page } from './pagination';
|
3
|
-
import { Query
|
4
|
-
import { XataRecord } from './record';
|
5
|
-
import {
|
6
|
-
|
4
|
+
import { Query } from './query';
|
5
|
+
import { BaseData, EditableData, Identifiable, XataRecord } from './record';
|
6
|
+
import { SelectedPick } from './selection';
|
7
|
+
declare type TableLink = string[];
|
8
|
+
export declare type LinkDictionary = Dictionary<TableLink[]>;
|
7
9
|
/**
|
8
10
|
* Common interface for performing operations on a table.
|
9
11
|
*/
|
10
|
-
export declare abstract class Repository<
|
12
|
+
export declare abstract class Repository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record, SelectedPick<Record, ['*']>> {
|
13
|
+
abstract create(object: EditableData<Data> & Partial<Identifiable>): Promise<SelectedPick<Record, ['*']>>;
|
11
14
|
/**
|
12
|
-
* Creates a record in the table.
|
15
|
+
* Creates a single record in the table with a unique id.
|
16
|
+
* @param id The unique id.
|
13
17
|
* @param object Object containing the column names with their values to be stored in the table.
|
14
18
|
* @returns The full persisted record.
|
15
19
|
*/
|
16
|
-
abstract create(object:
|
20
|
+
abstract create(id: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
|
17
21
|
/**
|
18
22
|
* Creates multiple records in the table.
|
19
23
|
* @param objects Array of objects with the column names and the values to be stored in the table.
|
20
24
|
* @returns Array of the persisted records.
|
21
25
|
*/
|
22
|
-
abstract
|
26
|
+
abstract create(objects: Array<EditableData<Data> & Partial<Identifiable>>): Promise<SelectedPick<Record, ['*']>[]>;
|
23
27
|
/**
|
24
28
|
* Queries a single record from the table given its unique id.
|
25
29
|
* @param id The unique id.
|
26
30
|
* @returns The persisted record for the given id or null if the record could not be found.
|
27
31
|
*/
|
28
|
-
abstract read(id: string): Promise<
|
32
|
+
abstract read(id: string): Promise<SelectedPick<Record, ['*']> | null>;
|
29
33
|
/**
|
30
|
-
*
|
31
|
-
* @param id
|
32
|
-
* @param object Object containing the column names with their values to be stored in the table.
|
34
|
+
* Partially update a single record.
|
35
|
+
* @param object An object with its id and the columns to be updated.
|
33
36
|
* @returns The full persisted record.
|
34
37
|
*/
|
35
|
-
abstract
|
38
|
+
abstract update(object: Partial<EditableData<Data>> & Identifiable): Promise<SelectedPick<Record, ['*']>>;
|
36
39
|
/**
|
37
40
|
* Partially update a single record given its unique id.
|
38
41
|
* @param id The unique id.
|
39
|
-
* @param object The column names and their values that have to be
|
42
|
+
* @param object The column names and their values that have to be updated.
|
43
|
+
* @returns The full persisted record.
|
44
|
+
*/
|
45
|
+
abstract update(id: string, object: Partial<EditableData<Data>>): Promise<SelectedPick<Record, ['*']>>;
|
46
|
+
/**
|
47
|
+
* Partially updates multiple records.
|
48
|
+
* @param objects An array of objects with their ids and columns to be updated.
|
49
|
+
* @returns Array of the persisted records.
|
50
|
+
*/
|
51
|
+
abstract update(objects: Array<Partial<EditableData<Data>> & Identifiable>): Promise<SelectedPick<Record, ['*']>[]>;
|
52
|
+
/**
|
53
|
+
* Creates or updates a single record. If a record exists with the given id,
|
54
|
+
* it will be update, otherwise a new record will be created.
|
55
|
+
* @param object Object containing the column names with their values to be persisted in the table.
|
40
56
|
* @returns The full persisted record.
|
41
57
|
*/
|
42
|
-
abstract
|
58
|
+
abstract createOrUpdate(object: EditableData<Data> & Identifiable): Promise<SelectedPick<Record, ['*']>>;
|
43
59
|
/**
|
44
|
-
*
|
60
|
+
* Creates or updates a single record. If a record exists with the given id,
|
45
61
|
* it will be update, otherwise a new record will be created.
|
46
62
|
* @param id A unique id.
|
47
63
|
* @param object The column names and the values to be persisted.
|
48
64
|
* @returns The full persisted record.
|
49
65
|
*/
|
50
|
-
abstract
|
66
|
+
abstract createOrUpdate(id: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
|
67
|
+
/**
|
68
|
+
* Creates or updates a single record. If a record exists with the given id,
|
69
|
+
* it will be update, otherwise a new record will be created.
|
70
|
+
* @param objects Array of objects with the column names and the values to be stored in the table.
|
71
|
+
* @returns Array of the persisted records.
|
72
|
+
*/
|
73
|
+
abstract createOrUpdate(objects: Array<EditableData<Data> & Identifiable>): Promise<SelectedPick<Record, ['*']>[]>;
|
51
74
|
/**
|
52
75
|
* Deletes a record given its unique id.
|
53
76
|
* @param id The unique id.
|
54
77
|
* @throws If the record could not be found or there was an error while performing the deletion.
|
55
78
|
*/
|
56
|
-
abstract delete(id: string): void
|
57
|
-
|
58
|
-
|
59
|
-
|
79
|
+
abstract delete(id: string): Promise<void>;
|
80
|
+
/**
|
81
|
+
* Deletes a record given its unique id.
|
82
|
+
* @param id An object with a unique id.
|
83
|
+
* @throws If the record could not be found or there was an error while performing the deletion.
|
84
|
+
*/
|
85
|
+
abstract delete(id: Identifiable): Promise<void>;
|
86
|
+
/**
|
87
|
+
* Deletes a record given a list of unique ids.
|
88
|
+
* @param ids The array of unique ids.
|
89
|
+
* @throws If the record could not be found or there was an error while performing the deletion.
|
90
|
+
*/
|
91
|
+
abstract delete(ids: string[]): Promise<void>;
|
92
|
+
/**
|
93
|
+
* Deletes a record given a list of unique ids.
|
94
|
+
* @param ids An array of objects with unique ids.
|
95
|
+
* @throws If the record could not be found or there was an error while performing the deletion.
|
96
|
+
*/
|
97
|
+
abstract delete(ids: Identifiable[]): Promise<void>;
|
98
|
+
/**
|
99
|
+
* Search for records in the table.
|
100
|
+
* @param query The query to search for.
|
101
|
+
* @param options The options to search with (like: fuzziness)
|
102
|
+
* @returns The found records.
|
103
|
+
*/
|
104
|
+
abstract search(query: string, options?: {
|
105
|
+
fuzziness?: number;
|
106
|
+
}): Promise<SelectedPick<Record, ['*']>[]>;
|
107
|
+
abstract query<Result extends XataRecord>(query: Query<Record, Result>): Promise<Page<Record, Result>>;
|
60
108
|
}
|
61
|
-
export declare class RestRepository<
|
109
|
+
export declare class RestRepository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record, SelectedPick<Record, ['*']>> {
|
62
110
|
#private;
|
63
|
-
constructor(client: BaseClient<any>, table: string);
|
64
|
-
create(object:
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
111
|
+
constructor(client: BaseClient<any>, table: string, links?: LinkDictionary);
|
112
|
+
create(object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
|
113
|
+
create(recordId: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
|
114
|
+
create(objects: EditableData<Data>[]): Promise<SelectedPick<Record, ['*']>[]>;
|
115
|
+
read(recordId: string): Promise<SelectedPick<Record, ['*']> | null>;
|
116
|
+
update(object: Partial<EditableData<Data>> & Identifiable): Promise<SelectedPick<Record, ['*']>>;
|
117
|
+
update(recordId: string, object: Partial<EditableData<Data>>): Promise<SelectedPick<Record, ['*']>>;
|
118
|
+
update(objects: Array<Partial<EditableData<Data>> & Identifiable>): Promise<SelectedPick<Record, ['*']>[]>;
|
119
|
+
createOrUpdate(object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
|
120
|
+
createOrUpdate(recordId: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
|
121
|
+
createOrUpdate(objects: EditableData<Data>[]): Promise<SelectedPick<Record, ['*']>[]>;
|
122
|
+
delete(recordId: string | Identifiable | Array<string | Identifiable>): Promise<void>;
|
123
|
+
search(query: string, options?: {
|
124
|
+
fuzziness?: number;
|
125
|
+
}): Promise<SelectedPick<Record, ['*']>[]>;
|
126
|
+
query<Result extends XataRecord>(query: Query<Record, Result>): Promise<Page<Record, Result>>;
|
74
127
|
}
|
75
128
|
interface RepositoryFactory {
|
76
|
-
createRepository<
|
129
|
+
createRepository<Data extends BaseData>(client: BaseClient<any>, table: string, links?: LinkDictionary): Repository<Data & XataRecord>;
|
77
130
|
}
|
78
131
|
export declare class RestRespositoryFactory implements RepositoryFactory {
|
79
|
-
createRepository<
|
132
|
+
createRepository<Data extends BaseData>(client: BaseClient<any>, table: string, links?: LinkDictionary): Repository<Data & XataRecord>;
|
80
133
|
}
|
81
134
|
declare type BranchStrategyValue = string | undefined | null;
|
82
135
|
declare type BranchStrategyBuilder = () => BranchStrategyValue | Promise<BranchStrategyValue>;
|
@@ -91,19 +144,22 @@ export declare type XataClientOptions = {
|
|
91
144
|
*/
|
92
145
|
fetch?: FetchImpl;
|
93
146
|
databaseURL?: string;
|
94
|
-
branch
|
147
|
+
branch?: BranchStrategyOption;
|
95
148
|
/**
|
96
149
|
* API key to be used. You can create one in your account settings at https://app.xata.io/settings.
|
97
150
|
*/
|
98
|
-
apiKey
|
151
|
+
apiKey?: string;
|
99
152
|
repositoryFactory?: RepositoryFactory;
|
100
153
|
};
|
101
|
-
export declare class BaseClient<D extends Record<string, Repository<any>>> {
|
102
|
-
#private;
|
154
|
+
export declare class BaseClient<D extends Record<string, Repository<any>> = Record<string, Repository<any>>> {
|
103
155
|
options: XataClientOptions;
|
104
156
|
db: D;
|
105
|
-
constructor(options
|
106
|
-
|
107
|
-
|
157
|
+
constructor(options?: XataClientOptions, links?: LinkDictionary);
|
158
|
+
search<Tables extends StringKeys<D>>(query: string, options?: {
|
159
|
+
fuzziness?: number;
|
160
|
+
tables?: Tables[];
|
161
|
+
}): Promise<{
|
162
|
+
[Model in GetArrayInnerType<NonNullable<NonNullable<typeof options>['tables']>>]: Awaited<ReturnType<D[Model]['search']>>;
|
163
|
+
}>;
|
108
164
|
}
|
109
165
|
export {};
|