@xata.io/client 0.0.0-beta.bdce130 → 0.0.0-beta.c21e40a

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.
@@ -42,9 +42,16 @@ 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
- 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;
48
55
  _Query_table.set(this, void 0);
49
56
  _Query_repository.set(this, void 0);
50
57
  _Query_data.set(this, { filter: {} });
@@ -58,12 +65,14 @@ class Query {
58
65
  else {
59
66
  __classPrivateFieldSet(this, _Query_repository, this, "f");
60
67
  }
61
- __classPrivateFieldGet(this, _Query_data, "f").filter.$any = (_b = (_a = data.filter) === null || _a === void 0 ? void 0 : _a.$any) !== null && _b !== void 0 ? _b : (_c = parent === null || parent === void 0 ? void 0 : parent.filter) === null || _c === void 0 ? void 0 : _c.$any;
62
- __classPrivateFieldGet(this, _Query_data, "f").filter.$all = (_e = (_d = data.filter) === null || _d === void 0 ? void 0 : _d.$all) !== null && _e !== void 0 ? _e : (_f = parent === null || parent === void 0 ? void 0 : parent.filter) === null || _f === void 0 ? void 0 : _f.$all;
63
- __classPrivateFieldGet(this, _Query_data, "f").filter.$not = (_h = (_g = data.filter) === null || _g === void 0 ? void 0 : _g.$not) !== null && _h !== void 0 ? _h : (_j = parent === null || parent === void 0 ? void 0 : parent.filter) === null || _j === void 0 ? void 0 : _j.$not;
64
- __classPrivateFieldGet(this, _Query_data, "f").filter.$none = (_l = (_k = data.filter) === null || _k === void 0 ? void 0 : _k.$none) !== null && _l !== void 0 ? _l : (_m = parent === null || parent === void 0 ? void 0 : parent.filter) === null || _m === void 0 ? void 0 : _m.$none;
65
- __classPrivateFieldGet(this, _Query_data, "f").sort = (_o = data.sort) !== null && _o !== void 0 ? _o : parent === null || parent === void 0 ? void 0 : parent.sort;
66
- __classPrivateFieldGet(this, _Query_data, "f").columns = (_q = (_p = data.columns) !== null && _p !== void 0 ? _p : parent === null || parent === void 0 ? void 0 : parent.columns) !== null && _q !== void 0 ? _q : ['*'];
68
+ __classPrivateFieldGet(this, _Query_data, "f").filter = (_b = (_a = data.filter) !== null && _a !== void 0 ? _a : parent === null || parent === void 0 ? void 0 : parent.filter) !== null && _b !== void 0 ? _b : {};
69
+ __classPrivateFieldGet(this, _Query_data, "f").filter.$any = (_d = (_c = data.filter) === null || _c === void 0 ? void 0 : _c.$any) !== null && _d !== void 0 ? _d : (_e = parent === null || parent === void 0 ? void 0 : parent.filter) === null || _e === void 0 ? void 0 : _e.$any;
70
+ __classPrivateFieldGet(this, _Query_data, "f").filter.$all = (_g = (_f = data.filter) === null || _f === void 0 ? void 0 : _f.$all) !== null && _g !== void 0 ? _g : (_h = parent === null || parent === void 0 ? void 0 : parent.filter) === null || _h === void 0 ? void 0 : _h.$all;
71
+ __classPrivateFieldGet(this, _Query_data, "f").filter.$not = (_k = (_j = data.filter) === null || _j === void 0 ? void 0 : _j.$not) !== null && _k !== void 0 ? _k : (_l = parent === null || parent === void 0 ? void 0 : parent.filter) === null || _l === void 0 ? void 0 : _l.$not;
72
+ __classPrivateFieldGet(this, _Query_data, "f").filter.$none = (_o = (_m = data.filter) === null || _m === void 0 ? void 0 : _m.$none) !== null && _o !== void 0 ? _o : (_p = parent === null || parent === void 0 ? void 0 : parent.filter) === null || _p === void 0 ? void 0 : _p.$none;
73
+ __classPrivateFieldGet(this, _Query_data, "f").sort = (_q = data.sort) !== null && _q !== void 0 ? _q : parent === null || parent === void 0 ? void 0 : parent.sort;
74
+ __classPrivateFieldGet(this, _Query_data, "f").columns = (_s = (_r = data.columns) !== null && _r !== void 0 ? _r : parent === null || parent === void 0 ? void 0 : parent.columns) !== null && _s !== void 0 ? _s : ['*'];
75
+ __classPrivateFieldGet(this, _Query_data, "f").page = (_t = data.page) !== null && _t !== void 0 ? _t : parent === null || parent === void 0 ? void 0 : parent.page;
67
76
  this.any = this.any.bind(this);
68
77
  this.all = this.all.bind(this);
69
78
  this.not = this.not.bind(this);
@@ -76,46 +85,77 @@ class Query {
76
85
  getQueryOptions() {
77
86
  return __classPrivateFieldGet(this, _Query_data, "f");
78
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
+ */
79
93
  any(...queries) {
80
- const $any = (0, lang_1.compact)(queries.map((query) => query.getQueryOptions().filter.$any)).flat();
94
+ const $any = queries.map((query) => { var _a; return (_a = query.getQueryOptions().filter) !== null && _a !== void 0 ? _a : {}; });
81
95
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $any } }, __classPrivateFieldGet(this, _Query_data, "f"));
82
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
+ */
83
102
  all(...queries) {
84
- const $all = (0, lang_1.compact)(queries.map((query) => query.getQueryOptions().filter.$all)).flat();
103
+ const $all = queries.map((query) => { var _a; return (_a = query.getQueryOptions().filter) !== null && _a !== void 0 ? _a : {}; });
85
104
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f"));
86
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
+ */
87
111
  not(...queries) {
88
- const $not = (0, lang_1.compact)(queries.map((query) => query.getQueryOptions().filter.$not)).flat();
112
+ const $not = queries.map((query) => { var _a; return (_a = query.getQueryOptions().filter) !== null && _a !== void 0 ? _a : {}; });
89
113
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $not } }, __classPrivateFieldGet(this, _Query_data, "f"));
90
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
+ */
91
120
  none(...queries) {
92
- const $none = (0, lang_1.compact)(queries.map((query) => query.getQueryOptions().filter.$none)).flat();
121
+ const $none = queries.map((query) => { var _a; return (_a = query.getQueryOptions().filter) !== null && _a !== void 0 ? _a : {}; });
93
122
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $none } }, __classPrivateFieldGet(this, _Query_data, "f"));
94
123
  }
