@xata.io/client 0.6.0 → 0.7.2

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.
@@ -1,72 +1,74 @@
1
- import { Constraint } from './filters';
2
- declare type ComparableType = number | Date;
1
+ import { ArrayFilter, ComparableType, ComparableTypeFilter, ExistanceFilter, PropertyFilter, StringTypeFilter } from './filters';
2
+ import { SelectableColumn } from './selection';
3
3
  /**
4
4
  * Operator to restrict results to only values that are greater than the given value.
5
5
  */
6
- export declare const gt: <T extends ComparableType>(value: T) => Constraint<T>;
6
+ export declare const gt: <T extends ComparableType>(value: T) => ComparableTypeFilter<T>;
7
7
  /**
8
8
  * Operator to restrict results to only values that are greater than or equal to the given value.
9
9
  */
10
- export declare const ge: <T extends ComparableType>(value: T) => Constraint<T>;
10
+ export declare const ge: <T extends ComparableType>(value: T) => ComparableTypeFilter<T>;
11
11
  /**
12
12
  * Operator to restrict results to only values that are greater than or equal to the given value.
13
13
  */
14
- export declare const gte: <T extends ComparableType>(value: T) => Constraint<T>;
14
+ export declare const gte: <T extends ComparableType>(value: T) => ComparableTypeFilter<T>;
15
15
  /**
16
16
  * Operator to restrict results to only values that are lower than the given value.
17
17
  */
18
- export declare const lt: <T extends ComparableType>(value: T) => Constraint<T>;
18
+ export declare const lt: <T extends ComparableType>(value: T) => ComparableTypeFilter<T>;
19
19
  /**
20
20
  * Operator to restrict results to only values that are lower than or equal to the given value.
21
21
  */
22
- export declare const lte: <T extends ComparableType>(value: T) => Constraint<T>;
22
+ export declare const lte: <T extends ComparableType>(value: T) => ComparableTypeFilter<T>;
23
23
  /**
24
24
  * Operator to restrict results to only values that are lower than or equal to the given value.
25
25
  */
26
- export declare const le: <T extends ComparableType>(value: T) => Constraint<T>;
26
+ export declare const le: <T extends ComparableType>(value: T) => ComparableTypeFilter<T>;
27
27
  /**
28
28
  * Operator to restrict results to only values that are not null.
29
29
  */
30
- export declare const exists: (column: string) => Constraint<string>;
30
+ export declare const exists: <T>(column: SelectableColumn<T, []>) => ExistanceFilter<T>;
31
31
  /**
32
32
  * Operator to restrict results to only values that are null.
33
33
  */
34
- export declare const notExists: (column: string) => Constraint<string>;
34
+ export declare const notExists: <T>(column: SelectableColumn<T, []>) => ExistanceFilter<T>;
35
35
  /**
36
36
  * Operator to restrict results to only values that start with the given prefix.
37
37
  */
38
- export declare const startsWith: (value: string) => Constraint<string>;
38
+ export declare const startsWith: (value: string) => StringTypeFilter;
39
39
  /**
40
40
  * Operator to restrict results to only values that end with the given suffix.
41
41
  */
42
- export declare const endsWith: (value: string) => Constraint<string>;
42
+ export declare const endsWith: (value: string) => StringTypeFilter;
43
43
  /**
44
44
  * Operator to restrict results to only values that match the given pattern.
45
45
  */
46
- export declare const pattern: (value: string) => Constraint<string>;
46
+ export declare const pattern: (value: string) => StringTypeFilter;
47
47
  /**
48
48
  * Operator to restrict results to only values that are equal to the given value.
49
49
  */
50
- export declare const is: <T>(value: T) => Constraint<T>;
50
+ export declare const is: <T>(value: T) => PropertyFilter<T>;
51
51
  /**
52
52
  * Operator to restrict results to only values that are not equal to the given value.
53
53
  */
54
- export declare const isNot: <T>(value: T) => Constraint<T>;
54
+ export declare const isNot: <T>(value: T) => PropertyFilter<T>;
55
55
  /**
56
56
  * Operator to restrict results to only values that contain the given value.
57
57
  */
58
- export declare const contains: <T>(value: T) => Constraint<T>;
58
+ export declare const contains: (value: string) => StringTypeFilter;
59
59
  /**
60
- * Operator to restrict results to only arrays that include the given value.
60
+ * Operator to restrict results if some array items match the predicate.
61
61
  */
62
- export declare const includes: (value: string) => Constraint<string>;
62
+ export declare const includes: <T>(value: T) => ArrayFilter<T>;
63
63
  /**
64
- * Operator to restrict results to only arrays that include a value matching the given substring.
64
+ * Operator to restrict results if all array items match the predicate.
65
65
  */
