@xata.io/client 0.0.0-alpha.fed4b2b → 0.0.0-beta.036d273

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 (44) hide show
  1. package/.eslintrc.cjs +13 -0
  2. package/CHANGELOG.md +44 -0
  3. package/dist/api/client.d.ts +1 -1
  4. package/dist/api/client.js +32 -17
  5. package/dist/api/components.d.ts +7 -6
  6. package/dist/api/components.js +7 -6
  7. package/dist/api/fetcher.d.ts +15 -0
  8. package/dist/api/fetcher.js +23 -22
  9. package/dist/api/providers.js +3 -2
  10. package/dist/api/responses.d.ts +6 -0
  11. package/dist/index.d.ts +1 -0
  12. package/dist/index.js +1 -0
  13. package/dist/schema/filters.d.ts +93 -18
  14. package/dist/schema/filters.js +0 -22
  15. package/dist/schema/filters.spec.d.ts +1 -0
  16. package/dist/schema/filters.spec.js +175 -0
  17. package/dist/schema/index.d.ts +2 -1
  18. package/dist/schema/index.js +4 -1
  19. package/dist/schema/operators.d.ts +26 -24
  20. package/dist/schema/operators.js +13 -11
  21. package/dist/schema/pagination.js +0 -1
  22. package/dist/schema/query.d.ts +23 -19
  23. package/dist/schema/query.js +13 -19
  24. package/dist/schema/record.d.ts +23 -2
  25. package/dist/schema/record.js +2 -1
  26. package/dist/schema/repository.d.ts +100 -42
  27. package/dist/schema/repository.js +261 -154
  28. package/dist/schema/selection.d.ts +14 -17
  29. package/dist/schema/selection.spec.d.ts +1 -0
  30. package/dist/schema/selection.spec.js +203 -0
  31. package/dist/schema/sorting.d.ts +17 -0
  32. package/dist/schema/sorting.js +28 -0
  33. package/dist/schema/sorting.spec.d.ts +1 -0
  34. package/dist/schema/sorting.spec.js +9 -0
  35. package/dist/util/config.d.ts +11 -0
  36. package/dist/util/config.js +121 -0
  37. package/dist/util/environment.d.ts +5 -0
  38. package/dist/util/environment.js +68 -0
  39. package/dist/util/fetch.d.ts +2 -0
  40. package/dist/util/fetch.js +13 -0
  41. package/dist/util/lang.d.ts +3 -0
  42. package/dist/util/lang.js +13 -1
  43. package/dist/util/types.d.ts +11 -2
  44. package/package.json +5 -2
@@ -1,73 +1,135 @@
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
- import { BaseData, XataRecord } from './record';
5
- import { SelectedRecordPick } from './selection';
6
- export declare type Links = Record<string, Array<string[]>>;
5
+ import { BaseData, EditableData, Identifiable, XataRecord } from './record';
6
+ import { SelectedPick } from './selection';
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
  */
