@xata.io/client 0.0.0-beta.58bd26f → 0.0.0-beta.641039e
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/api/client.d.ts +93 -0
- package/dist/api/client.js +229 -0
- package/dist/api/components.d.ts +1416 -0
- package/dist/api/components.js +988 -0
- package/dist/api/fetcher.d.ts +25 -0
- package/dist/api/fetcher.js +78 -0
- package/dist/api/index.d.ts +7 -0
- package/dist/api/index.js +21 -0
- package/dist/api/parameters.d.ts +15 -0
- package/dist/api/parameters.js +2 -0
- package/dist/api/providers.d.ts +8 -0
- package/dist/api/providers.js +29 -0
- package/dist/api/responses.d.ts +44 -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 +26 -98
- package/dist/index.js +144 -239
- package/dist/schema/filters.d.ts +20 -0
- package/dist/schema/filters.js +24 -0
- package/dist/schema/index.d.ts +1 -0
- package/dist/schema/index.js +17 -0
- package/dist/schema/operators.d.ts +21 -0
- package/dist/schema/operators.js +40 -0
- package/dist/schema/pagination.d.ts +41 -0
- package/dist/schema/pagination.js +58 -0
- package/dist/schema/query.d.ts +45 -0
- package/dist/schema/query.js +190 -0
- package/dist/schema/selection.d.ts +14 -0
- package/dist/schema/selection.js +2 -0
- package/dist/util/lang.d.ts +2 -0
- package/dist/util/lang.js +10 -0
- package/dist/util/types.d.ts +3 -0
- package/dist/util/types.js +2 -0
- package/package.json +3 -3
- package/dist/index.test.d.ts +0 -1
- package/dist/index.test.js +0 -304
- package/src/index.test.ts +0 -392
- package/src/index.ts +0 -506
- package/tsconfig.json +0 -21
package/dist/index.d.ts
CHANGED
@@ -1,129 +1,56 @@
|
|
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: {
|
4
8
|
version: number;
|
5
9
|
};
|
6
10
|
read(): Promise<this>;
|
7
|
-
update(data: Selectable<this
|
11
|
+
update(data: Partial<Selectable<this>>): Promise<this>;
|
8
12
|
delete(): Promise<void>;
|
9
13
|
}
|
10
|
-
export declare
|
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>>;
|
14
|
+
export declare abstract class Repository<T extends XataRecord> extends Query<T> {
|
88
15
|
abstract create(object: Selectable<T>): Promise<T>;
|
16
|
+
abstract createMany(objects: Selectable<T>[]): Promise<T[]>;
|
89
17
|
abstract read(id: string): Promise<T | null>;
|
90
|
-
abstract update(id: string, object: Partial<T
|
18
|
+
abstract update(id: string, object: Partial<Selectable<T>>): Promise<T>;
|
19
|
+
abstract upsert(id: string, object: Selectable<T>): Promise<T>;
|
91
20
|
abstract delete(id: string): void;
|
92
|
-
abstract query<R
|
21
|
+
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>>;
|
93
22
|
}
|
94
|
-
export declare class RestRepository<T> extends Repository<T> {
|
95
|
-
|
96
|
-
fetch: any;
|
23
|
+
export declare class RestRepository<T extends XataRecord> extends Repository<T> {
|
24
|
+
#private;
|
97
25
|
constructor(client: BaseClient<any>, table: string);
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
delete(
|
104
|
-
query<R
|
26
|
+
create(object: Selectable<T>): Promise<T>;
|
27
|
+
createMany(objects: T[]): Promise<T[]>;
|
28
|
+
read(recordId: string): Promise<T | null>;
|
29
|
+
update(recordId: string, object: Partial<Selectable<T>>): Promise<T>;
|
30
|
+
upsert(recordId: string, object: Selectable<T>): Promise<T>;
|
31
|
+
delete(recordId: string): Promise<void>;
|
32
|
+
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>>;
|
105
33
|
}
|
106
34
|
interface RepositoryFactory {
|
107
|
-
createRepository<T>(client: BaseClient<any>, table: string): Repository<T>;
|
35
|
+
createRepository<T extends XataRecord>(client: BaseClient<any>, table: string): Repository<T>;
|
108
36
|
}
|
109
37
|
export declare class RestRespositoryFactory implements RepositoryFactory {
|
110
|
-
createRepository<T>(client: BaseClient<any>, table: string): Repository<T>;
|
38
|
+
createRepository<T extends XataRecord>(client: BaseClient<any>, table: string): Repository<T>;
|
111
39
|
}
|
112
40
|
declare type BranchStrategyValue = string | undefined | null;
|
113
41
|
declare type BranchStrategyBuilder = () => BranchStrategyValue | Promise<BranchStrategyValue>;
|
114
42
|
declare type BranchStrategy = BranchStrategyValue | BranchStrategyBuilder;
|
115
43
|
declare type BranchStrategyOption = NonNullable<BranchStrategy | BranchStrategy[]>;
|
116
44
|
export declare type XataClientOptions = {
|
117
|
-
fetch?:
|
118
|
-
databaseURL
|
45
|
+
fetch?: FetchImpl;
|
46
|
+
databaseURL?: string;
|
119
47
|
branch: BranchStrategyOption;
|
120
48
|
apiKey: string;
|
121
49
|
repositoryFactory?: RepositoryFactory;
|
122
50
|
};
|
123
51
|
export declare class BaseClient<D extends Record<string, Repository<any>>> {
|
52
|
+
#private;
|
124
53
|
options: XataClientOptions;
|
125
|
-
private links;
|
126
|
-
private branch;
|
127
54
|
db: D;
|
128
55
|
constructor(options: XataClientOptions, links: Links);
|
129
56
|
initObject<T>(table: string, object: object): T;
|
@@ -134,4 +61,5 @@ export declare class XataError extends Error {
|
|
134
61
|
constructor(message: string, status: number);
|
135
62
|
}
|
136
63
|
export declare type Links = Record<string, Array<string[]>>;
|
137
|
-
export
|
64
|
+
export * from './api';
|
65
|
+
export * from './schema';
|
package/dist/index.js
CHANGED
@@ -1,4 +1,18 @@
|
|
1
1
|
"use strict";
|
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);
|
15
|
+
};
|
2
16
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
17
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
18
|
return new (P || (P = Promise))(function (resolve, reject) {
|
@@ -8,6 +22,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
8
22
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
23
|
});
|
10
24
|
};
|
25
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
26
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
27
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
28
|
+
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");
|
29
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
30
|
+
};
|
31
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
32
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
33
|
+
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");
|
34
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
35
|
+
};
|
11
36
|
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
12
37
|
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
13
38
|
var m = o[Symbol.asyncIterator], i;
|
@@ -15,272 +40,138 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
15
40
|
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
41
|
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
17
42
|
};
|
43
|
+
var _RestRepository_instances, _RestRepository_client, _RestRepository_fetch, _RestRepository_table, _RestRepository_getFetchProps, _BaseClient_links, _BaseClient_branch;
|
18
44
|
Object.defineProperty(exports, "__esModule", { value: true });
|
19
|
-
exports.XataError = exports.BaseClient = exports.RestRespositoryFactory = exports.RestRepository = exports.Repository =
|
20
|
-
const
|
21
|
-
|
22
|
-
const
|
23
|
-
|
24
|
-
|
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
|
-
}
|
45
|
+
exports.XataError = exports.BaseClient = exports.RestRespositoryFactory = exports.RestRepository = exports.Repository = void 0;
|
46
|
+
const api_1 = require("./api");
|
47
|
+
const filters_1 = require("./schema/filters");
|
48
|
+
const pagination_1 = require("./schema/pagination");
|
49
|
+
const query_1 = require("./schema/query");
|
50
|
+
class Repository extends query_1.Query {
|
162
51
|
}
|
163
52
|
exports.Repository = Repository;
|
164
53
|
class RestRepository extends Repository {
|
165
54
|
constructor(client, table) {
|
166
55
|
super(null, table, {});
|
167
|
-
this
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
56
|
+
_RestRepository_instances.add(this);
|
57
|
+
_RestRepository_client.set(this, void 0);
|
58
|
+
_RestRepository_fetch.set(this, void 0);
|
59
|
+
_RestRepository_table.set(this, void 0);
|
60
|
+
__classPrivateFieldSet(this, _RestRepository_client, client, "f");
|
61
|
+
__classPrivateFieldSet(this, _RestRepository_table, table, "f");
|
62
|
+
// TODO: Remove when integrating with API client
|
63
|
+
const fetchImpl = typeof fetch !== 'undefined' ? fetch : __classPrivateFieldGet(this, _RestRepository_client, "f").options.fetch;
|
64
|
+
if (!fetchImpl) {
|
65
|
+
throw new Error(`The \`fetch\` option passed to the Xata client is resolving to a falsy value and may not be correctly imported.`);
|
174
66
|
}
|
175
|
-
|
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 });
|
67
|
+
__classPrivateFieldSet(this, _RestRepository_fetch, fetchImpl, "f");
|
191
68
|
}
|
192
|
-
|
69
|
+
create(object) {
|
193
70
|
return __awaiter(this, void 0, void 0, function* () {
|
194
|
-
const
|
195
|
-
const
|
196
|
-
const
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
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);
|
71
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
72
|
+
const record = transformObjectLinks(object);
|
73
|
+
const response = object.id
|
74
|
+
? yield (0, api_1.insertRecordWithID)(Object.assign({ pathParams: {
|
75
|
+
workspace: '{workspaceId}',
|
76
|
+
dbBranchName: '{dbBranch}',
|
77
|
+
tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"),
|
78
|
+
recordId: object.id
|
79
|
+
}, body: record }, fetchProps))
|
80
|
+
: yield (0, api_1.insertRecord)(Object.assign({ pathParams: {
|
81
|
+
workspace: '{workspaceId}',
|
82
|
+
dbBranchName: '{dbBranch}',
|
83
|
+
tableName: __classPrivateFieldGet(this, _RestRepository_table, "f")
|
84
|
+
}, body: record }, fetchProps));
|
85
|
+
const finalObject = yield this.read(response.id);
|
86
|
+
if (!finalObject) {
|
87
|
+
throw new Error('The server failed to save the record');
|
220
88
|
}
|
221
|
-
|
222
|
-
return;
|
223
|
-
return resp.json();
|
89
|
+
return finalObject;
|
224
90
|
});
|
225
91
|
}
|
226
|
-
|
227
|
-
return new Query(this.repository, this.table, {});
|
228
|
-
}
|
229
|
-
create(object) {
|
92
|
+
createMany(objects) {
|
230
93
|
return __awaiter(this, void 0, void 0, function* () {
|
231
|
-
const
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
94
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
95
|
+
const records = objects.map((object) => transformObjectLinks(object));
|
96
|
+
const response = yield (0, api_1.bulkInsertTableRecords)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f") }, body: { records } }, fetchProps));
|
97
|
+
// TODO: Use filer.$any() to get all the records
|
98
|
+
const finalObjects = yield Promise.all(response.recordIDs.map((id) => this.read(id)));
|
99
|
+
if (finalObjects.some((object) => !object)) {
|
100
|
+
throw new Error('The server failed to save the record');
|
237
101
|
}
|
238
|
-
|
239
|
-
return this.client.initObject(this.table, obj);
|
102
|
+
return finalObjects;
|
240
103
|
});
|
241
104
|
}
|
242
|
-
read(
|
105
|
+
read(recordId) {
|
243
106
|
return __awaiter(this, void 0, void 0, function* () {
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
}
|
248
|
-
catch (err) {
|
249
|
-
if (err.status === 404)
|
250
|
-
return null;
|
251
|
-
throw err;
|
252
|
-
}
|
107
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
108
|
+
const response = yield (0, api_1.getRecord)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId } }, fetchProps));
|
109
|
+
return __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), response);
|
253
110
|
});
|
254
111
|
}
|
255
|
-
update(
|
112
|
+
update(recordId, object) {
|
256
113
|
return __awaiter(this, void 0, void 0, function* () {
|
257
|
-
const
|
258
|
-
|
114
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
115
|
+
const response = yield (0, api_1.updateRecordWithID)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId }, body: object }, fetchProps));
|
116
|
+
const item = yield this.read(response.id);
|
117
|
+
if (!item)
|
118
|
+
throw new Error('The server failed to save the record');
|
119
|
+
return item;
|
259
120
|
});
|
260
121
|
}
|
261
|
-
|
122
|
+
upsert(recordId, object) {
|
262
123
|
return __awaiter(this, void 0, void 0, function* () {
|
263
|
-
yield this.
|
124
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
125
|
+
const response = yield (0, api_1.upsertRecordWithID)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId }, body: object }, fetchProps));
|
126
|
+
const item = yield this.read(response.id);
|
127
|
+
if (!item)
|
128
|
+
throw new Error('The server failed to save the record');
|
129
|
+
return item;
|
264
130
|
});
|
265
131
|
}
|
266
|
-
|
132
|
+
delete(recordId) {
|
267
133
|
return __awaiter(this, void 0, void 0, function* () {
|
268
|
-
const
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
134
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
135
|
+
yield (0, api_1.deleteRecord)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId } }, fetchProps));
|
136
|
+
});
|
137
|
+
}
|
138
|
+
query(query, options) {
|
139
|
+
var _a, _b, _c;
|
140
|
+
return __awaiter(this, void 0, void 0, function* () {
|
141
|
+
const data = query.getQueryOptions();
|
274
142
|
const body = {
|
275
|
-
filter: Object.values(filter).some(Boolean) ? filter : undefined,
|
276
|
-
sort:
|
143
|
+
filter: Object.values(data.filter).some(Boolean) ? data.filter : undefined,
|
144
|
+
sort: (_a = (0, filters_1.buildSortFilter)(options === null || options === void 0 ? void 0 : options.sort)) !== null && _a !== void 0 ? _a : data.sort,
|
145
|
+
page: (_b = options === null || options === void 0 ? void 0 : options.page) !== null && _b !== void 0 ? _b : data.page,
|
146
|
+
columns: (_c = options === null || options === void 0 ? void 0 : options.columns) !== null && _c !== void 0 ? _c : data.columns
|
277
147
|
};
|
278
|
-
const
|
279
|
-
|
148
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
149
|
+
const { meta, records: objects } = yield (0, api_1.queryTable)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f") }, body }, fetchProps));
|
150
|
+
const records = objects.map((record) => __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), record));
|
151
|
+
// TODO: We should properly type this any
|
152
|
+
return new pagination_1.Page(query, meta, records);
|
280
153
|
});
|
281
154
|
}
|
282
155
|
}
|
283
156
|
exports.RestRepository = RestRepository;
|
157
|
+
_RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _RestRepository_table = new WeakMap(), _RestRepository_instances = new WeakSet(), _RestRepository_getFetchProps = function _RestRepository_getFetchProps() {
|
158
|
+
return __awaiter(this, void 0, void 0, function* () {
|
159
|
+
const branch = yield __classPrivateFieldGet(this, _RestRepository_client, "f").getBranch();
|
160
|
+
return {
|
161
|
+
fetchImpl: __classPrivateFieldGet(this, _RestRepository_fetch, "f"),
|
162
|
+
apiKey: __classPrivateFieldGet(this, _RestRepository_client, "f").options.apiKey,
|
163
|
+
apiUrl: '',
|
164
|
+
// Instead of using workspace and dbBranch, we inject a probably CNAME'd URL
|
165
|
+
workspacesApiUrl: (path, params) => {
|
166
|
+
var _a, _b;
|
167
|
+
const baseUrl = (_a = __classPrivateFieldGet(this, _RestRepository_client, "f").options.databaseURL) !== null && _a !== void 0 ? _a : '';
|
168
|
+
const hasBranch = (_b = params.dbBranchName) !== null && _b !== void 0 ? _b : params.branch;
|
169
|
+
const newPath = path.replace(/^\/db\/[^/]+/, hasBranch ? `:${branch}` : '');
|
170
|
+
return baseUrl + newPath;
|
171
|
+
}
|
172
|
+
};
|
173
|
+
});
|
174
|
+
};
|
284
175
|
class RestRespositoryFactory {
|
285
176
|
createRepository(client, table) {
|
286
177
|
return new RestRepository(client, table);
|
@@ -289,16 +180,18 @@ class RestRespositoryFactory {
|
|
289
180
|
exports.RestRespositoryFactory = RestRespositoryFactory;
|
290
181
|
class BaseClient {
|
291
182
|
constructor(options, links) {
|
183
|
+
_BaseClient_links.set(this, void 0);
|
184
|
+
_BaseClient_branch.set(this, void 0);
|
292
185
|
if (!options.databaseURL || !options.apiKey || !options.branch) {
|
293
186
|
throw new Error('Options databaseURL, apiKey and branch are required');
|
294
187
|
}
|
295
188
|
this.options = options;
|
296
|
-
this
|
189
|
+
__classPrivateFieldSet(this, _BaseClient_links, links, "f");
|
297
190
|
}
|
298
191
|
initObject(table, object) {
|
299
192
|
const o = {};
|
300
193
|
Object.assign(o, object);
|
301
|
-
const tableLinks = this
|
194
|
+
const tableLinks = __classPrivateFieldGet(this, _BaseClient_links, "f")[table] || [];
|
302
195
|
for (const link of tableLinks) {
|
303
196
|
const [field, linkTable] = link;
|
304
197
|
const value = o[field];
|
@@ -337,8 +230,8 @@ class BaseClient {
|
|
337
230
|
getBranch() {
|
338
231
|
var e_1, _a;
|
339
232
|
return __awaiter(this, void 0, void 0, function* () {
|
340
|
-
if (this
|
341
|
-
return this
|
233
|
+
if (__classPrivateFieldGet(this, _BaseClient_branch, "f"))
|
234
|
+
return __classPrivateFieldGet(this, _BaseClient_branch, "f");
|
342
235
|
const { branch: param } = this.options;
|
343
236
|
const strategies = Array.isArray(param) ? [...param] : [param];
|
344
237
|
const evaluateBranch = (strategy) => __awaiter(this, void 0, void 0, function* () {
|
@@ -349,7 +242,7 @@ class BaseClient {
|
|
349
242
|
const strategy = strategies_1_1.value;
|
350
243
|
const branch = yield evaluateBranch(strategy);
|
351
244
|
if (branch) {
|
352
|
-
this
|
245
|
+
__classPrivateFieldSet(this, _BaseClient_branch, branch, "f");
|
353
246
|
return branch;
|
354
247
|
}
|
355
248
|
}
|
@@ -366,6 +259,7 @@ class BaseClient {
|
|
366
259
|
}
|
367
260
|
}
|
368
261
|
exports.BaseClient = BaseClient;
|
262
|
+
_BaseClient_links = new WeakMap(), _BaseClient_branch = new WeakMap();
|
369
263
|
class XataError extends Error {
|
370
264
|
constructor(message, status) {
|
371
265
|
super(message);
|
@@ -376,3 +270,14 @@ exports.XataError = XataError;
|
|
376
270
|
const isBranchStrategyBuilder = (strategy) => {
|
377
271
|
return typeof strategy === 'function';
|
378
272
|
};
|
273
|
+
// TODO: We can find a better implementation for links
|
274
|
+
const transformObjectLinks = (object) => {
|
275
|
+
return Object.entries(object).reduce((acc, [key, value]) => {
|
276
|
+
if (value && typeof value === 'object' && typeof value.id === 'string') {
|
277
|
+
return Object.assign(Object.assign({}, acc), { [key]: value.id });
|
278
|
+
}
|
279
|
+
return Object.assign(Object.assign({}, acc), { [key]: value });
|
280
|
+
}, {});
|
281
|
+
};
|
282
|
+
__exportStar(require("./api"), exports);
|
283
|
+
__exportStar(require("./schema"), exports);
|