@xata.io/client 0.0.0-beta.f12621e → 0.0.0-beta.f2b2fa6

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/dist/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -18,6 +22,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
18
22
  step((generator = generator.apply(thisArg, _arguments || [])).next());
19
23
  });
20
24
  };
25
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
26
+ if (kind === "m") throw new TypeError("Private method is not writable");
27
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
28
+ 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");
29
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
30
+ };
31
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
32
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
33
+ 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");
34
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
35
+ };
21
36
  var __asyncValues = (this && this.__asyncValues) || function (o) {
22
37
  if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
23
38
  var m = o[Symbol.asyncIterator], i;
@@ -25,82 +40,48 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
25
40
  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); }); }; }
26
41
  function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
27
42
  };
43
+ var _RestRepository_instances, _RestRepository_client, _RestRepository_fetch, _RestRepository_table, _RestRepository_getFetchProps, _BaseClient_links, _BaseClient_branch;
28
44
  Object.defineProperty(exports, "__esModule", { value: true });
29
45
  exports.XataError = exports.BaseClient = exports.RestRespositoryFactory = exports.RestRepository = exports.Repository = void 0;
46
+ const api_1 = require("./api");
30
47
  const filters_1 = require("./schema/filters");
31
48
  const pagination_1 = require("./schema/pagination");
32
49
  const query_1 = require("./schema/query");
33
- const errors_1 = require("./util/errors");
34
50
  class Repository extends query_1.Query {
35
- select(columns) {
36
- return new query_1.Query(this.repository, this.table, { columns });
37
- }
38
51
  }
39
52
  exports.Repository = Repository;
