@xata.io/client 0.7.1 → 0.7.2
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/CHANGELOG.md +9 -0
- package/dist/api/client.js +22 -9
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/schema/repository.d.ts +25 -8
- package/dist/schema/repository.js +85 -77
- package/dist/util/config.d.ts +11 -0
- package/dist/util/config.js +121 -0
- package/dist/util/types.d.ts +1 -0
- package/package.json +2 -2
- package/dist/schema/config.d.ts +0 -4
- package/dist/schema/config.js +0 -83
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,14 @@
|
|
1
1
|
# @xata.io/client
|
2
2
|
|
3
|
+
## 0.7.2
|
4
|
+
|
5
|
+
### Patch Changes
|
6
|
+
|
7
|
+
- 4803b6f: Memoize ApiClient namespaces
|
8
|
+
- 1f268d7: Add search in XataClient
|
9
|
+
- d58c3d9: Hide private helper utilities
|
10
|
+
- f3b731b: Expose branch resolution API
|
11
|
+
|
3
12
|
## 0.7.1
|
4
13
|
|
5
14
|
### Patch Changes
|
package/dist/api/client.js
CHANGED
@@ -10,10 +10,10 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
10
10
|
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");
|
11
11
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
12
12
|
};
|
13
|
-
var _XataApiClient_extraProps;
|
13
|
+
var _XataApiClient_extraProps, _XataApiClient_namespaces;
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
15
15
|
exports.XataApiClient = void 0;
|
16
|
-
const config_1 = require("../
|
16
|
+
const config_1 = require("../util/config");
|
17
17
|
const fetch_1 = require("../util/fetch");
|
18
18
|
const components_1 = require("./components");
|
19
19
|
const providers_1 = require("./providers");
|
@@ -21,6 +21,7 @@ class XataApiClient {
|
|
21
21
|
constructor(options) {
|
22
22
|
var _a, _b;
|
23
23
|
_XataApiClient_extraProps.set(this, void 0);
|
24
|
+
_XataApiClient_namespaces.set(this, {});
|
24
25
|
const provider = (_a = options.host) !== null && _a !== void 0 ? _a : 'production';
|
25
26
|
const apiKey = (_b = options === null || options === void 0 ? void 0 : options.apiKey) !== null && _b !== void 0 ? _b : (0, config_1.getAPIKey)();
|
26
27
|
if (!apiKey) {
|
@@ -34,26 +35,38 @@ class XataApiClient {
|
|
34
35
|
}, "f");
|
35
36
|
}
|
36
37
|
get user() {
|
37
|
-
|
38
|
+
if (!__classPrivateFieldGet(this, _XataApiClient_namespaces, "f").user)
|
39
|
+
__classPrivateFieldGet(this, _XataApiClient_namespaces, "f").user = new UserApi(__classPrivateFieldGet(this, _XataApiClient_extraProps, "f"));
|
40
|
+
return __classPrivateFieldGet(this, _XataApiClient_namespaces, "f").user;
|
38
41
|
}
|
39
42
|
get workspaces() {
|
40
|
-
|
43
|
+
if (!__classPrivateFieldGet(this, _XataApiClient_namespaces, "f").workspaces)
|
44
|
+
__classPrivateFieldGet(this, _XataApiClient_namespaces, "f").workspaces = new WorkspaceApi(__classPrivateFieldGet(this, _XataApiClient_extraProps, "f"));
|
45
|
+
return __classPrivateFieldGet(this, _XataApiClient_namespaces, "f").workspaces;
|
41
46
|
}
|
42
47
|
get databases() {
|
43
|
-
|
48
|
+
if (!__classPrivateFieldGet(this, _XataApiClient_namespaces, "f").databases)
|
49
|
+
__classPrivateFieldGet(this, _XataApiClient_namespaces, "f").databases = new DatabaseApi(__classPrivateFieldGet(this, _XataApiClient_extraProps, "f"));
|
50
|
+
return __classPrivateFieldGet(this, _XataApiClient_namespaces, "f").databases;
|
44
51
|
}
|
45
52
|
get branches() {
|
46
|
-
|
53
|
+
if (!__classPrivateFieldGet(this, _XataApiClient_namespaces, "f").branches)
|
54
|
+
__classPrivateFieldGet(this, _XataApiClient_namespaces, "f").branches = new BranchApi(__classPrivateFieldGet(this, _XataApiClient_extraProps, "f"));
|
55
|
+
return __classPrivateFieldGet(this, _XataApiClient_namespaces, "f").branches;
|
47
56
|
}
|
48
57
|
get tables() {
|
49
|
-
|
58
|
+
if (!__classPrivateFieldGet(this, _XataApiClient_namespaces, "f").tables)
|
59
|
+
__classPrivateFieldGet(this, _XataApiClient_namespaces, "f").tables = new TableApi(__classPrivateFieldGet(this, _XataApiClient_extraProps, "f"));
|
60
|
+
return __classPrivateFieldGet(this, _XataApiClient_namespaces, "f").tables;
|
50
61
|
}
|
51
62
|
get records() {
|
52
|
-
|
63
|
+
if (!__classPrivateFieldGet(this, _XataApiClient_namespaces, "f").records)
|
64
|
+
__classPrivateFieldGet(this, _XataApiClient_namespaces, "f").records = new RecordsApi(__classPrivateFieldGet(this, _XataApiClient_extraProps, "f"));
|
65
|
+
return __classPrivateFieldGet(this, _XataApiClient_namespaces, "f").records;
|
53
66
|
}
|
54
67
|
}
|
55
68
|
exports.XataApiClient = XataApiClient;
|
56
|
-
_XataApiClient_extraProps = new WeakMap();
|
69
|
+
_XataApiClient_extraProps = new WeakMap(), _XataApiClient_namespaces = new WeakMap();
|
57
70
|
class UserApi {
|
58
71
|
constructor(extraProps) {
|
59
72
|
this.extraProps = extraProps;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
import { FetchImpl } from '../api/fetcher';
|
2
|
+
import { Dictionary, GetArrayInnerType, StringKeys } from '../util/types';
|
2
3
|
import { Page } from './pagination';
|
3
4
|
import { Query } from './query';
|
4
5
|
import { BaseData, EditableData, Identifiable, XataRecord } from './record';
|
5
6
|
import { SelectedPick } from './selection';
|
6
|
-
|
7
|
+
declare type TableLink = string[];
|
8
|
+
export declare type LinkDictionary = Dictionary<TableLink[]>;
|
7
9
|
/**
|
8
10
|
* Common interface for performing operations on a table.
|
9
11
|
*/
|
@@ -93,11 +95,20 @@ export declare abstract class Repository<Data extends BaseData, Record extends X
|
|
93
95
|
* @throws If the record could not be found or there was an error while performing the deletion.
|
94
96
|
*/
|
95
97
|
abstract delete(ids: Identifiable[]): Promise<void>;
|
98
|
+
/**
|
99
|
+
* Search for records in the table.
|
100
|
+
* @param query The query to search for.
|
101
|
+
* @param options The options to search with (like: fuzziness)
|
102
|
+
* @returns The found records.
|
103
|
+
*/
|
104
|
+
abstract search(query: string, options?: {
|
105
|
+
fuzziness?: number;
|
106
|
+
}): Promise<SelectedPick<Record, ['*']>[]>;
|
96
107
|
abstract query<Result extends XataRecord>(query: Query<Record, Result>): Promise<Page<Record, Result>>;
|
97
108
|
}
|
98
109
|
export declare class RestRepository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record, SelectedPick<Record, ['*']>> {
|
99
110
|
#private;
|
100
|
-
constructor(client: BaseClient<any>, table: string);
|
111
|
+
constructor(client: BaseClient<any>, table: string, links?: LinkDictionary);
|
101
112
|
create(object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
|
102
113
|
create(recordId: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
|
103
114
|
create(objects: EditableData<Data>[]): Promise<SelectedPick<Record, ['*']>[]>;
|
@@ -109,13 +120,16 @@ export declare class RestRepository<Data extends BaseData, Record extends XataRe
|
|
109
120
|
createOrUpdate(recordId: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
|
110
121
|
createOrUpdate(objects: EditableData<Data>[]): Promise<SelectedPick<Record, ['*']>[]>;
|
111
122
|
delete(recordId: string | Identifiable | Array<string | Identifiable>): Promise<void>;
|
123
|
+
search(query: string, options?: {
|
124
|
+
fuzziness?: number;
|
125
|
+
}): Promise<SelectedPick<Record, ['*']>[]>;
|
112
126
|
query<Result extends XataRecord>(query: Query<Record, Result>): Promise<Page<Record, Result>>;
|
113
127
|
}
|
114
128
|
interface RepositoryFactory {
|
115
|
-
createRepository<Data extends BaseData>(client: BaseClient<any>, table: string): Repository<Data & XataRecord>;
|
129
|
+
createRepository<Data extends BaseData>(client: BaseClient<any>, table: string, links?: LinkDictionary): Repository<Data & XataRecord>;
|
116
130
|
}
|
117
131
|
export declare class RestRespositoryFactory implements RepositoryFactory {
|
118
|
-
createRepository<Data extends BaseData>(client: BaseClient<any>, table: string): Repository<Data & XataRecord>;
|
132
|
+
createRepository<Data extends BaseData>(client: BaseClient<any>, table: string, links?: LinkDictionary): Repository<Data & XataRecord>;
|
119
133
|
}
|
120
134
|
declare type BranchStrategyValue = string | undefined | null;
|
121
135
|
declare type BranchStrategyBuilder = () => BranchStrategyValue | Promise<BranchStrategyValue>;
|
@@ -138,11 +152,14 @@ export declare type XataClientOptions = {
|
|
138
152
|
repositoryFactory?: RepositoryFactory;
|
139
153
|
};
|
140
154
|
export declare class BaseClient<D extends Record<string, Repository<any>> = Record<string, Repository<any>>> {
|
141
|
-
#private;
|
142
155
|
options: XataClientOptions;
|
143
156
|
db: D;
|
144
|
-
constructor(options?: XataClientOptions, links?:
|
145
|
-
|
146
|
-
|
157
|
+
constructor(options?: XataClientOptions, links?: LinkDictionary);
|
158
|
+
search<Tables extends StringKeys<D>>(query: string, options?: {
|
159
|
+
fuzziness?: number;
|
160
|
+
tables?: Tables[];
|
161
|
+
}): Promise<{
|
162
|
+
[Model in GetArrayInnerType<NonNullable<NonNullable<typeof options>['tables']>>]: Awaited<ReturnType<D[Model]['search']>>;
|
163
|
+
}>;
|
147
164
|
}
|
148
165
|
export {};
|
@@ -26,13 +26,13 @@ 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, _RestRepository_insertRecordWithoutId, _RestRepository_insertRecordWithId, _RestRepository_bulkInsertTableRecords, _RestRepository_updateRecordWithID, _RestRepository_upsertRecordWithID, _RestRepository_deleteRecord,
|
29
|
+
var _RestRepository_instances, _RestRepository_client, _RestRepository_fetch, _RestRepository_table, _RestRepository_links, _RestRepository_branch, _RestRepository_getFetchProps, _RestRepository_getBranch, _RestRepository_insertRecordWithoutId, _RestRepository_insertRecordWithId, _RestRepository_bulkInsertTableRecords, _RestRepository_updateRecordWithID, _RestRepository_upsertRecordWithID, _RestRepository_deleteRecord, _RestRepository_initObject;
|
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
33
|
const fetch_1 = require("../util/fetch");
|
34
34
|
const lang_1 = require("../util/lang");
|
35
|
-
const config_1 = require("
|
35
|
+
const config_1 = require("../util/config");
|
36
36
|
const pagination_1 = require("./pagination");
|
37
37
|
const query_1 = require("./query");
|
38
38
|
const record_1 = require("./record");
|
@@ -44,15 +44,18 @@ class Repository extends query_1.Query {
|
|
44
44
|
}
|
45
45
|
exports.Repository = Repository;
|
46
46
|
class RestRepository extends query_1.Query {
|
47
|
-
constructor(client, table) {
|
47
|
+
constructor(client, table, links) {
|
48
48
|
super(null, table, {});
|
49
49
|
_RestRepository_instances.add(this);
|
50
50
|
_RestRepository_client.set(this, void 0);
|
51
51
|
_RestRepository_fetch.set(this, void 0);
|
52
52
|
_RestRepository_table.set(this, void 0);
|
53
|
+
_RestRepository_links.set(this, void 0);
|
54
|
+
_RestRepository_branch.set(this, void 0);
|
53
55
|
__classPrivateFieldSet(this, _RestRepository_client, client, "f");
|
54
56
|
__classPrivateFieldSet(this, _RestRepository_table, table, "f");
|
55
57
|
__classPrivateFieldSet(this, _RestRepository_fetch, (0, fetch_1.getFetchImplementation)(__classPrivateFieldGet(this, _RestRepository_client, "f").options.fetch), "f");
|
58
|
+
__classPrivateFieldSet(this, _RestRepository_links, links !== null && links !== void 0 ? links : {}, "f");
|
56
59
|
}
|
57
60
|
create(a, b) {
|
58
61
|
return __awaiter(this, void 0, void 0, function* () {
|
@@ -85,7 +88,7 @@ class RestRepository extends query_1.Query {
|
|
85
88
|
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
86
89
|
try {
|
87
90
|
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,
|
91
|
+
return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_initObject).call(this, __classPrivateFieldGet(this, _RestRepository_table, "f"), response);
|
89
92
|
}
|
90
93
|
catch (e) {
|
91
94
|
if ((0, lang_1.isObject)(e) && e.status === 404) {
|
@@ -161,6 +164,13 @@ class RestRepository extends query_1.Query {
|
|
161
164
|
throw new Error('Invalid arguments for delete method');
|
162
165
|
});
|
163
166
|
}
|
167
|
+
search(query, options = {}) {
|
168
|
+
return __awaiter(this, void 0, void 0, function* () {
|
169
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
170
|
+
const { records } = yield (0, api_1.searchBranch)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}' }, body: { tables: [__classPrivateFieldGet(this, _RestRepository_table, "f")], query, fuzziness: options.fuzziness } }, fetchProps));
|
171
|
+
return records.map((item) => __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_initObject).call(this, __classPrivateFieldGet(this, _RestRepository_table, "f"), item));
|
172
|
+
});
|
173
|
+
}
|
164
174
|
query(query) {
|
165
175
|
var _a;
|
166
176
|
return __awaiter(this, void 0, void 0, function* () {
|
@@ -173,16 +183,16 @@ class RestRepository extends query_1.Query {
|
|
173
183
|
};
|
174
184
|
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
175
185
|
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,
|
186
|
+
const records = objects.map((record) => __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_initObject).call(this, __classPrivateFieldGet(this, _RestRepository_table, "f"), record));
|
177
187
|
return new pagination_1.Page(query, meta, records);
|
178
188
|
});
|
179
189
|
}
|
180
190
|
}
|
181
191
|
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() {
|
192
|
+
_RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _RestRepository_table = new WeakMap(), _RestRepository_links = new WeakMap(), _RestRepository_branch = new WeakMap(), _RestRepository_instances = new WeakSet(), _RestRepository_getFetchProps = function _RestRepository_getFetchProps() {
|
183
193
|
var _a;
|
184
194
|
return __awaiter(this, void 0, void 0, function* () {
|
185
|
-
const branch = yield __classPrivateFieldGet(this,
|
195
|
+
const branch = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getBranch).call(this);
|
186
196
|
const apiKey = (_a = __classPrivateFieldGet(this, _RestRepository_client, "f").options.apiKey) !== null && _a !== void 0 ? _a : (0, config_1.getAPIKey)();
|
187
197
|
if (!apiKey) {
|
188
198
|
throw new Error('Could not resolve a valid apiKey');
|
@@ -201,6 +211,35 @@ _RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _
|
|
201
211
|
}
|
202
212
|
};
|
203
213
|
});
|
214
|
+
}, _RestRepository_getBranch = function _RestRepository_getBranch() {
|
215
|
+
var e_1, _a;
|
216
|
+
return __awaiter(this, void 0, void 0, function* () {
|
217
|
+
if (__classPrivateFieldGet(this, _RestRepository_branch, "f"))
|
218
|
+
return __classPrivateFieldGet(this, _RestRepository_branch, "f");
|
219
|
+
const { branch: param } = __classPrivateFieldGet(this, _RestRepository_client, "f").options;
|
220
|
+
const strategies = Array.isArray(param) ? [...param] : [param];
|
221
|
+
const evaluateBranch = (strategy) => __awaiter(this, void 0, void 0, function* () {
|
222
|
+
return isBranchStrategyBuilder(strategy) ? yield strategy() : strategy;
|
223
|
+
});
|
224
|
+
try {
|
225
|
+
for (var strategies_1 = __asyncValues(strategies), strategies_1_1; strategies_1_1 = yield strategies_1.next(), !strategies_1_1.done;) {
|
226
|
+
const strategy = strategies_1_1.value;
|
227
|
+
const branch = yield evaluateBranch(strategy);
|
228
|
+
if (branch) {
|
229
|
+
__classPrivateFieldSet(this, _RestRepository_branch, branch, "f");
|
230
|
+
return branch;
|
231
|
+
}
|
232
|
+
}
|
233
|
+
}
|
234
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
235
|
+
finally {
|
236
|
+
try {
|
237
|
+
if (strategies_1_1 && !strategies_1_1.done && (_a = strategies_1.return)) yield _a.call(strategies_1);
|
238
|
+
}
|
239
|
+
finally { if (e_1) throw e_1.error; }
|
240
|
+
}
|
241
|
+
throw new Error('Unable to resolve branch value');
|
242
|
+
});
|
204
243
|
}, _RestRepository_insertRecordWithoutId = function _RestRepository_insertRecordWithoutId(object) {
|
205
244
|
return __awaiter(this, void 0, void 0, function* () {
|
206
245
|
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
@@ -267,22 +306,43 @@ _RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _
|
|
267
306
|
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
268
307
|
yield (0, api_1.deleteRecord)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId } }, fetchProps));
|
269
308
|
});
|
309
|
+
}, _RestRepository_initObject = function _RestRepository_initObject(table, object) {
|
310
|
+
const result = {};
|
311
|
+
Object.assign(result, object);
|
312
|
+
const tableLinks = __classPrivateFieldGet(this, _RestRepository_links, "f")[table] || [];
|
313
|
+
for (const link of tableLinks) {
|
314
|
+
const [field, linkTable] = link;
|
315
|
+
const value = result[field];
|
316
|
+
if (value && (0, lang_1.isObject)(value)) {
|
317
|
+
result[field] = __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_initObject).call(this, linkTable, value);
|
318
|
+
}
|
319
|
+
}
|
320
|
+
const db = __classPrivateFieldGet(this, _RestRepository_client, "f").db;
|
321
|
+
result.read = function () {
|
322
|
+
return db[table].read(result['id']);
|
323
|
+
};
|
324
|
+
result.update = function (data) {
|
325
|
+
return db[table].update(result['id'], data);
|
326
|
+
};
|
327
|
+
result.delete = function () {
|
328
|
+
return db[table].delete(result['id']);
|
329
|
+
};
|
330
|
+
for (const prop of ['read', 'update', 'delete']) {
|
331
|
+
Object.defineProperty(result, prop, { enumerable: false });
|
332
|
+
}
|
333
|
+
Object.freeze(result);
|
334
|
+
return result;
|
270
335
|
};
|
271
336
|
class RestRespositoryFactory {
|
272
|
-
createRepository(client, table) {
|
273
|
-
return new RestRepository(client, table);
|
337
|
+
createRepository(client, table, links) {
|
338
|
+
return new RestRepository(client, table, links);
|
274
339
|
}
|
275
340
|
}
|
276
341
|
exports.RestRespositoryFactory = RestRespositoryFactory;
|
277
342
|
function resolveXataClientOptions(options) {
|
278
|
-
const databaseURL = (options === null || options === void 0 ? void 0 : options.databaseURL) || (0, config_1.
|
343
|
+
const databaseURL = (options === null || options === void 0 ? void 0 : options.databaseURL) || (0, config_1.getDatabaseURL)() || '';
|
279
344
|
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
|
-
}));
|
345
|
+
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 }));
|
286
346
|
if (!databaseURL || !apiKey) {
|
287
347
|
throw new Error('Options databaseURL and apiKey are required');
|
288
348
|
}
|
@@ -291,82 +351,30 @@ function resolveXataClientOptions(options) {
|
|
291
351
|
branch });
|
292
352
|
}
|
293
353
|
class BaseClient {
|
294
|
-
constructor(options = {}, links
|
295
|
-
_BaseClient_links.set(this, void 0);
|
296
|
-
_BaseClient_branch.set(this, void 0);
|
354
|
+
constructor(options = {}, links) {
|
297
355
|
this.options = resolveXataClientOptions(options);
|
298
356
|
// Make this property not enumerable so it doesn't show up in console.dir, etc.
|
299
357
|
Object.defineProperty(this.options, 'apiKey', { enumerable: false });
|
300
|
-
__classPrivateFieldSet(this, _BaseClient_links, links, "f");
|
301
358
|
const factory = this.options.repositoryFactory || new RestRespositoryFactory();
|
302
359
|
this.db = new Proxy({}, {
|
303
360
|
get: (_target, prop) => {
|
304
|
-
if (
|
361
|
+
if (!(0, lang_1.isString)(prop))
|
305
362
|
throw new Error('Invalid table name');
|
306
|
-
return factory.createRepository(this, prop);
|
363
|
+
return factory.createRepository(this, prop, links);
|
307
364
|
}
|
308
365
|
});
|
309
366
|
}
|
310
|
-
|
311
|
-
|
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;
|
367
|
+
search(query, options) {
|
368
|
+
var _a;
|
339
369
|
return __awaiter(this, void 0, void 0, function* () {
|
340
|
-
|
341
|
-
|
342
|
-
const
|
343
|
-
|
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');
|
370
|
+
const tables = (_a = options === null || options === void 0 ? void 0 : options.tables) !== null && _a !== void 0 ? _a : Object.keys(this.db);
|
371
|
+
// TODO: Implement global search with a single call, REST repository abstraction needed
|
372
|
+
const results = yield Promise.all(tables.map((table) => this.db[table].search(query, options).then((results) => [table, results])));
|
373
|
+
return Object.fromEntries(results);
|
365
374
|
});
|
366
375
|
}
|
367
376
|
}
|
368
377
|
exports.BaseClient = BaseClient;
|
369
|
-
_BaseClient_links = new WeakMap(), _BaseClient_branch = new WeakMap();
|
370
378
|
const isBranchStrategyBuilder = (strategy) => {
|
371
379
|
return typeof strategy === 'function';
|
372
380
|
};
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { FetchImpl } from '../api/fetcher';
|
2
|
+
declare type BranchResolutionOptions = {
|
3
|
+
databaseURL?: string;
|
4
|
+
apiKey?: string;
|
5
|
+
fetchImpl?: FetchImpl;
|
6
|
+
};
|
7
|
+
export declare function getCurrentBranchName(options?: BranchResolutionOptions): Promise<string | undefined>;
|
8
|
+
export declare function getCurrentBranchDetails(options?: BranchResolutionOptions): Promise<import("../api/schemas").DBBranch | null>;
|
9
|
+
export declare function getDatabaseURL(): string | undefined;
|
10
|
+
export declare function getAPIKey(): string | undefined;
|
11
|
+
export {};
|
@@ -0,0 +1,121 @@
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
12
|
+
exports.getAPIKey = exports.getDatabaseURL = exports.getCurrentBranchDetails = exports.getCurrentBranchName = void 0;
|
13
|
+
const api_1 = require("../api");
|
14
|
+
const environment_1 = require("./environment");
|
15
|
+
const fetch_1 = require("./fetch");
|
16
|
+
const lang_1 = require("./lang");
|
17
|
+
const envBranchNames = [
|
18
|
+
'XATA_BRANCH',
|
19
|
+
'VERCEL_GIT_COMMIT_REF',
|
20
|
+
'CF_PAGES_BRANCH',
|
21
|
+
'BRANCH' // Netlify. Putting it the last one because it is more ambiguous
|
22
|
+
];
|
23
|
+
const defaultBranch = 'main';
|
24
|
+
function getCurrentBranchName(options) {
|
25
|
+
return __awaiter(this, void 0, void 0, function* () {
|
26
|
+
const env = yield getBranchByEnvVariable();
|
27
|
+
if (env)
|
28
|
+
return env;
|
29
|
+
const branch = yield (0, environment_1.getGitBranch)();
|
30
|
+
if (!branch)
|
31
|
+
return defaultBranch;
|
32
|
+
// TODO: in the future, call /resolve endpoint
|
33
|
+
// For now, call API to see if the branch exists. If not, use a default value.
|
34
|
+
const details = yield getDatabaseBranch(branch, options);
|
35
|
+
if (details)
|
36
|
+
return branch;
|
37
|
+
return defaultBranch;
|
38
|
+
});
|
39
|
+
}
|
40
|
+
exports.getCurrentBranchName = getCurrentBranchName;
|
41
|
+
function getCurrentBranchDetails(options) {
|
42
|
+
return __awaiter(this, void 0, void 0, function* () {
|
43
|
+
const env = yield getBranchByEnvVariable();
|
44
|
+
if (env)
|
45
|
+
return getDatabaseBranch(env, options);
|
46
|
+
const branch = yield (0, environment_1.getGitBranch)();
|
47
|
+
if (!branch)
|
48
|
+
return getDatabaseBranch(defaultBranch, options);
|
49
|
+
// TODO: in the future, call /resolve endpoint
|
50
|
+
// For now, call API to see if the branch exists. If not, use a default value.
|
51
|
+
const details = yield getDatabaseBranch(branch, options);
|
52
|
+
if (details)
|
53
|
+
return details;
|
54
|
+
return getDatabaseBranch(defaultBranch, options);
|
55
|
+
});
|
56
|
+
}
|
57
|
+
exports.getCurrentBranchDetails = getCurrentBranchDetails;
|
58
|
+
function getDatabaseBranch(branch, options) {
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
60
|
+
const databaseURL = (options === null || options === void 0 ? void 0 : options.databaseURL) || getDatabaseURL();
|
61
|
+
const apiKey = (options === null || options === void 0 ? void 0 : options.apiKey) || getAPIKey();
|
62
|
+
if (!databaseURL)
|
63
|
+
throw new Error('A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely');
|
64
|
+
if (!apiKey)
|
65
|
+
throw new Error('An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely');
|
66
|
+
const [protocol, , host, , database] = databaseURL.split('/');
|
67
|
+
const [workspace] = host.split('.');
|
68
|
+
const dbBranchName = `${database}:${branch}`;
|
69
|
+
try {
|
70
|
+
return yield (0, api_1.getBranchDetails)({
|
71
|
+
apiKey,
|
72
|
+
apiUrl: databaseURL,
|
73
|
+
fetchImpl: (0, fetch_1.getFetchImplementation)(options === null || options === void 0 ? void 0 : options.fetchImpl),
|
74
|
+
workspacesApiUrl: `${protocol}//${host}`,
|
75
|
+
pathParams: {
|
76
|
+
dbBranchName,
|
77
|
+
workspace
|
78
|
+
}
|
79
|
+
});
|
80
|
+
}
|
81
|
+
catch (err) {
|
82
|
+
if ((0, lang_1.isObject)(err) && err.status === 404)
|
83
|
+
return null;
|
84
|
+
throw err;
|
85
|
+
}
|
86
|
+
});
|
87
|
+
}
|
88
|
+
function getBranchByEnvVariable() {
|
89
|
+
for (const name of envBranchNames) {
|
90
|
+
const value = (0, environment_1.getEnvVariable)(name);
|
91
|
+
if (value) {
|
92
|
+
return value;
|
93
|
+
}
|
94
|
+
}
|
95
|
+
try {
|
96
|
+
return XATA_BRANCH;
|
97
|
+
}
|
98
|
+
catch (err) {
|
99
|
+
// Ignore ReferenceError. Only CloudFlare workers set env variables as global variables
|
100
|
+
}
|
101
|
+
}
|
102
|
+
function getDatabaseURL() {
|
103
|
+
var _a;
|
104
|
+
try {
|
105
|
+
return (_a = (0, environment_1.getEnvVariable)('XATA_DATABASE_URL')) !== null && _a !== void 0 ? _a : XATA_DATABASE_URL;
|
106
|
+
}
|
107
|
+
catch (err) {
|
108
|
+
return undefined;
|
109
|
+
}
|
110
|
+
}
|
111
|
+
exports.getDatabaseURL = getDatabaseURL;
|
112
|
+
function getAPIKey() {
|
113
|
+
var _a;
|
114
|
+
try {
|
115
|
+
return (_a = (0, environment_1.getEnvVariable)('XATA_API_KEY')) !== null && _a !== void 0 ? _a : XATA_API_KEY;
|
116
|
+
}
|
117
|
+
catch (err) {
|
118
|
+
return undefined;
|
119
|
+
}
|
120
|
+
}
|
121
|
+
exports.getAPIKey = getAPIKey;
|
package/dist/util/types.d.ts
CHANGED
@@ -21,4 +21,5 @@ declare type Impossible<K extends keyof any> = {
|
|
21
21
|
};
|
22
22
|
export declare type Exactly<T, U extends T = T> = U & Impossible<Exclude<keyof U, keyof T>>;
|
23
23
|
export declare type SingleOrArray<T> = T | T[];
|
24
|
+
export declare type Dictionary<T> = Record<string, T>;
|
24
25
|
export {};
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@xata.io/client",
|
3
|
-
"version": "0.7.
|
3
|
+
"version": "0.7.2",
|
4
4
|
"description": "Xata.io SDK for TypeScript and JavaScript",
|
5
5
|
"main": "./dist/index.js",
|
6
6
|
"types": "./dist/index.d.ts",
|
@@ -23,5 +23,5 @@
|
|
23
23
|
"url": "https://github.com/xataio/client-ts/issues"
|
24
24
|
},
|
25
25
|
"homepage": "https://github.com/xataio/client-ts/blob/main/client/README.md",
|
26
|
-
"gitHead": "
|
26
|
+
"gitHead": "036d273ce2e938ef4bae72c3727881eefaf30574"
|
27
27
|
}
|
package/dist/schema/config.d.ts
DELETED
@@ -1,4 +0,0 @@
|
|
1
|
-
import { FetcherExtraProps } from '../api/fetcher';
|
2
|
-
export declare function getBranch(fetchProps: Omit<FetcherExtraProps, 'workspacesApiUrl'>): Promise<string | undefined>;
|
3
|
-
export declare function getDatabaseUrl(): string | undefined;
|
4
|
-
export declare function getAPIKey(): string | undefined;
|
package/dist/schema/config.js
DELETED
@@ -1,83 +0,0 @@
|
|
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
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
12
|
-
exports.getAPIKey = exports.getDatabaseUrl = exports.getBranch = void 0;
|
13
|
-
const api_1 = require("../api");
|
14
|
-
const environment_1 = require("../util/environment");
|
15
|
-
const lang_1 = require("../util/lang");
|
16
|
-
const envBranchNames = [
|
17
|
-
'XATA_BRANCH',
|
18
|
-
'VERCEL_GIT_COMMIT_REF',
|
19
|
-
'CF_PAGES_BRANCH',
|
20
|
-
'BRANCH' // Netlify. Putting it the last one because it is more ambiguous
|
21
|
-
];
|
22
|
-
function getBranch(fetchProps) {
|
23
|
-
return __awaiter(this, void 0, void 0, function* () {
|
24
|
-
const env = getBranchByEnvVariable();
|
25
|
-
if (env)
|
26
|
-
return env;
|
27
|
-
const branch = yield (0, environment_1.getGitBranch)();
|
28
|
-
if (!branch)
|
29
|
-
return undefined;
|
30
|
-
// TODO: in the future, call /resolve endpoint
|
31
|
-
// For now, call API to see if the branch exists. If not, use a default value.
|
32
|
-
const [protocol, , host, , database] = fetchProps.apiUrl.split('/');
|
33
|
-
const [workspace] = host.split('.');
|
34
|
-
const dbBranchName = `${database}:${branch}`;
|
35
|
-
try {
|
36
|
-
yield (0, api_1.getBranchDetails)(Object.assign(Object.assign({}, fetchProps), { workspacesApiUrl: `${protocol}//${host}`, pathParams: {
|
37
|
-
dbBranchName,
|
38
|
-
workspace
|
39
|
-
} }));
|
40
|
-
}
|
41
|
-
catch (err) {
|
42
|
-
if ((0, lang_1.isObject)(err) && err.status === 404)
|
43
|
-
return 'main';
|
44
|
-
throw err;
|
45
|
-
}
|
46
|
-
return branch;
|
47
|
-
});
|
48
|
-
}
|
49
|
-
exports.getBranch = getBranch;
|
50
|
-
function getBranchByEnvVariable() {
|
51
|
-
for (const name of envBranchNames) {
|
52
|
-
const value = (0, environment_1.getEnvVariable)(name);
|
53
|
-
if (value) {
|
54
|
-
return value;
|
55
|
-
}
|
56
|
-
}
|
57
|
-
try {
|
58
|
-
return XATA_BRANCH;
|
59
|
-
}
|
60
|
-
catch (err) {
|
61
|
-
// Ignore ReferenceError. Only CloudFlare workers set env variables as global variables
|
62
|
-
}
|
63
|
-
}
|
64
|
-
function getDatabaseUrl() {
|
65
|
-
var _a;
|
66
|
-
try {
|
67
|
-
return (_a = (0, environment_1.getEnvVariable)('XATA_DATABASE_URL')) !== null && _a !== void 0 ? _a : XATA_DATABASE_URL;
|
68
|
-
}
|
69
|
-
catch (err) {
|
70
|
-
return undefined;
|
71
|
-
}
|
72
|
-
}
|
73
|
-
exports.getDatabaseUrl = getDatabaseUrl;
|
74
|
-
function getAPIKey() {
|
75
|
-
var _a;
|
76
|
-
try {
|
77
|
-
return (_a = (0, environment_1.getEnvVariable)('XATA_API_KEY')) !== null && _a !== void 0 ? _a : XATA_API_KEY;
|
78
|
-
}
|
79
|
-
catch (err) {
|
80
|
-
return undefined;
|
81
|
-
}
|
82
|
-
}
|
83
|
-
exports.getAPIKey = getAPIKey;
|