@xata.io/client 0.0.0-beta.cae436d → 0.0.0-beta.ccaf25d
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/CHANGELOG.md +24 -0
- package/dist/api/client.d.ts +6 -1
- package/dist/api/client.js +16 -1
- package/dist/api/components.d.ts +36 -26
- package/dist/api/components.js +9 -9
- package/dist/api/index.js +5 -1
- package/dist/api/responses.d.ts +6 -0
- package/dist/index.d.ts +0 -57
- package/dist/index.js +6 -236
- package/dist/schema/index.d.ts +5 -0
- package/dist/schema/index.js +14 -1
- package/dist/schema/operators.d.ts +51 -0
- package/dist/schema/operators.js +51 -0
- package/dist/schema/pagination.d.ts +44 -2
- package/dist/schema/pagination.js +37 -1
- package/dist/schema/query.d.ts +94 -10
- package/dist/schema/query.js +83 -19
- package/dist/schema/record.d.ts +44 -0
- package/dist/schema/record.js +2 -0
- package/dist/schema/repository.d.ts +104 -0
- package/dist/schema/repository.js +280 -0
- package/dist/schema/selection.d.ts +3 -9
- package/dist/util/lang.d.ts +1 -0
- package/package.json +2 -2
- package/tsconfig.json +21 -0
@@ -0,0 +1,44 @@
|
|
1
|
+
import { Selectable } from './selection';
|
2
|
+
/**
|
3
|
+
* Represents an identifiable record from the database.
|
4
|
+
*/
|
5
|
+
export interface Identifiable {
|
6
|
+
/**
|
7
|
+
* Unique id of this record.
|
8
|
+
*/
|
9
|
+
id: string;
|
10
|
+
}
|
11
|
+
export interface BaseData {
|
12
|
+
[key: string]: any;
|
13
|
+
}
|
14
|
+
/**
|
15
|
+
* Represents a persisted record from the database.
|
16
|
+
*/
|
17
|
+
export interface XataRecord extends Identifiable {
|
18
|
+
/**
|
19
|
+
* Metadata of this record.
|
20
|
+
*/
|
21
|
+
xata: {
|
22
|
+
/**
|
23
|
+
* Number that is increased every time the record is updated.
|
24
|
+
*/
|
25
|
+
version: number;
|
26
|
+
};
|
27
|
+
/**
|
28
|
+
* Retrieves a refreshed copy of the current record from the database.
|
29
|
+
*/
|
30
|
+
read(): Promise<this>;
|
31
|
+
/**
|
32
|
+
* Performs a partial update of the current record. On success a new object is
|
33
|
+
* returned and the current object is not mutated.
|
34
|
+
* @param data The columns and their values that have to be updated.
|
35
|
+
* @returns A new record containing the latest values for all the columns of the current record.
|
36
|
+
*/
|
37
|
+
update(data: Partial<Selectable<this>>): Promise<this>;
|
38
|
+
/**
|
39
|
+
* Performs a deletion of the current record in the database.
|
40
|
+
*
|
41
|
+
* @throws If the record was already deleted or if an error happened while performing the deletion.
|
42
|
+
*/
|
43
|
+
delete(): Promise<void>;
|
44
|
+
}
|
@@ -0,0 +1,104 @@
|
|
1
|
+
import { FetchImpl } from '../api/fetcher';
|
2
|
+
import { Page } from './pagination';
|
3
|
+
import { Query, QueryOptions } from './query';
|
4
|
+
import { BaseData, XataRecord } from './record';
|
5
|
+
import { Select, SelectableColumn } from './selection';
|
6
|
+
export declare type Links = Record<string, Array<string[]>>;
|
7
|
+
/**
|
8
|
+
* Common interface for performing operations on a table.
|
9
|
+
*/
|
10
|
+
export declare abstract class Repository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record> {
|
11
|
+
abstract create(object: Data): Promise<Record>;
|
12
|
+
/**
|
13
|
+
* Creates multiple records in the table.
|
14
|
+
* @param objects Array of objects with the column names and the values to be stored in the table.
|
15
|
+
* @returns Array of the persisted records.
|
16
|
+
*/
|
17
|
+
abstract createMany(objects: Data[]): Promise<Record[]>;
|
18
|
+
/**
|
19
|
+
* Queries a single record from the table given its unique id.
|
20
|
+
* @param id The unique id.
|
21
|
+
* @returns The persisted record for the given id or null if the record could not be found.
|
22
|
+
*/
|
23
|
+
abstract read(id: string): Promise<Record | null>;
|
24
|
+
/**
|
25
|
+
* Insert a single record with a unique id.
|
26
|
+
* @param id The unique id.
|
27
|
+
* @param object Object containing the column names with their values to be stored in the table.
|
28
|
+
* @returns The full persisted record.
|
29
|
+
*/
|
30
|
+
abstract insert(id: string, object: Data): Promise<Record>;
|
31
|
+
/**
|
32
|
+
* Partially update a single record given its unique id.
|
33
|
+
* @param id The unique id.
|
34
|
+
* @param object The column names and their values that have to be updatd.
|
35
|
+
* @returns The full persisted record.
|
36
|
+
*/
|
37
|
+
abstract update(id: string, object: Partial<Data>): Promise<Record>;
|
38
|
+
/**
|
39
|
+
* Updates or inserts a single record. If a record exists with the given id,
|
40
|
+
* it will be update, otherwise a new record will be created.
|
41
|
+
* @param id A unique id.
|
42
|
+
* @param object The column names and the values to be persisted.
|
43
|
+
* @returns The full persisted record.
|
44
|
+
*/
|
45
|
+
abstract updateOrInsert(id: string, object: Data): Promise<Record>;
|
46
|
+
/**
|
47
|
+
* Deletes a record given its unique id.
|
48
|
+
* @param id The unique id.
|
49
|
+
* @throws If the record could not be found or there was an error while performing the deletion.
|
50
|
+
*/
|
51
|
+
abstract delete(id: string): void;
|
52
|
+
abstract query<Result extends XataRecord, Options extends QueryOptions<Record>>(query: Query<Record, Result>, options: Options): Promise<Page<Record, typeof options extends {
|
53
|
+
columns: SelectableColumn<Data>[];
|
54
|
+
} ? Select<Data, typeof options['columns'][number]> : Result>>;
|
55
|
+
}
|
56
|
+
export declare class RestRepository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Repository<Data, Record> {
|
57
|
+
#private;
|
58
|
+
constructor(client: BaseClient<any>, table: string);
|
59
|
+
create(object: Data): Promise<Record>;
|
60
|
+
createMany(objects: Data[]): Promise<Record[]>;
|
61
|
+
read(recordId: string): Promise<Record | null>;
|
62
|
+
update(recordId: string, object: Partial<Data>): Promise<Record>;
|
63
|
+
insert(recordId: string, object: Data): Promise<Record>;
|
64
|
+
updateOrInsert(recordId: string, object: Data): Promise<Record>;
|
65
|
+
delete(recordId: string): Promise<void>;
|
66
|
+
query<Result extends XataRecord, Options extends QueryOptions<Record>>(query: Query<Record, Result>, options?: Options): Promise<Page<Record, typeof options extends {
|
67
|
+
columns: SelectableColumn<Data>[];
|
68
|
+
} ? Select<Data, typeof options['columns'][number]> : Result>>;
|
69
|
+
}
|
70
|
+
interface RepositoryFactory {
|
71
|
+
createRepository<Data extends BaseData>(client: BaseClient<any>, table: string): Repository<Data>;
|
72
|
+
}
|
73
|
+
export declare class RestRespositoryFactory implements RepositoryFactory {
|
74
|
+
createRepository<Data extends BaseData>(client: BaseClient<any>, table: string): Repository<Data>;
|
75
|
+
}
|
76
|
+
declare type BranchStrategyValue = string | undefined | null;
|
77
|
+
declare type BranchStrategyBuilder = () => BranchStrategyValue | Promise<BranchStrategyValue>;
|
78
|
+
declare type BranchStrategy = BranchStrategyValue | BranchStrategyBuilder;
|
79
|
+
declare type BranchStrategyOption = NonNullable<BranchStrategy | BranchStrategy[]>;
|
80
|
+
export declare type XataClientOptions = {
|
81
|
+
/**
|
82
|
+
* Fetch implementation. This option is only required if the runtime does not include a fetch implementation
|
83
|
+
* available in the global scope. If you are running your code on Deno or Cloudflare workers for example,
|
84
|
+
* you won't need to provide a specific fetch implementation. But for most versions of Node.js you'll need
|
85
|
+
* to provide one. Such as cross-fetch, node-fetch or isomorphic-fetch.
|
86
|
+
*/
|
87
|
+
fetch?: FetchImpl;
|
88
|
+
databaseURL?: string;
|
89
|
+
branch: BranchStrategyOption;
|
90
|
+
/**
|
91
|
+
* API key to be used. You can create one in your account settings at https://app.xata.io/settings.
|
92
|
+
*/
|
93
|
+
apiKey: string;
|
94
|
+
repositoryFactory?: RepositoryFactory;
|
95
|
+
};
|
96
|
+
export declare class BaseClient<D extends Record<string, Repository<any>> = Record<string, Repository<any>>> {
|
97
|
+
#private;
|
98
|
+
options: XataClientOptions;
|
99
|
+
db: D;
|
100
|
+
constructor(options: XataClientOptions, links?: Links);
|
101
|
+
initObject<T>(table: string, object: object): T;
|
102
|
+
getBranch(): Promise<string>;
|
103
|
+
}
|
104
|
+
export {};
|
@@ -0,0 +1,280 @@
|
|
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 __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
12
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
13
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
14
|
+
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");
|
15
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
16
|
+
};
|
17
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
18
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
19
|
+
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");
|
20
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
21
|
+
};
|
22
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
23
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
24
|
+
var m = o[Symbol.asyncIterator], i;
|
25
|
+
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);
|
26
|
+
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); }); }; }
|
27
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
28
|
+
};
|
29
|
+
var _RestRepository_instances, _RestRepository_client, _RestRepository_fetch, _RestRepository_table, _RestRepository_getFetchProps, _BaseClient_links, _BaseClient_branch;
|
30
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
31
|
+
exports.BaseClient = exports.RestRespositoryFactory = exports.RestRepository = exports.Repository = void 0;
|
32
|
+
const api_1 = require("../api");
|
33
|
+
const filters_1 = require("./filters");
|
34
|
+
const pagination_1 = require("./pagination");
|
35
|
+
const query_1 = require("./query");
|
36
|
+
/**
|
37
|
+
* Common interface for performing operations on a table.
|
38
|
+
*/
|
39
|
+
class Repository extends query_1.Query {
|
40
|
+
}
|
41
|
+
exports.Repository = Repository;
|
42
|
+
class RestRepository extends Repository {
|
43
|
+
constructor(client, table) {
|
44
|
+
super(null, table, {});
|
45
|
+
_RestRepository_instances.add(this);
|
46
|
+
_RestRepository_client.set(this, void 0);
|
47
|
+
_RestRepository_fetch.set(this, void 0);
|
48
|
+
_RestRepository_table.set(this, void 0);
|
49
|
+
__classPrivateFieldSet(this, _RestRepository_client, client, "f");
|
50
|
+
__classPrivateFieldSet(this, _RestRepository_table, table, "f");
|
51
|
+
// TODO: Remove when integrating with API client
|
52
|
+
const fetchImpl = typeof fetch !== 'undefined' ? fetch : __classPrivateFieldGet(this, _RestRepository_client, "f").options.fetch;
|
53
|
+
if (!fetchImpl) {
|
54
|
+
throw new Error(`The \`fetch\` option passed to the Xata client is resolving to a falsy value and may not be correctly imported.`);
|
55
|
+
}
|
56
|
+
__classPrivateFieldSet(this, _RestRepository_fetch, fetchImpl, "f");
|
57
|
+
}
|
58
|
+
create(object) {
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
60
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
61
|
+
const record = transformObjectLinks(object);
|
62
|
+
const response = yield (0, api_1.insertRecord)(Object.assign({ pathParams: {
|
63
|
+
workspace: '{workspaceId}',
|
64
|
+
dbBranchName: '{dbBranch}',
|
65
|
+
tableName: __classPrivateFieldGet(this, _RestRepository_table, "f")
|
66
|
+
}, body: record }, fetchProps));
|
67
|
+
const finalObject = yield this.read(response.id);
|
68
|
+
if (!finalObject) {
|
69
|
+
throw new Error('The server failed to save the record');
|
70
|
+
}
|
71
|
+
return finalObject;
|
72
|
+
});
|
73
|
+
}
|
74
|
+
createMany(objects) {
|
75
|
+
return __awaiter(this, void 0, void 0, function* () {
|
76
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
77
|
+
const records = objects.map((object) => transformObjectLinks(object));
|
78
|
+
const response = yield (0, api_1.bulkInsertTableRecords)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f") }, body: { records } }, fetchProps));
|
79
|
+
const finalObjects = yield this.any(...response.recordIDs.map((id) => this.filter('id', id))).getAll();
|
80
|
+
if (finalObjects.length !== objects.length) {
|
81
|
+
throw new Error('The server failed to save some records');
|
82
|
+
}
|
83
|
+
return finalObjects;
|
84
|
+
});
|
85
|
+
}
|
86
|
+
read(recordId) {
|
87
|
+
return __awaiter(this, void 0, void 0, function* () {
|
88
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
89
|
+
const response = yield (0, api_1.getRecord)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId } }, fetchProps));
|
90
|
+
return __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), response);
|
91
|
+
});
|
92
|
+
}
|
93
|
+
update(recordId, object) {
|
94
|
+
return __awaiter(this, void 0, void 0, function* () {
|
95
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
96
|
+
const response = yield (0, api_1.updateRecordWithID)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId }, body: object }, fetchProps));
|
97
|
+
const item = yield this.read(response.id);
|
98
|
+
if (!item)
|
99
|
+
throw new Error('The server failed to save the record');
|
100
|
+
return item;
|
101
|
+
});
|
102
|
+
}
|
103
|
+
insert(recordId, object) {
|
104
|
+
return __awaiter(this, void 0, void 0, function* () {
|
105
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
106
|
+
const record = transformObjectLinks(object);
|
107
|
+
const response = yield (0, api_1.insertRecordWithID)(Object.assign({ pathParams: {
|
108
|
+
workspace: '{workspaceId}',
|
109
|
+
dbBranchName: '{dbBranch}',
|
110
|
+
tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"),
|
111
|
+
recordId
|
112
|
+
}, body: record }, fetchProps));
|
113
|
+
const finalObject = yield this.read(response.id);
|
114
|
+
if (!finalObject) {
|
115
|
+
throw new Error('The server failed to save the record');
|
116
|
+
}
|
117
|
+
return finalObject;
|
118
|
+
});
|
119
|
+
}
|
120
|
+
updateOrInsert(recordId, object) {
|
121
|
+
return __awaiter(this, void 0, void 0, function* () {
|
122
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
123
|
+
const response = yield (0, api_1.upsertRecordWithID)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId }, body: object }, fetchProps));
|
124
|
+
const item = yield this.read(response.id);
|
125
|
+
if (!item)
|
126
|
+
throw new Error('The server failed to save the record');
|
127
|
+
return item;
|
128
|
+
});
|
129
|
+
}
|
130
|
+
delete(recordId) {
|
131
|
+
return __awaiter(this, void 0, void 0, function* () {
|
132
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
133
|
+
yield (0, api_1.deleteRecord)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId } }, fetchProps));
|
134
|
+
});
|
135
|
+
}
|
136
|
+
query(query, options = {}) {
|
137
|
+
var _a, _b, _c;
|
138
|
+
return __awaiter(this, void 0, void 0, function* () {
|
139
|
+
const data = query.getQueryOptions();
|
140
|
+
const body = {
|
141
|
+
filter: Object.values(data.filter).some(Boolean) ? data.filter : undefined,
|
142
|
+
sort: (_a = (0, filters_1.buildSortFilter)(options === null || options === void 0 ? void 0 : options.sort)) !== null && _a !== void 0 ? _a : data.sort,
|
143
|
+
page: (_b = options === null || options === void 0 ? void 0 : options.page) !== null && _b !== void 0 ? _b : data.page,
|
144
|
+
columns: (_c = options === null || options === void 0 ? void 0 : options.columns) !== null && _c !== void 0 ? _c : data.columns
|
145
|
+
};
|
146
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
147
|
+
const { meta, records: objects } = yield (0, api_1.queryTable)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f") }, body }, fetchProps));
|
148
|
+
const records = objects.map((record) => __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), record));
|
149
|
+
// TODO: We should properly type this any
|
150
|
+
return new pagination_1.Page(query, meta, records);
|
151
|
+
});
|
152
|
+
}
|
153
|
+
}
|
154
|
+
exports.RestRepository = RestRepository;
|
155
|
+
_RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _RestRepository_table = new WeakMap(), _RestRepository_instances = new WeakSet(), _RestRepository_getFetchProps = function _RestRepository_getFetchProps() {
|
156
|
+
return __awaiter(this, void 0, void 0, function* () {
|
157
|
+
const branch = yield __classPrivateFieldGet(this, _RestRepository_client, "f").getBranch();
|
158
|
+
return {
|
159
|
+
fetchImpl: __classPrivateFieldGet(this, _RestRepository_fetch, "f"),
|
160
|
+
apiKey: __classPrivateFieldGet(this, _RestRepository_client, "f").options.apiKey,
|
161
|
+
apiUrl: '',
|
162
|
+
// Instead of using workspace and dbBranch, we inject a probably CNAME'd URL
|
163
|
+
workspacesApiUrl: (path, params) => {
|
164
|
+
var _a, _b;
|
165
|
+
const baseUrl = (_a = __classPrivateFieldGet(this, _RestRepository_client, "f").options.databaseURL) !== null && _a !== void 0 ? _a : '';
|
166
|
+
const hasBranch = (_b = params.dbBranchName) !== null && _b !== void 0 ? _b : params.branch;
|
167
|
+
const newPath = path.replace(/^\/db\/[^/]+/, hasBranch ? `:${branch}` : '');
|
168
|
+
return baseUrl + newPath;
|
169
|
+
}
|
170
|
+
};
|
171
|
+
});
|
172
|
+
};
|
173
|
+
class RestRespositoryFactory {
|
174
|
+
createRepository(client, table) {
|
175
|
+
return new RestRepository(client, table);
|
176
|
+
}
|
177
|
+
}
|
178
|
+
exports.RestRespositoryFactory = RestRespositoryFactory;
|
179
|
+
class BaseClient {
|
180
|
+
constructor(options, links = {}) {
|
181
|
+
_BaseClient_links.set(this, void 0);
|
182
|
+
_BaseClient_branch.set(this, void 0);
|
183
|
+
if (!options.databaseURL || !options.apiKey || !options.branch) {
|
184
|
+
throw new Error('Options databaseURL, apiKey and branch are required');
|
185
|
+
}
|
186
|
+
this.options = options;
|
187
|
+
__classPrivateFieldSet(this, _BaseClient_links, links, "f");
|
188
|
+
const factory = options.repositoryFactory || new RestRespositoryFactory();
|
189
|
+
this.db = new Proxy({}, {
|
190
|
+
get: (_target, prop) => {
|
191
|
+
if (typeof prop !== 'string')
|
192
|
+
throw new Error('Invalid table name');
|
193
|
+
return factory.createRepository(this, prop);
|
194
|
+
}
|
195
|
+
});
|
196
|
+
}
|
197
|
+
initObject(table, object) {
|
198
|
+
const o = {};
|
199
|
+
Object.assign(o, object);
|
200
|
+
const tableLinks = __classPrivateFieldGet(this, _BaseClient_links, "f")[table] || [];
|
201
|
+
for (const link of tableLinks) {
|
202
|
+
const [field, linkTable] = link;
|
203
|
+
const value = o[field];
|
204
|
+
if (value && typeof value === 'object') {
|
205
|
+
const { id } = value;
|
206
|
+
if (Object.keys(value).find((col) => col === 'id')) {
|
207
|
+
o[field] = this.initObject(linkTable, value);
|
208
|
+
}
|
209
|
+
else if (id) {
|
210
|
+
o[field] = {
|
211
|
+
id,
|
212
|
+
get: () => {
|
213
|
+
this.db[linkTable].read(id);
|
214
|
+
}
|
215
|
+
};
|
216
|
+
}
|
217
|
+
}
|
218
|
+
}
|
219
|
+
const db = this.db;
|
220
|
+
o.read = function () {
|
221
|
+
return db[table].read(o['id']);
|
222
|
+
};
|
223
|
+
o.update = function (data) {
|
224
|
+
return db[table].update(o['id'], data);
|
225
|
+
};
|
226
|
+
o.delete = function () {
|
227
|
+
return db[table].delete(o['id']);
|
228
|
+
};
|
229
|
+
for (const prop of ['read', 'update', 'delete']) {
|
230
|
+
Object.defineProperty(o, prop, { enumerable: false });
|
231
|
+
}
|
232
|
+
// TODO: links and rev links
|
233
|
+
Object.freeze(o);
|
234
|
+
return o;
|
235
|
+
}
|
236
|
+
getBranch() {
|
237
|
+
var e_1, _a;
|
238
|
+
return __awaiter(this, void 0, void 0, function* () {
|
239
|
+
if (__classPrivateFieldGet(this, _BaseClient_branch, "f"))
|
240
|
+
return __classPrivateFieldGet(this, _BaseClient_branch, "f");
|
241
|
+
const { branch: param } = this.options;
|
242
|
+
const strategies = Array.isArray(param) ? [...param] : [param];
|
243
|
+
const evaluateBranch = (strategy) => __awaiter(this, void 0, void 0, function* () {
|
244
|
+
return isBranchStrategyBuilder(strategy) ? yield strategy() : strategy;
|
245
|
+
});
|
246
|
+
try {
|
247
|
+
for (var strategies_1 = __asyncValues(strategies), strategies_1_1; strategies_1_1 = yield strategies_1.next(), !strategies_1_1.done;) {
|
248
|
+
const strategy = strategies_1_1.value;
|
249
|
+
const branch = yield evaluateBranch(strategy);
|
250
|
+
if (branch) {
|
251
|
+
__classPrivateFieldSet(this, _BaseClient_branch, branch, "f");
|
252
|
+
return branch;
|
253
|
+
}
|
254
|
+
}
|
255
|
+
}
|
256
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
257
|
+
finally {
|
258
|
+
try {
|
259
|
+
if (strategies_1_1 && !strategies_1_1.done && (_a = strategies_1.return)) yield _a.call(strategies_1);
|
260
|
+
}
|
261
|
+
finally { if (e_1) throw e_1.error; }
|
262
|
+
}
|
263
|
+
throw new Error('Unable to resolve branch value');
|
264
|
+
});
|
265
|
+
}
|
266
|
+
}
|
267
|
+
exports.BaseClient = BaseClient;
|
268
|
+
_BaseClient_links = new WeakMap(), _BaseClient_branch = new WeakMap();
|
269
|
+
const isBranchStrategyBuilder = (strategy) => {
|
270
|
+
return typeof strategy === 'function';
|
271
|
+
};
|
272
|
+
// TODO: We can find a better implementation for links
|
273
|
+
const transformObjectLinks = (object) => {
|
274
|
+
return Object.entries(object).reduce((acc, [key, value]) => {
|
275
|
+
if (value && typeof value === 'object' && typeof value.id === 'string') {
|
276
|
+
return Object.assign(Object.assign({}, acc), { [key]: value.id });
|
277
|
+
}
|
278
|
+
return Object.assign(Object.assign({}, acc), { [key]: value });
|
279
|
+
}, {});
|
280
|
+
};
|
@@ -1,16 +1,10 @@
|
|
1
|
-
import { XataRecord } from '..';
|
2
1
|
import { StringKeys, UnionToIntersection, Values } from '../util/types';
|
3
2
|
import { Query } from './query';
|
3
|
+
import { BaseData, Identifiable, XataRecord } from './record';
|
4
4
|
declare type Queries<T> = {
|
5
|
-
[key in keyof T as T[key] extends Query<any> ? key : never]: T[key];
|
5
|
+
[key in keyof T as T[key] extends Query<any, any> ? key : never]: T[key];
|
6
6
|
};
|
7
|
-
declare type
|
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'>;
|
7
|
+
export declare type Selectable<T extends BaseData> = T & Partial<Identifiable>;
|
14
8
|
export declare type SelectableColumn<O> = '*' | (O extends Array<unknown> ? never : O extends Record<string, any> ? '*' | Values<{
|
15
9
|
[K in StringKeys<O>]: O[K] extends Record<string, any> ? `${K}.${SelectableColumn<O[K]>}` : K;
|
16
10
|
}> : '');
|
package/dist/util/lang.d.ts
CHANGED
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@xata.io/client",
|
3
|
-
"version": "0.0.0-beta.
|
3
|
+
"version": "0.0.0-beta.ccaf25d",
|
4
4
|
"description": "Xata.io SDK for TypeScript and JavaScript",
|
5
5
|
"main": "./dist/index.js",
|
6
6
|
"types": "./dist/index.d.ts",
|
@@ -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": "ccaf25d542edca3697b426284844cf7b39bf2bf8"
|
24
24
|
}
|
package/tsconfig.json
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
{
|
2
|
+
"compilerOptions": {
|
3
|
+
"target": "ES6",
|
4
|
+
"lib": ["dom", "esnext"],
|
5
|
+
"allowJs": true,
|
6
|
+
"skipLibCheck": true,
|
7
|
+
"esModuleInterop": true,
|
8
|
+
"allowSyntheticDefaultImports": true,
|
9
|
+
"strict": true,
|
10
|
+
"forceConsistentCasingInFileNames": true,
|
11
|
+
"noFallthroughCasesInSwitch": true,
|
12
|
+
"module": "CommonJS",
|
13
|
+
"moduleResolution": "node",
|
14
|
+
"resolveJsonModule": true,
|
15
|
+
"isolatedModules": true,
|
16
|
+
"noEmit": false,
|
17
|
+
"outDir": "dist",
|
18
|
+
"declaration": true
|
19
|
+
},
|
20
|
+
"include": ["src"]
|
21
|
+
}
|