@xata.io/client 0.0.0-beta.d0bc5ad → 0.0.0-beta.f12621e
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +16 -87
- package/dist/index.js +87 -178
- 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 +42 -0
- package/dist/schema/pagination.js +44 -0
- package/dist/schema/query.d.ts +43 -0
- package/dist/schema/query.js +188 -0
- package/dist/schema/selection.d.ts +18 -0
- package/dist/schema/selection.js +2 -0
- package/dist/util/errors.d.ts +3 -0
- package/dist/util/errors.js +9 -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,6 @@
|
|
1
|
+
import { Page } from './schema/pagination';
|
2
|
+
import { Query, QueryOptions } from './schema/query';
|
3
|
+
import { Selectable, SelectableColumn, Select } from './schema/selection';
|
1
4
|
export interface XataRecord {
|
2
5
|
id: string;
|
3
6
|
xata: {
|
@@ -7,107 +10,33 @@ export interface XataRecord {
|
|
7
10
|
update(data: Selectable<this>): Promise<this>;
|
8
11
|
delete(): Promise<void>;
|
9
12
|
}
|
10
|
-
export declare
|
11
|
-
|
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>>;
|
13
|
+
export declare abstract class Repository<T extends XataRecord> extends Query<T> {
|
14
|
+
select<K extends SelectableColumn<T>>(columns: K[]): Query<T, Select<T, K>>;
|
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
|
20
|
+
abstract _runQuery<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> {
|
22
|
+
export declare class RestRepository<T extends XataRecord> extends Repository<T> {
|
95
23
|
client: BaseClient<any>;
|
96
24
|
fetch: any;
|
97
25
|
constructor(client: BaseClient<any>, table: string);
|
98
|
-
request(method: string, path: string, body?: unknown): Promise<
|
99
|
-
select<K extends
|
26
|
+
request<T>(method: string, path: string, body?: unknown): Promise<T | undefined>;
|
27
|
+
select<K extends SelectableColumn<T>>(columns: K[]): Query<T, Select<T, K>>;
|
100
28
|
create(object: T): Promise<T>;
|
29
|
+
createMany(objects: T[]): Promise<T[]>;
|
101
30
|
read(id: string): Promise<T | null>;
|
102
31
|
update(id: string, object: Partial<T>): Promise<T>;
|
103
32
|
delete(id: string): Promise<void>;
|
104
|
-
|
33
|
+
_runQuery<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
34
|
}
|
106
35
|
interface RepositoryFactory {
|
107
|
-
createRepository<T>(client: BaseClient<any>, table: string): Repository<T>;
|
36
|
+
createRepository<T extends XataRecord>(client: BaseClient<any>, table: string): Repository<T>;
|
108
37
|
}
|
109
38
|
export declare class RestRespositoryFactory implements RepositoryFactory {
|
110
|
-
createRepository<T>(client: BaseClient<any>, table: string): Repository<T>;
|
39
|
+
createRepository<T extends XataRecord>(client: BaseClient<any>, table: string): Repository<T>;
|
111
40
|
}
|
112
41
|
declare type BranchStrategyValue = string | undefined | null;
|
113
42
|
declare type BranchStrategyBuilder = () => BranchStrategyValue | Promise<BranchStrategyValue>;
|
@@ -115,7 +44,7 @@ declare type BranchStrategy = BranchStrategyValue | BranchStrategyBuilder;
|
|
115
44
|
declare type BranchStrategyOption = NonNullable<BranchStrategy | BranchStrategy[]>;
|
116
45
|
export declare type XataClientOptions = {
|
117
46
|
fetch?: unknown;
|
118
|
-
databaseURL
|
47
|
+
databaseURL?: string;
|
119
48
|
branch: BranchStrategyOption;
|
120
49
|
apiKey: string;
|
121
50
|
repositoryFactory?: RepositoryFactory;
|
@@ -134,4 +63,4 @@ export declare class XataError extends Error {
|
|
134
63
|
constructor(message: string, status: number);
|
135
64
|
}
|
136
65
|
export declare type Links = Record<string, Array<string[]>>;
|
137
|
-
export
|
66
|
+
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) {
|
@@ -16,148 +26,14 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
16
26
|
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
17
27
|
};
|
18
28
|
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
|
-
|
26
|
-
|
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, {});
|
29
|
+
exports.XataError = exports.BaseClient = exports.RestRespositoryFactory = exports.RestRepository = exports.Repository = void 0;
|
30
|
+
const filters_1 = require("./schema/filters");
|
31
|
+
const pagination_1 = require("./schema/pagination");
|
32
|
+
const query_1 = require("./schema/query");
|
33
|
+
const errors_1 = require("./util/errors");
|
34
|
+
class Repository extends query_1.Query {
|
35
|
+
select(columns) {
|
36
|
+
return new query_1.Query(this.repository, this.table, { columns });
|
161
37
|
}
|
162
38
|
}
|
163
39
|
exports.Repository = Repository;
|
@@ -165,25 +41,16 @@ class RestRepository extends Repository {
|
|
165
41
|
constructor(client, table) {
|
166
42
|
super(null, table, {});
|
167
43
|
this.client = client;
|
168
|
-
const
|
169
|
-
|
44
|
+
const doWeHaveFetch = typeof fetch !== 'undefined';
|
45
|
+
const isInjectedFetchProblematic = !this.client.options.fetch;
|
46
|
+
if (doWeHaveFetch) {
|
170
47
|
this.fetch = fetch;
|
171
48
|
}
|
172
|
-
else if (
|
173
|
-
|
49
|
+
else if (isInjectedFetchProblematic) {
|
50
|
+
throw new Error(errors_1.errors.falsyFetchImplementation);
|
174
51
|
}
|
175
|
-
else
|
176
|
-
|
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
|
-
}
|
52
|
+
else {
|
53
|
+
this.fetch = this.client.options.fetch;
|
187
54
|
}
|
188
55
|
Object.defineProperty(this, 'client', { enumerable: false });
|
189
56
|
Object.defineProperty(this, 'fetch', { enumerable: false });
|
@@ -193,7 +60,8 @@ class RestRepository extends Repository {
|
|
193
60
|
return __awaiter(this, void 0, void 0, function* () {
|
194
61
|
const { databaseURL, apiKey } = this.client.options;
|
195
62
|
const branch = yield this.client.getBranch();
|
196
|
-
const
|
63
|
+
const fetchImpl = this.fetch;
|
64
|
+
const resp = yield fetchImpl(`${databaseURL}:${branch}${path}`, {
|
197
65
|
method,
|
198
66
|
headers: {
|
199
67
|
Accept: '*/*',
|
@@ -219,31 +87,49 @@ class RestRepository extends Repository {
|
|
219
87
|
throw new XataError(resp.statusText, resp.status);
|
220
88
|
}
|
221
89
|
if (resp.status === 204)
|
222
|
-
return;
|
90
|
+
return undefined;
|
223
91
|
return resp.json();
|
224
92
|
});
|
225
93
|
}
|
226
|
-
select(
|
227
|
-
return new Query(this.repository, this.table, {});
|
94
|
+
select(columns) {
|
95
|
+
return new query_1.Query(this.repository, this.table, { columns });
|
228
96
|
}
|
229
97
|
create(object) {
|
230
98
|
return __awaiter(this, void 0, void 0, function* () {
|
231
|
-
const
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
body[key] = value.id;
|
236
|
-
}
|
99
|
+
const record = transformObjectLinks(object);
|
100
|
+
const response = yield this.request('POST', `/tables/${this.table}/data`, record);
|
101
|
+
if (!response) {
|
102
|
+
throw new Error("The server didn't return any data for the query");
|
237
103
|
}
|
238
|
-
const
|
239
|
-
|
104
|
+
const finalObject = yield this.read(response.id);
|
105
|
+
if (!finalObject) {
|
106
|
+
throw new Error('The server failed to save the record');
|
107
|
+
}
|
108
|
+
return finalObject;
|
109
|
+
});
|
110
|
+
}
|
111
|
+
createMany(objects) {
|
112
|
+
return __awaiter(this, void 0, void 0, function* () {
|
113
|
+
const records = objects.map((object) => transformObjectLinks(object));
|
114
|
+
const response = yield this.request('POST', `/tables/${this.table}/bulk`, { records });
|
115
|
+
if (!response) {
|
116
|
+
throw new Error("The server didn't return any data for the query");
|
117
|
+
}
|
118
|
+
// TODO: Use filer.$any() to get all the records
|
119
|
+
const finalObjects = yield Promise.all(response.recordIDs.map((id) => this.read(id)));
|
120
|
+
if (finalObjects.some((object) => !object)) {
|
121
|
+
throw new Error('The server failed to save the record');
|
122
|
+
}
|
123
|
+
return finalObjects;
|
240
124
|
});
|
241
125
|
}
|
242
126
|
read(id) {
|
243
127
|
return __awaiter(this, void 0, void 0, function* () {
|
244
128
|
try {
|
245
|
-
const
|
246
|
-
|
129
|
+
const response = yield this.request('GET', `/tables/${this.table}/data/${id}`);
|
130
|
+
if (!response)
|
131
|
+
return null;
|
132
|
+
return this.client.initObject(this.table, response);
|
247
133
|
}
|
248
134
|
catch (err) {
|
249
135
|
if (err.status === 404)
|
@@ -254,8 +140,12 @@ class RestRepository extends Repository {
|
|
254
140
|
}
|
255
141
|
update(id, object) {
|
256
142
|
return __awaiter(this, void 0, void 0, function* () {
|
257
|
-
const
|
258
|
-
|
143
|
+
const response = yield this.request('PUT', `/tables/${this.table}/data/${id}`, object);
|
144
|
+
if (!response) {
|
145
|
+
throw new Error("The server didn't return any data for the query");
|
146
|
+
}
|
147
|
+
// TODO: Review this, not sure we are properly initializing the object
|
148
|
+
return this.client.initObject(this.table, response);
|
259
149
|
});
|
260
150
|
}
|
261
151
|
delete(id) {
|
@@ -263,7 +153,8 @@ class RestRepository extends Repository {
|
|
263
153
|
yield this.request('DELETE', `/tables/${this.table}/data/${id}`);
|
264
154
|
});
|
265
155
|
}
|
266
|
-
|
156
|
+
_runQuery(query, options) {
|
157
|
+
var _a, _b;
|
267
158
|
return __awaiter(this, void 0, void 0, function* () {
|
268
159
|
const filter = {
|
269
160
|
$any: query.$any,
|
@@ -273,10 +164,18 @@ class RestRepository extends Repository {
|
|
273
164
|
};
|
274
165
|
const body = {
|
275
166
|
filter: Object.values(filter).some(Boolean) ? filter : undefined,
|
276
|
-
sort: query.$sort
|
167
|
+
sort: (_a = (0, filters_1.buildSortFilter)(options === null || options === void 0 ? void 0 : options.sort)) !== null && _a !== void 0 ? _a : query.$sort,
|
168
|
+
page: options === null || options === void 0 ? void 0 : options.page,
|
169
|
+
columns: (_b = options === null || options === void 0 ? void 0 : options.columns) !== null && _b !== void 0 ? _b : query.columns
|
277
170
|
};
|
278
|
-
const
|
279
|
-
|
171
|
+
const response = yield this.request('POST', `/tables/${this.table}/query`, body);
|
172
|
+
if (!response) {
|
173
|
+
throw new Error("The server didn't return any data for the query");
|
174
|
+
}
|
175
|
+
const { meta, records: objects } = response;
|
176
|
+
const records = objects.map((record) => this.client.initObject(this.table, record));
|
177
|
+
// TODO: We should properly type this any
|
178
|
+
return new pagination_1.Page(query, meta, records);
|
280
179
|
});
|
281
180
|
}
|
282
181
|
}
|
@@ -376,3 +275,13 @@ exports.XataError = XataError;
|
|
376
275
|
const isBranchStrategyBuilder = (strategy) => {
|
377
276
|
return typeof strategy === 'function';
|
378
277
|
};
|
278
|
+
// TODO: We can find a better implementation for links
|
279
|
+
const transformObjectLinks = (object) => {
|
280
|
+
return Object.entries(object).reduce((acc, [key, value]) => {
|
281
|
+
if (value && typeof value === 'object' && typeof value.id === 'string') {
|
282
|
+
return Object.assign(Object.assign({}, acc), { [key]: value.id });
|
283
|
+
}
|
284
|
+
return Object.assign(Object.assign({}, acc), { [key]: value });
|
285
|
+
}, {});
|
286
|
+
};
|
287
|
+
__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';
|
@@ -0,0 +1,13 @@
|
|
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
|
+
};
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
13
|
+
__exportStar(require("./operators"), exports);
|
@@ -0,0 +1,21 @@
|
|
1
|
+
import { Constraint } from './filters';
|
2
|
+
declare type ComparableType = number | Date;
|
3
|
+
export declare const gt: <T extends ComparableType>(value: T) => Constraint<T>;
|
4
|
+
export declare const ge: <T extends ComparableType>(value: T) => Constraint<T>;
|
5
|
+
export declare const gte: <T extends ComparableType>(value: T) => Constraint<T>;
|
6
|
+
export declare const lt: <T extends ComparableType>(value: T) => Constraint<T>;
|
7
|
+
export declare const lte: <T extends ComparableType>(value: T) => Constraint<T>;
|
8
|
+
export declare const le: <T extends ComparableType>(value: T) => Constraint<T>;
|
9
|
+
export declare const exists: (column: string) => Constraint<string>;
|
10
|
+
export declare const notExists: (column: string) => Constraint<string>;
|
11
|
+
export declare const startsWith: (value: string) => Constraint<string>;
|
12
|
+
export declare const endsWith: (value: string) => Constraint<string>;
|
13
|
+
export declare const pattern: (value: string) => Constraint<string>;
|
14
|
+
export declare const is: <T>(value: T) => Constraint<T>;
|
15
|
+
export declare const isNot: <T>(value: T) => Constraint<T>;
|
16
|
+
export declare const contains: <T>(value: T) => Constraint<T>;
|
17
|
+
export declare const includes: (value: string) => Constraint<string>;
|
18
|
+
export declare const includesSubstring: (value: string) => Constraint<string>;
|
19
|
+
export declare const includesPattern: (value: string) => Constraint<string>;
|
20
|
+
export declare const includesAll: (value: string) => Constraint<string>;
|
21
|
+
export {};
|
@@ -0,0 +1,40 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.includesAll = exports.includesPattern = exports.includesSubstring = exports.includes = exports.contains = exports.isNot = exports.is = exports.pattern = exports.endsWith = exports.startsWith = exports.notExists = exports.exists = exports.le = exports.lte = exports.lt = exports.gte = exports.ge = exports.gt = void 0;
|
4
|
+
const gt = (value) => ({ $gt: value });
|
5
|
+
exports.gt = gt;
|
6
|
+
const ge = (value) => ({ $ge: value });
|
7
|
+
exports.ge = ge;
|
8
|
+
const gte = (value) => ({ $ge: value });
|
9
|
+
exports.gte = gte;
|
10
|
+
const lt = (value) => ({ $lt: value });
|
11
|
+
exports.lt = lt;
|
12
|
+
const lte = (value) => ({ $le: value });
|
13
|
+
exports.lte = lte;
|
14
|
+
const le = (value) => ({ $le: value });
|
15
|
+
exports.le = le;
|
16
|
+
const exists = (column) => ({ $exists: column });
|
17
|
+
exports.exists = exists;
|
18
|
+
const notExists = (column) => ({ $notExists: column });
|
19
|
+
exports.notExists = notExists;
|
20
|
+
const startsWith = (value) => ({ $startsWith: value });
|
21
|
+
exports.startsWith = startsWith;
|
22
|
+
const endsWith = (value) => ({ $endsWith: value });
|
23
|
+
exports.endsWith = endsWith;
|
24
|
+
const pattern = (value) => ({ $pattern: value });
|
25
|
+
exports.pattern = pattern;
|
26
|
+
const is = (value) => ({ $is: value });
|
27
|
+
exports.is = is;
|
28
|
+
const isNot = (value) => ({ $isNot: value });
|
29
|
+
exports.isNot = isNot;
|
30
|
+
const contains = (value) => ({ $contains: value });
|
31
|
+
exports.contains = contains;
|
32
|
+
// TODO: these can only be applied to columns of type "multiple"
|
33
|
+
const includes = (value) => ({ $includes: value });
|
34
|
+
exports.includes = includes;
|
35
|
+
const includesSubstring = (value) => ({ $includesSubstring: value });
|
36
|
+
exports.includesSubstring = includesSubstring;
|
37
|
+
const includesPattern = (value) => ({ $includesPattern: value });
|
38
|
+
exports.includesPattern = includesPattern;
|
39
|
+
const includesAll = (value) => ({ $includesAll: value });
|
40
|
+
exports.includesAll = includesAll;
|
@@ -0,0 +1,42 @@
|
|
1
|
+
import { XataRecord } from '..';
|
2
|
+
import { Query } from './query';
|
3
|
+
export declare type PaginationQueryMeta = {
|
4
|
+
page: {
|
5
|
+
cursor: string;
|
6
|
+
more: boolean;
|
7
|
+
};
|
8
|
+
};
|
9
|
+
export interface BasePage<T extends XataRecord, R extends XataRecord> {
|
10
|
+
query: Query<T, R>;
|
11
|
+
meta: PaginationQueryMeta;
|
12
|
+
records: R[];
|
13
|
+
nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
14
|
+
previousPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
15
|
+
firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
16
|
+
lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
17
|
+
hasNextPage(): boolean;
|
18
|
+
}
|
19
|
+
export declare class Page<T extends XataRecord, R extends XataRecord> implements BasePage<T, R> {
|
20
|
+
readonly query: Query<T, R>;
|
21
|
+
readonly meta: PaginationQueryMeta;
|
22
|
+
readonly records: R[];
|
23
|
+
constructor(query: Query<T, R>, meta: PaginationQueryMeta, records?: R[]);
|
24
|
+
nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
25
|
+
previousPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
26
|
+
firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
27
|
+
lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
28
|
+
hasNextPage(): boolean;
|
29
|
+
}
|
30
|
+
export declare type CursorNavigationOptions = {
|
31
|
+
first?: string;
|
32
|
+
} | {
|
33
|
+
last?: string;
|
34
|
+
} | {
|
35
|
+
after?: string;
|
36
|
+
before?: string;
|
37
|
+
};
|
38
|
+
export declare type OffsetNavigationOptions = {
|
39
|
+
size?: number;
|
40
|
+
offset?: number;
|
41
|
+
};
|
42
|
+
export declare type PaginationOptions = CursorNavigationOptions & OffsetNavigationOptions;
|