66
- export declare const includesSubstring: (value: string) => Constraint<string>;
66
+ export declare const includesAll: <T>(value: T) => ArrayFilter<T>;
67
67
  /**
68
- * Operator to restrict results to only arrays that include a value matching the given pattern.
68
+ * Operator to restrict results if none array items match the predicate.
69
69
  */
70
- export declare const includesPattern: (value: string) => Constraint<string>;
71
- export declare const includesAll: (value: string) => Constraint<string>;
72
- export {};
70
+ export declare const includesNone: <T>(value: T) => ArrayFilter<T>;
71
+ /**
72
+ * Operator to restrict results if some array items match the predicate.
73
+ */
74
+ export declare const includesAny: <T>(value: T) => ArrayFilter<T>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.includesAll = exports.includesPattern = exports.includesSubstring = exports.includes = exports.contains = exports.isNot = exports.is = exports.pattern = exports.endsWith = exports.startsWith = exports.notExists = exports.exists = exports.le = exports.lte = exports.lt = exports.gte = exports.ge = exports.gt = void 0;
3
+ exports.includesAny = exports.includesNone = exports.includesAll = exports.includes = exports.contains = exports.isNot = exports.is = exports.pattern = exports.endsWith = exports.startsWith = exports.notExists = exports.exists = exports.le = exports.lte = exports.lt = exports.gte = exports.ge = exports.gt = void 0;
4
4
  /**
5
5
  * Operator to restrict results to only values that are greater than the given value.
6
6
  */
@@ -71,21 +71,23 @@ exports.isNot = isNot;
71
71
  */
72
72
  const contains = (value) => ({ $contains: value });
73
73
  exports.contains = contains;
74
- // TODO: these can only be applied to columns of type "multiple"
75
74
  /**
76
- * Operator to restrict results to only arrays that include the given value.
75
+ * Operator to restrict results if some array items match the predicate.
77
76
  */
78
77
  const includes = (value) => ({ $includes: value });
79
78
  exports.includes = includes;
80
79
  /**
81
- * Operator to restrict results to only arrays that include a value matching the given substring.
80
+ * Operator to restrict results if all array items match the predicate.
82
81
  */
83
- const includesSubstring = (value) => ({ $includesSubstring: value });
84
- exports.includesSubstring = includesSubstring;
85
- /**
86
- * Operator to restrict results to only arrays that include a value matching the given pattern.
87
- */
88
- const includesPattern = (value) => ({ $includesPattern: value });
89
- exports.includesPattern = includesPattern;
90
82
  const includesAll = (value) => ({ $includesAll: value });
91
83
  exports.includesAll = includesAll;
84
+ /**
85
+ * Operator to restrict results if none array items match the predicate.
86
+ */
87
+ const includesNone = (value) => ({ $includesNone: value });
88
+ exports.includesNone = includesNone;
89
+ /**
90
+ * Operator to restrict results if some array items match the predicate.
91
+ */
92
+ const includesAny = (value) => ({ $includesAny: value });
93
+ exports.includesAny = includesAny;
@@ -1,10 +1,11 @@
1
1
  import { FilterExpression } from '../api/schemas';
2
2
  import { NonEmptyArray, RequiredBy } from '../util/types';
3
- import { DeepConstraint, FilterConstraints, SortDirection, SortFilter } from './filters';
3
+ import { Filter } from './filters';
4
4
  import { Page, Paginable, PaginationOptions, PaginationQueryMeta } from './pagination';
5
5
  import { XataRecord } from './record';
6
6
  import { Repository } from './repository';
7
7
  import { SelectableColumn, SelectedPick, ValueAtColumn } from './selection';