95
124
  filter(a, b) {
125
+ var _a, _b;
96
126
  if (arguments.length === 1) {
97
127
  const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
98
- 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));
99
129
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f"));
100
130
  }
101
131
  else {
102
- const column = a;
103
- const value = b;
104
- 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 }]));
105
133
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f"));
106
134
  }
107
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
+ */
108
142
  sort(column, direction) {
109
- const sort = Object.assign(Object.assign({}, __classPrivateFieldGet(this, _Query_data, "f").sort), { [column]: direction });
143
+ var _a;
144
+ const originalSort = [(_a = __classPrivateFieldGet(this, _Query_data, "f").sort) !== null && _a !== void 0 ? _a : []].flat();
145
+ const sort = [...originalSort, { column, direction }];
110
146
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { sort }, __classPrivateFieldGet(this, _Query_data, "f"));
111
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
+ */
112
153
  select(columns) {
113
154
  return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { columns }, __classPrivateFieldGet(this, _Query_data, "f"));
114
155
  }
115
156
  getPaginated(options = {}) {
116
- return __awaiter(this, void 0, void 0, function* () {
117
- return __classPrivateFieldGet(this, _Query_repository, "f").query(this, options);
118
- });
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);
119
159
  }
120
160
  [(_Query_table = new WeakMap(), _Query_repository = new WeakMap(), _Query_data = new WeakMap(), Symbol.asyncIterator)]() {
121
161
  return __asyncGenerator(this, arguments, function* _a() {
@@ -141,6 +181,7 @@ class Query {
141
181
  let end = false;
142
182
  while (!end) {
143
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.
144
185
  yield yield __await(records);
145
186
  offset += chunk;
146
187
  end = !meta.page.more;
@@ -150,38 +191,49 @@ class Query {
150
191
  getMany(options = {}) {
151
192
  return __awaiter(this, void 0, void 0, function* () {
152
193
  const { records } = yield this.getPaginated(options);
194
+ // Method overloading does not provide type inference for the return type.
153
195
  return records;
154
196
  });
155
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
+ }
156
219
  getOne(options = {}) {
157
220
  return __awaiter(this, void 0, void 0, function* () {
158
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.
159
223
  return records[0] || null;
160
224
  });
161
225
  }
162
- /**async deleteAll(): Promise<number> {
163
- // TODO: Return number of affected rows
164
- return 0;
165
- }**/
166
226
  nextPage(size, offset) {
167
- return __awaiter(this, void 0, void 0, function* () {
168
- return this.firstPage(size, offset);
169
- });
227
+ return this.firstPage(size, offset);
170
228
  }
171
229
  previousPage(size, offset) {
172
- return __awaiter(this, void 0, void 0, function* () {
173
- return this.firstPage(size, offset);
174
- });
230
+ return this.firstPage(size, offset);
175
231
  }
176
232
  firstPage(size, offset) {
177
- return __awaiter(this, void 0, void 0, function* () {
178
- return this.getPaginated({ page: { size, offset } });
179
- });
233
+ return this.getPaginated({ page: { size, offset } });
180
234
  }
181
235
  lastPage(size, offset) {
182
- return __awaiter(this, void 0, void 0, function* () {
183
- return this.getPaginated({ page: { size, offset, before: 'end' } });
184
- });
236
+ return this.getPaginated({ page: { size, offset, before: 'end' } });
185
237
  }
186
238
  hasNextPage() {
187
239
  return this.meta.page.more;
@@ -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<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(data: Partial<EditableData<Omit<this, keyof XataRecord>>>): Promise<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<SelectedPick<Record, ['*']> | null>;
50
+ /**
51
+ * Performs a partial update of the current record. On success a new object is
52
+ * returned and the current object is not mutated.
53
+ * @param data The columns and their values that have to be updated.
54
+ * @returns A new record containing the latest values for all the columns of the current record.
55
+ */
56
+ update(data: Partial<EditableData<Omit<Record, keyof XataRecord>>>): Promise<SelectedPick<Record, ['*']>>;
57
+ };
58
+ export declare function isIdentifiable(x: any): x is Identifiable & Record<string, unknown>;
59
+ export declare function isXataRecord(x: any): x is XataRecord & Record<string, unknown>;
60
+ export declare type EditableData<O extends BaseData> = {
61
+ [K in keyof O]: O[K] extends XataRecord ? {
62
+ id: string;
63
+ } : NonNullable<O[K]> extends XataRecord ? {
64
+ id: string;
65
+ } | undefined : O[K];
66
+ };
@@ -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,100 @@
1
+ import { FetchImpl } from '../api/fetcher';
2
+ import { Page } from './pagination';
3
+ import { Query } from './query';
4
+ import { BaseData, EditableData, Identifiable, XataRecord } from './record';
5
+ import { SelectedPick } 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, SelectedPick<Record, ['*']>> {
11
+ abstract create(object: EditableData<Data> & Partial<Identifiable>): Promise<SelectedPick<Record, ['*']>>;
12
+ /**
13
+ * Creates a single record in the table with a unique id.
14
+ * @param id The unique id.
15
+ * @param object Object containing the column names with their values to be stored in the table.
16
+ * @returns The full persisted record.
17
+ */
18
+ abstract create(id: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
19
+ /**
20
+ * Creates multiple records in the table.
21
+ * @param objects Array of objects with the column names and the values to be stored in the table.
22
+ * @returns Array of the persisted records.
23
+ */
24
+ abstract createMany(objects: EditableData<Data>[]): Promise<SelectedPick<Record, ['*']>[]>;
25
+ /**
26
+ * Queries a single record from the table given its unique id.
27
+ * @param id The unique id.
28
+ * @returns The persisted record for the given id or null if the record could not be found.
29
+ */
30
+ abstract read(id: string): Promise<SelectedPick<Record, ['*']> | null>;
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<EditableData<Data>>): Promise<SelectedPick<Record, ['*']>>;
38
+ /**
39
+ * Creates or updates 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 createOrUpdate(id: string, object: EditableData<Data>): Promise<SelectedPick<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>(query: Query<Record, Result>): Promise<Page<Record, Result>>;
53
+ }
54
+ export declare class RestRepository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record, SelectedPick<Record, ['*']>> {
55
+ #private;
56
+ constructor(client: BaseClient<any>, table: string);
57
+ create(object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
58
+ create(recordId: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
59
+ createMany(objects: EditableData<Data>[]): Promise<SelectedPick<Record, ['*']>[]>;
60
+ read(recordId: string): Promise<SelectedPick<Record, ['*']> | null>;
61
+ update(recordId: string, object: Partial<EditableData<Data>>): Promise<SelectedPick<Record, ['*']>>;
62
+ createOrUpdate(recordId: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
63
+ delete(recordId: string): Promise<void>;
64
+ query<Result extends XataRecord>(query: Query<Record, Result>): Promise<Page<Record, Result>>;
65
+ }
66
+ interface RepositoryFactory {
67
+ createRepository<Data extends BaseData>(client: BaseClient<any>, table: string): Repository<Data & XataRecord>;
68
+ }
69
+ export declare class RestRespositoryFactory implements RepositoryFactory {
70
+ createRepository<Data extends BaseData>(client: BaseClient<any>, table: string): Repository<Data & XataRecord>;
71
+ }
72
+ declare type BranchStrategyValue = string | undefined | null;
73
+ declare type BranchStrategyBuilder = () => BranchStrategyValue | Promise<BranchStrategyValue>;
74
+ declare type BranchStrategy = BranchStrategyValue | BranchStrategyBuilder;
75
+ declare type BranchStrategyOption = NonNullable<BranchStrategy | BranchStrategy[]>;
76
+ export declare type XataClientOptions = {
77
+ /**
78
+ * Fetch implementation. This option is only required if the runtime does not include a fetch implementation
79
+ * available in the global scope. If you are running your code on Deno or Cloudflare workers for example,
80
+ * you won't need to provide a specific fetch implementation. But for most versions of Node.js you'll need
81
+ * to provide one. Such as cross-fetch, node-fetch or isomorphic-fetch.
82
+ */
83
+ fetch?: FetchImpl;
84
+ databaseURL?: string;
85
+ branch: BranchStrategyOption;
86
+ /**
87
+ * API key to be used. You can create one in your account settings at https://app.xata.io/settings.
88
+ */
89
+ apiKey: string;
90
+ repositoryFactory?: RepositoryFactory;
91
+ };
92
+ export declare class BaseClient<D extends Record<string, Repository<any>> = Record<string, Repository<any>>> {
93
+ #private;
94
+ options: XataClientOptions;
95
+ db: D;
96
+ constructor(options: XataClientOptions, links?: Links);
97
+ initObject<T>(table: string, object: object): T;
98
+ getBranch(): Promise<string>;
99
+ }
100
+ export {};