@xata.io/client 0.0.0-beta.4e4e7fc → 0.0.0-beta.53c906e

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.
Files changed (48) hide show
  1. package/.eslintrc.cjs +13 -0
  2. package/CHANGELOG.md +71 -0
  3. package/dist/api/client.d.ts +4 -2
  4. package/dist/api/client.js +40 -18
  5. package/dist/api/components.d.ts +58 -37
  6. package/dist/api/components.js +48 -38
  7. package/dist/api/fetcher.d.ts +15 -0
  8. package/dist/api/fetcher.js +23 -22
  9. package/dist/api/parameters.d.ts +1 -0
  10. package/dist/api/providers.js +3 -2
  11. package/dist/api/responses.d.ts +6 -0
  12. package/dist/api/schemas.d.ts +1 -1
  13. package/dist/index.d.ts +1 -0
  14. package/dist/index.js +1 -0
  15. package/dist/schema/filters.d.ts +93 -17
  16. package/dist/schema/filters.js +0 -22
  17. package/dist/schema/filters.spec.d.ts +1 -0
  18. package/dist/schema/filters.spec.js +175 -0
  19. package/dist/schema/index.d.ts +5 -3
  20. package/dist/schema/index.js +8 -4
  21. package/dist/schema/operators.d.ts +74 -21
  22. package/dist/schema/operators.js +59 -6
  23. package/dist/schema/pagination.d.ts +56 -14
  24. package/dist/schema/pagination.js +37 -2
  25. package/dist/schema/query.d.ts +110 -37
  26. package/dist/schema/query.js +87 -35
  27. package/dist/schema/record.d.ts +60 -4
  28. package/dist/schema/record.js +11 -0
  29. package/dist/schema/repository.d.ts +140 -25
  30. package/dist/schema/repository.js +232 -100
  31. package/dist/schema/selection.d.ts +24 -13
  32. package/dist/schema/selection.spec.d.ts +1 -0
  33. package/dist/schema/selection.spec.js +203 -0
  34. package/dist/schema/sorting.d.ts +17 -0
  35. package/dist/schema/sorting.js +28 -0
  36. package/dist/schema/sorting.spec.d.ts +1 -0
  37. package/dist/schema/sorting.spec.js +9 -0
  38. package/dist/util/config.d.ts +11 -0
  39. package/dist/util/config.js +121 -0
  40. package/dist/util/environment.d.ts +5 -0
  41. package/dist/util/environment.js +68 -0
  42. package/dist/util/fetch.d.ts +2 -0
  43. package/dist/util/fetch.js +13 -0
  44. package/dist/util/lang.d.ts +3 -0
  45. package/dist/util/lang.js +13 -1
  46. package/dist/util/types.d.ts +22 -1
  47. package/package.json +5 -2
  48. package/tsconfig.json +21 -0