10
- export declare abstract class Repository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record, SelectedRecordPick<Record, ['*']>> {
11
- abstract create(object: BuildData<Data>): Promise<SelectedRecordPick<Record, ['*']>>;
12
+ export declare abstract class Repository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record, SelectedPick<Record, ['*']>> {
13
+ abstract create(object: EditableData<Data> & Partial<Identifiable>): Promise<SelectedPick<Record, ['*']>>;
14
+ /**
15
+ * Creates a single record in the table with a unique id.
16
+ * @param id The unique id.
17
+ * @param object Object containing the column names with their values to be stored in the table.
18
+ * @returns The full persisted record.
19
+ */
20
+ abstract create(id: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
12
21
  /**
13
22
  * Creates multiple records in the table.
14
23
  * @param objects Array of objects with the column names and the values to be stored in the table.
15
24
  * @returns Array of the persisted records.
16
25
  */
17
- abstract createMany(objects: BuildData<Data>[]): Promise<SelectedRecordPick<Record, ['*']>[]>;
26
+ abstract create(objects: Array<EditableData<Data> & Partial<Identifiable>>): Promise<SelectedPick<Record, ['*']>[]>;
18
27
  /**
19
28
  * Queries a single record from the table given its unique id.
20
29
  * @param id The unique id.
21
30
  * @returns The persisted record for the given id or null if the record could not be found.
22
31
  */
23
- abstract read(id: string): Promise<SelectedRecordPick<Record, ['*']> | null>;
32
+ abstract read(id: string): Promise<SelectedPick<Record, ['*']> | null>;
24
33
  /**
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.
34
+ * Partially update a single record.
35
+ * @param object An object with its id and the columns to be updated.
28
36
  * @returns The full persisted record.
29
37
  */
30
- abstract insert(id: string, object: BuildData<Data>): Promise<SelectedRecordPick<Record, ['*']>>;
38
+ abstract update(object: Partial<EditableData<Data>> & Identifiable): Promise<SelectedPick<Record, ['*']>>;
31
39
  /**
32
40
  * Partially update a single record given its unique id.
33
41
  * @param id The unique id.
34
- * @param object The column names and their values that have to be updatd.
42
+ * @param object The column names and their values that have to be updated.
35
43
  * @returns The full persisted record.
36
44
  */
37
- abstract update(id: string, object: Partial<BuildData<Data>>): Promise<SelectedRecordPick<Record, ['*']>>;
45
+ abstract update(id: string, object: Partial<EditableData<Data>>): Promise<SelectedPick<Record, ['*']>>;
38
46
  /**
39
- * Updates or inserts a single record. If a record exists with the given id,
47
+ * Partially updates multiple records.
48
+ * @param objects An array of objects with their ids and columns to be updated.
49
+ * @returns Array of the persisted records.
50
+ */
51
+ abstract update(objects: Array<Partial<EditableData<Data>> & Identifiable>): Promise<SelectedPick<Record, ['*']>[]>;
52
+ /**
53
+ * Creates or updates a single record. If a record exists with the given id,
54
+ * it will be update, otherwise a new record will be created.
55
+ * @param object Object containing the column names with their values to be persisted in the table.
56
+ * @returns The full persisted record.
57
+ */
58
+ abstract createOrUpdate(object: EditableData<Data> & Identifiable): Promise<SelectedPick<Record, ['*']>>;
59
+ /**
60
+ * Creates or updates a single record. If a record exists with the given id,
40
61
  * it will be update, otherwise a new record will be created.
41
62
  * @param id A unique id.
42
63
  * @param object The column names and the values to be persisted.
43
64
  * @returns The full persisted record.
44
65
  */
45
- abstract updateOrInsert(id: string, object: BuildData<Data>): Promise<SelectedRecordPick<Record, ['*']>>;
66
+ abstract createOrUpdate(id: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
67
+ /**
68
+ * Creates or updates a single record. If a record exists with the given id,
69
+ * it will be update, otherwise a new record will be created.
70
+ * @param objects Array of objects with the column names and the values to be stored in the table.
71
+ * @returns Array of the persisted records.
72
+ */
73
+ abstract createOrUpdate(objects: Array<EditableData<Data> & Identifiable>): Promise<SelectedPick<Record, ['*']>[]>;
46
74
  /**
47
75
  * Deletes a record given its unique id.
48
76
  * @param id The unique id.
49
77
  * @throws If the record could not be found or there was an error while performing the deletion.
50
78
  */
51
- abstract delete(id: string): void;
52
- abstract query(query: Query<Record, SelectedRecordPick<Record, ['*']>>): Promise<Page<Record, SelectedRecordPick<Record, ['*']>>>;
79
+ abstract delete(id: string): Promise<void>;
80
+ /**
81
+ * Deletes a record given its unique id.
82
+ * @param id An object with a unique id.
83
+ * @throws If the record could not be found or there was an error while performing the deletion.
84
+ */
85
+ abstract delete(id: Identifiable): Promise<void>;
86
+ /**
87
+ * Deletes a record given a list of unique ids.
88
+ * @param ids The array of unique ids.
89
+ * @throws If the record could not be found or there was an error while performing the deletion.
90
+ */
91
+ abstract delete(ids: string[]): Promise<void>;
92
+ /**
93
+ * Deletes a record given a list of unique ids.
94
+ * @param ids An array of objects with unique ids.
95
+ * @throws If the record could not be found or there was an error while performing the deletion.
96
+ */
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, ['*']>[]>;
107
+ abstract query<Result extends XataRecord>(query: Query<Record, Result>): Promise<Page<Record, Result>>;
53
108
  }
54
- export declare class RestRepository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record, SelectedRecordPick<Record, ['*']>> {
109
+ export declare class RestRepository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record, SelectedPick<Record, ['*']>> {
55
110
  #private;
56
- constructor(client: BaseClient<any>, table: string);
57
- create(object: BuildData<Data>): Promise<SelectedRecordPick<Record, ['*']>>;
58
- createMany(objects: BuildData<Data>[]): Promise<SelectedRecordPick<Record, ['*']>[]>;
59
- read(recordId: string): Promise<SelectedRecordPick<Record, ['*']> | null>;
60
- update(recordId: string, object: Partial<BuildData<Data>>): Promise<SelectedRecordPick<Record, ['*']>>;
61
- insert(recordId: string, object: BuildData<Data>): Promise<SelectedRecordPick<Record, ['*']>>;
62
- updateOrInsert(recordId: string, object: BuildData<Data>): Promise<SelectedRecordPick<Record, ['*']>>;
63
- delete(recordId: string): Promise<void>;
64
- query(query: Query<Record, SelectedRecordPick<Record, ['*']>>): Promise<Page<Record, SelectedRecordPick<Record, ['*']>>>;
111
+ constructor(client: BaseClient<any>, table: string, links?: LinkDictionary);
112
+ create(object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
113
+ create(recordId: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
114
+ create(objects: EditableData<Data>[]): Promise<SelectedPick<Record, ['*']>[]>;
115
+ read(recordId: string): Promise<SelectedPick<Record, ['*']> | null>;
116
+ update(object: Partial<EditableData<Data>> & Identifiable): Promise<SelectedPick<Record, ['*']>>;
117
+ update(recordId: string, object: Partial<EditableData<Data>>): Promise<SelectedPick<Record, ['*']>>;
118
+ update(objects: Array<Partial<EditableData<Data>> & Identifiable>): Promise<SelectedPick<Record, ['*']>[]>;
119
+ createOrUpdate(object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
120
+ createOrUpdate(recordId: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
121
+ createOrUpdate(objects: EditableData<Data>[]): Promise<SelectedPick<Record, ['*']>[]>;
122
+ delete(recordId: string | Identifiable | Array<string | Identifiable>): Promise<void>;
123
+ search(query: string, options?: {
124
+ fuzziness?: number;
125
+ }): Promise<SelectedPick<Record, ['*']>[]>;
126
+ query<Result extends XataRecord>(query: Query<Record, Result>): Promise<Page<Record, Result>>;
65
127
  }
66
128
  interface RepositoryFactory {
67
- 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>;
68
130
  }
69
131
  export declare class RestRespositoryFactory implements RepositoryFactory {
70
- 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>;
71
133
  }
72
134
  declare type BranchStrategyValue = string | undefined | null;
73
135
  declare type BranchStrategyBuilder = () => BranchStrategyValue | Promise<BranchStrategyValue>;
@@ -82,26 +144,22 @@ export declare type XataClientOptions = {
82
144
  */
83
145
  fetch?: FetchImpl;
84
146
  databaseURL?: string;
85
- branch: BranchStrategyOption;
147
+ branch?: BranchStrategyOption;
86
148
  /**
87
149
  * API key to be used. You can create one in your account settings at https://app.xata.io/settings.
88
150
  */
89
- apiKey: string;
151
+ apiKey?: string;
90
152
  repositoryFactory?: RepositoryFactory;
91
153
  };
92
154
  export declare class BaseClient<D extends Record<string, Repository<any>> = Record<string, Repository<any>>> {
93
- #private;
94
155
  options: XataClientOptions;
95
156
  db: D;
96
- constructor(options: XataClientOptions, links?: Links);
97
- initObject<T>(table: string, object: object): T;
98
- 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
+ }>;
99
164
  }
100
- declare type BuildData<O extends BaseData> = {
101
- [K in keyof O]: O[K] extends XataRecord ? {
102
- id: string;
103
- } : NonNullable<O[K]> extends XataRecord ? {
104
- id: string;
105
- } | undefined : O[K];
106
- };
107
165
  export {};