@xata.io/client 0.0.0-beta.d4d1df0 → 0.0.0-beta.e193c35
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 +36 -0
- package/dist/api/client.d.ts +2 -2
- package/dist/api/client.js +13 -10
- package/dist/api/components.d.ts +27 -31
- package/dist/api/components.js +27 -31
- package/dist/api/fetcher.d.ts +15 -0
- package/dist/api/fetcher.js +23 -22
- package/dist/api/providers.js +3 -2
- package/dist/api/responses.d.ts +6 -0
- package/dist/schema/config.d.ts +4 -0
- package/dist/schema/config.js +83 -0
- package/dist/schema/filters.d.ts +93 -17
- package/dist/schema/filters.js +0 -22
- package/dist/schema/filters.spec.d.ts +1 -0
- package/dist/schema/filters.spec.js +175 -0
- package/dist/schema/index.d.ts +5 -3
- package/dist/schema/index.js +8 -4
- package/dist/schema/operators.d.ts +17 -27
- package/dist/schema/operators.js +1 -14
- package/dist/schema/pagination.d.ts +18 -14
- package/dist/schema/pagination.js +5 -2
- package/dist/schema/query.d.ts +48 -44
- package/dist/schema/query.js +46 -31
- package/dist/schema/record.d.ts +35 -5
- package/dist/schema/record.js +11 -0
- package/dist/schema/repository.d.ts +76 -37
- package/dist/schema/repository.js +215 -111
- package/dist/schema/selection.d.ts +24 -12
- 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 +3 -0
- package/dist/util/lang.js +13 -1
- package/dist/util/types.d.ts +22 -1
- package/package.json +2 -2
@@ -26,20 +26,24 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
26
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
27
|
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
28
28
|
};
|
29
|
-
var _RestRepository_instances, _RestRepository_client, _RestRepository_fetch, _RestRepository_table, _RestRepository_getFetchProps, _BaseClient_links, _BaseClient_branch;
|
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
30
|
Object.defineProperty(exports, "__esModule", { value: true });
|
31
31
|
exports.BaseClient = exports.RestRespositoryFactory = exports.RestRepository = exports.Repository = void 0;
|
32
32
|
const api_1 = require("../api");
|
33
|
-
const
|
33
|
+
const fetch_1 = require("../util/fetch");
|
34
|
+
const lang_1 = require("../util/lang");
|
35
|
+
const config_1 = require("./config");
|
34
36
|
const pagination_1 = require("./pagination");
|
35
37
|
const query_1 = require("./query");
|
38
|
+
const record_1 = require("./record");
|
39
|
+
const sorting_1 = require("./sorting");
|
36
40
|
/**
|
37
41
|
* Common interface for performing operations on a table.
|
38
42
|
*/
|
39
43
|
class Repository extends query_1.Query {
|
40
44
|
}
|
41
45
|
exports.Repository = Repository;
|
42
|
-
class RestRepository extends
|
46
|
+
class RestRepository extends query_1.Query {
|
43
47
|
constructor(client, table) {
|
44
48
|
super(null, table, {});
|
45
49
|
_RestRepository_instances.add(this);
|
@@ -48,117 +52,144 @@ class RestRepository extends Repository {
|
|
48
52
|
_RestRepository_table.set(this, void 0);
|
49
53
|
__classPrivateFieldSet(this, _RestRepository_client, client, "f");
|
50
54
|
__classPrivateFieldSet(this, _RestRepository_table, table, "f");
|
51
|
-
|
52
|
-
const fetchImpl = typeof fetch !== 'undefined' ? fetch : __classPrivateFieldGet(this, _RestRepository_client, "f").options.fetch;
|
53
|
-
if (!fetchImpl) {
|
54
|
-
throw new Error(`The \`fetch\` option passed to the Xata client is resolving to a falsy value and may not be correctly imported.`);
|
55
|
-
}
|
56
|
-
__classPrivateFieldSet(this, _RestRepository_fetch, fetchImpl, "f");
|
55
|
+
__classPrivateFieldSet(this, _RestRepository_fetch, (0, fetch_1.getFetchImplementation)(__classPrivateFieldGet(this, _RestRepository_client, "f").options.fetch), "f");
|
57
56
|
}
|
58
|
-
create(
|
57
|
+
create(a, b) {
|
59
58
|
return __awaiter(this, void 0, void 0, function* () {
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
workspace: '{workspaceId}',
|
64
|
-
dbBranchName: '{dbBranch}',
|
65
|
-
tableName: __classPrivateFieldGet(this, _RestRepository_table, "f")
|
66
|
-
}, body: record }, fetchProps));
|
67
|
-
const finalObject = yield this.read(response.id);
|
68
|
-
if (!finalObject) {
|
69
|
-
throw new Error('The server failed to save the record');
|
59
|
+
// Create many records
|
60
|
+
if (Array.isArray(a)) {
|
61
|
+
return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_bulkInsertTableRecords).call(this, a);
|
70
62
|
}
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
throw new Error('The server failed to save the record');
|
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 }));
|
83
74
|
}
|
84
|
-
|
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');
|
85
80
|
});
|
86
81
|
}
|
82
|
+
// TODO: Add column support: https://github.com/xataio/openapi/issues/139
|
87
83
|
read(recordId) {
|
88
84
|
return __awaiter(this, void 0, void 0, function* () {
|
89
85
|
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
throw new Error('The server failed to save the record');
|
101
|
-
return item;
|
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
|
+
}
|
102
96
|
});
|
103
97
|
}
|
104
|
-
|
98
|
+
update(a, b) {
|
105
99
|
return __awaiter(this, void 0, void 0, function* () {
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
}, body: record }, fetchProps));
|
114
|
-
const finalObject = yield this.read(response.id);
|
115
|
-
if (!finalObject) {
|
116
|
-
throw new Error('The server failed to save the record');
|
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)));
|
117
107
|
}
|
118
|
-
|
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');
|
119
117
|
});
|
120
118
|
}
|
121
|
-
|
119
|
+
createOrUpdate(a, b) {
|
122
120
|
return __awaiter(this, void 0, void 0, function* () {
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
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');
|
129
138
|
});
|
130
139
|
}
|
131
140
|
delete(recordId) {
|
132
141
|
return __awaiter(this, void 0, void 0, function* () {
|
133
|
-
|
134
|
-
|
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');
|
135
162
|
});
|
136
163
|
}
|
137
|
-
query(query
|
138
|
-
var _a
|
164
|
+
query(query) {
|
165
|
+
var _a;
|
139
166
|
return __awaiter(this, void 0, void 0, function* () {
|
140
167
|
const data = query.getQueryOptions();
|
141
168
|
const body = {
|
142
|
-
filter: Object.values(data.filter).some(Boolean) ? data.filter : undefined,
|
143
|
-
sort:
|
144
|
-
page:
|
145
|
-
columns:
|
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
|
146
173
|
};
|
147
174
|
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
148
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));
|
149
176
|
const records = objects.map((record) => __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), record));
|
150
|
-
// TODO: We should properly type this any
|
151
177
|
return new pagination_1.Page(query, meta, records);
|
152
178
|
});
|
153
179
|
}
|
154
180
|
}
|
155
181
|
exports.RestRepository = RestRepository;
|
156
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;
|
157
184
|
return __awaiter(this, void 0, void 0, function* () {
|
158
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
|
+
}
|
159
190
|
return {
|
160
191
|
fetchImpl: __classPrivateFieldGet(this, _RestRepository_fetch, "f"),
|
161
|
-
apiKey
|
192
|
+
apiKey,
|
162
193
|
apiUrl: '',
|
163
194
|
// Instead of using workspace and dbBranch, we inject a probably CNAME'd URL
|
164
195
|
workspacesApiUrl: (path, params) => {
|
@@ -170,6 +201,72 @@ _RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _
|
|
170
201
|
}
|
171
202
|
};
|
172
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
|
+
});
|
173
270
|
};
|
174
271
|
class RestRespositoryFactory {
|
175
272
|
createRepository(client, table) {
|
@@ -177,54 +274,65 @@ class RestRespositoryFactory {
|
|
177
274
|
}
|
178
275
|
}
|
179
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
|
+
}
|
180
293
|
class BaseClient {
|
181
|
-
constructor(options, links = {}) {
|
294
|
+
constructor(options = {}, links = {}) {
|
182
295
|
_BaseClient_links.set(this, void 0);
|
183
296
|
_BaseClient_branch.set(this, void 0);
|
184
|
-
|
185
|
-
|
186
|
-
}
|
187
|
-
this.options = options;
|
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 });
|
188
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
|
+
});
|
189
309
|
}
|
190
310
|
initObject(table, object) {
|
191
|
-
const
|
192
|
-
Object.assign(
|
311
|
+
const result = {};
|
312
|
+
Object.assign(result, object);
|
193
313
|
const tableLinks = __classPrivateFieldGet(this, _BaseClient_links, "f")[table] || [];
|
194
314
|
for (const link of tableLinks) {
|
195
315
|
const [field, linkTable] = link;
|
196
|
-
const value =
|
197
|
-
if (value &&
|
198
|
-
|
199
|
-
if (Object.keys(value).find((col) => col === 'id')) {
|
200
|
-
o[field] = this.initObject(linkTable, value);
|
201
|
-
}
|
202
|
-
else if (id) {
|
203
|
-
o[field] = {
|
204
|
-
id,
|
205
|
-
get: () => {
|
206
|
-
this.db[linkTable].read(id);
|
207
|
-
}
|
208
|
-
};
|
209
|
-
}
|
316
|
+
const value = result[field];
|
317
|
+
if (value && (0, lang_1.isObject)(value)) {
|
318
|
+
result[field] = this.initObject(linkTable, value);
|
210
319
|
}
|
211
320
|
}
|
212
321
|
const db = this.db;
|
213
|
-
|
214
|
-
return db[table].read(
|
322
|
+
result.read = function () {
|
323
|
+
return db[table].read(result['id']);
|
215
324
|
};
|
216
|
-
|
217
|
-
return db[table].update(
|
325
|
+
result.update = function (data) {
|
326
|
+
return db[table].update(result['id'], data);
|
218
327
|
};
|
219
|
-
|
220
|
-
return db[table].delete(
|
328
|
+
result.delete = function () {
|
329
|
+
return db[table].delete(result['id']);
|
221
330
|
};
|
222
331
|
for (const prop of ['read', 'update', 'delete']) {
|
223
|
-
Object.defineProperty(
|
332
|
+
Object.defineProperty(result, prop, { enumerable: false });
|
224
333
|
}
|
225
|
-
|
226
|
-
|
227
|
-
return o;
|
334
|
+
Object.freeze(result);
|
335
|
+
return result;
|
228
336
|
}
|
229
337
|
getBranch() {
|
230
338
|
var e_1, _a;
|
@@ -262,12 +370,8 @@ _BaseClient_links = new WeakMap(), _BaseClient_branch = new WeakMap();
|
|
262
370
|
const isBranchStrategyBuilder = (strategy) => {
|
263
371
|
return typeof strategy === 'function';
|
264
372
|
};
|
265
|
-
// TODO: We can find a better implementation for links
|
266
373
|
const transformObjectLinks = (object) => {
|
267
374
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
268
|
-
|
269
|
-
return Object.assign(Object.assign({}, acc), { [key]: value.id });
|
270
|
-
}
|
271
|
-
return Object.assign(Object.assign({}, acc), { [key]: value });
|
375
|
+
return Object.assign(Object.assign({}, acc), { [key]: (0, record_1.isIdentifiable)(value) ? value.id : value });
|
272
376
|
}, {});
|
273
377
|
};
|
@@ -1,13 +1,25 @@
|
|
1
|
-
import {
|
2
|
-
import {
|
3
|
-
|
4
|
-
declare type
|
5
|
-
[
|
6
|
-
}
|
7
|
-
declare type
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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;
|
13
25
|
export {};
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|