@xata.io/client 0.7.2 → 0.8.2
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 +1 -1
- package/CHANGELOG.md +22 -0
- package/dist/api/index.d.ts +6 -0
- package/dist/api/index.js +20 -1
- package/dist/client.d.ts +27 -0
- package/dist/client.js +131 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/plugins.d.ts +7 -0
- package/dist/plugins.js +6 -0
- package/dist/schema/filters.spec.js +3 -1
- package/dist/schema/index.d.ts +20 -3
- package/dist/schema/index.js +37 -6
- package/dist/schema/record.d.ts +4 -4
- package/dist/schema/repository.d.ts +22 -52
- package/dist/schema/repository.js +21 -127
- package/dist/schema/selection.spec.js +2 -1
- package/dist/schema/sorting.d.ts +9 -4
- package/dist/schema/sorting.js +9 -2
- package/dist/schema/sorting.spec.js +3 -1
- package/dist/search/index.d.ts +34 -0
- package/dist/search/index.js +55 -0
- package/dist/util/branches.d.ts +5 -0
- package/dist/util/branches.js +7 -0
- package/dist/util/types.d.ts +2 -2
- package/package.json +2 -2
package/.eslintrc.cjs
CHANGED
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,27 @@
|
|
1
1
|
# @xata.io/client
|
2
2
|
|
3
|
+
## 0.8.2
|
4
|
+
|
5
|
+
### Patch Changes
|
6
|
+
|
7
|
+
- 3d81e7a: Make db model references stable
|
8
|
+
|
9
|
+
## 0.8.1
|
10
|
+
|
11
|
+
### Patch Changes
|
12
|
+
|
13
|
+
- 5110261: Fix execution from the browser
|
14
|
+
- aa3d7e7: Allow sending sort as in the API
|
15
|
+
- 0047193: Add new plugin system for the SDK
|
16
|
+
- 43856a5: Add discriminated union search
|
17
|
+
|
18
|
+
## 0.8.0
|
19
|
+
|
20
|
+
### Patch Changes
|
21
|
+
|
22
|
+
- bde908e: Refactor client builder
|
23
|
+
- ea3eef8: Make records returned by the API readonly
|
24
|
+
|
3
25
|
## 0.7.2
|
4
26
|
|
5
27
|
### Patch Changes
|
package/dist/api/index.d.ts
CHANGED
@@ -1,7 +1,13 @@
|
|
1
|
+
import { XataPlugin, XataPluginOptions } from '../plugins';
|
2
|
+
import { XataApiClient } from './client';
|
1
3
|
import { operationsByTag } from './components';
|
2
4
|
import type * as Responses from './responses';
|
3
5
|
import type * as Schemas from './schemas';
|
4
6
|
export * from './client';
|
5
7
|
export * from './components';
|
8
|
+
export type { FetcherExtraProps, FetchImpl } from './fetcher';
|
6
9
|
export { operationsByTag as Operations };
|
7
10
|
export type { Responses, Schemas };
|
11
|
+
export declare class XataApiPlugin implements XataPlugin {
|
12
|
+
build(options: XataPluginOptions): Promise<XataApiClient>;
|
13
|
+
}
|
package/dist/api/index.js
CHANGED
@@ -13,9 +13,28 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
13
13
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
15
|
};
|
16
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
17
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
18
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
19
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
20
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
21
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
22
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
23
|
+
});
|
24
|
+
};
|
16
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
-
exports.Operations = void 0;
|
26
|
+
exports.XataApiPlugin = exports.Operations = void 0;
|
27
|
+
const client_1 = require("./client");
|
18
28
|
const components_1 = require("./components");
|
19
29
|
Object.defineProperty(exports, "Operations", { enumerable: true, get: function () { return components_1.operationsByTag; } });
|
20
30
|
__exportStar(require("./client"), exports);
|
21
31
|
__exportStar(require("./components"), exports);
|
32
|
+
class XataApiPlugin {
|
33
|
+
build(options) {
|
34
|
+
return __awaiter(this, void 0, void 0, function* () {
|
35
|
+
const { fetchImpl, apiKey } = yield options.getFetchProps();
|
36
|
+
return new client_1.XataApiClient({ fetch: fetchImpl, apiKey });
|
37
|
+
});
|
38
|
+
}
|
39
|
+
}
|
40
|
+
exports.XataApiPlugin = XataApiPlugin;
|
package/dist/client.d.ts
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
import { FetchImpl } from './api/fetcher';
|
2
|
+
import { XataPlugin } from './plugins';
|
3
|
+
import { SchemaPlugin } from './schema';
|
4
|
+
import { BaseData } from './schema/record';
|
5
|
+
import { LinkDictionary } from './schema/repository';
|
6
|
+
import { SearchPlugin } from './search';
|
7
|
+
import { BranchStrategyOption } from './util/branches';
|
8
|
+
import { StringKeys } from './util/types';
|
9
|
+
export declare type BaseClientOptions = {
|
10
|
+
fetch?: FetchImpl;
|
11
|
+
apiKey?: string;
|
12
|
+
databaseURL?: string;
|
13
|
+
branch?: BranchStrategyOption;
|
14
|
+
};
|
15
|
+
export declare const buildClient: <Plugins extends Record<string, XataPlugin> = {}>(plugins?: Plugins | undefined) => ClientConstructor<Plugins>;
|
16
|
+
export interface ClientConstructor<Plugins extends Record<string, XataPlugin>> {
|
17
|
+
new <Schemas extends Record<string, BaseData>>(options?: Partial<BaseClientOptions>, links?: LinkDictionary): Omit<{
|
18
|
+
db: Awaited<ReturnType<SchemaPlugin<Schemas>['build']>>;
|
19
|
+
search: Awaited<ReturnType<SearchPlugin<Schemas>['build']>>;
|
20
|
+
}, keyof Plugins> & {
|
21
|
+
[Key in StringKeys<NonNullable<Plugins>>]: Awaited<ReturnType<NonNullable<Plugins>[Key]['build']>>;
|
22
|
+
};
|
23
|
+
}
|
24
|
+
declare const BaseClient_base: ClientConstructor<{}>;
|
25
|
+
export declare class BaseClient extends BaseClient_base<Record<string, any>> {
|
26
|
+
}
|
27
|
+
export {};
|
package/dist/client.js
ADDED
@@ -0,0 +1,131 @@
|
|
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 __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
12
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
13
|
+
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");
|
14
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
15
|
+
};
|
16
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
17
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
18
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
19
|
+
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");
|
20
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
30
|
+
exports.BaseClient = exports.buildClient = void 0;
|
31
|
+
const schema_1 = require("./schema");
|
32
|
+
const search_1 = require("./search");
|
33
|
+
const branches_1 = require("./util/branches");
|
34
|
+
const config_1 = require("./util/config");
|
35
|
+
const fetch_1 = require("./util/fetch");
|
36
|
+
const buildClient = (plugins) => {
|
37
|
+
var _instances, _branch, _parseOptions, _getFetchProps, _evaluateBranch, _a;
|
38
|
+
return _a = class {
|
39
|
+
constructor(options = {}, links) {
|
40
|
+
_instances.add(this);
|
41
|
+
_branch.set(this, void 0);
|
42
|
+
const safeOptions = __classPrivateFieldGet(this, _instances, "m", _parseOptions).call(this, options);
|
43
|
+
const namespaces = Object.assign({ db: new schema_1.SchemaPlugin(links), search: new search_1.SearchPlugin() }, plugins);
|
44
|
+
for (const [key, namespace] of Object.entries(namespaces)) {
|
45
|
+
if (!namespace)
|
46
|
+
continue;
|
47
|
+
const result = namespace.build({ getFetchProps: () => __classPrivateFieldGet(this, _instances, "m", _getFetchProps).call(this, safeOptions) });
|
48
|
+
if (result instanceof Promise) {
|
49
|
+
void result.then((namespace) => {
|
50
|
+
// @ts-ignore
|
51
|
+
this[key] = namespace;
|
52
|
+
});
|
53
|
+
}
|
54
|
+
else {
|
55
|
+
// @ts-ignore
|
56
|
+
this[key] = result;
|
57
|
+
}
|
58
|
+
}
|
59
|
+
}
|
60
|
+
},
|
61
|
+
_branch = new WeakMap(),
|
62
|
+
_instances = new WeakSet(),
|
63
|
+
_parseOptions = function _parseOptions(options) {
|
64
|
+
const fetch = (0, fetch_1.getFetchImplementation)(options === null || options === void 0 ? void 0 : options.fetch);
|
65
|
+
const databaseURL = (options === null || options === void 0 ? void 0 : options.databaseURL) || (0, config_1.getDatabaseURL)();
|
66
|
+
const apiKey = (options === null || options === void 0 ? void 0 : options.apiKey) || (0, config_1.getAPIKey)();
|
67
|
+
const branch = () => __awaiter(this, void 0, void 0, function* () {
|
68
|
+
return (options === null || options === void 0 ? void 0 : options.branch)
|
69
|
+
? yield __classPrivateFieldGet(this, _instances, "m", _evaluateBranch).call(this, options.branch)
|
70
|
+
: yield (0, config_1.getCurrentBranchName)({ apiKey, databaseURL, fetchImpl: options === null || options === void 0 ? void 0 : options.fetch });
|
71
|
+
});
|
72
|
+
if (!databaseURL || !apiKey) {
|
73
|
+
throw new Error('Options databaseURL and apiKey are required');
|
74
|
+
}
|
75
|
+
return { fetch, databaseURL, apiKey, branch };
|
76
|
+
},
|
77
|
+
_getFetchProps = function _getFetchProps({ fetch, apiKey, databaseURL, branch }) {
|
78
|
+
return __awaiter(this, void 0, void 0, function* () {
|
79
|
+
const branchValue = yield __classPrivateFieldGet(this, _instances, "m", _evaluateBranch).call(this, branch);
|
80
|
+
if (!branchValue)
|
81
|
+
throw new Error('Unable to resolve branch value');
|
82
|
+
return {
|
83
|
+
fetchImpl: fetch,
|
84
|
+
apiKey,
|
85
|
+
apiUrl: '',
|
86
|
+
// Instead of using workspace and dbBranch, we inject a probably CNAME'd URL
|
87
|
+
workspacesApiUrl: (path, params) => {
|
88
|
+
var _a;
|
89
|
+
const hasBranch = (_a = params.dbBranchName) !== null && _a !== void 0 ? _a : params.branch;
|
90
|
+
const newPath = path.replace(/^\/db\/[^/]+/, hasBranch ? `:${branchValue}` : '');
|
91
|
+
return databaseURL + newPath;
|
92
|
+
}
|
93
|
+
};
|
94
|
+
});
|
95
|
+
},
|
96
|
+
_evaluateBranch = function _evaluateBranch(param) {
|
97
|
+
var e_1, _a;
|
98
|
+
return __awaiter(this, void 0, void 0, function* () {
|
99
|
+
if (__classPrivateFieldGet(this, _branch, "f"))
|
100
|
+
return __classPrivateFieldGet(this, _branch, "f");
|
101
|
+
if (!param)
|
102
|
+
return undefined;
|
103
|
+
const strategies = Array.isArray(param) ? [...param] : [param];
|
104
|
+
const evaluateBranch = (strategy) => __awaiter(this, void 0, void 0, function* () {
|
105
|
+
return (0, branches_1.isBranchStrategyBuilder)(strategy) ? yield strategy() : strategy;
|
106
|
+
});
|
107
|
+
try {
|
108
|
+
for (var strategies_1 = __asyncValues(strategies), strategies_1_1; strategies_1_1 = yield strategies_1.next(), !strategies_1_1.done;) {
|
109
|
+
const strategy = strategies_1_1.value;
|
110
|
+
const branch = yield evaluateBranch(strategy);
|
111
|
+
if (branch) {
|
112
|
+
__classPrivateFieldSet(this, _branch, branch, "f");
|
113
|
+
return branch;
|
114
|
+
}
|
115
|
+
}
|
116
|
+
}
|
117
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
118
|
+
finally {
|
119
|
+
try {
|
120
|
+
if (strategies_1_1 && !strategies_1_1.done && (_a = strategies_1.return)) yield _a.call(strategies_1);
|
121
|
+
}
|
122
|
+
finally { if (e_1) throw e_1.error; }
|
123
|
+
}
|
124
|
+
});
|
125
|
+
},
|
126
|
+
_a;
|
127
|
+
};
|
128
|
+
exports.buildClient = buildClient;
|
129
|
+
class BaseClient extends (0, exports.buildClient)() {
|
130
|
+
}
|
131
|
+
exports.BaseClient = BaseClient;
|
package/dist/index.d.ts
CHANGED
@@ -3,5 +3,8 @@ export declare class XataError extends Error {
|
|
3
3
|
constructor(message: string, status: number);
|
4
4
|
}
|
5
5
|
export * from './api';
|
6
|
+
export * from './plugins';
|
7
|
+
export * from './client';
|
6
8
|
export * from './schema';
|
9
|
+
export * from './search';
|
7
10
|
export * from './util/config';
|
package/dist/index.js
CHANGED
@@ -23,5 +23,8 @@ class XataError extends Error {
|
|
23
23
|
}
|
24
24
|
exports.XataError = XataError;
|
25
25
|
__exportStar(require("./api"), exports);
|
26
|
+
__exportStar(require("./plugins"), exports);
|
27
|
+
__exportStar(require("./client"), exports);
|
26
28
|
__exportStar(require("./schema"), exports);
|
29
|
+
__exportStar(require("./search"), exports);
|
27
30
|
__exportStar(require("./util/config"), exports);
|
@@ -0,0 +1,7 @@
|
|
1
|
+
import { FetcherExtraProps } from './api/fetcher';
|
2
|
+
export declare abstract class XataPlugin {
|
3
|
+
abstract build(options: XataPluginOptions): unknown | Promise<unknown>;
|
4
|
+
}
|
5
|
+
export declare type XataPluginOptions = {
|
6
|
+
getFetchProps: () => Promise<FetcherExtraProps>;
|
7
|
+
};
|
package/dist/plugins.js
ADDED
@@ -1,5 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
4
|
+
const vitest_1 = require("vitest");
|
3
5
|
// Single column with implicit is
|
4
6
|
const singleColumnWithImplicitIs = { name: 'r2' };
|
5
7
|
// Single column with explicit is
|
@@ -170,6 +172,6 @@ const filterWithInvalidNestedObjectPropertyType = { settings: { plan: 42 } };
|
|
170
172
|
// Filter with invalid property $is type
|
171
173
|
// @ts-expect-error
|
172
174
|
const filterWithInvalidOperator = { name: { $is: 42 } };
|
173
|
-
test('fake test', () => {
|
175
|
+
(0, vitest_1.test)('fake test', () => {
|
174
176
|
// This is a fake test to make sure that the type definitions in this file are working
|
175
177
|
});
|
package/dist/schema/index.d.ts
CHANGED
@@ -1,7 +1,24 @@
|
|
1
|
+
import { XataPlugin, XataPluginOptions } from '../plugins';
|
2
|
+
import { BaseData } from './record';
|
3
|
+
import { LinkDictionary, Repository } from './repository';
|
1
4
|
export * from './operators';
|
2
5
|
export * from './pagination';
|
3
6
|
export { Query } from './query';
|
4
7
|
export { isIdentifiable, isXataRecord } from './record';
|
5
|
-
export type { Identifiable, XataRecord } from './record';
|
6
|
-
export {
|
7
|
-
export type {
|
8
|
+
export type { BaseData, EditableData, Identifiable, XataRecord } from './record';
|
9
|
+
export { Repository, RestRepository } from './repository';
|
10
|
+
export type { LinkDictionary } from './repository';
|
11
|
+
export * from './selection';
|
12
|
+
export declare type SchemaDefinition = {
|
13
|
+
table: string;
|
14
|
+
links?: LinkDictionary;
|
15
|
+
};
|
16
|
+
export declare type SchemaPluginResult<Schemas extends Record<string, BaseData>> = {
|
17
|
+
[Key in keyof Schemas]: Repository<Schemas[Key]>;
|
18
|
+
};
|
19
|
+
export declare class SchemaPlugin<Schemas extends Record<string, BaseData>> extends XataPlugin {
|
20
|
+
#private;
|
21
|
+
private links?;
|
22
|
+
constructor(links?: LinkDictionary | undefined);
|
23
|
+
build(options: XataPluginOptions): SchemaPluginResult<Schemas>;
|
24
|
+
}
|
package/dist/schema/index.js
CHANGED
@@ -13,8 +13,17 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
13
13
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
15
|
};
|
16
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
17
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
18
|
+
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");
|
19
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
20
|
+
};
|
21
|
+
var _SchemaPlugin_tables;
|
16
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
-
exports.
|
23
|
+
exports.SchemaPlugin = exports.RestRepository = exports.Repository = exports.isXataRecord = exports.isIdentifiable = exports.Query = void 0;
|
24
|
+
const plugins_1 = require("../plugins");
|
25
|
+
const lang_1 = require("../util/lang");
|
26
|
+
const repository_1 = require("./repository");
|
18
27
|
__exportStar(require("./operators"), exports);
|
19
28
|
__exportStar(require("./pagination"), exports);
|
20
29
|
var query_1 = require("./query");
|
@@ -22,8 +31,30 @@ Object.defineProperty(exports, "Query", { enumerable: true, get: function () { r
|
|
22
31
|
var record_1 = require("./record");
|
23
32
|
Object.defineProperty(exports, "isIdentifiable", { enumerable: true, get: function () { return record_1.isIdentifiable; } });
|
24
33
|
Object.defineProperty(exports, "isXataRecord", { enumerable: true, get: function () { return record_1.isXataRecord; } });
|
25
|
-
var
|
26
|
-
Object.defineProperty(exports, "
|
27
|
-
Object.defineProperty(exports, "
|
28
|
-
|
29
|
-
|
34
|
+
var repository_2 = require("./repository");
|
35
|
+
Object.defineProperty(exports, "Repository", { enumerable: true, get: function () { return repository_2.Repository; } });
|
36
|
+
Object.defineProperty(exports, "RestRepository", { enumerable: true, get: function () { return repository_2.RestRepository; } });
|
37
|
+
__exportStar(require("./selection"), exports);
|
38
|
+
class SchemaPlugin extends plugins_1.XataPlugin {
|
39
|
+
constructor(links) {
|
40
|
+
super();
|
41
|
+
this.links = links;
|
42
|
+
_SchemaPlugin_tables.set(this, {});
|
43
|
+
}
|
44
|
+
build(options) {
|
45
|
+
const { getFetchProps } = options;
|
46
|
+
const links = this.links;
|
47
|
+
const db = new Proxy({}, {
|
48
|
+
get: (_target, table) => {
|
49
|
+
if (!(0, lang_1.isString)(table))
|
50
|
+
throw new Error('Invalid table name');
|
51
|
+
if (!__classPrivateFieldGet(this, _SchemaPlugin_tables, "f")[table])
|
52
|
+
__classPrivateFieldGet(this, _SchemaPlugin_tables, "f")[table] = new repository_1.RestRepository({ db, getFetchProps, table, links });
|
53
|
+
return __classPrivateFieldGet(this, _SchemaPlugin_tables, "f")[table];
|
54
|
+
}
|
55
|
+
});
|
56
|
+
return db;
|
57
|
+
}
|
58
|
+
}
|
59
|
+
exports.SchemaPlugin = SchemaPlugin;
|
60
|
+
_SchemaPlugin_tables = new WeakMap();
|
package/dist/schema/record.d.ts
CHANGED
@@ -27,14 +27,14 @@ export interface XataRecord extends Identifiable {
|
|
27
27
|
/**
|
28
28
|
* Retrieves a refreshed copy of the current record from the database.
|
29
29
|
*/
|
30
|
-
read(): Promise<SelectedPick<this, ['*']
|
30
|
+
read(): Promise<Readonly<SelectedPick<this, ['*']>> | null>;
|
31
31
|
/**
|
32
32
|
* Performs a partial update of the current record. On success a new object is
|
33
33
|
* returned and the current object is not mutated.
|
34
34
|
* @param data The columns and their values that have to be updated.
|
35
35
|
* @returns A new record containing the latest values for all the columns of the current record.
|
36
36
|
*/
|
37
|
-
update(
|
37
|
+
update(partialUpdate: Partial<EditableData<Omit<this, keyof XataRecord>>>): Promise<Readonly<SelectedPick<this, ['*']>>>;
|
38
38
|
/**
|
39
39
|
* Performs a deletion of the current record in the database.
|
40
40
|
*
|
@@ -46,14 +46,14 @@ export declare type Link<Record extends XataRecord> = Omit<XataRecord, 'read' |
|
|
46
46
|
/**
|
47
47
|
* Retrieves a refreshed copy of the current record from the database.
|
48
48
|
*/
|
49
|
-
read(): Promise<SelectedPick<Record, ['*']
|
49
|
+
read(): Promise<Readonly<SelectedPick<Record, ['*']>> | null>;
|
50
50
|
/**
|
51
51
|
* Performs a partial update of the current record. On success a new object is
|
52
52
|
* returned and the current object is not mutated.
|
53
53
|
* @param data The columns and their values that have to be updated.
|
54
54
|
* @returns A new record containing the latest values for all the columns of the current record.
|
55
55
|
*/
|
56
|
-
update(
|
56
|
+
update(partialUpdate: Partial<EditableData<Omit<Record, keyof XataRecord>>>): Promise<Readonly<SelectedPick<Record, ['*']>>>;
|
57
57
|
};
|
58
58
|
export declare function isIdentifiable(x: any): x is Identifiable & Record<string, unknown>;
|
59
59
|
export declare function isXataRecord(x: any): x is XataRecord & Record<string, unknown>;
|
@@ -1,5 +1,6 @@
|
|
1
|
-
import {
|
2
|
-
import {
|
1
|
+
import { SchemaPluginResult } from '.';
|
2
|
+
import { FetcherExtraProps } from '../api/fetcher';
|
3
|
+
import { Dictionary } from '../util/types';
|
3
4
|
import { Page } from './pagination';
|
4
5
|
import { Query } from './query';
|
5
6
|
import { BaseData, EditableData, Identifiable, XataRecord } from './record';
|
@@ -9,53 +10,53 @@ export declare type LinkDictionary = Dictionary<TableLink[]>;
|
|
9
10
|
/**
|
10
11
|
* Common interface for performing operations on a table.
|
11
12
|
*/
|
12
|
-
export declare abstract class Repository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record, SelectedPick<Record, ['*']
|
13
|
-
abstract create(object: EditableData<Data> & Partial<Identifiable>): Promise<SelectedPick<Record, ['*']
|
13
|
+
export declare abstract class Repository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record, Readonly<SelectedPick<Record, ['*']>>> {
|
14
|
+
abstract create(object: EditableData<Data> & Partial<Identifiable>): Promise<Readonly<SelectedPick<Record, ['*']>>>;
|
14
15
|
/**
|
15
16
|
* Creates a single record in the table with a unique id.
|
16
17
|
* @param id The unique id.
|
17
18
|
* @param object Object containing the column names with their values to be stored in the table.
|
18
19
|
* @returns The full persisted record.
|
19
20
|
*/
|
20
|
-
abstract create(id: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']
|
21
|
+
abstract create(id: string, object: EditableData<Data>): Promise<Readonly<SelectedPick<Record, ['*']>>>;
|
21
22
|
/**
|
22
23
|
* Creates multiple records in the table.
|
23
24
|
* @param objects Array of objects with the column names and the values to be stored in the table.
|
24
25
|
* @returns Array of the persisted records.
|
25
26
|
*/
|
26
|
-
abstract create(objects: Array<EditableData<Data> & Partial<Identifiable>>): Promise<SelectedPick<Record, ['*']
|
27
|
+
abstract create(objects: Array<EditableData<Data> & Partial<Identifiable>>): Promise<Readonly<SelectedPick<Record, ['*']>>[]>;
|
27
28
|
/**
|
28
29
|
* Queries a single record from the table given its unique id.
|
29
30
|
* @param id The unique id.
|
30
31
|
* @returns The persisted record for the given id or null if the record could not be found.
|
31
32
|
*/
|
32
|
-
abstract read(id: string): Promise<SelectedPick<Record, ['*']> | null
|
33
|
+
abstract read(id: string): Promise<Readonly<SelectedPick<Record, ['*']> | null>>;
|
33
34
|
/**
|
34
35
|
* Partially update a single record.
|
35
36
|
* @param object An object with its id and the columns to be updated.
|
36
37
|
* @returns The full persisted record.
|
37
38
|
*/
|
38
|
-
abstract update(object: Partial<EditableData<Data>> & Identifiable): Promise<SelectedPick<Record, ['*']
|
39
|
+
abstract update(object: Partial<EditableData<Data>> & Identifiable): Promise<Readonly<SelectedPick<Record, ['*']>>>;
|
39
40
|
/**
|
40
41
|
* Partially update a single record given its unique id.
|
41
42
|
* @param id The unique id.
|
42
43
|
* @param object The column names and their values that have to be updated.
|
43
44
|
* @returns The full persisted record.
|
44
45
|
*/
|
45
|
-
abstract update(id: string, object: Partial<EditableData<Data>>): Promise<SelectedPick<Record, ['*']
|
46
|
+
abstract update(id: string, object: Partial<EditableData<Data>>): Promise<Readonly<SelectedPick<Record, ['*']>>>;
|
46
47
|
/**
|
47
48
|
* Partially updates multiple records.
|
48
49
|
* @param objects An array of objects with their ids and columns to be updated.
|
49
50
|
* @returns Array of the persisted records.
|
50
51
|
*/
|
51
|
-
abstract update(objects: Array<Partial<EditableData<Data>> & Identifiable>): Promise<SelectedPick<Record, ['*']
|
52
|
+
abstract update(objects: Array<Partial<EditableData<Data>> & Identifiable>): Promise<Readonly<SelectedPick<Record, ['*']>>[]>;
|
52
53
|
/**
|
53
54
|
* Creates or updates a single record. If a record exists with the given id,
|
54
55
|
* it will be update, otherwise a new record will be created.
|
55
56
|
* @param object Object containing the column names with their values to be persisted in the table.
|
56
57
|
* @returns The full persisted record.
|
57
58
|
*/
|
58
|
-
abstract createOrUpdate(object: EditableData<Data> & Identifiable): Promise<SelectedPick<Record, ['*']
|
59
|
+
abstract createOrUpdate(object: EditableData<Data> & Identifiable): Promise<Readonly<SelectedPick<Record, ['*']>>>;
|
59
60
|
/**
|
60
61
|
* Creates or updates a single record. If a record exists with the given id,
|
61
62
|
* it will be update, otherwise a new record will be created.
|
@@ -63,14 +64,14 @@ export declare abstract class Repository<Data extends BaseData, Record extends X
|
|
63
64
|
* @param object The column names and the values to be persisted.
|
64
65
|
* @returns The full persisted record.
|
65
66
|
*/
|
66
|
-
abstract createOrUpdate(id: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']
|
67
|
+
abstract createOrUpdate(id: string, object: EditableData<Data>): Promise<Readonly<SelectedPick<Record, ['*']>>>;
|
67
68
|
/**
|
68
69
|
* Creates or updates a single record. If a record exists with the given id,
|
69
70
|
* it will be update, otherwise a new record will be created.
|
70
71
|
* @param objects Array of objects with the column names and the values to be stored in the table.
|
71
72
|
* @returns Array of the persisted records.
|
72
73
|
*/
|
73
|
-
abstract createOrUpdate(objects: Array<EditableData<Data> & Identifiable>): Promise<SelectedPick<Record, ['*']
|
74
|
+
abstract createOrUpdate(objects: Array<EditableData<Data> & Identifiable>): Promise<Readonly<SelectedPick<Record, ['*']>>[]>;
|
74
75
|
/**
|
75
76
|
* Deletes a record given its unique id.
|
76
77
|
* @param id The unique id.
|
@@ -106,9 +107,15 @@ export declare abstract class Repository<Data extends BaseData, Record extends X
|
|
106
107
|
}): Promise<SelectedPick<Record, ['*']>[]>;
|
107
108
|
abstract query<Result extends XataRecord>(query: Query<Record, Result>): Promise<Page<Record, Result>>;
|
108
109
|
}
|
109
|
-
export declare class RestRepository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record, SelectedPick<Record, ['*']>> {
|
110
|
+
export declare class RestRepository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record, SelectedPick<Record, ['*']>> implements Repository<Data, Record> {
|
110
111
|
#private;
|
111
|
-
|
112
|
+
db: SchemaPluginResult<any>;
|
113
|
+
constructor(options: {
|
114
|
+
table: string;
|
115
|
+
links?: LinkDictionary;
|
116
|
+
getFetchProps: () => Promise<FetcherExtraProps>;
|
117
|
+
db: SchemaPluginResult<any>;
|
118
|
+
});
|
112
119
|
create(object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
|
113
120
|
create(recordId: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
|
114
121
|
create(objects: EditableData<Data>[]): Promise<SelectedPick<Record, ['*']>[]>;
|
@@ -125,41 +132,4 @@ export declare class RestRepository<Data extends BaseData, Record extends XataRe
|
|
125
132
|
}): Promise<SelectedPick<Record, ['*']>[]>;
|
126
133
|
query<Result extends XataRecord>(query: Query<Record, Result>): Promise<Page<Record, Result>>;
|
127
134
|
}
|
128
|
-
interface RepositoryFactory {
|
129
|
-
createRepository<Data extends BaseData>(client: BaseClient<any>, table: string, links?: LinkDictionary): Repository<Data & XataRecord>;
|
130
|
-
}
|
131
|
-
export declare class RestRespositoryFactory implements RepositoryFactory {
|
132
|
-
createRepository<Data extends BaseData>(client: BaseClient<any>, table: string, links?: LinkDictionary): Repository<Data & XataRecord>;
|
133
|
-
}
|
134
|
-
declare type BranchStrategyValue = string | undefined | null;
|
135
|
-
declare type BranchStrategyBuilder = () => BranchStrategyValue | Promise<BranchStrategyValue>;
|
136
|
-
declare type BranchStrategy = BranchStrategyValue | BranchStrategyBuilder;
|
137
|
-
declare type BranchStrategyOption = NonNullable<BranchStrategy | BranchStrategy[]>;
|
138
|
-
export declare type XataClientOptions = {
|
139
|
-
/**
|
140
|
-
* Fetch implementation. This option is only required if the runtime does not include a fetch implementation
|
141
|
-
* available in the global scope. If you are running your code on Deno or Cloudflare workers for example,
|
142
|
-
* you won't need to provide a specific fetch implementation. But for most versions of Node.js you'll need
|
143
|
-
* to provide one. Such as cross-fetch, node-fetch or isomorphic-fetch.
|
144
|
-
*/
|
145
|
-
fetch?: FetchImpl;
|
146
|
-
databaseURL?: string;
|
147
|
-
branch?: BranchStrategyOption;
|
148
|
-
/**
|
149
|
-
* API key to be used. You can create one in your account settings at https://app.xata.io/settings.
|
150
|
-
*/
|
151
|
-
apiKey?: string;
|
152
|
-
repositoryFactory?: RepositoryFactory;
|
153
|
-
};
|
154
|
-
export declare class BaseClient<D extends Record<string, Repository<any>> = Record<string, Repository<any>>> {
|
155
|
-
options: XataClientOptions;
|
156
|
-
db: D;
|
157
|
-
constructor(options?: XataClientOptions, links?: LinkDictionary);
|
158
|
-
search<Tables extends StringKeys<D>>(query: string, options?: {
|
159
|
-
fuzziness?: number;
|
160
|
-
tables?: Tables[];
|
161
|
-
}): Promise<{
|
162
|
-
[Model in GetArrayInnerType<NonNullable<NonNullable<typeof options>['tables']>>]: Awaited<ReturnType<D[Model]['search']>>;
|
163
|
-
}>;
|
164
|
-
}
|
165
135
|
export {};
|
@@ -19,20 +19,11 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
19
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
20
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
21
21
|
};
|
22
|
-
var
|
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_links, _RestRepository_branch, _RestRepository_getFetchProps, _RestRepository_getBranch, _RestRepository_insertRecordWithoutId, _RestRepository_insertRecordWithId, _RestRepository_bulkInsertTableRecords, _RestRepository_updateRecordWithID, _RestRepository_upsertRecordWithID, _RestRepository_deleteRecord, _RestRepository_initObject;
|
22
|
+
var _RestRepository_instances, _RestRepository_table, _RestRepository_links, _RestRepository_getFetchProps, _RestRepository_insertRecordWithoutId, _RestRepository_insertRecordWithId, _RestRepository_bulkInsertTableRecords, _RestRepository_updateRecordWithID, _RestRepository_upsertRecordWithID, _RestRepository_deleteRecord, _RestRepository_initObject;
|
30
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
31
|
-
exports.
|
24
|
+
exports.RestRepository = exports.Repository = void 0;
|
32
25
|
const api_1 = require("../api");
|
33
|
-
const fetch_1 = require("../util/fetch");
|
34
26
|
const lang_1 = require("../util/lang");
|
35
|
-
const config_1 = require("../util/config");
|
36
27
|
const pagination_1 = require("./pagination");
|
37
28
|
const query_1 = require("./query");
|
38
29
|
const record_1 = require("./record");
|
@@ -44,18 +35,17 @@ class Repository extends query_1.Query {
|
|
44
35
|
}
|
45
36
|
exports.Repository = Repository;
|
46
37
|
class RestRepository extends query_1.Query {
|
47
|
-
constructor(
|
48
|
-
|
38
|
+
constructor(options) {
|
39
|
+
var _a;
|
40
|
+
super(null, options.table, {});
|
49
41
|
_RestRepository_instances.add(this);
|
50
|
-
_RestRepository_client.set(this, void 0);
|
51
|
-
_RestRepository_fetch.set(this, void 0);
|
52
42
|
_RestRepository_table.set(this, void 0);
|
53
43
|
_RestRepository_links.set(this, void 0);
|
54
|
-
|
55
|
-
__classPrivateFieldSet(this,
|
56
|
-
__classPrivateFieldSet(this,
|
57
|
-
__classPrivateFieldSet(this,
|
58
|
-
|
44
|
+
_RestRepository_getFetchProps.set(this, void 0);
|
45
|
+
__classPrivateFieldSet(this, _RestRepository_table, options.table, "f");
|
46
|
+
__classPrivateFieldSet(this, _RestRepository_links, (_a = options.links) !== null && _a !== void 0 ? _a : {}, "f");
|
47
|
+
__classPrivateFieldSet(this, _RestRepository_getFetchProps, options.getFetchProps, "f");
|
48
|
+
this.db = options.db;
|
59
49
|
}
|
60
50
|
create(a, b) {
|
61
51
|
return __awaiter(this, void 0, void 0, function* () {
|
@@ -85,7 +75,7 @@ class RestRepository extends query_1.Query {
|
|
85
75
|
// TODO: Add column support: https://github.com/xataio/openapi/issues/139
|
86
76
|
read(recordId) {
|
87
77
|
return __awaiter(this, void 0, void 0, function* () {
|
88
|
-
const fetchProps = yield __classPrivateFieldGet(this,
|
78
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
|
89
79
|
try {
|
90
80
|
const response = yield (0, api_1.getRecord)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId } }, fetchProps));
|
91
81
|
return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_initObject).call(this, __classPrivateFieldGet(this, _RestRepository_table, "f"), response);
|
@@ -166,7 +156,7 @@ class RestRepository extends query_1.Query {
|
|
166
156
|
}
|
167
157
|
search(query, options = {}) {
|
168
158
|
return __awaiter(this, void 0, void 0, function* () {
|
169
|
-
const fetchProps = yield __classPrivateFieldGet(this,
|
159
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
|
170
160
|
const { records } = yield (0, api_1.searchBranch)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}' }, body: { tables: [__classPrivateFieldGet(this, _RestRepository_table, "f")], query, fuzziness: options.fuzziness } }, fetchProps));
|
171
161
|
return records.map((item) => __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_initObject).call(this, __classPrivateFieldGet(this, _RestRepository_table, "f"), item));
|
172
162
|
});
|
@@ -181,7 +171,7 @@ class RestRepository extends query_1.Query {
|
|
181
171
|
page: data.page,
|
182
172
|
columns: data.columns
|
183
173
|
};
|
184
|
-
const fetchProps = yield __classPrivateFieldGet(this,
|
174
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
|
185
175
|
const { meta, records: objects } = yield (0, api_1.queryTable)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f") }, body }, fetchProps));
|
186
176
|
const records = objects.map((record) => __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_initObject).call(this, __classPrivateFieldGet(this, _RestRepository_table, "f"), record));
|
187
177
|
return new pagination_1.Page(query, meta, records);
|
@@ -189,60 +179,9 @@ class RestRepository extends query_1.Query {
|
|
189
179
|
}
|
190
180
|
}
|
191
181
|
exports.RestRepository = RestRepository;
|
192
|
-
|
193
|
-
var _a;
|
194
|
-
return __awaiter(this, void 0, void 0, function* () {
|
195
|
-
const branch = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getBranch).call(this);
|
196
|
-
const apiKey = (_a = __classPrivateFieldGet(this, _RestRepository_client, "f").options.apiKey) !== null && _a !== void 0 ? _a : (0, config_1.getAPIKey)();
|
197
|
-
if (!apiKey) {
|
198
|
-
throw new Error('Could not resolve a valid apiKey');
|
199
|
-
}
|
200
|
-
return {
|
201
|
-
fetchImpl: __classPrivateFieldGet(this, _RestRepository_fetch, "f"),
|
202
|
-
apiKey,
|
203
|
-
apiUrl: '',
|
204
|
-
// Instead of using workspace and dbBranch, we inject a probably CNAME'd URL
|
205
|
-
workspacesApiUrl: (path, params) => {
|
206
|
-
var _a, _b;
|
207
|
-
const baseUrl = (_a = __classPrivateFieldGet(this, _RestRepository_client, "f").options.databaseURL) !== null && _a !== void 0 ? _a : '';
|
208
|
-
const hasBranch = (_b = params.dbBranchName) !== null && _b !== void 0 ? _b : params.branch;
|
209
|
-
const newPath = path.replace(/^\/db\/[^/]+/, hasBranch ? `:${branch}` : '');
|
210
|
-
return baseUrl + newPath;
|
211
|
-
}
|
212
|
-
};
|
213
|
-
});
|
214
|
-
}, _RestRepository_getBranch = function _RestRepository_getBranch() {
|
215
|
-
var e_1, _a;
|
216
|
-
return __awaiter(this, void 0, void 0, function* () {
|
217
|
-
if (__classPrivateFieldGet(this, _RestRepository_branch, "f"))
|
218
|
-
return __classPrivateFieldGet(this, _RestRepository_branch, "f");
|
219
|
-
const { branch: param } = __classPrivateFieldGet(this, _RestRepository_client, "f").options;
|
220
|
-
const strategies = Array.isArray(param) ? [...param] : [param];
|
221
|
-
const evaluateBranch = (strategy) => __awaiter(this, void 0, void 0, function* () {
|
222
|
-
return isBranchStrategyBuilder(strategy) ? yield strategy() : strategy;
|
223
|
-
});
|
224
|
-
try {
|
225
|
-
for (var strategies_1 = __asyncValues(strategies), strategies_1_1; strategies_1_1 = yield strategies_1.next(), !strategies_1_1.done;) {
|
226
|
-
const strategy = strategies_1_1.value;
|
227
|
-
const branch = yield evaluateBranch(strategy);
|
228
|
-
if (branch) {
|
229
|
-
__classPrivateFieldSet(this, _RestRepository_branch, branch, "f");
|
230
|
-
return branch;
|
231
|
-
}
|
232
|
-
}
|
233
|
-
}
|
234
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
235
|
-
finally {
|
236
|
-
try {
|
237
|
-
if (strategies_1_1 && !strategies_1_1.done && (_a = strategies_1.return)) yield _a.call(strategies_1);
|
238
|
-
}
|
239
|
-
finally { if (e_1) throw e_1.error; }
|
240
|
-
}
|
241
|
-
throw new Error('Unable to resolve branch value');
|
242
|
-
});
|
243
|
-
}, _RestRepository_insertRecordWithoutId = function _RestRepository_insertRecordWithoutId(object) {
|
182
|
+
_RestRepository_table = new WeakMap(), _RestRepository_links = new WeakMap(), _RestRepository_getFetchProps = new WeakMap(), _RestRepository_instances = new WeakSet(), _RestRepository_insertRecordWithoutId = function _RestRepository_insertRecordWithoutId(object) {
|
244
183
|
return __awaiter(this, void 0, void 0, function* () {
|
245
|
-
const fetchProps = yield __classPrivateFieldGet(this,
|
184
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
|
246
185
|
const record = transformObjectLinks(object);
|
247
186
|
const response = yield (0, api_1.insertRecord)(Object.assign({ pathParams: {
|
248
187
|
workspace: '{workspaceId}',
|
@@ -257,7 +196,7 @@ _RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _
|
|
257
196
|
});
|
258
197
|
}, _RestRepository_insertRecordWithId = function _RestRepository_insertRecordWithId(recordId, object) {
|
259
198
|
return __awaiter(this, void 0, void 0, function* () {
|
260
|
-
const fetchProps = yield __classPrivateFieldGet(this,
|
199
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
|
261
200
|
const record = transformObjectLinks(object);
|
262
201
|
const response = yield (0, api_1.insertRecordWithID)(Object.assign({ pathParams: {
|
263
202
|
workspace: '{workspaceId}',
|
@@ -273,7 +212,7 @@ _RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _
|
|
273
212
|
});
|
274
213
|
}, _RestRepository_bulkInsertTableRecords = function _RestRepository_bulkInsertTableRecords(objects) {
|
275
214
|
return __awaiter(this, void 0, void 0, function* () {
|
276
|
-
const fetchProps = yield __classPrivateFieldGet(this,
|
215
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
|
277
216
|
const records = objects.map((object) => transformObjectLinks(object));
|
278
217
|
const response = yield (0, api_1.bulkInsertTableRecords)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f") }, body: { records } }, fetchProps));
|
279
218
|
const finalObjects = yield this.any(...response.recordIDs.map((id) => this.filter('id', id))).getAll();
|
@@ -284,7 +223,7 @@ _RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _
|
|
284
223
|
});
|
285
224
|
}, _RestRepository_updateRecordWithID = function _RestRepository_updateRecordWithID(recordId, object) {
|
286
225
|
return __awaiter(this, void 0, void 0, function* () {
|
287
|
-
const fetchProps = yield __classPrivateFieldGet(this,
|
226
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
|
288
227
|
const record = transformObjectLinks(object);
|
289
228
|
const response = yield (0, api_1.updateRecordWithID)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId }, body: record }, fetchProps));
|
290
229
|
const item = yield this.read(response.id);
|
@@ -294,7 +233,7 @@ _RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _
|
|
294
233
|
});
|
295
234
|
}, _RestRepository_upsertRecordWithID = function _RestRepository_upsertRecordWithID(recordId, object) {
|
296
235
|
return __awaiter(this, void 0, void 0, function* () {
|
297
|
-
const fetchProps = yield __classPrivateFieldGet(this,
|
236
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
|
298
237
|
const response = yield (0, api_1.upsertRecordWithID)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId }, body: object }, fetchProps));
|
299
238
|
const item = yield this.read(response.id);
|
300
239
|
if (!item)
|
@@ -303,7 +242,7 @@ _RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _
|
|
303
242
|
});
|
304
243
|
}, _RestRepository_deleteRecord = function _RestRepository_deleteRecord(recordId) {
|
305
244
|
return __awaiter(this, void 0, void 0, function* () {
|
306
|
-
const fetchProps = yield __classPrivateFieldGet(this,
|
245
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
|
307
246
|
yield (0, api_1.deleteRecord)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId } }, fetchProps));
|
308
247
|
});
|
309
248
|
}, _RestRepository_initObject = function _RestRepository_initObject(table, object) {
|
@@ -317,7 +256,7 @@ _RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _
|
|
317
256
|
result[field] = __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_initObject).call(this, linkTable, value);
|
318
257
|
}
|
319
258
|
}
|
320
|
-
const db =
|
259
|
+
const db = this.db;
|
321
260
|
result.read = function () {
|
322
261
|
return db[table].read(result['id']);
|
323
262
|
};
|
@@ -333,51 +272,6 @@ _RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _
|
|
333
272
|
Object.freeze(result);
|
334
273
|
return result;
|
335
274
|
};
|
336
|
-
class RestRespositoryFactory {
|
337
|
-
createRepository(client, table, links) {
|
338
|
-
return new RestRepository(client, table, links);
|
339
|
-
}
|
340
|
-
}
|
341
|
-
exports.RestRespositoryFactory = RestRespositoryFactory;
|
342
|
-
function resolveXataClientOptions(options) {
|
343
|
-
const databaseURL = (options === null || options === void 0 ? void 0 : options.databaseURL) || (0, config_1.getDatabaseURL)() || '';
|
344
|
-
const apiKey = (options === null || options === void 0 ? void 0 : options.apiKey) || (0, config_1.getAPIKey)() || '';
|
345
|
-
const branch = (options === null || options === void 0 ? void 0 : options.branch) || (() => (0, config_1.getCurrentBranchName)({ apiKey, databaseURL, fetchImpl: options === null || options === void 0 ? void 0 : options.fetch }));
|
346
|
-
if (!databaseURL || !apiKey) {
|
347
|
-
throw new Error('Options databaseURL and apiKey are required');
|
348
|
-
}
|
349
|
-
return Object.assign(Object.assign({}, options), { databaseURL,
|
350
|
-
apiKey,
|
351
|
-
branch });
|
352
|
-
}
|
353
|
-
class BaseClient {
|
354
|
-
constructor(options = {}, links) {
|
355
|
-
this.options = resolveXataClientOptions(options);
|
356
|
-
// Make this property not enumerable so it doesn't show up in console.dir, etc.
|
357
|
-
Object.defineProperty(this.options, 'apiKey', { enumerable: false });
|
358
|
-
const factory = this.options.repositoryFactory || new RestRespositoryFactory();
|
359
|
-
this.db = new Proxy({}, {
|
360
|
-
get: (_target, prop) => {
|
361
|
-
if (!(0, lang_1.isString)(prop))
|
362
|
-
throw new Error('Invalid table name');
|
363
|
-
return factory.createRepository(this, prop, links);
|
364
|
-
}
|
365
|
-
});
|
366
|
-
}
|
367
|
-
search(query, options) {
|
368
|
-
var _a;
|
369
|
-
return __awaiter(this, void 0, void 0, function* () {
|
370
|
-
const tables = (_a = options === null || options === void 0 ? void 0 : options.tables) !== null && _a !== void 0 ? _a : Object.keys(this.db);
|
371
|
-
// TODO: Implement global search with a single call, REST repository abstraction needed
|
372
|
-
const results = yield Promise.all(tables.map((table) => this.db[table].search(query, options).then((results) => [table, results])));
|
373
|
-
return Object.fromEntries(results);
|
374
|
-
});
|
375
|
-
}
|
376
|
-
}
|
377
|
-
exports.BaseClient = BaseClient;
|
378
|
-
const isBranchStrategyBuilder = (strategy) => {
|
379
|
-
return typeof strategy === 'function';
|
380
|
-
};
|
381
275
|
const transformObjectLinks = (object) => {
|
382
276
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
383
277
|
// Ignore internal properties
|
@@ -1,6 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-floating-promises */
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
4
|
+
const vitest_1 = require("vitest");
|
4
5
|
// SelectableColumn<O> //
|
5
6
|
// --------------------------------------------------------------------------- //
|
6
7
|
const validTeamColumns = [
|
@@ -198,6 +199,6 @@ function test8(user) {
|
|
198
199
|
user.partner = null;
|
199
200
|
user.team = null;
|
200
201
|
}
|
201
|
-
test('fake test', () => {
|
202
|
+
(0, vitest_1.test)('fake test', () => {
|
202
203
|
// This is a fake test to make sure that the type definitions in this file are working
|
203
204
|
});
|
package/dist/schema/sorting.d.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { SingleOrArray, Values } from '../util/types';
|
1
|
+
import { SingleOrArray, StringKeys, Values } from '../util/types';
|
2
2
|
import { XataRecord } from './record';
|
3
3
|
import { SelectableColumn } from './selection';
|
4
4
|
export declare type SortDirection = 'asc' | 'desc';
|
@@ -6,12 +6,17 @@ export declare type SortFilterExtended<T extends XataRecord> = {
|
|
6
6
|
column: SelectableColumn<T>;
|
7
7
|
direction?: SortDirection;
|
8
8
|
};
|
9
|
-
export declare type SortFilter<T extends XataRecord> = SelectableColumn<T> | SortFilterExtended<T>;
|
9
|
+
export declare type SortFilter<T extends XataRecord> = SelectableColumn<T> | SortFilterExtended<T> | SortFilterBase<T>;
|
10
|
+
declare type SortFilterBase<T extends XataRecord> = {
|
11
|
+
[Key in StringKeys<T>]: SortDirection;
|
12
|
+
};
|
10
13
|
export declare type ApiSortFilter<T extends XataRecord> = SingleOrArray<Values<{
|
11
|
-
[
|
12
|
-
[K in
|
14
|
+
[Key in SelectableColumn<T>]: {
|
15
|
+
[K in Key]: SortDirection;
|
13
16
|
};
|
14
17
|
}>>;
|
15
18
|
export declare function isSortFilterString<T extends XataRecord>(value: any): value is SelectableColumn<T>;
|
19
|
+
export declare function isSortFilterBase<T extends XataRecord>(filter: SortFilter<T>): filter is SortFilterBase<T>;
|
16
20
|
export declare function isSortFilterObject<T extends XataRecord>(filter: SortFilter<T>): filter is SortFilterExtended<T>;
|
17
21
|
export declare function buildSortFilter<T extends XataRecord>(filter: SingleOrArray<SortFilter<T>>): ApiSortFilter<T>;
|
22
|
+
export {};
|
package/dist/schema/sorting.js
CHANGED
@@ -1,13 +1,17 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.buildSortFilter = exports.isSortFilterObject = exports.isSortFilterString = void 0;
|
3
|
+
exports.buildSortFilter = exports.isSortFilterObject = exports.isSortFilterBase = exports.isSortFilterString = void 0;
|
4
4
|
const lang_1 = require("../util/lang");
|
5
5
|
function isSortFilterString(value) {
|
6
6
|
return (0, lang_1.isString)(value);
|
7
7
|
}
|
8
8
|
exports.isSortFilterString = isSortFilterString;
|
9
|
+
function isSortFilterBase(filter) {
|
10
|
+
return (0, lang_1.isObject)(filter) && Object.values(filter).every((value) => value === 'asc' || value === 'desc');
|
11
|
+
}
|
12
|
+
exports.isSortFilterBase = isSortFilterBase;
|
9
13
|
function isSortFilterObject(filter) {
|
10
|
-
return (0, lang_1.isObject)(filter) && filter.column !== undefined;
|
14
|
+
return (0, lang_1.isObject)(filter) && !isSortFilterBase(filter) && filter.column !== undefined;
|
11
15
|
}
|
12
16
|
exports.isSortFilterObject = isSortFilterObject;
|
13
17
|
function buildSortFilter(filter) {
|
@@ -18,6 +22,9 @@ function buildSortFilter(filter) {
|
|
18
22
|
else if (Array.isArray(filter)) {
|
19
23
|
return filter.map((item) => buildSortFilter(item));
|
20
24
|
}
|
25
|
+
else if (isSortFilterBase(filter)) {
|
26
|
+
return filter;
|
27
|
+
}
|
21
28
|
else if (isSortFilterObject(filter)) {
|
22
29
|
return { [filter.column]: (_a = filter.direction) !== null && _a !== void 0 ? _a : 'asc' };
|
23
30
|
}
|
@@ -1,9 +1,11 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
4
|
+
const vitest_1 = require("vitest");
|
3
5
|
// Simple sorting
|
4
6
|
const simpleSorting = { name: 'asc' };
|
5
7
|
// Array of simple sorting
|
6
8
|
const arrayOfSimpleSorting = [{ name: 'asc' }, { age: 'desc' }];
|
7
|
-
test('fake test', () => {
|
9
|
+
(0, vitest_1.test)('fake test', () => {
|
8
10
|
// This is a fake test to make sure that the type definitions in this file are working
|
9
11
|
});
|
@@ -0,0 +1,34 @@
|
|
1
|
+
import { XataPlugin, XataPluginOptions } from '../plugins';
|
2
|
+
import { BaseData, XataRecord } from '../schema/record';
|
3
|
+
import { SelectedPick } from '../schema/selection';
|
4
|
+
import { GetArrayInnerType, Values } from '../util/types';
|
5
|
+
export declare class SearchPlugin<Schemas extends Record<string, BaseData>> extends XataPlugin {
|
6
|
+
#private;
|
7
|
+
build({ getFetchProps }: XataPluginOptions): {
|
8
|
+
all: <Tables extends Extract<keyof Schemas, string>>(query: string, options?: {
|
9
|
+
fuzziness?: number | undefined;
|
10
|
+
tables?: Tables[] | undefined;
|
11
|
+
}) => Promise<Values<{ [Model in Tables]: {
|
12
|
+
table: Model;
|
13
|
+
record: Awaited<SelectedPick<Schemas[Model] & XataRecord & {
|
14
|
+
xata: {
|
15
|
+
table: string;
|
16
|
+
};
|
17
|
+
}, ["*"]>>;
|
18
|
+
}; }>[]>;
|
19
|
+
byTable: <Tables_1 extends Extract<keyof Schemas, string>>(query: string, options?: {
|
20
|
+
fuzziness?: number | undefined;
|
21
|
+
tables?: Tables_1[] | undefined;
|
22
|
+
}) => Promise<{ [Model_1 in Tables_1]: SelectedPick<Schemas[Model_1] & XataRecord & {
|
23
|
+
xata: {
|
24
|
+
table: string;
|
25
|
+
};
|
26
|
+
}, ["*"]>[]; }>;
|
27
|
+
};
|
28
|
+
}
|
29
|
+
declare type SearchXataRecord = XataRecord & {
|
30
|
+
xata: {
|
31
|
+
table: string;
|
32
|
+
};
|
33
|
+
};
|
34
|
+
export {};
|
@@ -0,0 +1,55 @@
|
|
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 __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
12
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
13
|
+
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");
|
14
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
15
|
+
};
|
16
|
+
var _SearchPlugin_instances, _SearchPlugin_search;
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
18
|
+
exports.SearchPlugin = void 0;
|
19
|
+
const api_1 = require("../api");
|
20
|
+
const plugins_1 = require("../plugins");
|
21
|
+
class SearchPlugin extends plugins_1.XataPlugin {
|
22
|
+
constructor() {
|
23
|
+
super(...arguments);
|
24
|
+
_SearchPlugin_instances.add(this);
|
25
|
+
}
|
26
|
+
build({ getFetchProps }) {
|
27
|
+
return {
|
28
|
+
all: (query, options = {}) => __awaiter(this, void 0, void 0, function* () {
|
29
|
+
const records = yield __classPrivateFieldGet(this, _SearchPlugin_instances, "m", _SearchPlugin_search).call(this, query, options, getFetchProps);
|
30
|
+
return records.map((record) => {
|
31
|
+
const { table = 'orphan' } = record.xata;
|
32
|
+
return { table, record };
|
33
|
+
});
|
34
|
+
}),
|
35
|
+
byTable: (query, options = {}) => __awaiter(this, void 0, void 0, function* () {
|
36
|
+
const records = yield __classPrivateFieldGet(this, _SearchPlugin_instances, "m", _SearchPlugin_search).call(this, query, options, getFetchProps);
|
37
|
+
return records.reduce((acc, record) => {
|
38
|
+
var _a;
|
39
|
+
const { table = 'orphan' } = record.xata;
|
40
|
+
const items = (_a = acc[table]) !== null && _a !== void 0 ? _a : [];
|
41
|
+
return Object.assign(Object.assign({}, acc), { [table]: [...items, record] });
|
42
|
+
}, {});
|
43
|
+
})
|
44
|
+
};
|
45
|
+
}
|
46
|
+
}
|
47
|
+
exports.SearchPlugin = SearchPlugin;
|
48
|
+
_SearchPlugin_instances = new WeakSet(), _SearchPlugin_search = function _SearchPlugin_search(query, options, getFetchProps) {
|
49
|
+
return __awaiter(this, void 0, void 0, function* () {
|
50
|
+
const fetchProps = yield getFetchProps();
|
51
|
+
const { tables, fuzziness } = options !== null && options !== void 0 ? options : {};
|
52
|
+
const { records } = yield (0, api_1.searchBranch)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}' }, body: { tables, query, fuzziness } }, fetchProps));
|
53
|
+
return records;
|
54
|
+
});
|
55
|
+
};
|
@@ -0,0 +1,5 @@
|
|
1
|
+
export declare type BranchStrategyValue = string | undefined | null;
|
2
|
+
export declare type BranchStrategyBuilder = () => BranchStrategyValue | Promise<BranchStrategyValue>;
|
3
|
+
export declare type BranchStrategy = BranchStrategyValue | BranchStrategyBuilder;
|
4
|
+
export declare type BranchStrategyOption = NonNullable<BranchStrategy | BranchStrategy[]>;
|
5
|
+
export declare const isBranchStrategyBuilder: (strategy: BranchStrategy) => strategy is BranchStrategyBuilder;
|
@@ -0,0 +1,7 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.isBranchStrategyBuilder = void 0;
|
4
|
+
const isBranchStrategyBuilder = (strategy) => {
|
5
|
+
return typeof strategy === 'function';
|
6
|
+
};
|
7
|
+
exports.isBranchStrategyBuilder = isBranchStrategyBuilder;
|
package/dist/util/types.d.ts
CHANGED
@@ -12,8 +12,8 @@ export declare type RequiredBy<T, K extends keyof T> = T & {
|
|
12
12
|
[P in K]-?: NonNullable<T[P]>;
|
13
13
|
};
|
14
14
|
export declare type GetArrayInnerType<T extends readonly any[]> = T[number];
|
15
|
-
export declare type
|
16
|
-
[
|
15
|
+
export declare type AllRequired<T> = {
|
16
|
+
[P in keyof T]-?: T[P];
|
17
17
|
};
|
18
18
|
export declare type KeysOfUnion<T> = T extends T ? keyof T : never;
|
19
19
|
declare type Impossible<K extends keyof any> = {
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@xata.io/client",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.8.2",
|
4
4
|
"description": "Xata.io SDK for TypeScript and JavaScript",
|
5
5
|
"main": "./dist/index.js",
|
6
6
|
"types": "./dist/index.d.ts",
|
@@ -23,5 +23,5 @@
|
|
23
23
|
"url": "https://github.com/xataio/client-ts/issues"
|
24
24
|
},
|
25
25
|
"homepage": "https://github.com/xataio/client-ts/blob/main/client/README.md",
|
26
|
-
"gitHead": "
|
26
|
+
"gitHead": "af614392d9eac48c72ae01598f11689f409778df"
|
27
27
|
}
|