@xata.io/client 0.8.0 → 0.8.3
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 +21 -0
- package/dist/index.cjs +1762 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +2731 -6
- package/dist/index.mjs +1667 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +9 -5
- package/rollup.config.js +33 -0
- package/tsconfig.json +2 -2
- package/dist/api/client.d.ts +0 -95
- package/dist/api/client.js +0 -251
- package/dist/api/components.d.ts +0 -1437
- package/dist/api/components.js +0 -998
- package/dist/api/fetcher.d.ts +0 -40
- package/dist/api/fetcher.js +0 -79
- package/dist/api/index.d.ts +0 -7
- package/dist/api/index.js +0 -21
- package/dist/api/parameters.d.ts +0 -16
- package/dist/api/parameters.js +0 -2
- package/dist/api/providers.d.ts +0 -8
- package/dist/api/providers.js +0 -30
- package/dist/api/responses.d.ts +0 -50
- package/dist/api/responses.js +0 -2
- package/dist/api/schemas.d.ts +0 -311
- package/dist/api/schemas.js +0 -2
- package/dist/client.d.ts +0 -39
- package/dist/client.js +0 -124
- package/dist/index.js +0 -29
- package/dist/namespace.d.ts +0 -7
- package/dist/namespace.js +0 -6
- package/dist/schema/filters.d.ts +0 -96
- package/dist/schema/filters.js +0 -2
- package/dist/schema/filters.spec.d.ts +0 -1
- package/dist/schema/filters.spec.js +0 -177
- package/dist/schema/index.d.ts +0 -21
- package/dist/schema/index.js +0 -49
- package/dist/schema/operators.d.ts +0 -74
- package/dist/schema/operators.js +0 -93
- package/dist/schema/pagination.d.ts +0 -83
- package/dist/schema/pagination.js +0 -93
- package/dist/schema/query.d.ts +0 -118
- package/dist/schema/query.js +0 -242
- package/dist/schema/record.d.ts +0 -66
- package/dist/schema/record.js +0 -13
- package/dist/schema/repository.d.ts +0 -134
- package/dist/schema/repository.js +0 -284
- package/dist/schema/selection.d.ts +0 -25
- package/dist/schema/selection.js +0 -2
- package/dist/schema/selection.spec.d.ts +0 -1
- package/dist/schema/selection.spec.js +0 -204
- package/dist/schema/sorting.d.ts +0 -17
- package/dist/schema/sorting.js +0 -28
- package/dist/schema/sorting.spec.d.ts +0 -1
- package/dist/schema/sorting.spec.js +0 -11
- package/dist/search/index.d.ts +0 -20
- package/dist/search/index.js +0 -30
- package/dist/util/branches.d.ts +0 -5
- package/dist/util/branches.js +0 -7
- package/dist/util/config.d.ts +0 -11
- package/dist/util/config.js +0 -121
- package/dist/util/environment.d.ts +0 -5
- package/dist/util/environment.js +0 -68
- package/dist/util/fetch.d.ts +0 -2
- package/dist/util/fetch.js +0 -13
- package/dist/util/lang.d.ts +0 -5
- package/dist/util/lang.js +0 -22
- package/dist/util/types.d.ts +0 -25
- package/dist/util/types.js +0 -2
package/dist/schema/record.d.ts
DELETED
@@ -1,66 +0,0 @@
|
|
1
|
-
import { SelectedPick } 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<Readonly<SelectedPick<this, ['*']>> | null>;
|
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(partialUpdate: Partial<EditableData<Omit<this, keyof XataRecord>>>): Promise<Readonly<SelectedPick<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
|
-
}
|
45
|
-
export declare type Link<Record extends XataRecord> = Omit<XataRecord, 'read' | 'update'> & {
|
46
|
-
/**
|
47
|
-
* Retrieves a refreshed copy of the current record from the database.
|
48
|
-
*/
|
49
|
-
read(): Promise<Readonly<SelectedPick<Record, ['*']>> | null>;
|
50
|
-
/**
|
51
|
-
* Performs a partial update of the current record. On success a new object is
|
52
|
-
* returned and the current object is not mutated.
|
53
|
-
* @param data The columns and their values that have to be updated.
|
54
|
-
* @returns A new record containing the latest values for all the columns of the current record.
|
55
|
-
*/
|
56
|
-
update(partialUpdate: Partial<EditableData<Omit<Record, keyof XataRecord>>>): Promise<Readonly<SelectedPick<Record, ['*']>>>;
|
57
|
-
};
|
58
|
-
export declare function isIdentifiable(x: any): x is Identifiable & Record<string, unknown>;
|
59
|
-
export declare function isXataRecord(x: any): x is XataRecord & Record<string, unknown>;
|
60
|
-
export declare type EditableData<O extends BaseData> = {
|
61
|
-
[K in keyof O]: O[K] extends XataRecord ? {
|
62
|
-
id: string;
|
63
|
-
} : NonNullable<O[K]> extends XataRecord ? {
|
64
|
-
id: string;
|
65
|
-
} | null | undefined : O[K];
|
66
|
-
};
|
package/dist/schema/record.js
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.isXataRecord = exports.isIdentifiable = void 0;
|
4
|
-
const lang_1 = require("../util/lang");
|
5
|
-
function isIdentifiable(x) {
|
6
|
-
return (0, lang_1.isObject)(x) && (0, lang_1.isString)(x === null || x === void 0 ? void 0 : x.id);
|
7
|
-
}
|
8
|
-
exports.isIdentifiable = isIdentifiable;
|
9
|
-
function isXataRecord(x) {
|
10
|
-
var _a;
|
11
|
-
return (isIdentifiable(x) && typeof (x === null || x === void 0 ? void 0 : x.xata) === 'object' && typeof ((_a = x === null || x === void 0 ? void 0 : x.xata) === null || _a === void 0 ? void 0 : _a.version) === 'number');
|
12
|
-
}
|
13
|
-
exports.isXataRecord = isXataRecord;
|
@@ -1,134 +0,0 @@
|
|
1
|
-
import { SchemaNamespaceResult } from '.';
|
2
|
-
import { FetcherExtraProps } from '../api/fetcher';
|
3
|
-
import { Dictionary } from '../util/types';
|
4
|
-
import { Page } from './pagination';
|
5
|
-
import { Query } from './query';
|
6
|
-
import { BaseData, EditableData, Identifiable, XataRecord } from './record';
|
7
|
-
import { SelectedPick } from './selection';
|
8
|
-
declare type TableLink = string[];
|
9
|
-
export declare type LinkDictionary = Dictionary<TableLink[]>;
|
10
|
-
/**
|
11
|
-
* Common interface for performing operations on a table.
|
12
|
-
*/
|
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, ['*']>>>;
|
15
|
-
/**
|
16
|
-
* Creates a single record in the table with a unique id.
|
17
|
-
* @param id The unique id.
|
18
|
-
* @param object Object containing the column names with their values to be stored in the table.
|
19
|
-
* @returns The full persisted record.
|
20
|
-
*/
|
21
|
-
abstract create(id: string, object: EditableData<Data>): Promise<Readonly<SelectedPick<Record, ['*']>>>;
|
22
|
-
/**
|
23
|
-
* Creates multiple records in the table.
|
24
|
-
* @param objects Array of objects with the column names and the values to be stored in the table.
|
25
|
-
* @returns Array of the persisted records.
|
26
|
-
*/
|
27
|
-
abstract create(objects: Array<EditableData<Data> & Partial<Identifiable>>): Promise<Readonly<SelectedPick<Record, ['*']>>[]>;
|
28
|
-
/**
|
29
|
-
* Queries a single record from the table given its unique id.
|
30
|
-
* @param id The unique id.
|
31
|
-
* @returns The persisted record for the given id or null if the record could not be found.
|
32
|
-
*/
|
33
|
-
abstract read(id: string): Promise<Readonly<SelectedPick<Record, ['*']> | null>>;
|
34
|
-
/**
|
35
|
-
* Partially update a single record.
|
36
|
-
* @param object An object with its id and the columns to be updated.
|
37
|
-
* @returns The full persisted record.
|
38
|
-
*/
|
39
|
-
abstract update(object: Partial<EditableData<Data>> & Identifiable): Promise<Readonly<SelectedPick<Record, ['*']>>>;
|
40
|
-
/**
|
41
|
-
* Partially update a single record given its unique id.
|
42
|
-
* @param id The unique id.
|
43
|
-
* @param object The column names and their values that have to be updated.
|
44
|
-
* @returns The full persisted record.
|
45
|
-
*/
|
46
|
-
abstract update(id: string, object: Partial<EditableData<Data>>): Promise<Readonly<SelectedPick<Record, ['*']>>>;
|
47
|
-
/**
|
48
|
-
* Partially updates multiple records.
|
49
|
-
* @param objects An array of objects with their ids and columns to be updated.
|
50
|
-
* @returns Array of the persisted records.
|
51
|
-
*/
|
52
|
-
abstract update(objects: Array<Partial<EditableData<Data>> & Identifiable>): Promise<Readonly<SelectedPick<Record, ['*']>>[]>;
|
53
|
-
/**
|
54
|
-
* Creates or updates a single record. If a record exists with the given id,
|
55
|
-
* it will be update, otherwise a new record will be created.
|
56
|
-
* @param object Object containing the column names with their values to be persisted in the table.
|
57
|
-
* @returns The full persisted record.
|
58
|
-
*/
|
59
|
-
abstract createOrUpdate(object: EditableData<Data> & Identifiable): Promise<Readonly<SelectedPick<Record, ['*']>>>;
|
60
|
-
/**
|
61
|
-
* Creates or updates a single record. If a record exists with the given id,
|
62
|
-
* it will be update, otherwise a new record will be created.
|
63
|
-
* @param id A unique id.
|
64
|
-
* @param object The column names and the values to be persisted.
|
65
|
-
* @returns The full persisted record.
|
66
|
-
*/
|
67
|
-
abstract createOrUpdate(id: string, object: EditableData<Data>): Promise<Readonly<SelectedPick<Record, ['*']>>>;
|
68
|
-
/**
|
69
|
-
* Creates or updates a single record. If a record exists with the given id,
|
70
|
-
* it will be update, otherwise a new record will be created.
|
71
|
-
* @param objects Array of objects with the column names and the values to be stored in the table.
|
72
|
-
* @returns Array of the persisted records.
|
73
|
-
*/
|
74
|
-
abstract createOrUpdate(objects: Array<EditableData<Data> & Identifiable>): Promise<Readonly<SelectedPick<Record, ['*']>>[]>;
|
75
|
-
/**
|
76
|
-
* Deletes a record given its unique id.
|
77
|
-
* @param id The unique id.
|
78
|
-
* @throws If the record could not be found or there was an error while performing the deletion.
|
79
|
-
*/
|
80
|
-
abstract delete(id: string): Promise<void>;
|
81
|
-
/**
|
82
|
-
* Deletes a record given its unique id.
|
83
|
-
* @param id An object with a unique id.
|
84
|
-
* @throws If the record could not be found or there was an error while performing the deletion.
|
85
|
-
*/
|
86
|
-
abstract delete(id: Identifiable): Promise<void>;
|
87
|
-
/**
|
88
|
-
* Deletes a record given a list of unique ids.
|
89
|
-
* @param ids The array of unique ids.
|
90
|
-
* @throws If the record could not be found or there was an error while performing the deletion.
|
91
|
-
*/
|
92
|
-
abstract delete(ids: string[]): Promise<void>;
|
93
|
-
/**
|
94
|
-
* Deletes a record given a list of unique ids.
|
95
|
-
* @param ids An array of objects with unique ids.
|
96
|
-
* @throws If the record could not be found or there was an error while performing the deletion.
|
97
|
-
*/
|
98
|
-
abstract delete(ids: Identifiable[]): Promise<void>;
|
99
|
-
/**
|
100
|
-
* Search for records in the table.
|
101
|
-
* @param query The query to search for.
|
102
|
-
* @param options The options to search with (like: fuzziness)
|
103
|
-
* @returns The found records.
|
104
|
-
*/
|
105
|
-
abstract search(query: string, options?: {
|
106
|
-
fuzziness?: number;
|
107
|
-
}): Promise<SelectedPick<Record, ['*']>[]>;
|
108
|
-
abstract query<Result extends XataRecord>(query: Query<Record, Result>): Promise<Page<Record, Result>>;
|
109
|
-
}
|
110
|
-
export declare class RestRepository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record, SelectedPick<Record, ['*']>> {
|
111
|
-
#private;
|
112
|
-
constructor(options: {
|
113
|
-
table: string;
|
114
|
-
links?: LinkDictionary;
|
115
|
-
getFetchProps: () => Promise<FetcherExtraProps>;
|
116
|
-
schemaNamespace: SchemaNamespaceResult<any>;
|
117
|
-
});
|
118
|
-
create(object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
|
119
|
-
create(recordId: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
|
120
|
-
create(objects: EditableData<Data>[]): Promise<SelectedPick<Record, ['*']>[]>;
|
121
|
-
read(recordId: string): Promise<SelectedPick<Record, ['*']> | null>;
|
122
|
-
update(object: Partial<EditableData<Data>> & Identifiable): Promise<SelectedPick<Record, ['*']>>;
|
123
|
-
update(recordId: string, object: Partial<EditableData<Data>>): Promise<SelectedPick<Record, ['*']>>;
|
124
|
-
update(objects: Array<Partial<EditableData<Data>> & Identifiable>): Promise<SelectedPick<Record, ['*']>[]>;
|
125
|
-
createOrUpdate(object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
|
126
|
-
createOrUpdate(recordId: string, object: EditableData<Data>): Promise<SelectedPick<Record, ['*']>>;
|
127
|
-
createOrUpdate(objects: EditableData<Data>[]): Promise<SelectedPick<Record, ['*']>[]>;
|
128
|
-
delete(recordId: string | Identifiable | Array<string | Identifiable>): Promise<void>;
|
129
|
-
search(query: string, options?: {
|
130
|
-
fuzziness?: number;
|
131
|
-
}): Promise<SelectedPick<Record, ['*']>[]>;
|
132
|
-
query<Result extends XataRecord>(query: Query<Record, Result>): Promise<Page<Record, Result>>;
|
133
|
-
}
|
134
|
-
export {};
|
@@ -1,284 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
-
});
|
10
|
-
};
|
11
|
-
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 _RestRepository_instances, _RestRepository_table, _RestRepository_links, _RestRepository_getFetchProps, _RestRepository_schemaNamespace, _RestRepository_insertRecordWithoutId, _RestRepository_insertRecordWithId, _RestRepository_bulkInsertTableRecords, _RestRepository_updateRecordWithID, _RestRepository_upsertRecordWithID, _RestRepository_deleteRecord, _RestRepository_initObject;
|
23
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
24
|
-
exports.RestRepository = exports.Repository = void 0;
|
25
|
-
const api_1 = require("../api");
|
26
|
-
const lang_1 = require("../util/lang");
|
27
|
-
const pagination_1 = require("./pagination");
|
28
|
-
const query_1 = require("./query");
|
29
|
-
const record_1 = require("./record");
|
30
|
-
const sorting_1 = require("./sorting");
|
31
|
-
/**
|
32
|
-
* Common interface for performing operations on a table.
|
33
|
-
*/
|
34
|
-
class Repository extends query_1.Query {
|
35
|
-
}
|
36
|
-
exports.Repository = Repository;
|
37
|
-
class RestRepository extends query_1.Query {
|
38
|
-
constructor(options) {
|
39
|
-
var _a;
|
40
|
-
super(null, options.table, {});
|
41
|
-
_RestRepository_instances.add(this);
|
42
|
-
_RestRepository_table.set(this, void 0);
|
43
|
-
_RestRepository_links.set(this, void 0);
|
44
|
-
_RestRepository_getFetchProps.set(this, void 0);
|
45
|
-
_RestRepository_schemaNamespace.set(this, void 0);
|
46
|
-
__classPrivateFieldSet(this, _RestRepository_table, options.table, "f");
|
47
|
-
__classPrivateFieldSet(this, _RestRepository_links, (_a = options.links) !== null && _a !== void 0 ? _a : {}, "f");
|
48
|
-
__classPrivateFieldSet(this, _RestRepository_getFetchProps, options.getFetchProps, "f");
|
49
|
-
__classPrivateFieldSet(this, _RestRepository_schemaNamespace, options.schemaNamespace, "f");
|
50
|
-
}
|
51
|
-
create(a, b) {
|
52
|
-
return __awaiter(this, void 0, void 0, function* () {
|
53
|
-
// Create many records
|
54
|
-
if (Array.isArray(a)) {
|
55
|
-
return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_bulkInsertTableRecords).call(this, a);
|
56
|
-
}
|
57
|
-
// Create one record with id as param
|
58
|
-
if ((0, lang_1.isString)(a) && (0, lang_1.isObject)(b)) {
|
59
|
-
if (a === '')
|
60
|
-
throw new Error("The id can't be empty");
|
61
|
-
return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_insertRecordWithId).call(this, a, b);
|
62
|
-
}
|
63
|
-
// Create one record with id as property
|
64
|
-
if ((0, lang_1.isObject)(a) && (0, lang_1.isString)(a.id)) {
|
65
|
-
if (a.id === '')
|
66
|
-
throw new Error("The id can't be empty");
|
67
|
-
return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_insertRecordWithId).call(this, a.id, Object.assign(Object.assign({}, a), { id: undefined }));
|
68
|
-
}
|
69
|
-
// Create one record without id
|
70
|
-
if ((0, lang_1.isObject)(a)) {
|
71
|
-
return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_insertRecordWithoutId).call(this, a);
|
72
|
-
}
|
73
|
-
throw new Error('Invalid arguments for create method');
|
74
|
-
});
|
75
|
-
}
|
76
|
-
// TODO: Add column support: https://github.com/xataio/openapi/issues/139
|
77
|
-
read(recordId) {
|
78
|
-
return __awaiter(this, void 0, void 0, function* () {
|
79
|
-
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
|
80
|
-
try {
|
81
|
-
const response = yield (0, api_1.getRecord)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId } }, fetchProps));
|
82
|
-
return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_initObject).call(this, __classPrivateFieldGet(this, _RestRepository_table, "f"), response);
|
83
|
-
}
|
84
|
-
catch (e) {
|
85
|
-
if ((0, lang_1.isObject)(e) && e.status === 404) {
|
86
|
-
return null;
|
87
|
-
}
|
88
|
-
throw e;
|
89
|
-
}
|
90
|
-
});
|
91
|
-
}
|
92
|
-
update(a, b) {
|
93
|
-
return __awaiter(this, void 0, void 0, function* () {
|
94
|
-
// Update many records
|
95
|
-
if (Array.isArray(a)) {
|
96
|
-
if (a.length > 100) {
|
97
|
-
// TODO: Implement bulk update when API has support for it
|
98
|
-
console.warn('Bulk update operation is not optimized in the Xata API yet, this request might be slow');
|
99
|
-
}
|
100
|
-
return Promise.all(a.map((object) => this.update(object)));
|
101
|
-
}
|
102
|
-
// Update one record with id as param
|
103
|
-
if ((0, lang_1.isString)(a) && (0, lang_1.isObject)(b)) {
|
104
|
-
return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_updateRecordWithID).call(this, a, b);
|
105
|
-
}
|
106
|
-
// Update one record with id as property
|
107
|
-
if ((0, lang_1.isObject)(a) && (0, lang_1.isString)(a.id)) {
|
108
|
-
return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_updateRecordWithID).call(this, a.id, Object.assign(Object.assign({}, a), { id: undefined }));
|
109
|
-
}
|
110
|
-
throw new Error('Invalid arguments for update method');
|
111
|
-
});
|
112
|
-
}
|
113
|
-
createOrUpdate(a, b) {
|
114
|
-
return __awaiter(this, void 0, void 0, function* () {
|
115
|
-
// Create or update many records
|
116
|
-
if (Array.isArray(a)) {
|
117
|
-
if (a.length > 100) {
|
118
|
-
// TODO: Implement bulk update when API has support for it
|
119
|
-
console.warn('Bulk update operation is not optimized in the Xata API yet, this request might be slow');
|
120
|
-
}
|
121
|
-
return Promise.all(a.map((object) => this.createOrUpdate(object)));
|
122
|
-
}
|
123
|
-
// Create or update one record with id as param
|
124
|
-
if ((0, lang_1.isString)(a) && (0, lang_1.isObject)(b)) {
|
125
|
-
return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_upsertRecordWithID).call(this, a, b);
|
126
|
-
}
|
127
|
-
// Create or update one record with id as property
|
128
|
-
if ((0, lang_1.isObject)(a) && (0, lang_1.isString)(a.id)) {
|
129
|
-
return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_upsertRecordWithID).call(this, a.id, Object.assign(Object.assign({}, a), { id: undefined }));
|
130
|
-
}
|
131
|
-
throw new Error('Invalid arguments for createOrUpdate method');
|
132
|
-
});
|
133
|
-
}
|
134
|
-
delete(recordId) {
|
135
|
-
return __awaiter(this, void 0, void 0, function* () {
|
136
|
-
// Delete many records
|
137
|
-
if (Array.isArray(recordId)) {
|
138
|
-
if (recordId.length > 100) {
|
139
|
-
// TODO: Implement bulk delete when API has support for it
|
140
|
-
console.warn('Bulk delete operation is not optimized in the Xata API yet, this request might be slow');
|
141
|
-
}
|
142
|
-
yield Promise.all(recordId.map((id) => this.delete(id)));
|
143
|
-
return;
|
144
|
-
}
|
145
|
-
// Delete one record with id as param
|
146
|
-
if ((0, lang_1.isString)(recordId)) {
|
147
|
-
yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_deleteRecord).call(this, recordId);
|
148
|
-
return;
|
149
|
-
}
|
150
|
-
// Delete one record with id as property
|
151
|
-
if ((0, lang_1.isObject)(recordId) && (0, lang_1.isString)(recordId.id)) {
|
152
|
-
yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_deleteRecord).call(this, recordId.id);
|
153
|
-
return;
|
154
|
-
}
|
155
|
-
throw new Error('Invalid arguments for delete method');
|
156
|
-
});
|
157
|
-
}
|
158
|
-
search(query, options = {}) {
|
159
|
-
return __awaiter(this, void 0, void 0, function* () {
|
160
|
-
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
|
161
|
-
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));
|
162
|
-
return records.map((item) => __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_initObject).call(this, __classPrivateFieldGet(this, _RestRepository_table, "f"), item));
|
163
|
-
});
|
164
|
-
}
|
165
|
-
query(query) {
|
166
|
-
var _a;
|
167
|
-
return __awaiter(this, void 0, void 0, function* () {
|
168
|
-
const data = query.getQueryOptions();
|
169
|
-
const body = {
|
170
|
-
filter: Object.values((_a = data.filter) !== null && _a !== void 0 ? _a : {}).some(Boolean) ? data.filter : undefined,
|
171
|
-
sort: data.sort ? (0, sorting_1.buildSortFilter)(data.sort) : undefined,
|
172
|
-
page: data.page,
|
173
|
-
columns: data.columns
|
174
|
-
};
|
175
|
-
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
|
176
|
-
const { meta, records: objects } = yield (0, api_1.queryTable)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f") }, body }, fetchProps));
|
177
|
-
const records = objects.map((record) => __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_initObject).call(this, __classPrivateFieldGet(this, _RestRepository_table, "f"), record));
|
178
|
-
return new pagination_1.Page(query, meta, records);
|
179
|
-
});
|
180
|
-
}
|
181
|
-
}
|
182
|
-
exports.RestRepository = RestRepository;
|
183
|
-
_RestRepository_table = new WeakMap(), _RestRepository_links = new WeakMap(), _RestRepository_getFetchProps = new WeakMap(), _RestRepository_schemaNamespace = new WeakMap(), _RestRepository_instances = new WeakSet(), _RestRepository_insertRecordWithoutId = function _RestRepository_insertRecordWithoutId(object) {
|
184
|
-
return __awaiter(this, void 0, void 0, function* () {
|
185
|
-
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
|
186
|
-
const record = transformObjectLinks(object);
|
187
|
-
const response = yield (0, api_1.insertRecord)(Object.assign({ pathParams: {
|
188
|
-
workspace: '{workspaceId}',
|
189
|
-
dbBranchName: '{dbBranch}',
|
190
|
-
tableName: __classPrivateFieldGet(this, _RestRepository_table, "f")
|
191
|
-
}, body: record }, fetchProps));
|
192
|
-
const finalObject = yield this.read(response.id);
|
193
|
-
if (!finalObject) {
|
194
|
-
throw new Error('The server failed to save the record');
|
195
|
-
}
|
196
|
-
return finalObject;
|
197
|
-
});
|
198
|
-
}, _RestRepository_insertRecordWithId = function _RestRepository_insertRecordWithId(recordId, object) {
|
199
|
-
return __awaiter(this, void 0, void 0, function* () {
|
200
|
-
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
|
201
|
-
const record = transformObjectLinks(object);
|
202
|
-
const response = yield (0, api_1.insertRecordWithID)(Object.assign({ pathParams: {
|
203
|
-
workspace: '{workspaceId}',
|
204
|
-
dbBranchName: '{dbBranch}',
|
205
|
-
tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"),
|
206
|
-
recordId
|
207
|
-
}, body: record, queryParams: { createOnly: true } }, fetchProps));
|
208
|
-
const finalObject = yield this.read(response.id);
|
209
|
-
if (!finalObject) {
|
210
|
-
throw new Error('The server failed to save the record');
|
211
|
-
}
|
212
|
-
return finalObject;
|
213
|
-
});
|
214
|
-
}, _RestRepository_bulkInsertTableRecords = function _RestRepository_bulkInsertTableRecords(objects) {
|
215
|
-
return __awaiter(this, void 0, void 0, function* () {
|
216
|
-
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
|
217
|
-
const records = objects.map((object) => transformObjectLinks(object));
|
218
|
-
const response = yield (0, api_1.bulkInsertTableRecords)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f") }, body: { records } }, fetchProps));
|
219
|
-
const finalObjects = yield this.any(...response.recordIDs.map((id) => this.filter('id', id))).getAll();
|
220
|
-
if (finalObjects.length !== objects.length) {
|
221
|
-
throw new Error('The server failed to save some records');
|
222
|
-
}
|
223
|
-
return finalObjects;
|
224
|
-
});
|
225
|
-
}, _RestRepository_updateRecordWithID = function _RestRepository_updateRecordWithID(recordId, object) {
|
226
|
-
return __awaiter(this, void 0, void 0, function* () {
|
227
|
-
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
|
228
|
-
const record = transformObjectLinks(object);
|
229
|
-
const response = yield (0, api_1.updateRecordWithID)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId }, body: record }, fetchProps));
|
230
|
-
const item = yield this.read(response.id);
|
231
|
-
if (!item)
|
232
|
-
throw new Error('The server failed to save the record');
|
233
|
-
return item;
|
234
|
-
});
|
235
|
-
}, _RestRepository_upsertRecordWithID = function _RestRepository_upsertRecordWithID(recordId, object) {
|
236
|
-
return __awaiter(this, void 0, void 0, function* () {
|
237
|
-
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
|
238
|
-
const response = yield (0, api_1.upsertRecordWithID)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId }, body: object }, fetchProps));
|
239
|
-
const item = yield this.read(response.id);
|
240
|
-
if (!item)
|
241
|
-
throw new Error('The server failed to save the record');
|
242
|
-
return item;
|
243
|
-
});
|
244
|
-
}, _RestRepository_deleteRecord = function _RestRepository_deleteRecord(recordId) {
|
245
|
-
return __awaiter(this, void 0, void 0, function* () {
|
246
|
-
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_getFetchProps, "f").call(this);
|
247
|
-
yield (0, api_1.deleteRecord)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId } }, fetchProps));
|
248
|
-
});
|
249
|
-
}, _RestRepository_initObject = function _RestRepository_initObject(table, object) {
|
250
|
-
const result = {};
|
251
|
-
Object.assign(result, object);
|
252
|
-
const tableLinks = __classPrivateFieldGet(this, _RestRepository_links, "f")[table] || [];
|
253
|
-
for (const link of tableLinks) {
|
254
|
-
const [field, linkTable] = link;
|
255
|
-
const value = result[field];
|
256
|
-
if (value && (0, lang_1.isObject)(value)) {
|
257
|
-
result[field] = __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_initObject).call(this, linkTable, value);
|
258
|
-
}
|
259
|
-
}
|
260
|
-
const db = __classPrivateFieldGet(this, _RestRepository_schemaNamespace, "f");
|
261
|
-
result.read = function () {
|
262
|
-
return db[table].read(result['id']);
|
263
|
-
};
|
264
|
-
result.update = function (data) {
|
265
|
-
return db[table].update(result['id'], data);
|
266
|
-
};
|
267
|
-
result.delete = function () {
|
268
|
-
return db[table].delete(result['id']);
|
269
|
-
};
|
270
|
-
for (const prop of ['read', 'update', 'delete']) {
|
271
|
-
Object.defineProperty(result, prop, { enumerable: false });
|
272
|
-
}
|
273
|
-
Object.freeze(result);
|
274
|
-
return result;
|
275
|
-
};
|
276
|
-
const transformObjectLinks = (object) => {
|
277
|
-
return Object.entries(object).reduce((acc, [key, value]) => {
|
278
|
-
// Ignore internal properties
|
279
|
-
if (key === 'xata')
|
280
|
-
return acc;
|
281
|
-
// Transform links to identifier
|
282
|
-
return Object.assign(Object.assign({}, acc), { [key]: (0, record_1.isIdentifiable)(value) ? value.id : value });
|
283
|
-
}, {});
|
284
|
-
};
|
@@ -1,25 +0,0 @@
|
|
1
|
-
import { If, IsArray, IsObject, StringKeys, UnionToIntersection, Values } from '../util/types';
|
2
|
-
import { Link, XataRecord } from './record';
|
3
|
-
export declare type SelectableColumn<O, RecursivePath extends any[] = []> = '*' | 'id' | DataProps<O> | NestedColumns<O, RecursivePath>;
|
4
|
-
export declare type SelectedPick<O extends XataRecord, Key extends SelectableColumn<O>[]> = XataRecord & UnionToIntersection<Values<{
|
5
|
-
[K in Key[number]]: NestedValueAtColumn<O, K> & XataRecord;
|
6
|
-
}>>;
|
7
|
-
export declare type ValueAtColumn<O, P extends SelectableColumn<O>> = P extends '*' ? Values<O> : P extends 'id' ? string : P extends keyof O ? O[P] : P extends `${infer K}.${infer V}` ? K extends keyof O ? Values<O[K] extends XataRecord ? (V extends SelectableColumn<O[K]> ? {
|
8
|
-
V: ValueAtColumn<O[K], V>;
|
9
|
-
} : never) : O[K]> : never : never;
|
10
|
-
declare type MAX_RECURSION = 5;
|
11
|
-
declare type NestedColumns<O, RecursivePath extends any[]> = RecursivePath['length'] extends MAX_RECURSION ? never : If<IsObject<O>, Values<{
|
12
|
-
[K in DataProps<O>]: If<IsArray<NonNullable<O[K]>>, K, // If the property is an array, we stop recursion. We don't support object arrays yet
|
13
|
-
If<IsObject<NonNullable<O[K]>>, NonNullable<O[K]> extends XataRecord ? SelectableColumn<NonNullable<O[K]>, [...RecursivePath, O[K]]> extends string ? K | `${K}.${SelectableColumn<NonNullable<O[K]>, [...RecursivePath, O[K]]>}` : never : `${K}.${StringKeys<NonNullable<O[K]>> | '*'}`, // This allows usage of objects that are not links
|
14
|
-
K>>;
|
15
|
-
}>, never>;
|
16
|
-
declare type DataProps<O> = Exclude<StringKeys<O>, StringKeys<XataRecord>>;
|
17
|
-
declare type NestedValueAtColumn<O, Key extends SelectableColumn<O>> = Key extends `${infer N}.${infer M}` ? N extends DataProps<O> ? {
|
18
|
-
[K in N]: M extends SelectableColumn<NonNullable<O[K]>> ? NonNullable<O[K]> extends XataRecord ? ForwardNullable<O[K], NestedValueAtColumn<NonNullable<O[K]>, M> & XataRecord> : ForwardNullable<O[K], NestedValueAtColumn<NonNullable<O[K]>, M>> : unknown;
|
19
|
-
} : unknown : Key extends DataProps<O> ? {
|
20
|
-
[K in Key]: NonNullable<O[K]> extends XataRecord ? ForwardNullable<O[K], SelectedPick<NonNullable<O[K]>, ['*']>> : O[K];
|
21
|
-
} : Key extends '*' ? {
|
22
|
-
[K in StringKeys<O>]: NonNullable<O[K]> extends XataRecord ? ForwardNullable<O[K], Link<NonNullable<O[K]>>> : O[K];
|
23
|
-
} : unknown;
|
24
|
-
declare type ForwardNullable<T, R> = T extends NonNullable<T> ? R : R | null;
|
25
|
-
export {};
|
package/dist/schema/selection.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export {};
|