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