-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysql-executer.ts
87 lines (79 loc) · 3.05 KB
/
mysql-executer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { Connection, Pool, PoolConnection } from 'mysql2/promise';
import { Executer, SelectResultType, ParameterType, InsertResultType, MutateResultType } from '../';
/**
* An Executer for executing MySQL queries.
*/
export class MySQLExecuter implements Executer {
/**
* Initialize the Executer instance.
* @param pool - A MySQL connection instance (pool or single connection)
*/
constructor(
public pool: Connection | Pool | PoolConnection) {
}
/**
* Execute a select query.
* @param query - The SQL to execute.
* @param params - An object containing query parameters for the query. Each
* parameter will be preceded with a colon in query.
* @return A promise that is resolved with an array of query rows. Each row
* should have key-value pairs, where the keys are the selected columns.
*/
select(query: string, params: ParameterType): Promise<SelectResultType> {
return this.pool
.query(query, params)
.then(([results]) => results as SelectResultType);
}
/**
* Execute an insert query.
* @param query - The SQL to execute.
* @param params - An object containing query parameters for the query. Each
* parameter will be preceded with a colon in query.
* @return A promise that will be resolved with an object describing the
* result of the insert. An insertId should be present on the object if
* available (e.g. if the insert inolved a generated column).
*/
insert(query: string, params: ParameterType): Promise<InsertResultType> {
return this.pool
.query(query, params)
.then(([result]) => result as InsertResultType);
}
/**
* Execute an update query.
* @param query - The SQL to execute.
* @param params - An object containing query parameters for the query. Each
* parameter will be preceded with a colon in query.
* @return An object that has an affectedRows property indicating the number
* of rows affected (changed) by the query.
*/
update(query: string, params: ParameterType): Promise<MutateResultType> {
return this.pool
.query(query, params)
.then(([result]) => result as MutateResultType);
}
/**
* Execute a delete query.
* @param query - The SQL to execute.
* @param params - An object containing query parameters for the query. Each
* parameter will be preceded with a colon in query.
* @return An object that has an affectedRows property indicating the number
* of rows affected (changed) by the query.
*/
delete(query: string, params: ParameterType): Promise<MutateResultType> {
return this.pool
.query(query, params)
.then(([result]) => result as MutateResultType);
}
/**
* Execute a raw query.
* @param query - The SQL to execute.
* @param params - An object containing query parameters for the query. Each
* parameter will be preceded with a colon in query.
* @return A promise that shall be resolved if the query succeeds, or
* rejected otherwise.
*/
query(query: string, params: ParameterType): Promise<any> {
return this.pool
.query(query, params);
}
}