@xata.io/client 0.7.0 → 0.8.0

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.
@@ -1,59 +1,62 @@
1
- import { FetchImpl } from '../api/fetcher';
1
+ import { SchemaNamespaceResult } from '.';
2
+ import { FetcherExtraProps } from '../api/fetcher';
3
+ import { Dictionary } from '../util/types';
2
4
  import { Page } from './pagination';
3
5
  import { Query } from './query';
4
6
  import { BaseData, EditableData, Identifiable, XataRecord } from './record';
5
7
  import { SelectedPick } from './selection';
6
- export declare type Links = Record<string, Array<string[]>>;
8
+ declare type TableLink = string[];
9
+ export declare type LinkDictionary = Dictionary<TableLink[]>;
7
10
  /**
8
11
  * Common interface for performing operations on a table.
9
12
  */
10
- export declare abstract class Repository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record, SelectedPick<Record, ['*']>> {
11
- abstract create(object: EditableData<Data> & Partial<Identifiable>): Promise<SelectedPick<Record, ['*']>>;
13
+ export declare abstract class Repository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record, Readonly<SelectedPick<Record, ['*']>>> {
14
+ abstract create(object: EditableData<Data> & Partial<Identifiable>): Promise<Readonly<SelectedPick<Record, ['*']>>>;
12
15
  /**
13
16
  * Creates a single record in the table with a unique id.
14
17
  * @param id The unique id.
15
18
  * @param object Object containing the column names with their values to be stored in the table.
16
19
  * @returns The full persisted record.
17
20
  */
18
- abstract create(id: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
21
+ abstract create(id: string, object: EditableData<Data>): Promise<Readonly<SelectedPick<Record, ['*']>>>;
19
22
  /**
20
23
  * Creates multiple records in the table.
21
24
  * @param objects Array of objects with the column names and the values to be stored in the table.
22
25
  * @returns Array of the persisted records.
23
26
  */
24
- abstract create(objects: Array<EditableData<Data> & Partial<Identifiable>>): Promise<SelectedPick<Record, ['*']>[]>;
27
+ abstract create(objects: Array<EditableData<Data> & Partial<Identifiable>>): Promise<Readonly<SelectedPick<Record, ['*']>>[]>;
25
28
  /**
26
29
  * Queries a single record from the table given its unique id.
27
30
  * @param id The unique id.
28
31
  * @returns The persisted record for the given id or null if the record could not be found.
29
32
  */
30
- abstract read(id: string): Promise<SelectedPick<Record, ['*']> | null>;
33
+ abstract read(id: string): Promise<Readonly<SelectedPick<Record, ['*']> | null>>;
31
34
  /**
32
35
  * Partially update a single record.
33
36
  * @param object An object with its id and the columns to be updated.
34
37
  * @returns The full persisted record.
35
38
  */
36
- abstract update(object: Partial<EditableData<Data>> & Identifiable): Promise<SelectedPick<Record, ['*']>>;
39
+ abstract update(object: Partial<EditableData<Data>> & Identifiable): Promise<Readonly<SelectedPick<Record, ['*']>>>;
37
40
  /**
38
41
  * Partially update a single record given its unique id.
39
42
  * @param id The unique id.
40
43
  * @param object The column names and their values that have to be updated.
41
44
  * @returns The full persisted record.
42
45
  */
43
- abstract update(id: string, object: Partial<EditableData<Data>>): Promise<SelectedPick<Record, ['*']>>;
46
+ abstract update(id: string, object: Partial<EditableData<Data>>): Promise<Readonly<SelectedPick<Record, ['*']>>>;
44
47
  /**
45
48
  * Partially updates multiple records.
46
49
  * @param objects An array of objects with their ids and columns to be updated.
47
50
  * @returns Array of the persisted records.
48
51
  */
49
- abstract update(objects: Array<Partial<EditableData<Data>> & Identifiable>): Promise<SelectedPick<Record, ['*']>[]>;
52
+ abstract update(objects: Array<Partial<EditableData<Data>> & Identifiable>): Promise<Readonly<SelectedPick<Record, ['*']>>[]>;
50
53
  /**
51
54
  * Creates or updates a single record. If a record exists with the given id,
52
55
  * it will be update, otherwise a new record will be created.
53
56
  * @param object Object containing the column names with their values to be persisted in the table.
54
57
  * @returns The full persisted record.
55
58
  */
56
- abstract createOrUpdate(object: EditableData<Data> & Identifiable): Promise<SelectedPick<Record, ['*']>>;
59
+ abstract createOrUpdate(object: EditableData<Data> & Identifiable): Promise<Readonly<SelectedPick<Record, ['*']>>>;
57
60
  /**
58
61
  * Creates or updates a single record. If a record exists with the given id,
59
62
  * it will be update, otherwise a new record will be created.
@@ -61,14 +64,14 @@ export declare abstract class Repository<Data extends BaseData, Record extends X
61
64
  * @param object The column names and the values to be persisted.
62
65
  * @returns The full persisted record.
63
66
  */
64
- abstract createOrUpdate(id: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
67
+ abstract createOrUpdate(id: string, object: EditableData<Data>): Promise<Readonly<SelectedPick<Record, ['*']>>>;
65
68
  /**
66
69
  * Creates or updates a single record. If a record exists with the given id,
67
70
  * it will be update, otherwise a new record will be created.
68
71
  * @param objects Array of objects with the column names and the values to be stored in the table.
69
72
  * @returns Array of the persisted records.
70
73
  */
71
- abstract createOrUpdate(objects: Array<EditableData<Data> & Identifiable>): Promise<SelectedPick<Record, ['*']>[]>;
74
+ abstract createOrUpdate(objects: Array<EditableData<Data> & Identifiable>): Promise<Readonly<SelectedPick<Record, ['*']>>[]>;
72
75
  /**
73
76
  * Deletes a record given its unique id.
74
77
  * @param id The unique id.
@@ -93,11 +96,25 @@ export declare abstract class Repository<Data extends BaseData, Record extends X
93
96
  * @throws If the record could not be found or there was an error while performing the deletion.
94
97
  */
95
98
  abstract delete(ids: Identifiable[]): Promise<void>;
99
+ /**
100
+ * Search for records in the table.
101
+ * @param query The query to search for.
102
+ * @param options The options to search with (like: fuzziness)
103
+ * @returns The found records.
104
+ */
105
+ abstract search(query: string, options?: {
106
+ fuzziness?: number;
107
+ }): Promise<SelectedPick<Record, ['*']>[]>;
96
108
  abstract query<Result extends XataRecord>(query: Query<Record, Result>): Promise<Page<Record, Result>>;
97
109
  }
98
110
  export declare class RestRepository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record, SelectedPick<Record, ['*']>> {
99
111
  #private;
100
- constructor(client: BaseClient<any>, table: string);
112
+ constructor(options: {
113
+ table: string;
114
+ links?: LinkDictionary;
115
+ getFetchProps: () => Promise<FetcherExtraProps>;
116
+ schemaNamespace: SchemaNamespaceResult<any>;
117
+ });
101
118
  create(object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
102
119
  create(recordId: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
103
120
  create(objects: EditableData<Data>[]): Promise<SelectedPick<Record, ['*']>[]>;
@@ -109,40 +126,9 @@ export declare class RestRepository<Data extends BaseData, Record extends XataRe
109
126
  createOrUpdate(recordId: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
110
127
  createOrUpdate(objects: EditableData<Data>[]): Promise<SelectedPick<Record, ['*']>[]>;
111
128
  delete(recordId: string | Identifiable | Array<string | Identifiable>): Promise<void>;
129
+ search(query: string, options?: {
130
+ fuzziness?: number;
131
+ }): Promise<SelectedPick<Record, ['*']>[]>;
112
132
  query<Result extends XataRecord>(query: Query<Record, Result>): Promise<Page<Record, Result>>;
113
133
  }
114
- interface RepositoryFactory {
115
- createRepository<Data extends BaseData>(client: BaseClient<any>, table: string): Repository<Data & XataRecord>;
116
- }
117
- export declare class RestRespositoryFactory implements RepositoryFactory {
118
- createRepository<Data extends BaseData>(client: BaseClient<any>, table: string): Repository<Data & XataRecord>;
119
- }
120
- declare type BranchStrategyValue = string | undefined | null;
121
- declare type BranchStrategyBuilder = () => BranchStrategyValue | Promise<BranchStrategyValue>;
122
- declare type BranchStrategy = BranchStrategyValue | BranchStrategyBuilder;
123
- declare type BranchStrategyOption = NonNullable<BranchStrategy | BranchStrategy[]>;
124
- export declare type XataClientOptions = {
125
- /**
126
- * Fetch implementation. This option is only required if the runtime does not include a fetch implementation
127
- * available in the global scope. If you are running your code on Deno or Cloudflare workers for example,
128
- * you won't need to provide a specific fetch implementation. But for most versions of Node.js you'll need
129
- * to provide one. Such as cross-fetch, node-fetch or isomorphic-fetch.
130
- */
131
- fetch?: FetchImpl;
132
- databaseURL?: string;
133
- branch?: BranchStrategyOption;
134
- /**
135
- * API key to be used. You can create one in your account settings at https://app.xata.io/settings.
136
- */
137
- apiKey?: string;
138
- repositoryFactory?: RepositoryFactory;
139
- };
140
- export declare class BaseClient<D extends Record<string, Repository<any>> = Record<string, Repository<any>>> {
141
- #private;
142
- options: XataClientOptions;
143
- db: D;
144
- constructor(options?: XataClientOptions, links?: Links);
145
- initObject<T>(table: string, object: object): T;
146
- getBranch(): Promise<string>;
147
- }
148
134
  export {};
@@ -19,20 +19,11 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
19
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
20
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
21
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;
22
+ var _RestRepository_instances, _RestRepository_table, _RestRepository_links, _RestRepository_getFetchProps, _RestRepository_schemaNamespace, _RestRepository_insertRecordWithoutId, _RestRepository_insertRecordWithId, _RestRepository_bulkInsertTableRecords, _RestRepository_updateRecordWithID, _RestRepository_upsertRecordWithID, _RestRepository_deleteRecord, _RestRepository_initObject;
30
23
  Object.defineProperty(exports, "__esModule", { value: true });
31
- exports.BaseClient = exports.RestRespositoryFactory = exports.RestRepository = exports.Repository = void 0;
24
+ exports.RestRepository = exports.Repository = void 0;
32
25
  const api_1 = require("../api");
33
- const fetch_1 = require("../util/fetch");
34
26
  const lang_1 = require("../util/lang");
35
- const config_1 = require("./config");
36
27
  const pagination_1 = require("./pagination");
37
28
  const query_1 = require("./query");
38
29
  const record_1 = require("./record");
@@ -44,15 +35,18 @@ class Repository extends query_1.Query {
44
35
  }
45
36
  exports.Repository = Repository;
46
37
  class RestRepository extends query_1.Query {
47
- constructor(client, table) {
48
- super(null, table, {});
38
+ constructor(options) {
39
+ var _a;
40
+ super(null, options.table, {});
49
41
  _RestRepository_instances.add(this);
50
- _RestRepository_client.set(this, void 0);
51
- _RestRepository_fetch.set(this, void 0);
52
42
  _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");
43
+ _RestRepository_links.set(this, void 0);
44
+ _RestRepository_getFetchProps.set(this, void 0);
45
+ _RestRepository_schemaNamespace.set(this, void 0);
46
+ __classPrivateFieldSet(this, _RestRepository_table, options.table, "f");
47
+ __classPrivateFieldSet(this, _RestRepository_links, (_a = options.links) !== null && _a !== void 0 ? _a : {}, "f");
48
+ __classPrivateFieldSet(this, _RestRepository_getFetchProps, options.getFetchProps, "f");
49
+ __classPrivateFieldSet(this, _RestRepository_schemaNamespace, options.schemaNamespace, "f");
56
50
  }
57
51
  create(a, b) {
58
52
  return __awaiter(this, void 0, void 0, function* () {
@@ -82,10 +76,10 @@ class RestRepository extends query_1.Query {
82
76
  // TODO: Add column support: https://github.com/xataio/openapi/issues/139
83
77
  read(recordId) {
84
78
  return __awaiter(this, void 0, void 0, function* () {
85
- const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
79
+ const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
86
80
  try {
87
81
  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);
82
+ return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_initObject).call(this, __classPrivateFieldGet(this, _RestRepository_table, "f"), response);
89
83
  }
90
84
  catch (e) {
91
85
  if ((0, lang_1.isObject)(e) && e.status === 404) {
@@ -161,6 +155,13 @@ class RestRepository extends query_1.Query {
161
155
  throw new Error('Invalid arguments for delete method');
162
156
  });
163
157
  }
158
+ search(query, options = {}) {
159
+ return __awaiter(this, void 0, void 0, function* () {
160
+ const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
161
+ 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));
162
+ return records.map((item) => __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_initObject).call(this, __classPrivateFieldGet(this, _RestRepository_table, "f"), item));
163
+ });
164
+ }
164
165
  query(query) {
165
166
  var _a;
166
167
  return __awaiter(this, void 0, void 0, function* () {
@@ -171,39 +172,17 @@ class RestRepository extends query_1.Query {
171
172
  page: data.page,
172
173
  columns: data.columns
173
174
  };
174
- const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
175
+ const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
175
176
  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
+ const records = objects.map((record) => __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_initObject).call(this, __classPrivateFieldGet(this, _RestRepository_table, "f"), record));
177
178
  return new pagination_1.Page(query, meta, records);
178
179
  });
179
180
  }
180
181
  }
181
182
  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) {
183
+ _RestRepository_table = new WeakMap(), _RestRepository_links = new WeakMap(), _RestRepository_getFetchProps = new WeakMap(), _RestRepository_schemaNamespace = new WeakMap(), _RestRepository_instances = new WeakSet(), _RestRepository_insertRecordWithoutId = function _RestRepository_insertRecordWithoutId(object) {
205
184
  return __awaiter(this, void 0, void 0, function* () {
206
- const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
185
+ const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
207
186
  const record = transformObjectLinks(object);
208
187
  const response = yield (0, api_1.insertRecord)(Object.assign({ pathParams: {
209
188
  workspace: '{workspaceId}',
@@ -218,7 +197,7 @@ _RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _
218
197
  });
219
198
  }, _RestRepository_insertRecordWithId = function _RestRepository_insertRecordWithId(recordId, object) {
220
199
  return __awaiter(this, void 0, void 0, function* () {
221
- const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
200
+ const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
222
201
  const record = transformObjectLinks(object);
223
202
  const response = yield (0, api_1.insertRecordWithID)(Object.assign({ pathParams: {
224
203
  workspace: '{workspaceId}',
@@ -234,7 +213,7 @@ _RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _
234
213
  });
235
214
  }, _RestRepository_bulkInsertTableRecords = function _RestRepository_bulkInsertTableRecords(objects) {
236
215
  return __awaiter(this, void 0, void 0, function* () {
237
- const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
216
+ const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
238
217
  const records = objects.map((object) => transformObjectLinks(object));
239
218
  const response = yield (0, api_1.bulkInsertTableRecords)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f") }, body: { records } }, fetchProps));
240
219
  const finalObjects = yield this.any(...response.recordIDs.map((id) => this.filter('id', id))).getAll();
@@ -245,7 +224,7 @@ _RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _
245
224
  });
246
225
  }, _RestRepository_updateRecordWithID = function _RestRepository_updateRecordWithID(recordId, object) {
247
226
  return __awaiter(this, void 0, void 0, function* () {
248
- const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
227
+ const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
249
228
  const record = transformObjectLinks(object);
250
229
  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
230
  const item = yield this.read(response.id);
@@ -255,7 +234,7 @@ _RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _
255
234
  });
256
235
  }, _RestRepository_upsertRecordWithID = function _RestRepository_upsertRecordWithID(recordId, object) {
257
236
  return __awaiter(this, void 0, void 0, function* () {
258
- const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
237
+ const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
259
238
  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
239
  const item = yield this.read(response.id);
261
240
  if (!item)
@@ -264,114 +243,42 @@ _RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _
264
243
  });
265
244
  }, _RestRepository_deleteRecord = function _RestRepository_deleteRecord(recordId) {
266
245
  return __awaiter(this, void 0, void 0, function* () {
267
- const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
246
+ const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
268
247
  yield (0, api_1.deleteRecord)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId } }, fetchProps));
269
248
  });
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
- }
249
+ }, _RestRepository_initObject = function _RestRepository_initObject(table, object) {
250
+ const result = {};
251
+ Object.assign(result, object);
252
+ const tableLinks = __classPrivateFieldGet(this, _RestRepository_links, "f")[table] || [];
253
+ for (const link of tableLinks) {
254
+ const [field, linkTable] = link;
255
+ const value = result[field];
256
+ if (value && (0, lang_1.isObject)(value)) {
257
+ result[field] = __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_initObject).call(this, linkTable, value);
320
258
  }
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
259
  }
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
- });
260
+ const db = __classPrivateFieldGet(this, _RestRepository_schemaNamespace, "f");
261
+ result.read = function () {
262
+ return db[table].read(result['id']);
263
+ };
264
+ result.update = function (data) {
265
+ return db[table].update(result['id'], data);
266
+ };
267
+ result.delete = function () {
268
+ return db[table].delete(result['id']);
269
+ };
270
+ for (const prop of ['read', 'update', 'delete']) {
271
+ Object.defineProperty(result, prop, { enumerable: false });
366
272
  }
367
- }
368
- exports.BaseClient = BaseClient;
369
- _BaseClient_links = new WeakMap(), _BaseClient_branch = new WeakMap();
370
- const isBranchStrategyBuilder = (strategy) => {
371
- return typeof strategy === 'function';
273
+ Object.freeze(result);
274
+ return result;
372
275
  };
373
276
  const transformObjectLinks = (object) => {
374
277
  return Object.entries(object).reduce((acc, [key, value]) => {
278
+ // Ignore internal properties
279
+ if (key === 'xata')
280
+ return acc;
281
+ // Transform links to identifier
375
282
  return Object.assign(Object.assign({}, acc), { [key]: (0, record_1.isIdentifiable)(value) ? value.id : value });
376
283
  }, {});
377
284
  };
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  /* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-floating-promises */
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
+ const vitest_1 = require("vitest");
4
5
  // SelectableColumn<O> //
5
6
  // --------------------------------------------------------------------------- //
6
7
  const validTeamColumns = [
@@ -198,6 +199,6 @@ function test8(user) {
198
199
  user.partner = null;
199
200
  user.team = null;
200
201
  }
201
- test('fake test', () => {
202
+ (0, vitest_1.test)('fake test', () => {
202
203
  // This is a fake test to make sure that the type definitions in this file are working
203
204
  });
@@ -1,9 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ /* eslint-disable @typescript-eslint/no-unused-vars */
4
+ const vitest_1 = require("vitest");
3
5
  // Simple sorting
4
6
  const simpleSorting = { name: 'asc' };
5
7
  // Array of simple sorting
6
8
  const arrayOfSimpleSorting = [{ name: 'asc' }, { age: 'desc' }];
7
- test('fake test', () => {
9
+ (0, vitest_1.test)('fake test', () => {
8
10
  // This is a fake test to make sure that the type definitions in this file are working
9
11
  });
@@ -0,0 +1,20 @@
1
+ import { Namespace, NamespaceBuildOptions } from '../namespace';
2
+ import { BaseData, XataRecord } from '../schema/record';
3
+ import { SelectedPick } from '../schema/selection';
4
+ import { GetArrayInnerType } from '../util/types';
5
+ export declare class SearchNamespace<Schemas extends Record<string, BaseData>> extends Namespace {
6
+ build({ getFetchProps }: NamespaceBuildOptions): <Tables extends Extract<keyof Schemas, string>>(query: string, options?: {
7
+ fuzziness?: number | undefined;
8
+ tables?: Tables[] | undefined;
9
+ } | undefined) => Promise<{ [Model in Tables]: SelectedPick<Schemas[Model] & XataRecord & {
10
+ xata: {
11
+ table: string;
12
+ };
13
+ }, ["*"]>[]; }>;
14
+ }
15
+ declare type SearchXataRecord = XataRecord & {
16
+ xata: {
17
+ table: string;
18
+ };
19
+ };
20
+ export {};
@@ -0,0 +1,30 @@
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.SearchNamespace = void 0;
13
+ const api_1 = require("../api");
14
+ const namespace_1 = require("../namespace");
15
+ class SearchNamespace extends namespace_1.Namespace {
16
+ build({ getFetchProps }) {
17
+ return (query, options) => __awaiter(this, void 0, void 0, function* () {
18
+ const fetchProps = yield getFetchProps();
19
+ const { tables, fuzziness } = options !== null && options !== void 0 ? options : {};
20
+ const { records } = yield (0, api_1.searchBranch)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}' }, body: { tables, query, fuzziness } }, fetchProps));
21
+ return records.reduce((acc, record) => {
22
+ var _a;
23
+ const { table = 'orphan' } = record.xata;
24
+ const items = (_a = acc[table]) !== null && _a !== void 0 ? _a : [];
25
+ return Object.assign(Object.assign({}, acc), { [table]: [...items, record] });
26
+ }, {});
27
+ });
28
+ }
29
+ }
30
+ exports.SearchNamespace = SearchNamespace;
@@ -0,0 +1,5 @@
1
+ export declare type BranchStrategyValue = string | undefined | null;
2
+ export declare type BranchStrategyBuilder = () => BranchStrategyValue | Promise<BranchStrategyValue>;
3
+ export declare type BranchStrategy = BranchStrategyValue | BranchStrategyBuilder;
4
+ export declare type BranchStrategyOption = NonNullable<BranchStrategy | BranchStrategy[]>;
5
+ export declare const isBranchStrategyBuilder: (strategy: BranchStrategy) => strategy is BranchStrategyBuilder;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isBranchStrategyBuilder = void 0;
4
+ const isBranchStrategyBuilder = (strategy) => {
5
+ return typeof strategy === 'function';
6
+ };
7
+ exports.isBranchStrategyBuilder = isBranchStrategyBuilder;
@@ -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 {};