40
53
  class RestRepository extends Repository {
41
54
  constructor(client, table) {
42
55
  super(null, table, {});
43
- this.client = client;
44
- const doWeHaveFetch = typeof fetch !== 'undefined';
45
- const isInjectedFetchProblematic = !this.client.options.fetch;
46
- if (doWeHaveFetch) {
47
- this.fetch = fetch;
48
- }
49
- else if (isInjectedFetchProblematic) {
50
- throw new Error(errors_1.errors.falsyFetchImplementation);
56
+ _RestRepository_instances.add(this);
57
+ _RestRepository_client.set(this, void 0);
58
+ _RestRepository_fetch.set(this, void 0);
59
+ _RestRepository_table.set(this, void 0);
60
+ __classPrivateFieldSet(this, _RestRepository_client, client, "f");
61
+ __classPrivateFieldSet(this, _RestRepository_table, table, "f");
62
+ // TODO: Remove when integrating with API client
63
+ const fetchImpl = typeof fetch !== 'undefined' ? fetch : __classPrivateFieldGet(this, _RestRepository_client, "f").options.fetch;
64
+ if (!fetchImpl) {
65
+ throw new Error(`The \`fetch\` option passed to the Xata client is resolving to a falsy value and may not be correctly imported.`);
51
66
  }
52
- else {
53
- this.fetch = this.client.options.fetch;
54
- }
55
- Object.defineProperty(this, 'client', { enumerable: false });
56
- Object.defineProperty(this, 'fetch', { enumerable: false });
57
- Object.defineProperty(this, 'hostname', { enumerable: false });
58
- }
59
- request(method, path, body) {
60
- return __awaiter(this, void 0, void 0, function* () {
61
- const { databaseURL, apiKey } = this.client.options;
62
- const branch = yield this.client.getBranch();
63
- const fetchImpl = this.fetch;
64
- const resp = yield fetchImpl(`${databaseURL}:${branch}${path}`, {
65
- method,
66
- headers: {
67
- Accept: '*/*',
68
- 'Content-Type': 'application/json',
69
- Authorization: `Bearer ${apiKey}`
70
- },
71
- body: JSON.stringify(body)
72
- });
73
- if (!resp.ok) {
74
- try {
75
- const json = yield resp.json();
76
- const message = json.message;
77
- if (typeof message === 'string') {
78
- throw new XataError(message, resp.status);
79
- }
80
- }
81
- catch (err) {
82
- if (err instanceof XataError)
83
- throw err;
84
- // Ignore errors for other reasons.
85
- // For example if the response's body cannot be parsed as JSON
86
- }
87
- throw new XataError(resp.statusText, resp.status);
88
- }
89
- if (resp.status === 204)
90
- return undefined;
91
- return resp.json();
92
- });
93
- }
94
- select(columns) {
95
- return new query_1.Query(this.repository, this.table, { columns });
67
+ __classPrivateFieldSet(this, _RestRepository_fetch, fetchImpl, "f");
96
68
  }
97
69
  create(object) {
98
70
  return __awaiter(this, void 0, void 0, function* () {
71
+ const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
99
72
  const record = transformObjectLinks(object);
100
- const response = yield this.request('POST', `/tables/${this.table}/data`, record);
101
- if (!response) {
102
- throw new Error("The server didn't return any data for the query");
103
- }
73
+ const response = object.id
74
+ ? yield (0, api_1.insertRecordWithID)(Object.assign({ pathParams: {
75
+ workspace: '{workspaceId}',
76
+ dbBranchName: '{dbBranch}',
77
+ tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"),
78
+ recordId: object.id
79
+ }, body: record }, fetchProps))
80
+ : yield (0, api_1.insertRecord)(Object.assign({ pathParams: {
81
+ workspace: '{workspaceId}',
82
+ dbBranchName: '{dbBranch}',
83
+ tableName: __classPrivateFieldGet(this, _RestRepository_table, "f")
84
+ }, body: record }, fetchProps));
104
85
  const finalObject = yield this.read(response.id);
105
86
  if (!finalObject) {
106
87
  throw new Error('The server failed to save the record');
@@ -110,11 +91,9 @@ class RestRepository extends Repository {
110
91
  }
111
92
  createMany(objects) {
112
93
  return __awaiter(this, void 0, void 0, function* () {
94
+ const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
113
95
  const records = objects.map((object) => transformObjectLinks(object));
114
- const response = yield this.request('POST', `/tables/${this.table}/bulk`, { records });
115
- if (!response) {
116
- throw new Error("The server didn't return any data for the query");
117
- }
96
+ const response = yield (0, api_1.bulkInsertTableRecords)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f") }, body: { records } }, fetchProps));
118
97
  // TODO: Use filer.$any() to get all the records
119
98
  const finalObjects = yield Promise.all(response.recordIDs.map((id) => this.read(id)));
120
99
  if (finalObjects.some((object) => !object)) {
@@ -123,63 +102,76 @@ class RestRepository extends Repository {
123
102
  return finalObjects;
124
103
  });
125
104
  }
126
- read(id) {
105
+ read(recordId) {
127
106
  return __awaiter(this, void 0, void 0, function* () {
128
- try {
129
- const response = yield this.request('GET', `/tables/${this.table}/data/${id}`);
130
- if (!response)
131
- return null;
132
- return this.client.initObject(this.table, response);
133
- }
134
- catch (err) {
135
- if (err.status === 404)
136
- return null;
137
- throw err;
138
- }
107
+ const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
108
+ const response = yield (0, api_1.getRecord)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId } }, fetchProps));
109
+ return __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), response);
139
110
  });
140
111
  }
141
- update(id, object) {
112
+ update(recordId, object) {
142
113
  return __awaiter(this, void 0, void 0, function* () {
143
- const response = yield this.request('PUT', `/tables/${this.table}/data/${id}`, object);
144
- if (!response) {
145
- throw new Error("The server didn't return any data for the query");
146
- }
147
- // TODO: Review this, not sure we are properly initializing the object
148
- return this.client.initObject(this.table, response);
114
+ const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
115
+ const response = yield (0, api_1.updateRecordWithID)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId }, body: object }, fetchProps));
116
+ const item = yield this.read(response.id);
117
+ if (!item)
118
+ throw new Error('The server failed to save the record');
119
+ return item;
149
120
  });
150
121
  }
151
- delete(id) {
122
+ upsert(recordId, object) {
152
123
  return __awaiter(this, void 0, void 0, function* () {
153
- yield this.request('DELETE', `/tables/${this.table}/data/${id}`);
124
+ const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
125
+ const response = yield (0, api_1.upsertRecordWithID)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId }, body: object }, fetchProps));
126
+ const item = yield this.read(response.id);
127
+ if (!item)
128
+ throw new Error('The server failed to save the record');
129
+ return item;
154
130
  });
155
131
  }
