@xata.io/client 0.0.0-beta.518ec9b → 0.0.0-beta.5f52401

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.
@@ -26,18 +26,23 @@ 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, _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 lang_1 = require("../util/lang");
33
34
  const filters_1 = require("./filters");
34
35
  const pagination_1 = require("./pagination");
35
36
  const query_1 = require("./query");
37
+ /**
38
+ * Common interface for performing operations on a table.
39
+ */
36
40
  class Repository extends query_1.Query {
37
41
  }
38
42
  exports.Repository = Repository;
39
43
  class RestRepository extends Repository {
40
44
  constructor(client, table) {
45
+ var _a;
41
46
  super(null, table, {});
42
47
  _RestRepository_instances.add(this);
43
48
  _RestRepository_client.set(this, void 0);
@@ -46,33 +51,32 @@ class RestRepository extends Repository {
46
51
  __classPrivateFieldSet(this, _RestRepository_client, client, "f");
47
52
  __classPrivateFieldSet(this, _RestRepository_table, table, "f");
48
53
  // TODO: Remove when integrating with API client
49
- const fetchImpl = typeof fetch !== 'undefined' ? fetch : __classPrivateFieldGet(this, _RestRepository_client, "f").options.fetch;
54
+ const globalFetch = typeof fetch !== 'undefined' ? fetch : undefined;
55
+ const fetchImpl = (_a = __classPrivateFieldGet(this, _RestRepository_client, "f").options.fetch) !== null && _a !== void 0 ? _a : globalFetch;
50
56
  if (!fetchImpl) {
57
+ /** @todo add a link after docs exist */
51
58
  throw new Error(`The \`fetch\` option passed to the Xata client is resolving to a falsy value and may not be correctly imported.`);
52
59
  }
53
60
  __classPrivateFieldSet(this, _RestRepository_fetch, fetchImpl, "f");
54
61
  }
55
- create(object) {
62
+ create(a, b) {
56
63
  return __awaiter(this, void 0, void 0, function* () {
57
- const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
58
- const record = transformObjectLinks(object);
59
- const response = object.id
60
- ? yield (0, api_1.insertRecordWithID)(Object.assign({ pathParams: {
61
- workspace: '{workspaceId}',
62
- dbBranchName: '{dbBranch}',
63
- tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"),
64
- recordId: object.id
65
- }, body: record }, fetchProps))
66
- : yield (0, api_1.insertRecord)(Object.assign({ pathParams: {
67
- workspace: '{workspaceId}',
68
- dbBranchName: '{dbBranch}',
69
- tableName: __classPrivateFieldGet(this, _RestRepository_table, "f")
70
- }, body: record }, fetchProps));
71
- const finalObject = yield this.read(response.id);
72
- if (!finalObject) {
73
- throw new Error('The server failed to save the record');
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
+ else if ((0, lang_1.isObject)(a) && (0, lang_1.isString)(a.id)) {
70
+ if (a.id === '')
71
+ throw new Error("The id can't be empty");
72
+ return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_insertRecordWithId).call(this, a.id, Object.assign(Object.assign({}, a), { id: undefined }));
73
+ }
74
+ else if ((0, lang_1.isObject)(a)) {
75
+ return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_insertRecordWithoutId).call(this, a);
76
+ }
77
+ else {
78
+ throw new Error('Invalid arguments for create method');
74
79
  }
75
- return finalObject;
76
80
  });
77
81
  }
78
82
  createMany(objects) {
@@ -80,10 +84,9 @@ class RestRepository extends Repository {
80
84
  const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
81
85
  const records = objects.map((object) => transformObjectLinks(object));
82
86
  const response = yield (0, api_1.bulkInsertTableRecords)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f") }, body: { records } }, fetchProps));
83
- // TODO: Use filer.$any() to get all the records
84
- const finalObjects = yield Promise.all(response.recordIDs.map((id) => this.read(id)));
85
- if (finalObjects.some((object) => !object)) {
86
- throw new Error('The server failed to save the record');
87
+ const finalObjects = yield this.any(...response.recordIDs.map((id) => this.filter('id', id))).getAll();
88
+ if (finalObjects.length !== objects.length) {
89
+ throw new Error('The server failed to save some records');
87
90
  }
88
91
  return finalObjects;
89
92
  });
@@ -105,7 +108,7 @@ class RestRepository extends Repository {
105
108
  return item;
106
109
  });
107
110
  }
108
- upsert(recordId, object) {
111
+ createOrUpdate(recordId, object) {
109
112
  return __awaiter(this, void 0, void 0, function* () {
110
113
  const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
111
114
  const response = yield (0, api_1.upsertRecordWithID)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId }, body: object }, fetchProps));
@@ -157,6 +160,37 @@ _RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _
157
160
  }
158
161
  };
159
162
  });
163
+ }, _RestRepository_insertRecordWithoutId = function _RestRepository_insertRecordWithoutId(object) {
164
+ return __awaiter(this, void 0, void 0, function* () {
165
+ const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
166
+ const record = transformObjectLinks(object);
167
+ const response = yield (0, api_1.insertRecord)(Object.assign({ pathParams: {
168
+ workspace: '{workspaceId}',
169
+ dbBranchName: '{dbBranch}',
170
+ tableName: __classPrivateFieldGet(this, _RestRepository_table, "f")
171
+ }, body: record }, fetchProps));
172
+ const finalObject = yield this.read(response.id);
173
+ if (!finalObject) {
174
+ throw new Error('The server failed to save the record');
175
+ }
176
+ return finalObject;
177
+ });
178
+ }, _RestRepository_insertRecordWithId = function _RestRepository_insertRecordWithId(recordId, object) {
179
+ return __awaiter(this, void 0, void 0, function* () {
180
+ const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
181
+ const record = transformObjectLinks(object);
182
+ const response = yield (0, api_1.insertRecordWithID)(Object.assign({ pathParams: {
183
+ workspace: '{workspaceId}',
184
+ dbBranchName: '{dbBranch}',
185
+ tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"),
186
+ recordId
187
+ }, body: record, queryParams: { createOnly: true } }, fetchProps));
188
+ const finalObject = yield this.read(response.id);
189
+ if (!finalObject) {
190
+ throw new Error('The server failed to save the record');
191
+ }
192
+ return finalObject;
193
+ });
160
194
  };
