@xata.io/client 0.0.0-beta.123dd7a → 0.0.0-beta.14375f7
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/.eslintrc.cjs +13 -0
- package/CHANGELOG.md +63 -0
- package/dist/api/client.d.ts +95 -0
- package/dist/api/client.js +238 -0
- package/dist/api/components.d.ts +1437 -0
- package/dist/api/components.js +998 -0
- package/dist/api/fetcher.d.ts +40 -0
- package/dist/api/fetcher.js +79 -0
- package/dist/api/index.d.ts +7 -0
- package/dist/api/index.js +21 -0
- package/dist/api/parameters.d.ts +16 -0
- package/dist/api/parameters.js +2 -0
- package/dist/api/providers.d.ts +8 -0
- package/dist/api/providers.js +30 -0
- package/dist/api/responses.d.ts +50 -0
- package/dist/api/responses.js +2 -0
- package/dist/api/schemas.d.ts +311 -0
- package/dist/api/schemas.js +2 -0
- package/dist/index.d.ts +2 -133
- package/dist/index.js +16 -368
- package/dist/schema/config.d.ts +4 -0
- package/dist/schema/config.js +83 -0
- package/dist/schema/filters.d.ts +96 -0
- package/dist/schema/filters.js +2 -0
- package/dist/{index.test.d.ts → schema/filters.spec.d.ts} +0 -0
- package/dist/schema/filters.spec.js +175 -0
- package/dist/schema/index.d.ts +7 -0
- package/dist/schema/index.js +29 -0
- package/dist/schema/operators.d.ts +74 -0
- package/dist/schema/operators.js +93 -0
- package/dist/schema/pagination.d.ts +83 -0
- package/dist/schema/pagination.js +93 -0
- package/dist/schema/query.d.ts +118 -0
- package/dist/schema/query.js +242 -0
- package/dist/schema/record.d.ts +66 -0
- package/dist/schema/record.js +13 -0
- package/dist/schema/repository.d.ts +148 -0
- package/dist/schema/repository.js +381 -0
- package/dist/schema/selection.d.ts +25 -0
- package/dist/schema/selection.js +2 -0
- package/dist/schema/selection.spec.d.ts +1 -0
- package/dist/schema/selection.spec.js +203 -0
- package/dist/schema/sorting.d.ts +17 -0
- package/dist/schema/sorting.js +28 -0
- package/dist/schema/sorting.spec.d.ts +1 -0
- package/dist/schema/sorting.spec.js +9 -0
- package/dist/util/environment.d.ts +5 -0
- package/dist/util/environment.js +68 -0
- package/dist/util/fetch.d.ts +2 -0
- package/dist/util/fetch.js +13 -0
- package/dist/util/lang.d.ts +5 -0
- package/dist/util/lang.js +22 -0
- package/dist/util/types.d.ts +24 -0
- package/dist/util/types.js +2 -0
- package/package.json +3 -3
- package/dist/index.test.js +0 -304
- package/src/index.test.ts +0 -392
- package/src/index.ts +0 -506
package/dist/index.d.ts
CHANGED
@@ -1,137 +1,6 @@
|
|
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 BulkQueryOptions<T> = {
|
58
|
-
filter?: FilterConstraints<T>;
|
59
|
-
sort?: {
|
60
|
-
column: keyof T;
|
61
|
-
direction?: SortDirection;
|
62
|
-
} | keyof T;
|
63
|
-
};
|
64
|
-
declare type QueryOrConstraint<T, R> = Query<T, R> | Constraint<T>;
|
65
|
-
export declare class Query<T, R = T> {
|
66
|
-
table: string;
|
67
|
-
repository: Repository<T>;
|
68
|
-
readonly $any?: QueryOrConstraint<T, R>[];
|
69
|
-
readonly $all?: QueryOrConstraint<T, R>[];
|
70
|
-
readonly $not?: QueryOrConstraint<T, R>[];
|
71
|
-
readonly $none?: QueryOrConstraint<T, R>[];
|
72
|
-
readonly $sort?: Record<string, SortDirection>;
|
73
|
-
constructor(repository: Repository<T> | null, table: string, data: Partial<Query<T, R>>, parent?: Query<T, R>);
|
74
|
-
any(...queries: Query<T, R>[]): Query<T, R>;
|
75
|
-
all(...queries: Query<T, R>[]): Query<T, R>;
|
76
|
-
not(...queries: Query<T, R>[]): Query<T, R>;
|
77
|
-
none(...queries: Query<T, R>[]): Query<T, R>;
|
78
|
-
filter(constraints: FilterConstraints<T>): Query<T, R>;
|
79
|
-
filter<F extends keyof T>(column: F, value: FilterConstraints<T[F]> | DeepConstraint<T[F]>): Query<T, R>;
|
80
|
-
sort<F extends keyof T>(column: F, direction: SortDirection): Query<T, R>;
|
81
|
-
getMany(options?: BulkQueryOptions<T>): Promise<R[]>;
|
82
|
-
getOne(options?: BulkQueryOptions<T>): Promise<R | null>;
|
83
|
-
deleteAll(): Promise<number>;
|
84
|
-
include(columns: Include<T>): this;
|
85
|
-
}
|
86
|
-
export declare abstract class Repository<T> extends Query<T, Selectable<T>> {
|
87
|
-
select<K extends keyof Selectable<T>>(...columns: K[]): Query<T, Select<T, K>>;
|
88
|
-
abstract create(object: Selectable<T>): Promise<T>;
|
89
|
-
abstract read(id: string): Promise<T | null>;
|
90
|
-
abstract update(id: string, object: Partial<T>): Promise<T>;
|
91
|
-
abstract delete(id: string): void;
|
92
|
-
abstract query<R>(query: Query<T, R>): Promise<R[]>;
|
93
|
-
}
|
94
|
-
export declare class RestRepository<T> extends Repository<T> {
|
95
|
-
client: BaseClient<any>;
|
96
|
-
fetch: any;
|
97
|
-
constructor(client: BaseClient<any>, table: string);
|
98
|
-
request(method: string, path: string, body?: unknown): Promise<any>;
|
99
|
-
select<K extends keyof T>(...columns: K[]): Query<T, Select<T, K>>;
|
100
|
-
create(object: T): Promise<T>;
|
101
|
-
read(id: string): Promise<T | null>;
|
102
|
-
update(id: string, object: Partial<T>): Promise<T>;
|
103
|
-
delete(id: string): Promise<void>;
|
104
|
-
query<R>(query: Query<T, R>): Promise<R[]>;
|
105
|
-
}
|
106
|
-
interface RepositoryFactory {
|
107
|
-
createRepository<T>(client: BaseClient<any>, table: string): Repository<T>;
|
108
|
-
}
|
109
|
-
export declare class RestRespositoryFactory implements RepositoryFactory {
|
110
|
-
createRepository<T>(client: BaseClient<any>, table: string): Repository<T>;
|
111
|
-
}
|
112
|
-
declare type BranchStrategyValue = string | undefined | null;
|
113
|
-
declare type BranchStrategyBuilder = () => BranchStrategyValue | Promise<BranchStrategyValue>;
|
114
|
-
declare type BranchStrategy = BranchStrategyValue | BranchStrategyBuilder;
|
115
|
-
declare type BranchStrategyOption = NonNullable<BranchStrategy | BranchStrategy[]>;
|
116
|
-
export declare type XataClientOptions = {
|
117
|
-
fetch?: unknown;
|
118
|
-
databaseURL: string;
|
119
|
-
branch: BranchStrategyOption;
|
120
|
-
apiKey: string;
|
121
|
-
repositoryFactory?: RepositoryFactory;
|
122
|
-
};
|
123
|
-
export declare class BaseClient<D extends Record<string, Repository<any>>> {
|
124
|
-
options: XataClientOptions;
|
125
|
-
private links;
|
126
|
-
private branch;
|
127
|
-
db: D;
|
128
|
-
constructor(options: XataClientOptions, links: Links);
|
129
|
-
initObject<T>(table: string, object: object): T;
|
130
|
-
getBranch(): Promise<string>;
|
131
|
-
}
|
132
1
|
export declare class XataError extends Error {
|
133
2
|
readonly status: number;
|
134
3
|
constructor(message: string, status: number);
|
135
4
|
}
|
136
|
-
export
|
137
|
-
export
|
5
|
+
export * from './api';
|
6
|
+
export * from './schema';
|
package/dist/index.js
CHANGED
@@ -1,371 +1,20 @@
|
|
1
1
|
"use strict";
|
2
|
-
var
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
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); }); }; }
|
16
|
-
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
17
15
|
};
|
18
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
19
|
-
exports.XataError =
|
20
|
-
const gt = (value) => ({ $gt: value });
|
21
|
-
exports.gt = gt;
|
22
|
-
const ge = (value) => ({ $ge: value });
|
23
|
-
exports.ge = ge;
|
24
|
-
const gte = (value) => ({ $ge: value });
|
25
|
-
exports.gte = gte;
|
26
|
-
const lt = (value) => ({ $lt: value });
|
27
|
-
exports.lt = lt;
|
28
|
-
const lte = (value) => ({ $le: value });
|
29
|
-
exports.lte = lte;
|
30
|
-
const le = (value) => ({ $le: value });
|
31
|
-
exports.le = le;
|
32
|
-
const exists = (column) => ({ $exists: column });
|
33
|
-
exports.exists = exists;
|
34
|
-
const notExists = (column) => ({ $notExists: column });
|
35
|
-
exports.notExists = notExists;
|
36
|
-
const startsWith = (value) => ({ $startsWith: value });
|
37
|
-
exports.startsWith = startsWith;
|
38
|
-
const endsWith = (value) => ({ $endsWith: value });
|
39
|
-
exports.endsWith = endsWith;
|
40
|
-
const pattern = (value) => ({ $pattern: value });
|
41
|
-
exports.pattern = pattern;
|
42
|
-
const is = (value) => ({ $is: value });
|
43
|
-
exports.is = is;
|
44
|
-
const isNot = (value) => ({ $isNot: value });
|
45
|
-
exports.isNot = isNot;
|
46
|
-
const contains = (value) => ({ $contains: value });
|
47
|
-
exports.contains = contains;
|
48
|
-
// TODO: these can only be applied to columns of type "multiple"
|
49
|
-
const includes = (value) => ({ $includes: value });
|
50
|
-
exports.includes = includes;
|
51
|
-
const includesSubstring = (value) => ({ $includesSubstring: value });
|
52
|
-
exports.includesSubstring = includesSubstring;
|
53
|
-
const includesPattern = (value) => ({ $includesPattern: value });
|
54
|
-
exports.includesPattern = includesPattern;
|
55
|
-
const includesAll = (value) => ({ $includesAll: value });
|
56
|
-
exports.includesAll = includesAll;
|
57
|
-
class Query {
|
58
|
-
constructor(repository, table, data, parent) {
|
59
|
-
if (repository) {
|
60
|
-
this.repository = repository;
|
61
|
-
}
|
62
|
-
else {
|
63
|
-
this.repository = this;
|
64
|
-
}
|
65
|
-
this.table = table;
|
66
|
-
// For some reason Object.assign(this, parent) didn't work in this case
|
67
|
-
// so doing all this manually:
|
68
|
-
this.$any = parent === null || parent === void 0 ? void 0 : parent.$any;
|
69
|
-
this.$all = parent === null || parent === void 0 ? void 0 : parent.$all;
|
70
|
-
this.$not = parent === null || parent === void 0 ? void 0 : parent.$not;
|
71
|
-
this.$none = parent === null || parent === void 0 ? void 0 : parent.$none;
|
72
|
-
this.$sort = parent === null || parent === void 0 ? void 0 : parent.$sort;
|
73
|
-
Object.assign(this, data);
|
74
|
-
// These bindings are used to support deconstructing
|
75
|
-
// const { any, not, filter, sort } = xata.users.query()
|
76
|
-
this.any = this.any.bind(this);
|
77
|
-
this.all = this.all.bind(this);
|
78
|
-
this.not = this.not.bind(this);
|
79
|
-
this.filter = this.filter.bind(this);
|
80
|
-
this.sort = this.sort.bind(this);
|
81
|
-
this.none = this.none.bind(this);
|
82
|
-
Object.defineProperty(this, 'table', { enumerable: false });
|
83
|
-
Object.defineProperty(this, 'repository', { enumerable: false });
|
84
|
-
}
|
85
|
-
any(...queries) {
|
86
|
-
return new Query(this.repository, this.table, {
|
87
|
-
$any: (this.$any || []).concat(queries)
|
88
|
-
}, this);
|
89
|
-
}
|
90
|
-
all(...queries) {
|
91
|
-
return new Query(this.repository, this.table, {
|
92
|
-
$all: (this.$all || []).concat(queries)
|
93
|
-
}, this);
|
94
|
-
}
|
95
|
-
not(...queries) {
|
96
|
-
return new Query(this.repository, this.table, {
|
97
|
-
$not: (this.$not || []).concat(queries)
|
98
|
-
}, this);
|
99
|
-
}
|
100
|
-
none(...queries) {
|
101
|
-
return new Query(this.repository, this.table, {
|
102
|
-
$none: (this.$none || []).concat(queries)
|
103
|
-
}, this);
|
104
|
-
}
|
105
|
-
filter(a, b) {
|
106
|
-
if (arguments.length === 1) {
|
107
|
-
const constraints = a;
|
108
|
-
const queries = [];
|
109
|
-
for (const [column, constraint] of Object.entries(constraints)) {
|
110
|
-
queries.push({ [column]: constraint });
|
111
|
-
}
|
112
|
-
return new Query(this.repository, this.table, {
|
113
|
-
$all: (this.$all || []).concat(queries)
|
114
|
-
}, this);
|
115
|
-
}
|
116
|
-
else {
|
117
|
-
const column = a;
|
118
|
-
const value = b;
|
119
|
-
return new Query(this.repository, this.table, {
|
120
|
-
$all: (this.$all || []).concat({ [column]: value })
|
121
|
-
}, this);
|
122
|
-
}
|
123
|
-
}
|
124
|
-
sort(column, direction) {
|
125
|
-
const sort = Object.assign(Object.assign({}, this.$sort), { [column]: direction });
|
126
|
-
const q = new Query(this.repository, this.table, {
|
127
|
-
$sort: sort
|
128
|
-
}, this);
|
129
|
-
return q;
|
130
|
-
}
|
131
|
-
// TODO: pagination. Maybe implement different methods for different type of paginations
|
132
|
-
// and one to simply get the first records returned by the query with no pagination.
|
133
|
-
getMany(options) {
|
134
|
-
return __awaiter(this, void 0, void 0, function* () {
|
135
|
-
// TODO: use options
|
136
|
-
return this.repository.query(this);
|
137
|
-
});
|
138
|
-
}
|
139
|
-
getOne(options) {
|
140
|
-
return __awaiter(this, void 0, void 0, function* () {
|
141
|
-
// TODO: use options
|
142
|
-
const arr = yield this.getMany(); // TODO, limit to 1
|
143
|
-
return arr[0] || null;
|
144
|
-
});
|
145
|
-
}
|
146
|
-
deleteAll() {
|
147
|
-
return __awaiter(this, void 0, void 0, function* () {
|
148
|
-
// Return number of affected rows
|
149
|
-
return 0;
|
150
|
-
});
|
151
|
-
}
|
152
|
-
include(columns) {
|
153
|
-
// TODO
|
154
|
-
return this;
|
155
|
-
}
|
156
|
-
}
|
157
|
-
exports.Query = Query;
|
158
|
-
class Repository extends Query {
|
159
|
-
select(...columns) {
|
160
|
-
return new Query(this.repository, this.table, {});
|
161
|
-
}
|
162
|
-
}
|
163
|
-
exports.Repository = Repository;
|
164
|
-
class RestRepository extends Repository {
|
165
|
-
constructor(client, table) {
|
166
|
-
super(null, table, {});
|
167
|
-
this.client = client;
|
168
|
-
const { fetch } = client.options;
|
169
|
-
if (fetch) {
|
170
|
-
this.fetch = fetch;
|
171
|
-
}
|
172
|
-
else if (typeof window === 'object') {
|
173
|
-
this.fetch = window.fetch;
|
174
|
-
}
|
175
|
-
else if (typeof require === 'function') {
|
176
|
-
try {
|
177
|
-
this.fetch = require('node-fetch');
|
178
|
-
}
|
179
|
-
catch (err) {
|
180
|
-
try {
|
181
|
-
this.fetch = require('cross-fetch');
|
182
|
-
}
|
183
|
-
catch (err) {
|
184
|
-
throw new Error('No fetch implementation found. Please provide one in the constructor');
|
185
|
-
}
|
186
|
-
}
|
187
|
-
}
|
188
|
-
Object.defineProperty(this, 'client', { enumerable: false });
|
189
|
-
Object.defineProperty(this, 'fetch', { enumerable: false });
|
190
|
-
Object.defineProperty(this, 'hostname', { enumerable: false });
|
191
|
-
}
|
192
|
-
request(method, path, body) {
|
193
|
-
return __awaiter(this, void 0, void 0, function* () {
|
194
|
-
const { databaseURL, apiKey } = this.client.options;
|
195
|
-
const branch = yield this.client.getBranch();
|
196
|
-
const resp = yield this.fetch(`${databaseURL}:${branch}${path}`, {
|
197
|
-
method,
|
198
|
-
headers: {
|
199
|
-
Accept: '*/*',
|
200
|
-
'Content-Type': 'application/json',
|
201
|
-
Authorization: `Bearer ${apiKey}`
|
202
|
-
},
|
203
|
-
body: JSON.stringify(body)
|
204
|
-
});
|
205
|
-
if (!resp.ok) {
|
206
|
-
try {
|
207
|
-
const json = yield resp.json();
|
208
|
-
const message = json.message;
|
209
|
-
if (typeof message === 'string') {
|
210
|
-
throw new XataError(message, resp.status);
|
211
|
-
}
|
212
|
-
}
|
213
|
-
catch (err) {
|
214
|
-
if (err instanceof XataError)
|
215
|
-
throw err;
|
216
|
-
// Ignore errors for other reasons.
|
217
|
-
// For example if the response's body cannot be parsed as JSON
|
218
|
-
}
|
219
|
-
throw new XataError(resp.statusText, resp.status);
|
220
|
-
}
|
221
|
-
if (resp.status === 204)
|
222
|
-
return;
|
223
|
-
return resp.json();
|
224
|
-
});
|
225
|
-
}
|
226
|
-
select(...columns) {
|
227
|
-
return new Query(this.repository, this.table, {});
|
228
|
-
}
|
229
|
-
create(object) {
|
230
|
-
return __awaiter(this, void 0, void 0, function* () {
|
231
|
-
const body = Object.assign({}, object);
|
232
|
-
for (const key of Object.keys(body)) {
|
233
|
-
const value = body[key];
|
234
|
-
if (value && typeof value === 'object' && typeof value.id === 'string') {
|
235
|
-
body[key] = value.id;
|
236
|
-
}
|
237
|
-
}
|
238
|
-
const obj = yield this.request('POST', `/tables/${this.table}/data`, body);
|
239
|
-
return this.client.initObject(this.table, obj);
|
240
|
-
});
|
241
|
-
}
|
242
|
-
read(id) {
|
243
|
-
return __awaiter(this, void 0, void 0, function* () {
|
244
|
-
try {
|
245
|
-
const obj = yield this.request('GET', `/tables/${this.table}/data/${id}`);
|
246
|
-
return this.client.initObject(this.table, obj);
|
247
|
-
}
|
248
|
-
catch (err) {
|
249
|
-
if (err.status === 404)
|
250
|
-
return null;
|
251
|
-
throw err;
|
252
|
-
}
|
253
|
-
});
|
254
|
-
}
|
255
|
-
update(id, object) {
|
256
|
-
return __awaiter(this, void 0, void 0, function* () {
|
257
|
-
const obj = yield this.request('PUT', `/tables/${this.table}/data/${id}`, object);
|
258
|
-
return this.client.initObject(this.table, obj);
|
259
|
-
});
|
260
|
-
}
|
261
|
-
delete(id) {
|
262
|
-
return __awaiter(this, void 0, void 0, function* () {
|
263
|
-
yield this.request('DELETE', `/tables/${this.table}/data/${id}`);
|
264
|
-
});
|
265
|
-
}
|
266
|
-
query(query) {
|
267
|
-
return __awaiter(this, void 0, void 0, function* () {
|
268
|
-
const filter = {
|
269
|
-
$any: query.$any,
|
270
|
-
$all: query.$all,
|
271
|
-
$not: query.$not,
|
272
|
-
$none: query.$none
|
273
|
-
};
|
274
|
-
const body = {
|
275
|
-
filter: Object.values(filter).some(Boolean) ? filter : undefined,
|
276
|
-
sort: query.$sort
|
277
|
-
};
|
278
|
-
const result = yield this.request('POST', `/tables/${this.table}/query`, body);
|
279
|
-
return result.records.map((record) => this.client.initObject(this.table, record));
|
280
|
-
});
|
281
|
-
}
|
282
|
-
}
|
283
|
-
exports.RestRepository = RestRepository;
|
284
|
-
class RestRespositoryFactory {
|
285
|
-
createRepository(client, table) {
|
286
|
-
return new RestRepository(client, table);
|
287
|
-
}
|
288
|
-
}
|
289
|
-
exports.RestRespositoryFactory = RestRespositoryFactory;
|
290
|
-
class BaseClient {
|
291
|
-
constructor(options, links) {
|
292
|
-
if (!options.databaseURL || !options.apiKey || !options.branch) {
|
293
|
-
throw new Error('Options databaseURL, apiKey and branch are required');
|
294
|
-
}
|
295
|
-
this.options = options;
|
296
|
-
this.links = links;
|
297
|
-
}
|
298
|
-
initObject(table, object) {
|
299
|
-
const o = {};
|
300
|
-
Object.assign(o, object);
|
301
|
-
const tableLinks = this.links[table] || [];
|
302
|
-
for (const link of tableLinks) {
|
303
|
-
const [field, linkTable] = link;
|
304
|
-
const value = o[field];
|
305
|
-
if (value && typeof value === 'object') {
|
306
|
-
const { id } = value;
|
307
|
-
if (Object.keys(value).find((col) => col === 'id')) {
|
308
|
-
o[field] = this.initObject(linkTable, value);
|
309
|
-
}
|
310
|
-
else if (id) {
|
311
|
-
o[field] = {
|
312
|
-
id,
|
313
|
-
get: () => {
|
314
|
-
this.db[linkTable].read(id);
|
315
|
-
}
|
316
|
-
};
|
317
|
-
}
|
318
|
-
}
|
319
|
-
}
|
320
|
-
const db = this.db;
|
321
|
-
o.read = function () {
|
322
|
-
return db[table].read(o['id']);
|
323
|
-
};
|
324
|
-
o.update = function (data) {
|
325
|
-
return db[table].update(o['id'], data);
|
326
|
-
};
|
327
|
-
o.delete = function () {
|
328
|
-
return db[table].delete(o['id']);
|
329
|
-
};
|
330
|
-
for (const prop of ['read', 'update', 'delete']) {
|
331
|
-
Object.defineProperty(o, prop, { enumerable: false });
|
332
|
-
}
|
333
|
-
// TODO: links and rev links
|
334
|
-
Object.freeze(o);
|
335
|
-
return o;
|
336
|
-
}
|
337
|
-
getBranch() {
|
338
|
-
var e_1, _a;
|
339
|
-
return __awaiter(this, void 0, void 0, function* () {
|
340
|
-
if (this.branch)
|
341
|
-
return this.branch;
|
342
|
-
const { branch: param } = this.options;
|
343
|
-
const strategies = Array.isArray(param) ? [...param] : [param];
|
344
|
-
const evaluateBranch = (strategy) => __awaiter(this, void 0, void 0, function* () {
|
345
|
-
return isBranchStrategyBuilder(strategy) ? yield strategy() : strategy;
|
346
|
-
});
|
347
|
-
try {
|
348
|
-
for (var strategies_1 = __asyncValues(strategies), strategies_1_1; strategies_1_1 = yield strategies_1.next(), !strategies_1_1.done;) {
|
349
|
-
const strategy = strategies_1_1.value;
|
350
|
-
const branch = yield evaluateBranch(strategy);
|
351
|
-
if (branch) {
|
352
|
-
this.branch = branch;
|
353
|
-
return branch;
|
354
|
-
}
|
355
|
-
}
|
356
|
-
}
|
357
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
358
|
-
finally {
|
359
|
-
try {
|
360
|
-
if (strategies_1_1 && !strategies_1_1.done && (_a = strategies_1.return)) yield _a.call(strategies_1);
|
361
|
-
}
|
362
|
-
finally { if (e_1) throw e_1.error; }
|
363
|
-
}
|
364
|
-
throw new Error('Unable to resolve branch value');
|
365
|
-
});
|
366
|
-
}
|
367
|
-
}
|
368
|
-
exports.BaseClient = BaseClient;
|
17
|
+
exports.XataError = void 0;
|
369
18
|
class XataError extends Error {
|
370
19
|
constructor(message, status) {
|
371
20
|
super(message);
|
@@ -373,6 +22,5 @@ class XataError extends Error {
|
|
373
22
|
}
|
374
23
|
}
|
375
24
|
exports.XataError = XataError;
|
376
|
-
|
377
|
-
|
378
|
-
};
|
25
|
+
__exportStar(require("./api"), exports);
|
26
|
+
__exportStar(require("./schema"), exports);
|
@@ -0,0 +1,4 @@
|
|
1
|
+
import { FetcherExtraProps } from '../api/fetcher';
|
2
|
+
export declare function getBranch(fetchProps: Omit<FetcherExtraProps, 'workspacesApiUrl'>): Promise<string | undefined>;
|
3
|
+
export declare function getDatabaseUrl(): string | undefined;
|
4
|
+
export declare function getAPIKey(): string | undefined;
|
@@ -0,0 +1,83 @@
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
12
|
+
exports.getAPIKey = exports.getDatabaseUrl = exports.getBranch = void 0;
|
13
|
+
const api_1 = require("../api");
|
14
|
+
const environment_1 = require("../util/environment");
|
15
|
+
const lang_1 = require("../util/lang");
|
16
|
+
const envBranchNames = [
|
17
|
+
'XATA_BRANCH',
|
18
|
+
'VERCEL_GIT_COMMIT_REF',
|
19
|
+
'CF_PAGES_BRANCH',
|
20
|
+
'BRANCH' // Netlify. Putting it the last one because it is more ambiguous
|
21
|
+
];
|
22
|
+
function getBranch(fetchProps) {
|
23
|
+
return __awaiter(this, void 0, void 0, function* () {
|
24
|
+
const env = getBranchByEnvVariable();
|
25
|
+
if (env)
|
26
|
+
return env;
|
27
|
+
const branch = yield (0, environment_1.getGitBranch)();
|
28
|
+
if (!branch)
|
29
|
+
return undefined;
|
30
|
+
// TODO: in the future, call /resolve endpoint
|
31
|
+
// For now, call API to see if the branch exists. If not, use a default value.
|
32
|
+
const [protocol, , host, , database] = fetchProps.apiUrl.split('/');
|
33
|
+
const [workspace] = host.split('.');
|
34
|
+
const dbBranchName = `${database}:${branch}`;
|
35
|
+
try {
|
36
|
+
yield (0, api_1.getBranchDetails)(Object.assign(Object.assign({}, fetchProps), { workspacesApiUrl: `${protocol}//${host}`, pathParams: {
|
37
|
+
dbBranchName,
|
38
|
+
workspace
|
39
|
+
} }));
|
40
|
+
}
|
41
|
+
catch (err) {
|
42
|
+
if ((0, lang_1.isObject)(err) && err.status === 404)
|
43
|
+
return 'main';
|
44
|
+
throw err;
|
45
|
+
}
|
46
|
+
return branch;
|
47
|
+
});
|
48
|
+
}
|
49
|
+
exports.getBranch = getBranch;
|
50
|
+
function getBranchByEnvVariable() {
|
51
|
+
for (const name of envBranchNames) {
|
52
|
+
const value = (0, environment_1.getEnvVariable)(name);
|
53
|
+
if (value) {
|
54
|
+
return value;
|
55
|
+
}
|
56
|
+
}
|
57
|
+
try {
|
58
|
+
return XATA_BRANCH;
|
59
|
+
}
|
60
|
+
catch (err) {
|
61
|
+
// Ignore ReferenceError. Only CloudFlare workers set env variables as global variables
|
62
|
+
}
|
63
|
+
}
|
64
|
+
function getDatabaseUrl() {
|
65
|
+
var _a;
|
66
|
+
try {
|
67
|
+
return (_a = (0, environment_1.getEnvVariable)('XATA_DATABASE_URL')) !== null && _a !== void 0 ? _a : XATA_DATABASE_URL;
|
68
|
+
}
|
69
|
+
catch (err) {
|
70
|
+
return undefined;
|
71
|
+
}
|
72
|
+
}
|
73
|
+
exports.getDatabaseUrl = getDatabaseUrl;
|
74
|
+
function getAPIKey() {
|
75
|
+
var _a;
|
76
|
+
try {
|
77
|
+
return (_a = (0, environment_1.getEnvVariable)('XATA_API_KEY')) !== null && _a !== void 0 ? _a : XATA_API_KEY;
|
78
|
+
}
|
79
|
+
catch (err) {
|
80
|
+
return undefined;
|
81
|
+
}
|
82
|
+
}
|
83
|
+
exports.getAPIKey = getAPIKey;
|