pg-query-sdk 1.0.0 → 1.0.1
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/README.md +94 -365
- package/dist/cjs/builders/ConditionBuilder.d.ts +34 -0
- package/dist/cjs/builders/ConditionBuilder.js +34 -0
- package/dist/cjs/builders/QueryBuilder.d.ts +112 -1
- package/dist/cjs/builders/QueryBuilder.js +94 -1
- package/dist/cjs/core/Database.d.ts +26 -0
- package/dist/cjs/core/Database.js +26 -0
- package/dist/cjs/core/ParamContext.d.ts +16 -0
- package/dist/cjs/core/ParamContext.js +16 -0
- package/dist/cjs/core/QueryExecutor.d.ts +26 -0
- package/dist/cjs/core/QueryExecutor.js +26 -0
- package/dist/cjs/core/TransactionManager.d.ts +13 -0
- package/dist/cjs/core/TransactionManager.js +13 -0
- package/dist/cjs/core/UnitOfWork.d.ts +19 -0
- package/dist/cjs/core/UnitOfWork.js +19 -0
- package/dist/cjs/dialects/Dialect.d.ts +13 -0
- package/dist/cjs/dialects/MysqlDialect.d.ts +12 -0
- package/dist/cjs/dialects/MysqlDialect.js +12 -0
- package/dist/cjs/dialects/PostgresDialect.d.ts +13 -0
- package/dist/cjs/dialects/PostgresDialect.js +13 -0
- package/dist/cjs/index.d.ts +28 -0
- package/dist/cjs/index.js +28 -0
- package/dist/cjs/orm/Repository.d.ts +34 -0
- package/dist/cjs/orm/Repository.js +34 -0
- package/dist/cjs/query/ConditionBuilder.d.ts +33 -0
- package/dist/cjs/query/ConditionBuilder.js +33 -0
- package/dist/cjs/query/QueryBuilder.d.ts +154 -0
- package/dist/cjs/query/QueryBuilder.js +136 -0
- package/dist/esm/builders/ConditionBuilder.d.ts +34 -0
- package/dist/esm/builders/ConditionBuilder.js +34 -0
- package/dist/esm/builders/QueryBuilder.d.ts +112 -1
- package/dist/esm/builders/QueryBuilder.js +94 -1
- package/dist/esm/core/Database.d.ts +26 -0
- package/dist/esm/core/Database.js +26 -0
- package/dist/esm/core/ParamContext.d.ts +16 -0
- package/dist/esm/core/ParamContext.js +16 -0
- package/dist/esm/core/QueryExecutor.d.ts +26 -0
- package/dist/esm/core/QueryExecutor.js +26 -0
- package/dist/esm/core/TransactionManager.d.ts +13 -0
- package/dist/esm/core/TransactionManager.js +13 -0
- package/dist/esm/core/UnitOfWork.d.ts +19 -0
- package/dist/esm/core/UnitOfWork.js +19 -0
- package/dist/esm/dialects/Dialect.d.ts +13 -0
- package/dist/esm/dialects/MysqlDialect.d.ts +12 -0
- package/dist/esm/dialects/MysqlDialect.js +12 -0
- package/dist/esm/dialects/PostgresDialect.d.ts +13 -0
- package/dist/esm/dialects/PostgresDialect.js +13 -0
- package/dist/esm/index.d.ts +28 -0
- package/dist/esm/index.js +28 -0
- package/dist/esm/orm/Repository.d.ts +34 -0
- package/dist/esm/orm/Repository.js +34 -0
- package/dist/esm/query/ConditionBuilder.d.ts +33 -0
- package/dist/esm/query/ConditionBuilder.js +33 -0
- package/dist/esm/query/QueryBuilder.d.ts +154 -0
- package/dist/esm/query/QueryBuilder.js +136 -0
- package/package.json +1 -1
|
@@ -1,43 +1,197 @@
|
|
|
1
1
|
import ConditionBuilder from './ConditionBuilder';
|
|
2
2
|
import QueryExecutor from '../core/QueryExecutor';
|
|
3
3
|
import { Dialect } from '../dialects/Dialect';
|
|
4
|
+
/**
|
|
5
|
+
* A fluent SQL query builder for constructing and executing database queries.
|
|
6
|
+
* @template T The expected type of the results.
|
|
7
|
+
*/
|
|
4
8
|
export default class QueryBuilder<T = any> {
|
|
5
9
|
private executor;
|
|
6
10
|
private dialect;
|
|
7
11
|
private cacheTTL?;
|
|
12
|
+
/**
|
|
13
|
+
* The fields to be selected in the query.
|
|
14
|
+
*/
|
|
8
15
|
private fields;
|
|
16
|
+
/**
|
|
17
|
+
* The join clauses for the query.
|
|
18
|
+
*/
|
|
9
19
|
private joins;
|
|
20
|
+
/**
|
|
21
|
+
* The fields to group by.
|
|
22
|
+
*/
|
|
10
23
|
private groupByFields;
|
|
24
|
+
/**
|
|
25
|
+
* The fields to order by.
|
|
26
|
+
*/
|
|
11
27
|
private orderByFields;
|
|
28
|
+
/**
|
|
29
|
+
* The maximum number of rows to return.
|
|
30
|
+
*/
|
|
12
31
|
private limitCount?;
|
|
32
|
+
/**
|
|
33
|
+
* The number of rows to skip.
|
|
34
|
+
*/
|
|
13
35
|
private offsetCount?;
|
|
36
|
+
/**
|
|
37
|
+
* Common Table Expressions (CTEs) defined for the query.
|
|
38
|
+
*/
|
|
14
39
|
private ctes;
|
|
40
|
+
/**
|
|
41
|
+
* The FROM clause of the query.
|
|
42
|
+
*/
|
|
15
43
|
private fromClause;
|
|
44
|
+
/**
|
|
45
|
+
* The parameter context for managing query parameters.
|
|
46
|
+
*/
|
|
16
47
|
private ctx;
|
|
48
|
+
/**
|
|
49
|
+
* The condition builder for WHERE clauses.
|
|
50
|
+
*/
|
|
17
51
|
private condition;
|
|
52
|
+
/**
|
|
53
|
+
* The condition builder for HAVING clauses.
|
|
54
|
+
*/
|
|
18
55
|
private havingCondition;
|
|
56
|
+
/**
|
|
57
|
+
* Creates an instance of QueryBuilder.
|
|
58
|
+
* @param table - The name of the table to query from.
|
|
59
|
+
* @param executor - The query executor to use.
|
|
60
|
+
* @param dialect - The database dialect to use.
|
|
61
|
+
* @param cacheTTL - Optional time-to-live for query caching.
|
|
62
|
+
*/
|
|
19
63
|
constructor(table: string, executor: QueryExecutor, dialect: Dialect, cacheTTL?: number | 0 | undefined);
|
|
64
|
+
/**
|
|
65
|
+
* Specifies the fields to select.
|
|
66
|
+
* @param fields - An array of field names or keys of T.
|
|
67
|
+
* @returns The current QueryBuilder instance.
|
|
68
|
+
*/
|
|
20
69
|
select(fields: (keyof T | string)[]): this;
|
|
70
|
+
/**
|
|
71
|
+
* Adds a join clause to the query.
|
|
72
|
+
* @param type - The type of join (INNER, LEFT, RIGHT).
|
|
73
|
+
* @param table - The table to join.
|
|
74
|
+
* @param localKey - The local key for the join condition.
|
|
75
|
+
* @param foreignKey - The foreign key for the join condition.
|
|
76
|
+
* @returns The current QueryBuilder instance.
|
|
77
|
+
*/
|
|
21
78
|
private addJoin;
|
|
79
|
+
/**
|
|
80
|
+
* Adds an INNER JOIN clause.
|
|
81
|
+
* @param table - The table to join.
|
|
82
|
+
* @param localKey - The local key for the join condition.
|
|
83
|
+
* @param foreignKey - The foreign key for the join condition.
|
|
84
|
+
* @returns The current QueryBuilder instance.
|
|
85
|
+
*/
|
|
22
86
|
join(table: string, localKey: string, foreignKey: string): this;
|
|
87
|
+
/**
|
|
88
|
+
* Adds a LEFT JOIN clause.
|
|
89
|
+
* @param table - The table to join.
|
|
90
|
+
* @param localKey - The local key for the join condition.
|
|
91
|
+
* @param foreignKey - The foreign key for the join condition.
|
|
92
|
+
* @returns The current QueryBuilder instance.
|
|
93
|
+
*/
|
|
23
94
|
leftJoin(table: string, localKey: string, foreignKey: string): this;
|
|
95
|
+
/**
|
|
96
|
+
* Adds a RIGHT JOIN clause.
|
|
97
|
+
* @param table - The table to join.
|
|
98
|
+
* @param localKey - The local key for the join condition.
|
|
99
|
+
* @param foreignKey - The foreign key for the join condition.
|
|
100
|
+
* @returns The current QueryBuilder instance.
|
|
101
|
+
*/
|
|
24
102
|
rightJoin(table: string, localKey: string, foreignKey: string): this;
|
|
103
|
+
/**
|
|
104
|
+
* Adds WHERE conditions based on an object.
|
|
105
|
+
* @param obj - An object where keys are column names and values are the desired values.
|
|
106
|
+
* @returns The current QueryBuilder instance.
|
|
107
|
+
*/
|
|
25
108
|
where(obj: Partial<T>): this;
|
|
109
|
+
/**
|
|
110
|
+
* Adds a raw WHERE expression.
|
|
111
|
+
* @param expression - The raw SQL expression for the WHERE clause.
|
|
112
|
+
* @returns The current QueryBuilder instance.
|
|
113
|
+
*/
|
|
26
114
|
whereRaw(expression: string): this;
|
|
115
|
+
/**
|
|
116
|
+
* Adds an AND group of conditions.
|
|
117
|
+
* @param cb - A callback function that receives a ConditionBuilder to define conditions within the group.
|
|
118
|
+
* @returns The current QueryBuilder instance.
|
|
119
|
+
*/
|
|
27
120
|
andGroup(cb: (qb: ConditionBuilder) => void): this;
|
|
121
|
+
/**
|
|
122
|
+
* Adds an OR group of conditions.
|
|
123
|
+
* @param cb - A callback function that receives a ConditionBuilder to define conditions within the group.
|
|
124
|
+
* @returns The current QueryBuilder instance.
|
|
125
|
+
*/
|
|
28
126
|
orGroup(cb: (qb: ConditionBuilder) => void): this;
|
|
127
|
+
/**
|
|
128
|
+
* Specifies fields to group by.
|
|
129
|
+
* @param fields - A single field name or an array of field names.
|
|
130
|
+
* @returns The current QueryBuilder instance.
|
|
131
|
+
*/
|
|
29
132
|
groupBy(fields: string | string[]): this;
|
|
133
|
+
/**
|
|
134
|
+
* Adds HAVING conditions based on an object.
|
|
135
|
+
* @param obj - An object where keys are column names and values are the desired values.
|
|
136
|
+
* @returns The current QueryBuilder instance.
|
|
137
|
+
*/
|
|
30
138
|
having(obj: Record<string, any>): this;
|
|
139
|
+
/**
|
|
140
|
+
* Adds a raw HAVING expression.
|
|
141
|
+
* @param expr - The raw SQL expression for the HAVING clause.
|
|
142
|
+
* @returns The current QueryBuilder instance.
|
|
143
|
+
*/
|
|
31
144
|
havingRaw(expr: string): this;
|
|
145
|
+
/**
|
|
146
|
+
* Specifies the order by clause.
|
|
147
|
+
* @param column - The column to order by.
|
|
148
|
+
* @param direction - The order direction ('ASC' or 'DESC'). Defaults to 'ASC'.
|
|
149
|
+
* @returns The current QueryBuilder instance.
|
|
150
|
+
*/
|
|
32
151
|
orderBy(column: string, direction?: 'ASC' | 'DESC'): this;
|
|
152
|
+
/**
|
|
153
|
+
* Sets the LIMIT for the query.
|
|
154
|
+
* @param value - The maximum number of rows to return.
|
|
155
|
+
* @returns The current QueryBuilder instance.
|
|
156
|
+
*/
|
|
33
157
|
limit(value: number): this;
|
|
158
|
+
/**
|
|
159
|
+
* Sets the OFFSET for the query.
|
|
160
|
+
* @param value - The number of rows to skip.
|
|
161
|
+
* @returns The current QueryBuilder instance.
|
|
162
|
+
*/
|
|
34
163
|
offset(value: number): this;
|
|
164
|
+
/**
|
|
165
|
+
* Adds a Common Table Expression (CTE) to the query.
|
|
166
|
+
* @param name - The name of the CTE.
|
|
167
|
+
* @param subQuery - The QueryBuilder instance representing the subquery for the CTE.
|
|
168
|
+
* @param recursive - Whether the CTE is recursive. Defaults to false.
|
|
169
|
+
* @returns The current QueryBuilder instance.
|
|
170
|
+
*/
|
|
35
171
|
with(name: string, subQuery: QueryBuilder<any>, recursive?: boolean): this;
|
|
172
|
+
/**
|
|
173
|
+
* Sets the FROM clause to a subquery.
|
|
174
|
+
* @param sub - The QueryBuilder instance representing the subquery.
|
|
175
|
+
* @param alias - The alias for the subquery.
|
|
176
|
+
* @returns The current QueryBuilder instance.
|
|
177
|
+
*/
|
|
36
178
|
fromSubquery(sub: QueryBuilder<any>, alias: string): this;
|
|
179
|
+
/**
|
|
180
|
+
* Creates a clone of the current QueryBuilder instance.
|
|
181
|
+
* @returns A new QueryBuilder instance with the same state.
|
|
182
|
+
*/
|
|
37
183
|
clone(): QueryBuilder<T>;
|
|
184
|
+
/**
|
|
185
|
+
* Builds the SQL query string and its parameters without executing it.
|
|
186
|
+
* @returns An object containing the SQL query string and an array of parameters.
|
|
187
|
+
*/
|
|
38
188
|
build(): {
|
|
39
189
|
query: string;
|
|
40
190
|
params: any[];
|
|
41
191
|
};
|
|
192
|
+
/**
|
|
193
|
+
* Executes the built SQL query and returns the results.
|
|
194
|
+
* @returns A Promise that resolves to an array of results of type T.
|
|
195
|
+
*/
|
|
42
196
|
execute(): Promise<T[]>;
|
|
43
197
|
}
|
|
@@ -5,54 +5,139 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const ParamContext_1 = __importDefault(require("../core/ParamContext"));
|
|
7
7
|
const ConditionBuilder_1 = __importDefault(require("./ConditionBuilder"));
|
|
8
|
+
/**
|
|
9
|
+
* A fluent SQL query builder for constructing and executing database queries.
|
|
10
|
+
* @template T The expected type of the results.
|
|
11
|
+
*/
|
|
8
12
|
class QueryBuilder {
|
|
13
|
+
/**
|
|
14
|
+
* Creates an instance of QueryBuilder.
|
|
15
|
+
* @param table - The name of the table to query from.
|
|
16
|
+
* @param executor - The query executor to use.
|
|
17
|
+
* @param dialect - The database dialect to use.
|
|
18
|
+
* @param cacheTTL - Optional time-to-live for query caching.
|
|
19
|
+
*/
|
|
9
20
|
constructor(table, executor, dialect, cacheTTL) {
|
|
10
21
|
this.executor = executor;
|
|
11
22
|
this.dialect = dialect;
|
|
12
23
|
this.cacheTTL = cacheTTL;
|
|
24
|
+
/**
|
|
25
|
+
* The fields to be selected in the query.
|
|
26
|
+
*/
|
|
13
27
|
this.fields = [];
|
|
28
|
+
/**
|
|
29
|
+
* The join clauses for the query.
|
|
30
|
+
*/
|
|
14
31
|
this.joins = [];
|
|
32
|
+
/**
|
|
33
|
+
* The fields to group by.
|
|
34
|
+
*/
|
|
15
35
|
this.groupByFields = [];
|
|
36
|
+
/**
|
|
37
|
+
* The fields to order by.
|
|
38
|
+
*/
|
|
16
39
|
this.orderByFields = [];
|
|
40
|
+
/**
|
|
41
|
+
* Common Table Expressions (CTEs) defined for the query.
|
|
42
|
+
*/
|
|
17
43
|
this.ctes = [];
|
|
18
44
|
this.fromClause = table;
|
|
19
45
|
this.ctx = new ParamContext_1.default(this.dialect);
|
|
20
46
|
this.condition = new ConditionBuilder_1.default(this.ctx);
|
|
21
47
|
this.havingCondition = new ConditionBuilder_1.default(this.ctx);
|
|
22
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* Specifies the fields to select.
|
|
51
|
+
* @param fields - An array of field names or keys of T.
|
|
52
|
+
* @returns The current QueryBuilder instance.
|
|
53
|
+
*/
|
|
23
54
|
select(fields) {
|
|
24
55
|
this.fields = fields.map(String);
|
|
25
56
|
return this;
|
|
26
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* Adds a join clause to the query.
|
|
60
|
+
* @param type - The type of join (INNER, LEFT, RIGHT).
|
|
61
|
+
* @param table - The table to join.
|
|
62
|
+
* @param localKey - The local key for the join condition.
|
|
63
|
+
* @param foreignKey - The foreign key for the join condition.
|
|
64
|
+
* @returns The current QueryBuilder instance.
|
|
65
|
+
*/
|
|
27
66
|
addJoin(type, table, localKey, foreignKey) {
|
|
28
67
|
this.joins.push(`${type} JOIN ${table} ON ${localKey} = ${foreignKey}`);
|
|
29
68
|
return this;
|
|
30
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Adds an INNER JOIN clause.
|
|
72
|
+
* @param table - The table to join.
|
|
73
|
+
* @param localKey - The local key for the join condition.
|
|
74
|
+
* @param foreignKey - The foreign key for the join condition.
|
|
75
|
+
* @returns The current QueryBuilder instance.
|
|
76
|
+
*/
|
|
31
77
|
join(table, localKey, foreignKey) {
|
|
32
78
|
return this.addJoin('INNER', table, localKey, foreignKey);
|
|
33
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Adds a LEFT JOIN clause.
|
|
82
|
+
* @param table - The table to join.
|
|
83
|
+
* @param localKey - The local key for the join condition.
|
|
84
|
+
* @param foreignKey - The foreign key for the join condition.
|
|
85
|
+
* @returns The current QueryBuilder instance.
|
|
86
|
+
*/
|
|
34
87
|
leftJoin(table, localKey, foreignKey) {
|
|
35
88
|
return this.addJoin('LEFT', table, localKey, foreignKey);
|
|
36
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* Adds a RIGHT JOIN clause.
|
|
92
|
+
* @param table - The table to join.
|
|
93
|
+
* @param localKey - The local key for the join condition.
|
|
94
|
+
* @param foreignKey - The foreign key for the join condition.
|
|
95
|
+
* @returns The current QueryBuilder instance.
|
|
96
|
+
*/
|
|
37
97
|
rightJoin(table, localKey, foreignKey) {
|
|
38
98
|
return this.addJoin('RIGHT', table, localKey, foreignKey);
|
|
39
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* Adds WHERE conditions based on an object.
|
|
102
|
+
* @param obj - An object where keys are column names and values are the desired values.
|
|
103
|
+
* @returns The current QueryBuilder instance.
|
|
104
|
+
*/
|
|
40
105
|
where(obj) {
|
|
41
106
|
this.condition.where(obj);
|
|
42
107
|
return this;
|
|
43
108
|
}
|
|
109
|
+
/**
|
|
110
|
+
* Adds a raw WHERE expression.
|
|
111
|
+
* @param expression - The raw SQL expression for the WHERE clause.
|
|
112
|
+
* @returns The current QueryBuilder instance.
|
|
113
|
+
*/
|
|
44
114
|
whereRaw(expression) {
|
|
45
115
|
this.condition.raw(expression);
|
|
46
116
|
return this;
|
|
47
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* Adds an AND group of conditions.
|
|
120
|
+
* @param cb - A callback function that receives a ConditionBuilder to define conditions within the group.
|
|
121
|
+
* @returns The current QueryBuilder instance.
|
|
122
|
+
*/
|
|
48
123
|
andGroup(cb) {
|
|
49
124
|
this.condition.andGroup(cb);
|
|
50
125
|
return this;
|
|
51
126
|
}
|
|
127
|
+
/**
|
|
128
|
+
* Adds an OR group of conditions.
|
|
129
|
+
* @param cb - A callback function that receives a ConditionBuilder to define conditions within the group.
|
|
130
|
+
* @returns The current QueryBuilder instance.
|
|
131
|
+
*/
|
|
52
132
|
orGroup(cb) {
|
|
53
133
|
this.condition.orGroup(cb);
|
|
54
134
|
return this;
|
|
55
135
|
}
|
|
136
|
+
/**
|
|
137
|
+
* Specifies fields to group by.
|
|
138
|
+
* @param fields - A single field name or an array of field names.
|
|
139
|
+
* @returns The current QueryBuilder instance.
|
|
140
|
+
*/
|
|
56
141
|
groupBy(fields) {
|
|
57
142
|
if (Array.isArray(fields)) {
|
|
58
143
|
this.groupByFields.push(...fields);
|
|
@@ -62,36 +147,79 @@ class QueryBuilder {
|
|
|
62
147
|
}
|
|
63
148
|
return this;
|
|
64
149
|
}
|
|
150
|
+
/**
|
|
151
|
+
* Adds HAVING conditions based on an object.
|
|
152
|
+
* @param obj - An object where keys are column names and values are the desired values.
|
|
153
|
+
* @returns The current QueryBuilder instance.
|
|
154
|
+
*/
|
|
65
155
|
having(obj) {
|
|
66
156
|
this.havingCondition.where(obj);
|
|
67
157
|
return this;
|
|
68
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* Adds a raw HAVING expression.
|
|
161
|
+
* @param expr - The raw SQL expression for the HAVING clause.
|
|
162
|
+
* @returns The current QueryBuilder instance.
|
|
163
|
+
*/
|
|
69
164
|
havingRaw(expr) {
|
|
70
165
|
this.havingCondition.raw(expr);
|
|
71
166
|
return this;
|
|
72
167
|
}
|
|
168
|
+
/**
|
|
169
|
+
* Specifies the order by clause.
|
|
170
|
+
* @param column - The column to order by.
|
|
171
|
+
* @param direction - The order direction ('ASC' or 'DESC'). Defaults to 'ASC'.
|
|
172
|
+
* @returns The current QueryBuilder instance.
|
|
173
|
+
*/
|
|
73
174
|
orderBy(column, direction = 'ASC') {
|
|
74
175
|
this.orderByFields.push(`${column} ${direction}`);
|
|
75
176
|
return this;
|
|
76
177
|
}
|
|
178
|
+
/**
|
|
179
|
+
* Sets the LIMIT for the query.
|
|
180
|
+
* @param value - The maximum number of rows to return.
|
|
181
|
+
* @returns The current QueryBuilder instance.
|
|
182
|
+
*/
|
|
77
183
|
limit(value) {
|
|
78
184
|
this.limitCount = value;
|
|
79
185
|
return this;
|
|
80
186
|
}
|
|
187
|
+
/**
|
|
188
|
+
* Sets the OFFSET for the query.
|
|
189
|
+
* @param value - The number of rows to skip.
|
|
190
|
+
* @returns The current QueryBuilder instance.
|
|
191
|
+
*/
|
|
81
192
|
offset(value) {
|
|
82
193
|
this.offsetCount = value;
|
|
83
194
|
return this;
|
|
84
195
|
}
|
|
196
|
+
/**
|
|
197
|
+
* Adds a Common Table Expression (CTE) to the query.
|
|
198
|
+
* @param name - The name of the CTE.
|
|
199
|
+
* @param subQuery - The QueryBuilder instance representing the subquery for the CTE.
|
|
200
|
+
* @param recursive - Whether the CTE is recursive. Defaults to false.
|
|
201
|
+
* @returns The current QueryBuilder instance.
|
|
202
|
+
*/
|
|
85
203
|
with(name, subQuery, recursive = false) {
|
|
86
204
|
this.ctes.push({ name, query: subQuery, recursive });
|
|
87
205
|
return this;
|
|
88
206
|
}
|
|
207
|
+
/**
|
|
208
|
+
* Sets the FROM clause to a subquery.
|
|
209
|
+
* @param sub - The QueryBuilder instance representing the subquery.
|
|
210
|
+
* @param alias - The alias for the subquery.
|
|
211
|
+
* @returns The current QueryBuilder instance.
|
|
212
|
+
*/
|
|
89
213
|
fromSubquery(sub, alias) {
|
|
90
214
|
const { query, params } = sub.build();
|
|
91
215
|
params.forEach(p => this.ctx.add(p));
|
|
92
216
|
this.fromClause = `(${query}) AS ${alias}`;
|
|
93
217
|
return this;
|
|
94
218
|
}
|
|
219
|
+
/**
|
|
220
|
+
* Creates a clone of the current QueryBuilder instance.
|
|
221
|
+
* @returns A new QueryBuilder instance with the same state.
|
|
222
|
+
*/
|
|
95
223
|
clone() {
|
|
96
224
|
const qb = new QueryBuilder(this.fromClause, this.executor, this.dialect, this.cacheTTL);
|
|
97
225
|
qb.fields = [...this.fields];
|
|
@@ -103,6 +231,10 @@ class QueryBuilder {
|
|
|
103
231
|
qb.ctes = [...this.ctes];
|
|
104
232
|
return qb;
|
|
105
233
|
}
|
|
234
|
+
/**
|
|
235
|
+
* Builds the SQL query string and its parameters without executing it.
|
|
236
|
+
* @returns An object containing the SQL query string and an array of parameters.
|
|
237
|
+
*/
|
|
106
238
|
build() {
|
|
107
239
|
let query = '';
|
|
108
240
|
if (this.ctes.length) {
|
|
@@ -143,6 +275,10 @@ class QueryBuilder {
|
|
|
143
275
|
params: this.ctx.getParams()
|
|
144
276
|
};
|
|
145
277
|
}
|
|
278
|
+
/**
|
|
279
|
+
* Executes the built SQL query and returns the results.
|
|
280
|
+
* @returns A Promise that resolves to an array of results of type T.
|
|
281
|
+
*/
|
|
146
282
|
async execute() {
|
|
147
283
|
const { query, params } = this.build();
|
|
148
284
|
const result = await this.executor.execute(query, params, this.cacheTTL);
|
|
@@ -1,11 +1,45 @@
|
|
|
1
1
|
import ParamContext from "../core/ParamContext";
|
|
2
|
+
/**
|
|
3
|
+
* A builder for constructing SQL WHERE and HAVING clauses.
|
|
4
|
+
* This builder is specifically designed for the `builders` directory and might have a different scope or feature set
|
|
5
|
+
* compared to other ConditionBuilder implementations in the project.
|
|
6
|
+
*/
|
|
2
7
|
export default class ConditionBuilder {
|
|
3
8
|
private ctx;
|
|
4
9
|
private conditions;
|
|
10
|
+
/**
|
|
11
|
+
* Creates an instance of ConditionBuilder.
|
|
12
|
+
* @param ctx - The ParamContext to manage query parameters.
|
|
13
|
+
*/
|
|
5
14
|
constructor(ctx: ParamContext);
|
|
15
|
+
/**
|
|
16
|
+
* Adds one or more WHERE conditions based on an object.
|
|
17
|
+
* @param obj - An object where keys are column names and values are the desired values.
|
|
18
|
+
* @returns The current ConditionBuilder instance.
|
|
19
|
+
*/
|
|
6
20
|
where(obj: Record<string, any>): this;
|
|
21
|
+
/**
|
|
22
|
+
* Adds a raw SQL expression to the conditions.
|
|
23
|
+
* @param expression - The raw SQL expression.
|
|
24
|
+
* @returns The current ConditionBuilder instance.
|
|
25
|
+
*/
|
|
7
26
|
raw(expression: string): this;
|
|
27
|
+
/**
|
|
28
|
+
* Adds a group of conditions connected by AND.
|
|
29
|
+
* @param callback - A callback function that receives a nested ConditionBuilder to define conditions within the group.
|
|
30
|
+
* @returns The current ConditionBuilder instance.
|
|
31
|
+
*/
|
|
8
32
|
andGroup(callback: (qb: ConditionBuilder) => void): this;
|
|
33
|
+
/**
|
|
34
|
+
* Adds a group of conditions connected by OR.
|
|
35
|
+
* @param callback - A callback function that receives a nested ConditionBuilder to define conditions within the group.
|
|
36
|
+
* @returns The current ConditionBuilder instance.
|
|
37
|
+
*/
|
|
9
38
|
orGroup(callback: (qb: ConditionBuilder) => void): this;
|
|
39
|
+
/**
|
|
40
|
+
* Builds the SQL condition string.
|
|
41
|
+
* @param prefix - The prefix for the condition (e.g., 'WHERE', 'HAVING'). Defaults to 'WHERE'.
|
|
42
|
+
* @returns The built SQL condition string, or an empty string if no conditions were added.
|
|
43
|
+
*/
|
|
10
44
|
build(prefix?: string): string;
|
|
11
45
|
}
|
|
@@ -1,8 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A builder for constructing SQL WHERE and HAVING clauses.
|
|
3
|
+
* This builder is specifically designed for the `builders` directory and might have a different scope or feature set
|
|
4
|
+
* compared to other ConditionBuilder implementations in the project.
|
|
5
|
+
*/
|
|
1
6
|
export default class ConditionBuilder {
|
|
7
|
+
/**
|
|
8
|
+
* Creates an instance of ConditionBuilder.
|
|
9
|
+
* @param ctx - The ParamContext to manage query parameters.
|
|
10
|
+
*/
|
|
2
11
|
constructor(ctx) {
|
|
3
12
|
this.ctx = ctx;
|
|
4
13
|
this.conditions = [];
|
|
5
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Adds one or more WHERE conditions based on an object.
|
|
17
|
+
* @param obj - An object where keys are column names and values are the desired values.
|
|
18
|
+
* @returns The current ConditionBuilder instance.
|
|
19
|
+
*/
|
|
6
20
|
where(obj) {
|
|
7
21
|
Object.entries(obj).forEach(([key, value]) => {
|
|
8
22
|
const placeholder = this.ctx.add(value);
|
|
@@ -10,10 +24,20 @@ export default class ConditionBuilder {
|
|
|
10
24
|
});
|
|
11
25
|
return this;
|
|
12
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Adds a raw SQL expression to the conditions.
|
|
29
|
+
* @param expression - The raw SQL expression.
|
|
30
|
+
* @returns The current ConditionBuilder instance.
|
|
31
|
+
*/
|
|
13
32
|
raw(expression) {
|
|
14
33
|
this.conditions.push(expression);
|
|
15
34
|
return this;
|
|
16
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Adds a group of conditions connected by AND.
|
|
38
|
+
* @param callback - A callback function that receives a nested ConditionBuilder to define conditions within the group.
|
|
39
|
+
* @returns The current ConditionBuilder instance.
|
|
40
|
+
*/
|
|
17
41
|
andGroup(callback) {
|
|
18
42
|
const nested = new ConditionBuilder(this.ctx);
|
|
19
43
|
callback(nested);
|
|
@@ -23,6 +47,11 @@ export default class ConditionBuilder {
|
|
|
23
47
|
}
|
|
24
48
|
return this;
|
|
25
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* Adds a group of conditions connected by OR.
|
|
52
|
+
* @param callback - A callback function that receives a nested ConditionBuilder to define conditions within the group.
|
|
53
|
+
* @returns The current ConditionBuilder instance.
|
|
54
|
+
*/
|
|
26
55
|
orGroup(callback) {
|
|
27
56
|
const nested = new ConditionBuilder(this.ctx);
|
|
28
57
|
callback(nested);
|
|
@@ -32,6 +61,11 @@ export default class ConditionBuilder {
|
|
|
32
61
|
}
|
|
33
62
|
return this;
|
|
34
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* Builds the SQL condition string.
|
|
66
|
+
* @param prefix - The prefix for the condition (e.g., 'WHERE', 'HAVING'). Defaults to 'WHERE'.
|
|
67
|
+
* @returns The built SQL condition string, or an empty string if no conditions were added.
|
|
68
|
+
*/
|
|
35
69
|
build(prefix = 'WHERE') {
|
|
36
70
|
if (!this.conditions.length)
|
|
37
71
|
return '';
|
|
@@ -1,31 +1,142 @@
|
|
|
1
1
|
import QueryExecutor from '../core/QueryExecutor';
|
|
2
2
|
import { Dialect } from "../dialects/Dialect";
|
|
3
|
+
/**
|
|
4
|
+
* A fluent SQL query builder for constructing and executing database queries.
|
|
5
|
+
* This builder is specifically designed for the `builders` directory and might have a different scope or feature set
|
|
6
|
+
* compared to other QueryBuilder implementations in the project.
|
|
7
|
+
* @template T The expected type of the results.
|
|
8
|
+
*/
|
|
3
9
|
export default class QueryBuilder<T = any> {
|
|
4
10
|
private executor;
|
|
11
|
+
/**
|
|
12
|
+
* The fields to be selected in the query.
|
|
13
|
+
*/
|
|
5
14
|
private fields;
|
|
15
|
+
/**
|
|
16
|
+
* The join clauses for the query.
|
|
17
|
+
*/
|
|
6
18
|
private joins;
|
|
19
|
+
/**
|
|
20
|
+
* The fields to group by.
|
|
21
|
+
*/
|
|
7
22
|
private groupByFields;
|
|
23
|
+
/**
|
|
24
|
+
* The fields to order by.
|
|
25
|
+
*/
|
|
8
26
|
private orderByFields;
|
|
27
|
+
/**
|
|
28
|
+
* The maximum number of rows to return.
|
|
29
|
+
*/
|
|
9
30
|
private limitCount?;
|
|
31
|
+
/**
|
|
32
|
+
* The number of rows to skip.
|
|
33
|
+
*/
|
|
10
34
|
private offsetCount?;
|
|
35
|
+
/**
|
|
36
|
+
* Common Table Expressions (CTEs) defined for the query.
|
|
37
|
+
*/
|
|
11
38
|
private ctes;
|
|
39
|
+
/**
|
|
40
|
+
* The FROM clause of the query.
|
|
41
|
+
*/
|
|
12
42
|
private fromClause;
|
|
43
|
+
/**
|
|
44
|
+
* The condition builder for WHERE clauses.
|
|
45
|
+
*/
|
|
13
46
|
private condition;
|
|
47
|
+
/**
|
|
48
|
+
* The condition builder for HAVING clauses.
|
|
49
|
+
*/
|
|
14
50
|
private havingCondition;
|
|
51
|
+
/**
|
|
52
|
+
* The parameter context for managing query parameters.
|
|
53
|
+
*/
|
|
15
54
|
private ctx;
|
|
55
|
+
/**
|
|
56
|
+
* Creates an instance of QueryBuilder.
|
|
57
|
+
* @param table - The name of the table to query from.
|
|
58
|
+
* @param executor - The query executor to use.
|
|
59
|
+
* @param dialect - The database dialect to use.
|
|
60
|
+
*/
|
|
16
61
|
constructor(table: string, executor: QueryExecutor, dialect: Dialect);
|
|
62
|
+
/**
|
|
63
|
+
* Specifies the fields to select.
|
|
64
|
+
* @param fields - An array of field names.
|
|
65
|
+
* @returns The current QueryBuilder instance.
|
|
66
|
+
*/
|
|
17
67
|
select(fields: string[]): this;
|
|
68
|
+
/**
|
|
69
|
+
* Adds a join clause to the query.
|
|
70
|
+
* @param type - The type of join (INNER, LEFT, RIGHT).
|
|
71
|
+
* @param table - The table to join.
|
|
72
|
+
* @param localKey - The local key for the join condition.
|
|
73
|
+
* @param foreignKey - The foreign key for the join condition.
|
|
74
|
+
* @returns The current QueryBuilder instance.
|
|
75
|
+
*/
|
|
18
76
|
private addJoin;
|
|
77
|
+
/**
|
|
78
|
+
* Adds an INNER JOIN clause.
|
|
79
|
+
* @param table - The table to join.
|
|
80
|
+
* @param localKey - The local key for the join condition.
|
|
81
|
+
* @param foreignKey - The foreign key for the join condition.
|
|
82
|
+
* @returns The current QueryBuilder instance.
|
|
83
|
+
*/
|
|
19
84
|
join(table: string, localKey: string, foreignKey: string): this;
|
|
85
|
+
/**
|
|
86
|
+
* Adds a LEFT JOIN clause.
|
|
87
|
+
* @param table - The table to join.
|
|
88
|
+
* @param localKey - The local key for the join condition.
|
|
89
|
+
* @param foreignKey - The foreign key for the join condition.
|
|
90
|
+
* @returns The current QueryBuilder instance.
|
|
91
|
+
*/
|
|
20
92
|
leftJoin(table: string, localKey: string, foreignKey: string): this;
|
|
93
|
+
/**
|
|
94
|
+
* Adds a RIGHT JOIN clause.
|
|
95
|
+
* @param table - The table to join.
|
|
96
|
+
* @param localKey - The local key for the join condition.
|
|
97
|
+
* @param foreignKey - The foreign key for the join condition.
|
|
98
|
+
* @returns The current QueryBuilder instance.
|
|
99
|
+
*/
|
|
21
100
|
rightJoin(table: string, localKey: string, foreignKey: string): this;
|
|
101
|
+
/**
|
|
102
|
+
* Sets the FROM clause to a subquery.
|
|
103
|
+
* @param sub - The QueryBuilder instance representing the subquery.
|
|
104
|
+
* @param alias - The alias for the subquery.
|
|
105
|
+
* @returns The current QueryBuilder instance.
|
|
106
|
+
*/
|
|
22
107
|
fromSubquery(sub: QueryBuilder, alias: string): this;
|
|
108
|
+
/**
|
|
109
|
+
* Adds WHERE conditions based on an object.
|
|
110
|
+
* @param obj - An object where keys are column names and values are the desired values.
|
|
111
|
+
* @returns The current QueryBuilder instance.
|
|
112
|
+
*/
|
|
23
113
|
where(obj: Record<string, any>): this;
|
|
114
|
+
/**
|
|
115
|
+
* Adds a WHERE condition with a subquery.
|
|
116
|
+
* @param column - The column to apply the condition to.
|
|
117
|
+
* @param operator - The operator ('IN' or 'NOT IN').
|
|
118
|
+
* @param sub - The QueryBuilder instance representing the subquery.
|
|
119
|
+
* @returns The current QueryBuilder instance.
|
|
120
|
+
*/
|
|
24
121
|
whereSub(column: string, operator: 'IN' | 'NOT IN', sub: QueryBuilder): this;
|
|
122
|
+
/**
|
|
123
|
+
* Sets the LIMIT for the query.
|
|
124
|
+
* @param limit - The maximum number of rows to return.
|
|
125
|
+
* @returns The current QueryBuilder instance.
|
|
126
|
+
*/
|
|
25
127
|
limit(limit: number): this;
|
|
128
|
+
/**
|
|
129
|
+
* Builds the SQL query string and its parameters without executing it.
|
|
130
|
+
* @returns An object containing the SQL query string and an array of parameters.
|
|
131
|
+
*/
|
|
26
132
|
build(): {
|
|
27
133
|
query: string;
|
|
28
134
|
params: any[];
|
|
29
135
|
};
|
|
30
|
-
|
|
136
|
+
/**
|
|
137
|
+
* Executes the built SQL query and returns the results.
|
|
138
|
+
* @returns A Promise that resolves to an array of results of type T.
|
|
139
|
+
* @throws Error if no QueryExecutor is provided.
|
|
140
|
+
*/
|
|
141
|
+
execute(): Promise<T[]>;
|
|
31
142
|
}
|