-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathpromise.ts
53 lines (43 loc) · 1.43 KB
/
promise.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
import * as mysql from 'mysql2/promise';
// Connections
async function testConnections() {
let connection = await mysql.createConnection({
host: 'localhost',
user: 'me',
password: 'secret'
});
connection.connect()
.then(() => connection.query<mysql.RowDataPacket[]>('SELECT 1 + 1 AS solution'))
.then(([rows, fields]) => {
console.log('The solution is: ', rows[0]['solution']);
});
connection.connect()
.then(() => connection.execute<mysql.RowDataPacket[]>('SELECT 1 + 1 AS solution'))
.then(([rows, fields]) => {
console.log('The solution is: ', rows[0]['solution']);
});
}
/// Pools
let poolConfig = {
connectionLimit: 10,
host: 'example.org',
user: 'bob',
password: 'secret'
};
let pool = mysql.createPool(poolConfig);
pool.query<mysql.RowDataPacket[]>('SELECT 1 + 1 AS solution')
.then(([rows, fields]) => {
console.log('The solution is: ', rows[0]['solution']);
});
pool.execute<mysql.RowDataPacket[]>('SELECT 1 + 1 AS solution')
.then(([rows, fields]) => {
console.log('The solution is: ', rows[0]['solution']);
});
async function test() {
const connection = await pool.getConnection();
// Use the connection
await connection.ping();
const rows = await connection.query('SELECT something FROM sometable');
// And done with the connection.
connection.release();
}