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