156
- _runQuery(query, options) {
157
- var _a, _b;
132
+ delete(recordId) {
158
133
  return __awaiter(this, void 0, void 0, function* () {
159
- const filter = {
160
- $any: query.$any,
161
- $all: query.$all,
162
- $not: query.$not,
163
- $none: query.$none
164
- };
134
+ const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
135
+ yield (0, api_1.deleteRecord)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId } }, fetchProps));
136
+ });
137
+ }
138
+ query(query, options) {
139
+ var _a, _b, _c;
140
+ return __awaiter(this, void 0, void 0, function* () {
141
+ const data = query.getQueryOptions();
165
142
  const body = {
166
- filter: Object.values(filter).some(Boolean) ? filter : undefined,
167
- sort: (_a = (0, filters_1.buildSortFilter)(options === null || options === void 0 ? void 0 : options.sort)) !== null && _a !== void 0 ? _a : query.$sort,
168
- page: options === null || options === void 0 ? void 0 : options.page,
169
- columns: (_b = options === null || options === void 0 ? void 0 : options.columns) !== null && _b !== void 0 ? _b : query.columns
143
+ filter: Object.values(data.filter).some(Boolean) ? data.filter : undefined,
144
+ sort: (_a = (0, filters_1.buildSortFilter)(options === null || options === void 0 ? void 0 : options.sort)) !== null && _a !== void 0 ? _a : data.sort,
145
+ page: (_b = options === null || options === void 0 ? void 0 : options.page) !== null && _b !== void 0 ? _b : data.page,
146
+ columns: (_c = options === null || options === void 0 ? void 0 : options.columns) !== null && _c !== void 0 ? _c : data.columns
170
147
  };
171
- const response = yield this.request('POST', `/tables/${this.table}/query`, body);
172
- if (!response) {
173
- throw new Error("The server didn't return any data for the query");
174
- }
175
- const { meta, records: objects } = response;
176
- const records = objects.map((record) => this.client.initObject(this.table, record));
148
+ const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
149
+ const { meta, records: objects } = yield (0, api_1.queryTable)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f") }, body }, fetchProps));
150
+ const records = objects.map((record) => __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), record));
177
151
  // TODO: We should properly type this any
178
152
  return new pagination_1.Page(query, meta, records);
179
153
  });
180
154
  }
181
155
  }
182
156
  exports.RestRepository = RestRepository;
