@xata.io/client 0.0.0-beta.d76c443 → 0.0.0-beta.dbbad41

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,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
- export declare type Links = Record<string, Array<string[]>>;
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?: Links);
145
- initObject<T>(table: string, object: object): T;
146
- getBranch(): Promise<string>;
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,7 +26,7 @@ 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, _BaseClient_links, _BaseClient_branch;
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");
@@ -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, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), response);
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, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), record));
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, _RestRepository_client, "f").getBranch();
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,10 +306,36 @@ _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;
@@ -286,82 +351,30 @@ function resolveXataClientOptions(options) {
286
351
  branch });
287
352
  }
288
353
  class BaseClient {
289
- constructor(options = {}, links = {}) {
290
- _BaseClient_links.set(this, void 0);
291
- _BaseClient_branch.set(this, void 0);
354
+ constructor(options = {}, links) {
292
355
  this.options = resolveXataClientOptions(options);
293
356
  // Make this property not enumerable so it doesn't show up in console.dir, etc.
294
357
  Object.defineProperty(this.options, 'apiKey', { enumerable: false });
295
- __classPrivateFieldSet(this, _BaseClient_links, links, "f");
296
358
  const factory = this.options.repositoryFactory || new RestRespositoryFactory();
297
359
  this.db = new Proxy({}, {
298
360
  get: (_target, prop) => {
299
- if (typeof prop !== 'string')
361
+ if (!(0, lang_1.isString)(prop))
300
362
  throw new Error('Invalid table name');
301
- return factory.createRepository(this, prop);
363
+ return factory.createRepository(this, prop, links);
302
364
  }
303
365
  });
304
366
  }
305
- initObject(table, object) {
306
- const result = {};
307
- Object.assign(result, object);
308
- const tableLinks = __classPrivateFieldGet(this, _BaseClient_links, "f")[table] || [];
309
- for (const link of tableLinks) {
310
- const [field, linkTable] = link;
311
- const value = result[field];
312
- if (value && (0, lang_1.isObject)(value)) {
313
- result[field] = this.initObject(linkTable, value);
314
- }
315
- }
316
- const db = this.db;
317
- result.read = function () {
318
- return db[table].read(result['id']);
319
- };
320
- result.update = function (data) {
321
- return db[table].update(result['id'], data);
322
- };
323
- result.delete = function () {
324
- return db[table].delete(result['id']);
325
- };
326
- for (const prop of ['read', 'update', 'delete']) {
327
- Object.defineProperty(result, prop, { enumerable: false });
328
- }
329
- Object.freeze(result);
330
- return result;
331
- }
332
- getBranch() {
333
- var e_1, _a;
367
+ search(query, options) {
368
+ var _a;
334
369
  return __awaiter(this, void 0, void 0, function* () {
335
- if (__classPrivateFieldGet(this, _BaseClient_branch, "f"))
336
- return __classPrivateFieldGet(this, _BaseClient_branch, "f");
337
- const { branch: param } = this.options;
338
- const strategies = Array.isArray(param) ? [...param] : [param];
339
- const evaluateBranch = (strategy) => __awaiter(this, void 0, void 0, function* () {
340
- return isBranchStrategyBuilder(strategy) ? yield strategy() : strategy;
341
- });
342
- try {
343
- for (var strategies_1 = __asyncValues(strategies), strategies_1_1; strategies_1_1 = yield strategies_1.next(), !strategies_1_1.done;) {
344
- const strategy = strategies_1_1.value;
345
- const branch = yield evaluateBranch(strategy);
346
- if (branch) {
347
- __classPrivateFieldSet(this, _BaseClient_branch, branch, "f");
348
- return branch;
349
- }
350
- }
351
- }
352
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
353
- finally {
354
- try {
355
- if (strategies_1_1 && !strategies_1_1.done && (_a = strategies_1.return)) yield _a.call(strategies_1);
356
- }
357
- finally { if (e_1) throw e_1.error; }
358
- }
359
- 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);
360
374
  });
361
375
  }
362
376
  }
363
377
  exports.BaseClient = BaseClient;
364
- _BaseClient_links = new WeakMap(), _BaseClient_branch = new WeakMap();
365
378
  const isBranchStrategyBuilder = (strategy) => {
366
379
  return typeof strategy === 'function';
367
380
  };
@@ -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.0.0-beta.d76c443",
3
+ "version": "0.0.0-beta.dbbad41",
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": "d76c443e4769df5339ab46ecd2714a8613dd9084"
26
+ "gitHead": "dbbad41f2eba57aac5fcb90380bb3355a0126925"
27
27
  }