@xata.io/client 0.0.0-beta.c9e08d0 → 0.0.0-beta.ca9389a

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.
Files changed (66) hide show
  1. package/.eslintrc.cjs +13 -0
  2. package/CHANGELOG.md +87 -0
  3. package/dist/api/client.d.ts +95 -0
  4. package/dist/api/client.js +251 -0
  5. package/dist/api/components.d.ts +1437 -0
  6. package/dist/api/components.js +998 -0
  7. package/dist/api/fetcher.d.ts +40 -0
  8. package/dist/api/fetcher.js +79 -0
  9. package/dist/api/index.d.ts +7 -0
  10. package/dist/api/index.js +21 -0
  11. package/dist/api/parameters.d.ts +16 -0
  12. package/dist/api/parameters.js +2 -0
  13. package/dist/api/providers.d.ts +8 -0
  14. package/dist/api/providers.js +30 -0
  15. package/dist/api/responses.d.ts +50 -0
  16. package/dist/api/responses.js +2 -0
  17. package/dist/api/schemas.d.ts +311 -0
  18. package/dist/api/schemas.js +2 -0
  19. package/dist/client.d.ts +39 -0
  20. package/dist/client.js +124 -0
  21. package/dist/index.d.ts +5 -182
  22. package/dist/index.js +19 -499
  23. package/dist/namespace.d.ts +7 -0
  24. package/dist/namespace.js +6 -0
  25. package/dist/schema/filters.d.ts +96 -0
  26. package/dist/schema/filters.js +2 -0
  27. package/dist/schema/filters.spec.d.ts +1 -0
  28. package/dist/schema/filters.spec.js +177 -0
  29. package/dist/schema/index.d.ts +21 -0
  30. package/dist/schema/index.js +49 -0
  31. package/dist/schema/operators.d.ts +74 -0
  32. package/dist/schema/operators.js +93 -0
  33. package/dist/schema/pagination.d.ts +83 -0
  34. package/dist/schema/pagination.js +93 -0
  35. package/dist/schema/query.d.ts +118 -0
  36. package/dist/schema/query.js +242 -0
  37. package/dist/schema/record.d.ts +66 -0
  38. package/dist/schema/record.js +13 -0
  39. package/dist/schema/repository.d.ts +134 -0
  40. package/dist/schema/repository.js +284 -0
  41. package/dist/schema/selection.d.ts +25 -0
  42. package/dist/schema/selection.js +2 -0
  43. package/dist/schema/selection.spec.d.ts +1 -0
  44. package/dist/schema/selection.spec.js +204 -0
  45. package/dist/schema/sorting.d.ts +17 -0
  46. package/dist/schema/sorting.js +28 -0
  47. package/dist/schema/sorting.spec.d.ts +1 -0
  48. package/dist/schema/sorting.spec.js +11 -0
  49. package/dist/search/index.d.ts +20 -0
  50. package/dist/search/index.js +30 -0
  51. package/dist/util/branches.d.ts +5 -0
  52. package/dist/util/branches.js +7 -0
  53. package/dist/util/config.d.ts +11 -0
  54. package/dist/util/config.js +121 -0
  55. package/dist/util/environment.d.ts +5 -0
  56. package/dist/util/environment.js +68 -0
  57. package/dist/util/fetch.d.ts +2 -0
  58. package/dist/util/fetch.js +13 -0
  59. package/dist/util/lang.d.ts +5 -0
  60. package/dist/util/lang.js +22 -0
  61. package/dist/util/types.d.ts +25 -0
  62. package/dist/util/types.js +2 -0
  63. package/package.json +5 -2
  64. package/tsconfig.json +21 -0
  65. package/dist/util/errors.d.ts +0 -3
  66. package/dist/util/errors.js +0 -9
