@xata.io/client 0.0.0-beta.c9e08d0 → 0.0.0-beta.cae436d

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/dist/index.d.ts CHANGED
@@ -1,3 +1,7 @@
1
+ import { FetchImpl } from './api/fetcher';
2
+ import { Page } from './schema/pagination';
3
+ import { Query, QueryOptions } from './schema/query';
4
+ import { Selectable, SelectableColumn, Select } from './schema/selection';
1
5
  export interface XataRecord {
2
6
  id: string;
3
7
  xata: {
@@ -7,172 +11,44 @@ export interface XataRecord {
7
11
  update(data: Selectable<this>): Promise<this>;
8
12
  delete(): Promise<void>;
9
13
  }
10
- export declare type Queries<T> = {
11
- [key in keyof T as T[key] extends Query<infer A, infer B> ? key : never]: T[key];
12
- };
13
- export declare type OmitQueries<T> = {
14
- [key in keyof T as T[key] extends Query<infer A, infer B> ? never : key]: T[key];
15
- };
16
- export declare type OmitLinks<T> = {
17
- [key in keyof T as T[key] extends XataRecord ? never : key]: T[key];
18
- };
19
- export declare type OmitMethods<T> = {
20
- [key in keyof T as T[key] extends Function ? never : key]: T[key];
21
- };
22
- export declare type Selectable<T> = Omit<OmitQueries<OmitMethods<T>>, 'id' | 'xata'>;
23
- export declare type Select<T, K extends keyof T> = Pick<T, K> & Queries<T> & XataRecord;
24
- export declare type Include<T> = {
25
- [key in keyof T as T[key] extends XataRecord ? key : never]?: boolean | Array<keyof Selectable<T[key]>>;
26
- };
27
- declare type SortDirection = 'asc' | 'desc';
28
- declare type Operator = '$gt' | '$lt' | '$ge' | '$le' | '$exists' | '$notExists' | '$endsWith' | '$startsWith' | '$pattern' | '$is' | '$isNot' | '$contains' | '$includes' | '$includesSubstring' | '$includesPattern' | '$includesAll';
29
- declare type Constraint<T> = {
30
- [key in Operator]?: T;
31
- };
32
- declare type DeepConstraint<T> = T extends Record<string, any> ? {
33
- [key in keyof T]?: T[key] | DeepConstraint<T[key]>;
34
- } : Constraint<T>;
35
- declare type ComparableType = number | Date;
36
- export declare const gt: <T extends ComparableType>(value: T) => Constraint<T>;
37
- export declare const ge: <T extends ComparableType>(value: T) => Constraint<T>;
38
- export declare const gte: <T extends ComparableType>(value: T) => Constraint<T>;
39
- export declare const lt: <T extends ComparableType>(value: T) => Constraint<T>;
40
- export declare const lte: <T extends ComparableType>(value: T) => Constraint<T>;
41
- export declare const le: <T extends ComparableType>(value: T) => Constraint<T>;
42
- export declare const exists: (column: string) => Constraint<string>;
43
- export declare const notExists: (column: string) => Constraint<string>;
44
- export declare const startsWith: (value: string) => Constraint<string>;
45
- export declare const endsWith: (value: string) => Constraint<string>;
46
- export declare const pattern: (value: string) => Constraint<string>;
47
- export declare const is: <T>(value: T) => Constraint<T>;
48
- export declare const isNot: <T>(value: T) => Constraint<T>;
49
- export declare const contains: <T>(value: T) => Constraint<T>;
50
- export declare const includes: (value: string) => Constraint<string>;
51
- export declare const includesSubstring: (value: string) => Constraint<string>;
52
- export declare const includesPattern: (value: string) => Constraint<string>;
53
- export declare const includesAll: (value: string) => Constraint<string>;
54
- declare type FilterConstraints<T> = {
55
- [key in keyof T]?: T[key] extends Record<string, any> ? FilterConstraints<T[key]> : T[key] | DeepConstraint<T[key]>;
56
- };
57
- declare type CursorNavigationOptions = {
58
- first?: string;
59
- } | {
60
- last?: string;
61
- } | {
62
- after?: string;
63
- before?: string;
64
- };
65
- declare type OffsetNavigationOptions = {
66
- size?: number;
67
- offset?: number;
68
- };
69
- declare type PaginationOptions = CursorNavigationOptions & OffsetNavigationOptions;
70
- declare type BulkQueryOptions<T> = {
71
- page?: PaginationOptions;
72
- };
73
- declare type QueryOrConstraint<T, R> = Query<T, R> | Constraint<T>;
74
- declare type QueryMeta = {
75
- page: {
76
- cursor: string;
77
- more: boolean;
78
- };
79
- };
80
- interface BasePage<T, R> {
81
- query: Query<T, R>;
82
- meta: QueryMeta;
83
- records: R[];
84
- nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
85
- previousPage(size?: number, offset?: number): Promise<Page<T, R>>;
86
- firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
87
- lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
88
- hasNextPage(): boolean;
89
- }
90
- declare class Page<T, R> implements BasePage<T, R> {
91
- readonly query: Query<T, R>;
92
- readonly meta: QueryMeta;
93
- readonly records: R[];
94
- constructor(query: Query<T, R>, meta: QueryMeta, records?: R[]);
95
- nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
96
- previousPage(size?: number, offset?: number): Promise<Page<T, R>>;
97
- firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
98
- lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
99
- hasNextPage(): boolean;
100
- }
101
- export declare class Query<T, R = T> implements BasePage<T, R> {
102
- table: string;
103
- repository: Repository<T>;
104
- readonly $any?: QueryOrConstraint<T, R>[];
105
- readonly $all?: QueryOrConstraint<T, R>[];
106
- readonly $not?: QueryOrConstraint<T, R>[];
107
- readonly $none?: QueryOrConstraint<T, R>[];
108
- readonly $sort?: Record<string, SortDirection>;
109
- readonly query: Query<T, R>;
110
- readonly meta: QueryMeta;
111
- readonly records: R[];
112
- constructor(repository: Repository<T> | null, table: string, data: Partial<Query<T, R>>, parent?: Query<T, R>);
113
- any(...queries: Query<T, R>[]): Query<T, R>;
114
- all(...queries: Query<T, R>[]): Query<T, R>;
115
- not(...queries: Query<T, R>[]): Query<T, R>;
116
- none(...queries: Query<T, R>[]): Query<T, R>;
117
- filter(constraints: FilterConstraints<T>): Query<T, R>;
118
- filter<F extends keyof T>(column: F, value: FilterConstraints<T[F]> | DeepConstraint<T[F]>): Query<T, R>;
119
- sort<F extends keyof T>(column: F, direction: SortDirection): Query<T, R>;
120
- getPaginated(options?: BulkQueryOptions<T>): Promise<Page<T, R>>;
121
- [Symbol.asyncIterator](): AsyncIterableIterator<R>;
122
- getIterator(chunk: number, options?: Omit<BulkQueryOptions<T>, 'page'>): AsyncGenerator<R[]>;
123
- getMany(options?: BulkQueryOptions<T>): Promise<R[]>;
124
- getOne(options?: Omit<BulkQueryOptions<T>, 'page'>): Promise<R | null>;
125
- deleteAll(): Promise<number>;
126
- include(columns: Include<T>): this;
127
- nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
128
- previousPage(size?: number, offset?: number): Promise<Page<T, R>>;
129
- firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
130
- lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
131
- hasNextPage(): boolean;
132
- }
133
- export declare abstract class Repository<T> extends Query<T, Selectable<T>> {
134
- select<K extends keyof Selectable<T>>(...columns: K[]): Query<T, Select<T, K>>;
14
+ export declare abstract class Repository<T extends XataRecord> extends Query<T> {
135
15
  abstract create(object: Selectable<T>): Promise<T>;
136
16
  abstract createMany(objects: Selectable<T>[]): Promise<T[]>;
137
17
  abstract read(id: string): Promise<T | null>;
138
18
  abstract update(id: string, object: Partial<T>): Promise<T>;
139
19
  abstract delete(id: string): void;
140
- abstract _runQuery<R>(query: Query<T, R>, options?: BulkQueryOptions<T>): Promise<Page<T, R>>;
20
+ 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>>;
141
21
  }
142
- export declare class RestRepository<T> extends Repository<T> {
143
- client: BaseClient<any>;
144
- fetch: any;
22
+ export declare class RestRepository<T extends XataRecord> extends Repository<T> {
23
+ #private;
145
24
  constructor(client: BaseClient<any>, table: string);
146
- request<T>(method: string, path: string, body?: unknown): Promise<T | undefined>;
147
- select<K extends keyof T>(...columns: K[]): Query<T, Select<T, K>>;
148
25
  create(object: T): Promise<T>;
149
26
  createMany(objects: T[]): Promise<T[]>;
150
- read(id: string): Promise<T | null>;
151
- update(id: string, object: Partial<T>): Promise<T>;
152
- delete(id: string): Promise<void>;
153
- _runQuery<R>(query: Query<T, R>, options?: BulkQueryOptions<T>): Promise<Page<T, R>>;
27
+ read(recordId: string): Promise<T | null>;
28
+ update(recordId: string, object: Partial<T>): Promise<T>;
29
+ delete(recordId: string): Promise<void>;
30
+ 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>>;
154
31
  }
155
32
  interface RepositoryFactory {
156
- createRepository<T>(client: BaseClient<any>, table: string): Repository<T>;
33
+ createRepository<T extends XataRecord>(client: BaseClient<any>, table: string): Repository<T>;
157
34
  }
158
35
  export declare class RestRespositoryFactory implements RepositoryFactory {
159
- createRepository<T>(client: BaseClient<any>, table: string): Repository<T>;
36
+ createRepository<T extends XataRecord>(client: BaseClient<any>, table: string): Repository<T>;
160
37
  }
161
38
  declare type BranchStrategyValue = string | undefined | null;
162
39
  declare type BranchStrategyBuilder = () => BranchStrategyValue | Promise<BranchStrategyValue>;
163
40
  declare type BranchStrategy = BranchStrategyValue | BranchStrategyBuilder;
164
41
  declare type BranchStrategyOption = NonNullable<BranchStrategy | BranchStrategy[]>;
165
42
  export declare type XataClientOptions = {
166
- fetch?: unknown;
43
+ fetch?: FetchImpl;
167
44
  databaseURL?: string;
168
45
  branch: BranchStrategyOption;
169
46
  apiKey: string;
170
47
  repositoryFactory?: RepositoryFactory;
171
48
  };
172
49
  export declare class BaseClient<D extends Record<string, Repository<any>>> {
50
+ #private;
173
51
  options: XataClientOptions;
174
- private links;
175
- private branch;
176
52
  db: D;
177
53
  constructor(options: XataClientOptions, links: Links);
178
54
  initObject<T>(table: string, object: object): T;
@@ -183,4 +59,5 @@ export declare class XataError extends Error {
183
59
  constructor(message: string, status: number);
184
60
  }
185
61
  export declare type Links = Record<string, Array<string[]>>;
186
- export {};
62
+ export * from './api';
63
+ export * from './schema';