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

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 (66) hide show
  1. package/.eslintrc.cjs +13 -0
  2. package/CHANGELOG.md +87 -0
  3. package/dist/api/client.d.ts +95 -0
  4. package/dist/api/client.js +251 -0
  5. package/dist/api/components.d.ts +1437 -0
  6. package/dist/api/components.js +998 -0
  7. package/dist/api/fetcher.d.ts +40 -0
  8. package/dist/api/fetcher.js +79 -0
  9. package/dist/api/index.d.ts +7 -0
  10. package/dist/api/index.js +21 -0
  11. package/dist/api/parameters.d.ts +16 -0
  12. package/dist/api/parameters.js +2 -0
  13. package/dist/api/providers.d.ts +8 -0
  14. package/dist/api/providers.js +30 -0
  15. package/dist/api/responses.d.ts +50 -0
  16. package/dist/api/responses.js +2 -0
  17. package/dist/api/schemas.d.ts +311 -0
  18. package/dist/api/schemas.js +2 -0
  19. package/dist/client.d.ts +39 -0
  20. package/dist/client.js +124 -0
  21. package/dist/index.d.ts +5 -182
  22. package/dist/index.js +19 -499
  23. package/dist/namespace.d.ts +7 -0
  24. package/dist/namespace.js +6 -0
  25. package/dist/schema/filters.d.ts +96 -0
  26. package/dist/schema/filters.js +2 -0
  27. package/dist/schema/filters.spec.d.ts +1 -0
  28. package/dist/schema/filters.spec.js +177 -0
  29. package/dist/schema/index.d.ts +21 -0
  30. package/dist/schema/index.js +49 -0
  31. package/dist/schema/operators.d.ts +74 -0
  32. package/dist/schema/operators.js +93 -0
  33. package/dist/schema/pagination.d.ts +83 -0
  34. package/dist/schema/pagination.js +93 -0
  35. package/dist/schema/query.d.ts +118 -0
  36. package/dist/schema/query.js +242 -0
  37. package/dist/schema/record.d.ts +66 -0
  38. package/dist/schema/record.js +13 -0
  39. package/dist/schema/repository.d.ts +134 -0
  40. package/dist/schema/repository.js +284 -0
  41. package/dist/schema/selection.d.ts +25 -0
  42. package/dist/schema/selection.js +2 -0
  43. package/dist/schema/selection.spec.d.ts +1 -0
  44. package/dist/schema/selection.spec.js +204 -0
  45. package/dist/schema/sorting.d.ts +17 -0
  46. package/dist/schema/sorting.js +28 -0
  47. package/dist/schema/sorting.spec.d.ts +1 -0
  48. package/dist/schema/sorting.spec.js +11 -0
  49. package/dist/search/index.d.ts +20 -0
  50. package/dist/search/index.js +30 -0
  51. package/dist/util/branches.d.ts +5 -0
  52. package/dist/util/branches.js +7 -0
  53. package/dist/util/config.d.ts +11 -0
  54. package/dist/util/config.js +121 -0
  55. package/dist/util/environment.d.ts +5 -0
  56. package/dist/util/environment.js +68 -0
  57. package/dist/util/fetch.d.ts +2 -0
  58. package/dist/util/fetch.js +13 -0
  59. package/dist/util/lang.d.ts +5 -0
  60. package/dist/util/lang.js +22 -0
  61. package/dist/util/types.d.ts +25 -0
  62. package/dist/util/types.js +2 -0
  63. package/package.json +5 -2
  64. package/tsconfig.json +21 -0
  65. package/dist/util/errors.d.ts +0 -3
  66. package/dist/util/errors.js +0 -9
