@xata.io/client 0.0.0-beta.09892c4 → 0.0.0-beta.1550d19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc.cjs ADDED
@@ -0,0 +1,13 @@
1
+ module.exports = {
2
+ ignorePatterns: ["dist"],
3
+ parserOptions: {
4
+ ecmaVersion: 2020,
5
+ sourceType: 'module',
6
+ project: 'client/tsconfig.json'
7
+ },
8
+ rules: {
9
+ '@typescript-eslint/no-explicit-any': 'off',
10
+ '@typescript-eslint/ban-types': 'off',
11
+ '@typescript-eslint/no-floating-promises': 'error',
12
+ }
13
+ };
@@ -10,6 +10,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.fetch = void 0;
13
+ /* eslint-disable @typescript-eslint/no-throw-literal */
14
+ /* eslint-disable @typescript-eslint/ban-types */
15
+ const lang_1 = require("../util/lang");
13
16
  const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
14
17
  const query = new URLSearchParams(queryParams).toString();
15
18
  const queryString = query.length > 0 ? `?${query}` : '';
@@ -65,7 +68,7 @@ function fetch({ url: path, method, body, headers, pathParams, queryParams, fetc
65
68
  };
66
69
  throw withStatus(error, response.status);
67
70
  }
68
- else if (typeof e === 'object' && typeof e.message === 'string') {
71
+ else if ((0, lang_1.isObject)(e) && (0, lang_1.isString)(e.message)) {
69
72
  throw withStatus(e, response.status);
70
73
  }
71
74
  else {
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getHostUrl = void 0;
4
+ const lang_1 = require("../util/lang");
4
5
  function getHostUrl(provider, type) {
5
6
  if (isValidAlias(provider)) {
6
7
  return providers[provider][type];
@@ -22,8 +23,8 @@ const providers = {
22
23
  }
23
24
  };
24
25
  function isValidAlias(alias) {
25
- return typeof alias === 'string' && Object.keys(providers).includes(alias);
26
+ return (0, lang_1.isString)(alias) && Object.keys(providers).includes(alias);
26
27
  }
27
28
  function isValidBuilder(builder) {
28
- return typeof builder === 'object' && typeof builder.main === 'string' && typeof builder.workspaces === 'string';
29
+ return (0, lang_1.isObject)(builder) && (0, lang_1.isString)(builder.main) && (0, lang_1.isString)(builder.workspaces);
29
30
  }
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.buildSortFilter = exports.isSortFilterObject = void 0;
4
+ const lang_1 = require("../util/lang");
4
5
  function isSortFilterObject(filter) {
5
- return typeof filter === 'object' && filter.column !== undefined;
6
+ return (0, lang_1.isObject)(filter) && filter.column !== undefined;
6
7
  }
7
8
  exports.isSortFilterObject = isSortFilterObject;
8
9
  function buildSortFilter(filter) {
@@ -1,14 +1,21 @@
1
1
  import { FetchImpl } from '../api/fetcher';
2
2
  import { Page } from './pagination';
3
3
  import { Query, QueryOptions } from './query';
4
- import { BaseData, XataRecord } from './record';
4
+ import { BaseData, Identifiable, XataRecord } from './record';
5
5
  import { Select, SelectableColumn } from './selection';
6
6
  export declare type Links = Record<string, Array<string[]>>;
7
7
  /**
8
8
  * Common interface for performing operations on a table.
9
9
  */
10
10
  export declare abstract class Repository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record> {
11
- abstract create(object: Data): Promise<Record>;
11
+ abstract create(object: Data & Partial<Identifiable>): Promise<Record>;
12
+ /**
13
+ * Creates a single record in the table with a unique id.
14
+ * @param id The unique id.
15
+ * @param object Object containing the column names with their values to be stored in the table.
16
+ * @returns The full persisted record.
17
+ */
18
+ abstract create(id: string, object: Data): Promise<Record>;
12
19
  /**
13
20
  * Creates multiple records in the table.
14
21
  * @param objects Array of objects with the column names and the values to be stored in the table.
@@ -21,13 +28,6 @@ export declare abstract class Repository<Data extends BaseData, Record extends X
21
28
  * @returns The persisted record for the given id or null if the record could not be found.
22
29
  */
23
30
  abstract read(id: string): Promise<Record | null>;
24
- /**
25
- * Insert a single record with a unique id.
26
- * @param id The unique id.
27
- * @param object Object containing the column names with their values to be stored in the table.
28
- * @returns The full persisted record.
29
- */
30
- abstract insert(id: string, object: Data): Promise<Record>;
31
31
  /**
32
32
  * Partially update a single record given its unique id.
33
33
  * @param id The unique id.
@@ -36,13 +36,13 @@ export declare abstract class Repository<Data extends BaseData, Record extends X
36
36
  */
37
37
  abstract update(id: string, object: Partial<Data>): Promise<Record>;
38
38
  /**
39
- * Updates or inserts a single record. If a record exists with the given id,
39
+ * Creates or updates a single record. If a record exists with the given id,
40
40
  * it will be update, otherwise a new record will be created.
41
41
  * @param id A unique id.
42
42
  * @param object The column names and the values to be persisted.
43
43
  * @returns The full persisted record.
44
44
  */
45
- abstract updateOrInsert(id: string, object: Data): Promise<Record>;
45
+ abstract createOrUpdate(id: string, object: Data): Promise<Record>;
46
46
  /**
47
47
  * Deletes a record given its unique id.
48
48
  * @param id The unique id.
@@ -57,11 +57,11 @@ export declare class RestRepository<Data extends BaseData, Record extends XataRe
57
57
  #private;
58
58
  constructor(client: BaseClient<any>, table: string);
59
59
  create(object: Data): Promise<Record>;
60
+ create(recordId: string, object: Data): Promise<Record>;
60
61
  createMany(objects: Data[]): Promise<Record[]>;
61
62
  read(recordId: string): Promise<Record | null>;
62
63
  update(recordId: string, object: Partial<Data>): Promise<Record>;
63
- insert(recordId: string, object: Data): Promise<Record>;
64
- updateOrInsert(recordId: string, object: Data): Promise<Record>;
64
+ createOrUpdate(recordId: string, object: Data): Promise<Record>;
65
65
  delete(recordId: string): Promise<void>;
66
66
  query<Result extends XataRecord, Options extends QueryOptions<Record>>(query: Query<Record, Result>, options?: Options): Promise<Page<Record, typeof options extends {
67
67
  columns: SelectableColumn<Data>[];
@@ -26,10 +26,11 @@ 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");
@@ -58,20 +59,24 @@ class RestRepository extends Repository {
58
59
  }
59
60
  __classPrivateFieldSet(this, _RestRepository_fetch, fetchImpl, "f");
60
61
  }
61
- create(object) {
62
+ create(a, b) {
62
63
  return __awaiter(this, void 0, void 0, function* () {
63
- const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
64
- const record = transformObjectLinks(object);
65
- const response = yield (0, api_1.insertRecord)(Object.assign({ pathParams: {
66
- workspace: '{workspaceId}',
67
- dbBranchName: '{dbBranch}',
68
- tableName: __classPrivateFieldGet(this, _RestRepository_table, "f")
69
- }, body: record }, fetchProps));
70
- const finalObject = yield this.read(response.id);
71
- if (!finalObject) {
72
- 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');
73
79
  }
74
- return finalObject;
75
80
  });
76
81
  }
77
82
  createMany(objects) {
@@ -103,24 +108,7 @@ class RestRepository extends Repository {
103
108
  return item;
104
109
  });
105
110
  }
106
- insert(recordId, object) {
107
- return __awaiter(this, void 0, void 0, function* () {
108
- const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
109
- const record = transformObjectLinks(object);
110
- const response = yield (0, api_1.insertRecordWithID)(Object.assign({ pathParams: {
111
- workspace: '{workspaceId}',
112
- dbBranchName: '{dbBranch}',
113
- tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"),
114
- recordId
115
- }, body: record }, fetchProps));
116
- const finalObject = yield this.read(response.id);
117
- if (!finalObject) {
118
- throw new Error('The server failed to save the record');
119
- }
120
- return finalObject;
121
- });
122
- }
123
- updateOrInsert(recordId, object) {
111
+ createOrUpdate(recordId, object) {
124
112
  return __awaiter(this, void 0, void 0, function* () {
125
113
  const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
126
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));
@@ -172,6 +160,37 @@ _RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _
172
160
  }
173
161
  };
174
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
+ });
175
194
  };
176
195
  class RestRespositoryFactory {
177
196
  createRepository(client, table) {
@@ -204,7 +223,7 @@ class BaseClient {
204
223
  for (const link of tableLinks) {
205
224
  const [field, linkTable] = link;
206
225
  const value = o[field];
207
- if (value && typeof value === 'object') {
226
+ if (value && (0, lang_1.isObject)(value)) {
208
227
  const { id } = value;
209
228
  if (Object.keys(value).find((col) => col === 'id')) {
210
229
  o[field] = this.initObject(linkTable, value);
@@ -213,7 +232,7 @@ class BaseClient {
213
232
  o[field] = {
214
233
  id,
215
234
  get: () => {
216
- this.db[linkTable].read(id);
235
+ return this.db[linkTable].read(id);
217
236
  }
218
237
  };
219
238
  }
@@ -274,8 +293,8 @@ const isBranchStrategyBuilder = (strategy) => {
274
293
  };
275
294
  // TODO: We can find a better implementation for links
276
295
  const transformObjectLinks = (object) => {
277
- return Object.entries(object).reduce((acc, [key, value]) => {
278
- 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)) {
279
298
  return Object.assign(Object.assign({}, acc), { [key]: value.id });
280
299
  }
281
300
  return Object.assign(Object.assign({}, acc), { [key]: value });
@@ -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.09892c4",
3
+ "version": "0.0.0-beta.1550d19",
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": "09892c40a01ece59c802844310aac17aba7a281c"
23
+ "gitHead": "1550d192303548249462a49ebc539a321b0f0db9"
24
24
  }