@stashfin/mysql2 1.3.1 → 1.3.3
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/index.d.ts +3 -1
- package/index.js +16 -5
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import mysql, { RowDataPacket, PoolOptions, ResultSetHeader } from 'mysql2';
|
|
2
|
+
import { Connection } from 'mysql2/typings/mysql/lib/Connection';
|
|
2
3
|
export interface MysqlConnectorOptions extends PoolOptions {
|
|
3
4
|
}
|
|
4
5
|
export declare class MysqlConnector {
|
|
@@ -6,7 +7,8 @@ export declare class MysqlConnector {
|
|
|
6
7
|
private credentials;
|
|
7
8
|
constructor(credentials: PoolOptions);
|
|
8
9
|
/** A random method to simulate a step before to get the class methods */
|
|
9
|
-
|
|
10
|
+
getPoolConnection(): mysql.Pool;
|
|
11
|
+
getConnection(): Promise<Connection>;
|
|
10
12
|
queryRow<T extends RowDataPacket>(sql: string, values?: Array<string | number>): Promise<T | undefined>;
|
|
11
13
|
queryRows<T extends RowDataPacket>(sql: string, values?: Array<string | number>): Promise<T[]>;
|
|
12
14
|
queryArray<T extends RowDataPacket>(sql: string, values?: Array<string | number>): Promise<T[]>;
|
package/index.js
CHANGED
|
@@ -14,13 +14,24 @@ class MysqlConnector {
|
|
|
14
14
|
this.pool = mysql2_1.default.createPool(this.credentials);
|
|
15
15
|
}
|
|
16
16
|
/** A random method to simulate a step before to get the class methods */
|
|
17
|
-
|
|
17
|
+
getPoolConnection() {
|
|
18
18
|
if (!this?.pool)
|
|
19
19
|
this.pool = mysql2_1.default.createPool(this.credentials);
|
|
20
20
|
return this.pool;
|
|
21
21
|
}
|
|
22
|
+
getConnection() {
|
|
23
|
+
if (!this?.pool)
|
|
24
|
+
this.pool = mysql2_1.default.createPool(this.credentials);
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
this.pool.getConnection((err, connection) => {
|
|
27
|
+
if (err)
|
|
28
|
+
reject(err);
|
|
29
|
+
resolve(connection);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
}
|
|
22
33
|
async queryRow(sql, values) {
|
|
23
|
-
const pool = this.
|
|
34
|
+
const pool = this.getPoolConnection();
|
|
24
35
|
return new Promise((resolve, reject) => {
|
|
25
36
|
pool.query(sql, values, function (err, results, fields) {
|
|
26
37
|
if (err)
|
|
@@ -32,7 +43,7 @@ class MysqlConnector {
|
|
|
32
43
|
});
|
|
33
44
|
}
|
|
34
45
|
async queryRows(sql, values) {
|
|
35
|
-
const pool = this.
|
|
46
|
+
const pool = this.getPoolConnection();
|
|
36
47
|
return new Promise((resolve, reject) => {
|
|
37
48
|
pool.query(sql, values, function (err, results, fields) {
|
|
38
49
|
// Connection is automatically released when query resolves
|
|
@@ -44,7 +55,7 @@ class MysqlConnector {
|
|
|
44
55
|
});
|
|
45
56
|
}
|
|
46
57
|
async queryArray(sql, values) {
|
|
47
|
-
const pool = this.
|
|
58
|
+
const pool = this.getPoolConnection();
|
|
48
59
|
return new Promise((resolve, reject) => {
|
|
49
60
|
pool.query({ sql, values, rowsAsArray: true }, function (err, results, fields) {
|
|
50
61
|
// Connection is automatically released when query resolves
|
|
@@ -56,7 +67,7 @@ class MysqlConnector {
|
|
|
56
67
|
});
|
|
57
68
|
}
|
|
58
69
|
async executeResult(sql, values) {
|
|
59
|
-
const pool = this.
|
|
70
|
+
const pool = this.getPoolConnection();
|
|
60
71
|
return new Promise((resolve, reject) => {
|
|
61
72
|
pool.query(sql, values, function (err, results, fields) {
|
|
62
73
|
// Connection is automatically released when query resolves
|