@xata.io/client 0.0.0-beta.cae436d → 0.0.0-beta.d29cf06
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/dist/api/client.d.ts +6 -1
- package/dist/api/client.js +16 -1
- package/dist/api/components.d.ts +36 -26
- package/dist/api/components.js +9 -9
- package/dist/api/index.js +5 -1
- package/dist/api/responses.d.ts +6 -0
- package/dist/index.d.ts +0 -57
- package/dist/index.js +6 -236
- package/dist/schema/index.d.ts +5 -0
- package/dist/schema/index.js +14 -1
- package/dist/schema/operators.d.ts +51 -0
- package/dist/schema/operators.js +51 -0
- package/dist/schema/pagination.d.ts +43 -1
- package/dist/schema/pagination.js +37 -1
- package/dist/schema/query.d.ts +93 -10
- package/dist/schema/query.js +83 -19
- package/dist/schema/record.d.ts +41 -0
- package/dist/schema/record.js +2 -0
- package/dist/schema/repository.d.ts +109 -0
- package/dist/schema/repository.js +280 -0
- package/dist/schema/selection.d.ts +3 -7
- package/dist/util/lang.d.ts +1 -0
- package/package.json +2 -2
- package/tsconfig.json +21 -0
package/dist/schema/index.js
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
"use strict";
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
3
|
if (k2 === undefined) k2 = k;
|
4
|
-
Object.
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
5
9
|
}) : (function(o, m, k, k2) {
|
6
10
|
if (k2 === undefined) k2 = k;
|
7
11
|
o[k2] = m[k];
|
@@ -10,4 +14,13 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
11
15
|
};
|
12
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
exports.RestRespositoryFactory = exports.RestRepository = exports.Repository = exports.BaseClient = exports.Query = void 0;
|
13
18
|
__exportStar(require("./operators"), exports);
|
19
|
+
__exportStar(require("./pagination"), exports);
|
20
|
+
var query_1 = require("./query");
|
21
|
+
Object.defineProperty(exports, "Query", { enumerable: true, get: function () { return query_1.Query; } });
|
22
|
+
var repository_1 = require("./repository");
|
23
|
+
Object.defineProperty(exports, "BaseClient", { enumerable: true, get: function () { return repository_1.BaseClient; } });
|
24
|
+
Object.defineProperty(exports, "Repository", { enumerable: true, get: function () { return repository_1.Repository; } });
|
25
|
+
Object.defineProperty(exports, "RestRepository", { enumerable: true, get: function () { return repository_1.RestRepository; } });
|
26
|
+
Object.defineProperty(exports, "RestRespositoryFactory", { enumerable: true, get: function () { return repository_1.RestRespositoryFactory; } });
|
@@ -1,21 +1,72 @@
|
|
1
1
|
import { Constraint } from './filters';
|
2
2
|
declare type ComparableType = number | Date;
|
3
|
+
/**
|
4
|
+
* Operator to restrict results to only values that are greater than the given value.
|
5
|
+
*/
|
3
6
|
export declare const gt: <T extends ComparableType>(value: T) => Constraint<T>;
|
7
|
+
/**
|
8
|
+
* Operator to restrict results to only values that are greater than or equal to the given value.
|
9
|
+
*/
|
4
10
|
export declare const ge: <T extends ComparableType>(value: T) => Constraint<T>;
|
11
|
+
/**
|
12
|
+
* Operator to restrict results to only values that are greater than or equal to the given value.
|
13
|
+
*/
|
5
14
|
export declare const gte: <T extends ComparableType>(value: T) => Constraint<T>;
|
15
|
+
/**
|
16
|
+
* Operator to restrict results to only values that are lower than the given value.
|
17
|
+
*/
|
6
18
|
export declare const lt: <T extends ComparableType>(value: T) => Constraint<T>;
|
19
|
+
/**
|
20
|
+
* Operator to restrict results to only values that are lower than or equal to the given value.
|
21
|
+
*/
|
7
22
|
export declare const lte: <T extends ComparableType>(value: T) => Constraint<T>;
|
23
|
+
/**
|
24
|
+
* Operator to restrict results to only values that are lower than or equal to the given value.
|
25
|
+
*/
|
8
26
|
export declare const le: <T extends ComparableType>(value: T) => Constraint<T>;
|
27
|
+
/**
|
28
|
+
* Operator to restrict results to only values that are not null.
|
29
|
+
*/
|
9
30
|
export declare const exists: (column: string) => Constraint<string>;
|
31
|
+
/**
|
32
|
+
* Operator to restrict results to only values that are null.
|
33
|
+
*/
|
10
34
|
export declare const notExists: (column: string) => Constraint<string>;
|
35
|
+
/**
|
36
|
+
* Operator to restrict results to only values that start with the given prefix.
|
37
|
+
*/
|
11
38
|
export declare const startsWith: (value: string) => Constraint<string>;
|
39
|
+
/**
|
40
|
+
* Operator to restrict results to only values that end with the given suffix.
|
41
|
+
*/
|
12
42
|
export declare const endsWith: (value: string) => Constraint<string>;
|
43
|
+
/**
|
44
|
+
* Operator to restrict results to only values that match the given pattern.
|
45
|
+
*/
|
13
46
|
export declare const pattern: (value: string) => Constraint<string>;
|
47
|
+
/**
|
48
|
+
* Operator to restrict results to only values that are equal to the given value.
|
49
|
+
*/
|
14
50
|
export declare const is: <T>(value: T) => Constraint<T>;
|
51
|
+
/**
|
52
|
+
* Operator to restrict results to only values that are not equal to the given value.
|
53
|
+
*/
|
15
54
|
export declare const isNot: <T>(value: T) => Constraint<T>;
|
55
|
+
/**
|
56
|
+
* Operator to restrict results to only values that contain the given value.
|
57
|
+
*/
|
16
58
|
export declare const contains: <T>(value: T) => Constraint<T>;
|
59
|
+
/**
|
60
|
+
* Operator to restrict results to only arrays that include the given value.
|
61
|
+
*/
|
17
62
|
export declare const includes: (value: string) => Constraint<string>;
|
63
|
+
/**
|
64
|
+
* Operator to restrict results to only arrays that include a value matching the given substring.
|
65
|
+
*/
|
18
66
|
export declare const includesSubstring: (value: string) => Constraint<string>;
|
67
|
+
/**
|
68
|
+
* Operator to restrict results to only arrays that include a value matching the given pattern.
|
69
|
+
*/
|
19
70
|
export declare const includesPattern: (value: string) => Constraint<string>;
|
20
71
|
export declare const includesAll: (value: string) => Constraint<string>;
|
21
72
|
export {};
|
package/dist/schema/operators.js
CHANGED
@@ -1,39 +1,90 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
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
|
+
*/
|
4
7
|
const gt = (value) => ({ $gt: value });
|
5
8
|
exports.gt = gt;
|
9
|
+
/**
|
10
|
+
* Operator to restrict results to only values that are greater than or equal to the given value.
|
11
|
+
*/
|
6
12
|
const ge = (value) => ({ $ge: value });
|
7
13
|
exports.ge = ge;
|
14
|
+
/**
|
15
|
+
* Operator to restrict results to only values that are greater than or equal to the given value.
|
16
|
+
*/
|
8
17
|
const gte = (value) => ({ $ge: value });
|
9
18
|
exports.gte = gte;
|
19
|
+
/**
|
20
|
+
* Operator to restrict results to only values that are lower than the given value.
|
21
|
+
*/
|
10
22
|
const lt = (value) => ({ $lt: value });
|
11
23
|
exports.lt = lt;
|
24
|
+
/**
|
25
|
+
* Operator to restrict results to only values that are lower than or equal to the given value.
|
26
|
+
*/
|
12
27
|
const lte = (value) => ({ $le: value });
|
13
28
|
exports.lte = lte;
|
29
|
+
/**
|
30
|
+
* Operator to restrict results to only values that are lower than or equal to the given value.
|
31
|
+
*/
|
14
32
|
const le = (value) => ({ $le: value });
|
15
33
|
exports.le = le;
|
34
|
+
/**
|
35
|
+
* Operator to restrict results to only values that are not null.
|
36
|
+
*/
|
16
37
|
const exists = (column) => ({ $exists: column });
|
17
38
|
exports.exists = exists;
|
39
|
+
/**
|
40
|
+
* Operator to restrict results to only values that are null.
|
41
|
+
*/
|
18
42
|
const notExists = (column) => ({ $notExists: column });
|
19
43
|
exports.notExists = notExists;
|
44
|
+
/**
|
45
|
+
* Operator to restrict results to only values that start with the given prefix.
|
46
|
+
*/
|
20
47
|
const startsWith = (value) => ({ $startsWith: value });
|
21
48
|
exports.startsWith = startsWith;
|
49
|
+
/**
|
50
|
+
* Operator to restrict results to only values that end with the given suffix.
|
51
|
+
*/
|
22
52
|
const endsWith = (value) => ({ $endsWith: value });
|
23
53
|
exports.endsWith = endsWith;
|
54
|
+
/**
|
55
|
+
* Operator to restrict results to only values that match the given pattern.
|
56
|
+
*/
|
24
57
|
const pattern = (value) => ({ $pattern: value });
|
25
58
|
exports.pattern = pattern;
|
59
|
+
/**
|
60
|
+
* Operator to restrict results to only values that are equal to the given value.
|
61
|
+
*/
|
26
62
|
const is = (value) => ({ $is: value });
|
27
63
|
exports.is = is;
|
64
|
+
/**
|
65
|
+
* Operator to restrict results to only values that are not equal to the given value.
|
66
|
+
*/
|
28
67
|
const isNot = (value) => ({ $isNot: value });
|
29
68
|
exports.isNot = isNot;
|
69
|
+
/**
|
70
|
+
* Operator to restrict results to only values that contain the given value.
|
71
|
+
*/
|
30
72
|
const contains = (value) => ({ $contains: value });
|
31
73
|
exports.contains = contains;
|
32
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
|
+
*/
|
33
78
|
const includes = (value) => ({ $includes: value });
|
34
79
|
exports.includes = includes;
|
80
|
+
/**
|
81
|
+
* Operator to restrict results to only arrays that include a value matching the given substring.
|
82
|
+
*/
|
35
83
|
const includesSubstring = (value) => ({ $includesSubstring: value });
|
36
84
|
exports.includesSubstring = includesSubstring;
|
85
|
+
/**
|
86
|
+
* Operator to restrict results to only arrays that include a value matching the given pattern.
|
87
|
+
*/
|
37
88
|
const includesPattern = (value) => ({ $includesPattern: value });
|
38
89
|
exports.includesPattern = includesPattern;
|
39
90
|
const includesAll = (value) => ({ $includesAll: value });
|
@@ -15,15 +15,53 @@ export interface Paginable<T extends XataRecord, R extends XataRecord = T> {
|
|
15
15
|
lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
16
16
|
hasNextPage(): boolean;
|
17
17
|
}
|
18
|
-
|
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 = T> implements Paginable<T, R> {
|
19
23
|
#private;
|
24
|
+
/**
|
25
|
+
* Page metadata, required to retrieve additional records.
|
26
|
+
*/
|
20
27
|
readonly meta: PaginationQueryMeta;
|
28
|
+
/**
|
29
|
+
* The set of results for this page.
|
30
|
+
*/
|
21
31
|
readonly records: R[];
|
22
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
|
+
*/
|
23
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
|
+
*/
|
24
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
|
+
*/
|
25
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
|
+
*/
|
26
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
|
+
*/
|
27
65
|
hasNextPage(): boolean;
|
28
66
|
}
|
29
67
|
export declare type CursorNavigationOptions = {
|
@@ -39,3 +77,7 @@ export declare type OffsetNavigationOptions = {
|
|
39
77
|
offset?: number;
|
40
78
|
};
|
41
79
|
export declare type PaginationOptions = CursorNavigationOptions & OffsetNavigationOptions;
|
80
|
+
export declare const PAGINATION_MAX_SIZE = 200;
|
81
|
+
export declare const PAGINATION_DEFAULT_SIZE = 200;
|
82
|
+
export declare const PAGINATION_MAX_OFFSET = 800;
|
83
|
+
export declare const PAGINATION_DEFAULT_OFFSET = 0;
|
@@ -21,7 +21,11 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
21
21
|
};
|
22
22
|
var _Page_query;
|
23
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
24
|
-
exports.Page = void 0;
|
24
|
+
exports.PAGINATION_DEFAULT_OFFSET = exports.PAGINATION_MAX_OFFSET = exports.PAGINATION_DEFAULT_SIZE = exports.PAGINATION_MAX_SIZE = 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
|
+
*/
|
25
29
|
class Page {
|
26
30
|
constructor(query, meta, records = []) {
|
27
31
|
_Page_query.set(this, void 0);
|
@@ -29,30 +33,62 @@ class Page {
|
|
29
33
|
this.meta = meta;
|
30
34
|
this.records = records;
|
31
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
|
+
*/
|
32
42
|
nextPage(size, offset) {
|
33
43
|
return __awaiter(this, void 0, void 0, function* () {
|
34
44
|
return __classPrivateFieldGet(this, _Page_query, "f").getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
|
35
45
|
});
|
36
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
|
+
*/
|
37
53
|
previousPage(size, offset) {
|
38
54
|
return __awaiter(this, void 0, void 0, function* () {
|
39
55
|
return __classPrivateFieldGet(this, _Page_query, "f").getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
|
40
56
|
});
|
41
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
|
+
*/
|
42
64
|
firstPage(size, offset) {
|
43
65
|
return __awaiter(this, void 0, void 0, function* () {
|
44
66
|
return __classPrivateFieldGet(this, _Page_query, "f").getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
|
45
67
|
});
|
46
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
|
+
*/
|
47
75
|
lastPage(size, offset) {
|
48
76
|
return __awaiter(this, void 0, void 0, function* () {
|
49
77
|
return __classPrivateFieldGet(this, _Page_query, "f").getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
|
50
78
|
});
|
51
79
|
}
|
52
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
|
+
*/
|
53
85
|
hasNextPage() {
|
54
86
|
return this.meta.page.more;
|
55
87
|
}
|
56
88
|
}
|
57
89
|
exports.Page = Page;
|
58
90
|
_Page_query = new WeakMap();
|
91
|
+
exports.PAGINATION_MAX_SIZE = 200;
|
92
|
+
exports.PAGINATION_DEFAULT_SIZE = 200;
|
93
|
+
exports.PAGINATION_MAX_OFFSET = 800;
|
94
|
+
exports.PAGINATION_DEFAULT_OFFSET = 0;
|
package/dist/schema/query.d.ts
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
import {
|
2
|
-
import {
|
1
|
+
import { Repository, XataRecord } from '..';
|
2
|
+
import { ColumnsFilter, FilterExpression, PageConfig, SortExpression } from '../api/schemas';
|
3
3
|
import { DeepConstraint, FilterConstraints, SortDirection, SortFilter } from './filters';
|
4
|
-
import {
|
5
|
-
import { Selectable, SelectableColumn
|
6
|
-
export declare type QueryOptions<T> = {
|
4
|
+
import { Page, Paginable, PaginationOptions, PaginationQueryMeta } from './pagination';
|
5
|
+
import { Select, Selectable, SelectableColumn } from './selection';
|
6
|
+
export declare type QueryOptions<T extends XataRecord> = {
|
7
7
|
page?: PaginationOptions;
|
8
8
|
columns?: Extract<keyof Selectable<T>, string>[];
|
9
9
|
sort?: SortFilter<T> | SortFilter<T>[];
|
@@ -14,25 +14,95 @@ export declare type QueryTableOptions = {
|
|
14
14
|
page?: PageConfig;
|
15
15
|
columns?: ColumnsFilter;
|
16
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
|
+
*/
|
17
23
|
export declare class Query<T extends XataRecord, R extends XataRecord = T> implements Paginable<T, R> {
|
18
24
|
#private;
|
19
25
|
readonly meta: PaginationQueryMeta;
|
20
26
|
readonly records: R[];
|
21
27
|
constructor(repository: Repository<T> | null, table: string, data: Partial<QueryTableOptions>, parent?: Partial<QueryTableOptions>);
|
22
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
|
+
*/
|
23
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
|
+
*/
|
24
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
|
+
*/
|
25
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
|
+
*/
|
26
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
|
+
*/
|
27
69
|
filter(constraints: FilterConstraints<T>): Query<T, R>;
|
28
|
-
filter<F extends keyof T
|
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
|
+
*/
|
29
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
|
+
*/
|
30
83
|
select<K extends SelectableColumn<T>>(columns: K[]): Query<T, Select<T, K>>;
|
31
|
-
getPaginated<Options extends QueryOptions<T>>(options?: Options): Promise<Page<T,
|
84
|
+
getPaginated<Options extends QueryOptions<T>>(options?: Options): Promise<Page<T, GetWithColumnOptions<T, R, typeof options>>>;
|
32
85
|
[Symbol.asyncIterator](): AsyncIterableIterator<R>;
|
33
|
-
getIterator(chunk: number, options?: Omit<
|
34
|
-
|
35
|
-
|
86
|
+
getIterator<Options extends QueryOptions<T>>(chunk: number, options?: Omit<Options, 'page'>): AsyncGenerator<GetWithColumnOptions<T, R, typeof options>[]>;
|
87
|
+
/**
|
88
|
+
* Performs the query in the database and returns a set of results.
|
89
|
+
* @param options Additional options to be used when performing the query.
|
90
|
+
* @returns An array of records from the database.
|
91
|
+
*/
|
92
|
+
getMany<Options extends QueryOptions<T>>(options?: Options): Promise<GetWithColumnOptions<T, R, typeof options>[]>;
|
93
|
+
/**
|
94
|
+
* Performs the query in the database and returns all the results.
|
95
|
+
* Warning: If there are a large number of results, this method can have performance implications.
|
96
|
+
* @param options Additional options to be used when performing the query.
|
97
|
+
* @returns An array of records from the database.
|
98
|
+
*/
|
99
|
+
getAll<Options extends QueryOptions<T>>(chunk?: number, options?: Omit<Options, 'page'>): Promise<GetWithColumnOptions<T, R, typeof options>[]>;
|
100
|
+
/**
|
101
|
+
* Performs the query in the database and returns the first result.
|
102
|
+
* @param options Additional options to be used when performing the query.
|
103
|
+
* @returns The first record that matches the query, or null if no record matched the query.
|
104
|
+
*/
|
105
|
+
getOne<Options extends Omit<QueryOptions<T>, 'page'>>(options?: Options): Promise<GetWithColumnOptions<T, R, typeof options> | null>;
|
36
106
|
/**async deleteAll(): Promise<number> {
|
37
107
|
// TODO: Return number of affected rows
|
38
108
|
return 0;
|
@@ -43,3 +113,16 @@ export declare class Query<T extends XataRecord, R extends XataRecord = T> imple
|
|
43
113
|
lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
|
44
114
|
hasNextPage(): boolean;
|
45
115
|
}
|
116
|
+
/**
|
117
|
+
* Helper type to read options and compute the correct type for the result values
|
118
|
+
* T: Original type
|
119
|
+
* R: Default destination type
|
120
|
+
* Options: QueryOptions
|
121
|
+
*
|
122
|
+
* If the columns are overriden in the options, the result type is the pick of the original type and the columns
|
123
|
+
* If the columns are not overriden, the result type is the default destination type
|
124
|
+
*/
|
125
|
+
declare type GetWithColumnOptions<T, R, Options> = Options extends {
|
126
|
+
columns: SelectableColumn<T>[];
|
127
|
+
} ? Select<T, Options['columns'][number]> : R;
|
128
|
+
export {};
|
package/dist/schema/query.js
CHANGED
@@ -42,6 +42,13 @@ var _Query_table, _Query_repository, _Query_data;
|
|
42
42
|
Object.defineProperty(exports, "__esModule", { value: true });
|
43
43
|
exports.Query = void 0;
|
44
44
|
const lang_1 = require("../util/lang");
|
45
|
+
const pagination_1 = require("./pagination");
|
46
|
+
/**
|
47
|
+
* Query objects contain the information of all filters, sorting, etc. to be included in the database query.
|
48
|
+
*
|
49
|
+
* Query objects are immutable. Any method that adds more constraints or options to the query will return
|
50
|
+
* a new Query object containing the both the previous and the new constraints and options.
|
51
|
+
*/
|
45
52
|
class Query {
|
46
53
|
constructor(repository, table, data, parent) {
|
47
54
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
|
@@ -76,20 +83,40 @@ class Query {
|
|
76
83
|
getQueryOptions() {
|
77
84
|
return __classPrivateFieldGet(this, _Query_data, "f");
|
78
85
|
}
|
86
|
+
/**
|
87
|
+
* Builds a new query object representing a logical OR between the given subqueries.
|
88
|
+
* @param queries An array of subqueries.
|
89
|
+
* @returns A new Query object.
|
90
|
+
*/
|
79
91
|
any(...queries) {
|
80
|
-
const $any =
|
92
|
+
const $any = queries.map((query) => query.getQueryOptions().filter);
|
81
93
|
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $any } }, __classPrivateFieldGet(this, _Query_data, "f"));
|
82
94
|
}
|
95
|
+
/**
|
96
|
+
* Builds a new query object representing a logical AND between the given subqueries.
|
97
|
+
* @param queries An array of subqueries.
|
98
|
+
* @returns A new Query object.
|
99
|
+
*/
|
83
100
|
all(...queries) {
|
84
|
-
const $all =
|
101
|
+
const $all = queries.map((query) => query.getQueryOptions().filter);
|
85
102
|
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f"));
|
86
103
|
}
|
104
|
+
/**
|
105
|
+
* Builds a new query object representing a logical OR negating each subquery. In pseudo-code: !q1 OR !q2
|
106
|
+
* @param queries An array of subqueries.
|
107
|
+
* @returns A new Query object.
|
108
|
+
*/
|
87
109
|
not(...queries) {
|
88
|
-
const $not =
|
110
|
+
const $not = queries.map((query) => query.getQueryOptions().filter);
|
89
111
|
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $not } }, __classPrivateFieldGet(this, _Query_data, "f"));
|
90
112
|
}
|
113
|
+
/**
|
114
|
+
* Builds a new query object representing a logical AND negating each subquery. In pseudo-code: !q1 AND !q2
|
115
|
+
* @param queries An array of subqueries.
|
116
|
+
* @returns A new Query object.
|
117
|
+
*/
|
91
118
|
none(...queries) {
|
92
|
-
const $none =
|
119
|
+
const $none = queries.map((query) => query.getQueryOptions().filter);
|
93
120
|
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $none } }, __classPrivateFieldGet(this, _Query_data, "f"));
|
94
121
|
}
|
95
122
|
filter(a, b) {
|
@@ -105,17 +132,26 @@ class Query {
|
|
105
132
|
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { filter: { $all } }, __classPrivateFieldGet(this, _Query_data, "f"));
|
106
133
|
}
|
107
134
|
}
|
135
|
+
/**
|
136
|
+
* Builds a new query with a new sort option.
|
137
|
+
* @param column The column name.
|
138
|
+
* @param direction The direction. Either ascending or descending.
|
139
|
+
* @returns A new Query object.
|
140
|
+
*/
|
108
141
|
sort(column, direction) {
|
109
142
|
const sort = Object.assign(Object.assign({}, __classPrivateFieldGet(this, _Query_data, "f").sort), { [column]: direction });
|
110
143
|
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { sort }, __classPrivateFieldGet(this, _Query_data, "f"));
|
111
144
|
}
|
145
|
+
/**
|
146
|
+
* Builds a new query specifying the set of columns to be returned in the query response.
|
147
|
+
* @param columns Array of column names to be returned by the query.
|
148
|
+
* @returns A new Query object.
|
149
|
+
*/
|
112
150
|
select(columns) {
|
113
151
|
return new Query(__classPrivateFieldGet(this, _Query_repository, "f"), __classPrivateFieldGet(this, _Query_table, "f"), { columns }, __classPrivateFieldGet(this, _Query_data, "f"));
|
114
152
|
}
|
115
153
|
getPaginated(options = {}) {
|
116
|
-
return
|
117
|
-
return __classPrivateFieldGet(this, _Query_repository, "f").query(this, options);
|
118
|
-
});
|
154
|
+
return __classPrivateFieldGet(this, _Query_repository, "f").query(this, options);
|
119
155
|
}
|
120
156
|
[(_Query_table = new WeakMap(), _Query_repository = new WeakMap(), _Query_data = new WeakMap(), Symbol.asyncIterator)]() {
|
121
157
|
return __asyncGenerator(this, arguments, function* _a() {
|
@@ -147,12 +183,48 @@ class Query {
|
|
147
183
|
}
|
148
184
|
});
|
149
185
|
}
|
186
|
+
/**
|
187
|
+
* Performs the query in the database and returns a set of results.
|
188
|
+
* @param options Additional options to be used when performing the query.
|
189
|
+
* @returns An array of records from the database.
|
190
|
+
*/
|
150
191
|
getMany(options = {}) {
|
151
192
|
return __awaiter(this, void 0, void 0, function* () {
|
152
193
|
const { records } = yield this.getPaginated(options);
|
153
194
|
return records;
|
154
195
|
});
|
155
196
|
}
|
197
|
+
/**
|
198
|
+
* Performs the query in the database and returns all the results.
|
199
|
+
* Warning: If there are a large number of results, this method can have performance implications.
|
200
|
+
* @param options Additional options to be used when performing the query.
|
201
|
+
* @returns An array of records from the database.
|
202
|
+
*/
|
203
|
+
getAll(chunk = pagination_1.PAGINATION_MAX_SIZE, options = {}) {
|
204
|
+
var e_2, _a;
|
205
|
+
return __awaiter(this, void 0, void 0, function* () {
|
206
|
+
const results = [];
|
207
|
+
try {
|
208
|
+
for (var _b = __asyncValues(this.getIterator(chunk, options)), _c; _c = yield _b.next(), !_c.done;) {
|
209
|
+
const page = _c.value;
|
210
|
+
results.push(...page);
|
211
|
+
}
|
212
|
+
}
|
213
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
214
|
+
finally {
|
215
|
+
try {
|
216
|
+
if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
|
217
|
+
}
|
218
|
+
finally { if (e_2) throw e_2.error; }
|
219
|
+
}
|
220
|
+
return results;
|
221
|
+
});
|
222
|
+
}
|
223
|
+
/**
|
224
|
+
* Performs the query in the database and returns the first result.
|
225
|
+
* @param options Additional options to be used when performing the query.
|
226
|
+
* @returns The first record that matches the query, or null if no record matched the query.
|
227
|
+
*/
|
156
228
|
getOne(options = {}) {
|
157
229
|
return __awaiter(this, void 0, void 0, function* () {
|
158
230
|
const records = yield this.getMany(Object.assign(Object.assign({}, options), { page: { size: 1 } }));
|
@@ -164,24 +236,16 @@ class Query {
|
|
164
236
|
return 0;
|
165
237
|
}**/
|
166
238
|
nextPage(size, offset) {
|
167
|
-
return
|
168
|
-
return this.firstPage(size, offset);
|
169
|
-
});
|
239
|
+
return this.firstPage(size, offset);
|
170
240
|
}
|
171
241
|
previousPage(size, offset) {
|
172
|
-
return
|
173
|
-
return this.firstPage(size, offset);
|
174
|
-
});
|
242
|
+
return this.firstPage(size, offset);
|
175
243
|
}
|
176
244
|
firstPage(size, offset) {
|
177
|
-
return
|
178
|
-
return this.getPaginated({ page: { size, offset } });
|
179
|
-
});
|
245
|
+
return this.getPaginated({ page: { size, offset } });
|
180
246
|
}
|
181
247
|
lastPage(size, offset) {
|
182
|
-
return
|
183
|
-
return this.getPaginated({ page: { size, offset, before: 'end' } });
|
184
|
-
});
|
248
|
+
return this.getPaginated({ page: { size, offset, before: 'end' } });
|
185
249
|
}
|
186
250
|
hasNextPage() {
|
187
251
|
return this.meta.page.more;
|
@@ -0,0 +1,41 @@
|
|
1
|
+
import { Selectable } from './selection';
|
2
|
+
/**
|
3
|
+
* Represents an identifiable record from the database.
|
4
|
+
*/
|
5
|
+
export interface Identifiable {
|
6
|
+
/**
|
7
|
+
* Unique id of this record.
|
8
|
+
*/
|
9
|
+
id: string;
|
10
|
+
}
|
11
|
+
/**
|
12
|
+
* Represents a persisted record from the database.
|
13
|
+
*/
|
14
|
+
export interface XataRecord extends Identifiable {
|
15
|
+
/**
|
16
|
+
* Metadata of this record.
|
17
|
+
*/
|
18
|
+
xata: {
|
19
|
+
/**
|
20
|
+
* Number that is increased every time the record is updated.
|
21
|
+
*/
|
22
|
+
version: number;
|
23
|
+
};
|
24
|
+
/**
|
25
|
+
* Retrieves a refreshed copy of the current record from the database.
|
26
|
+
*/
|
27
|
+
read(): Promise<this>;
|
28
|
+
/**
|
29
|
+
* Performs a partial update of the current record. On success a new object is
|
30
|
+
* returned and the current object is not mutated.
|
31
|
+
* @param data The columns and their values that have to be updated.
|
32
|
+
* @returns A new record containing the latest values for all the columns of the current record.
|
33
|
+
*/
|
34
|
+
update(data: Partial<Selectable<this>>): Promise<this>;
|
35
|
+
/**
|
36
|
+
* Performs a deletion of the current record in the database.
|
37
|
+
*
|
38
|
+
* @throws If the record was already deleted or if an error happened while performing the deletion.
|
39
|
+
*/
|
40
|
+
delete(): Promise<void>;
|
41
|
+
}
|