@xata.io/client 0.0.0-beta.7fe10d6 → 0.0.0-beta.8f2aade
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/dist/api/client.d.ts +95 -0
- package/dist/api/client.js +235 -0
- package/dist/api/components.d.ts +1440 -0
- package/dist/api/components.js +1001 -0
- package/dist/api/fetcher.d.ts +25 -0
- package/dist/api/fetcher.js +78 -0
- package/dist/api/index.d.ts +7 -0
- package/dist/api/index.js +21 -0
- package/dist/api/parameters.d.ts +16 -0
- package/dist/api/parameters.js +2 -0
- package/dist/api/providers.d.ts +8 -0
- package/dist/api/providers.js +29 -0
- package/dist/api/responses.d.ts +44 -0
- package/dist/api/responses.js +2 -0
- package/dist/api/schemas.d.ts +311 -0
- package/dist/api/schemas.js +2 -0
- package/dist/index.d.ts +2 -182
- package/dist/index.js +16 -499
- package/dist/schema/filters.d.ts +20 -0
- package/dist/schema/filters.js +24 -0
- package/dist/schema/index.d.ts +5 -0
- package/dist/schema/index.js +25 -0
- package/dist/schema/operators.d.ts +72 -0
- package/dist/schema/operators.js +91 -0
- package/dist/schema/pagination.d.ts +79 -0
- package/dist/schema/pagination.js +90 -0
- package/dist/schema/query.d.ts +114 -0
- package/dist/schema/query.js +227 -0
- package/dist/schema/record.d.ts +36 -0
- package/dist/schema/record.js +2 -0
- package/dist/schema/repository.d.ts +109 -0
- package/dist/schema/repository.js +273 -0
- package/dist/schema/selection.d.ts +13 -0
- package/dist/schema/selection.js +2 -0
- package/dist/util/lang.d.ts +2 -0
- package/dist/util/lang.js +10 -0
- package/dist/util/types.d.ts +3 -0
- package/dist/util/types.js +2 -0
- package/package.json +2 -2
- package/tsconfig.json +21 -0
- package/dist/util/errors.d.ts +0 -3
- package/dist/util/errors.js +0 -9
@@ -0,0 +1,91 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.includesAll = exports.includesPattern = exports.includesSubstring = exports.includes = exports.contains = exports.isNot = exports.is = exports.pattern = exports.endsWith = exports.startsWith = exports.notExists = exports.exists = exports.le = exports.lte = exports.lt = exports.gte = exports.ge = exports.gt = void 0;
|
4
|
+
/**
|
5
|
+
* Operator to restrict results to only values that are greater than the given value.
|
6
|
+
*/
|
7
|
+
const gt = (value) => ({ $gt: value });
|
8
|
+
exports.gt = gt;
|
9
|
+
/**
|
10
|
+
* Operator to restrict results to only values that are greater than or equal to the given value.
|
11
|
+
*/
|
12
|
+
const ge = (value) => ({ $ge: value });
|
13
|
+
exports.ge = ge;
|
14
|
+
/**
|
15
|
+
* Operator to restrict results to only values that are greater than or equal to the given value.
|
16
|
+
*/
|
17
|
+
const gte = (value) => ({ $ge: value });
|
18
|
+
exports.gte = gte;
|
19
|
+
/**
|
20
|
+
* Operator to restrict results to only values that are lower than the given value.
|
21
|
+
*/
|
22
|
+
const lt = (value) => ({ $lt: value });
|
23
|
+
exports.lt = lt;
|
24
|
+
/**
|
25
|
+
* Operator to restrict results to only values that are lower than or equal to the given value.
|
26
|
+
*/
|
27
|
+
const lte = (value) => ({ $le: value });
|
28
|
+
exports.lte = lte;
|
29
|
+
/**
|
30
|
+
* Operator to restrict results to only values that are lower than or equal to the given value.
|
31
|
+
*/
|
32
|
+
const le = (value) => ({ $le: value });
|
33
|
+
exports.le = le;
|
34
|
+
/**
|
35
|
+
* Operator to restrict results to only values that are not null.
|
36
|
+
*/
|
37
|
+
const exists = (column) => ({ $exists: column });
|
38
|
+
exports.exists = exists;
|
39
|
+
/**
|
40
|
+
* Operator to restrict results to only values that are null.
|
41
|
+
*/
|
42
|
+
const notExists = (column) => ({ $notExists: column });
|
43
|
+
exports.notExists = notExists;
|
44
|
+
/**
|
45
|
+
* Operator to restrict results to only values that start with the given prefix.
|
46
|
+
*/
|
47
|
+
const startsWith = (value) => ({ $startsWith: value });
|
48
|
+
exports.startsWith = startsWith;
|
49
|
+
/**
|
50
|
+
* Operator to restrict results to only values that end with the given suffix.
|
51
|
+
*/
|
52
|
+
const endsWith = (value) => ({ $endsWith: value });
|
53
|
+
exports.endsWith = endsWith;
|
54
|
+
/**
|
55
|
+
* Operator to restrict results to only values that match the given pattern.
|
56
|
+
*/
|
57
|
+
const pattern = (value) => ({ $pattern: value });
|
58
|
+
exports.pattern = pattern;
|
59
|
+
/**
|
60
|
+
* Operator to restrict results to only values that are equal to the given value.
|
61
|
+
*/
|
62
|
+
const is = (value) => ({ $is: value });
|
63
|
+
exports.is = is;
|
64
|
+
/**
|
65
|
+
* Operator to restrict results to only values that are not equal to the given value.
|
66
|
+
*/
|
67
|
+
const isNot = (value) => ({ $isNot: value });
|
68
|
+
exports.isNot = isNot;
|
69
|
+
/**
|
70
|
+
* Operator to restrict results to only values that contain the given value.
|
71
|
+
*/
|
72
|
+
const contains = (value) => ({ $contains: value });
|
73
|
+
exports.contains = contains;
|
74
|
+
// TODO: these can only be applied to columns of type "multiple"
|
75
|
+
/**
|
76
|
+
* Operator to restrict results to only arrays that include the given value.
|
77
|
+
*/
|
78
|
+
const includes = (value) => ({ $includes: value });
|
79
|
+
exports.includes = includes;
|
80
|
+
/**
|
81
|
+
* Operator to restrict results to only arrays that include a value matching the given substring.
|
82
|
+
*/
|
83
|
+
const includesSubstring = (value) => ({ $includesSubstring: value });
|
84
|
+
exports.includesSubstring = includesSubstring;
|
85
|
+
/**
|
86
|
+
* Operator to restrict results to only arrays that include a value matching the given pattern.
|
87
|
+
*/
|
88
|
+
const includesPattern = (value) => ({ $includesPattern: value });
|
89
|
+
exports.includesPattern = includesPattern;
|
90
|
+
const includesAll = (value) => ({ $includesAll: value });
|
91
|
+
exports.includesAll = includesAll;
|
@@ -0,0 +1,79 @@
|
|
1
|
+
import { XataRecord } from '..';
|
2
|
+
import { Query } from './query';
|
3
|
+
export declare type PaginationQueryMeta = {
|
4
|
+
page: {
|
5
|
+
cursor: string;
|
6
|
+
more: boolean;
|
7
|
+
};
|
8
|
+
};
|
9
|
+
export interface Paginable<T extends XataRecord, R extends XataRecord = T> {
|
10
|
+
meta: PaginationQueryMeta;
|
11
|
+
records: R[];
|
12
|
+
nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
13
|
+
previousPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
14
|
+
firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
15
|
+
lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
16
|
+
hasNextPage(): boolean;
|
17
|
+
}
|
18
|
+
/**
|
19
|
+
* A Page contains a set of results from a query plus metadata about the retrieved
|
20
|
+
* set of values such as the cursor, required to retrieve additional records.
|
21
|
+
*/
|
22
|
+
export declare class Page<T extends XataRecord, R extends XataRecord> implements Paginable<T, R> {
|
23
|
+
#private;
|
24
|
+
/**
|
25
|
+
* Page metadata, required to retrieve additional records.
|
26
|
+
*/
|
27
|
+
readonly meta: PaginationQueryMeta;
|
28
|
+
/**
|
29
|
+
* The set of results for this page.
|
30
|
+
*/
|
31
|
+
readonly records: R[];
|
32
|
+
constructor(query: Query<T, R>, meta: PaginationQueryMeta, records?: R[]);
|
33
|
+
/**
|
34
|
+
* Retrieves the next page of results.
|
35
|
+
* @param size Maximum number of results to be retrieved.
|
36
|
+
* @param offset Number of results to skip when retrieving the results.
|
37
|
+
* @returns The next page or results.
|
38
|
+
*/
|
39
|
+
nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
40
|
+
/**
|
41
|
+
* Retrieves the previous page of results.
|
42
|
+
* @param size Maximum number of results to be retrieved.
|
43
|
+
* @param offset Number of results to skip when retrieving the results.
|
44
|
+
* @returns The previous page or results.
|
45
|
+
*/
|
46
|
+
previousPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
47
|
+
/**
|
48
|
+
* Retrieves the first page of results.
|
49
|
+
* @param size Maximum number of results to be retrieved.
|
50
|
+
* @param offset Number of results to skip when retrieving the results.
|
51
|
+
* @returns The first page or results.
|
52
|
+
*/
|
53
|
+
firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
54
|
+
/**
|
55
|
+
* Retrieves the last page of results.
|
56
|
+
* @param size Maximum number of results to be retrieved.
|
57
|
+
* @param offset Number of results to skip when retrieving the results.
|
58
|
+
* @returns The last page or results.
|
59
|
+
*/
|
60
|
+
lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
61
|
+
/**
|
62
|
+
* Shortcut method to check if there will be additional results if the next page of results is retrieved.
|
63
|
+
* @returns Whether or not there will be additional results in the next page of results.
|
64
|
+
*/
|
65
|
+
hasNextPage(): boolean;
|
66
|
+
}
|
67
|
+
export declare type CursorNavigationOptions = {
|
68
|
+
first?: string;
|
69
|
+
} | {
|
70
|
+
last?: string;
|
71
|
+
} | {
|
72
|
+
after?: string;
|
73
|
+
before?: string;
|
74
|
+
};
|
75
|
+
export declare type OffsetNavigationOptions = {
|
76
|
+
size?: number;
|
77
|
+
offset?: number;
|
78
|
+
};
|
79
|
+
export declare type PaginationOptions = CursorNavigationOptions & OffsetNavigationOptions;
|
@@ -0,0 +1,90 @@
|
|
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 _Page_query;
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
24
|
+
exports.Page = void 0;
|
25
|
+
/**
|
26
|
+
* A Page contains a set of results from a query plus metadata about the retrieved
|
27
|
+
* set of values such as the cursor, required to retrieve additional records.
|
28
|
+
*/
|
29
|
+
class Page {
|
30
|
+
constructor(query, meta, records = []) {
|
31
|
+
_Page_query.set(this, void 0);
|
32
|
+
__classPrivateFieldSet(this, _Page_query, query, "f");
|
33
|
+
this.meta = meta;
|
34
|
+
this.records = records;
|
35
|
+
}
|
36
|
+
/**
|
37
|
+
* Retrieves the next page of results.
|
38
|
+
* @param size Maximum number of results to be retrieved.
|
39
|
+
* @param offset Number of results to skip when retrieving the results.
|
40
|
+
* @returns The next page or results.
|
41
|
+
*/
|
42
|
+
nextPage(size, offset) {
|
43
|
+
return __awaiter(this, void 0, void 0, function* () {
|
44
|
+
return __classPrivateFieldGet(this, _Page_query, "f").getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
|
45
|
+
});
|
46
|
+
}
|
47
|
+
/**
|
48
|
+
* Retrieves the previous page of results.
|
49
|
+
* @param size Maximum number of results to be retrieved.
|
50
|
+
* @param offset Number of results to skip when retrieving the results.
|
51
|
+
* @returns The previous page or results.
|
52
|
+
*/
|
53
|
+
previousPage(size, offset) {
|
54
|
+
return __awaiter(this, void 0, void 0, function* () {
|
55
|
+
return __classPrivateFieldGet(this, _Page_query, "f").getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
|
56
|
+
});
|
57
|
+
}
|
58
|
+
/**
|
59
|
+
* Retrieves the first page of results.
|
60
|
+
* @param size Maximum number of results to be retrieved.
|
61
|
+
* @param offset Number of results to skip when retrieving the results.
|
62
|
+
* @returns The first page or results.
|
63
|
+
*/
|
64
|
+
firstPage(size, offset) {
|
65
|
+
return __awaiter(this, void 0, void 0, function* () {
|
66
|
+
return __classPrivateFieldGet(this, _Page_query, "f").getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
|
67
|
+
});
|
68
|
+
}
|
69
|
+
/**
|
70
|
+
* Retrieves the last page of results.
|
71
|
+
* @param size Maximum number of results to be retrieved.
|
72
|
+
* @param offset Number of results to skip when retrieving the results.
|
73
|
+
* @returns The last page or results.
|
74
|
+
*/
|
75
|
+
lastPage(size, offset) {
|
76
|
+
return __awaiter(this, void 0, void 0, function* () {
|
77
|
+
return __classPrivateFieldGet(this, _Page_query, "f").getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
|
78
|
+
});
|
79
|
+
}
|
80
|
+
// TODO: We need to add something on the backend if we want a hasPreviousPage
|
81
|
+
/**
|
82
|
+
* Shortcut method to check if there will be additional results if the next page of results is retrieved.
|
83
|
+
* @returns Whether or not there will be additional results in the next page of results.
|
84
|
+
*/
|
85
|
+
hasNextPage() {
|
86
|
+
return this.meta.page.more;
|
87
|
+
}
|
88
|
+
}
|
89
|
+
exports.Page = Page;
|
90
|
+
_Page_query = new WeakMap();
|
@@ -0,0 +1,114 @@
|
|
1
|
+
import { XataRecord, Repository } from '..';
|
2
|
+
import { FilterExpression, SortExpression, PageConfig, ColumnsFilter } from '../api/schemas';
|
3
|
+
import { DeepConstraint, FilterConstraints, SortDirection, SortFilter } from './filters';
|
4
|
+
import { PaginationOptions, Page, Paginable, PaginationQueryMeta } from './pagination';
|
5
|
+
import { Selectable, SelectableColumn, Select } from './selection';
|
6
|
+
export declare type QueryOptions<T extends XataRecord> = {
|
7
|
+
page?: PaginationOptions;
|
8
|
+
columns?: Extract<keyof Selectable<T>, string>[];
|
9
|
+
sort?: SortFilter<T> | SortFilter<T>[];
|
10
|
+
};
|
11
|
+
export declare type QueryTableOptions = {
|
12
|
+
filter: FilterExpression;
|
13
|
+
sort?: SortExpression;
|
14
|
+
page?: PageConfig;
|
15
|
+
columns?: ColumnsFilter;
|
16
|
+
};
|
17
|
+
/**
|
18
|
+
* Query objects contain the information of all filters, sorting, etc. to be included in the database query.
|
19
|
+
*
|
20
|
+
* Query objects are immutable. Any method that adds more constraints or options to the query will return
|
21
|
+
* a new Query object containing the both the previous and the new constraints and options.
|
22
|
+
*/
|
23
|
+
export declare class Query<T extends XataRecord, R extends XataRecord = T> implements Paginable<T, R> {
|
24
|
+
#private;
|
25
|
+
readonly meta: PaginationQueryMeta;
|
26
|
+
readonly records: R[];
|
27
|
+
constructor(repository: Repository<T> | null, table: string, data: Partial<QueryTableOptions>, parent?: Partial<QueryTableOptions>);
|
28
|
+
getQueryOptions(): QueryTableOptions;
|
29
|
+
/**
|
30
|
+
* Builds a new query object representing a logical OR between the given subqueries.
|
31
|
+
* @param queries An array of subqueries.
|
32
|
+
* @returns A new Query object.
|
33
|
+
*/
|
34
|
+
any(...queries: Query<T, R>[]): Query<T, R>;
|
35
|
+
/**
|
36
|
+
* Builds a new query object representing a logical AND between the given subqueries.
|
37
|
+
* @param queries An array of subqueries.
|
38
|
+
* @returns A new Query object.
|
39
|
+
*/
|
40
|
+
all(...queries: Query<T, R>[]): Query<T, R>;
|
41
|
+
/**
|
42
|
+
* Builds a new query object representing a logical OR negating each subquery. In pseudo-code: !q1 OR !q2
|
43
|
+
* @param queries An array of subqueries.
|
44
|
+
* @returns A new Query object.
|
45
|
+
*/
|
46
|
+
not(...queries: Query<T, R>[]): Query<T, R>;
|
47
|
+
/**
|
48
|
+
* Builds a new query object representing a logical AND negating each subquery. In pseudo-code: !q1 AND !q2
|
49
|
+
* @param queries An array of subqueries.
|
50
|
+
* @returns A new Query object.
|
51
|
+
*/
|
52
|
+
none(...queries: Query<T, R>[]): Query<T, R>;
|
53
|
+
/**
|
54
|
+
* Builds a new query object adding one or more constraints. Examples:
|
55
|
+
*
|
56
|
+
* ```
|
57
|
+
* query.filter("columnName", columnValue)
|
58
|
+
* query.filter({
|
59
|
+
* "columnName": columnValue
|
60
|
+
* })
|
61
|
+
* query.filter({
|
62
|
+
* "columnName": operator(columnValue) // Use gt, gte, lt, lte, startsWith,...
|
63
|
+
* })
|
64
|
+
* ```
|
65
|
+
*
|
66
|
+
* @param constraints
|
67
|
+
* @returns A new Query object.
|
68
|
+
*/
|
69
|
+
filter(constraints: FilterConstraints<T>): Query<T, R>;
|
70
|
+
filter<F extends keyof Selectable<T>>(column: F, value: FilterConstraints<T[F]> | DeepConstraint<T[F]>): Query<T, R>;
|
71
|
+
/**
|
72
|
+
* Builds a new query with a new sort option.
|
73
|
+
* @param column The column name.
|
74
|
+
* @param direction The direction. Either ascending or descending.
|
75
|
+
* @returns A new Query object.
|
76
|
+
*/
|
77
|
+
sort<F extends keyof T>(column: F, direction: SortDirection): Query<T, R>;
|
78
|
+
/**
|
79
|
+
* Builds a new query specifying the set of columns to be returned in the query response.
|
80
|
+
* @param columns Array of column names to be returned by the query.
|
81
|
+
* @returns A new Query object.
|
82
|
+
*/
|
83
|
+
select<K extends SelectableColumn<T>>(columns: K[]): Query<T, Select<T, K>>;
|
84
|
+
getPaginated<Options extends QueryOptions<T>>(options?: Options): Promise<Page<T, typeof options extends {
|
85
|
+
columns: SelectableColumn<T>[];
|
86
|
+
} ? Select<T, typeof options['columns'][number]> : R>>;
|
87
|
+
[Symbol.asyncIterator](): AsyncIterableIterator<R>;
|
88
|
+
getIterator(chunk: number, options?: Omit<QueryOptions<T>, 'page'>): AsyncGenerator<R[]>;
|
89
|
+
/**
|
90
|
+
* Performs the query in the database and returns a set of results.
|
91
|
+
* @param options Additional options to be used when performing the query.
|
92
|
+
* @returns An array of records from the database.
|
93
|
+
*/
|
94
|
+
getMany<Options extends QueryOptions<T>>(options?: Options): Promise<(typeof options extends {
|
95
|
+
columns: SelectableColumn<T>[];
|
96
|
+
} ? Select<T, typeof options['columns'][number]> : R)[]>;
|
97
|
+
/**
|
98
|
+
* Performs the query in the database and returns the first result.
|
99
|
+
* @param options Additional options to be used when performing the query.
|
100
|
+
* @returns The first record that matches the query, or null if no record matched the query.
|
101
|
+
*/
|
102
|
+
getOne<Options extends Omit<QueryOptions<T>, 'page'>>(options?: Options): Promise<(typeof options extends {
|
103
|
+
columns: SelectableColumn<T>[];
|
104
|
+
} ? Select<T, typeof options['columns'][number]> : R) | null>;
|
105
|
+
/**async deleteAll(): Promise<number> {
|
106
|
+
// TODO: Return number of affected rows
|
107
|
+
return 0;
|
108
|
+
}**/
|
109
|
+
nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
110
|
+
previousPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
111
|
+
firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
112
|
+
lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
113
|
+
hasNextPage(): boolean;
|
114
|
+
}
|
@@ -0,0 +1,227 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
12
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
13
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
14
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
15
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
16
|
+
};
|
17
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
18
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
19
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
20
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
21
|
+
};
|
22
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
23
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
24
|
+
var m = o[Symbol.asyncIterator], i;
|
25
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
26
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
27
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
28
|
+
};
|
29
|
+
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
|
30
|
+
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
|
31
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
32
|
+
var g = generator.apply(thisArg, _arguments || []), i, q = [];
|
33
|
+
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
|
34
|
+
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
|
35
|
+
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
|
36
|
+
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
|
37
|
+
function fulfill(value) { resume("next", value); }
|
38
|
+
function reject(value) { resume("throw", value); }
|
39
|
+
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
|
40
|
+
};
|
41
|
+
var _Query_table, _Query_repository, _Query_data;
|
42
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
43
|
+
exports.Query = void 0;
|
44
|
+
const lang_1 = require("../util/lang");
|
45
|
+
/**
|
46
|
+
* Query objects contain the information of all filters, sorting, etc. to be included in the database query.
|
47
|
+
*
|
48
|
+
* Query objects are immutable. Any method that adds more constraints or options to the query will return
|
49
|
+
* a new Query object containing the both the previous and the new constraints and options.
|
50
|
+
*/
|
51
|
+
class Query {
|
52
|
+
constructor(repository, table, data, parent) {
|
53
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
|
54
|
+
_Query_table.set(this, void 0);
|
55
|
+
_Query_repository.set(this, void 0);
|
56
|
+
_Query_data.set(this, { filter: {} });
|
57
|
+
// Implements pagination
|
58
|
+
this.meta = { page: { cursor: 'start', more: true } };
|
59
|
+
this.records = [];
|
60
|
+
__classPrivateFieldSet(this, _Query_table, table, "f");
|
61
|
+
if (repository) {
|
62
|
+
__classPrivateFieldSet(this, _Query_repository, repository, "f");
|
63
|
+
}
|
64
|
+
else {
|
65
|
+
__classPrivateFieldSet(this, _Query_repository, this, "f");
|
66
|
+
}
|
67
|
+
__classPrivateFieldGet(this, _Query_data, "f").filter.$any = (_b = (_a = data.filter) === null || _a === void 0 ? void 0 : _a.$any) !== null && _b !== void 0 ? _b : (_c = parent === null || parent === void 0 ? void 0 : parent.filter) === null || _c === void 0 ? void 0 : _c.$any;
|
68
|
+
__classPrivateFieldGet(this, _Query_data, "f").filter.$all = (_e = (_d = data.filter) === null || _d === void 0 ? void 0 : _d.$all) !== null && _e !== void 0 ? _e : (_f = parent === null || parent === void 0 ? void 0 : parent.filter) === null || _f === void 0 ? void 0 : _f.$all;
|
69
|
+
__classPrivateFieldGet(this, _Query_data, "f").filter.$not = (_h = (_g = data.filter) === null || _g === void 0 ? void 0 : _g.$not) !== null && _h !== void 0 ? _h : (_j = parent === null || parent === void 0 ? void 0 : parent.filter) === null || _j === void 0 ? void 0 : _j.$not;
|
70
|
+
__classPrivateFieldGet(this, _Query_data, "f").filter.$none = (_l = (_k = data.filter) === null || _k === void 0 ? void 0 : _k.$none) !== null && _l !== void 0 ? _l : (_m = parent === null || parent === void 0 ? void 0 : parent.filter) === null || _m === void 0 ? void 0 : _m.$none;
|
71
|
+
__classPrivateFieldGet(this, _Query_data, "f").sort = (_o = data.sort) !== null && _o !== void 0 ? _o : parent === null || parent === void 0 ? void 0 : parent.sort;
|
72
|
+
__classPrivateFieldGet(this, _Query_data, "f").columns = (_q = (_p = data.columns) !== null && _p !== void 0 ? _p : parent === null || parent === void 0 ? void 0 : parent.columns) !== null && _q !== void 0 ? _q : ['*'];
|
73
|
+
this.any = this.any.bind(this);
|
74
|
+
this.all = this.all.bind(this);
|
75
|
+
this.not = this.not.bind(this);
|
76
|
+
this.filter = this.filter.bind(this);
|
77
|
+
this.sort = this.sort.bind(this);
|
78
|
+
this.none = this.none.bind(this);
|
79
|
+
Object.defineProperty(this, 'table', { enumerable: false });
|
80
|
+
Object.defineProperty(this, 'repository', { enumerable: false });
|
81
|
+
}
|
82
|
+
getQueryOptions() {
|
83
|
+
return __classPrivateFieldGet(this, _Query_data, "f");
|
84
|
+
}
|
85
|
+
/**
|
86
|
+
* Builds a new query object representing a logical OR between the given subqueries.
|
87
|
+
* @param queries An array of subqueries.
|
88
|
+
* @returns A new Query object.
|
89
|
+
*/
|
90
|
+
any(...queries) {
|
91
|
+
const $any = queries.map((query) => query.getQueryOptions().filter);
|
92
|
+
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $any } }, __classPrivateFieldGet(this, _Query_data, "f"));
|
93
|
+
}
|
94
|
+
/**
|
95
|
+
* Builds a new query object representing a logical AND between the given subqueries.
|
96
|
+
* @param queries An array of subqueries.
|
97
|
+
* @returns A new Query object.
|
98
|
+
*/
|
99
|
+
all(...queries) {
|
100
|
+
const $all = queries.map((query) => query.getQueryOptions().filter);
|
101
|
+
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f"));
|
102
|
+
}
|
103
|
+
/**
|
104
|
+
* Builds a new query object representing a logical OR negating each subquery. In pseudo-code: !q1 OR !q2
|
105
|
+
* @param queries An array of subqueries.
|
106
|
+
* @returns A new Query object.
|
107
|
+
*/
|
108
|
+
not(...queries) {
|
109
|
+
const $not = queries.map((query) => query.getQueryOptions().filter);
|
110
|
+
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $not } }, __classPrivateFieldGet(this, _Query_data, "f"));
|
111
|
+
}
|
112
|
+
/**
|
113
|
+
* Builds a new query object representing a logical AND negating each subquery. In pseudo-code: !q1 AND !q2
|
114
|
+
* @param queries An array of subqueries.
|
115
|
+
* @returns A new Query object.
|
116
|
+
*/
|
117
|
+
none(...queries) {
|
118
|
+
const $none = queries.map((query) => query.getQueryOptions().filter);
|
119
|
+
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $none } }, __classPrivateFieldGet(this, _Query_data, "f"));
|
120
|
+
}
|
121
|
+
filter(a, b) {
|
122
|
+
if (arguments.length === 1) {
|
123
|
+
const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
|
124
|
+
const $all = (0, lang_1.compact)([__classPrivateFieldGet(this, _Query_data, "f").filter.$all].flat().concat(constraints));
|
125
|
+
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f"));
|
126
|
+
}
|
127
|
+
else {
|
128
|
+
const column = a;
|
129
|
+
const value = b;
|
130
|
+
const $all = (0, lang_1.compact)([__classPrivateFieldGet(this, _Query_data, "f").filter.$all].flat().concat({ [column]: value }));
|
131
|
+
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f"));
|
132
|
+
}
|
133
|
+
}
|
134
|
+
/**
|
135
|
+
* Builds a new query with a new sort option.
|
136
|
+
* @param column The column name.
|
137
|
+
* @param direction The direction. Either ascending or descending.
|
138
|
+
* @returns A new Query object.
|
139
|
+
*/
|
140
|
+
sort(column, direction) {
|
141
|
+
const sort = Object.assign(Object.assign({}, __classPrivateFieldGet(this, _Query_data, "f").sort), { [column]: direction });
|
142
|
+
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { sort }, __classPrivateFieldGet(this, _Query_data, "f"));
|
143
|
+
}
|
144
|
+
/**
|
145
|
+
* Builds a new query specifying the set of columns to be returned in the query response.
|
146
|
+
* @param columns Array of column names to be returned by the query.
|
147
|
+
* @returns A new Query object.
|
148
|
+
*/
|
149
|
+
select(columns) {
|
150
|
+
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { columns }, __classPrivateFieldGet(this, _Query_data, "f"));
|
151
|
+
}
|
152
|
+
getPaginated(options = {}) {
|
153
|
+
return __classPrivateFieldGet(this, _Query_repository, "f").query(this, options);
|
154
|
+
}
|
155
|
+
[(_Query_table = new WeakMap(), _Query_repository = new WeakMap(), _Query_data = new WeakMap(), Symbol.asyncIterator)]() {
|
156
|
+
return __asyncGenerator(this, arguments, function* _a() {
|
157
|
+
var e_1, _b;
|
158
|
+
try {
|
159
|
+
for (var _c = __asyncValues(this.getIterator(1)), _d; _d = yield __await(_c.next()), !_d.done;) {
|
160
|
+
const [record] = _d.value;
|
161
|
+
yield yield __await(record);
|
162
|
+
}
|
163
|
+
}
|
164
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
165
|
+
finally {
|
166
|
+
try {
|
167
|
+
if (_d && !_d.done && (_b = _c.return)) yield __await(_b.call(_c));
|
168
|
+
}
|
169
|
+
finally { if (e_1) throw e_1.error; }
|
170
|
+
}
|
171
|
+
});
|
172
|
+
}
|
173
|
+
getIterator(chunk, options = {}) {
|
174
|
+
return __asyncGenerator(this, arguments, function* getIterator_1() {
|
175
|
+
let offset = 0;
|
176
|
+
let end = false;
|
177
|
+
while (!end) {
|
178
|
+
const { records, meta } = yield __await(this.getPaginated(Object.assign(Object.assign({}, options), { page: { size: chunk, offset } })));
|
179
|
+
yield yield __await(records);
|
180
|
+
offset += chunk;
|
181
|
+
end = !meta.page.more;
|
182
|
+
}
|
183
|
+
});
|
184
|
+
}
|
185
|
+
/**
|
186
|
+
* Performs the query in the database and returns a set of results.
|
187
|
+
* @param options Additional options to be used when performing the query.
|
188
|
+
* @returns An array of records from the database.
|
189
|
+
*/
|
190
|
+
getMany(options = {}) {
|
191
|
+
return __awaiter(this, void 0, void 0, function* () {
|
192
|
+
const { records } = yield this.getPaginated(options);
|
193
|
+
return records;
|
194
|
+
});
|
195
|
+
}
|
196
|
+
/**
|
197
|
+
* Performs the query in the database and returns the first result.
|
198
|
+
* @param options Additional options to be used when performing the query.
|
199
|
+
* @returns The first record that matches the query, or null if no record matched the query.
|
200
|
+
*/
|
201
|
+
getOne(options = {}) {
|
202
|
+
return __awaiter(this, void 0, void 0, function* () {
|
203
|
+
const records = yield this.getMany(Object.assign(Object.assign({}, options), { page: { size: 1 } }));
|
204
|
+
return records[0] || null;
|
205
|
+
});
|
206
|
+
}
|
207
|
+
/**async deleteAll(): Promise<number> {
|
208
|
+
// TODO: Return number of affected rows
|
209
|
+
return 0;
|
210
|
+
}**/
|
211
|
+
nextPage(size, offset) {
|
212
|
+
return this.firstPage(size, offset);
|
213
|
+
}
|
214
|
+
previousPage(size, offset) {
|
215
|
+
return this.firstPage(size, offset);
|
216
|
+
}
|
217
|
+
firstPage(size, offset) {
|
218
|
+
return this.getPaginated({ page: { size, offset } });
|
219
|
+
}
|
220
|
+
lastPage(size, offset) {
|
221
|
+
return this.getPaginated({ page: { size, offset, before: 'end' } });
|
222
|
+
}
|
223
|
+
hasNextPage() {
|
224
|
+
return this.meta.page.more;
|
225
|
+
}
|
226
|
+
}
|
227
|
+
exports.Query = Query;
|
@@ -0,0 +1,36 @@
|
|
1
|
+
import { Selectable } from './selection';
|
2
|
+
/**
|
3
|
+
* Represents a persisted record from the database.
|
4
|
+
*/
|
5
|
+
export interface XataRecord {
|
6
|
+
/**
|
7
|
+
* Unique id of this record.
|
8
|
+
*/
|
9
|
+
id: string;
|
10
|
+
/**
|
11
|
+
* Metadata of this record.
|
12
|
+
*/
|
13
|
+
xata: {
|
14
|
+
/**
|
15
|
+
* Number that is increased every time the record is updated.
|
16
|
+
*/
|
17
|
+
version: number;
|
18
|
+
};
|
19
|
+
/**
|
20
|
+
* Retrieves a refreshed copy of the current record from the database.
|
21
|
+
*/
|
22
|
+
read(): Promise<this>;
|
23
|
+
/**
|
24
|
+
* Performs a partial update of the current record. On success a new object is
|
25
|
+
* returned and the current object is not mutated.
|
26
|
+
* @param data The columns and their values that have to be updated.
|
27
|
+
* @returns A new record containing the latest values for all the columns of the current record.
|
28
|
+
*/
|
29
|
+
update(data: Partial<Selectable<this>>): Promise<this>;
|
30
|
+
/**
|
31
|
+
* Performs a deletion of the current record in the database.
|
32
|
+
*
|
33
|
+
* @throws If the record was already deleted or if an error happened while performing the deletion.
|
34
|
+
*/
|
35
|
+
delete(): Promise<void>;
|
36
|
+
}
|