161
195
  class RestRespositoryFactory {
162
196
  createRepository(client, table) {
@@ -173,6 +207,14 @@ class BaseClient {
173
207
  }
174
208
  this.options = options;
175
209
  __classPrivateFieldSet(this, _BaseClient_links, links, "f");
210
+ const factory = options.repositoryFactory || new RestRespositoryFactory();
211
+ this.db = new Proxy({}, {
212
+ get: (_target, prop) => {
213
+ if (typeof prop !== 'string')
214
+ throw new Error('Invalid table name');
215
+ return factory.createRepository(this, prop);
216
+ }
217
+ });
176
218
  }
177
219
  initObject(table, object) {
178
220
  const o = {};
@@ -181,7 +223,7 @@ class BaseClient {
181
223
  for (const link of tableLinks) {
182
224
  const [field, linkTable] = link;
183
225
  const value = o[field];
184
- if (value && typeof value === 'object') {
226
+ if (value && (0, lang_1.isObject)(value)) {
185
227
  const { id } = value;
186
228
  if (Object.keys(value).find((col) => col === 'id')) {
187
229
  o[field] = this.initObject(linkTable, value);
@@ -190,7 +232,7 @@ class BaseClient {
190
232
  o[field] = {
191
233
  id,
192
234
  get: () => {
193
- this.db[linkTable].read(id);
235
+ return this.db[linkTable].read(id);
194
236
  }
195
237
  };
196
238
  }
@@ -251,8 +293,8 @@ const isBranchStrategyBuilder = (strategy) => {
251
293
  };
252
294
  // TODO: We can find a better implementation for links
253
295
  const transformObjectLinks = (object) => {
254
- return Object.entries(object).reduce((acc, [key, value]) => {
255
- if (value && typeof value === 'object' && typeof value.id === 'string') {
296
+ return Object.entries(object !== null && object !== void 0 ? object : {}).reduce((acc, [key, value]) => {
297
+ if ((0, lang_1.isObject)(value) && (0, lang_1.isString)(value.id)) {
256
298
  return Object.assign(Object.assign({}, acc), { [key]: value.id });
257
299
  }
258
300
  return Object.assign(Object.assign({}, acc), { [key]: value });
@@ -1,12 +1,10 @@
1
- import { XataRecord } from '..';
2
- import { PartialBy } from '../util/lang';
3
1
  import { StringKeys, UnionToIntersection, Values } from '../util/types';
4
2
  import { Query } from './query';
3
+ import { BaseData, Identifiable, XataRecord } from './record';
5
4
  declare type Queries<T> = {
6
- [key in keyof T as T[key] extends Query<any> ? key : never]: T[key];
5
+ [key in keyof T as T[key] extends Query<any, any> ? key : never]: T[key];
7
6
  };
8
- declare type InternalProperties = keyof Omit<XataRecord, 'id'>;
9
- export declare type Selectable<T extends XataRecord> = Omit<PartialBy<T, 'id'>, InternalProperties>;
7
+ export declare type Selectable<T extends BaseData> = T & Partial<Identifiable>;
10
8
  export declare type SelectableColumn<O> = '*' | (O extends Array<unknown> ? never : O extends Record<string, any> ? '*' | Values<{
11
9
  [K in StringKeys<O>]: O[K] extends Record<string, any> ? `${K}.${SelectableColumn<O[K]>}` : K;
12
10
  }> : '');
@@ -1,2 +1,4 @@
1
1
  export declare function compact<T>(arr: Array<T | null | undefined>): T[];
2
2
  export declare type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
3
+ export declare function isObject(value: any): value is object;
4
+ export declare function isString(value: any): value is string;
package/dist/util/lang.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.compact = void 0;
3
+ exports.isString = exports.isObject = exports.compact = void 0;
4
4
  function notEmpty(value) {
5
5
  return value !== null && value !== undefined;
6
6
  }
@@ -8,3 +8,11 @@ function compact(arr) {
8
8
  return arr.filter(notEmpty);
9
9
  }
10
10
  exports.compact = compact;
11
+ function isObject(value) {
12
+ return value !== undefined && value !== null && typeof value === 'object';
13
+ }
14
+ exports.isObject = isObject;
15
+ function isString(value) {
16
+ return value !== undefined && value !== null && typeof value === 'string';
17
+ }
18
+ exports.isString = isString;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xata.io/client",
3
- "version": "0.0.0-beta.518ec9b",
3
+ "version": "0.0.0-beta.5f52401",
4
4
  "description": "Xata.io SDK for TypeScript and JavaScript",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -20,5 +20,5 @@
20
20
  "url": "https://github.com/xataio/client-ts/issues"
21
21
  },
22
22
  "homepage": "https://github.com/xataio/client-ts/blob/main/client/README.md",
23
- "gitHead": "518ec9b02b676c3f01cb30e12beb7df315275eff"
23
+ "gitHead": "5f5240141e43b289bf359b027154e208bc902875"
24
24
  }