@xata.io/client 0.0.0-beta.d4fe58e → 0.0.0-beta.d76c443
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 +58 -0
- package/dist/api/client.d.ts +2 -2
- package/dist/api/client.js +34 -18
- package/dist/api/components.d.ts +34 -37
- package/dist/api/components.js +34 -37
- 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/index.d.ts +1 -0
- package/dist/index.js +1 -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 +26 -24
- package/dist/schema/operators.js +13 -11
- 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 +28 -3
- package/dist/schema/record.js +11 -0
- package/dist/schema/repository.d.ts +76 -37
- package/dist/schema/repository.js +214 -110
- package/dist/schema/selection.d.ts +24 -13
- 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/config.d.ts +11 -0
- package/dist/util/config.js +121 -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 +5 -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("../util/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,116 +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
|
-
|
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 }));
|
82
74
|
}
|
83
|
-
|
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');
|
84
80
|
});
|
85
81
|
}
|
82
|
+
// TODO: Add column support: https://github.com/xataio/openapi/issues/139
|
86
83
|
read(recordId) {
|
87
84
|
return __awaiter(this, void 0, void 0, function* () {
|
88
85
|
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
throw new Error('The server failed to save the record');
|
100
|
-
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
|
+
}
|
101
96
|
});
|
102
97
|
}
|
103
|
-
|
98
|
+
update(a, b) {
|
104
99
|
return __awaiter(this, void 0, void 0, function* () {
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
}, body: record }, fetchProps));
|
113
|
-
const finalObject = yield this.read(response.id);
|
114
|
-
if (!finalObject) {
|
115
|
-
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)));
|
116
107
|
}
|
117
|
-
|
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');
|
118
117
|
});
|
119
118
|
}
|
120
|
-
|
119
|
+
createOrUpdate(a, b) {
|
121
120
|
return __awaiter(this, void 0, void 0, function* () {
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
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');
|
128
138
|
});
|
129
139
|
}
|
130
140
|
delete(recordId) {
|
131
141
|
return __awaiter(this, void 0, void 0, function* () {
|
132
|
-
|
133
|
-
|
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');
|
134
162
|
});
|
135
163
|
}
|
136
|
-
query(query
|
137
|
-
var _a
|
164
|
+
query(query) {
|
165
|
+
var _a;
|
138
166
|
return __awaiter(this, void 0, void 0, function* () {
|
139
167
|
const data = query.getQueryOptions();
|
140
168
|
const body = {
|
141
|
-
filter: Object.values(data.filter).some(Boolean) ? data.filter : undefined,
|
142
|
-
sort:
|
143
|
-
page:
|
144
|
-
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
|
145
173
|
};
|
146
174
|
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
147
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));
|
148
176
|
const records = objects.map((record) => __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), record));
|
149
|
-
// TODO: We should properly type this any
|
150
177
|
return new pagination_1.Page(query, meta, records);
|
151
178
|
});
|
152
179
|
}
|
153
180
|
}
|
154
181
|
exports.RestRepository = RestRepository;
|
155
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;
|
156
184
|
return __awaiter(this, void 0, void 0, function* () {
|
157
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
|
+
}
|
158
190
|
return {
|
159
191
|
fetchImpl: __classPrivateFieldGet(this, _RestRepository_fetch, "f"),
|
160
|
-
apiKey
|
192
|
+
apiKey,
|
161
193
|
apiUrl: '',
|
162
194
|
// Instead of using workspace and dbBranch, we inject a probably CNAME'd URL
|
163
195
|
workspacesApiUrl: (path, params) => {
|
@@ -169,6 +201,72 @@ _RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _
|
|
169
201
|
}
|
170
202
|
};
|
171
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
|
+
});
|
172
270
|
};
|
173
271
|
class RestRespositoryFactory {
|
174
272
|
createRepository(client, table) {
|
@@ -176,54 +274,60 @@ class RestRespositoryFactory {
|
|
176
274
|
}
|
177
275
|
}
|
178
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) || (() => (0, config_1.getCurrentBranchName)({ apiKey, databaseURL, fetchImpl: options === null || options === void 0 ? void 0 : options.fetch }));
|
281
|
+
if (!databaseURL || !apiKey) {
|
282
|
+
throw new Error('Options databaseURL and apiKey are required');
|
283
|
+
}
|
284
|
+
return Object.assign(Object.assign({}, options), { databaseURL,
|
285
|
+
apiKey,
|
286
|
+
branch });
|
287
|
+
}
|
179
288
|
class BaseClient {
|
180
|
-
constructor(options, links = {}) {
|
289
|
+
constructor(options = {}, links = {}) {
|
181
290
|
_BaseClient_links.set(this, void 0);
|
182
291
|
_BaseClient_branch.set(this, void 0);
|
183
|
-
|
184
|
-
|
185
|
-
}
|
186
|
-
this.options = options;
|
292
|
+
this.options = resolveXataClientOptions(options);
|
293
|
+
// Make this property not enumerable so it doesn't show up in console.dir, etc.
|
294
|
+
Object.defineProperty(this.options, 'apiKey', { enumerable: false });
|
187
295
|
__classPrivateFieldSet(this, _BaseClient_links, links, "f");
|
296
|
+
const factory = this.options.repositoryFactory || new RestRespositoryFactory();
|
297
|
+
this.db = new Proxy({}, {
|
298
|
+
get: (_target, prop) => {
|
299
|
+
if (typeof prop !== 'string')
|
300
|
+
throw new Error('Invalid table name');
|
301
|
+
return factory.createRepository(this, prop);
|
302
|
+
}
|
303
|
+
});
|
188
304
|
}
|
189
305
|
initObject(table, object) {
|
190
|
-
const
|
191
|
-
Object.assign(
|
306
|
+
const result = {};
|
307
|
+
Object.assign(result, object);
|
192
308
|
const tableLinks = __classPrivateFieldGet(this, _BaseClient_links, "f")[table] || [];
|
193
309
|
for (const link of tableLinks) {
|
194
310
|
const [field, linkTable] = link;
|
195
|
-
const value =
|
196
|
-
if (value &&
|
197
|
-
|
198
|
-
if (Object.keys(value).find((col) => col === 'id')) {
|
199
|
-
o[field] = this.initObject(linkTable, value);
|
200
|
-
}
|
201
|
-
else if (id) {
|
202
|
-
o[field] = {
|
203
|
-
id,
|
204
|
-
get: () => {
|
205
|
-
this.db[linkTable].read(id);
|
206
|
-
}
|
207
|
-
};
|
208
|
-
}
|
311
|
+
const value = result[field];
|
312
|
+
if (value && (0, lang_1.isObject)(value)) {
|
313
|
+
result[field] = this.initObject(linkTable, value);
|
209
314
|
}
|
210
315
|
}
|
211
316
|
const db = this.db;
|
212
|
-
|
213
|
-
return db[table].read(
|
317
|
+
result.read = function () {
|
318
|
+
return db[table].read(result['id']);
|
214
319
|
};
|
215
|
-
|
216
|
-
return db[table].update(
|
320
|
+
result.update = function (data) {
|
321
|
+
return db[table].update(result['id'], data);
|
217
322
|
};
|
218
|
-
|
219
|
-
return db[table].delete(
|
323
|
+
result.delete = function () {
|
324
|
+
return db[table].delete(result['id']);
|
220
325
|
};
|
221
326
|
for (const prop of ['read', 'update', 'delete']) {
|
222
|
-
Object.defineProperty(
|
327
|
+
Object.defineProperty(result, prop, { enumerable: false });
|
223
328
|
}
|
224
|
-
|
225
|
-
|
226
|
-
return o;
|
329
|
+
Object.freeze(result);
|
330
|
+
return result;
|
227
331
|
}
|
228
332
|
getBranch() {
|
229
333
|
var e_1, _a;
|
@@ -261,12 +365,12 @@ _BaseClient_links = new WeakMap(), _BaseClient_branch = new WeakMap();
|
|
261
365
|
const isBranchStrategyBuilder = (strategy) => {
|
262
366
|
return typeof strategy === 'function';
|
263
367
|
};
|
264
|
-
// TODO: We can find a better implementation for links
|
265
368
|
const transformObjectLinks = (object) => {
|
266
369
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
370
|
+
// Ignore internal properties
|
371
|
+
if (key === 'xata')
|
372
|
+
return acc;
|
373
|
+
// Transform links to identifier
|
374
|
+
return Object.assign(Object.assign({}, acc), { [key]: (0, record_1.isIdentifiable)(value) ? value.id : value });
|
271
375
|
}, {});
|
272
376
|
};
|
@@ -1,14 +1,25 @@
|
|
1
|
-
import {
|
2
|
-
import {
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
}
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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;
|
14
25
|
export {};
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|