157
+ _RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _RestRepository_table = new WeakMap(), _RestRepository_instances = new WeakSet(), _RestRepository_getFetchProps = function _RestRepository_getFetchProps() {
158
+ return __awaiter(this, void 0, void 0, function* () {
159
+ const branch = yield __classPrivateFieldGet(this, _RestRepository_client, "f").getBranch();
160
+ return {
161
+ fetchImpl: __classPrivateFieldGet(this, _RestRepository_fetch, "f"),
162
+ apiKey: __classPrivateFieldGet(this, _RestRepository_client, "f").options.apiKey,
163
+ apiUrl: '',
164
+ // Instead of using workspace and dbBranch, we inject a probably CNAME'd URL
165
+ workspacesApiUrl: (path, params) => {
166
+ var _a, _b;
167
+ const baseUrl = (_a = __classPrivateFieldGet(this, _RestRepository_client, "f").options.databaseURL) !== null && _a !== void 0 ? _a : '';
168
+ const hasBranch = (_b = params.dbBranchName) !== null && _b !== void 0 ? _b : params.branch;
169
+ const newPath = path.replace(/^\/db\/[^/]+/, hasBranch ? `:${branch}` : '');
170
+ return baseUrl + newPath;
171
+ }
172
+ };
173
+ });
174
+ };
183
175
  class RestRespositoryFactory {
184
176
  createRepository(client, table) {
185
177
  return new RestRepository(client, table);
@@ -188,16 +180,18 @@ class RestRespositoryFactory {
188
180
  exports.RestRespositoryFactory = RestRespositoryFactory;
189
181
  class BaseClient {
190
182
  constructor(options, links) {
183
+ _BaseClient_links.set(this, void 0);
184
+ _BaseClient_branch.set(this, void 0);
191
185
  if (!options.databaseURL || !options.apiKey || !options.branch) {
192
186
  throw new Error('Options databaseURL, apiKey and branch are required');
193
187
  }
194
188
  this.options = options;
195
- this.links = links;
189
+ __classPrivateFieldSet(this, _BaseClient_links, links, "f");
196
190
  }
197
191
  initObject(table, object) {
198
192
  const o = {};
199
193
  Object.assign(o, object);
200
- const tableLinks = this.links[table] || [];
194
+ const tableLinks = __classPrivateFieldGet(this, _BaseClient_links, "f")[table] || [];
201
195
  for (const link of tableLinks) {
202
196
  const [field, linkTable] = link;
203
197
  const value = o[field];
@@ -236,8 +230,8 @@ class BaseClient {
236
230
  getBranch() {
237
231
  var e_1, _a;
238
232
  return __awaiter(this, void 0, void 0, function* () {
239
- if (this.branch)
240
- return this.branch;
233
+ if (__classPrivateFieldGet(this, _BaseClient_branch, "f"))
234
+ return __classPrivateFieldGet(this, _BaseClient_branch, "f");
241
235
  const { branch: param } = this.options;
242
236
  const strategies = Array.isArray(param) ? [...param] : [param];
243
237
  const evaluateBranch = (strategy) => __awaiter(this, void 0, void 0, function* () {
@@ -248,7 +242,7 @@ class BaseClient {
248
242
  const strategy = strategies_1_1.value;
249
243
  const branch = yield evaluateBranch(strategy);
250
244
  if (branch) {
251
- this.branch = branch;
245
+ __classPrivateFieldSet(this, _BaseClient_branch, branch, "f");
252
246
  return branch;
253
247
  }
254
248
  }
@@ -265,6 +259,7 @@ class BaseClient {
265
259
  }
266
260
  }
267
261
  exports.BaseClient = BaseClient;
262
+ _BaseClient_links = new WeakMap(), _BaseClient_branch = new WeakMap();
268
263
  class XataError extends Error {
269
264
  constructor(message, status) {
270
265
  super(message);
@@ -284,4 +279,5 @@ const transformObjectLinks = (object) => {
284
279
  return Object.assign(Object.assign({}, acc), { [key]: value });
285
280
  }, {});
286
281
  };
282
+ __exportStar(require("./api"), exports);
287
283
  __exportStar(require("./schema"), exports);
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -6,8 +6,7 @@ export declare type PaginationQueryMeta = {
6
6
  more: boolean;
7
7
  };
8
8
  };
9
- export interface BasePage<T extends XataRecord, R extends XataRecord> {
10
- query: Query<T, R>;
9
+ export interface Paginable<T extends XataRecord, R extends XataRecord = T> {
11
10
  meta: PaginationQueryMeta;
12
11
  records: R[];
13
12
  nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
@@ -16,8 +15,8 @@ export interface BasePage<T extends XataRecord, R extends XataRecord> {
16
15
  lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
17
16
  hasNextPage(): boolean;
18
17
  }
19
- export declare class Page<T extends XataRecord, R extends XataRecord> implements BasePage<T, R> {
20
- readonly query: Query<T, R>;
18
+ export declare class Page<T extends XataRecord, R extends XataRecord> implements Paginable<T, R> {
19
+ #private;
21
20
  readonly meta: PaginationQueryMeta;
22
21
  readonly records: R[];
23
22
  constructor(query: Query<T, R>, meta: PaginationQueryMeta, records?: R[]);
@@ -8,32 +8,45 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
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 _Page_query;
11
23
  Object.defineProperty(exports, "__esModule", { value: true });
12
24
  exports.Page = void 0;
13
25
  class Page {
14
26
  constructor(query, meta, records = []) {
15
- this.query = query;
27
+ _Page_query.set(this, void 0);
28
+ __classPrivateFieldSet(this, _Page_query, query, "f");
16
29
  this.meta = meta;
17
30
  this.records = records;
18
31
  }
19
32
  nextPage(size, offset) {
20
33
  return __awaiter(this, void 0, void 0, function* () {
21
- return this.query.getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
34
+ return __classPrivateFieldGet(this, _Page_query, "f").getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
22
35
  });
23
36
  }
24
37
  previousPage(size, offset) {
25
38
  return __awaiter(this, void 0, void 0, function* () {
26
- return this.query.getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
39
+ return __classPrivateFieldGet(this, _Page_query, "f").getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
27
40
  });
28
41
  }
29
42
  firstPage(size, offset) {
30
43
  return __awaiter(this, void 0, void 0, function* () {
31
- return this.query.getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
44
+ return __classPrivateFieldGet(this, _Page_query, "f").getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
32
45
  });
33
46
  }
34
47
  lastPage(size, offset) {
35
48
  return __awaiter(this, void 0, void 0, function* () {
36
- return this.query.getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
49
+ return __classPrivateFieldGet(this, _Page_query, "f").getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
37
50
  });
38
51
  }
39
52
  // TODO: We need to add something on the backend if we want a hasPreviousPage
@@ -42,3 +55,4 @@ class Page {
42
55
  }
43
56
  }
44
57
  exports.Page = Page;
58
+ _Page_query = new WeakMap();
@@ -1,26 +1,25 @@
1
1
  import { XataRecord, Repository } from '..';
2
- import { Constraint, DeepConstraint, FilterConstraints, SortDirection, SortFilter } from './filters';
3
- import { PaginationOptions, BasePage, PaginationQueryMeta, Page } from './pagination';
2
+ import { FilterExpression, SortExpression, PageConfig, ColumnsFilter } from '../api/schemas';
3
+ import { DeepConstraint, FilterConstraints, SortDirection, SortFilter } from './filters';
4
+ import { PaginationOptions, Page, Paginable, PaginationQueryMeta } from './pagination';
4
5
  import { Selectable, SelectableColumn, Select } from './selection';
5
- export declare type QueryOptions<T> = {
6
+ export declare type QueryOptions<T extends XataRecord> = {
6
7
  page?: PaginationOptions;
7
- columns?: Array<keyof Selectable<T>>;
8
+ columns?: Extract<keyof Selectable<T>, string>[];
8
9
  sort?: SortFilter<T> | SortFilter<T>[];
9
10
  };
10
- declare type QueryOrConstraint<T extends XataRecord, R extends XataRecord> = Query<T, R> | Constraint<T>;
11
- export declare class Query<T extends XataRecord, R extends XataRecord = T> implements BasePage<T, R> {
12
- table: string;
13
- repository: Repository<T>;
14
- readonly $any?: QueryOrConstraint<T, R>[];
15
- readonly $all?: QueryOrConstraint<T, R>[];
16
- readonly $not?: QueryOrConstraint<T, R>[];
17
- readonly $none?: QueryOrConstraint<T, R>[];
18
- readonly $sort?: Record<string, SortDirection>;
19
- readonly columns: SelectableColumn<T>[];
20
- readonly query: Query<T, R>;
11
+ export declare type QueryTableOptions = {
12
+ filter: FilterExpression;
13
+ sort?: SortExpression;
14
+ page?: PageConfig;
15
+ columns?: ColumnsFilter;
16
+ };
17
+ export declare class Query<T extends XataRecord, R extends XataRecord = T> implements Paginable<T, R> {
18
+ #private;
21
19
  readonly meta: PaginationQueryMeta;
22
20
  readonly records: R[];
23
- constructor(repository: Repository<T> | null, table: string, data: Partial<Query<T, R>>, parent?: Query<T, R>);
21
+ constructor(repository: Repository<T> | null, table: string, data: Partial<QueryTableOptions>, parent?: Partial<QueryTableOptions>);
22
+ getQueryOptions(): QueryTableOptions;
24
23
  any(...queries: Query<T, R>[]): Query<T, R>;
25
24
  all(...queries: Query<T, R>[]): Query<T, R>;
26
25
  not(...queries: Query<T, R>[]): Query<T, R>;
@@ -28,16 +27,19 @@ export declare class Query<T extends XataRecord, R extends XataRecord = T> imple
28
27
  filter(constraints: FilterConstraints<T>): Query<T, R>;
29
28
  filter<F extends keyof T>(column: F, value: FilterConstraints<T[F]> | DeepConstraint<T[F]>): Query<T, R>;
30
29
  sort<F extends keyof T>(column: F, direction: SortDirection): Query<T, R>;
30
+ select<K extends SelectableColumn<T>>(columns: K[]): Query<T, Select<T, K>>;
31
31
  getPaginated<Options extends QueryOptions<T>>(options?: Options): Promise<Page<T, typeof options['columns'] extends SelectableColumn<T>[] ? Select<T, typeof options['columns'][number]> : R>>;
32
32
  [Symbol.asyncIterator](): AsyncIterableIterator<R>;
33
33
  getIterator(chunk: number, options?: Omit<QueryOptions<T>, 'page'>): AsyncGenerator<R[]>;
34
34
  getMany<Options extends QueryOptions<T>>(options?: Options): Promise<(typeof options['columns'] extends SelectableColumn<T>[] ? Select<T, typeof options['columns'][number]> : R)[]>;
35
35
  getOne<Options extends Omit<QueryOptions<T>, 'page'>>(options?: Options): Promise<(typeof options['columns'] extends SelectableColumn<T>[] ? Select<T, typeof options['columns'][number]> : R) | null>;
36
- deleteAll(): Promise<number>;
36
+ /**async deleteAll(): Promise<number> {
37
+ // TODO: Return number of affected rows
38
+ return 0;
39
+ }**/
37
40
  nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
38
41
  previousPage(size?: number, offset?: number): Promise<Page<T, R>>;
39
42
  firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
40
43
  lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
41
44
  hasNextPage(): boolean;
42
45
  }
43
- export {};