pg-query-sdk 1.0.0 → 1.0.2
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 +99 -370
- 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,13 +1,47 @@
|
|
|
1
1
|
import { QueryBuilder, QueryExecutor } from "../index";
|
|
2
2
|
import { Dialect } from "../dialects/Dialect";
|
|
3
|
+
/**
|
|
4
|
+
* Abstract base class for repositories, providing common database operations.
|
|
5
|
+
* @template T The type of the entity managed by the repository.
|
|
6
|
+
*/
|
|
3
7
|
export default abstract class Repository<T> {
|
|
4
8
|
protected table: string;
|
|
5
9
|
protected executor: QueryExecutor;
|
|
6
10
|
protected dialect: Dialect;
|
|
11
|
+
/**
|
|
12
|
+
* Creates an instance of Repository.
|
|
13
|
+
* @param table - The name of the database table associated with this repository.
|
|
14
|
+
* @param executor - The QueryExecutor instance for executing queries.
|
|
15
|
+
* @param dialect - The Dialect instance for database-specific syntax.
|
|
16
|
+
*/
|
|
7
17
|
constructor(table: string, executor: QueryExecutor, dialect: Dialect);
|
|
18
|
+
/**
|
|
19
|
+
* Returns a new QueryBuilder instance for the repository's table.
|
|
20
|
+
* @returns A QueryBuilder instance.
|
|
21
|
+
*/
|
|
8
22
|
qb(): QueryBuilder<T>;
|
|
23
|
+
/**
|
|
24
|
+
* Finds an entity by its ID.
|
|
25
|
+
* @param id - The ID of the entity to find.
|
|
26
|
+
* @returns A Promise that resolves to the found entity or null if not found.
|
|
27
|
+
*/
|
|
9
28
|
findById(id: number): Promise<T | null>;
|
|
29
|
+
/**
|
|
30
|
+
* Inserts a new entity into the database.
|
|
31
|
+
* This method should be implemented by concrete repository classes.
|
|
32
|
+
* @param data - The partial entity data to insert.
|
|
33
|
+
*/
|
|
10
34
|
insert(data: Partial<T>): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Updates an existing entity in the database.
|
|
37
|
+
* This method should be implemented by concrete repository classes.
|
|
38
|
+
* @param data - The partial entity data to update.
|
|
39
|
+
*/
|
|
11
40
|
update(data: Partial<T>): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Deletes an entity from the database.
|
|
43
|
+
* This method should be implemented by concrete repository classes.
|
|
44
|
+
* @param data - The partial entity data to delete (e.g., containing the ID).
|
|
45
|
+
*/
|
|
12
46
|
delete(data: Partial<T>): Promise<void>;
|
|
13
47
|
}
|
|
@@ -1,13 +1,32 @@
|
|
|
1
1
|
import { QueryBuilder } from "../index";
|
|
2
|
+
/**
|
|
3
|
+
* Abstract base class for repositories, providing common database operations.
|
|
4
|
+
* @template T The type of the entity managed by the repository.
|
|
5
|
+
*/
|
|
2
6
|
export default class Repository {
|
|
7
|
+
/**
|
|
8
|
+
* Creates an instance of Repository.
|
|
9
|
+
* @param table - The name of the database table associated with this repository.
|
|
10
|
+
* @param executor - The QueryExecutor instance for executing queries.
|
|
11
|
+
* @param dialect - The Dialect instance for database-specific syntax.
|
|
12
|
+
*/
|
|
3
13
|
constructor(table, executor, dialect) {
|
|
4
14
|
this.table = table;
|
|
5
15
|
this.executor = executor;
|
|
6
16
|
this.dialect = dialect;
|
|
7
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Returns a new QueryBuilder instance for the repository's table.
|
|
20
|
+
* @returns A QueryBuilder instance.
|
|
21
|
+
*/
|
|
8
22
|
qb() {
|
|
9
23
|
return new QueryBuilder(this.table, this.executor, this.dialect);
|
|
10
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Finds an entity by its ID.
|
|
27
|
+
* @param id - The ID of the entity to find.
|
|
28
|
+
* @returns A Promise that resolves to the found entity or null if not found.
|
|
29
|
+
*/
|
|
11
30
|
async findById(id) {
|
|
12
31
|
const rows = await this.qb()
|
|
13
32
|
.where({ id })
|
|
@@ -15,12 +34,27 @@ export default class Repository {
|
|
|
15
34
|
.execute();
|
|
16
35
|
return rows[0] ?? null;
|
|
17
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Inserts a new entity into the database.
|
|
39
|
+
* This method should be implemented by concrete repository classes.
|
|
40
|
+
* @param data - The partial entity data to insert.
|
|
41
|
+
*/
|
|
18
42
|
async insert(data) {
|
|
19
43
|
// implementação insert segura
|
|
20
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Updates an existing entity in the database.
|
|
47
|
+
* This method should be implemented by concrete repository classes.
|
|
48
|
+
* @param data - The partial entity data to update.
|
|
49
|
+
*/
|
|
21
50
|
async update(data) {
|
|
22
51
|
// implementação update segura
|
|
23
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Deletes an entity from the database.
|
|
55
|
+
* This method should be implemented by concrete repository classes.
|
|
56
|
+
* @param data - The partial entity data to delete (e.g., containing the ID).
|
|
57
|
+
*/
|
|
24
58
|
async delete(data) {
|
|
25
59
|
// soft delete opcional
|
|
26
60
|
}
|
|
@@ -1,11 +1,44 @@
|
|
|
1
1
|
import ParamContext from '../core/ParamContext';
|
|
2
|
+
/**
|
|
3
|
+
* A builder for constructing SQL WHERE and HAVING clauses.
|
|
4
|
+
*/
|
|
2
5
|
export default class ConditionBuilder {
|
|
3
6
|
private ctx;
|
|
4
7
|
private parts;
|
|
8
|
+
/**
|
|
9
|
+
* Creates an instance of ConditionBuilder.
|
|
10
|
+
* @param ctx - The ParamContext to manage query parameters.
|
|
11
|
+
*/
|
|
5
12
|
constructor(ctx: ParamContext);
|
|
13
|
+
/**
|
|
14
|
+
* Adds one or more WHERE conditions based on an object.
|
|
15
|
+
* Supports direct equality, `null` checks, and custom operators.
|
|
16
|
+
* @param obj - An object where keys are column names and values are the desired values or an object with `op` and `value`.
|
|
17
|
+
* @returns The current ConditionBuilder instance.
|
|
18
|
+
*/
|
|
6
19
|
where(obj: Record<string, any>): this;
|
|
20
|
+
/**
|
|
21
|
+
* Adds a raw SQL expression to the conditions.
|
|
22
|
+
* @param expression - The raw SQL expression.
|
|
23
|
+
* @returns The current ConditionBuilder instance.
|
|
24
|
+
*/
|
|
7
25
|
raw(expression: string): this;
|
|
26
|
+
/**
|
|
27
|
+
* Adds a group of conditions connected by AND.
|
|
28
|
+
* @param cb - A callback function that receives a nested ConditionBuilder to define conditions within the group.
|
|
29
|
+
* @returns The current ConditionBuilder instance.
|
|
30
|
+
*/
|
|
8
31
|
andGroup(cb: (qb: ConditionBuilder) => void): this;
|
|
32
|
+
/**
|
|
33
|
+
* Adds a group of conditions connected by OR.
|
|
34
|
+
* @param cb - A callback function that receives a nested ConditionBuilder to define conditions within the group.
|
|
35
|
+
* @returns The current ConditionBuilder instance.
|
|
36
|
+
*/
|
|
9
37
|
orGroup(cb: (qb: ConditionBuilder) => void): this;
|
|
38
|
+
/**
|
|
39
|
+
* Builds the SQL condition string.
|
|
40
|
+
* @param prefix - The prefix for the condition (e.g., 'WHERE', 'HAVING'). Defaults to 'WHERE'.
|
|
41
|
+
* @returns The built SQL condition string, or an empty string if no conditions were added.
|
|
42
|
+
*/
|
|
10
43
|
build(prefix?: string): string;
|
|
11
44
|
}
|
|
@@ -1,8 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A builder for constructing SQL WHERE and HAVING clauses.
|
|
3
|
+
*/
|
|
1
4
|
export default class ConditionBuilder {
|
|
5
|
+
/**
|
|
6
|
+
* Creates an instance of ConditionBuilder.
|
|
7
|
+
* @param ctx - The ParamContext to manage query parameters.
|
|
8
|
+
*/
|
|
2
9
|
constructor(ctx) {
|
|
3
10
|
this.ctx = ctx;
|
|
4
11
|
this.parts = [];
|
|
5
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* Adds one or more WHERE conditions based on an object.
|
|
15
|
+
* Supports direct equality, `null` checks, and custom operators.
|
|
16
|
+
* @param obj - An object where keys are column names and values are the desired values or an object with `op` and `value`.
|
|
17
|
+
* @returns The current ConditionBuilder instance.
|
|
18
|
+
*/
|
|
6
19
|
where(obj) {
|
|
7
20
|
Object.entries(obj).forEach(([key, value]) => {
|
|
8
21
|
if (value === null) {
|
|
@@ -20,10 +33,20 @@ export default class ConditionBuilder {
|
|
|
20
33
|
});
|
|
21
34
|
return this;
|
|
22
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Adds a raw SQL expression to the conditions.
|
|
38
|
+
* @param expression - The raw SQL expression.
|
|
39
|
+
* @returns The current ConditionBuilder instance.
|
|
40
|
+
*/
|
|
23
41
|
raw(expression) {
|
|
24
42
|
this.parts.push(expression);
|
|
25
43
|
return this;
|
|
26
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Adds a group of conditions connected by AND.
|
|
47
|
+
* @param cb - A callback function that receives a nested ConditionBuilder to define conditions within the group.
|
|
48
|
+
* @returns The current ConditionBuilder instance.
|
|
49
|
+
*/
|
|
27
50
|
andGroup(cb) {
|
|
28
51
|
const nested = new ConditionBuilder(this.ctx);
|
|
29
52
|
cb(nested);
|
|
@@ -33,6 +56,11 @@ export default class ConditionBuilder {
|
|
|
33
56
|
}
|
|
34
57
|
return this;
|
|
35
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Adds a group of conditions connected by OR.
|
|
61
|
+
* @param cb - A callback function that receives a nested ConditionBuilder to define conditions within the group.
|
|
62
|
+
* @returns The current ConditionBuilder instance.
|
|
63
|
+
*/
|
|
36
64
|
orGroup(cb) {
|
|
37
65
|
const nested = new ConditionBuilder(this.ctx);
|
|
38
66
|
cb(nested);
|
|
@@ -43,6 +71,11 @@ export default class ConditionBuilder {
|
|
|
43
71
|
}
|
|
44
72
|
return this;
|
|
45
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* Builds the SQL condition string.
|
|
76
|
+
* @param prefix - The prefix for the condition (e.g., 'WHERE', 'HAVING'). Defaults to 'WHERE'.
|
|
77
|
+
* @returns The built SQL condition string, or an empty string if no conditions were added.
|
|
78
|
+
*/
|
|
46
79
|
build(prefix = 'WHERE') {
|
|
47
80
|
if (!this.parts.length)
|
|
48
81
|
return '';
|
|
@@ -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
|
}
|
|
@@ -1,53 +1,138 @@
|
|
|
1
1
|
import ParamContext from '../core/ParamContext';
|
|
2
2
|
import ConditionBuilder from './ConditionBuilder';
|
|
3
|
+
/**
|
|
4
|
+
* A fluent SQL query builder for constructing and executing database queries.
|
|
5
|
+
* @template T The expected type of the results.
|
|
6
|
+
*/
|
|
3
7
|
export default class QueryBuilder {
|
|
8
|
+
/**
|
|
9
|
+
* Creates an instance of QueryBuilder.
|
|
10
|
+
* @param table - The name of the table to query from.
|
|
11
|
+
* @param executor - The query executor to use.
|
|
12
|
+
* @param dialect - The database dialect to use.
|
|
13
|
+
* @param cacheTTL - Optional time-to-live for query caching.
|
|
14
|
+
*/
|
|
4
15
|
constructor(table, executor, dialect, cacheTTL) {
|
|
5
16
|
this.executor = executor;
|
|
6
17
|
this.dialect = dialect;
|
|
7
18
|
this.cacheTTL = cacheTTL;
|
|
19
|
+
/**
|
|
20
|
+
* The fields to be selected in the query.
|
|
21
|
+
*/
|
|
8
22
|
this.fields = [];
|
|
23
|
+
/**
|
|
24
|
+
* The join clauses for the query.
|
|
25
|
+
*/
|
|
9
26
|
this.joins = [];
|
|
27
|
+
/**
|
|
28
|
+
* The fields to group by.
|
|
29
|
+
*/
|
|
10
30
|
this.groupByFields = [];
|
|
31
|
+
/**
|
|
32
|
+
* The fields to order by.
|
|
33
|
+
*/
|
|
11
34
|
this.orderByFields = [];
|
|
35
|
+
/**
|
|
36
|
+
* Common Table Expressions (CTEs) defined for the query.
|
|
37
|
+
*/
|
|
12
38
|
this.ctes = [];
|
|
13
39
|
this.fromClause = table;
|
|
14
40
|
this.ctx = new ParamContext(this.dialect);
|
|
15
41
|
this.condition = new ConditionBuilder(this.ctx);
|
|
16
42
|
this.havingCondition = new ConditionBuilder(this.ctx);
|
|
17
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Specifies the fields to select.
|
|
46
|
+
* @param fields - An array of field names or keys of T.
|
|
47
|
+
* @returns The current QueryBuilder instance.
|
|
48
|
+
*/
|
|
18
49
|
select(fields) {
|
|
19
50
|
this.fields = fields.map(String);
|
|
20
51
|
return this;
|
|
21
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Adds a join clause to the query.
|
|
55
|
+
* @param type - The type of join (INNER, LEFT, RIGHT).
|
|
56
|
+
* @param table - The table to join.
|
|
57
|
+
* @param localKey - The local key for the join condition.
|
|
58
|
+
* @param foreignKey - The foreign key for the join condition.
|
|
59
|
+
* @returns The current QueryBuilder instance.
|
|
60
|
+
*/
|
|
22
61
|
addJoin(type, table, localKey, foreignKey) {
|
|
23
62
|
this.joins.push(`${type} JOIN ${table} ON ${localKey} = ${foreignKey}`);
|
|
24
63
|
return this;
|
|
25
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* Adds an INNER JOIN clause.
|
|
67
|
+
* @param table - The table to join.
|
|
68
|
+
* @param localKey - The local key for the join condition.
|
|
69
|
+
* @param foreignKey - The foreign key for the join condition.
|
|
70
|
+
* @returns The current QueryBuilder instance.
|
|
71
|
+
*/
|
|
26
72
|
join(table, localKey, foreignKey) {
|
|
27
73
|
return this.addJoin('INNER', table, localKey, foreignKey);
|
|
28
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* Adds a LEFT JOIN clause.
|
|
77
|
+
* @param table - The table to join.
|
|
78
|
+
* @param localKey - The local key for the join condition.
|
|
79
|
+
* @param foreignKey - The foreign key for the join condition.
|
|
80
|
+
* @returns The current QueryBuilder instance.
|
|
81
|
+
*/
|
|
29
82
|
leftJoin(table, localKey, foreignKey) {
|
|
30
83
|
return this.addJoin('LEFT', table, localKey, foreignKey);
|
|
31
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* Adds a RIGHT 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
|
+
*/
|
|
32
92
|
rightJoin(table, localKey, foreignKey) {
|
|
33
93
|
return this.addJoin('RIGHT', table, localKey, foreignKey);
|
|
34
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* Adds WHERE conditions based on an object.
|
|
97
|
+
* @param obj - An object where keys are column names and values are the desired values.
|
|
98
|
+
* @returns The current QueryBuilder instance.
|
|
99
|
+
*/
|
|
35
100
|
where(obj) {
|
|
36
101
|
this.condition.where(obj);
|
|
37
102
|
return this;
|
|
38
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Adds a raw WHERE expression.
|
|
106
|
+
* @param expression - The raw SQL expression for the WHERE clause.
|
|
107
|
+
* @returns The current QueryBuilder instance.
|
|
108
|
+
*/
|
|
39
109
|
whereRaw(expression) {
|
|
40
110
|
this.condition.raw(expression);
|
|
41
111
|
return this;
|
|
42
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* Adds an AND group of conditions.
|
|
115
|
+
* @param cb - A callback function that receives a ConditionBuilder to define conditions within the group.
|
|
116
|
+
* @returns The current QueryBuilder instance.
|
|
117
|
+
*/
|
|
43
118
|
andGroup(cb) {
|
|
44
119
|
this.condition.andGroup(cb);
|
|
45
120
|
return this;
|
|
46
121
|
}
|
|
122
|
+
/**
|
|
123
|
+
* Adds an OR group of conditions.
|
|
124
|
+
* @param cb - A callback function that receives a ConditionBuilder to define conditions within the group.
|
|
125
|
+
* @returns The current QueryBuilder instance.
|
|
126
|
+
*/
|
|
47
127
|
orGroup(cb) {
|
|
48
128
|
this.condition.orGroup(cb);
|
|
49
129
|
return this;
|
|
50
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* Specifies fields to group by.
|
|
133
|
+
* @param fields - A single field name or an array of field names.
|
|
134
|
+
* @returns The current QueryBuilder instance.
|
|
135
|
+
*/
|
|
51
136
|
groupBy(fields) {
|
|
52
137
|
if (Array.isArray(fields)) {
|
|
53
138
|
this.groupByFields.push(...fields);
|
|
@@ -57,36 +142,79 @@ export default class QueryBuilder {
|
|
|
57
142
|
}
|
|
58
143
|
return this;
|
|
59
144
|
}
|
|
145
|
+
/**
|
|
146
|
+
* Adds HAVING conditions based on an object.
|
|
147
|
+
* @param obj - An object where keys are column names and values are the desired values.
|
|
148
|
+
* @returns The current QueryBuilder instance.
|
|
149
|
+
*/
|
|
60
150
|
having(obj) {
|
|
61
151
|
this.havingCondition.where(obj);
|
|
62
152
|
return this;
|
|
63
153
|
}
|
|
154
|
+
/**
|
|
155
|
+
* Adds a raw HAVING expression.
|
|
156
|
+
* @param expr - The raw SQL expression for the HAVING clause.
|
|
157
|
+
* @returns The current QueryBuilder instance.
|
|
158
|
+
*/
|
|
64
159
|
havingRaw(expr) {
|
|
65
160
|
this.havingCondition.raw(expr);
|
|
66
161
|
return this;
|
|
67
162
|
}
|
|
163
|
+
/**
|
|
164
|
+
* Specifies the order by clause.
|
|
165
|
+
* @param column - The column to order by.
|
|
166
|
+
* @param direction - The order direction ('ASC' or 'DESC'). Defaults to 'ASC'.
|
|
167
|
+
* @returns The current QueryBuilder instance.
|
|
168
|
+
*/
|
|
68
169
|
orderBy(column, direction = 'ASC') {
|
|
69
170
|
this.orderByFields.push(`${column} ${direction}`);
|
|
70
171
|
return this;
|
|
71
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* Sets the LIMIT for the query.
|
|
175
|
+
* @param value - The maximum number of rows to return.
|
|
176
|
+
* @returns The current QueryBuilder instance.
|
|
177
|
+
*/
|
|
72
178
|
limit(value) {
|
|
73
179
|
this.limitCount = value;
|
|
74
180
|
return this;
|
|
75
181
|
}
|
|
182
|
+
/**
|
|
183
|
+
* Sets the OFFSET for the query.
|
|
184
|
+
* @param value - The number of rows to skip.
|
|
185
|
+
* @returns The current QueryBuilder instance.
|
|
186
|
+
*/
|
|
76
187
|
offset(value) {
|
|
77
188
|
this.offsetCount = value;
|
|
78
189
|
return this;
|
|
79
190
|
}
|
|
191
|
+
/**
|
|
192
|
+
* Adds a Common Table Expression (CTE) to the query.
|
|
193
|
+
* @param name - The name of the CTE.
|
|
194
|
+
* @param subQuery - The QueryBuilder instance representing the subquery for the CTE.
|
|
195
|
+
* @param recursive - Whether the CTE is recursive. Defaults to false.
|
|
196
|
+
* @returns The current QueryBuilder instance.
|
|
197
|
+
*/
|
|
80
198
|
with(name, subQuery, recursive = false) {
|
|
81
199
|
this.ctes.push({ name, query: subQuery, recursive });
|
|
82
200
|
return this;
|
|
83
201
|
}
|
|
202
|
+
/**
|
|
203
|
+
* Sets the FROM clause to a subquery.
|
|
204
|
+
* @param sub - The QueryBuilder instance representing the subquery.
|
|
205
|
+
* @param alias - The alias for the subquery.
|
|
206
|
+
* @returns The current QueryBuilder instance.
|
|
207
|
+
*/
|
|
84
208
|
fromSubquery(sub, alias) {
|
|
85
209
|
const { query, params } = sub.build();
|
|
86
210
|
params.forEach(p => this.ctx.add(p));
|
|
87
211
|
this.fromClause = `(${query}) AS ${alias}`;
|
|
88
212
|
return this;
|
|
89
213
|
}
|
|
214
|
+
/**
|
|
215
|
+
* Creates a clone of the current QueryBuilder instance.
|
|
216
|
+
* @returns A new QueryBuilder instance with the same state.
|
|
217
|
+
*/
|
|
90
218
|
clone() {
|
|
91
219
|
const qb = new QueryBuilder(this.fromClause, this.executor, this.dialect, this.cacheTTL);
|
|
92
220
|
qb.fields = [...this.fields];
|
|
@@ -98,6 +226,10 @@ export default class QueryBuilder {
|
|
|
98
226
|
qb.ctes = [...this.ctes];
|
|
99
227
|
return qb;
|
|
100
228
|
}
|
|
229
|
+
/**
|
|
230
|
+
* Builds the SQL query string and its parameters without executing it.
|
|
231
|
+
* @returns An object containing the SQL query string and an array of parameters.
|
|
232
|
+
*/
|
|
101
233
|
build() {
|
|
102
234
|
let query = '';
|
|
103
235
|
if (this.ctes.length) {
|
|
@@ -138,6 +270,10 @@ export default class QueryBuilder {
|
|
|
138
270
|
params: this.ctx.getParams()
|
|
139
271
|
};
|
|
140
272
|
}
|
|
273
|
+
/**
|
|
274
|
+
* Executes the built SQL query and returns the results.
|
|
275
|
+
* @returns A Promise that resolves to an array of results of type T.
|
|
276
|
+
*/
|
|
141
277
|
async execute() {
|
|
142
278
|
const { query, params } = this.build();
|
|
143
279
|
const result = await this.executor.execute(query, params, this.cacheTTL);
|