@@ -1,10 +1,66 @@
1
- import { Selectable } from './selection';
2
- export interface XataRecord {
1
+ import { SelectedPick } from './selection';
2
+ /**
3
+ * Represents an identifiable record from the database.
4
+ */
5
+ export interface Identifiable {
6
+ /**
7
+ * Unique id of this record.
8
+ */
3
9
  id: string;
10
+ }
11
+ export interface BaseData {
12
+ [key: string]: any;
13
+ }
14
+ /**
15
+ * Represents a persisted record from the database.
16
+ */
17
+ export interface XataRecord extends Identifiable {
18
+ /**
19
+ * Metadata of this record.
20
+ */
4
21
  xata: {
22
+ /**
23
+ * Number that is increased every time the record is updated.
24
+ */
5
25
  version: number;
6
26
  };
7
- read(): Promise<this>;
8
- update(data: Partial<Selectable<this>>): Promise<this>;
27
+ /**
28
+ * Retrieves a refreshed copy of the current record from the database.
29
+ */
30
+ read(): Promise<SelectedPick<this, ['*']> | null>;
31
+ /**
32
+ * Performs a partial update of the current record. On success a new object is
33
+ * returned and the current object is not mutated.
34
+ * @param data The columns and their values that have to be updated.
35
+ * @returns A new record containing the latest values for all the columns of the current record.
36
+ */
37
+ update(data: Partial<EditableData<Omit<this, keyof XataRecord>>>): Promise<SelectedPick<this, ['*']>>;
38
+ /**
39
+ * Performs a deletion of the current record in the database.
40
+ *
41
+ * @throws If the record was already deleted or if an error happened while performing the deletion.
42
+ */
9
43
  delete(): Promise<void>;
10
44
  }
45
+ export declare type Link<Record extends XataRecord> = Omit<XataRecord, 'read' | 'update'> & {
46
+ /**
47
+ * Retrieves a refreshed copy of the current record from the database.
48
+ */
49
+ read(): Promise<SelectedPick<Record, ['*']> | null>;
50
+ /**
51
+ * Performs a partial update of the current record. On success a new object is
52
+ * returned and the current object is not mutated.
53
+ * @param data The columns and their values that have to be updated.
54
+ * @returns A new record containing the latest values for all the columns of the current record.
55
+ */
56
+ update(data: Partial<EditableData<Omit<Record, keyof XataRecord>>>): Promise<SelectedPick<Record, ['*']>>;
57
+ };
58
+ export declare function isIdentifiable(x: any): x is Identifiable & Record<string, unknown>;
59
+ export declare function isXataRecord(x: any): x is XataRecord & Record<string, unknown>;
60
+ export declare type EditableData<O extends BaseData> = {
61
+ [K in keyof O]: O[K] extends XataRecord ? {
62
+ id: string;
63
+ } : NonNullable<O[K]> extends XataRecord ? {
64
+ id: string;
65
+ } | null | undefined : O[K];
66
+ };
@@ -1,2 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isXataRecord = exports.isIdentifiable = void 0;
4
+ const lang_1 = require("../util/lang");
5
+ function isIdentifiable(x) {
6
+ return (0, lang_1.isObject)(x) && (0, lang_1.isString)(x === null || x === void 0 ? void 0 : x.id);
7
+ }
8
+ exports.isIdentifiable = isIdentifiable;
9
+ function isXataRecord(x) {
10
+ var _a;
11
+ return (isIdentifiable(x) && typeof (x === null || x === void 0 ? void 0 : x.xata) === 'object' && typeof ((_a = x === null || x === void 0 ? void 0 : x.xata) === null || _a === void 0 ? void 0 : _a.version) === 'number');
12
+ }
13
+ exports.isXataRecord = isXataRecord;
@@ -1,51 +1,166 @@
1
1
  import { FetchImpl } from '../api/fetcher';
2
+ import { GetArrayInnerType } from '../util/types';
2
3
  import { Page } from './pagination';
3
- import { Query, QueryOptions } from './query';
4
- import { XataRecord } from './record';
5
- import { Selectable, SelectableColumn, Select } from './selection';
4
+ import { Query } from './query';
5
+ import { BaseData, EditableData, Identifiable, XataRecord } from './record';
6
+ import { SelectedPick } from './selection';
6
7
  export declare type Links = Record<string, Array<string[]>>;
7
- export declare abstract class Repository<T extends XataRecord> extends Query<T> {
8
- abstract create(object: Selectable<T>): Promise<T>;
9
- abstract createMany(objects: Selectable<T>[]): Promise<T[]>;
10
- abstract read(id: string): Promise<T | null>;
11
- abstract update(id: string, object: Partial<Selectable<T>>): Promise<T>;
12
- abstract upsert(id: string, object: Selectable<T>): Promise<T>;
13
- abstract delete(id: string): void;
14
- abstract query<R extends XataRecord, Options extends QueryOptions<T>>(query: Query<T, R>, options: Options): Promise<Page<T, typeof options['columns'] extends SelectableColumn<T>[] ? Select<T, typeof options['columns'][number]> : R>>;
8
+ /**
9
+ * Common interface for performing operations on a table.
10
+ */
11
+ export declare abstract class Repository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record, SelectedPick<Record, ['*']>> {
12
+ abstract create(object: EditableData<Data> & Partial<Identifiable>): Promise<SelectedPick<Record, ['*']>>;
13
+ /**
14
+ * Creates a single record in the table with a unique id.
15
+ * @param id The unique id.
16
+ * @param object Object containing the column names with their values to be stored in the table.
17
+ * @returns The full persisted record.
18
+ */
19
+ abstract create(id: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
20
+ /**
21
+ * Creates multiple records in the table.
22
+ * @param objects Array of objects with the column names and the values to be stored in the table.
23
+ * @returns Array of the persisted records.
24
+ */
25
+ abstract create(objects: Array<EditableData<Data> & Partial<Identifiable>>): Promise<SelectedPick<Record, ['*']>[]>;
26
+ /**
27
+ * Queries a single record from the table given its unique id.
28
+ * @param id The unique id.
29
+ * @returns The persisted record for the given id or null if the record could not be found.
30
+ */
31
+ abstract read(id: string): Promise<SelectedPick<Record, ['*']> | null>;
32
+ /**
33
+ * Partially update a single record.
34
+ * @param object An object with its id and the columns to be updated.
35
+ * @returns The full persisted record.
36
+ */
37
+ abstract update(object: Partial<EditableData<Data>> & Identifiable): Promise<SelectedPick<Record, ['*']>>;
38
+ /**
39
+ * Partially update a single record given its unique id.
40
+ * @param id The unique id.
41
+ * @param object The column names and their values that have to be updated.
42
+ * @returns The full persisted record.
43
+ */
44
+ abstract update(id: string, object: Partial<EditableData<Data>>): Promise<SelectedPick<Record, ['*']>>;
45
+ /**
46
+ * Partially updates multiple records.
47
+ * @param objects An array of objects with their ids and columns to be updated.
48
+ * @returns Array of the persisted records.
49
+ */
50
+ abstract update(objects: Array<Partial<EditableData<Data>> & Identifiable>): Promise<SelectedPick<Record, ['*']>[]>;
51
+ /**
52
+ * Creates or updates a single record. If a record exists with the given id,
53
+ * it will be update, otherwise a new record will be created.
54
+ * @param object Object containing the column names with their values to be persisted in the table.
55
+ * @returns The full persisted record.
56
+ */
57
+ abstract createOrUpdate(object: EditableData<Data> & Identifiable): Promise<SelectedPick<Record, ['*']>>;
58
+ /**
59
+ * Creates or updates a single record. If a record exists with the given id,
60
+ * it will be update, otherwise a new record will be created.
61
+ * @param id A unique id.
62
+ * @param object The column names and the values to be persisted.
63
+ * @returns The full persisted record.
64
+ */
65
+ abstract createOrUpdate(id: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
66
+ /**
67
+ * Creates or updates a single record. If a record exists with the given id,
68
+ * it will be update, otherwise a new record will be created.
69
+ * @param objects Array of objects with the column names and the values to be stored in the table.
70
+ * @returns Array of the persisted records.
71
+ */
72
+ abstract createOrUpdate(objects: Array<EditableData<Data> & Identifiable>): Promise<SelectedPick<Record, ['*']>[]>;
73
+ /**
74
+ * Deletes a record given its unique id.
75
+ * @param id The unique id.
76
+ * @throws If the record could not be found or there was an error while performing the deletion.
77
+ */
78
+ abstract delete(id: string): Promise<void>;
79
+ /**
80
+ * Deletes a record given its unique id.
81
+ * @param id An object with a unique id.
82
+ * @throws If the record could not be found or there was an error while performing the deletion.
83
+ */
84
+ abstract delete(id: Identifiable): Promise<void>;
85
+ /**
86
+ * Deletes a record given a list of unique ids.
87
+ * @param ids The array of unique ids.
88
+ * @throws If the record could not be found or there was an error while performing the deletion.
89
+ */
90
+ abstract delete(ids: string[]): Promise<void>;
91
+ /**
92
+ * Deletes a record given a list of unique ids.
93
+ * @param ids An array of objects with unique ids.
94
+ * @throws If the record could not be found or there was an error while performing the deletion.
95
+ */
96
+ abstract delete(ids: Identifiable[]): Promise<void>;
97
+ /**
98
+ * Search for records in the table.
99
+ * @param query The query to search for.
100
+ * @param options The options to search with (like: fuzziness)
101
+ * @returns The found records.
102
+ */
103
+ abstract search(query: string, options?: {
104
+ fuzziness?: number;
105
+ }): Promise<SelectedPick<Record, ['*']>[]>;
106
+ abstract query<Result extends XataRecord>(query: Query<Record, Result>): Promise<Page<Record, Result>>;
15
107
  }
16
- export declare class RestRepository<T extends XataRecord> extends Repository<T> {
108
+ export declare class RestRepository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record, SelectedPick<Record, ['*']>> {
17
109
  #private;
18
110
  constructor(client: BaseClient<any>, table: string);
19
- create(object: Selectable<T>): Promise<T>;
20
- createMany(objects: T[]): Promise<T[]>;
21
- read(recordId: string): Promise<T | null>;
22
- update(recordId: string, object: Partial<Selectable<T>>): Promise<T>;
23
- upsert(recordId: string, object: Selectable<T>): Promise<T>;
24
- delete(recordId: string): Promise<void>;
25
- query<R extends XataRecord, Options extends QueryOptions<T>>(query: Query<T, R>, options: Options): Promise<Page<T, typeof options['columns'] extends SelectableColumn<T>[] ? Select<T, typeof options['columns'][number]> : R>>;
111
+ create(object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
112
+ create(recordId: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
113
+ create(objects: EditableData<Data>[]): Promise<SelectedPick<Record, ['*']>[]>;
114
+ read(recordId: string): Promise<SelectedPick<Record, ['*']> | null>;
115
+ update(object: Partial<EditableData<Data>> & Identifiable): Promise<SelectedPick<Record, ['*']>>;
116
+ update(recordId: string, object: Partial<EditableData<Data>>): Promise<SelectedPick<Record, ['*']>>;
117
+ update(objects: Array<Partial<EditableData<Data>> & Identifiable>): Promise<SelectedPick<Record, ['*']>[]>;
118
+ createOrUpdate(object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
119
+ createOrUpdate(recordId: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
120
+ createOrUpdate(objects: EditableData<Data>[]): Promise<SelectedPick<Record, ['*']>[]>;
121
+ delete(recordId: string | Identifiable | Array<string | Identifiable>): Promise<void>;
122
+ search(query: string, options?: {
123
+ fuzziness?: number;
124
+ }): Promise<SelectedPick<Record, ['*']>[]>;
125
+ query<Result extends XataRecord>(query: Query<Record, Result>): Promise<Page<Record, Result>>;
26
126
  }
27
127
  interface RepositoryFactory {
28
- createRepository<T extends XataRecord>(client: BaseClient<any>, table: string): Repository<T>;
128
+ createRepository<Data extends BaseData>(client: BaseClient<any>, table: string): Repository<Data & XataRecord>;
29
129
  }
30
130
  export declare class RestRespositoryFactory implements RepositoryFactory {
31
- createRepository<T extends XataRecord>(client: BaseClient<any>, table: string): Repository<T>;
131
+ createRepository<Data extends BaseData>(client: BaseClient<any>, table: string): Repository<Data & XataRecord>;
32
132
  }
33
133
  declare type BranchStrategyValue = string | undefined | null;
34
134
  declare type BranchStrategyBuilder = () => BranchStrategyValue | Promise<BranchStrategyValue>;
35
135
  declare type BranchStrategy = BranchStrategyValue | BranchStrategyBuilder;
36
136
  declare type BranchStrategyOption = NonNullable<BranchStrategy | BranchStrategy[]>;
37
137
  export declare type XataClientOptions = {
138
+ /**
139
+ * Fetch implementation. This option is only required if the runtime does not include a fetch implementation
140
+ * available in the global scope. If you are running your code on Deno or Cloudflare workers for example,
141
+ * you won't need to provide a specific fetch implementation. But for most versions of Node.js you'll need
142
+ * to provide one. Such as cross-fetch, node-fetch or isomorphic-fetch.
143
+ */
38
144
  fetch?: FetchImpl;
39
145
  databaseURL?: string;
40
- branch: BranchStrategyOption;
41
- apiKey: string;
146
+ branch?: BranchStrategyOption;
147
+ /**
148
+ * API key to be used. You can create one in your account settings at https://app.xata.io/settings.
149
+ */
150
+ apiKey?: string;
42
151
  repositoryFactory?: RepositoryFactory;
43
152
  };
44
- export declare class BaseClient<D extends Record<string, Repository<any>>> {
153
+ export declare class BaseClient<D extends Record<string, Repository<any>> = Record<string, Repository<any>>> {
45
154
  #private;
46
155
  options: XataClientOptions;
47
156
  db: D;
48
- constructor(options: XataClientOptions, links: Links);
157
+ constructor(options?: XataClientOptions, links?: Links);
158
+ search<Tables extends keyof 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
+ }>;
49
164
  initObject<T>(table: string, object: object): T;
50
165
  getBranch(): Promise<string>;
51
166
  }