8
+ import { SortDirection, SortFilter } from './sorting';
8
9
  export declare type QueryOptions<T extends XataRecord> = {
9
10
  page?: PaginationOptions;
10
11
  columns?: NonEmptyArray<SelectableColumn<T>>;
@@ -28,25 +29,25 @@ export declare class Query<Record extends XataRecord, Result extends XataRecord
28
29
  * @param queries An array of subqueries.
29
30
  * @returns A new Query object.
30
31
  */
31
- any(...queries: Query<Record, Result>[]): Query<Record, Result>;
32
+ any(...queries: Query<Record, any>[]): Query<Record, Result>;
32
33
  /**
33
34
  * Builds a new query object representing a logical AND between the given subqueries.
34
35
  * @param queries An array of subqueries.
35
36
  * @returns A new Query object.
36
37
  */
37
- all(...queries: Query<Record, Result>[]): Query<Record, Result>;
38
+ all(...queries: Query<Record, any>[]): Query<Record, Result>;
38
39
  /**
39
40
  * Builds a new query object representing a logical OR negating each subquery. In pseudo-code: !q1 OR !q2
40
41
  * @param queries An array of subqueries.
41
42
  * @returns A new Query object.
42
43
  */
43
- not(...queries: Query<Record, Result>[]): Query<Record, Result>;
44
+ not(...queries: Query<Record, any>[]): Query<Record, Result>;
44
45
  /**
45
46
  * Builds a new query object representing a logical AND negating each subquery. In pseudo-code: !q1 AND !q2
46
47
  * @param queries An array of subqueries.
47
48
  * @returns A new Query object.
48
49
  */
49
- none(...queries: Query<Record, Result>[]): Query<Record, Result>;
50
+ none(...queries: Query<Record, any>[]): Query<Record, Result>;
50
51
  /**
51
52
  * Builds a new query object adding one or more constraints. Examples:
52
53
  *
@@ -60,11 +61,10 @@ export declare class Query<Record extends XataRecord, Result extends XataRecord
60
61
  * })
61
62
  * ```
62
63
  *
63
- * @param constraints
64
64
  * @returns A new Query object.
65
65
  */
66
- filter(constraints: FilterConstraints<Record>): Query<Record, Result>;
67
- filter<F extends SelectableColumn<Record>>(column: F, value: FilterConstraints<ValueAtColumn<Record, F>> | DeepConstraint<ValueAtColumn<Record, F>>): Query<Record, Result>;
66
+ filter(filters: Filter<Record>): Query<Record, Result>;
67
+ filter<F extends SelectableColumn<Record>>(column: F, value: Filter<ValueAtColumn<Record, F>>): Query<Record, Result>;
68
68
  /**
69
69
  * Builds a new query with a new sort option.
70
70
  * @param column The column name.
@@ -62,5 +62,5 @@ export declare type EditableData<O extends BaseData> = {
62
62
  id: string;
63
63
  } : NonNullable<O[K]> extends XataRecord ? {
64
64
  id: string;
65
- } | undefined : O[K];
65
+ } | null | undefined : O[K];
66
66
  };
@@ -1,9 +1,11 @@
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
5
  import { BaseData, EditableData, Identifiable, XataRecord } from './record';
5
6
  import { SelectedPick } from './selection';
6
- export declare type Links = Record<string, Array<string[]>>;
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
  */
@@ -21,20 +23,39 @@ export declare abstract class Repository<Data extends BaseData, Record extends X
21
23
  * @param objects Array of objects with the column names and the values to be stored in the table.
22
24
  * @returns Array of the persisted records.
23
25
  */
24
- abstract createMany(objects: EditableData<Data>[]): Promise<SelectedPick<Record, ['*']>[]>;
26
+ abstract create(objects: Array<EditableData<Data> & Partial<Identifiable>>): Promise<SelectedPick<Record, ['*']>[]>;
25
27
  /**
26
28
  * Queries a single record from the table given its unique id.
27
29
  * @param id The unique id.
28
30
  * @returns The persisted record for the given id or null if the record could not be found.
29
31
  */
30
32
  abstract read(id: string): Promise<SelectedPick<Record, ['*']> | null>;
33
+ /**
34
+ * Partially update a single record.
35
+ * @param object An object with its id and the columns to be updated.
36
+ * @returns The full persisted record.
37
+ */
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
45
  abstract update(id: string, object: Partial<EditableData<Data>>): Promise<SelectedPick<Record, ['*']>>;
46
+ /**
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, ['*']>>;
38
59
  /**
39
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.
@@ -43,31 +64,72 @@ export declare abstract class Repository<Data extends BaseData, Record extends X
43
64
  * @returns The full persisted record.
44
65
  */
45
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;
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, ['*']>[]>;
52
107
  abstract query<Result extends XataRecord>(query: Query<Record, Result>): Promise<Page<Record, Result>>;
53
108
  }
54
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);
111
+ constructor(client: BaseClient<any>, table: string, links?: LinkDictionary);
57
112
  create(object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
58
113
  create(recordId: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
59
- createMany(objects: EditableData<Data>[]): Promise<SelectedPick<Record, ['*']>[]>;
114
+ create(objects: EditableData<Data>[]): Promise<SelectedPick<Record, ['*']>[]>;
60
115
  read(recordId: string): Promise<SelectedPick<Record, ['*']> | null>;
116
+ update(object: Partial<EditableData<Data>> & Identifiable): Promise<SelectedPick<Record, ['*']>>;
61
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, ['*']>>;
62
120
  createOrUpdate(recordId: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
63
- delete(recordId: string): Promise<void>;
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, ['*']>[]>;
64
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,19 +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
165
  export {};