@xata.io/client 0.0.0-beta.7fe10d6 → 0.0.0-beta.9b83636
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/api/client.d.ts +90 -0
- package/dist/api/client.js +220 -0
- package/dist/api/components.d.ts +1430 -0
- package/dist/api/components.js +1001 -0
- package/dist/api/fetcher.d.ts +25 -0
- package/dist/api/fetcher.js +78 -0
- package/dist/api/index.d.ts +7 -0
- package/dist/api/index.js +21 -0
- package/dist/api/parameters.d.ts +16 -0
- package/dist/api/parameters.js +2 -0
- package/dist/api/providers.d.ts +8 -0
- package/dist/api/providers.js +29 -0
- package/dist/api/responses.d.ts +38 -0
- package/dist/api/responses.js +2 -0
- package/dist/api/schemas.d.ts +311 -0
- package/dist/api/schemas.js +2 -0
- package/dist/index.d.ts +18 -141
- package/dist/index.js +101 -350
- package/dist/schema/filters.d.ts +20 -0
- package/dist/schema/filters.js +24 -0
- package/dist/schema/index.d.ts +1 -0
- package/dist/schema/index.js +17 -0
- package/dist/schema/operators.d.ts +21 -0
- package/dist/schema/operators.js +40 -0
- package/dist/schema/pagination.d.ts +41 -0
- package/dist/schema/pagination.js +58 -0
- package/dist/schema/query.d.ts +45 -0
- package/dist/schema/query.js +190 -0
- package/dist/schema/selection.d.ts +18 -0
- package/dist/schema/selection.js +2 -0
- package/dist/util/lang.d.ts +1 -0
- package/dist/util/lang.js +10 -0
- package/dist/util/types.d.ts +3 -0
- package/dist/util/types.js +2 -0
- package/package.json +2 -2
- package/dist/util/errors.d.ts +0 -3
- package/dist/util/errors.js +0 -9
package/dist/index.js
CHANGED
@@ -1,4 +1,18 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = 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);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
2
16
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
17
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
18
|
return new (P || (P = Promise))(function (resolve, reject) {
|
@@ -8,6 +22,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
8
22
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
23
|
});
|
10
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
|
+
};
|
11
36
|
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
12
37
|
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
13
38
|
var m = o[Symbol.asyncIterator], i;
|
@@ -15,318 +40,37 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
15
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); }); }; }
|
16
41
|
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
17
42
|
};
|
18
|
-
var
|
19
|
-
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
|
20
|
-
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
21
|
-
var g = generator.apply(thisArg, _arguments || []), i, q = [];
|
22
|
-
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
|
23
|
-
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); }); }; }
|
24
|
-
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
|
25
|
-
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
|
26
|
-
function fulfill(value) { resume("next", value); }
|
27
|
-
function reject(value) { resume("throw", value); }
|
28
|
-
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
|
29
|
-
};
|
43
|
+
var _RestRepository_instances, _RestRepository_client, _RestRepository_fetch, _RestRepository_table, _RestRepository_getFetchProps, _BaseClient_links, _BaseClient_branch;
|
30
44
|
Object.defineProperty(exports, "__esModule", { value: true });
|
31
|
-
exports.XataError = exports.BaseClient = exports.RestRespositoryFactory = exports.RestRepository = exports.Repository =
|
32
|
-
const
|
33
|
-
const
|
34
|
-
|
35
|
-
const
|
36
|
-
|
37
|
-
const gte = (value) => ({ $ge: value });
|
38
|
-
exports.gte = gte;
|
39
|
-
const lt = (value) => ({ $lt: value });
|
40
|
-
exports.lt = lt;
|
41
|
-
const lte = (value) => ({ $le: value });
|
42
|
-
exports.lte = lte;
|
43
|
-
const le = (value) => ({ $le: value });
|
44
|
-
exports.le = le;
|
45
|
-
const exists = (column) => ({ $exists: column });
|
46
|
-
exports.exists = exists;
|
47
|
-
const notExists = (column) => ({ $notExists: column });
|
48
|
-
exports.notExists = notExists;
|
49
|
-
const startsWith = (value) => ({ $startsWith: value });
|
50
|
-
exports.startsWith = startsWith;
|
51
|
-
const endsWith = (value) => ({ $endsWith: value });
|
52
|
-
exports.endsWith = endsWith;
|
53
|
-
const pattern = (value) => ({ $pattern: value });
|
54
|
-
exports.pattern = pattern;
|
55
|
-
const is = (value) => ({ $is: value });
|
56
|
-
exports.is = is;
|
57
|
-
const isNot = (value) => ({ $isNot: value });
|
58
|
-
exports.isNot = isNot;
|
59
|
-
const contains = (value) => ({ $contains: value });
|
60
|
-
exports.contains = contains;
|
61
|
-
// TODO: these can only be applied to columns of type "multiple"
|
62
|
-
const includes = (value) => ({ $includes: value });
|
63
|
-
exports.includes = includes;
|
64
|
-
const includesSubstring = (value) => ({ $includesSubstring: value });
|
65
|
-
exports.includesSubstring = includesSubstring;
|
66
|
-
const includesPattern = (value) => ({ $includesPattern: value });
|
67
|
-
exports.includesPattern = includesPattern;
|
68
|
-
const includesAll = (value) => ({ $includesAll: value });
|
69
|
-
exports.includesAll = includesAll;
|
70
|
-
class Page {
|
71
|
-
constructor(query, meta, records = []) {
|
72
|
-
this.query = query;
|
73
|
-
this.meta = meta;
|
74
|
-
this.records = records;
|
75
|
-
}
|
76
|
-
nextPage(size, offset) {
|
77
|
-
return __awaiter(this, void 0, void 0, function* () {
|
78
|
-
return this.query.getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
|
79
|
-
});
|
80
|
-
}
|
81
|
-
previousPage(size, offset) {
|
82
|
-
return __awaiter(this, void 0, void 0, function* () {
|
83
|
-
return this.query.getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
|
84
|
-
});
|
85
|
-
}
|
86
|
-
firstPage(size, offset) {
|
87
|
-
return __awaiter(this, void 0, void 0, function* () {
|
88
|
-
return this.query.getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
|
89
|
-
});
|
90
|
-
}
|
91
|
-
lastPage(size, offset) {
|
92
|
-
return __awaiter(this, void 0, void 0, function* () {
|
93
|
-
return this.query.getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
|
94
|
-
});
|
95
|
-
}
|
96
|
-
// TODO: We need to add something on the backend if we want a hasPreviousPage
|
97
|
-
hasNextPage() {
|
98
|
-
return this.meta.page.more;
|
99
|
-
}
|
100
|
-
}
|
101
|
-
class Query {
|
102
|
-
constructor(repository, table, data, parent) {
|
103
|
-
// Cursor pagination
|
104
|
-
this.query = this;
|
105
|
-
this.meta = { page: { cursor: 'start', more: true } };
|
106
|
-
this.records = [];
|
107
|
-
if (repository) {
|
108
|
-
this.repository = repository;
|
109
|
-
}
|
110
|
-
else {
|
111
|
-
this.repository = this;
|
112
|
-
}
|
113
|
-
this.table = table;
|
114
|
-
// For some reason Object.assign(this, parent) didn't work in this case
|
115
|
-
// so doing all this manually:
|
116
|
-
this.$any = parent === null || parent === void 0 ? void 0 : parent.$any;
|
117
|
-
this.$all = parent === null || parent === void 0 ? void 0 : parent.$all;
|
118
|
-
this.$not = parent === null || parent === void 0 ? void 0 : parent.$not;
|
119
|
-
this.$none = parent === null || parent === void 0 ? void 0 : parent.$none;
|
120
|
-
this.$sort = parent === null || parent === void 0 ? void 0 : parent.$sort;
|
121
|
-
Object.assign(this, data);
|
122
|
-
// These bindings are used to support deconstructing
|
123
|
-
// const { any, not, filter, sort } = xata.users.query()
|
124
|
-
this.any = this.any.bind(this);
|
125
|
-
this.all = this.all.bind(this);
|
126
|
-
this.not = this.not.bind(this);
|
127
|
-
this.filter = this.filter.bind(this);
|
128
|
-
this.sort = this.sort.bind(this);
|
129
|
-
this.none = this.none.bind(this);
|
130
|
-
Object.defineProperty(this, 'table', { enumerable: false });
|
131
|
-
Object.defineProperty(this, 'repository', { enumerable: false });
|
132
|
-
}
|
133
|
-
any(...queries) {
|
134
|
-
return new Query(this.repository, this.table, {
|
135
|
-
$any: (this.$any || []).concat(queries)
|
136
|
-
}, this);
|
137
|
-
}
|
138
|
-
all(...queries) {
|
139
|
-
return new Query(this.repository, this.table, {
|
140
|
-
$all: (this.$all || []).concat(queries)
|
141
|
-
}, this);
|
142
|
-
}
|
143
|
-
not(...queries) {
|
144
|
-
return new Query(this.repository, this.table, {
|
145
|
-
$not: (this.$not || []).concat(queries)
|
146
|
-
}, this);
|
147
|
-
}
|
148
|
-
none(...queries) {
|
149
|
-
return new Query(this.repository, this.table, {
|
150
|
-
$none: (this.$none || []).concat(queries)
|
151
|
-
}, this);
|
152
|
-
}
|
153
|
-
filter(a, b) {
|
154
|
-
if (arguments.length === 1) {
|
155
|
-
const constraints = a;
|
156
|
-
const queries = [];
|
157
|
-
for (const [column, constraint] of Object.entries(constraints)) {
|
158
|
-
queries.push({ [column]: constraint });
|
159
|
-
}
|
160
|
-
return new Query(this.repository, this.table, {
|
161
|
-
$all: (this.$all || []).concat(queries)
|
162
|
-
}, this);
|
163
|
-
}
|
164
|
-
else {
|
165
|
-
const column = a;
|
166
|
-
const value = b;
|
167
|
-
return new Query(this.repository, this.table, {
|
168
|
-
$all: (this.$all || []).concat({ [column]: value })
|
169
|
-
}, this);
|
170
|
-
}
|
171
|
-
}
|
172
|
-
sort(column, direction) {
|
173
|
-
const sort = Object.assign(Object.assign({}, this.$sort), { [column]: direction });
|
174
|
-
const q = new Query(this.repository, this.table, {
|
175
|
-
$sort: sort
|
176
|
-
}, this);
|
177
|
-
return q;
|
178
|
-
}
|
179
|
-
getPaginated(options) {
|
180
|
-
return __awaiter(this, void 0, void 0, function* () {
|
181
|
-
return this.repository._runQuery(this, options);
|
182
|
-
});
|
183
|
-
}
|
184
|
-
[Symbol.asyncIterator]() {
|
185
|
-
return __asyncGenerator(this, arguments, function* _a() {
|
186
|
-
var e_1, _b;
|
187
|
-
try {
|
188
|
-
for (var _c = __asyncValues(this.getIterator(1)), _d; _d = yield __await(_c.next()), !_d.done;) {
|
189
|
-
const [record] = _d.value;
|
190
|
-
yield yield __await(record);
|
191
|
-
}
|
192
|
-
}
|
193
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
194
|
-
finally {
|
195
|
-
try {
|
196
|
-
if (_d && !_d.done && (_b = _c.return)) yield __await(_b.call(_c));
|
197
|
-
}
|
198
|
-
finally { if (e_1) throw e_1.error; }
|
199
|
-
}
|
200
|
-
});
|
201
|
-
}
|
202
|
-
getIterator(chunk, options = {}) {
|
203
|
-
return __asyncGenerator(this, arguments, function* getIterator_1() {
|
204
|
-
let offset = 0;
|
205
|
-
let end = false;
|
206
|
-
while (!end) {
|
207
|
-
const { records, meta } = yield __await(this.getPaginated(Object.assign(Object.assign({}, options), { page: { size: chunk, offset } })));
|
208
|
-
yield yield __await(records);
|
209
|
-
offset += chunk;
|
210
|
-
end = !meta.page.more;
|
211
|
-
}
|
212
|
-
});
|
213
|
-
}
|
214
|
-
getMany(options) {
|
215
|
-
return __awaiter(this, void 0, void 0, function* () {
|
216
|
-
const { records } = yield this.getPaginated(options);
|
217
|
-
return records;
|
218
|
-
});
|
219
|
-
}
|
220
|
-
getOne(options = {}) {
|
221
|
-
return __awaiter(this, void 0, void 0, function* () {
|
222
|
-
const records = yield this.getMany(Object.assign(Object.assign({}, options), { page: { size: 1 } }));
|
223
|
-
return records[0] || null;
|
224
|
-
});
|
225
|
-
}
|
226
|
-
deleteAll() {
|
227
|
-
return __awaiter(this, void 0, void 0, function* () {
|
228
|
-
// TODO: Return number of affected rows
|
229
|
-
return 0;
|
230
|
-
});
|
231
|
-
}
|
232
|
-
include(columns) {
|
233
|
-
// TODO
|
234
|
-
return this;
|
235
|
-
}
|
236
|
-
nextPage(size, offset) {
|
237
|
-
return __awaiter(this, void 0, void 0, function* () {
|
238
|
-
return this.firstPage(size, offset);
|
239
|
-
});
|
240
|
-
}
|
241
|
-
previousPage(size, offset) {
|
242
|
-
return __awaiter(this, void 0, void 0, function* () {
|
243
|
-
return this.firstPage(size, offset);
|
244
|
-
});
|
245
|
-
}
|
246
|
-
firstPage(size, offset) {
|
247
|
-
return __awaiter(this, void 0, void 0, function* () {
|
248
|
-
return this.getPaginated({ page: { size, offset } });
|
249
|
-
});
|
250
|
-
}
|
251
|
-
lastPage(size, offset) {
|
252
|
-
return __awaiter(this, void 0, void 0, function* () {
|
253
|
-
return this.getPaginated({ page: { size, offset, before: 'end' } });
|
254
|
-
});
|
255
|
-
}
|
256
|
-
hasNextPage() {
|
257
|
-
return this.meta.page.more;
|
258
|
-
}
|
259
|
-
}
|
260
|
-
exports.Query = Query;
|
261
|
-
class Repository extends Query {
|
262
|
-
select(...columns) {
|
263
|
-
return new Query(this.repository, this.table, {});
|
264
|
-
}
|
45
|
+
exports.XataError = exports.BaseClient = exports.RestRespositoryFactory = exports.RestRepository = exports.Repository = void 0;
|
46
|
+
const api_1 = require("./api");
|
47
|
+
const filters_1 = require("./schema/filters");
|
48
|
+
const pagination_1 = require("./schema/pagination");
|
49
|
+
const query_1 = require("./schema/query");
|
50
|
+
class Repository extends query_1.Query {
|
265
51
|
}
|
266
52
|
exports.Repository = Repository;
|
267
53
|
class RestRepository extends Repository {
|
268
54
|
constructor(client, table) {
|
269
55
|
super(null, table, {});
|
270
|
-
this
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
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.`);
|
278
66
|
}
|
279
|
-
|
280
|
-
this.fetch = this.client.options.fetch;
|
281
|
-
}
|
282
|
-
Object.defineProperty(this, 'client', { enumerable: false });
|
283
|
-
Object.defineProperty(this, 'fetch', { enumerable: false });
|
284
|
-
Object.defineProperty(this, 'hostname', { enumerable: false });
|
285
|
-
}
|
286
|
-
request(method, path, body) {
|
287
|
-
return __awaiter(this, void 0, void 0, function* () {
|
288
|
-
const { databaseURL, apiKey } = this.client.options;
|
289
|
-
const branch = yield this.client.getBranch();
|
290
|
-
const resp = yield this.fetch(`${databaseURL}:${branch}${path}`, {
|
291
|
-
method,
|
292
|
-
headers: {
|
293
|
-
Accept: '*/*',
|
294
|
-
'Content-Type': 'application/json',
|
295
|
-
Authorization: `Bearer ${apiKey}`
|
296
|
-
},
|
297
|
-
body: JSON.stringify(body)
|
298
|
-
});
|
299
|
-
if (!resp.ok) {
|
300
|
-
try {
|
301
|
-
const json = yield resp.json();
|
302
|
-
const message = json.message;
|
303
|
-
if (typeof message === 'string') {
|
304
|
-
throw new XataError(message, resp.status);
|
305
|
-
}
|
306
|
-
}
|
307
|
-
catch (err) {
|
308
|
-
if (err instanceof XataError)
|
309
|
-
throw err;
|
310
|
-
// Ignore errors for other reasons.
|
311
|
-
// For example if the response's body cannot be parsed as JSON
|
312
|
-
}
|
313
|
-
throw new XataError(resp.statusText, resp.status);
|
314
|
-
}
|
315
|
-
if (resp.status === 204)
|
316
|
-
return undefined;
|
317
|
-
return resp.json();
|
318
|
-
});
|
319
|
-
}
|
320
|
-
select(...columns) {
|
321
|
-
return new Query(this.repository, this.table, {});
|
67
|
+
__classPrivateFieldSet(this, _RestRepository_fetch, fetchImpl, "f");
|
322
68
|
}
|
323
69
|
create(object) {
|
324
70
|
return __awaiter(this, void 0, void 0, function* () {
|
71
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
325
72
|
const record = transformObjectLinks(object);
|
326
|
-
const response = yield
|
327
|
-
if (!response) {
|
328
|
-
throw new Error("The server didn't return any data for the query");
|
329
|
-
}
|
73
|
+
const response = yield (0, api_1.insertRecord)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f") }, body: record }, fetchProps));
|
330
74
|
const finalObject = yield this.read(response.id);
|
331
75
|
if (!finalObject) {
|
332
76
|
throw new Error('The server failed to save the record');
|
@@ -336,11 +80,9 @@ class RestRepository extends Repository {
|
|
336
80
|
}
|
337
81
|
createMany(objects) {
|
338
82
|
return __awaiter(this, void 0, void 0, function* () {
|
83
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
339
84
|
const records = objects.map((object) => transformObjectLinks(object));
|
340
|
-
const response = yield
|
341
|
-
if (!response) {
|
342
|
-
throw new Error("The server didn't return any data for the query");
|
343
|
-
}
|
85
|
+
const response = yield (0, api_1.bulkInsertTableRecords)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f") }, body: { records } }, fetchProps));
|
344
86
|
// TODO: Use filer.$any() to get all the records
|
345
87
|
const finalObjects = yield Promise.all(response.recordIDs.map((id) => this.read(id)));
|
346
88
|
if (finalObjects.some((object) => !object)) {
|
@@ -349,60 +91,64 @@ class RestRepository extends Repository {
|
|
349
91
|
return finalObjects;
|
350
92
|
});
|
351
93
|
}
|
352
|
-
read(
|
94
|
+
read(recordId) {
|
353
95
|
return __awaiter(this, void 0, void 0, function* () {
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
return null;
|
358
|
-
return this.client.initObject(this.table, response);
|
359
|
-
}
|
360
|
-
catch (err) {
|
361
|
-
if (err.status === 404)
|
362
|
-
return null;
|
363
|
-
throw err;
|
364
|
-
}
|
96
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
97
|
+
const response = yield (0, api_1.getRecord)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId } }, fetchProps));
|
98
|
+
return __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), response);
|
365
99
|
});
|
366
100
|
}
|
367
|
-
update(
|
101
|
+
update(recordId, object) {
|
368
102
|
return __awaiter(this, void 0, void 0, function* () {
|
369
|
-
const
|
370
|
-
|
371
|
-
throw new Error("The server didn't return any data for the query");
|
372
|
-
}
|
103
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
104
|
+
const response = yield (0, api_1.insertRecordWithID)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId }, body: object }, fetchProps));
|
373
105
|
// TODO: Review this, not sure we are properly initializing the object
|
374
|
-
return this.
|
106
|
+
return __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), response);
|
375
107
|
});
|
376
108
|
}
|
377
|
-
delete(
|
109
|
+
delete(recordId) {
|
378
110
|
return __awaiter(this, void 0, void 0, function* () {
|
379
|
-
yield this.
|
111
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
112
|
+
yield (0, api_1.deleteRecord)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId } }, fetchProps));
|
380
113
|
});
|
381
114
|
}
|
382
|
-
|
115
|
+
query(query, options) {
|
116
|
+
var _a, _b, _c;
|
383
117
|
return __awaiter(this, void 0, void 0, function* () {
|
384
|
-
const
|
385
|
-
$any: query.$any,
|
386
|
-
$all: query.$all,
|
387
|
-
$not: query.$not,
|
388
|
-
$none: query.$none
|
389
|
-
};
|
118
|
+
const data = query.getQueryOptions();
|
390
119
|
const body = {
|
391
|
-
filter: Object.values(filter).some(Boolean) ? filter : undefined,
|
392
|
-
sort:
|
393
|
-
page: options === null || options === void 0 ? void 0 : options.page
|
120
|
+
filter: Object.values(data.filter).some(Boolean) ? data.filter : undefined,
|
121
|
+
sort: (_a = (0, filters_1.buildSortFilter)(options === null || options === void 0 ? void 0 : options.sort)) !== null && _a !== void 0 ? _a : data.sort,
|
122
|
+
page: (_b = options === null || options === void 0 ? void 0 : options.page) !== null && _b !== void 0 ? _b : data.page,
|
123
|
+
columns: (_c = options === null || options === void 0 ? void 0 : options.columns) !== null && _c !== void 0 ? _c : data.columns
|
394
124
|
};
|
395
|
-
const
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
const records = objects.map((record) => this.client.initObject(this.table, record));
|
401
|
-
return new Page(query, meta, records);
|
125
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
126
|
+
const { meta, records: objects } = yield (0, api_1.queryTable)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f") }, body }, fetchProps));
|
127
|
+
const records = objects.map((record) => __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), record));
|
128
|
+
// TODO: We should properly type this any
|
129
|
+
return new pagination_1.Page(query, meta, records);
|
402
130
|
});
|
403
131
|
}
|
404
132
|
}
|
405
133
|
exports.RestRepository = RestRepository;
|
134
|
+
_RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _RestRepository_table = new WeakMap(), _RestRepository_instances = new WeakSet(), _RestRepository_getFetchProps = function _RestRepository_getFetchProps() {
|
135
|
+
return __awaiter(this, void 0, void 0, function* () {
|
136
|
+
const branch = yield __classPrivateFieldGet(this, _RestRepository_client, "f").getBranch();
|
137
|
+
return {
|
138
|
+
fetchImpl: __classPrivateFieldGet(this, _RestRepository_fetch, "f"),
|
139
|
+
apiKey: __classPrivateFieldGet(this, _RestRepository_client, "f").options.apiKey,
|
140
|
+
apiUrl: '',
|
141
|
+
// Instead of using workspace and dbBranch, we inject a probably CNAME'd URL
|
142
|
+
workspacesApiUrl: (path, params) => {
|
143
|
+
var _a, _b;
|
144
|
+
const baseUrl = (_a = __classPrivateFieldGet(this, _RestRepository_client, "f").options.databaseURL) !== null && _a !== void 0 ? _a : '';
|
145
|
+
const hasBranch = (_b = params.dbBranchName) !== null && _b !== void 0 ? _b : params.branch;
|
146
|
+
const newPath = path.replace(/^\/db\/[^/]+/, hasBranch ? `:${branch}` : '');
|
147
|
+
return baseUrl + newPath;
|
148
|
+
}
|
149
|
+
};
|
150
|
+
});
|
151
|
+
};
|
406
152
|
class RestRespositoryFactory {
|
407
153
|
createRepository(client, table) {
|
408
154
|
return new RestRepository(client, table);
|
@@ -411,16 +157,18 @@ class RestRespositoryFactory {
|
|
411
157
|
exports.RestRespositoryFactory = RestRespositoryFactory;
|
412
158
|
class BaseClient {
|
413
159
|
constructor(options, links) {
|
160
|
+
_BaseClient_links.set(this, void 0);
|
161
|
+
_BaseClient_branch.set(this, void 0);
|
414
162
|
if (!options.databaseURL || !options.apiKey || !options.branch) {
|
415
163
|
throw new Error('Options databaseURL, apiKey and branch are required');
|
416
164
|
}
|
417
165
|
this.options = options;
|
418
|
-
this
|
166
|
+
__classPrivateFieldSet(this, _BaseClient_links, links, "f");
|
419
167
|
}
|
420
168
|
initObject(table, object) {
|
421
169
|
const o = {};
|
422
170
|
Object.assign(o, object);
|
423
|
-
const tableLinks = this
|
171
|
+
const tableLinks = __classPrivateFieldGet(this, _BaseClient_links, "f")[table] || [];
|
424
172
|
for (const link of tableLinks) {
|
425
173
|
const [field, linkTable] = link;
|
426
174
|
const value = o[field];
|
@@ -457,10 +205,10 @@ class BaseClient {
|
|
457
205
|
return o;
|
458
206
|
}
|
459
207
|
getBranch() {
|
460
|
-
var
|
208
|
+
var e_1, _a;
|
461
209
|
return __awaiter(this, void 0, void 0, function* () {
|
462
|
-
if (this
|
463
|
-
return this
|
210
|
+
if (__classPrivateFieldGet(this, _BaseClient_branch, "f"))
|
211
|
+
return __classPrivateFieldGet(this, _BaseClient_branch, "f");
|
464
212
|
const { branch: param } = this.options;
|
465
213
|
const strategies = Array.isArray(param) ? [...param] : [param];
|
466
214
|
const evaluateBranch = (strategy) => __awaiter(this, void 0, void 0, function* () {
|
@@ -471,23 +219,24 @@ class BaseClient {
|
|
471
219
|
const strategy = strategies_1_1.value;
|
472
220
|
const branch = yield evaluateBranch(strategy);
|
473
221
|
if (branch) {
|
474
|
-
this
|
222
|
+
__classPrivateFieldSet(this, _BaseClient_branch, branch, "f");
|
475
223
|
return branch;
|
476
224
|
}
|
477
225
|
}
|
478
226
|
}
|
479
|
-
catch (
|
227
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
480
228
|
finally {
|
481
229
|
try {
|
482
230
|
if (strategies_1_1 && !strategies_1_1.done && (_a = strategies_1.return)) yield _a.call(strategies_1);
|
483
231
|
}
|
484
|
-
finally { if (
|
232
|
+
finally { if (e_1) throw e_1.error; }
|
485
233
|
}
|
486
234
|
throw new Error('Unable to resolve branch value');
|
487
235
|
});
|
488
236
|
}
|
489
237
|
}
|
490
238
|
exports.BaseClient = BaseClient;
|
239
|
+
_BaseClient_links = new WeakMap(), _BaseClient_branch = new WeakMap();
|
491
240
|
class XataError extends Error {
|
492
241
|
constructor(message, status) {
|
493
242
|
super(message);
|
@@ -507,3 +256,5 @@ const transformObjectLinks = (object) => {
|
|
507
256
|
return Object.assign(Object.assign({}, acc), { [key]: value });
|
508
257
|
}, {});
|
509
258
|
};
|
259
|
+
__exportStar(require("./api"), exports);
|
260
|
+
__exportStar(require("./schema"), exports);
|
@@ -0,0 +1,20 @@
|
|
1
|
+
export declare type SortDirection = 'asc' | 'desc';
|
2
|
+
export declare type SortFilterExtended<T> = {
|
3
|
+
column: keyof T;
|
4
|
+
direction?: SortDirection;
|
5
|
+
};
|
6
|
+
export declare type SortFilter<T> = SortFilterExtended<T> | keyof T;
|
7
|
+
export declare function isSortFilterObject<T>(filter: SortFilter<T>): filter is SortFilterExtended<T>;
|
8
|
+
export declare type FilterOperator = '$gt' | '$lt' | '$ge' | '$le' | '$exists' | '$notExists' | '$endsWith' | '$startsWith' | '$pattern' | '$is' | '$isNot' | '$contains' | '$includes' | '$includesSubstring' | '$includesPattern' | '$includesAll';
|
9
|
+
export declare function buildSortFilter<T>(filter?: SortFilter<T> | SortFilter<T>[]): {
|
10
|
+
[key: string]: SortDirection;
|
11
|
+
} | undefined;
|
12
|
+
export declare type Constraint<T> = {
|
13
|
+
[key in FilterOperator]?: T;
|
14
|
+
};
|
15
|
+
export declare type DeepConstraint<T> = T extends Record<string, any> ? {
|
16
|
+
[key in keyof T]?: T[key] | DeepConstraint<T[key]>;
|
17
|
+
} : Constraint<T>;
|
18
|
+
export declare type FilterConstraints<T> = {
|
19
|
+
[key in keyof T]?: T[key] extends Record<string, any> ? FilterConstraints<T[key]> : T[key] | DeepConstraint<T[key]>;
|
20
|
+
};
|
@@ -0,0 +1,24 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.buildSortFilter = exports.isSortFilterObject = void 0;
|
4
|
+
function isSortFilterObject(filter) {
|
5
|
+
return typeof filter === 'object' && filter.column !== undefined;
|
6
|
+
}
|
7
|
+
exports.isSortFilterObject = isSortFilterObject;
|
8
|
+
function buildSortFilter(filter) {
|
9
|
+
if (!filter)
|
10
|
+
return undefined;
|
11
|
+
const filters = Array.isArray(filter) ? filter : [filter];
|
12
|
+
return filters.reduce((acc, item) => {
|
13
|
+
if (typeof item === 'string') {
|
14
|
+
return Object.assign(Object.assign({}, acc), { [item]: 'asc' });
|
15
|
+
}
|
16
|
+
else if (isSortFilterObject(item)) {
|
17
|
+
return Object.assign(Object.assign({}, acc), { [item.column]: item.direction });
|
18
|
+
}
|
19
|
+
else {
|
20
|
+
return acc;
|
21
|
+
}
|
22
|
+
}, {});
|
23
|
+
}
|
24
|
+
exports.buildSortFilter = buildSortFilter;
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './operators';
|
@@ -0,0 +1,17 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = 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);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./operators"), exports);
|
@@ -0,0 +1,21 @@
|
|
1
|
+
import { Constraint } from './filters';
|
2
|
+
declare type ComparableType = number | Date;
|
3
|
+
export declare const gt: <T extends ComparableType>(value: T) => Constraint<T>;
|
4
|
+
export declare const ge: <T extends ComparableType>(value: T) => Constraint<T>;
|
5
|
+
export declare const gte: <T extends ComparableType>(value: T) => Constraint<T>;
|
6
|
+
export declare const lt: <T extends ComparableType>(value: T) => Constraint<T>;
|
7
|
+
export declare const lte: <T extends ComparableType>(value: T) => Constraint<T>;
|
8
|
+
export declare const le: <T extends ComparableType>(value: T) => Constraint<T>;
|
9
|
+
export declare const exists: (column: string) => Constraint<string>;
|
10
|
+
export declare const notExists: (column: string) => Constraint<string>;
|
11
|
+
export declare const startsWith: (value: string) => Constraint<string>;
|
12
|
+
export declare const endsWith: (value: string) => Constraint<string>;
|
13
|
+
export declare const pattern: (value: string) => Constraint<string>;
|
14
|
+
export declare const is: <T>(value: T) => Constraint<T>;
|
15
|
+
export declare const isNot: <T>(value: T) => Constraint<T>;
|
16
|
+
export declare const contains: <T>(value: T) => Constraint<T>;
|
17
|
+
export declare const includes: (value: string) => Constraint<string>;
|
18
|
+
export declare const includesSubstring: (value: string) => Constraint<string>;
|
19
|
+
export declare const includesPattern: (value: string) => Constraint<string>;
|
20
|
+
export declare const includesAll: (value: string) => Constraint<string>;
|
21
|
+
export {};
|