@xata.io/client 0.0.0-beta.bdce130 → 0.0.0-beta.c21e40a
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.cjs +13 -0
- package/CHANGELOG.md +49 -0
- package/dist/api/client.d.ts +8 -3
- package/dist/api/client.js +42 -12
- package/dist/api/components.d.ts +63 -57
- package/dist/api/components.js +36 -40
- package/dist/api/fetcher.d.ts +3 -2
- package/dist/api/fetcher.js +31 -33
- package/dist/api/index.js +5 -1
- package/dist/api/providers.js +3 -2
- package/dist/api/responses.d.ts +12 -0
- package/dist/index.d.ts +0 -58
- package/dist/index.js +6 -265
- package/dist/schema/filters.d.ts +7 -5
- package/dist/schema/filters.js +2 -1
- package/dist/schema/index.d.ts +6 -0
- package/dist/schema/index.js +17 -1
- package/dist/schema/operators.d.ts +51 -0
- package/dist/schema/operators.js +51 -0
- package/dist/schema/pagination.d.ts +56 -14
- package/dist/schema/pagination.js +37 -2
- package/dist/schema/query.d.ts +108 -100
- package/dist/schema/query.js +87 -35
- package/dist/schema/record.d.ts +66 -0
- package/dist/schema/record.js +13 -0
- package/dist/schema/repository.d.ts +100 -0
- package/dist/schema/repository.js +288 -0
- package/dist/schema/selection.d.ts +24 -17
- package/dist/schema/selection.spec.d.ts +1 -0
- package/dist/schema/selection.spec.js +203 -0
- package/dist/util/lang.d.ts +4 -0
- package/dist/util/lang.js +13 -1
- package/dist/util/types.d.ts +11 -1
- package/package.json +2 -2
- package/tsconfig.json +21 -0
package/dist/api/fetcher.js
CHANGED
@@ -10,34 +10,39 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
10
10
|
};
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
12
12
|
exports.fetch = void 0;
|
13
|
+
/* eslint-disable @typescript-eslint/no-throw-literal */
|
14
|
+
/* eslint-disable @typescript-eslint/ban-types */
|
15
|
+
const lang_1 = require("../util/lang");
|
13
16
|
const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
|
14
17
|
const query = new URLSearchParams(queryParams).toString();
|
15
18
|
const queryString = query.length > 0 ? `?${query}` : '';
|
16
19
|
return url.replace(/\{\w*\}/g, (key) => pathParams[key.slice(1, -1)]) + queryString;
|
17
20
|
};
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
return workspacesApiUrl.replace('{workspaceId}.', '');
|
24
|
-
}
|
25
|
-
return workspacesApiUrl.replace('{workspaceId}', workspace);
|
21
|
+
function buildBaseUrl({ path, workspacesApiUrl, apiUrl, pathParams }) {
|
22
|
+
if (!(pathParams === null || pathParams === void 0 ? void 0 : pathParams.workspace))
|
23
|
+
return `${apiUrl}${path}`;
|
24
|
+
const url = typeof workspacesApiUrl === 'string' ? `${workspacesApiUrl}${path}` : workspacesApiUrl(path, pathParams);
|
25
|
+
return url.replace('{workspaceId}', pathParams.workspace);
|
26
26
|
}
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
const
|
32
|
-
|
27
|
+
// The host header is needed by Node.js on localhost.
|
28
|
+
// It is ignored by fetch() in the frontend
|
29
|
+
function hostHeader(url) {
|
30
|
+
var _a;
|
31
|
+
const pattern = /.*:\/\/(?<host>[^/]+).*/;
|
32
|
+
const { groups } = (_a = pattern.exec(url)) !== null && _a !== void 0 ? _a : {};
|
33
|
+
return (groups === null || groups === void 0 ? void 0 : groups.host) ? { Host: groups.host } : {};
|
33
34
|
}
|
34
|
-
function fetch({ url, method, body, headers, pathParams, queryParams, fetchImpl, apiKey, apiUrl, workspacesApiUrl }) {
|
35
|
+
function fetch({ url: path, method, body, headers, pathParams, queryParams, fetchImpl, apiKey, apiUrl, workspacesApiUrl }) {
|
35
36
|
return __awaiter(this, void 0, void 0, function* () {
|
36
|
-
const
|
37
|
-
const
|
37
|
+
const baseUrl = buildBaseUrl({ path, workspacesApiUrl, pathParams, apiUrl });
|
38
|
+
const fullUrl = resolveUrl(baseUrl, queryParams, pathParams);
|
39
|
+
// Node.js on localhost won't resolve localhost subdomains unless mapped in /etc/hosts
|
40
|
+
// So, instead, we use localhost without subdomains, but will add a Host header
|
41
|
+
const url = fullUrl.includes('localhost') ? fullUrl.replace(/^[^.]+\./, 'http://') : fullUrl;
|
42
|
+
const response = yield fetchImpl(url, {
|
38
43
|
method: method.toUpperCase(),
|
39
44
|
body: body ? JSON.stringify(body) : undefined,
|
40
|
-
headers: Object.assign(Object.assign(Object.assign({ 'Content-Type': 'application/json' }, headers), { Authorization: `Bearer ${apiKey}` })
|
45
|
+
headers: Object.assign(Object.assign(Object.assign({ 'Content-Type': 'application/json' }, headers), hostHeader(fullUrl)), { Authorization: `Bearer ${apiKey}` })
|
41
46
|
});
|
42
47
|
// No content
|
43
48
|
if (response.status === 204) {
|
@@ -48,28 +53,21 @@ function fetch({ url, method, body, headers, pathParams, queryParams, fetchImpl,
|
|
48
53
|
if (response.ok) {
|
49
54
|
return jsonResponse;
|
50
55
|
}
|
51
|
-
|
52
|
-
|
53
|
-
}
|
54
|
-
else {
|
55
|
-
throw withStatus(fallbackError, response.status);
|
56
|
-
}
|
56
|
+
const { message = 'Unknown error', errors } = jsonResponse;
|
57
|
+
throw withStatus({ message, errors }, response.status);
|
57
58
|
}
|
58
59
|
catch (e) {
|
59
|
-
if (e
|
60
|
-
const error = {
|
61
|
-
message: e.message
|
62
|
-
};
|
63
|
-
throw withStatus(error, response.status);
|
64
|
-
}
|
65
|
-
else if (typeof e === 'object' && typeof e.message === 'string') {
|
60
|
+
if (isError(e)) {
|
66
61
|
throw withStatus(e, response.status);
|
67
62
|
}
|
68
63
|
else {
|
69
|
-
throw withStatus(
|
64
|
+
throw withStatus({ message: 'Network response was not ok' }, response.status);
|
70
65
|
}
|
71
66
|
}
|
72
67
|
});
|
73
68
|
}
|
74
69
|
exports.fetch = fetch;
|
75
|
-
const
|
70
|
+
const isError = (error) => {
|
71
|
+
return (0, lang_1.isObject)(error) && (0, lang_1.isString)(error.message);
|
72
|
+
};
|
73
|
+
const withStatus = (error, status) => (0, lang_1.compactObject)(Object.assign(Object.assign({}, error), { status }));
|
package/dist/api/index.js
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
"use strict";
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
3
|
if (k2 === undefined) k2 = k;
|
4
|
-
Object.
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
5
9
|
}) : (function(o, m, k, k2) {
|
6
10
|
if (k2 === undefined) k2 = k;
|
7
11
|
o[k2] = m[k];
|
package/dist/api/providers.js
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.getHostUrl = void 0;
|
4
|
+
const lang_1 = require("../util/lang");
|
4
5
|
function getHostUrl(provider, type) {
|
5
6
|
if (isValidAlias(provider)) {
|
6
7
|
return providers[provider][type];
|
@@ -22,8 +23,8 @@ const providers = {
|
|
22
23
|
}
|
23
24
|
};
|
24
25
|
function isValidAlias(alias) {
|
25
|
-
return
|
26
|
+
return (0, lang_1.isString)(alias) && Object.keys(providers).includes(alias);
|
26
27
|
}
|
27
28
|
function isValidBuilder(builder) {
|
28
|
-
return
|
29
|
+
return (0, lang_1.isObject)(builder) && (0, lang_1.isString)(builder.main) && (0, lang_1.isString)(builder.workspaces);
|
29
30
|
}
|
package/dist/api/responses.d.ts
CHANGED
@@ -19,10 +19,22 @@ export declare type AuthError = {
|
|
19
19
|
id?: string;
|
20
20
|
message: string;
|
21
21
|
};
|
22
|
+
export declare type BulkError = {
|
23
|
+
errors: {
|
24
|
+
message?: string;
|
25
|
+
status?: number;
|
26
|
+
}[];
|
27
|
+
};
|
22
28
|
export declare type BranchMigrationPlan = {
|
23
29
|
version: number;
|
24
30
|
migration: Schemas.BranchMigration;
|
25
31
|
};
|
32
|
+
export declare type RecordUpdateResponse = {
|
33
|
+
id: string;
|
34
|
+
xata: {
|
35
|
+
version: number;
|
36
|
+
};
|
37
|
+
};
|
26
38
|
export declare type QueryResponse = {
|
27
39
|
records: Schemas.XataRecord[];
|
28
40
|
meta: Schemas.RecordsMetadata;
|
package/dist/index.d.ts
CHANGED
@@ -1,64 +1,6 @@
|
|
1
|
-
import { Page } from './schema/pagination';
|
2
|
-
import { Query, QueryOptions } from './schema/query';
|
3
|
-
import { Selectable, SelectableColumn, Select } from './schema/selection';
|
4
|
-
export interface XataRecord {
|
5
|
-
id: string;
|
6
|
-
xata: {
|
7
|
-
version: number;
|
8
|
-
};
|
9
|
-
read(): Promise<this>;
|
10
|
-
update(data: Selectable<this>): Promise<this>;
|
11
|
-
delete(): Promise<void>;
|
12
|
-
}
|
13
|
-
export declare abstract class Repository<T extends XataRecord> extends Query<T> {
|
14
|
-
abstract create(object: Selectable<T>): Promise<T>;
|
15
|
-
abstract createMany(objects: Selectable<T>[]): Promise<T[]>;
|
16
|
-
abstract read(id: string): Promise<T | null>;
|
17
|
-
abstract update(id: string, object: Partial<T>): Promise<T>;
|
18
|
-
abstract delete(id: string): void;
|
19
|
-
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>>;
|
20
|
-
}
|
21
|
-
export declare class RestRepository<T extends XataRecord> extends Repository<T> {
|
22
|
-
#private;
|
23
|
-
constructor(client: BaseClient<any>, table: string);
|
24
|
-
request<T>(method: string, path: string, body?: unknown): Promise<T | undefined>;
|
25
|
-
create(object: T): Promise<T>;
|
26
|
-
createMany(objects: T[]): Promise<T[]>;
|
27
|
-
read(id: string): Promise<T | null>;
|
28
|
-
update(id: string, object: Partial<T>): Promise<T>;
|
29
|
-
delete(id: 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>>;
|
31
|
-
}
|
32
|
-
interface RepositoryFactory {
|
33
|
-
createRepository<T extends XataRecord>(client: BaseClient<any>, table: string): Repository<T>;
|
34
|
-
}
|
35
|
-
export declare class RestRespositoryFactory implements RepositoryFactory {
|
36
|
-
createRepository<T extends XataRecord>(client: BaseClient<any>, table: string): Repository<T>;
|
37
|
-
}
|
38
|
-
declare type BranchStrategyValue = string | undefined | null;
|
39
|
-
declare type BranchStrategyBuilder = () => BranchStrategyValue | Promise<BranchStrategyValue>;
|
40
|
-
declare type BranchStrategy = BranchStrategyValue | BranchStrategyBuilder;
|
41
|
-
declare type BranchStrategyOption = NonNullable<BranchStrategy | BranchStrategy[]>;
|
42
|
-
export declare type XataClientOptions = {
|
43
|
-
fetch?: unknown;
|
44
|
-
databaseURL?: string;
|
45
|
-
branch: BranchStrategyOption;
|
46
|
-
apiKey: string;
|
47
|
-
repositoryFactory?: RepositoryFactory;
|
48
|
-
};
|
49
|
-
export declare class BaseClient<D extends Record<string, Repository<any>>> {
|
50
|
-
options: XataClientOptions;
|
51
|
-
private links;
|
52
|
-
private branch;
|
53
|
-
db: D;
|
54
|
-
constructor(options: XataClientOptions, links: Links);
|
55
|
-
initObject<T>(table: string, object: object): T;
|
56
|
-
getBranch(): Promise<string>;
|
57
|
-
}
|
58
1
|
export declare class XataError extends Error {
|
59
2
|
readonly status: number;
|
60
3
|
constructor(message: string, status: number);
|
61
4
|
}
|
62
|
-
export declare type Links = Record<string, Array<string[]>>;
|
63
5
|
export * from './api';
|
64
6
|
export * from './schema';
|
package/dist/index.js
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
"use strict";
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
3
|
if (k2 === undefined) k2 = k;
|
4
|
-
Object.
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
5
9
|
}) : (function(o, m, k, k2) {
|
6
10
|
if (k2 === undefined) k2 = k;
|
7
11
|
o[k2] = m[k];
|
@@ -9,259 +13,8 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
9
13
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
10
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
11
15
|
};
|
12
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
13
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
14
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
15
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
16
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
17
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
18
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
19
|
-
});
|
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
|
-
};
|
32
|
-
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
33
|
-
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
34
|
-
var m = o[Symbol.asyncIterator], i;
|
35
|
-
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);
|
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); }); }; }
|
37
|
-
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
38
|
-
};
|
39
|
-
var _RestRepository_client, _RestRepository_fetch, _RestRepository_table;
|
40
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
41
|
-
exports.XataError =
|
42
|
-
const filters_1 = require("./schema/filters");
|
43
|
-
const pagination_1 = require("./schema/pagination");
|
44
|
-
const query_1 = require("./schema/query");
|
45
|
-
class Repository extends query_1.Query {
|
46
|
-
}
|
47
|
-
exports.Repository = Repository;
|
48
|
-
class RestRepository extends Repository {
|
49
|
-
constructor(client, table) {
|
50
|
-
super(null, table, {});
|
51
|
-
_RestRepository_client.set(this, void 0);
|
52
|
-
_RestRepository_fetch.set(this, void 0);
|
53
|
-
_RestRepository_table.set(this, void 0);
|
54
|
-
__classPrivateFieldSet(this, _RestRepository_client, client, "f");
|
55
|
-
__classPrivateFieldSet(this, _RestRepository_table, table, "f");
|
56
|
-
// TODO: Remove when integrating with API client
|
57
|
-
const fetchImpl = typeof fetch !== 'undefined' ? fetch : __classPrivateFieldGet(this, _RestRepository_client, "f").options.fetch;
|
58
|
-
if (!fetchImpl) {
|
59
|
-
throw new Error(`The \`fetch\` option passed to the Xata client is resolving to a falsy value and may not be correctly imported.`);
|
60
|
-
}
|
61
|
-
__classPrivateFieldSet(this, _RestRepository_fetch, fetchImpl, "f");
|
62
|
-
}
|
63
|
-
request(method, path, body) {
|
64
|
-
return __awaiter(this, void 0, void 0, function* () {
|
65
|
-
const { databaseURL, apiKey } = __classPrivateFieldGet(this, _RestRepository_client, "f").options;
|
66
|
-
const branch = yield __classPrivateFieldGet(this, _RestRepository_client, "f").getBranch();
|
67
|
-
const fetchImpl = __classPrivateFieldGet(this, _RestRepository_fetch, "f");
|
68
|
-
const resp = yield fetchImpl(`${databaseURL}:${branch}${path}`, {
|
69
|
-
method,
|
70
|
-
headers: {
|
71
|
-
Accept: '*/*',
|
72
|
-
'Content-Type': 'application/json',
|
73
|
-
Authorization: `Bearer ${apiKey}`
|
74
|
-
},
|
75
|
-
body: JSON.stringify(body)
|
76
|
-
});
|
77
|
-
if (!resp.ok) {
|
78
|
-
try {
|
79
|
-
const json = yield resp.json();
|
80
|
-
const message = json.message;
|
81
|
-
if (typeof message === 'string') {
|
82
|
-
throw new XataError(message, resp.status);
|
83
|
-
}
|
84
|
-
}
|
85
|
-
catch (err) {
|
86
|
-
if (err instanceof XataError)
|
87
|
-
throw err;
|
88
|
-
// Ignore errors for other reasons.
|
89
|
-
// For example if the response's body cannot be parsed as JSON
|
90
|
-
}
|
91
|
-
throw new XataError(resp.statusText, resp.status);
|
92
|
-
}
|
93
|
-
if (resp.status === 204)
|
94
|
-
return undefined;
|
95
|
-
return resp.json();
|
96
|
-
});
|
97
|
-
}
|
98
|
-
create(object) {
|
99
|
-
return __awaiter(this, void 0, void 0, function* () {
|
100
|
-
const record = transformObjectLinks(object);
|
101
|
-
const response = yield this.request('POST', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/data`, record);
|
102
|
-
if (!response) {
|
103
|
-
throw new Error("The server didn't return any data for the query");
|
104
|
-
}
|
105
|
-
const finalObject = yield this.read(response.id);
|
106
|
-
if (!finalObject) {
|
107
|
-
throw new Error('The server failed to save the record');
|
108
|
-
}
|
109
|
-
return finalObject;
|
110
|
-
});
|
111
|
-
}
|
112
|
-
createMany(objects) {
|
113
|
-
return __awaiter(this, void 0, void 0, function* () {
|
114
|
-
const records = objects.map((object) => transformObjectLinks(object));
|
115
|
-
const response = yield this.request('POST', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/bulk`, { records });
|
116
|
-
if (!response) {
|
117
|
-
throw new Error("The server didn't return any data for the query");
|
118
|
-
}
|
119
|
-
// TODO: Use filer.$any() to get all the records
|
120
|
-
const finalObjects = yield Promise.all(response.recordIDs.map((id) => this.read(id)));
|
121
|
-
if (finalObjects.some((object) => !object)) {
|
122
|
-
throw new Error('The server failed to save the record');
|
123
|
-
}
|
124
|
-
return finalObjects;
|
125
|
-
});
|
126
|
-
}
|
127
|
-
read(id) {
|
128
|
-
return __awaiter(this, void 0, void 0, function* () {
|
129
|
-
try {
|
130
|
-
const response = yield this.request('GET', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/data/${id}`);
|
131
|
-
if (!response)
|
132
|
-
return null;
|
133
|
-
return __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), response);
|
134
|
-
}
|
135
|
-
catch (err) {
|
136
|
-
if (err.status === 404)
|
137
|
-
return null;
|
138
|
-
throw err;
|
139
|
-
}
|
140
|
-
});
|
141
|
-
}
|
142
|
-
update(id, object) {
|
143
|
-
return __awaiter(this, void 0, void 0, function* () {
|
144
|
-
const response = yield this.request('PUT', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/data/${id}`, object);
|
145
|
-
if (!response) {
|
146
|
-
throw new Error("The server didn't return any data for the query");
|
147
|
-
}
|
148
|
-
// TODO: Review this, not sure we are properly initializing the object
|
149
|
-
return __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), response);
|
150
|
-
});
|
151
|
-
}
|
152
|
-
delete(id) {
|
153
|
-
return __awaiter(this, void 0, void 0, function* () {
|
154
|
-
yield this.request('DELETE', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/data/${id}`);
|
155
|
-
});
|
156
|
-
}
|
157
|
-
query(query, options) {
|
158
|
-
var _a, _b, _c;
|
159
|
-
return __awaiter(this, void 0, void 0, function* () {
|
160
|
-
const data = query.getQueryOptions();
|
161
|
-
const body = {
|
162
|
-
filter: Object.values(data.filter).some(Boolean) ? data.filter : undefined,
|
163
|
-
sort: (_a = (0, filters_1.buildSortFilter)(options === null || options === void 0 ? void 0 : options.sort)) !== null && _a !== void 0 ? _a : data.sort,
|
164
|
-
page: (_b = options === null || options === void 0 ? void 0 : options.page) !== null && _b !== void 0 ? _b : data.page,
|
165
|
-
columns: (_c = options === null || options === void 0 ? void 0 : options.columns) !== null && _c !== void 0 ? _c : data.columns
|
166
|
-
};
|
167
|
-
const response = yield this.request('POST', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/query`, body);
|
168
|
-
if (!response) {
|
169
|
-
throw new Error("The server didn't return any data for the query");
|
170
|
-
}
|
171
|
-
const { meta, records: objects } = response;
|
172
|
-
const records = objects.map((record) => __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), record));
|
173
|
-
// TODO: We should properly type this any
|
174
|
-
return new pagination_1.Page(query, meta, records);
|
175
|
-
});
|
176
|
-
}
|
177
|
-
}
|
178
|
-
exports.RestRepository = RestRepository;
|
179
|
-
_RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _RestRepository_table = new WeakMap();
|
180
|
-
class RestRespositoryFactory {
|
181
|
-
createRepository(client, table) {
|
182
|
-
return new RestRepository(client, table);
|
183
|
-
}
|
184
|
-
}
|
185
|
-
exports.RestRespositoryFactory = RestRespositoryFactory;
|
186
|
-
class BaseClient {
|
187
|
-
constructor(options, links) {
|
188
|
-
if (!options.databaseURL || !options.apiKey || !options.branch) {
|
189
|
-
throw new Error('Options databaseURL, apiKey and branch are required');
|
190
|
-
}
|
191
|
-
this.options = options;
|
192
|
-
this.links = links;
|
193
|
-
}
|
194
|
-
initObject(table, object) {
|
195
|
-
const o = {};
|
196
|
-
Object.assign(o, object);
|
197
|
-
const tableLinks = this.links[table] || [];
|
198
|
-
for (const link of tableLinks) {
|
199
|
-
const [field, linkTable] = link;
|
200
|
-
const value = o[field];
|
201
|
-
if (value && typeof value === 'object') {
|
202
|
-
const { id } = value;
|
203
|
-
if (Object.keys(value).find((col) => col === 'id')) {
|
204
|
-
o[field] = this.initObject(linkTable, value);
|
205
|
-
}
|
206
|
-
else if (id) {
|
207
|
-
o[field] = {
|
208
|
-
id,
|
209
|
-
get: () => {
|
210
|
-
this.db[linkTable].read(id);
|
211
|
-
}
|
212
|
-
};
|
213
|
-
}
|
214
|
-
}
|
215
|
-
}
|
216
|
-
const db = this.db;
|
217
|
-
o.read = function () {
|
218
|
-
return db[table].read(o['id']);
|
219
|
-
};
|
220
|
-
o.update = function (data) {
|
221
|
-
return db[table].update(o['id'], data);
|
222
|
-
};
|
223
|
-
o.delete = function () {
|
224
|
-
return db[table].delete(o['id']);
|
225
|
-
};
|
226
|
-
for (const prop of ['read', 'update', 'delete']) {
|
227
|
-
Object.defineProperty(o, prop, { enumerable: false });
|
228
|
-
}
|
229
|
-
// TODO: links and rev links
|
230
|
-
Object.freeze(o);
|
231
|
-
return o;
|
232
|
-
}
|
233
|
-
getBranch() {
|
234
|
-
var e_1, _a;
|
235
|
-
return __awaiter(this, void 0, void 0, function* () {
|
236
|
-
if (this.branch)
|
237
|
-
return this.branch;
|
238
|
-
const { branch: param } = this.options;
|
239
|
-
const strategies = Array.isArray(param) ? [...param] : [param];
|
240
|
-
const evaluateBranch = (strategy) => __awaiter(this, void 0, void 0, function* () {
|
241
|
-
return isBranchStrategyBuilder(strategy) ? yield strategy() : strategy;
|
242
|
-
});
|
243
|
-
try {
|
244
|
-
for (var strategies_1 = __asyncValues(strategies), strategies_1_1; strategies_1_1 = yield strategies_1.next(), !strategies_1_1.done;) {
|
245
|
-
const strategy = strategies_1_1.value;
|
246
|
-
const branch = yield evaluateBranch(strategy);
|
247
|
-
if (branch) {
|
248
|
-
this.branch = branch;
|
249
|
-
return branch;
|
250
|
-
}
|
251
|
-
}
|
252
|
-
}
|
253
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
254
|
-
finally {
|
255
|
-
try {
|
256
|
-
if (strategies_1_1 && !strategies_1_1.done && (_a = strategies_1.return)) yield _a.call(strategies_1);
|
257
|
-
}
|
258
|
-
finally { if (e_1) throw e_1.error; }
|
259
|
-
}
|
260
|
-
throw new Error('Unable to resolve branch value');
|
261
|
-
});
|
262
|
-
}
|
263
|
-
}
|
264
|
-
exports.BaseClient = BaseClient;
|
17
|
+
exports.XataError = void 0;
|
265
18
|
class XataError extends Error {
|
266
19
|
constructor(message, status) {
|
267
20
|
super(message);
|
@@ -269,17 +22,5 @@ class XataError extends Error {
|
|
269
22
|
}
|
270
23
|
}
|
271
24
|
exports.XataError = XataError;
|
272
|
-
const isBranchStrategyBuilder = (strategy) => {
|
273
|
-
return typeof strategy === 'function';
|
274
|
-
};
|
275
|
-
// TODO: We can find a better implementation for links
|
276
|
-
const transformObjectLinks = (object) => {
|
277
|
-
return Object.entries(object).reduce((acc, [key, value]) => {
|
278
|
-
if (value && typeof value === 'object' && typeof value.id === 'string') {
|
279
|
-
return Object.assign(Object.assign({}, acc), { [key]: value.id });
|
280
|
-
}
|
281
|
-
return Object.assign(Object.assign({}, acc), { [key]: value });
|
282
|
-
}, {});
|
283
|
-
};
|
284
25
|
__exportStar(require("./api"), exports);
|
285
26
|
__exportStar(require("./schema"), exports);
|
package/dist/schema/filters.d.ts
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
+
import { XataRecord } from './record';
|
2
|
+
import { SelectableColumn } from './selection';
|
1
3
|
export declare type SortDirection = 'asc' | 'desc';
|
2
|
-
export declare type SortFilterExtended<T> = {
|
3
|
-
column:
|
4
|
+
export declare type SortFilterExtended<T extends XataRecord> = {
|
5
|
+
column: SelectableColumn<T>;
|
4
6
|
direction?: SortDirection;
|
5
7
|
};
|
6
|
-
export declare type SortFilter<T> =
|
7
|
-
export declare function isSortFilterObject<T>(filter: SortFilter<T>): filter is SortFilterExtended<T>;
|
8
|
+
export declare type SortFilter<T extends XataRecord> = SelectableColumn<T> | SortFilterExtended<T>;
|
9
|
+
export declare function isSortFilterObject<T extends XataRecord>(filter: SortFilter<T>): filter is SortFilterExtended<T>;
|
8
10
|
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>[]): {
|
11
|
+
export declare function buildSortFilter<T extends XataRecord>(filter?: SortFilter<T> | SortFilter<T>[]): {
|
10
12
|
[key: string]: SortDirection;
|
11
13
|
} | undefined;
|
12
14
|
export declare type Constraint<T> = {
|
package/dist/schema/filters.js
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.buildSortFilter = exports.isSortFilterObject = void 0;
|
4
|
+
const lang_1 = require("../util/lang");
|
4
5
|
function isSortFilterObject(filter) {
|
5
|
-
return
|
6
|
+
return (0, lang_1.isObject)(filter) && filter.column !== undefined;
|
6
7
|
}
|
7
8
|
exports.isSortFilterObject = isSortFilterObject;
|
8
9
|
function buildSortFilter(filter) {
|
package/dist/schema/index.d.ts
CHANGED
@@ -1 +1,7 @@
|
|
1
1
|
export * from './operators';
|
2
|
+
export * from './pagination';
|
3
|
+
export { Query } from './query';
|
4
|
+
export { isIdentifiable, isXataRecord } from './record';
|
5
|
+
export type { Identifiable, XataRecord } from './record';
|
6
|
+
export { BaseClient, Repository, RestRepository, RestRespositoryFactory } from './repository';
|
7
|
+
export type { XataClientOptions } from './repository';
|
package/dist/schema/index.js
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
"use strict";
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
3
|
if (k2 === undefined) k2 = k;
|
4
|
-
Object.
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
5
9
|
}) : (function(o, m, k, k2) {
|
6
10
|
if (k2 === undefined) k2 = k;
|
7
11
|
o[k2] = m[k];
|
@@ -10,4 +14,16 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
11
15
|
};
|
12
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
exports.RestRespositoryFactory = exports.RestRepository = exports.Repository = exports.BaseClient = exports.isXataRecord = exports.isIdentifiable = exports.Query = void 0;
|
13
18
|
__exportStar(require("./operators"), exports);
|
19
|
+
__exportStar(require("./pagination"), exports);
|
20
|
+
var query_1 = require("./query");
|
21
|
+
Object.defineProperty(exports, "Query", { enumerable: true, get: function () { return query_1.Query; } });
|
22
|
+
var record_1 = require("./record");
|
23
|
+
Object.defineProperty(exports, "isIdentifiable", { enumerable: true, get: function () { return record_1.isIdentifiable; } });
|
24
|
+
Object.defineProperty(exports, "isXataRecord", { enumerable: true, get: function () { return record_1.isXataRecord; } });
|
25
|
+
var repository_1 = require("./repository");
|
26
|
+
Object.defineProperty(exports, "BaseClient", { enumerable: true, get: function () { return repository_1.BaseClient; } });
|
27
|
+
Object.defineProperty(exports, "Repository", { enumerable: true, get: function () { return repository_1.Repository; } });
|
28
|
+
Object.defineProperty(exports, "RestRepository", { enumerable: true, get: function () { return repository_1.RestRepository; } });
|
29
|
+
Object.defineProperty(exports, "RestRespositoryFactory", { enumerable: true, get: function () { return repository_1.RestRespositoryFactory; } });
|
@@ -1,21 +1,72 @@
|
|
1
1
|
import { Constraint } from './filters';
|
2
2
|
declare type ComparableType = number | Date;
|
3
|
+
/**
|
4
|
+
* Operator to restrict results to only values that are greater than the given value.
|
5
|
+
*/
|
3
6
|
export declare const gt: <T extends ComparableType>(value: T) => Constraint<T>;
|
7
|
+
/**
|
8
|
+
* Operator to restrict results to only values that are greater than or equal to the given value.
|
9
|
+
*/
|
4
10
|
export declare const ge: <T extends ComparableType>(value: T) => Constraint<T>;
|
11
|
+
/**
|
12
|
+
* Operator to restrict results to only values that are greater than or equal to the given value.
|
13
|
+
*/
|
5
14
|
export declare const gte: <T extends ComparableType>(value: T) => Constraint<T>;
|
15
|
+
/**
|
16
|
+
* Operator to restrict results to only values that are lower than the given value.
|
17
|
+
*/
|
6
18
|
export declare const lt: <T extends ComparableType>(value: T) => Constraint<T>;
|
19
|
+
/**
|
20
|
+
* Operator to restrict results to only values that are lower than or equal to the given value.
|
21
|
+
*/
|
7
22
|
export declare const lte: <T extends ComparableType>(value: T) => Constraint<T>;
|
23
|
+
/**
|
24
|
+
* Operator to restrict results to only values that are lower than or equal to the given value.
|
25
|
+
*/
|
8
26
|
export declare const le: <T extends ComparableType>(value: T) => Constraint<T>;
|
27
|
+
/**
|
28
|
+
* Operator to restrict results to only values that are not null.
|
29
|
+
*/
|
9
30
|
export declare const exists: (column: string) => Constraint<string>;
|
31
|
+
/**
|
32
|
+
* Operator to restrict results to only values that are null.
|
33
|
+
*/
|
10
34
|
export declare const notExists: (column: string) => Constraint<string>;
|
35
|
+
/**
|
36
|
+
* Operator to restrict results to only values that start with the given prefix.
|
37
|
+
*/
|
11
38
|
export declare const startsWith: (value: string) => Constraint<string>;
|
39
|
+
/**
|
40
|
+
* Operator to restrict results to only values that end with the given suffix.
|
41
|
+
*/
|
12
42
|
export declare const endsWith: (value: string) => Constraint<string>;
|
43
|
+
/**
|
44
|
+
* Operator to restrict results to only values that match the given pattern.
|
45
|
+
*/
|
13
46
|
export declare const pattern: (value: string) => Constraint<string>;
|
47
|
+
/**
|
48
|
+
* Operator to restrict results to only values that are equal to the given value.
|
49
|
+
*/
|
14
50
|
export declare const is: <T>(value: T) => Constraint<T>;
|
51
|
+
/**
|
52
|
+
* Operator to restrict results to only values that are not equal to the given value.
|
53
|
+
*/
|
15
54
|
export declare const isNot: <T>(value: T) => Constraint<T>;
|
55
|
+
/**
|
56
|
+
* Operator to restrict results to only values that contain the given value.
|
57
|
+
*/
|
16
58
|
export declare const contains: <T>(value: T) => Constraint<T>;
|
59
|
+
/**
|
60
|
+
* Operator to restrict results to only arrays that include the given value.
|
61
|
+
*/
|
17
62
|
export declare const includes: (value: string) => Constraint<string>;
|
63
|
+
/**
|
64
|
+
* Operator to restrict results to only arrays that include a value matching the given substring.
|
65
|
+
*/
|
18
66
|
export declare const includesSubstring: (value: string) => Constraint<string>;
|
67
|
+
/**
|
68
|
+
* Operator to restrict results to only arrays that include a value matching the given pattern.
|
69
|
+
*/
|
19
70
|
export declare const includesPattern: (value: string) => Constraint<string>;
|
20
71
|
export declare const includesAll: (value: string) => Constraint<string>;
|
21
72
|
export {};
|