@xata.io/client 0.3.0 → 0.4.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @xata.io/client
2
2
 
3
+ ## 0.4.0
4
+
5
+ ### Patch Changes
6
+
7
+ - b951331: Add support for new float column
8
+ - d470610: Add new getAll() method
9
+ - eaf92a8: Expose pagination constants (size and offset limits)
10
+ - 57fde77: Reduce subrequests for createMany
11
+ - eaf92a8: Implement schema-less client
12
+ - 97a3caa: Make createBranch from optional with empty branch
13
+
3
14
  ## 0.3.0
4
15
 
5
16
  ### Minor Changes
@@ -141,7 +141,7 @@ class BranchApi {
141
141
  getBranchDetails(workspace, database, branch) {
142
142
  return components_1.operationsByTag.branch.getBranchDetails(Object.assign({ pathParams: { workspace, dbBranchName: `${database}:${branch}` } }, this.extraProps));
143
143
  }
144
- createBranch(workspace, database, branch, from, options = {}) {
144
+ createBranch(workspace, database, branch, from = '', options = {}) {
145
145
  return components_1.operationsByTag.branch.createBranch(Object.assign({ pathParams: { workspace, dbBranchName: `${database}:${branch}` }, queryParams: { from }, body: options }, this.extraProps));
146
146
  }
147
147
  deleteBranch(workspace, database, branch) {
@@ -130,7 +130,7 @@ export declare type Table = {
130
130
  */
131
131
  export declare type Column = {
132
132
  name: string;
133
- type: 'bool' | 'int' | 'string' | 'text' | 'email' | 'multiple' | 'link' | 'object';
133
+ type: 'bool' | 'int' | 'float' | 'string' | 'text' | 'email' | 'multiple' | 'link' | 'object';
134
134
  link?: {
135
135
  table: string;
136
136
  };
@@ -1,5 +1,6 @@
1
1
  export * from './operators';
2
- export type { XataRecord } from './record';
3
- export { Repository, RestRepository, RestRespositoryFactory, BaseClient } from './repository';
4
- export type { XataClientOptions } from './repository';
2
+ export * from './pagination';
5
3
  export { Query } from './query';
4
+ export type { Identifiable, XataRecord } from './record';
5
+ export { BaseClient, Repository, RestRepository, RestRespositoryFactory } from './repository';
6
+ export type { XataClientOptions } from './repository';
@@ -14,12 +14,13 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.Query = exports.BaseClient = exports.RestRespositoryFactory = exports.RestRepository = exports.Repository = void 0;
17
+ exports.RestRespositoryFactory = exports.RestRepository = exports.Repository = exports.BaseClient = exports.Query = void 0;
18
18
  __exportStar(require("./operators"), exports);
19
+ __exportStar(require("./pagination"), exports);
20
+ var query_1 = require("./query");
21
+ Object.defineProperty(exports, "Query", { enumerable: true, get: function () { return query_1.Query; } });
19
22
  var repository_1 = require("./repository");
23
+ Object.defineProperty(exports, "BaseClient", { enumerable: true, get: function () { return repository_1.BaseClient; } });
20
24
  Object.defineProperty(exports, "Repository", { enumerable: true, get: function () { return repository_1.Repository; } });
21
25
  Object.defineProperty(exports, "RestRepository", { enumerable: true, get: function () { return repository_1.RestRepository; } });
22
26
  Object.defineProperty(exports, "RestRespositoryFactory", { enumerable: true, get: function () { return repository_1.RestRespositoryFactory; } });
23
- Object.defineProperty(exports, "BaseClient", { enumerable: true, get: function () { return repository_1.BaseClient; } });
24
- var query_1 = require("./query");
25
- Object.defineProperty(exports, "Query", { enumerable: true, get: function () { return query_1.Query; } });
@@ -19,7 +19,7 @@ export interface Paginable<T extends XataRecord, R extends XataRecord = T> {
19
19
  * A Page contains a set of results from a query plus metadata about the retrieved
20
20
  * set of values such as the cursor, required to retrieve additional records.
21
21
  */
22
- export declare class Page<T extends XataRecord, R extends XataRecord> implements Paginable<T, R> {
22
+ export declare class Page<T extends XataRecord, R extends XataRecord = T> implements Paginable<T, R> {
23
23
  #private;
24
24
  /**
25
25
  * Page metadata, required to retrieve additional records.
@@ -77,3 +77,7 @@ export declare type OffsetNavigationOptions = {
77
77
  offset?: number;
78
78
  };
79
79
  export declare type PaginationOptions = CursorNavigationOptions & OffsetNavigationOptions;
80
+ export declare const PAGINATION_MAX_SIZE = 200;
81
+ export declare const PAGINATION_DEFAULT_SIZE = 200;
82
+ export declare const PAGINATION_MAX_OFFSET = 800;
83
+ export declare const PAGINATION_DEFAULT_OFFSET = 0;
@@ -21,7 +21,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
21
21
  };
22
22
  var _Page_query;
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
- exports.Page = void 0;
24
+ exports.PAGINATION_DEFAULT_OFFSET = exports.PAGINATION_MAX_OFFSET = exports.PAGINATION_DEFAULT_SIZE = exports.PAGINATION_MAX_SIZE = exports.Page = void 0;
25
25
  /**
26
26
  * A Page contains a set of results from a query plus metadata about the retrieved
27
27
  * set of values such as the cursor, required to retrieve additional records.
@@ -88,3 +88,7 @@ class Page {
88
88
  }
89
89
  exports.Page = Page;
90
90
  _Page_query = new WeakMap();
91
+ exports.PAGINATION_MAX_SIZE = 200;
92
+ exports.PAGINATION_DEFAULT_SIZE = 200;
93
+ exports.PAGINATION_MAX_OFFSET = 800;
94
+ exports.PAGINATION_DEFAULT_OFFSET = 0;
@@ -1,8 +1,8 @@
1
- import { XataRecord, Repository } from '..';
2
- import { FilterExpression, SortExpression, PageConfig, ColumnsFilter } from '../api/schemas';
1
+ import { Repository, XataRecord } from '..';
2
+ import { ColumnsFilter, FilterExpression, PageConfig, SortExpression } from '../api/schemas';
3
3
  import { DeepConstraint, FilterConstraints, SortDirection, SortFilter } from './filters';
4
- import { PaginationOptions, Page, Paginable, PaginationQueryMeta } from './pagination';
5
- import { Selectable, SelectableColumn, Select } from './selection';
4
+ import { Page, Paginable, PaginationOptions, PaginationQueryMeta } from './pagination';
5
+ import { Select, Selectable, SelectableColumn } from './selection';
6
6
  export declare type QueryOptions<T extends XataRecord> = {
7
7
  page?: PaginationOptions;
8
8
  columns?: Extract<keyof Selectable<T>, string>[];
@@ -81,27 +81,28 @@ export declare class Query<T extends XataRecord, R extends XataRecord = T> imple
81
81
  * @returns A new Query object.
82
82
  */
83
83
  select<K extends SelectableColumn<T>>(columns: K[]): Query<T, Select<T, K>>;
84
- getPaginated<Options extends QueryOptions<T>>(options?: Options): Promise<Page<T, typeof options extends {
85
- columns: SelectableColumn<T>[];
86
- } ? Select<T, typeof options['columns'][number]> : R>>;
84
+ getPaginated<Options extends QueryOptions<T>>(options?: Options): Promise<Page<T, GetWithColumnOptions<T, R, typeof options>>>;
87
85
  [Symbol.asyncIterator](): AsyncIterableIterator<R>;
88
- getIterator(chunk: number, options?: Omit<QueryOptions<T>, 'page'>): AsyncGenerator<R[]>;
86
+ getIterator<Options extends QueryOptions<T>>(chunk: number, options?: Omit<Options, 'page'>): AsyncGenerator<GetWithColumnOptions<T, R, typeof options>[]>;
89
87
  /**
90
88
  * Performs the query in the database and returns a set of results.
91
89
  * @param options Additional options to be used when performing the query.
92
90
  * @returns An array of records from the database.
93
91
  */
94
- getMany<Options extends QueryOptions<T>>(options?: Options): Promise<(typeof options extends {
95
- columns: SelectableColumn<T>[];
96
- } ? Select<T, typeof options['columns'][number]> : R)[]>;
92
+ getMany<Options extends QueryOptions<T>>(options?: Options): Promise<GetWithColumnOptions<T, R, typeof options>[]>;
93
+ /**
94
+ * Performs the query in the database and returns all the results.
95
+ * Warning: If there are a large number of results, this method can have performance implications.
96
+ * @param options Additional options to be used when performing the query.
97
+ * @returns An array of records from the database.
98
+ */
99
+ getAll<Options extends QueryOptions<T>>(chunk?: number, options?: Omit<Options, 'page'>): Promise<GetWithColumnOptions<T, R, typeof options>[]>;
97
100
  /**
98
101
  * Performs the query in the database and returns the first result.
99
102
  * @param options Additional options to be used when performing the query.
100
103
  * @returns The first record that matches the query, or null if no record matched the query.
101
104
  */
102
- getOne<Options extends Omit<QueryOptions<T>, 'page'>>(options?: Options): Promise<(typeof options extends {
103
- columns: SelectableColumn<T>[];
104
- } ? Select<T, typeof options['columns'][number]> : R) | null>;
105
+ getOne<Options extends Omit<QueryOptions<T>, 'page'>>(options?: Options): Promise<GetWithColumnOptions<T, R, typeof options> | null>;
105
106
  /**async deleteAll(): Promise<number> {
106
107
  // TODO: Return number of affected rows
107
108
  return 0;
@@ -112,3 +113,16 @@ export declare class Query<T extends XataRecord, R extends XataRecord = T> imple
112
113
  lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
113
114
  hasNextPage(): boolean;
114
115
  }
116
+ /**
117
+ * Helper type to read options and compute the correct type for the result values
118
+ * T: Original type
119
+ * R: Default destination type
120
+ * Options: QueryOptions
121
+ *
122
+ * If the columns are overriden in the options, the result type is the pick of the original type and the columns
123
+ * If the columns are not overriden, the result type is the default destination type
124
+ */
125
+ declare type GetWithColumnOptions<T, R, Options> = Options extends {
126
+ columns: SelectableColumn<T>[];
127
+ } ? Select<T, Options['columns'][number]> : R;
128
+ export {};
@@ -42,6 +42,7 @@ var _Query_table, _Query_repository, _Query_data;
42
42
  Object.defineProperty(exports, "__esModule", { value: true });
43
43
  exports.Query = void 0;
44
44
  const lang_1 = require("../util/lang");
45
+ const pagination_1 = require("./pagination");
45
46
  /**
46
47
  * Query objects contain the information of all filters, sorting, etc. to be included in the database query.
47
48
  *
@@ -193,6 +194,32 @@ class Query {
193
194
  return records;
194
195
  });
195
196
  }
197
+ /**
198
+ * Performs the query in the database and returns all the results.
199
+ * Warning: If there are a large number of results, this method can have performance implications.
200
+ * @param options Additional options to be used when performing the query.
201
+ * @returns An array of records from the database.
202
+ */
203
+ getAll(chunk = pagination_1.PAGINATION_MAX_SIZE, options = {}) {
204
+ var e_2, _a;
205
+ return __awaiter(this, void 0, void 0, function* () {
206
+ const results = [];
207
+ try {
208
+ for (var _b = __asyncValues(this.getIterator(chunk, options)), _c; _c = yield _b.next(), !_c.done;) {
209
+ const page = _c.value;
210
+ results.push(...page);
211
+ }
212
+ }
213
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
214
+ finally {
215
+ try {
216
+ if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
217
+ }
218
+ finally { if (e_2) throw e_2.error; }
219
+ }
220
+ return results;
221
+ });
222
+ }
196
223
  /**
197
224
  * Performs the query in the database and returns the first result.
198
225
  * @param options Additional options to be used when performing the query.
@@ -1,12 +1,17 @@
1
1
  import { Selectable } from './selection';
2
2
  /**
3
- * Represents a persisted record from the database.
3
+ * Represents an identifiable record from the database.
4
4
  */
5
- export interface XataRecord {
5
+ export interface Identifiable {
6
6
  /**
7
7
  * Unique id of this record.
8
8
  */
9
9
  id: string;
10
+ }
11
+ /**
12
+ * Represents a persisted record from the database.
13
+ */
14
+ export interface XataRecord extends Identifiable {
10
15
  /**
11
16
  * Metadata of this record.
12
17
  */
@@ -2,7 +2,7 @@ import { FetchImpl } from '../api/fetcher';
2
2
  import { Page } from './pagination';
3
3
  import { Query, QueryOptions } from './query';
4
4
  import { XataRecord } from './record';
5
- import { Selectable, SelectableColumn, Select } from './selection';
5
+ import { Select, Selectable, 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.
@@ -98,7 +98,7 @@ export declare type XataClientOptions = {
98
98
  apiKey: string;
99
99
  repositoryFactory?: RepositoryFactory;
100
100
  };
101
- export declare class BaseClient<D extends Record<string, Repository<any>>> {
101
+ export declare class BaseClient<D extends Record<string, Repository<any>> = Record<string, Repository<any>>> {
102
102
  #private;
103
103
  options: XataClientOptions;
104
104
  db: D;
@@ -76,10 +76,9 @@ class RestRepository extends Repository {
76
76
  const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
77
77
  const records = objects.map((object) => transformObjectLinks(object));
78
78
  const response = yield (0, api_1.bulkInsertTableRecords)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f") }, body: { records } }, fetchProps));
79
- // TODO: Use filer.$any() to get all the records
80
- const finalObjects = yield Promise.all(response.recordIDs.map((id) => this.read(id)));
81
- if (finalObjects.some((object) => !object)) {
82
- throw new Error('The server failed to save the record');
79
+ const finalObjects = yield this.any(...response.recordIDs.map((id) => this.filter('id', id))).getAll();
80
+ if (finalObjects.length !== objects.length) {
81
+ throw new Error('The server failed to save some records');
83
82
  }
84
83
  return finalObjects;
85
84
  });
@@ -186,6 +185,14 @@ class BaseClient {
186
185
  }
187
186
  this.options = options;
188
187
  __classPrivateFieldSet(this, _BaseClient_links, links, "f");
188
+ const factory = options.repositoryFactory || new RestRespositoryFactory();
189
+ this.db = new Proxy({}, {
190
+ get: (_target, prop) => {
191
+ if (typeof prop !== 'string')
192
+ throw new Error('Invalid table name');
193
+ return factory.createRepository(this, prop);
194
+ }
195
+ });
189
196
  }
190
197
  initObject(table, object) {
191
198
  const o = {};
@@ -1,11 +1,12 @@
1
1
  import { XataRecord } from '..';
2
2
  import { StringKeys, UnionToIntersection, Values } from '../util/types';
3
3
  import { Query } from './query';
4
+ import { Identifiable } from './record';
4
5
  declare type Queries<T> = {
5
6
  [key in keyof T as T[key] extends Query<any> ? key : never]: T[key];
6
7
  };
7
8
  declare type InternalProperties = keyof XataRecord;
8
- export declare type Selectable<T extends XataRecord> = Omit<T, InternalProperties>;
9
+ export declare type Selectable<T extends XataRecord> = Omit<T, InternalProperties> & Identifiable;
9
10
  export declare type SelectableColumn<O> = '*' | (O extends Array<unknown> ? never : O extends Record<string, any> ? '*' | Values<{
10
11
  [K in StringKeys<O>]: O[K] extends Record<string, any> ? `${K}.${SelectableColumn<O[K]>}` : K;
11
12
  }> : '');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xata.io/client",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
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": "68cefbad5825d10c1f15efd39766280d3b863a87"
23
+ "gitHead": "d29cf06c96a17f69ca80c57b430059abc90fcad8"
24
24
  }