@@ -0,0 +1,118 @@
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';
9
+ export declare type QueryOptions<T extends XataRecord> = {
10
+ page?: PaginationOptions;
11
+ columns?: NonEmptyArray<SelectableColumn<T>>;
12
+ filter?: FilterExpression;
13
+ sort?: SortFilter<T> | SortFilter<T>[];
14
+ };
15
+ /**
16
+ * Query objects contain the information of all filters, sorting, etc. to be included in the database query.
17
+ *
18
+ * Query objects are immutable. Any method that adds more constraints or options to the query will return
19
+ * a new Query object containing the both the previous and the new constraints and options.
20
+ */
21
+ export declare class Query<Record extends XataRecord, Result extends XataRecord = Record> implements Paginable<Record, Result> {
22
+ #private;
23
+ readonly meta: PaginationQueryMeta;
24
+ readonly records: Result[];
25
+ constructor(repository: Repository<Record> | null, table: string, data: Partial<QueryOptions<Record>>, parent?: Partial<QueryOptions<Record>>);
26
+ getQueryOptions(): QueryOptions<Record>;
27
+ /**
28
+ * Builds a new query object representing a logical OR between the given subqueries.
29
+ * @param queries An array of subqueries.
30
+ * @returns A new Query object.
31
+ */
32
+ any(...queries: Query<Record, any>[]): Query<Record, Result>;
33
+ /**
34
+ * Builds a new query object representing a logical AND between the given subqueries.
35
+ * @param queries An array of subqueries.
36
+ * @returns A new Query object.
37
+ */
38
+ all(...queries: Query<Record, any>[]): Query<Record, Result>;
39
+ /**
40
+ * Builds a new query object representing a logical OR negating each subquery. In pseudo-code: !q1 OR !q2
41
+ * @param queries An array of subqueries.
42
+ * @returns A new Query object.
43
+ */
44
+ not(...queries: Query<Record, any>[]): Query<Record, Result>;
45
+ /**
46
+ * Builds a new query object representing a logical AND negating each subquery. In pseudo-code: !q1 AND !q2
47
+ * @param queries An array of subqueries.
48
+ * @returns A new Query object.
49
+ */
50
+ none(...queries: Query<Record, any>[]): Query<Record, Result>;
51
+ /**
52
+ * Builds a new query object adding one or more constraints. Examples:
53
+ *
54
+ * ```
55
+ * query.filter("columnName", columnValue)
56
+ * query.filter({
57
+ * "columnName": columnValue
58
+ * })
59
+ * query.filter({
60
+ * "columnName": operator(columnValue) // Use gt, gte, lt, lte, startsWith,...
61
+ * })
62
+ * ```
63
+ *
64
+ * @returns A new Query object.
65
+ */
66
+ filter(filters: Filter<Record>): Query<Record, Result>;
67
+ filter<F extends SelectableColumn<Record>>(column: F, value: Filter<ValueAtColumn<Record, F>>): Query<Record, Result>;
68
+ /**
69
+ * Builds a new query with a new sort option.
70
+ * @param column The column name.
71
+ * @param direction The direction. Either ascending or descending.
72
+ * @returns A new Query object.
73
+ */
74
+ sort<F extends SelectableColumn<Record>>(column: F, direction: SortDirection): Query<Record, Result>;
75
+ /**
76
+ * Builds a new query specifying the set of columns to be returned in the query response.
77
+ * @param columns Array of column names to be returned by the query.
78
+ * @returns A new Query object.
79
+ */
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
+ /**
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(): Promise<Result[]>;
94
+ getMany(options: Omit<QueryOptions<Record>, 'columns'>): Promise<Result[]>;
95
+ getMany<Options extends RequiredBy<QueryOptions<Record>, 'columns'>>(options: Options): Promise<SelectedPick<Record, typeof options['columns']>[]>;
96
+ /**
97
+ * Performs the query in the database and returns all the results.
98
+ * Warning: If there are a large number of results, this method can have performance implications.
99
+ * @param options Additional options to be used when performing the query.
100
+ * @returns An array of records from the database.
101
+ */
102
+ getAll(chunk?: number): Promise<Result[]>;
103
+ getAll(chunk: number | undefined, options: Omit<QueryOptions<Record>, 'columns' | 'page'>): Promise<Result[]>;
104
+ getAll<Options extends RequiredBy<Omit<QueryOptions<Record>, 'page'>, 'columns'>>(chunk: number | undefined, options: Options): Promise<SelectedPick<Record, typeof options['columns']>[]>;
105
+ /**
106
+ * Performs the query in the database and returns the first result.
107
+ * @param options Additional options to be used when performing the query.
108
+ * @returns The first record that matches the query, or null if no record matched the query.
109
+ */
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>>;
117
+ hasNextPage(): boolean;
118
+ }
@@ -0,0 +1,242 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
12
+ if (kind === "m") throw new TypeError("Private method is not writable");
13
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
14
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
15
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
16
+ };
17
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
18
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
19
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
20
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
21
+ };
22
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
23
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
24
+ var m = o[Symbol.asyncIterator], i;
25
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
26
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
27
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
28
+ };
29
+ var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
30
+ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
31
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
32
+ var g = generator.apply(thisArg, _arguments || []), i, q = [];
33
+ return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
34
+ function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
35
+ function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
36
+ function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
37
+ function fulfill(value) { resume("next", value); }
38
+ function reject(value) { resume("throw", value); }
39
+ function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
40
+ };
41
+ var _Query_table, _Query_repository, _Query_data;
42
+ Object.defineProperty(exports, "__esModule", { value: true });
43
+ exports.Query = void 0;
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
+ */
52
+ class Query {
53
+ constructor(repository, table, data, parent) {
54
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
55
+ _Query_table.set(this, void 0);
56
+ _Query_repository.set(this, void 0);
57
+ _Query_data.set(this, { filter: {} });
58
+ // Implements pagination
59
+ this.meta = { page: { cursor: 'start', more: true } };
60
+ this.records = [];
61
+ __classPrivateFieldSet(this, _Query_table, table, "f");
62
+ if (repository) {
63
+ __classPrivateFieldSet(this, _Query_repository, repository, "f");
64
+ }
65
+ else {
66
+ __classPrivateFieldSet(this, _Query_repository, this, "f");
67
+ }
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;
76
+ this.any = this.any.bind(this);
77
+ this.all = this.all.bind(this);
78
+ this.not = this.not.bind(this);
79
+ this.filter = this.filter.bind(this);
80
+ this.sort = this.sort.bind(this);
81
+ this.none = this.none.bind(this);
82
+ Object.defineProperty(this, 'table', { enumerable: false });
83
+ Object.defineProperty(this, 'repository', { enumerable: false });
84
+ }
85
+ getQueryOptions() {
86
+ return __classPrivateFieldGet(this, _Query_data, "f");
87
+ }
88
+ /**
89
+ * Builds a new query object representing a logical OR between the given subqueries.
90
+ * @param queries An array of subqueries.
91
+ * @returns A new Query object.
92
+ */
93
+ any(...queries) {
94
+ const $any = queries.map((query) => { var _a; return (_a = query.getQueryOptions().filter) !== null && _a !== void 0 ? _a : {}; });
95
+ return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $any } }, __classPrivateFieldGet(this, _Query_data, "f"));
96
+ }
97
+ /**
98
+ * Builds a new query object representing a logical AND between the given subqueries.
99
+ * @param queries An array of subqueries.
100
+ * @returns A new Query object.
101
+ */
102
+ all(...queries) {
103
+ const $all = queries.map((query) => { var _a; return (_a = query.getQueryOptions().filter) !== null && _a !== void 0 ? _a : {}; });
104
+ return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f"));
105
+ }
106
+ /**
107
+ * Builds a new query object representing a logical OR negating each subquery. In pseudo-code: !q1 OR !q2
108
+ * @param queries An array of subqueries.
109
+ * @returns A new Query object.
110
+ */
111
+ not(...queries) {
112
+ const $not = queries.map((query) => { var _a; return (_a = query.getQueryOptions().filter) !== null && _a !== void 0 ? _a : {}; });
113
+ return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $not } }, __classPrivateFieldGet(this, _Query_data, "f"));
114
+ }
115
+ /**
116
+ * Builds a new query object representing a logical AND negating each subquery. In pseudo-code: !q1 AND !q2
117
+ * @param queries An array of subqueries.
118
+ * @returns A new Query object.
119
+ */
120
+ none(...queries) {
121
+ const $none = queries.map((query) => { var _a; return (_a = query.getQueryOptions().filter) !== null && _a !== void 0 ? _a : {}; });
122
+ return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $none } }, __classPrivateFieldGet(this, _Query_data, "f"));
123
+ }
124
+ filter(a, b) {
125
+ var _a, _b;
126
+ if (arguments.length === 1) {
127
+ const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
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));
129
+ return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f"));
130
+ }
131
+ else {
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 }]));
133
+ return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f"));
134
+ }
135
+ }
136
+ /**
137
+ * Builds a new query with a new sort option.
138
+ * @param column The column name.
139
+ * @param direction The direction. Either ascending or descending.
140
+ * @returns A new Query object.
141
+ */
142
+ sort(column, direction) {
143
+ var _a;
144
+ const originalSort = [(_a = __classPrivateFieldGet(this, _Query_data, "f").sort) !== null && _a !== void 0 ? _a : []].flat();
145
+ const sort = [...originalSort, { column, direction }];
146
+ return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { sort }, __classPrivateFieldGet(this, _Query_data, "f"));
147
+ }
148
+ /**
149
+ * Builds a new query specifying the set of columns to be returned in the query response.
150
+ * @param columns Array of column names to be returned by the query.
151
+ * @returns A new Query object.
152
+ */
153
+ select(columns) {
154
+ return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { columns }, __classPrivateFieldGet(this, _Query_data, "f"));
155
+ }
156
+ getPaginated(options = {}) {
157
+ const query = new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), options, __classPrivateFieldGet(this, _Query_data, "f"));
158
+ return __classPrivateFieldGet(this, _Query_repository, "f").query(query);
159
+ }
160
+ [(_Query_table = new WeakMap(), _Query_repository = new WeakMap(), _Query_data = new WeakMap(), Symbol.asyncIterator)]() {
161
+ return __asyncGenerator(this, arguments, function* _a() {
162
+ var e_1, _b;
163
+ try {
164
+ for (var _c = __asyncValues(this.getIterator(1)), _d; _d = yield __await(_c.next()), !_d.done;) {
165
+ const [record] = _d.value;
166
+ yield yield __await(record);
167
+ }
168
+ }
169
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
170
+ finally {
171
+ try {
172
+ if (_d && !_d.done && (_b = _c.return)) yield __await(_b.call(_c));
173
+ }
174
+ finally { if (e_1) throw e_1.error; }
175
+ }
176
+ });
177
+ }
178
+ getIterator(chunk, options = {}) {
179
+ return __asyncGenerator(this, arguments, function* getIterator_1() {
180
+ let offset = 0;
181
+ let end = false;
182
+ while (!end) {
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.
185
+ yield yield __await(records);
186
+ offset += chunk;
187
+ end = !meta.page.more;
188
+ }
189
+ });
190
+ }
191
+ getMany(options = {}) {
192
+ return __awaiter(this, void 0, void 0, function* () {
193
+ const { records } = yield this.getPaginated(options);
194
+ // Method overloading does not provide type inference for the return type.
195
+ return records;
196
+ });
197
+ }
198
+ getAll(chunk = pagination_1.PAGINATION_MAX_SIZE, options = {}) {
199
+ var e_2, _a;
200
+ return __awaiter(this, void 0, void 0, function* () {
201
+ const results = [];
202
+ try {
203
+ for (var _b = __asyncValues(this.getIterator(chunk, options)), _c; _c = yield _b.next(), !_c.done;) {
204
+ const page = _c.value;
205
+ results.push(...page);
206
+ }
207
+ }
208
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
209
+ finally {
210
+ try {
211
+ if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
212
+ }
213
+ finally { if (e_2) throw e_2.error; }
214
+ }
215
+ // Method overloading does not provide type inference for the return type.
216
+ return results;
217
+ });
218
+ }
219
+ getOne(options = {}) {
220
+ return __awaiter(this, void 0, void 0, function* () {
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.
223
+ return records[0] || null;
224
+ });
225
+ }
226
+ nextPage(size, offset) {
227
+ return this.firstPage(size, offset);
228
+ }
229
+ previousPage(size, offset) {
230
+ return this.firstPage(size, offset);
231
+ }
232
+ firstPage(size, offset) {
233
+ return this.getPaginated({ page: { size, offset } });
234
+ }
235
+ lastPage(size, offset) {
236
+ return this.getPaginated({ page: { size, offset, before: 'end' } });
237
+ }
238
+ hasNextPage() {
239
+ return this.meta.page.more;
240
+ }
241
+ }
242
+ exports.Query = Query;
@@ -0,0 +1,66 @@
1
+ import { SelectedPick } 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<Readonly<SelectedPick<this, ['*']>> | null>;
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(partialUpdate: Partial<EditableData<Omit<this, keyof XataRecord>>>): Promise<Readonly<SelectedPick<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
+ }
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<Readonly<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(partialUpdate: Partial<EditableData<Omit<Record, keyof XataRecord>>>): Promise<Readonly<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
+ };
@@ -0,0 +1,13 @@
1
+ "use strict";
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;
@@ -0,0 +1,134 @@
1
+ import { SchemaNamespaceResult } from '.';
2
+ import { FetcherExtraProps } from '../api/fetcher';
3
+ import { Dictionary } from '../util/types';
4
+ import { Page } from './pagination';
5
+ import { Query } from './query';
6
+ import { BaseData, EditableData, Identifiable, XataRecord } from './record';
7
+ import { SelectedPick } from './selection';
8
+ declare type TableLink = string[];
9
+ export declare type LinkDictionary = Dictionary<TableLink[]>;
10
+ /**
11
+ * Common interface for performing operations on a table.
12
+ */
13
+ export declare abstract class Repository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record, Readonly<SelectedPick<Record, ['*']>>> {
14
+ abstract create(object: EditableData<Data> & Partial<Identifiable>): Promise<Readonly<SelectedPick<Record, ['*']>>>;
15
+ /**
16
+ * Creates a single record in the table with a unique id.
17
+ * @param id The unique id.
18
+ * @param object Object containing the column names with their values to be stored in the table.
19
+ * @returns The full persisted record.
20
+ */
21
+ abstract create(id: string, object: EditableData<Data>): Promise<Readonly<SelectedPick<Record, ['*']>>>;
22
+ /**
23
+ * Creates multiple records in the table.
24
+ * @param objects Array of objects with the column names and the values to be stored in the table.
25
+ * @returns Array of the persisted records.
26
+ */
27
+ abstract create(objects: Array<EditableData<Data> & Partial<Identifiable>>): Promise<Readonly<SelectedPick<Record, ['*']>>[]>;
28
+ /**
29
+ * Queries a single record from the table given its unique id.
30
+ * @param id The unique id.
31
+ * @returns The persisted record for the given id or null if the record could not be found.
32
+ */
33
+ abstract read(id: string): Promise<Readonly<SelectedPick<Record, ['*']> | null>>;
34
+ /**
35
+ * Partially update a single record.
36
+ * @param object An object with its id and the columns to be updated.
37
+ * @returns The full persisted record.
38
+ */
39
+ abstract update(object: Partial<EditableData<Data>> & Identifiable): Promise<Readonly<SelectedPick<Record, ['*']>>>;
40
+ /**
41
+ * Partially update a single record given its unique id.
42
+ * @param id The unique id.
43
+ * @param object The column names and their values that have to be updated.
44
+ * @returns The full persisted record.
45
+ */
46
+ abstract update(id: string, object: Partial<EditableData<Data>>): Promise<Readonly<SelectedPick<Record, ['*']>>>;
47
+ /**
48
+ * Partially updates multiple records.
49
+ * @param objects An array of objects with their ids and columns to be updated.
50
+ * @returns Array of the persisted records.
51
+ */
52
+ abstract update(objects: Array<Partial<EditableData<Data>> & Identifiable>): Promise<Readonly<SelectedPick<Record, ['*']>>[]>;
53
+ /**
54
+ * Creates or updates a single record. If a record exists with the given id,
55
+ * it will be update, otherwise a new record will be created.
56
+ * @param object Object containing the column names with their values to be persisted in the table.
57
+ * @returns The full persisted record.
58
+ */
59
+ abstract createOrUpdate(object: EditableData<Data> & Identifiable): Promise<Readonly<SelectedPick<Record, ['*']>>>;
60
+ /**
61
+ * Creates or updates a single record. If a record exists with the given id,
62
+ * it will be update, otherwise a new record will be created.
63
+ * @param id A unique id.
64
+ * @param object The column names and the values to be persisted.
65
+ * @returns The full persisted record.
66
+ */
67
+ abstract createOrUpdate(id: string, object: EditableData<Data>): Promise<Readonly<SelectedPick<Record, ['*']>>>;
68
+ /**
69
+ * Creates or updates a single record. If a record exists with the given id,
70
+ * it will be update, otherwise a new record will be created.
71
+ * @param objects Array of objects with the column names and the values to be stored in the table.
72
+ * @returns Array of the persisted records.
73
+ */
74
+ abstract createOrUpdate(objects: Array<EditableData<Data> & Identifiable>): Promise<Readonly<SelectedPick<Record, ['*']>>[]>;
75
+ /**
76
+ * Deletes a record given its unique id.
77
+ * @param id The unique id.
78
+ * @throws If the record could not be found or there was an error while performing the deletion.
79
+ */
80
+ abstract delete(id: string): Promise<void>;
81
+ /**
82
+ * Deletes a record given its unique id.
83
+ * @param id An object with a unique id.
84
+ * @throws If the record could not be found or there was an error while performing the deletion.
85
+ */
86
+ abstract delete(id: Identifiable): Promise<void>;
87
+ /**
88
+ * Deletes a record given a list of unique ids.
89
+ * @param ids The array of unique ids.
90
+ * @throws If the record could not be found or there was an error while performing the deletion.
91
+ */
92
+ abstract delete(ids: string[]): Promise<void>;
93
+ /**
94
+ * Deletes a record given a list of unique ids.
95
+ * @param ids An array of objects with unique ids.
96
+ * @throws If the record could not be found or there was an error while performing the deletion.
97
+ */
98
+ abstract delete(ids: Identifiable[]): Promise<void>;
99
+ /**
100
+ * Search for records in the table.
101
+ * @param query The query to search for.
102
+ * @param options The options to search with (like: fuzziness)
103
+ * @returns The found records.
104
+ */
105
+ abstract search(query: string, options?: {
106
+ fuzziness?: number;
107
+ }): Promise<SelectedPick<Record, ['*']>[]>;
108
+ abstract query<Result extends XataRecord>(query: Query<Record, Result>): Promise<Page<Record, Result>>;
109
+ }
110
+ export declare class RestRepository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record, SelectedPick<Record, ['*']>> {
111
+ #private;
112
+ constructor(options: {
113
+ table: string;
114
+ links?: LinkDictionary;
115
+ getFetchProps: () => Promise<FetcherExtraProps>;
116
+ schemaNamespace: SchemaNamespaceResult<any>;
117
+ });
118
+ create(object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
119
+ create(recordId: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
120
+ create(objects: EditableData<Data>[]): Promise<SelectedPick<Record, ['*']>[]>;
121
+ read(recordId: string): Promise<SelectedPick<Record, ['*']> | null>;
122
+ update(object: Partial<EditableData<Data>> & Identifiable): Promise<SelectedPick<Record, ['*']>>;
123
+ update(recordId: string, object: Partial<EditableData<Data>>): Promise<SelectedPick<Record, ['*']>>;
124
+ update(objects: Array<Partial<EditableData<Data>> & Identifiable>): Promise<SelectedPick<Record, ['*']>[]>;
125
+ createOrUpdate(object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
126
+ createOrUpdate(recordId: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
127
+ createOrUpdate(objects: EditableData<Data>[]): Promise<SelectedPick<Record, ['*']>[]>;
128
+ delete(recordId: string | Identifiable | Array<string | Identifiable>): Promise<void>;
129
+ search(query: string, options?: {
130
+ fuzziness?: number;
131
+ }): Promise<SelectedPick<Record, ['*']>[]>;
132
+ query<Result extends XataRecord>(query: Query<Record, Result>): Promise<Page<Record, Result>>;
133
+ }
134
+ export {};