@xata.io/client 0.0.0-beta.123dd7a → 0.0.0-beta.14375f7
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/.eslintrc.cjs +13 -0
- package/CHANGELOG.md +63 -0
- package/dist/api/client.d.ts +95 -0
- package/dist/api/client.js +238 -0
- package/dist/api/components.d.ts +1437 -0
- package/dist/api/components.js +998 -0
- package/dist/api/fetcher.d.ts +40 -0
- package/dist/api/fetcher.js +79 -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 +30 -0
- package/dist/api/responses.d.ts +50 -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 +2 -133
- package/dist/index.js +16 -368
- package/dist/schema/config.d.ts +4 -0
- package/dist/schema/config.js +83 -0
- package/dist/schema/filters.d.ts +96 -0
- package/dist/schema/filters.js +2 -0
- package/dist/{index.test.d.ts → schema/filters.spec.d.ts} +0 -0
- package/dist/schema/filters.spec.js +175 -0
- package/dist/schema/index.d.ts +7 -0
- package/dist/schema/index.js +29 -0
- package/dist/schema/operators.d.ts +74 -0
- package/dist/schema/operators.js +93 -0
- package/dist/schema/pagination.d.ts +83 -0
- package/dist/schema/pagination.js +93 -0
- package/dist/schema/query.d.ts +118 -0
- package/dist/schema/query.js +242 -0
- package/dist/schema/record.d.ts +66 -0
- package/dist/schema/record.js +13 -0
- package/dist/schema/repository.d.ts +148 -0
- package/dist/schema/repository.js +381 -0
- package/dist/schema/selection.d.ts +25 -0
- package/dist/schema/selection.js +2 -0
- package/dist/schema/selection.spec.d.ts +1 -0
- package/dist/schema/selection.spec.js +203 -0
- package/dist/schema/sorting.d.ts +17 -0
- package/dist/schema/sorting.js +28 -0
- package/dist/schema/sorting.spec.d.ts +1 -0
- package/dist/schema/sorting.spec.js +9 -0
- package/dist/util/environment.d.ts +5 -0
- package/dist/util/environment.js +68 -0
- package/dist/util/fetch.d.ts +2 -0
- package/dist/util/fetch.js +13 -0
- package/dist/util/lang.d.ts +5 -0
- package/dist/util/lang.js +22 -0
- package/dist/util/types.d.ts +24 -0
- package/dist/util/types.js +2 -0
- package/package.json +3 -3
- package/dist/index.test.js +0 -304
- package/src/index.test.ts +0 -392
- package/src/index.ts +0 -506
@@ -0,0 +1,381 @@
|
|
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 _RestRepository_instances, _RestRepository_client, _RestRepository_fetch, _RestRepository_table, _RestRepository_getFetchProps, _RestRepository_insertRecordWithoutId, _RestRepository_insertRecordWithId, _RestRepository_bulkInsertTableRecords, _RestRepository_updateRecordWithID, _RestRepository_upsertRecordWithID, _RestRepository_deleteRecord, _BaseClient_links, _BaseClient_branch;
|
30
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
31
|
+
exports.BaseClient = exports.RestRespositoryFactory = exports.RestRepository = exports.Repository = void 0;
|
32
|
+
const api_1 = require("../api");
|
33
|
+
const fetch_1 = require("../util/fetch");
|
34
|
+
const lang_1 = require("../util/lang");
|
35
|
+
const config_1 = require("./config");
|
36
|
+
const pagination_1 = require("./pagination");
|
37
|
+
const query_1 = require("./query");
|
38
|
+
const record_1 = require("./record");
|
39
|
+
const sorting_1 = require("./sorting");
|
40
|
+
/**
|
41
|
+
* Common interface for performing operations on a table.
|
42
|
+
*/
|
43
|
+
class Repository extends query_1.Query {
|
44
|
+
}
|
45
|
+
exports.Repository = Repository;
|
46
|
+
class RestRepository extends query_1.Query {
|
47
|
+
constructor(client, table) {
|
48
|
+
super(null, table, {});
|
49
|
+
_RestRepository_instances.add(this);
|
50
|
+
_RestRepository_client.set(this, void 0);
|
51
|
+
_RestRepository_fetch.set(this, void 0);
|
52
|
+
_RestRepository_table.set(this, void 0);
|
53
|
+
__classPrivateFieldSet(this, _RestRepository_client, client, "f");
|
54
|
+
__classPrivateFieldSet(this, _RestRepository_table, table, "f");
|
55
|
+
__classPrivateFieldSet(this, _RestRepository_fetch, (0, fetch_1.getFetchImplementation)(__classPrivateFieldGet(this, _RestRepository_client, "f").options.fetch), "f");
|
56
|
+
}
|
57
|
+
create(a, b) {
|
58
|
+
return __awaiter(this, void 0, void 0, function* () {
|
59
|
+
// Create many records
|
60
|
+
if (Array.isArray(a)) {
|
61
|
+
return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_bulkInsertTableRecords).call(this, a);
|
62
|
+
}
|
63
|
+
// Create one record with id as param
|
64
|
+
if ((0, lang_1.isString)(a) && (0, lang_1.isObject)(b)) {
|
65
|
+
if (a === '')
|
66
|
+
throw new Error("The id can't be empty");
|
67
|
+
return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_insertRecordWithId).call(this, a, b);
|
68
|
+
}
|
69
|
+
// Create one record with id as property
|
70
|
+
if ((0, lang_1.isObject)(a) && (0, lang_1.isString)(a.id)) {
|
71
|
+
if (a.id === '')
|
72
|
+
throw new Error("The id can't be empty");
|
73
|
+
return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_insertRecordWithId).call(this, a.id, Object.assign(Object.assign({}, a), { id: undefined }));
|
74
|
+
}
|
75
|
+
// Create one record without id
|
76
|
+
if ((0, lang_1.isObject)(a)) {
|
77
|
+
return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_insertRecordWithoutId).call(this, a);
|
78
|
+
}
|
79
|
+
throw new Error('Invalid arguments for create method');
|
80
|
+
});
|
81
|
+
}
|
82
|
+
// TODO: Add column support: https://github.com/xataio/openapi/issues/139
|
83
|
+
read(recordId) {
|
84
|
+
return __awaiter(this, void 0, void 0, function* () {
|
85
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
86
|
+
try {
|
87
|
+
const response = yield (0, api_1.getRecord)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId } }, fetchProps));
|
88
|
+
return __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), response);
|
89
|
+
}
|
90
|
+
catch (e) {
|
91
|
+
if ((0, lang_1.isObject)(e) && e.status === 404) {
|
92
|
+
return null;
|
93
|
+
}
|
94
|
+
throw e;
|
95
|
+
}
|
96
|
+
});
|
97
|
+
}
|
98
|
+
update(a, b) {
|
99
|
+
return __awaiter(this, void 0, void 0, function* () {
|
100
|
+
// Update many records
|
101
|
+
if (Array.isArray(a)) {
|
102
|
+
if (a.length > 100) {
|
103
|
+
// TODO: Implement bulk update when API has support for it
|
104
|
+
console.warn('Bulk update operation is not optimized in the Xata API yet, this request might be slow');
|
105
|
+
}
|
106
|
+
return Promise.all(a.map((object) => this.update(object)));
|
107
|
+
}
|
108
|
+
// Update one record with id as param
|
109
|
+
if ((0, lang_1.isString)(a) && (0, lang_1.isObject)(b)) {
|
110
|
+
return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_updateRecordWithID).call(this, a, b);
|
111
|
+
}
|
112
|
+
// Update one record with id as property
|
113
|
+
if ((0, lang_1.isObject)(a) && (0, lang_1.isString)(a.id)) {
|
114
|
+
return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_updateRecordWithID).call(this, a.id, Object.assign(Object.assign({}, a), { id: undefined }));
|
115
|
+
}
|
116
|
+
throw new Error('Invalid arguments for update method');
|
117
|
+
});
|
118
|
+
}
|
119
|
+
createOrUpdate(a, b) {
|
120
|
+
return __awaiter(this, void 0, void 0, function* () {
|
121
|
+
// Create or update many records
|
122
|
+
if (Array.isArray(a)) {
|
123
|
+
if (a.length > 100) {
|
124
|
+
// TODO: Implement bulk update when API has support for it
|
125
|
+
console.warn('Bulk update operation is not optimized in the Xata API yet, this request might be slow');
|
126
|
+
}
|
127
|
+
return Promise.all(a.map((object) => this.createOrUpdate(object)));
|
128
|
+
}
|
129
|
+
// Create or update one record with id as param
|
130
|
+
if ((0, lang_1.isString)(a) && (0, lang_1.isObject)(b)) {
|
131
|
+
return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_upsertRecordWithID).call(this, a, b);
|
132
|
+
}
|
133
|
+
// Create or update one record with id as property
|
134
|
+
if ((0, lang_1.isObject)(a) && (0, lang_1.isString)(a.id)) {
|
135
|
+
return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_upsertRecordWithID).call(this, a.id, Object.assign(Object.assign({}, a), { id: undefined }));
|
136
|
+
}
|
137
|
+
throw new Error('Invalid arguments for createOrUpdate method');
|
138
|
+
});
|
139
|
+
}
|
140
|
+
delete(recordId) {
|
141
|
+
return __awaiter(this, void 0, void 0, function* () {
|
142
|
+
// Delete many records
|
143
|
+
if (Array.isArray(recordId)) {
|
144
|
+
if (recordId.length > 100) {
|
145
|
+
// TODO: Implement bulk delete when API has support for it
|
146
|
+
console.warn('Bulk delete operation is not optimized in the Xata API yet, this request might be slow');
|
147
|
+
}
|
148
|
+
yield Promise.all(recordId.map((id) => this.delete(id)));
|
149
|
+
return;
|
150
|
+
}
|
151
|
+
// Delete one record with id as param
|
152
|
+
if ((0, lang_1.isString)(recordId)) {
|
153
|
+
yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_deleteRecord).call(this, recordId);
|
154
|
+
return;
|
155
|
+
}
|
156
|
+
// Delete one record with id as property
|
157
|
+
if ((0, lang_1.isObject)(recordId) && (0, lang_1.isString)(recordId.id)) {
|
158
|
+
yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_deleteRecord).call(this, recordId.id);
|
159
|
+
return;
|
160
|
+
}
|
161
|
+
throw new Error('Invalid arguments for delete method');
|
162
|
+
});
|
163
|
+
}
|
164
|
+
query(query) {
|
165
|
+
var _a;
|
166
|
+
return __awaiter(this, void 0, void 0, function* () {
|
167
|
+
const data = query.getQueryOptions();
|
168
|
+
const body = {
|
169
|
+
filter: Object.values((_a = data.filter) !== null && _a !== void 0 ? _a : {}).some(Boolean) ? data.filter : undefined,
|
170
|
+
sort: data.sort ? (0, sorting_1.buildSortFilter)(data.sort) : undefined,
|
171
|
+
page: data.page,
|
172
|
+
columns: data.columns
|
173
|
+
};
|
174
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
175
|
+
const { meta, records: objects } = yield (0, api_1.queryTable)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f") }, body }, fetchProps));
|
176
|
+
const records = objects.map((record) => __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), record));
|
177
|
+
return new pagination_1.Page(query, meta, records);
|
178
|
+
});
|
179
|
+
}
|
180
|
+
}
|
181
|
+
exports.RestRepository = RestRepository;
|
182
|
+
_RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _RestRepository_table = new WeakMap(), _RestRepository_instances = new WeakSet(), _RestRepository_getFetchProps = function _RestRepository_getFetchProps() {
|
183
|
+
var _a;
|
184
|
+
return __awaiter(this, void 0, void 0, function* () {
|
185
|
+
const branch = yield __classPrivateFieldGet(this, _RestRepository_client, "f").getBranch();
|
186
|
+
const apiKey = (_a = __classPrivateFieldGet(this, _RestRepository_client, "f").options.apiKey) !== null && _a !== void 0 ? _a : (0, config_1.getAPIKey)();
|
187
|
+
if (!apiKey) {
|
188
|
+
throw new Error('Could not resolve a valid apiKey');
|
189
|
+
}
|
190
|
+
return {
|
191
|
+
fetchImpl: __classPrivateFieldGet(this, _RestRepository_fetch, "f"),
|
192
|
+
apiKey,
|
193
|
+
apiUrl: '',
|
194
|
+
// Instead of using workspace and dbBranch, we inject a probably CNAME'd URL
|
195
|
+
workspacesApiUrl: (path, params) => {
|
196
|
+
var _a, _b;
|
197
|
+
const baseUrl = (_a = __classPrivateFieldGet(this, _RestRepository_client, "f").options.databaseURL) !== null && _a !== void 0 ? _a : '';
|
198
|
+
const hasBranch = (_b = params.dbBranchName) !== null && _b !== void 0 ? _b : params.branch;
|
199
|
+
const newPath = path.replace(/^\/db\/[^/]+/, hasBranch ? `:${branch}` : '');
|
200
|
+
return baseUrl + newPath;
|
201
|
+
}
|
202
|
+
};
|
203
|
+
});
|
204
|
+
}, _RestRepository_insertRecordWithoutId = function _RestRepository_insertRecordWithoutId(object) {
|
205
|
+
return __awaiter(this, void 0, void 0, function* () {
|
206
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
207
|
+
const record = transformObjectLinks(object);
|
208
|
+
const response = yield (0, api_1.insertRecord)(Object.assign({ pathParams: {
|
209
|
+
workspace: '{workspaceId}',
|
210
|
+
dbBranchName: '{dbBranch}',
|
211
|
+
tableName: __classPrivateFieldGet(this, _RestRepository_table, "f")
|
212
|
+
}, body: record }, fetchProps));
|
213
|
+
const finalObject = yield this.read(response.id);
|
214
|
+
if (!finalObject) {
|
215
|
+
throw new Error('The server failed to save the record');
|
216
|
+
}
|
217
|
+
return finalObject;
|
218
|
+
});
|
219
|
+
}, _RestRepository_insertRecordWithId = function _RestRepository_insertRecordWithId(recordId, object) {
|
220
|
+
return __awaiter(this, void 0, void 0, function* () {
|
221
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
222
|
+
const record = transformObjectLinks(object);
|
223
|
+
const response = yield (0, api_1.insertRecordWithID)(Object.assign({ pathParams: {
|
224
|
+
workspace: '{workspaceId}',
|
225
|
+
dbBranchName: '{dbBranch}',
|
226
|
+
tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"),
|
227
|
+
recordId
|
228
|
+
}, body: record, queryParams: { createOnly: true } }, fetchProps));
|
229
|
+
const finalObject = yield this.read(response.id);
|
230
|
+
if (!finalObject) {
|
231
|
+
throw new Error('The server failed to save the record');
|
232
|
+
}
|
233
|
+
return finalObject;
|
234
|
+
});
|
235
|
+
}, _RestRepository_bulkInsertTableRecords = function _RestRepository_bulkInsertTableRecords(objects) {
|
236
|
+
return __awaiter(this, void 0, void 0, function* () {
|
237
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
238
|
+
const records = objects.map((object) => transformObjectLinks(object));
|
239
|
+
const response = yield (0, api_1.bulkInsertTableRecords)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f") }, body: { records } }, fetchProps));
|
240
|
+
const finalObjects = yield this.any(...response.recordIDs.map((id) => this.filter('id', id))).getAll();
|
241
|
+
if (finalObjects.length !== objects.length) {
|
242
|
+
throw new Error('The server failed to save some records');
|
243
|
+
}
|
244
|
+
return finalObjects;
|
245
|
+
});
|
246
|
+
}, _RestRepository_updateRecordWithID = function _RestRepository_updateRecordWithID(recordId, object) {
|
247
|
+
return __awaiter(this, void 0, void 0, function* () {
|
248
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
249
|
+
const record = transformObjectLinks(object);
|
250
|
+
const response = yield (0, api_1.updateRecordWithID)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId }, body: record }, fetchProps));
|
251
|
+
const item = yield this.read(response.id);
|
252
|
+
if (!item)
|
253
|
+
throw new Error('The server failed to save the record');
|
254
|
+
return item;
|
255
|
+
});
|
256
|
+
}, _RestRepository_upsertRecordWithID = function _RestRepository_upsertRecordWithID(recordId, object) {
|
257
|
+
return __awaiter(this, void 0, void 0, function* () {
|
258
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
259
|
+
const response = yield (0, api_1.upsertRecordWithID)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId }, body: object }, fetchProps));
|
260
|
+
const item = yield this.read(response.id);
|
261
|
+
if (!item)
|
262
|
+
throw new Error('The server failed to save the record');
|
263
|
+
return item;
|
264
|
+
});
|
265
|
+
}, _RestRepository_deleteRecord = function _RestRepository_deleteRecord(recordId) {
|
266
|
+
return __awaiter(this, void 0, void 0, function* () {
|
267
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
268
|
+
yield (0, api_1.deleteRecord)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId } }, fetchProps));
|
269
|
+
});
|
270
|
+
};
|
271
|
+
class RestRespositoryFactory {
|
272
|
+
createRepository(client, table) {
|
273
|
+
return new RestRepository(client, table);
|
274
|
+
}
|
275
|
+
}
|
276
|
+
exports.RestRespositoryFactory = RestRespositoryFactory;
|
277
|
+
function resolveXataClientOptions(options) {
|
278
|
+
const databaseURL = (options === null || options === void 0 ? void 0 : options.databaseURL) || (0, config_1.getDatabaseUrl)() || '';
|
279
|
+
const apiKey = (options === null || options === void 0 ? void 0 : options.apiKey) || (0, config_1.getAPIKey)() || '';
|
280
|
+
const branch = (options === null || options === void 0 ? void 0 : options.branch) ||
|
281
|
+
(() => (0, config_1.getBranch)({
|
282
|
+
apiKey,
|
283
|
+
apiUrl: databaseURL,
|
284
|
+
fetchImpl: (0, fetch_1.getFetchImplementation)(options === null || options === void 0 ? void 0 : options.fetch)
|
285
|
+
}));
|
286
|
+
if (!databaseURL || !apiKey) {
|
287
|
+
throw new Error('Options databaseURL and apiKey are required');
|
288
|
+
}
|
289
|
+
return Object.assign(Object.assign({}, options), { databaseURL,
|
290
|
+
apiKey,
|
291
|
+
branch });
|
292
|
+
}
|
293
|
+
class BaseClient {
|
294
|
+
constructor(options = {}, links = {}) {
|
295
|
+
_BaseClient_links.set(this, void 0);
|
296
|
+
_BaseClient_branch.set(this, void 0);
|
297
|
+
this.options = resolveXataClientOptions(options);
|
298
|
+
// Make this property not enumerable so it doesn't show up in console.dir, etc.
|
299
|
+
Object.defineProperty(this.options, 'apiKey', { enumerable: false });
|
300
|
+
__classPrivateFieldSet(this, _BaseClient_links, links, "f");
|
301
|
+
const factory = this.options.repositoryFactory || new RestRespositoryFactory();
|
302
|
+
this.db = new Proxy({}, {
|
303
|
+
get: (_target, prop) => {
|
304
|
+
if (typeof prop !== 'string')
|
305
|
+
throw new Error('Invalid table name');
|
306
|
+
return factory.createRepository(this, prop);
|
307
|
+
}
|
308
|
+
});
|
309
|
+
}
|
310
|
+
initObject(table, object) {
|
311
|
+
const result = {};
|
312
|
+
Object.assign(result, object);
|
313
|
+
const tableLinks = __classPrivateFieldGet(this, _BaseClient_links, "f")[table] || [];
|
314
|
+
for (const link of tableLinks) {
|
315
|
+
const [field, linkTable] = link;
|
316
|
+
const value = result[field];
|
317
|
+
if (value && (0, lang_1.isObject)(value)) {
|
318
|
+
result[field] = this.initObject(linkTable, value);
|
319
|
+
}
|
320
|
+
}
|
321
|
+
const db = this.db;
|
322
|
+
result.read = function () {
|
323
|
+
return db[table].read(result['id']);
|
324
|
+
};
|
325
|
+
result.update = function (data) {
|
326
|
+
return db[table].update(result['id'], data);
|
327
|
+
};
|
328
|
+
result.delete = function () {
|
329
|
+
return db[table].delete(result['id']);
|
330
|
+
};
|
331
|
+
for (const prop of ['read', 'update', 'delete']) {
|
332
|
+
Object.defineProperty(result, prop, { enumerable: false });
|
333
|
+
}
|
334
|
+
Object.freeze(result);
|
335
|
+
return result;
|
336
|
+
}
|
337
|
+
getBranch() {
|
338
|
+
var e_1, _a;
|
339
|
+
return __awaiter(this, void 0, void 0, function* () {
|
340
|
+
if (__classPrivateFieldGet(this, _BaseClient_branch, "f"))
|
341
|
+
return __classPrivateFieldGet(this, _BaseClient_branch, "f");
|
342
|
+
const { branch: param } = this.options;
|
343
|
+
const strategies = Array.isArray(param) ? [...param] : [param];
|
344
|
+
const evaluateBranch = (strategy) => __awaiter(this, void 0, void 0, function* () {
|
345
|
+
return isBranchStrategyBuilder(strategy) ? yield strategy() : strategy;
|
346
|
+
});
|
347
|
+
try {
|
348
|
+
for (var strategies_1 = __asyncValues(strategies), strategies_1_1; strategies_1_1 = yield strategies_1.next(), !strategies_1_1.done;) {
|
349
|
+
const strategy = strategies_1_1.value;
|
350
|
+
const branch = yield evaluateBranch(strategy);
|
351
|
+
if (branch) {
|
352
|
+
__classPrivateFieldSet(this, _BaseClient_branch, branch, "f");
|
353
|
+
return branch;
|
354
|
+
}
|
355
|
+
}
|
356
|
+
}
|
357
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
358
|
+
finally {
|
359
|
+
try {
|
360
|
+
if (strategies_1_1 && !strategies_1_1.done && (_a = strategies_1.return)) yield _a.call(strategies_1);
|
361
|
+
}
|
362
|
+
finally { if (e_1) throw e_1.error; }
|
363
|
+
}
|
364
|
+
throw new Error('Unable to resolve branch value');
|
365
|
+
});
|
366
|
+
}
|
367
|
+
}
|
368
|
+
exports.BaseClient = BaseClient;
|
369
|
+
_BaseClient_links = new WeakMap(), _BaseClient_branch = new WeakMap();
|
370
|
+
const isBranchStrategyBuilder = (strategy) => {
|
371
|
+
return typeof strategy === 'function';
|
372
|
+
};
|
373
|
+
const transformObjectLinks = (object) => {
|
374
|
+
return Object.entries(object).reduce((acc, [key, value]) => {
|
375
|
+
// Ignore internal properties
|
376
|
+
if (key === 'xata')
|
377
|
+
return acc;
|
378
|
+
// Transform links to identifier
|
379
|
+
return Object.assign(Object.assign({}, acc), { [key]: (0, record_1.isIdentifiable)(value) ? value.id : value });
|
380
|
+
}, {});
|
381
|
+
};
|
@@ -0,0 +1,25 @@
|
|
1
|
+
import { If, IsArray, IsObject, StringKeys, UnionToIntersection, Values } from '../util/types';
|
2
|
+
import { Link, XataRecord } from './record';
|
3
|
+
export declare type SelectableColumn<O, RecursivePath extends any[] = []> = '*' | 'id' | DataProps<O> | NestedColumns<O, RecursivePath>;
|
4
|
+
export declare type SelectedPick<O extends XataRecord, Key extends SelectableColumn<O>[]> = XataRecord & UnionToIntersection<Values<{
|
5
|
+
[K in Key[number]]: NestedValueAtColumn<O, K> & XataRecord;
|
6
|
+
}>>;
|
7
|
+
export declare type ValueAtColumn<O, P extends SelectableColumn<O>> = P extends '*' ? Values<O> : P extends 'id' ? string : P extends keyof O ? O[P] : P extends `${infer K}.${infer V}` ? K extends keyof O ? Values<O[K] extends XataRecord ? (V extends SelectableColumn<O[K]> ? {
|
8
|
+
V: ValueAtColumn<O[K], V>;
|
9
|
+
} : never) : O[K]> : never : never;
|
10
|
+
declare type MAX_RECURSION = 5;
|
11
|
+
declare type NestedColumns<O, RecursivePath extends any[]> = RecursivePath['length'] extends MAX_RECURSION ? never : If<IsObject<O>, Values<{
|
12
|
+
[K in DataProps<O>]: If<IsArray<NonNullable<O[K]>>, K, // If the property is an array, we stop recursion. We don't support object arrays yet
|
13
|
+
If<IsObject<NonNullable<O[K]>>, NonNullable<O[K]> extends XataRecord ? SelectableColumn<NonNullable<O[K]>, [...RecursivePath, O[K]]> extends string ? K | `${K}.${SelectableColumn<NonNullable<O[K]>, [...RecursivePath, O[K]]>}` : never : `${K}.${StringKeys<NonNullable<O[K]>> | '*'}`, // This allows usage of objects that are not links
|
14
|
+
K>>;
|
15
|
+
}>, never>;
|
16
|
+
declare type DataProps<O> = Exclude<StringKeys<O>, StringKeys<XataRecord>>;
|
17
|
+
declare type NestedValueAtColumn<O, Key extends SelectableColumn<O>> = Key extends `${infer N}.${infer M}` ? N extends DataProps<O> ? {
|
18
|
+
[K in N]: M extends SelectableColumn<NonNullable<O[K]>> ? NonNullable<O[K]> extends XataRecord ? ForwardNullable<O[K], NestedValueAtColumn<NonNullable<O[K]>, M> & XataRecord> : ForwardNullable<O[K], NestedValueAtColumn<NonNullable<O[K]>, M>> : unknown;
|
19
|
+
} : unknown : Key extends DataProps<O> ? {
|
20
|
+
[K in Key]: NonNullable<O[K]> extends XataRecord ? ForwardNullable<O[K], SelectedPick<NonNullable<O[K]>, ['*']>> : O[K];
|
21
|
+
} : Key extends '*' ? {
|
22
|
+
[K in StringKeys<O>]: NonNullable<O[K]> extends XataRecord ? ForwardNullable<O[K], Link<NonNullable<O[K]>>> : O[K];
|
23
|
+
} : unknown;
|
24
|
+
declare type ForwardNullable<T, R> = T extends NonNullable<T> ? R : R | null;
|
25
|
+
export {};
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1,203 @@
|
|
1
|
+
"use strict";
|
2
|
+
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-floating-promises */
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
4
|
+
// SelectableColumn<O> //
|
5
|
+
// --------------------------------------------------------------------------- //
|
6
|
+
const validTeamColumns = [
|
7
|
+
'*',
|
8
|
+
'id',
|
9
|
+
'name',
|
10
|
+
'owner.*',
|
11
|
+
'owner.address.*',
|
12
|
+
'owner.address',
|
13
|
+
'owner.address.street'
|
14
|
+
];
|
15
|
+
// @ts-expect-error
|
16
|
+
const invalidFullNameTeamColumn = 'full_name';
|
17
|
+
// @ts-expect-error
|
18
|
+
const invalidPartialTeamColumn = 'owner.address.';
|
19
|
+
// @ts-expect-error
|
20
|
+
const invalidDeleteTeamColumn = 'owner.delete';
|
21
|
+
// @ts-expect-error
|
22
|
+
const invalidReadTeamColumn = 'owner.read.*';
|
23
|
+
// ValueAtColumn<O, P> //
|
24
|
+
// --------------------------------------------------------------------------- //
|
25
|
+
const labelsValue = ['foo'];
|
26
|
+
// @ts-expect-error
|
27
|
+
const invalidLabelsValue = [1];
|
28
|
+
// SelectedRecordPick<O, Key> //
|
29
|
+
// ---------------------------------------------------------------------------- //
|
30
|
+
function test1(user) {
|
31
|
+
var _a, _b, _c, _d;
|
32
|
+
user.id;
|
33
|
+
user.read();
|
34
|
+
user.full_name;
|
35
|
+
(_a = user.address) === null || _a === void 0 ? void 0 : _a.street;
|
36
|
+
// @ts-expect-error
|
37
|
+
user.team.id;
|
38
|
+
(_b = user.team) === null || _b === void 0 ? void 0 : _b.id;
|
39
|
+
(_c = user.team) === null || _c === void 0 ? void 0 : _c.read();
|
40
|
+
// @ts-expect-error
|
41
|
+
(_d = user.team) === null || _d === void 0 ? void 0 : _d.name;
|
42
|
+
user.partner.id;
|
43
|
+
user.partner.read();
|
44
|
+
// @ts-expect-error
|
45
|
+
user.partner.full_name;
|
46
|
+
user.team = null;
|
47
|
+
// @ts-expect-error
|
48
|
+
user.partner = null;
|
49
|
+
}
|
50
|
+
function test2(user) {
|
51
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
52
|
+
user.id;
|
53
|
+
user.read();
|
54
|
+
user.full_name;
|
55
|
+
(_a = user.team) === null || _a === void 0 ? void 0 : _a.id;
|
56
|
+
(_b = user.team) === null || _b === void 0 ? void 0 : _b.read();
|
57
|
+
(_c = user.team) === null || _c === void 0 ? void 0 : _c.name;
|
58
|
+
(_d = user.team) === null || _d === void 0 ? void 0 : _d.owner;
|
59
|
+
(_f = (_e = user.team) === null || _e === void 0 ? void 0 : _e.owner) === null || _f === void 0 ? void 0 : _f.id;
|
60
|
+
(_h = (_g = user.team) === null || _g === void 0 ? void 0 : _g.owner) === null || _h === void 0 ? void 0 : _h.read();
|
61
|
+
// @ts-expect-error
|
62
|
+
(_k = (_j = user.team) === null || _j === void 0 ? void 0 : _j.owner) === null || _k === void 0 ? void 0 : _k.full_name;
|
63
|
+
// @ts-expect-error
|
64
|
+
user.full_name = null;
|
65
|
+
user.email = null;
|
66
|
+
user.email = '';
|
67
|
+
// @ts-expect-error
|
68
|
+
user.email = 2;
|
69
|
+
if (user.team) {
|
70
|
+
// @ts-expect-error
|
71
|
+
user.team.name = null;
|
72
|
+
user.team.labels = null;
|
73
|
+
user.team.labels = ['foo'];
|
74
|
+
// @ts-expect-error
|
75
|
+
user.team.labels = [1];
|
76
|
+
}
|
77
|
+
}
|
78
|
+
function test3(user) {
|
79
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
80
|
+
user.id;
|
81
|
+
user.read();
|
82
|
+
// @ts-expect-error
|
83
|
+
user.full_name;
|
84
|
+
(_a = user.team) === null || _a === void 0 ? void 0 : _a.id;
|
85
|
+
(_b = user.team) === null || _b === void 0 ? void 0 : _b.read();
|
86
|
+
// @ts-expect-error
|
87
|
+
(_c = user.team) === null || _c === void 0 ? void 0 : _c.name;
|
88
|
+
(_e = (_d = user.team) === null || _d === void 0 ? void 0 : _d.owner) === null || _e === void 0 ? void 0 : _e.id;
|
89
|
+
(_g = (_f = user.team) === null || _f === void 0 ? void 0 : _f.owner) === null || _g === void 0 ? void 0 : _g.read();
|
90
|
+
(_j = (_h = user.team) === null || _h === void 0 ? void 0 : _h.owner) === null || _j === void 0 ? void 0 : _j.full_name;
|
91
|
+
}
|
92
|
+
function test4(user) {
|
93
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
|
94
|
+
user.id;
|
95
|
+
user.read();
|
96
|
+
// @ts-expect-error
|
97
|
+
user.full_name;
|
98
|
+
(_a = user.team) === null || _a === void 0 ? void 0 : _a.id;
|
99
|
+
(_b = user.team) === null || _b === void 0 ? void 0 : _b.read();
|
100
|
+
// @ts-expect-error
|
101
|
+
(_c = user.team) === null || _c === void 0 ? void 0 : _c.name;
|
102
|
+
(_e = (_d = user.team) === null || _d === void 0 ? void 0 : _d.owner) === null || _e === void 0 ? void 0 : _e.id;
|
103
|
+
(_g = (_f = user.team) === null || _f === void 0 ? void 0 : _f.owner) === null || _g === void 0 ? void 0 : _g.read();
|
104
|
+
// @ts-expect-error
|
105
|
+
(_j = (_h = user.team) === null || _h === void 0 ? void 0 : _h.owner) === null || _j === void 0 ? void 0 : _j.full_name;
|
106
|
+
(_l = (_k = user.team) === null || _k === void 0 ? void 0 : _k.owner) === null || _l === void 0 ? void 0 : _l.address;
|
107
|
+
(_p = (_o = (_m = user.team) === null || _m === void 0 ? void 0 : _m.owner) === null || _o === void 0 ? void 0 : _o.address) === null || _p === void 0 ? void 0 : _p.street;
|
108
|
+
(_s = (_r = (_q = user.team) === null || _q === void 0 ? void 0 : _q.owner) === null || _r === void 0 ? void 0 : _r.address) === null || _s === void 0 ? void 0 : _s.zipcode;
|
109
|
+
}
|
110
|
+
function test5(user) {
|
111
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
112
|
+
user.id;
|
113
|
+
user.read();
|
114
|
+
// @ts-expect-error
|
115
|
+
user.full_name;
|
116
|
+
user.settings;
|
117
|
+
(_a = user.settings) === null || _a === void 0 ? void 0 : _a.theme;
|
118
|
+
(_b = user.settings) === null || _b === void 0 ? void 0 : _b.language;
|
119
|
+
(_c = user.settings) === null || _c === void 0 ? void 0 : _c.signin;
|
120
|
+
(_e = (_d = user.settings) === null || _d === void 0 ? void 0 : _d.signin) === null || _e === void 0 ? void 0 : _e.email;
|
121
|
+
(_g = (_f = user.settings) === null || _f === void 0 ? void 0 : _f.signin) === null || _g === void 0 ? void 0 : _g.github;
|
122
|
+
// @ts-expect-error
|
123
|
+
(_h = user.settings) === null || _h === void 0 ? void 0 : _h.id;
|
124
|
+
// @ts-expect-error
|
125
|
+
(_j = user.settings) === null || _j === void 0 ? void 0 : _j.read();
|
126
|
+
// @ts-expect-error
|
127
|
+
(_l = (_k = user.settings) === null || _k === void 0 ? void 0 : _k.signin) === null || _l === void 0 ? void 0 : _l.id;
|
128
|
+
user.settings = null;
|
129
|
+
user.nonNullable.nested.value = 2;
|
130
|
+
// @ts-expect-error
|
131
|
+
user.nonNullable = null;
|
132
|
+
// @ts-expect-error
|
133
|
+
user.nonNullable.nested.value = null;
|
134
|
+
}
|
135
|
+
function test6(user) {
|
136
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
137
|
+
user.id;
|
138
|
+
user.read();
|
139
|
+
// @ts-expect-error
|
140
|
+
user.full_name;
|
141
|
+
user.settings;
|
142
|
+
(_a = user.settings) === null || _a === void 0 ? void 0 : _a.theme;
|
143
|
+
(_b = user.settings) === null || _b === void 0 ? void 0 : _b.language;
|
144
|
+
(_c = user.settings) === null || _c === void 0 ? void 0 : _c.signin;
|
145
|
+
(_e = (_d = user.settings) === null || _d === void 0 ? void 0 : _d.signin) === null || _e === void 0 ? void 0 : _e.email;
|
146
|
+
(_g = (_f = user.settings) === null || _f === void 0 ? void 0 : _f.signin) === null || _g === void 0 ? void 0 : _g.github;
|
147
|
+
// @ts-expect-error
|
148
|
+
(_h = user.settings) === null || _h === void 0 ? void 0 : _h.id;
|
149
|
+
// @ts-expect-error
|
150
|
+
(_j = user.settings) === null || _j === void 0 ? void 0 : _j.read();
|
151
|
+
// @ts-expect-error
|
152
|
+
(_l = (_k = user.settings) === null || _k === void 0 ? void 0 : _k.signin) === null || _l === void 0 ? void 0 : _l.id;
|
153
|
+
user.nonNullable.nested.value = 2;
|
154
|
+
user.nonNullable.nested.deep.depths.of.numbers = [1, 2, 3];
|
155
|
+
// @ts-expect-error
|
156
|
+
user.nonNullable.nested.deep.depths.of.numbers = 'invalid-string';
|
157
|
+
user.settings = null;
|
158
|
+
// @ts-expect-error
|
159
|
+
user.nonNullable = null;
|
160
|
+
// @ts-expect-error
|
161
|
+
user.nonNullable.nested.value = null;
|
162
|
+
}
|
163
|
+
function test7(user) {
|
164
|
+
var _a, _b;
|
165
|
+
user.partner;
|
166
|
+
user.partner.id;
|
167
|
+
user.partner.read();
|
168
|
+
user.partner.full_name;
|
169
|
+
// @ts-expect-error
|
170
|
+
user.partner.full_name = null;
|
171
|
+
user.partner.address;
|
172
|
+
// @ts-expect-error
|
173
|
+
user.team.id;
|
174
|
+
(_a = user.team) === null || _a === void 0 ? void 0 : _a.id;
|
175
|
+
// @ts-expect-error
|
176
|
+
user.team.read();
|
177
|
+
(_b = user.team) === null || _b === void 0 ? void 0 : _b.read();
|
178
|
+
// @ts-expect-error
|
179
|
+
user.partner = null;
|
180
|
+
user.team = null;
|
181
|
+
}
|
182
|
+
function test8(user) {
|
183
|
+
var _a, _b;
|
184
|
+
user.partner;
|
185
|
+
user.partner.id;
|
186
|
+
user.partner.read();
|
187
|
+
user.partner.full_name;
|
188
|
+
// @ts-expect-error
|
189
|
+
user.partner.full_name = null;
|
190
|
+
user.partner.address;
|
191
|
+
// @ts-expect-error
|
192
|
+
user.team.id;
|
193
|
+
(_a = user.team) === null || _a === void 0 ? void 0 : _a.id;
|
194
|
+
// @ts-expect-error
|
195
|
+
user.team.read();
|
196
|
+
(_b = user.team) === null || _b === void 0 ? void 0 : _b.read();
|
197
|
+
// @ts-expect-error
|
198
|
+
user.partner = null;
|
199
|
+
user.team = null;
|
200
|
+
}
|
201
|
+
test('fake test', () => {
|
202
|
+
// This is a fake test to make sure that the type definitions in this file are working
|
203
|
+
});
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import { SingleOrArray, Values } from '../util/types';
|
2
|
+
import { XataRecord } from './record';
|
3
|
+
import { SelectableColumn } from './selection';
|
4
|
+
export declare type SortDirection = 'asc' | 'desc';
|
5
|
+
export declare type SortFilterExtended<T extends XataRecord> = {
|
6
|
+
column: SelectableColumn<T>;
|
7
|
+
direction?: SortDirection;
|
8
|
+
};
|
9
|
+
export declare type SortFilter<T extends XataRecord> = SelectableColumn<T> | SortFilterExtended<T>;
|
10
|
+
export declare type ApiSortFilter<T extends XataRecord> = SingleOrArray<Values<{
|
11
|
+
[key in SelectableColumn<T>]: {
|
12
|
+
[K in key]: SortDirection;
|
13
|
+
};
|
14
|
+
}>>;
|
15
|
+
export declare function isSortFilterString<T extends XataRecord>(value: any): value is SelectableColumn<T>;
|
16
|
+
export declare function isSortFilterObject<T extends XataRecord>(filter: SortFilter<T>): filter is SortFilterExtended<T>;
|
17
|
+
export declare function buildSortFilter<T extends XataRecord>(filter: SingleOrArray<SortFilter<T>>): ApiSortFilter<T>;
|
@@ -0,0 +1,28 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.buildSortFilter = exports.isSortFilterObject = exports.isSortFilterString = void 0;
|
4
|
+
const lang_1 = require("../util/lang");
|
5
|
+
function isSortFilterString(value) {
|
6
|
+
return (0, lang_1.isString)(value);
|
7
|
+
}
|
8
|
+
exports.isSortFilterString = isSortFilterString;
|
9
|
+
function isSortFilterObject(filter) {
|
10
|
+
return (0, lang_1.isObject)(filter) && filter.column !== undefined;
|
11
|
+
}
|
12
|
+
exports.isSortFilterObject = isSortFilterObject;
|
13
|
+
function buildSortFilter(filter) {
|
14
|
+
var _a;
|
15
|
+
if (isSortFilterString(filter)) {
|
16
|
+
return { [filter]: 'asc' };
|
17
|
+
}
|
18
|
+
else if (Array.isArray(filter)) {
|
19
|
+
return filter.map((item) => buildSortFilter(item));
|
20
|
+
}
|
21
|
+
else if (isSortFilterObject(filter)) {
|
22
|
+
return { [filter.column]: (_a = filter.direction) !== null && _a !== void 0 ? _a : 'asc' };
|
23
|
+
}
|
24
|
+
else {
|
25
|
+
throw new Error(`Invalid sort filter: ${filter}`);
|
26
|
+
}
|
27
|
+
}
|
28
|
+
exports.buildSortFilter = buildSortFilter;
|