@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
@@ -0,0 +1,44 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
12
|
+
exports.Page = void 0;
|
13
|
+
class Page {
|
14
|
+
constructor(query, meta, records = []) {
|
15
|
+
this.query = query;
|
16
|
+
this.meta = meta;
|
17
|
+
this.records = records;
|
18
|
+
}
|
19
|
+
nextPage(size, offset) {
|
20
|
+
return __awaiter(this, void 0, void 0, function* () {
|
21
|
+
return this.query.getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
|
22
|
+
});
|
23
|
+
}
|
24
|
+
previousPage(size, offset) {
|
25
|
+
return __awaiter(this, void 0, void 0, function* () {
|
26
|
+
return this.query.getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
|
27
|
+
});
|
28
|
+
}
|
29
|
+
firstPage(size, offset) {
|
30
|
+
return __awaiter(this, void 0, void 0, function* () {
|
31
|
+
return this.query.getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
|
32
|
+
});
|
33
|
+
}
|
34
|
+
lastPage(size, offset) {
|
35
|
+
return __awaiter(this, void 0, void 0, function* () {
|
36
|
+
return this.query.getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
|
37
|
+
});
|
38
|
+
}
|
39
|
+
// TODO: We need to add something on the backend if we want a hasPreviousPage
|
40
|
+
hasNextPage() {
|
41
|
+
return this.meta.page.more;
|
42
|
+
}
|
43
|
+
}
|
44
|
+
exports.Page = Page;
|
@@ -0,0 +1,43 @@
|
|
1
|
+
import { XataRecord, Repository } from '..';
|
2
|
+
import { Constraint, DeepConstraint, FilterConstraints, SortDirection, SortFilter } from './filters';
|
3
|
+
import { PaginationOptions, BasePage, PaginationQueryMeta, Page } from './pagination';
|
4
|
+
import { Selectable, SelectableColumn, Select } from './selection';
|
5
|
+
export declare type QueryOptions<T> = {
|
6
|
+
page?: PaginationOptions;
|
7
|
+
columns?: Array<keyof Selectable<T>>;
|
8
|
+
sort?: SortFilter<T> | SortFilter<T>[];
|
9
|
+
};
|
10
|
+
declare type QueryOrConstraint<T extends XataRecord, R extends XataRecord> = Query<T, R> | Constraint<T>;
|
11
|
+
export declare class Query<T extends XataRecord, R extends XataRecord = T> implements BasePage<T, R> {
|
12
|
+
table: string;
|
13
|
+
repository: Repository<T>;
|
14
|
+
readonly $any?: QueryOrConstraint<T, R>[];
|
15
|
+
readonly $all?: QueryOrConstraint<T, R>[];
|
16
|
+
readonly $not?: QueryOrConstraint<T, R>[];
|
17
|
+
readonly $none?: QueryOrConstraint<T, R>[];
|
18
|
+
readonly $sort?: Record<string, SortDirection>;
|
19
|
+
readonly columns: SelectableColumn<T>[];
|
20
|
+
readonly query: Query<T, R>;
|
21
|
+
readonly meta: PaginationQueryMeta;
|
22
|
+
readonly records: R[];
|
23
|
+
constructor(repository: Repository<T> | null, table: string, data: Partial<Query<T, R>>, parent?: Query<T, R>);
|
24
|
+
any(...queries: Query<T, R>[]): Query<T, R>;
|
25
|
+
all(...queries: Query<T, R>[]): Query<T, R>;
|
26
|
+
not(...queries: Query<T, R>[]): Query<T, R>;
|
27
|
+
none(...queries: Query<T, R>[]): Query<T, R>;
|
28
|
+
filter(constraints: FilterConstraints<T>): Query<T, R>;
|
29
|
+
filter<F extends keyof T>(column: F, value: FilterConstraints<T[F]> | DeepConstraint<T[F]>): Query<T, R>;
|
30
|
+
sort<F extends keyof T>(column: F, direction: SortDirection): Query<T, R>;
|
31
|
+
getPaginated<Options extends QueryOptions<T>>(options?: Options): Promise<Page<T, typeof options['columns'] extends SelectableColumn<T>[] ? Select<T, typeof options['columns'][number]> : R>>;
|
32
|
+
[Symbol.asyncIterator](): AsyncIterableIterator<R>;
|
33
|
+
getIterator(chunk: number, options?: Omit<QueryOptions<T>, 'page'>): AsyncGenerator<R[]>;
|
34
|
+
getMany<Options extends QueryOptions<T>>(options?: Options): Promise<(typeof options['columns'] extends SelectableColumn<T>[] ? Select<T, typeof options['columns'][number]> : R)[]>;
|
35
|
+
getOne<Options extends Omit<QueryOptions<T>, 'page'>>(options?: Options): Promise<(typeof options['columns'] extends SelectableColumn<T>[] ? Select<T, typeof options['columns'][number]> : R) | null>;
|
36
|
+
deleteAll(): Promise<number>;
|
37
|
+
nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
38
|
+
previousPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
39
|
+
firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
40
|
+
lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
41
|
+
hasNextPage(): boolean;
|
42
|
+
}
|
43
|
+
export {};
|
@@ -0,0 +1,188 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
12
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
13
|
+
var m = o[Symbol.asyncIterator], i;
|
14
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
15
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
16
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
17
|
+
};
|
18
|
+
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
|
19
|
+
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
|
20
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
21
|
+
var g = generator.apply(thisArg, _arguments || []), i, q = [];
|
22
|
+
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
|
23
|
+
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
|
24
|
+
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
|
25
|
+
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
|
26
|
+
function fulfill(value) { resume("next", value); }
|
27
|
+
function reject(value) { resume("throw", value); }
|
28
|
+
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
|
29
|
+
};
|
30
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
31
|
+
exports.Query = void 0;
|
32
|
+
class Query {
|
33
|
+
constructor(repository, table, data, parent) {
|
34
|
+
this.columns = ['*'];
|
35
|
+
// Cursor pagination
|
36
|
+
this.query = this;
|
37
|
+
this.meta = { page: { cursor: 'start', more: true } };
|
38
|
+
this.records = [];
|
39
|
+
if (repository) {
|
40
|
+
this.repository = repository;
|
41
|
+
}
|
42
|
+
else {
|
43
|
+
this.repository = this;
|
44
|
+
}
|
45
|
+
this.table = table;
|
46
|
+
// For some reason Object.assign(this, parent) didn't work in this case
|
47
|
+
// so doing all this manually:
|
48
|
+
this.$any = parent === null || parent === void 0 ? void 0 : parent.$any;
|
49
|
+
this.$all = parent === null || parent === void 0 ? void 0 : parent.$all;
|
50
|
+
this.$not = parent === null || parent === void 0 ? void 0 : parent.$not;
|
51
|
+
this.$none = parent === null || parent === void 0 ? void 0 : parent.$none;
|
52
|
+
this.$sort = parent === null || parent === void 0 ? void 0 : parent.$sort;
|
53
|
+
Object.assign(this, data);
|
54
|
+
// These bindings are used to support deconstructing
|
55
|
+
// const { any, not, filter, sort } = xata.users.query()
|
56
|
+
this.any = this.any.bind(this);
|
57
|
+
this.all = this.all.bind(this);
|
58
|
+
this.not = this.not.bind(this);
|
59
|
+
this.filter = this.filter.bind(this);
|
60
|
+
this.sort = this.sort.bind(this);
|
61
|
+
this.none = this.none.bind(this);
|
62
|
+
Object.defineProperty(this, 'table', { enumerable: false });
|
63
|
+
Object.defineProperty(this, 'repository', { enumerable: false });
|
64
|
+
}
|
65
|
+
any(...queries) {
|
66
|
+
return new Query(this.repository, this.table, {
|
67
|
+
$any: (this.$any || []).concat(queries)
|
68
|
+
}, this);
|
69
|
+
}
|
70
|
+
all(...queries) {
|
71
|
+
return new Query(this.repository, this.table, {
|
72
|
+
$all: (this.$all || []).concat(queries)
|
73
|
+
}, this);
|
74
|
+
}
|
75
|
+
not(...queries) {
|
76
|
+
return new Query(this.repository, this.table, {
|
77
|
+
$not: (this.$not || []).concat(queries)
|
78
|
+
}, this);
|
79
|
+
}
|
80
|
+
none(...queries) {
|
81
|
+
return new Query(this.repository, this.table, {
|
82
|
+
$none: (this.$none || []).concat(queries)
|
83
|
+
}, this);
|
84
|
+
}
|
85
|
+
filter(a, b) {
|
86
|
+
if (arguments.length === 1) {
|
87
|
+
const constraints = a;
|
88
|
+
const queries = [];
|
89
|
+
for (const [column, constraint] of Object.entries(constraints)) {
|
90
|
+
queries.push({ [column]: constraint });
|
91
|
+
}
|
92
|
+
return new Query(this.repository, this.table, {
|
93
|
+
$all: (this.$all || []).concat(queries)
|
94
|
+
}, this);
|
95
|
+
}
|
96
|
+
else {
|
97
|
+
const column = a;
|
98
|
+
const value = b;
|
99
|
+
return new Query(this.repository, this.table, {
|
100
|
+
$all: (this.$all || []).concat({ [column]: value })
|
101
|
+
}, this);
|
102
|
+
}
|
103
|
+
}
|
104
|
+
sort(column, direction) {
|
105
|
+
const sort = Object.assign(Object.assign({}, this.$sort), { [column]: direction });
|
106
|
+
const q = new Query(this.repository, this.table, {
|
107
|
+
$sort: sort
|
108
|
+
}, this);
|
109
|
+
return q;
|
110
|
+
}
|
111
|
+
getPaginated(options = {}) {
|
112
|
+
return __awaiter(this, void 0, void 0, function* () {
|
113
|
+
return this.repository._runQuery(this, options);
|
114
|
+
});
|
115
|
+
}
|
116
|
+
[Symbol.asyncIterator]() {
|
117
|
+
return __asyncGenerator(this, arguments, function* _a() {
|
118
|
+
var e_1, _b;
|
119
|
+
try {
|
120
|
+
for (var _c = __asyncValues(this.getIterator(1)), _d; _d = yield __await(_c.next()), !_d.done;) {
|
121
|
+
const [record] = _d.value;
|
122
|
+
yield yield __await(record);
|
123
|
+
}
|
124
|
+
}
|
125
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
126
|
+
finally {
|
127
|
+
try {
|
128
|
+
if (_d && !_d.done && (_b = _c.return)) yield __await(_b.call(_c));
|
129
|
+
}
|
130
|
+
finally { if (e_1) throw e_1.error; }
|
131
|
+
}
|
132
|
+
});
|
133
|
+
}
|
134
|
+
getIterator(chunk, options = {}) {
|
135
|
+
return __asyncGenerator(this, arguments, function* getIterator_1() {
|
136
|
+
let offset = 0;
|
137
|
+
let end = false;
|
138
|
+
while (!end) {
|
139
|
+
const { records, meta } = yield __await(this.getPaginated(Object.assign(Object.assign({}, options), { page: { size: chunk, offset } })));
|
140
|
+
yield yield __await(records);
|
141
|
+
offset += chunk;
|
142
|
+
end = !meta.page.more;
|
143
|
+
}
|
144
|
+
});
|
145
|
+
}
|
146
|
+
getMany(options = {}) {
|
147
|
+
return __awaiter(this, void 0, void 0, function* () {
|
148
|
+
const { records } = yield this.getPaginated(options);
|
149
|
+
return records;
|
150
|
+
});
|
151
|
+
}
|
152
|
+
getOne(options = {}) {
|
153
|
+
return __awaiter(this, void 0, void 0, function* () {
|
154
|
+
const records = yield this.getMany(Object.assign(Object.assign({}, options), { page: { size: 1 } }));
|
155
|
+
return records[0] || null;
|
156
|
+
});
|
157
|
+
}
|
158
|
+
deleteAll() {
|
159
|
+
return __awaiter(this, void 0, void 0, function* () {
|
160
|
+
// TODO: Return number of affected rows
|
161
|
+
return 0;
|
162
|
+
});
|
163
|
+
}
|
164
|
+
nextPage(size, offset) {
|
165
|
+
return __awaiter(this, void 0, void 0, function* () {
|
166
|
+
return this.firstPage(size, offset);
|
167
|
+
});
|
168
|
+
}
|
169
|
+
previousPage(size, offset) {
|
170
|
+
return __awaiter(this, void 0, void 0, function* () {
|
171
|
+
return this.firstPage(size, offset);
|
172
|
+
});
|
173
|
+
}
|
174
|
+
firstPage(size, offset) {
|
175
|
+
return __awaiter(this, void 0, void 0, function* () {
|
176
|
+
return this.getPaginated({ page: { size, offset } });
|
177
|
+
});
|
178
|
+
}
|
179
|
+
lastPage(size, offset) {
|
180
|
+
return __awaiter(this, void 0, void 0, function* () {
|
181
|
+
return this.getPaginated({ page: { size, offset, before: 'end' } });
|
182
|
+
});
|
183
|
+
}
|
184
|
+
hasNextPage() {
|
185
|
+
return this.meta.page.more;
|
186
|
+
}
|
187
|
+
}
|
188
|
+
exports.Query = Query;
|
@@ -0,0 +1,18 @@
|
|
1
|
+
import { XataRecord } from '..';
|
2
|
+
import { StringKeys, UnionToIntersection, Values } from '../util/types';
|
3
|
+
import { Query } from './query';
|
4
|
+
declare type Queries<T> = {
|
5
|
+
[key in keyof T as T[key] extends Query<any> ? key : never]: T[key];
|
6
|
+
};
|
7
|
+
declare type OmitQueries<T> = {
|
8
|
+
[key in keyof T as T[key] extends Query<any> ? never : key]: T[key];
|
9
|
+
};
|
10
|
+
declare type OmitMethods<T> = {
|
11
|
+
[key in keyof T as T[key] extends Function ? never : key]: T[key];
|
12
|
+
};
|
13
|
+
export declare type Selectable<T> = Omit<OmitQueries<OmitMethods<T>>, 'id' | 'xata'>;
|
14
|
+
export declare type SelectableColumn<O> = '*' | (O extends Array<unknown> ? never : O extends Record<string, any> ? '*' | Values<{
|
15
|
+
[K in StringKeys<O>]: O[K] extends Record<string, any> ? `${K}.${SelectableColumn<O[K]>}` : K;
|
16
|
+
}> : '');
|
17
|
+
export declare type Select<T, K extends SelectableColumn<T>> = UnionToIntersection<K extends keyof T ? Pick<T, K> : T> & Queries<T> & XataRecord;
|
18
|
+
export {};
|
@@ -0,0 +1,9 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.errors = void 0;
|
4
|
+
exports.errors = {
|
5
|
+
falsyFetchImplementation: `The \`fetch\` option passed to the Xata client is resolving to a falsy value and may not be correctly imported.
|
6
|
+
|
7
|
+
More in the docs:
|
8
|
+
` /** @todo add a link after docs exist */
|
9
|
+
};
|
package/package.json
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
{
|
2
2
|
"name": "@xata.io/client",
|
3
|
-
"version": "0.0.0-beta.
|
3
|
+
"version": "0.0.0-beta.f12621e",
|
4
4
|
"description": "Xata.io SDK for TypeScript and JavaScript",
|
5
5
|
"main": "./dist/index.js",
|
6
6
|
"types": "./dist/index.d.ts",
|
7
7
|
"scripts": {
|
8
8
|
"test": "echo \"Error: no test specified\" && exit 1",
|
9
|
-
"build": "tsc",
|
9
|
+
"build": "tsc -p tsconfig.build.json",
|
10
10
|
"prepack": "npm run build"
|
11
11
|
},
|
12
12
|
"repository": {
|
@@ -20,5 +20,5 @@
|
|
20
20
|
"url": "https://github.com/xataio/client-ts/issues"
|
21
21
|
},
|
22
22
|
"homepage": "https://github.com/xataio/client-ts/blob/main/client/README.md",
|
23
|
-
"gitHead": "
|
23
|
+
"gitHead": "f12621e75d650a761ab49fbb4f1ae4720b1effd8"
|
24
24
|
}
|
package/dist/index.test.d.ts
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export {};
|
package/dist/index.test.js
DELETED
@@ -1,304 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
-
});
|
10
|
-
};
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
12
|
-
const _1 = require("./");
|
13
|
-
const buildClient = (options = {}) => {
|
14
|
-
const { apiKey = '1234', databaseURL = 'https://my-workspace-5df34do.staging.xatabase.co/db/xata', branch = 'main' } = options;
|
15
|
-
const fetch = jest.fn();
|
16
|
-
const client = new _1.BaseClient({ fetch, apiKey, databaseURL, branch }, {});
|
17
|
-
const users = new _1.RestRepository(client, 'users');
|
18
|
-
return { fetch, client, users };
|
19
|
-
};
|
20
|
-
describe('client options', () => {
|
21
|
-
test('option parameters are set', () => {
|
22
|
-
const { client } = buildClient({ apiKey: 'apiKey', databaseURL: 'url' });
|
23
|
-
expect(client.options.apiKey).toBe('apiKey');
|
24
|
-
expect(client.options.databaseURL).toBe('url');
|
25
|
-
});
|
26
|
-
test('throws if mandatory options are missing', () => {
|
27
|
-
// @ts-expect-error Options are mandatory in TypeScript
|
28
|
-
expect(() => buildClient({ apiKey: null }, {})).toThrow('Options databaseURL, apiKey and branch are required');
|
29
|
-
// @ts-expect-error Options are mandatory in TypeScript
|
30
|
-
expect(() => buildClient({ databaseURL: null }, {})).toThrow('Options databaseURL, apiKey and branch are required');
|
31
|
-
// @ts-expect-error Options are mandatory in TypeScript
|
32
|
-
expect(() => buildClient({ branch: null }, {})).toThrow('Options databaseURL, apiKey and branch are required');
|
33
|
-
});
|
34
|
-
test('throws if branch cannot be resolved', () => {
|
35
|
-
const { users } = buildClient({ branch: () => null });
|
36
|
-
expect(users.request('GET', '/foo')).rejects.toThrow('Unable to resolve branch value');
|
37
|
-
});
|
38
|
-
test('provide branch as a string', () => __awaiter(void 0, void 0, void 0, function* () {
|
39
|
-
const { fetch, users } = buildClient({ branch: 'branch' });
|
40
|
-
fetch.mockReset().mockImplementation(() => {
|
41
|
-
return {
|
42
|
-
ok: true,
|
43
|
-
json: () => __awaiter(void 0, void 0, void 0, function* () { return ({}); })
|
44
|
-
};
|
45
|
-
});
|
46
|
-
yield users.request('GET', '/foo');
|
47
|
-
expect(fetch).toHaveBeenCalledTimes(1);
|
48
|
-
expect(fetch.mock.calls[0]).toMatchInlineSnapshot(`
|
49
|
-
Array [
|
50
|
-
"https://my-workspace-5df34do.staging.xatabase.co/db/xata:branch/foo",
|
51
|
-
Object {
|
52
|
-
"body": undefined,
|
53
|
-
"headers": Object {
|
54
|
-
"Accept": "*/*",
|
55
|
-
"Authorization": "Bearer 1234",
|
56
|
-
"Content-Type": "application/json",
|
57
|
-
},
|
58
|
-
"method": "GET",
|
59
|
-
},
|
60
|
-
]
|
61
|
-
`);
|
62
|
-
}));
|
63
|
-
test('provide branch as an array', () => __awaiter(void 0, void 0, void 0, function* () {
|
64
|
-
const { fetch, users } = buildClient({
|
65
|
-
branch: [process.env.NOT_DEFINED_VARIABLE, () => __awaiter(void 0, void 0, void 0, function* () { return null; }), 'branch', 'main']
|
66
|
-
});
|
67
|
-
fetch.mockReset().mockImplementation(() => {
|
68
|
-
return {
|
69
|
-
ok: true,
|
70
|
-
json: () => __awaiter(void 0, void 0, void 0, function* () { return ({}); })
|
71
|
-
};
|
72
|
-
});
|
73
|
-
yield users.request('GET', '/foo');
|
74
|
-
expect(fetch).toHaveBeenCalledTimes(1);
|
75
|
-
expect(fetch.mock.calls[0]).toMatchInlineSnapshot(`
|
76
|
-
Array [
|
77
|
-
"https://my-workspace-5df34do.staging.xatabase.co/db/xata:branch/foo",
|
78
|
-
Object {
|
79
|
-
"body": undefined,
|
80
|
-
"headers": Object {
|
81
|
-
"Accept": "*/*",
|
82
|
-
"Authorization": "Bearer 1234",
|
83
|
-
"Content-Type": "application/json",
|
84
|
-
},
|
85
|
-
"method": "GET",
|
86
|
-
},
|
87
|
-
]
|
88
|
-
`);
|
89
|
-
}));
|
90
|
-
test('provide branch as a function', () => __awaiter(void 0, void 0, void 0, function* () {
|
91
|
-
const { fetch, users } = buildClient({ branch: () => 'branch' });
|
92
|
-
fetch.mockReset().mockImplementation(() => {
|
93
|
-
return {
|
94
|
-
ok: true,
|
95
|
-
json: () => __awaiter(void 0, void 0, void 0, function* () { return ({}); })
|
96
|
-
};
|
97
|
-
});
|
98
|
-
yield users.request('GET', '/foo');
|
99
|
-
expect(fetch).toHaveBeenCalledTimes(1);
|
100
|
-
expect(fetch.mock.calls[0]).toMatchInlineSnapshot(`
|
101
|
-
Array [
|
102
|
-
"https://my-workspace-5df34do.staging.xatabase.co/db/xata:branch/foo",
|
103
|
-
Object {
|
104
|
-
"body": undefined,
|
105
|
-
"headers": Object {
|
106
|
-
"Accept": "*/*",
|
107
|
-
"Authorization": "Bearer 1234",
|
108
|
-
"Content-Type": "application/json",
|
109
|
-
},
|
110
|
-
"method": "GET",
|
111
|
-
},
|
112
|
-
]
|
113
|
-
`);
|
114
|
-
}));
|
115
|
-
test('ensure branch resolution is memoized', () => __awaiter(void 0, void 0, void 0, function* () {
|
116
|
-
const branchGetter = jest.fn(() => 'branch');
|
117
|
-
const { fetch, users } = buildClient({ branch: branchGetter });
|
118
|
-
fetch.mockReset().mockImplementation(() => {
|
119
|
-
return {
|
120
|
-
ok: true,
|
121
|
-
json: () => __awaiter(void 0, void 0, void 0, function* () { return ({}); })
|
122
|
-
};
|
123
|
-
});
|
124
|
-
yield users.request('GET', '/foo');
|
125
|
-
yield users.request('GET', '/foo');
|
126
|
-
expect(branchGetter).toHaveBeenCalledTimes(1);
|
127
|
-
}));
|
128
|
-
});
|
129
|
-
describe('request', () => {
|
130
|
-
test('builds the right arguments for a GET request', () => __awaiter(void 0, void 0, void 0, function* () {
|
131
|
-
const { fetch, users } = buildClient();
|
132
|
-
fetch.mockReset().mockImplementation(() => {
|
133
|
-
return {
|
134
|
-
ok: true,
|
135
|
-
json: () => __awaiter(void 0, void 0, void 0, function* () { return ({}); })
|
136
|
-
};
|
137
|
-
});
|
138
|
-
yield users.request('GET', '/foo');
|
139
|
-
expect(fetch).toHaveBeenCalledTimes(1);
|
140
|
-
expect(fetch.mock.calls[0]).toMatchInlineSnapshot(`
|
141
|
-
Array [
|
142
|
-
"https://my-workspace-5df34do.staging.xatabase.co/db/xata:main/foo",
|
143
|
-
Object {
|
144
|
-
"body": undefined,
|
145
|
-
"headers": Object {
|
146
|
-
"Accept": "*/*",
|
147
|
-
"Authorization": "Bearer 1234",
|
148
|
-
"Content-Type": "application/json",
|
149
|
-
},
|
150
|
-
"method": "GET",
|
151
|
-
},
|
152
|
-
]
|
153
|
-
`);
|
154
|
-
}));
|
155
|
-
test('builds the right arguments for a POST request', () => __awaiter(void 0, void 0, void 0, function* () {
|
156
|
-
const { fetch, users } = buildClient();
|
157
|
-
fetch.mockReset().mockImplementation(() => {
|
158
|
-
return {
|
159
|
-
ok: true,
|
160
|
-
json: () => __awaiter(void 0, void 0, void 0, function* () { return ({}); })
|
161
|
-
};
|
162
|
-
});
|
163
|
-
yield users.request('POST', '/foo', { a: 1 });
|
164
|
-
expect(fetch).toHaveBeenCalledTimes(1);
|
165
|
-
expect(fetch.mock.calls[0]).toMatchInlineSnapshot(`
|
166
|
-
Array [
|
167
|
-
"https://my-workspace-5df34do.staging.xatabase.co/db/xata:main/foo",
|
168
|
-
Object {
|
169
|
-
"body": "{\\"a\\":1}",
|
170
|
-
"headers": Object {
|
171
|
-
"Accept": "*/*",
|
172
|
-
"Authorization": "Bearer 1234",
|
173
|
-
"Content-Type": "application/json",
|
174
|
-
},
|
175
|
-
"method": "POST",
|
176
|
-
},
|
177
|
-
]
|
178
|
-
`);
|
179
|
-
}));
|
180
|
-
test('throws if the response is not ok', () => __awaiter(void 0, void 0, void 0, function* () {
|
181
|
-
const { fetch, users } = buildClient();
|
182
|
-
fetch.mockImplementation(() => {
|
183
|
-
return {
|
184
|
-
ok: false,
|
185
|
-
status: 404,
|
186
|
-
statusText: 'Not Found'
|
187
|
-
};
|
188
|
-
});
|
189
|
-
expect(users.request('GET', '/foo')).rejects.toThrow(new _1.XataError('Not Found', 404));
|
190
|
-
}));
|
191
|
-
test('throws with the error from the server if the response is not ok', () => __awaiter(void 0, void 0, void 0, function* () {
|
192
|
-
const { fetch, users } = buildClient();
|
193
|
-
fetch.mockImplementation(() => {
|
194
|
-
return {
|
195
|
-
ok: false,
|
196
|
-
status: 404,
|
197
|
-
statusText: 'Not Found',
|
198
|
-
json: () => __awaiter(void 0, void 0, void 0, function* () { return ({ message: 'Resource not found' }); })
|
199
|
-
};
|
200
|
-
});
|
201
|
-
expect(users.request('GET', '/foo')).rejects.toThrow(new _1.XataError('Resource not found', 404));
|
202
|
-
}));
|
203
|
-
test('returns the json body if the response is ok', () => __awaiter(void 0, void 0, void 0, function* () {
|
204
|
-
const { fetch, users } = buildClient();
|
205
|
-
const json = { a: 1 };
|
206
|
-
fetch.mockImplementation(() => {
|
207
|
-
return {
|
208
|
-
ok: true,
|
209
|
-
json: () => __awaiter(void 0, void 0, void 0, function* () { return json; })
|
210
|
-
};
|
211
|
-
});
|
212
|
-
const result = yield users.request('GET', '/foo');
|
213
|
-
expect(result).toEqual(json);
|
214
|
-
}));
|
215
|
-
});
|
216
|
-
function expectRequest(users, expectedRequest, callback, response) {
|
217
|
-
return __awaiter(this, void 0, void 0, function* () {
|
218
|
-
const request = jest.fn(() => __awaiter(this, void 0, void 0, function* () { return response; }));
|
219
|
-
users.request = request;
|
220
|
-
yield callback();
|
221
|
-
const { calls } = request.mock;
|
222
|
-
expect(calls.length).toBe(1);
|
223
|
-
const [method, path, body] = calls[0];
|
224
|
-
expect(method).toBe(expectedRequest.method);
|
225
|
-
expect(path).toBe(expectedRequest.path);
|
226
|
-
expect(JSON.stringify(body)).toBe(JSON.stringify(expectedRequest.body));
|
227
|
-
});
|
228
|
-
}
|
229
|
-
describe('query', () => {
|
230
|
-
describe('getMany', () => {
|
231
|
-
test('simple query', () => __awaiter(void 0, void 0, void 0, function* () {
|
232
|
-
const { users } = buildClient();
|
233
|
-
const expected = { method: 'POST', path: '/tables/users/query', body: {} };
|
234
|
-
expectRequest(users, expected, () => users.getMany(), { records: [] });
|
235
|
-
}));
|
236
|
-
test('query with one filter', () => __awaiter(void 0, void 0, void 0, function* () {
|
237
|
-
const { users } = buildClient();
|
238
|
-
const expected = { method: 'POST', path: '/tables/users/query', body: { filter: { $all: [{ name: 'foo' }] } } };
|
239
|
-
expectRequest(users, expected, () => users.filter('name', 'foo').getMany(), { records: [] });
|
240
|
-
}));
|
241
|
-
});
|
242
|
-
describe('getOne', () => {
|
243
|
-
test('returns a single object', () => __awaiter(void 0, void 0, void 0, function* () {
|
244
|
-
const { users } = buildClient();
|
245
|
-
const result = { records: [{ id: '1234' }] };
|
246
|
-
const expected = { method: 'POST', path: '/tables/users/query', body: {} };
|
247
|
-
expectRequest(users, expected, () => __awaiter(void 0, void 0, void 0, function* () {
|
248
|
-
const first = yield users.select().getOne();
|
249
|
-
expect(first === null || first === void 0 ? void 0 : first.id).toBe(result.records[0].id);
|
250
|
-
}), result);
|
251
|
-
}));
|
252
|
-
test('returns null if no objects are returned', () => __awaiter(void 0, void 0, void 0, function* () {
|
253
|
-
const { users } = buildClient();
|
254
|
-
const result = { records: [] };
|
255
|
-
const expected = { method: 'POST', path: '/tables/users/query', body: {} };
|
256
|
-
expectRequest(users, expected, () => __awaiter(void 0, void 0, void 0, function* () {
|
257
|
-
const first = yield users.getOne();
|
258
|
-
expect(first).toBeNull();
|
259
|
-
}), result);
|
260
|
-
}));
|
261
|
-
});
|
262
|
-
});
|
263
|
-
describe('read', () => {
|
264
|
-
test('reads an object by id successfully', () => __awaiter(void 0, void 0, void 0, function* () {
|
265
|
-
const { users } = buildClient();
|
266
|
-
const id = 'rec_1234';
|
267
|
-
const expected = { method: 'GET', path: `/tables/users/data/${id}`, body: undefined };
|
268
|
-
expectRequest(users, expected, () => users.read(id));
|
269
|
-
}));
|
270
|
-
});
|
271
|
-
describe('Repository.update', () => {
|
272
|
-
test('updates and object successfully', () => __awaiter(void 0, void 0, void 0, function* () {
|
273
|
-
const { users } = buildClient();
|
274
|
-
const object = { id: 'rec_1234', xata: { version: 1 }, name: 'Ada' };
|
275
|
-
const expected = { method: 'PUT', path: `/tables/users/data/${object.id}`, body: object };
|
276
|
-
expectRequest(users, expected, () => __awaiter(void 0, void 0, void 0, function* () {
|
277
|
-
const result = yield users.update(object.id, object);
|
278
|
-
expect(result.id).toBe(object.id);
|
279
|
-
}), { id: object.id });
|
280
|
-
}));
|
281
|
-
});
|
282
|
-
describe('Repository.delete', () => {
|
283
|
-
test('deletes a record by id successfully', () => __awaiter(void 0, void 0, void 0, function* () {
|
284
|
-
const { users } = buildClient();
|
285
|
-
const id = 'rec_1234';
|
286
|
-
const expected = { method: 'DELETE', path: `/tables/users/data/${id}`, body: undefined };
|
287
|
-
expectRequest(users, expected, () => __awaiter(void 0, void 0, void 0, function* () {
|
288
|
-
const result = yield users.delete(id);
|
289
|
-
expect(result).toBe(undefined);
|
290
|
-
}));
|
291
|
-
}));
|
292
|
-
});
|
293
|
-
describe('create', () => {
|
294
|
-
test('successful', () => __awaiter(void 0, void 0, void 0, function* () {
|
295
|
-
const { users } = buildClient();
|
296
|
-
const created = { id: 'rec_1234', _version: 0 };
|
297
|
-
const object = { name: 'Ada' };
|
298
|
-
const expected = { method: 'POST', path: '/tables/users/data', body: object };
|
299
|
-
expectRequest(users, expected, () => __awaiter(void 0, void 0, void 0, function* () {
|
300
|
-
const result = yield users.create(object);
|
301
|
-
expect(result.id).toBe(created.id);
|
302
|
-
}), created);
|
303
|
-
}));
|
304
|
-
});
|