package/dist/client.js ADDED
@@ -0,0 +1,124 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
12
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
13
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
14
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
15
+ };
16
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
17
+ if (kind === "m") throw new TypeError("Private method is not writable");
18
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
19
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
20
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
21
+ };
22
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
23
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
24
+ var m = o[Symbol.asyncIterator], i;
25
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
26
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
27
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
28
+ };
29
+ Object.defineProperty(exports, "__esModule", { value: true });
30
+ exports.BaseClient = exports.buildClient = exports.buildClientWithNamespaces = void 0;
31
+ const schema_1 = require("./schema");
32
+ const search_1 = require("./search");
33
+ const branches_1 = require("./util/branches");
34
+ const config_1 = require("./util/config");
35
+ const fetch_1 = require("./util/fetch");
36
+ const buildClientWithNamespaces = (namespaces) => () => (0, exports.buildClient)(namespaces);
37
+ exports.buildClientWithNamespaces = buildClientWithNamespaces;
38
+ const buildClient = (external) => {
39
+ var _instances, _branch, _parseOptions, _getFetchProps, _evaluateBranch, _a;
40
+ return _a = class {
41
+ constructor(options = {}, links) {
42
+ _instances.add(this);
43
+ _branch.set(this, void 0);
44
+ const safeOptions = __classPrivateFieldGet(this, _instances, "m", _parseOptions).call(this, options);
45
+ const namespaces = Object.assign({ db: new schema_1.SchemaNamespace(links), search: new search_1.SearchNamespace() }, external);
46
+ for (const [key, namespace] of Object.entries(namespaces)) {
47
+ if (!namespace)
48
+ continue;
49
+ // @ts-ignore
50
+ this[key] = namespace.build({ getFetchProps: () => __classPrivateFieldGet(this, _instances, "m", _getFetchProps).call(this, safeOptions) });
51
+ }
52
+ }
53
+ },
54
+ _branch = new WeakMap(),
55
+ _instances = new WeakSet(),
56
+ _parseOptions = function _parseOptions(options) {
57
+ const fetch = (0, fetch_1.getFetchImplementation)(options === null || options === void 0 ? void 0 : options.fetch);
58
+ const databaseURL = (options === null || options === void 0 ? void 0 : options.databaseURL) || (0, config_1.getDatabaseURL)();
59
+ const apiKey = (options === null || options === void 0 ? void 0 : options.apiKey) || (0, config_1.getAPIKey)();
60
+ const branch = () => __awaiter(this, void 0, void 0, function* () {
61
+ return (options === null || options === void 0 ? void 0 : options.branch)
62
+ ? yield __classPrivateFieldGet(this, _instances, "m", _evaluateBranch).call(this, options.branch)
63
+ : yield (0, config_1.getCurrentBranchName)({ apiKey, databaseURL, fetchImpl: options === null || options === void 0 ? void 0 : options.fetch });
64
+ });
65
+ if (!databaseURL || !apiKey) {
66
+ throw new Error('Options databaseURL and apiKey are required');
67
+ }
68
+ return { fetch, databaseURL, apiKey, branch };
69
+ },
70
+ _getFetchProps = function _getFetchProps({ fetch, apiKey, databaseURL, branch }) {
71
+ return __awaiter(this, void 0, void 0, function* () {
72
+ const branchValue = yield __classPrivateFieldGet(this, _instances, "m", _evaluateBranch).call(this, branch);
73
+ if (!branchValue)
74
+ throw new Error('Unable to resolve branch value');
75
+ return {
76
+ fetchImpl: fetch,
77
+ apiKey,
78
+ apiUrl: '',
79
+ // Instead of using workspace and dbBranch, we inject a probably CNAME'd URL
80
+ workspacesApiUrl: (path, params) => {
81
+ var _a;
82
+ const hasBranch = (_a = params.dbBranchName) !== null && _a !== void 0 ? _a : params.branch;
83
+ const newPath = path.replace(/^\/db\/[^/]+/, hasBranch ? `:${branchValue}` : '');
84
+ return databaseURL + newPath;
85
+ }
86
+ };
87
+ });
88
+ },
89
+ _evaluateBranch = function _evaluateBranch(param) {
90
+ var e_1, _a;
91
+ return __awaiter(this, void 0, void 0, function* () {
92
+ if (__classPrivateFieldGet(this, _branch, "f"))
93
+ return __classPrivateFieldGet(this, _branch, "f");
94
+ if (!param)
95
+ return undefined;
96
+ const strategies = Array.isArray(param) ? [...param] : [param];
97
+ const evaluateBranch = (strategy) => __awaiter(this, void 0, void 0, function* () {
98
+ return (0, branches_1.isBranchStrategyBuilder)(strategy) ? yield strategy() : strategy;
99
+ });
100
+ try {
101
+ for (var strategies_1 = __asyncValues(strategies), strategies_1_1; strategies_1_1 = yield strategies_1.next(), !strategies_1_1.done;) {
102
+ const strategy = strategies_1_1.value;
103
+ const branch = yield evaluateBranch(strategy);
104
+ if (branch) {
105
+ __classPrivateFieldSet(this, _branch, branch, "f");
106
+ return branch;
107
+ }
108
+ }
109
+ }
110
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
111
+ finally {
112
+ try {
113
+ if (strategies_1_1 && !strategies_1_1.done && (_a = strategies_1.return)) yield _a.call(strategies_1);
114
+ }
115
+ finally { if (e_1) throw e_1.error; }
116
+ }
117
+ });
118
+ },
119
+ _a;
120
+ };
121
+ exports.buildClient = buildClient;
122
+ class BaseClient extends (0, exports.buildClient)() {
123
+ }
124
+ exports.BaseClient = BaseClient;
package/dist/index.d.ts CHANGED
@@ -1,186 +1,9 @@
1
- export interface XataRecord {
2
- id: string;
3
- xata: {
4
- version: number;
5
- };
6
- read(): Promise<this>;
7
- update(data: Selectable<this>): Promise<this>;
8
- delete(): Promise<void>;
9
- }
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>>;
135
- abstract create(object: Selectable<T>): Promise<T>;
136
- abstract createMany(objects: Selectable<T>[]): Promise<T[]>;
137
- abstract read(id: string): Promise<T | null>;
138
- abstract update(id: string, object: Partial<T>): Promise<T>;
139
- abstract delete(id: string): void;
140
- abstract _runQuery<R>(query: Query<T, R>, options?: BulkQueryOptions<T>): Promise<Page<T, R>>;
141
- }
142
- export declare class RestRepository<T> extends Repository<T> {
143
- client: BaseClient<any>;
144
- fetch: any;
145
- 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
- create(object: T): Promise<T>;
149
- 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>>;
154
- }
155
- interface RepositoryFactory {
156
- createRepository<T>(client: BaseClient<any>, table: string): Repository<T>;
157
- }
158
- export declare class RestRespositoryFactory implements RepositoryFactory {
159
- createRepository<T>(client: BaseClient<any>, table: string): Repository<T>;
160
- }
161
- declare type BranchStrategyValue = string | undefined | null;
162
- declare type BranchStrategyBuilder = () => BranchStrategyValue | Promise<BranchStrategyValue>;
163
- declare type BranchStrategy = BranchStrategyValue | BranchStrategyBuilder;
164
- declare type BranchStrategyOption = NonNullable<BranchStrategy | BranchStrategy[]>;
165
- export declare type XataClientOptions = {
166
- fetch?: unknown;
167
- databaseURL?: string;
168
- branch: BranchStrategyOption;
169
- apiKey: string;
170
- repositoryFactory?: RepositoryFactory;
171
- };
172
- export declare class BaseClient<D extends Record<string, Repository<any>>> {
173
- options: XataClientOptions;
174
- private links;
175
- private branch;
176
- db: D;
177
- constructor(options: XataClientOptions, links: Links);
178
- initObject<T>(table: string, object: object): T;
179
- getBranch(): Promise<string>;
180
- }
181
1
  export declare class XataError extends Error {
182
2
  readonly status: number;
183
3
  constructor(message: string, status: number);
184
4
  }
185
- export declare type Links = Record<string, Array<string[]>>;
186
- export {};
5
+ export * from './api';
6
+ export * from './client';
7
+ export * from './schema';
8
+ export * from './search';
9
+ export * from './util/config';