@decaf-ts/core 0.20.0 → 0.21.0
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/lib/cjs/index.cjs +1 -1
- package/lib/esm/index.js +1 -1
- package/lib/types/auth/constants.d.ts +5 -0
- package/lib/types/auth/decorators.d.ts +5 -0
- package/lib/types/auth/index.d.ts +2 -0
- package/lib/types/fs/FilesystemAdapter.d.ts +70 -0
- package/lib/types/fs/FsDispatch.d.ts +7 -0
- package/lib/types/fs/helpers.d.ts +17 -0
- package/lib/types/fs/index.d.ts +4 -0
- package/lib/types/fs/indexStore.d.ts +28 -0
- package/lib/types/fs/locks/FilesystemLock.d.ts +13 -0
- package/lib/types/fs/locks/FilesystemMultiLock.d.ts +8 -0
- package/lib/types/fs/types.d.ts +40 -0
- package/lib/types/identity/decorators.d.ts +72 -0
- package/lib/types/identity/index.d.ts +6 -0
- package/lib/types/index.d.cts +1 -1
- package/lib/types/index.d.mts +1 -1
- package/lib/types/index.d.ts +34 -0
- package/lib/types/interfaces/ContextuallyLogged.d.ts +8 -0
- package/lib/types/interfaces/ErrorParser.d.ts +16 -0
- package/lib/types/interfaces/Executor.d.ts +16 -0
- package/lib/types/interfaces/Observable.d.ts +33 -0
- package/lib/types/interfaces/Observer.d.ts +15 -0
- package/lib/types/interfaces/Paginatable.d.ts +20 -0
- package/lib/types/interfaces/Queriable.d.ts +47 -0
- package/lib/types/interfaces/RawExecutor.d.ts +28 -0
- package/lib/types/interfaces/SequenceOptions.d.ts +79 -0
- package/lib/types/interfaces/index.d.ts +12 -0
- package/lib/types/migrations/Migration.d.ts +19 -0
- package/lib/types/migrations/MigrationService.d.ts +63 -0
- package/lib/types/migrations/MigrationTaskBuilder.d.ts +10 -0
- package/lib/types/migrations/MigrationTasks.d.ts +9 -0
- package/lib/types/migrations/MigrationVersioning.d.ts +7 -0
- package/lib/types/migrations/SemverMigrationVersioning.d.ts +10 -0
- package/lib/types/migrations/StandardMigrationVersioning.d.ts +12 -0
- package/lib/types/migrations/constants.d.ts +2 -0
- package/lib/types/migrations/decorators.d.ts +12 -0
- package/lib/types/migrations/index.d.ts +15 -0
- package/lib/types/migrations/types.d.ts +46 -0
- package/lib/types/model/BaseModel.d.ts +37 -0
- package/lib/types/model/SequenceModel.d.ts +32 -0
- package/lib/types/model/construction.d.ts +455 -0
- package/lib/types/model/decorators.d.ts +237 -0
- package/lib/types/model/index.d.ts +11 -0
- package/lib/types/model/indexing.d.ts +20 -0
- package/lib/types/model/types.d.ts +60 -0
- package/lib/types/overrides/Metadata.d.ts +22 -0
- package/lib/types/overrides/Model.d.ts +94 -0
- package/lib/types/overrides/ModelBuilder.d.ts +31 -0
- package/lib/types/overrides/index.d.ts +11 -0
- package/lib/types/overrides/injectables.d.ts +10 -0
- package/lib/types/overrides/overrides.d.ts +1 -0
- package/lib/types/persistence/Adapter.d.ts +469 -0
- package/lib/types/persistence/Context.d.ts +16 -0
- package/lib/types/persistence/ContextLock.d.ts +18 -0
- package/lib/types/persistence/Dispatch.d.ts +128 -0
- package/lib/types/persistence/ObserverHandler.d.ts +111 -0
- package/lib/types/persistence/Sequence.d.ts +126 -0
- package/lib/types/persistence/constants.d.ts +74 -0
- package/lib/types/persistence/decorators.d.ts +28 -0
- package/lib/types/persistence/errors.d.ts +33 -0
- package/lib/types/persistence/event-filters.d.ts +20 -0
- package/lib/types/persistence/generators.d.ts +14 -0
- package/lib/types/persistence/index.d.ts +18 -0
- package/lib/types/persistence/transactions.d.ts +4 -0
- package/lib/types/persistence/types.d.ts +86 -0
- package/lib/types/query/Condition.d.ts +131 -0
- package/lib/types/query/MethodQueryBuilder.d.ts +185 -0
- package/lib/types/query/Paginator.d.ts +105 -0
- package/lib/types/query/Statement.d.ts +136 -0
- package/lib/types/query/constants.d.ts +69 -0
- package/lib/types/query/decorators.d.ts +8 -0
- package/lib/types/query/errors.d.ts +21 -0
- package/lib/types/query/index.d.ts +16 -0
- package/lib/types/query/options.d.ts +393 -0
- package/lib/types/query/selectors.d.ts +39 -0
- package/lib/types/query/types.d.ts +158 -0
- package/lib/types/query/utils.d.ts +40 -0
- package/lib/types/ram/RamAdapter.d.ts +341 -0
- package/lib/types/ram/RamPaginator.d.ts +54 -0
- package/lib/types/ram/RamStatement.d.ts +92 -0
- package/lib/types/ram/constants.d.ts +8 -0
- package/lib/types/ram/handlers.d.ts +24 -0
- package/lib/types/ram/index.d.ts +12 -0
- package/lib/types/ram/types.d.ts +50 -0
- package/lib/types/repository/Repository.d.ts +501 -0
- package/lib/types/repository/constants.d.ts +36 -0
- package/lib/types/repository/decorators.d.ts +30 -0
- package/lib/types/repository/errors.d.ts +19 -0
- package/lib/types/repository/index.d.ts +12 -0
- package/lib/types/repository/injectables.d.ts +95 -0
- package/lib/types/repository/types.d.ts +28 -0
- package/lib/types/repository/utils.d.ts +36 -0
- package/lib/types/services/ModelService.d.ts +45 -0
- package/lib/types/services/PersistenceService.d.ts +17 -0
- package/lib/types/services/index.d.ts +8 -0
- package/lib/types/services/services.d.ts +96 -0
- package/lib/types/tasks/CleanUpTask.d.ts +15 -0
- package/lib/types/tasks/TaskContext.d.ts +30 -0
- package/lib/types/tasks/TaskEngine.d.ts +97 -0
- package/lib/types/tasks/TaskErrors.d.ts +65 -0
- package/lib/types/tasks/TaskEventBus.d.ts +17 -0
- package/lib/types/tasks/TaskEventService.d.ts +7 -0
- package/lib/types/tasks/TaskHandler.d.ts +12 -0
- package/lib/types/tasks/TaskHandlerRegistry.d.ts +8 -0
- package/lib/types/tasks/TaskService.d.ts +65 -0
- package/lib/types/tasks/TaskStateChangeError.d.ts +11 -0
- package/lib/types/tasks/TaskTracker.d.ts +47 -0
- package/lib/types/tasks/builder.d.ts +72 -0
- package/lib/types/tasks/constants.d.ts +31 -0
- package/lib/types/tasks/decorators.d.ts +4 -0
- package/lib/types/tasks/index.d.ts +23 -0
- package/lib/types/tasks/logging.d.ts +31 -0
- package/lib/types/tasks/models/TaskBackoffModel.d.ts +9 -0
- package/lib/types/tasks/models/TaskErrorModel.d.ts +8 -0
- package/lib/types/tasks/models/TaskEventModel.d.ts +11 -0
- package/lib/types/tasks/models/TaskIOSerializer.d.ts +33 -0
- package/lib/types/tasks/models/TaskLogEntryModel.d.ts +10 -0
- package/lib/types/tasks/models/TaskModel.d.ts +43 -0
- package/lib/types/tasks/models/TaskStepResultModel.d.ts +9 -0
- package/lib/types/tasks/models/TaskStepSpecModel.d.ts +9 -0
- package/lib/types/tasks/models/index.d.ts +8 -0
- package/lib/types/tasks/types.d.ts +71 -0
- package/lib/types/tasks/utils.d.ts +5 -0
- package/lib/types/utils/ContextualLoggedClass.d.ts +37 -0
- package/lib/types/utils/decorators.d.ts +27 -0
- package/lib/types/utils/errors.d.ts +56 -0
- package/lib/types/utils/index.d.ts +11 -0
- package/lib/types/utils/throttling.d.ts +9 -0
- package/lib/types/utils/types.d.ts +23 -0
- package/lib/types/utils/utils.d.ts +22 -0
- package/lib/types/workers/TaskEngine.d.ts +40 -0
- package/lib/types/workers/WorkThreadEnvironment.d.ts +35 -0
- package/lib/types/workers/index.d.ts +5 -0
- package/lib/types/workers/messages.d.ts +69 -0
- package/lib/types/workers/types.d.ts +41 -0
- package/lib/types/workers/workerThread.d.ts +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { QueryAction, QueryAssist } from "./types";
|
|
2
|
+
import { LoggedClass, Logger } from "@decaf-ts/logging";
|
|
3
|
+
export type QueryActionPrefix = {
|
|
4
|
+
action: QueryAction;
|
|
5
|
+
prefix: string;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* @description
|
|
9
|
+
* Utility class to build query objects from repository method names.
|
|
10
|
+
*
|
|
11
|
+
* @summary
|
|
12
|
+
* The `MethodQueryBuilder` class parses method names that follow a specific naming convention
|
|
13
|
+
* (e.g., `findByNameAndAgeOrderByCountryAsc`) and converts them into structured query objects
|
|
14
|
+
* (`QueryAssist`). It extracts clauses such as `select`, `where`, `groupBy`, `orderBy`, `limit`,
|
|
15
|
+
* and `offset`, ensuring that developers can declare repository queries using expressive method names.
|
|
16
|
+
*
|
|
17
|
+
* @param methodName {string} - The repository method name to parse and convert into a query.
|
|
18
|
+
* @param values {any[]} - The values corresponding to method parameters used for query conditions.
|
|
19
|
+
*
|
|
20
|
+
* @return {QueryAssist} A structured query object describing the parsed action, select, where,
|
|
21
|
+
* groupBy, orderBy, limit, and offset clauses.
|
|
22
|
+
*
|
|
23
|
+
* @class
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* const query = MethodQueryBuilder.build(
|
|
28
|
+
* "findByNameAndAgeOrderByCountryAsc",
|
|
29
|
+
* "John",
|
|
30
|
+
* 25,
|
|
31
|
+
* [["country", "ASC"]]
|
|
32
|
+
* );
|
|
33
|
+
*
|
|
34
|
+
* console.log(query);
|
|
35
|
+
* // {
|
|
36
|
+
* // action: "find",
|
|
37
|
+
* // select: undefined,
|
|
38
|
+
* // where: { ... },
|
|
39
|
+
* // groupBy: undefined,
|
|
40
|
+
* // orderBy: [["country", "ASC"]],
|
|
41
|
+
* // limit: undefined,
|
|
42
|
+
* // offset: undefined
|
|
43
|
+
* // }
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @mermaid
|
|
47
|
+
* sequenceDiagram
|
|
48
|
+
* participant Repo as Repository Method
|
|
49
|
+
* participant MQB as MethodQueryBuilder
|
|
50
|
+
* participant Query as QueryAssist
|
|
51
|
+
*
|
|
52
|
+
* Repo->>MQB: build(methodName, ...values)
|
|
53
|
+
* MQB->>MQB: extractCore(methodName)
|
|
54
|
+
* MQB->>MQB: extractSelect(methodName)
|
|
55
|
+
* MQB->>MQB: extractGroupBy(methodName)
|
|
56
|
+
* MQB->>MQB: buildWhere(core, values)
|
|
57
|
+
* MQB->>MQB: extractOrderLimitOffset(core, values)
|
|
58
|
+
* MQB->>Query: return structured QueryAssist object
|
|
59
|
+
*/
|
|
60
|
+
export declare class MethodQueryBuilder extends LoggedClass {
|
|
61
|
+
private static _logger;
|
|
62
|
+
protected static get log(): Logger;
|
|
63
|
+
/**
|
|
64
|
+
* @description
|
|
65
|
+
* Map of query prefixes to their corresponding actions.
|
|
66
|
+
*/
|
|
67
|
+
private static readonly prefixMap;
|
|
68
|
+
/**
|
|
69
|
+
* @description
|
|
70
|
+
* Determines the action and prefix length from a method name.
|
|
71
|
+
*
|
|
72
|
+
* @param methodName {string} - The repository method name.
|
|
73
|
+
* @return {QueryActionPrefix} | undefined} The action and prefix if found.
|
|
74
|
+
*/
|
|
75
|
+
private static getActionFromMethodName;
|
|
76
|
+
/**
|
|
77
|
+
* @description
|
|
78
|
+
* Builds a `QueryAssist` object by parsing a repository method name and values.
|
|
79
|
+
*
|
|
80
|
+
* @summary
|
|
81
|
+
* The method validates the method name, extracts clauses (core, select, groupBy, where,
|
|
82
|
+
* orderBy, limit, and offset), and assembles them into a structured query object
|
|
83
|
+
* that can be executed against a data source.
|
|
84
|
+
*
|
|
85
|
+
* @param methodName {string} - The repository method name that encodes query information.
|
|
86
|
+
* @param values {any[]} - The values corresponding to conditions and extra clauses.
|
|
87
|
+
*
|
|
88
|
+
* @return {QueryAssist} A structured query object representing the parsed query.
|
|
89
|
+
*/
|
|
90
|
+
static build(methodName: string, ...values: any[]): QueryAssist;
|
|
91
|
+
/**
|
|
92
|
+
* @description
|
|
93
|
+
* Extracts the aggregation selector field from method names like countByAge, sumByPrice.
|
|
94
|
+
*
|
|
95
|
+
* @param methodName {string} - The method name.
|
|
96
|
+
* @param prefix {string} - The prefix to remove.
|
|
97
|
+
* @return {string | undefined} The selector field name.
|
|
98
|
+
*/
|
|
99
|
+
private static extractAggregationSelector;
|
|
100
|
+
/**
|
|
101
|
+
* @description
|
|
102
|
+
* Extracts the core part of the method name after the prefix and before any special clauses.
|
|
103
|
+
*
|
|
104
|
+
* @summary
|
|
105
|
+
* Removes prefixes and detects delimiters (`Then`, `OrderBy`, `GroupBy`, `Limit`, `Offset`)
|
|
106
|
+
* to isolate the main conditional part of the query.
|
|
107
|
+
*
|
|
108
|
+
* @param methodName {string} - The method name to parse.
|
|
109
|
+
* @param prefix {string} - The prefix to remove (e.g., "findBy", "countBy").
|
|
110
|
+
*
|
|
111
|
+
* @return {string} The extracted core string used for building conditions.
|
|
112
|
+
*/
|
|
113
|
+
private static extractCore;
|
|
114
|
+
static getFieldsFromMethodName(methodName: string): Array<string>;
|
|
115
|
+
/**
|
|
116
|
+
* @description
|
|
117
|
+
* Extracts the select clause from a method name.
|
|
118
|
+
*
|
|
119
|
+
* @summary
|
|
120
|
+
* Detects the `Select` keyword in the method name, isolates the fields following it,
|
|
121
|
+
* and returns them as an array of lowercase-first strings.
|
|
122
|
+
*
|
|
123
|
+
* @param methodName {string} - The method name to parse.
|
|
124
|
+
*
|
|
125
|
+
* @return {string[] | undefined} An array of selected fields or `undefined` if no select clause exists.
|
|
126
|
+
*/
|
|
127
|
+
private static extractSelect;
|
|
128
|
+
/**
|
|
129
|
+
* @description
|
|
130
|
+
* Extracts the group by clause from a method name.
|
|
131
|
+
*
|
|
132
|
+
* @summary
|
|
133
|
+
* Detects the `GroupBy` keyword in the method name, isolates the fields following it,
|
|
134
|
+
* and returns them as an array of lowercase-first strings.
|
|
135
|
+
*
|
|
136
|
+
* @param methodName {string} - The method name to parse.
|
|
137
|
+
*
|
|
138
|
+
* @return {string[] | undefined} An array of group by fields or `undefined` if no group by clause exists.
|
|
139
|
+
*/
|
|
140
|
+
private static extractGroupBy;
|
|
141
|
+
/**
|
|
142
|
+
* @description
|
|
143
|
+
* Builds the `where` condition object based on the parsed core string and parameter values.
|
|
144
|
+
*
|
|
145
|
+
* @summary
|
|
146
|
+
* Splits the core string by logical operators (`And`, `Or`), parses each token into a field
|
|
147
|
+
* and operator, and combines them into a `Condition` object using the provided values.
|
|
148
|
+
*
|
|
149
|
+
* @param core {string} - The extracted core string from the method name.
|
|
150
|
+
* @param values {any[]} - The values corresponding to the conditions.
|
|
151
|
+
*
|
|
152
|
+
* @return {Condition<any>} A structured condition object representing the query's where clause.
|
|
153
|
+
*/
|
|
154
|
+
private static buildWhere;
|
|
155
|
+
/**
|
|
156
|
+
* @description
|
|
157
|
+
* Parses a field name and operator from a string token.
|
|
158
|
+
*
|
|
159
|
+
* @summary
|
|
160
|
+
* Identifies the operator suffix (if present) and returns a descriptor containing the field
|
|
161
|
+
* name in lowercase-first format along with the operator.
|
|
162
|
+
*
|
|
163
|
+
* @param str {string} - The token string to parse.
|
|
164
|
+
*
|
|
165
|
+
* @return {FilterDescriptor} An object containing the field name and operator.
|
|
166
|
+
*/
|
|
167
|
+
private static parseFieldAndOperator;
|
|
168
|
+
private static extractOrderByField;
|
|
169
|
+
private static getProperlyOrderByOrThrow;
|
|
170
|
+
/**
|
|
171
|
+
* @description
|
|
172
|
+
* Extracts `orderBy`, `limit`, and `offset` clauses from method arguments.
|
|
173
|
+
*
|
|
174
|
+
* @summary
|
|
175
|
+
* Determines the number of condition arguments, then checks the remaining arguments
|
|
176
|
+
* to resolve sorting, limiting, and pagination.
|
|
177
|
+
*
|
|
178
|
+
* @param methodName {string} - The method name.
|
|
179
|
+
* @param values {any[]} - The values corresponding to method arguments, including conditions and extras.
|
|
180
|
+
* @param core {string} - The pre-extracted core string.
|
|
181
|
+
*
|
|
182
|
+
* @return {OrderLimitOffsetExtract} An object containing orderBy, limit, and offset values if present.
|
|
183
|
+
*/
|
|
184
|
+
private static extractOrderLimitOffset;
|
|
185
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { Adapter } from "../persistence/Adapter";
|
|
2
|
+
import { Model } from "@decaf-ts/decorator-validation";
|
|
3
|
+
import { Constructor } from "@decaf-ts/decoration";
|
|
4
|
+
import { LoggedClass } from "@decaf-ts/logging";
|
|
5
|
+
import { ContextualArgs, MaybeContextualArg } from "../utils/ContextualLoggedClass";
|
|
6
|
+
import { PreparedStatement } from "./types";
|
|
7
|
+
/**
|
|
8
|
+
* @description Handles pagination for database queries
|
|
9
|
+
* @summary Provides functionality for navigating through paginated query results
|
|
10
|
+
*
|
|
11
|
+
* This abstract class manages the state and navigation of paginated database query results.
|
|
12
|
+
* It tracks the current page, total pages, and record count, and provides methods for
|
|
13
|
+
* moving between pages.
|
|
14
|
+
*
|
|
15
|
+
* @template M - The model type this paginator operates on
|
|
16
|
+
* @template R - The return type of the paginated query (defaults to M[])
|
|
17
|
+
* @template Q - The query type (defaults to any)
|
|
18
|
+
* @param {Adapter<any, Q, any, any>} adapter - The database adapter to use for executing queries
|
|
19
|
+
* @param {Q} query - The query to paginate
|
|
20
|
+
* @param {number} size - The number of records per page
|
|
21
|
+
* @param {Constructor<M>} clazz - The constructor for the model type
|
|
22
|
+
* @class Paginator
|
|
23
|
+
* @example
|
|
24
|
+
* // Create a paginator for a user query
|
|
25
|
+
* const userQuery = db.select().from(User);
|
|
26
|
+
* const paginator = await userQuery.paginate(10); // 10 users per page
|
|
27
|
+
*
|
|
28
|
+
* // Get the first page of results
|
|
29
|
+
* const firstPage = await paginator.page(1);
|
|
30
|
+
*
|
|
31
|
+
* // Navigate to the next page
|
|
32
|
+
* const secondPage = await paginator.next();
|
|
33
|
+
*
|
|
34
|
+
* // Get information about the pagination
|
|
35
|
+
* console.log(`Page ${paginator.current} of ${paginator.total}, ${paginator.count} total records`);
|
|
36
|
+
*
|
|
37
|
+
* @mermaid
|
|
38
|
+
* sequenceDiagram
|
|
39
|
+
* participant Client
|
|
40
|
+
* participant Paginator
|
|
41
|
+
* participant Adapter
|
|
42
|
+
* participant Database
|
|
43
|
+
*
|
|
44
|
+
* Client->>Paginator: new Paginator(adapter, query, size, clazz)
|
|
45
|
+
* Client->>Paginator: page(1)
|
|
46
|
+
* Paginator->>Paginator: validatePage(1)
|
|
47
|
+
* Paginator->>Paginator: prepare(query)
|
|
48
|
+
* Paginator->>Adapter: execute query with pagination
|
|
49
|
+
* Adapter->>Database: execute query
|
|
50
|
+
* Database-->>Adapter: return results
|
|
51
|
+
* Adapter-->>Paginator: return results
|
|
52
|
+
* Paginator-->>Client: return page results
|
|
53
|
+
*
|
|
54
|
+
* Client->>Paginator: next()
|
|
55
|
+
* Paginator->>Paginator: page(current + 1)
|
|
56
|
+
* Paginator->>Paginator: validatePage(current + 1)
|
|
57
|
+
* Paginator->>Adapter: execute query with pagination
|
|
58
|
+
* Adapter->>Database: execute query
|
|
59
|
+
* Database-->>Adapter: return results
|
|
60
|
+
* Adapter-->>Paginator: return results
|
|
61
|
+
* Paginator-->>Client: return page results
|
|
62
|
+
*/
|
|
63
|
+
export declare abstract class Paginator<M extends Model, R = M[], Q = any> extends LoggedClass {
|
|
64
|
+
protected readonly adapter: Adapter<any, any, Q, any>;
|
|
65
|
+
protected readonly query: Q | PreparedStatement<M>;
|
|
66
|
+
readonly size: number;
|
|
67
|
+
protected readonly clazz: Constructor<M>;
|
|
68
|
+
protected _currentPage: number;
|
|
69
|
+
protected _totalPages: number;
|
|
70
|
+
protected _recordCount: number;
|
|
71
|
+
protected _bookmark?: number | string;
|
|
72
|
+
protected limit: number;
|
|
73
|
+
private _statement?;
|
|
74
|
+
get current(): number;
|
|
75
|
+
get total(): number;
|
|
76
|
+
get count(): number;
|
|
77
|
+
protected get statement(): Q;
|
|
78
|
+
protected constructor(adapter: Adapter<any, any, Q, any>, query: Q | PreparedStatement<M>, size: number, clazz: Constructor<M>);
|
|
79
|
+
protected isPreparedStatement(): "" | RegExpMatchArray | null;
|
|
80
|
+
protected pagePrefix(page?: number, ...args: MaybeContextualArg<any>): Promise<any[]>;
|
|
81
|
+
protected pagePrepared(page: number, bookmark?: any, ...argz: ContextualArgs<any>): Promise<M[]>;
|
|
82
|
+
/**
|
|
83
|
+
* @description Prepares a statement for pagination
|
|
84
|
+
* @summary Modifies the raw query statement to include pagination parameters.
|
|
85
|
+
* This protected method sets the limit parameter on the query to match the page size.
|
|
86
|
+
* @param {RawRamQuery<M>} rawStatement - The original query statement
|
|
87
|
+
* @return {RawRamQuery<M>} The modified query with pagination parameters
|
|
88
|
+
*/
|
|
89
|
+
protected abstract prepare(rawStatement: Q): Q;
|
|
90
|
+
next(...args: MaybeContextualArg<any>): Promise<R>;
|
|
91
|
+
previous(...args: MaybeContextualArg<any>): Promise<R>;
|
|
92
|
+
protected validatePage(page: number): number;
|
|
93
|
+
page(page?: number, bookmark?: any, ...args: MaybeContextualArg<any>): Promise<R>;
|
|
94
|
+
serialize(data: M[], toString?: boolean): string | SerializedPage<M>;
|
|
95
|
+
apply(serialization: string | SerializedPage<M>): M[];
|
|
96
|
+
static deserialize<M extends Model>(str: string): SerializedPage<M>;
|
|
97
|
+
static isSerializedPage(obj: SerializedPage<any> | any): any;
|
|
98
|
+
}
|
|
99
|
+
export type SerializedPage<M extends Model> = {
|
|
100
|
+
current: number;
|
|
101
|
+
total: number;
|
|
102
|
+
count: number;
|
|
103
|
+
data: M[];
|
|
104
|
+
bookmark?: number | string;
|
|
105
|
+
};
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { Model } from "@decaf-ts/decorator-validation";
|
|
2
|
+
import type { Executor, RawExecutor } from "../interfaces";
|
|
3
|
+
import type { FromSelector, GroupBySelector, OrderBySelector, OrderDirectionInput, SelectSelector } from "./selectors";
|
|
4
|
+
import { Condition } from "./Condition";
|
|
5
|
+
import { Logger } from "@decaf-ts/logging";
|
|
6
|
+
import type { CountDistinctOption, CountOption, DistinctOption, GroupByResult, MaxOption, MinOption, OffsetOption, OrderAndGroupOption, OrderByResult, OrderByThenByOption, PreparableStatementExecutor, SelectOption, StatementExecutor, SumOption, AvgOption, WhereOption } from "./options";
|
|
7
|
+
import { Paginatable } from "../interfaces/Paginatable";
|
|
8
|
+
import { Paginator } from "./Paginator";
|
|
9
|
+
import { Adapter } from "../persistence/Adapter";
|
|
10
|
+
import type { AdapterFlags, ContextOf } from "../persistence/types";
|
|
11
|
+
import { Constructor } from "@decaf-ts/decoration";
|
|
12
|
+
import { type ContextualArgs, ContextualLoggedClass, type MaybeContextualArg } from "../utils/ContextualLoggedClass";
|
|
13
|
+
import { PreparedStatement } from "./types";
|
|
14
|
+
import { Repository } from "../repository/Repository";
|
|
15
|
+
/**
|
|
16
|
+
* @description Base class for database query statements
|
|
17
|
+
* @summary Provides a foundation for building and executing database queries
|
|
18
|
+
*
|
|
19
|
+
* This abstract class implements the query builder pattern for constructing
|
|
20
|
+
* database queries. It supports various query operations like select, from,
|
|
21
|
+
* where, orderBy, groupBy, limit, and offset. It also provides methods for
|
|
22
|
+
* executing queries and handling pagination.
|
|
23
|
+
*
|
|
24
|
+
* @template Q - The query type specific to the database adapter
|
|
25
|
+
* @template M - The model type this statement operates on
|
|
26
|
+
* @template R - The return type of the query
|
|
27
|
+
* @param {Adapter<any, Q, any, any>} adapter - The database adapter to use for executing queries
|
|
28
|
+
* @class Statement
|
|
29
|
+
* @example
|
|
30
|
+
* // Create a statement to query users
|
|
31
|
+
* const statement = new SQLStatement(adapter);
|
|
32
|
+
* const users = await statement
|
|
33
|
+
* .select()
|
|
34
|
+
* .from(User)
|
|
35
|
+
* .where(Condition.attribute("status").eq("active"))
|
|
36
|
+
* .orderBy(["createdAt", "DESC"])
|
|
37
|
+
* .limit(10)
|
|
38
|
+
* .execute();
|
|
39
|
+
*
|
|
40
|
+
* // Use pagination
|
|
41
|
+
* const paginator = await statement
|
|
42
|
+
* .select()
|
|
43
|
+
* .from(User)
|
|
44
|
+
* .paginate(20); // 20 users per page
|
|
45
|
+
*
|
|
46
|
+
* @mermaid
|
|
47
|
+
* sequenceDiagram
|
|
48
|
+
* participant Client
|
|
49
|
+
* participant Statement
|
|
50
|
+
* participant Adapter
|
|
51
|
+
* participant Database
|
|
52
|
+
*
|
|
53
|
+
* Client->>Statement: select()
|
|
54
|
+
* Client->>Statement: from(Model)
|
|
55
|
+
* Client->>Statement: where(condition)
|
|
56
|
+
* Client->>Statement: orderBy([field, direction])
|
|
57
|
+
* Client->>Statement: limit(value)
|
|
58
|
+
* Client->>Statement: execute()
|
|
59
|
+
* Statement->>Statement: build()
|
|
60
|
+
* Statement->>Adapter: raw(query)
|
|
61
|
+
* Adapter->>Database: execute query
|
|
62
|
+
* Database-->>Adapter: return results
|
|
63
|
+
* Adapter-->>Statement: return processed results
|
|
64
|
+
* Statement-->>Client: return final results
|
|
65
|
+
*/
|
|
66
|
+
export declare abstract class Statement<M extends Model, A extends Adapter<any, any, any, any>, R, Q = A extends Adapter<any, any, infer Q, any> ? Q : never> extends ContextualLoggedClass<ContextOf<A>> implements Executor<R>, RawExecutor<Q>, Paginatable<M, R, Q> {
|
|
67
|
+
protected adapter: Adapter<any, any, Q, any>;
|
|
68
|
+
protected overrides?: Partial<AdapterFlags> | undefined;
|
|
69
|
+
protected readonly selectSelector?: SelectSelector<M>[];
|
|
70
|
+
protected distinctSelector?: SelectSelector<M>;
|
|
71
|
+
protected maxSelector?: SelectSelector<M>;
|
|
72
|
+
protected minSelector?: SelectSelector<M>;
|
|
73
|
+
protected countSelector?: SelectSelector<M> | null;
|
|
74
|
+
protected countDistinctSelector?: SelectSelector<M> | null;
|
|
75
|
+
protected sumSelector?: SelectSelector<M>;
|
|
76
|
+
protected avgSelector?: SelectSelector<M>;
|
|
77
|
+
protected _inCountMode: boolean;
|
|
78
|
+
protected fromSelector: Constructor<M>;
|
|
79
|
+
protected whereCondition?: Condition<M>;
|
|
80
|
+
protected orderBySelectors?: OrderBySelector<M>[];
|
|
81
|
+
protected groupBySelectors?: GroupBySelector<M>[];
|
|
82
|
+
protected limitSelector?: number;
|
|
83
|
+
protected offsetSelector?: number;
|
|
84
|
+
protected prepared?: PreparedStatement<M>;
|
|
85
|
+
protected constructor(adapter: Adapter<any, any, Q, any>, overrides?: Partial<AdapterFlags> | undefined);
|
|
86
|
+
protected executionPrefix(method: any, ...args: MaybeContextualArg<ContextOf<A>>): Promise<[Constructor<M>, ...any[], any]>;
|
|
87
|
+
protected get log(): Logger;
|
|
88
|
+
select<S extends readonly SelectSelector<M>[]>(): SelectOption<M, M[]>;
|
|
89
|
+
select<S extends readonly SelectSelector<M>[]>(selector: readonly [...S]): SelectOption<M, Pick<M, S[number]>[]>;
|
|
90
|
+
distinct<S extends SelectSelector<M>>(selector: S): DistinctOption<M, M[S][]>;
|
|
91
|
+
distinct(): CountDistinctOption<M>;
|
|
92
|
+
max<S extends SelectSelector<M>>(selector: S): MaxOption<M, M[S]>;
|
|
93
|
+
min<S extends SelectSelector<M>>(selector: S): MinOption<M, M[S]>;
|
|
94
|
+
sum<S extends SelectSelector<M>>(selector: S): SumOption<M, number>;
|
|
95
|
+
avg<S extends SelectSelector<M>>(selector: S): AvgOption<M, number>;
|
|
96
|
+
count<S extends SelectSelector<M>>(selector?: S): CountOption<M>;
|
|
97
|
+
from(selector: FromSelector<M>): WhereOption<M, R>;
|
|
98
|
+
where(condition: Condition<M>): OrderAndGroupOption<M, R>;
|
|
99
|
+
orderBy(selector: OrderBySelector<M>): OrderByResult<M, R>;
|
|
100
|
+
orderBy(attribute: keyof M, direction: OrderDirectionInput): OrderByResult<M, R>;
|
|
101
|
+
thenBy(selector: GroupBySelector<M>): GroupByResult<M>;
|
|
102
|
+
thenBy(selector: OrderBySelector<M>): OrderByThenByOption<M, R>;
|
|
103
|
+
thenBy(attribute: keyof M, direction: OrderDirectionInput): OrderByThenByOption<M, R>;
|
|
104
|
+
private normalizeOrderCriterion;
|
|
105
|
+
private normalizeOrderDirection;
|
|
106
|
+
groupBy<Key extends GroupBySelector<M>>(selector: Key): GroupByResult<M, [Key]>;
|
|
107
|
+
limit(value: number): OffsetOption<M, R>;
|
|
108
|
+
offset(value: number): PreparableStatementExecutor<M, R>;
|
|
109
|
+
execute(...args: MaybeContextualArg<ContextOf<A>>): Promise<R>;
|
|
110
|
+
protected revertGroupedResults(value: any, processor: (record: any) => any): any;
|
|
111
|
+
protected executePrepared(...argz: ContextualArgs<ContextOf<A>>): Promise<R>;
|
|
112
|
+
raw<R>(rawInput: Q, ...args: ContextualArgs<ContextOf<A>>): Promise<R>;
|
|
113
|
+
protected processRecord(record: any, ctx: ContextOf<A>): M;
|
|
114
|
+
protected applyAfterHandlersToResult(value: any, ctx: ContextOf<A>): Promise<any>;
|
|
115
|
+
protected getRepository(): Repository<M, A>;
|
|
116
|
+
protected prepareCondition(condition: Condition<any>, ctx: ContextOf<A>): PreparedStatement<any>;
|
|
117
|
+
protected squash(ctx: ContextOf<A>): PreparedStatement<any> | undefined;
|
|
118
|
+
private matchDefaultQueryCondition;
|
|
119
|
+
private extractDefaultStartsWithAttributes;
|
|
120
|
+
private collectStartsWithAttributes;
|
|
121
|
+
private getOrderDirection;
|
|
122
|
+
prepare(ctx?: ContextOf<A>): Promise<StatementExecutor<M, R>>;
|
|
123
|
+
protected isSimpleQuery(): boolean;
|
|
124
|
+
protected hasAggregation(): boolean;
|
|
125
|
+
protected abstract build(): Q;
|
|
126
|
+
protected abstract parseCondition(condition: Condition<M>, ...args: any[]): Q;
|
|
127
|
+
/**
|
|
128
|
+
* @description Creates a paginator for the query
|
|
129
|
+
* @summary Builds the query and wraps it in a RamPaginator to enable pagination of results.
|
|
130
|
+
* This allows retrieving large result sets in smaller chunks.
|
|
131
|
+
* @param {number} size - The page size (number of results per page)
|
|
132
|
+
* @return {Promise<Paginator<M, R, RawRamQuery<M>>>} A promise that resolves to a paginator for the query
|
|
133
|
+
*/
|
|
134
|
+
paginate(size: number, ...args: MaybeContextualArg<ContextOf<A>>): Promise<Paginator<M, R, Q>>;
|
|
135
|
+
toString(): string;
|
|
136
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description Comparison operators for query conditions
|
|
3
|
+
* @summary Enum defining the available operators for comparing values in database queries
|
|
4
|
+
* @enum {string}
|
|
5
|
+
* @readonly
|
|
6
|
+
* @memberOf module:core
|
|
7
|
+
*/
|
|
8
|
+
export declare enum Operator {
|
|
9
|
+
/** Equal comparison (=) */
|
|
10
|
+
EQUAL = "EQUAL",
|
|
11
|
+
/** Not equal comparison (!=) */
|
|
12
|
+
DIFFERENT = "DIFFERENT",
|
|
13
|
+
/** Greater than comparison (>) */
|
|
14
|
+
BIGGER = "BIGGER",
|
|
15
|
+
/** Greater than or equal comparison (>=) */
|
|
16
|
+
BIGGER_EQ = "BIGGER_EQ",
|
|
17
|
+
/** Less than comparison (<) */
|
|
18
|
+
SMALLER = "SMALLER",
|
|
19
|
+
/** Less than or equal comparison (<=) */
|
|
20
|
+
SMALLER_EQ = "SMALLER_EQ",
|
|
21
|
+
/** Between comparison (BETWEEN) */
|
|
22
|
+
BETWEEN = "BETWEEN",
|
|
23
|
+
/** Negation operator (NOT) */
|
|
24
|
+
NOT = "NOT",
|
|
25
|
+
/** Inclusion operator (IN) */
|
|
26
|
+
IN = "IN",
|
|
27
|
+
/** Regular expression matching */
|
|
28
|
+
REGEXP = "REGEXP",
|
|
29
|
+
/** String starts-with comparison */
|
|
30
|
+
STARTS_WITH = "STARTS_WITH",
|
|
31
|
+
/** String ends-with comparison */
|
|
32
|
+
ENDS_WITH = "ENDS_WITH",
|
|
33
|
+
GROUP_BY = "group-by",
|
|
34
|
+
COUNT = "count",
|
|
35
|
+
SUM = "sum",
|
|
36
|
+
MAX = "v_max",
|
|
37
|
+
MIN = "v_min",
|
|
38
|
+
DISTINCT = "distinct",
|
|
39
|
+
VIEW = "view"
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* @description Logical operators for combining query conditions
|
|
43
|
+
* @summary Enum defining the available operators for grouping multiple conditions in database queries
|
|
44
|
+
* @enum {string}
|
|
45
|
+
* @readonly
|
|
46
|
+
* @memberOf module:core
|
|
47
|
+
*/
|
|
48
|
+
export declare enum GroupOperator {
|
|
49
|
+
/** Logical AND operator - all conditions must be true */
|
|
50
|
+
AND = "AND",
|
|
51
|
+
/** Logical OR operator - at least one condition must be true */
|
|
52
|
+
OR = "OR"
|
|
53
|
+
}
|
|
54
|
+
export declare enum PreparedStatementKeys {
|
|
55
|
+
LIST_BY = "listBy",
|
|
56
|
+
FIND_BY = "findBy",
|
|
57
|
+
FIND_BY_PAGINATE = "findByPaginate",
|
|
58
|
+
FIND_ONE_BY = "findOneBy",
|
|
59
|
+
PAGE_BY = "paginateBy",
|
|
60
|
+
FIND = "find",
|
|
61
|
+
PAGE = "page",
|
|
62
|
+
COUNT_OF = "countOf",
|
|
63
|
+
MAX_OF = "maxOf",
|
|
64
|
+
MIN_OF = "minOf",
|
|
65
|
+
AVG_OF = "avgOf",
|
|
66
|
+
SUM_OF = "sumOf",
|
|
67
|
+
DISTINCT_OF = "distinctOf",
|
|
68
|
+
GROUP_OF = "groupOf"
|
|
69
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { QueryOptions, ViewKind, ViewOptions } from "./types";
|
|
2
|
+
import { PersistenceKeys } from "../persistence/constants";
|
|
3
|
+
import { Operator } from "./constants";
|
|
4
|
+
export declare function defaultQueryAttr(): (obj: object, prop?: any) => void;
|
|
5
|
+
export declare function prepared(): (target: any, propertyKey?: any, descriptor?: TypedPropertyDescriptor<any>) => any;
|
|
6
|
+
export declare function query(options?: QueryOptions): (target: any, propertyKey?: any, descriptor?: TypedPropertyDescriptor<any>) => any;
|
|
7
|
+
export declare function applyViewDecorator(metaKey: PersistenceKeys | Operator, kind: ViewKind, opts?: ViewOptions): (target: any, attr: any) => void;
|
|
8
|
+
export declare function view<OPTS extends ViewOptions>(opts?: OPTS): (target: any, propertyKey?: any, descriptor?: TypedPropertyDescriptor<any>) => any;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { InternalError } from "@decaf-ts/db-decorators";
|
|
2
|
+
/**
|
|
3
|
+
* @description Error thrown during query operations
|
|
4
|
+
* @summary Represents errors that occur during query building or execution
|
|
5
|
+
* @param {string | Error} msg - The error message or Error object
|
|
6
|
+
* @class QueryError
|
|
7
|
+
* @category Errors
|
|
8
|
+
*/
|
|
9
|
+
export declare class QueryError extends InternalError {
|
|
10
|
+
constructor(msg: string | Error);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* @description Error thrown during pagination operations
|
|
14
|
+
* @summary Represents errors that occur during pagination setup or execution
|
|
15
|
+
* @param {string | Error} msg - The error message or Error object
|
|
16
|
+
* @class PagingError
|
|
17
|
+
* @category Errors
|
|
18
|
+
*/
|
|
19
|
+
export declare class PagingError extends InternalError {
|
|
20
|
+
constructor(msg: string | Error);
|
|
21
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description Exports for the query module.
|
|
3
|
+
* @summary This file exports all the necessary components for the query functionality, including Condition, constants, errors, and more.
|
|
4
|
+
* @module core/query
|
|
5
|
+
*/
|
|
6
|
+
export * from "./Condition";
|
|
7
|
+
export * from "./constants";
|
|
8
|
+
export * from "./errors";
|
|
9
|
+
export * from "./options";
|
|
10
|
+
export * from "./Paginator";
|
|
11
|
+
export * from "./selectors";
|
|
12
|
+
export * from "./Statement";
|
|
13
|
+
export * from "./decorators";
|
|
14
|
+
export * from "./MethodQueryBuilder";
|
|
15
|
+
export * from "./types";
|
|
16
|
+
export * from "./utils";
|