|
| 1 | +# Using MySQL2 with TypeScript |
| 2 | + |
| 3 | +## Installation |
| 4 | +```bash |
| 5 | +npm install --save mysql2 |
| 6 | +npm install --save-dev @types/node |
| 7 | +``` |
| 8 | + |
| 9 | +> The `@types/node` ensure the proper interaction between **TypeScript** and the **Node.js** modules used by **MySQL2** (*net*, *events*, *stream*, *tls*, etc.). |
| 10 | +
|
| 11 | +## Import |
| 12 | +You can import **MySQL2** in two ways: |
| 13 | +```ts |
| 14 | +import * as mysql from 'mysql2'; |
| 15 | +import * as mysql from 'mysql2/promise'; |
| 16 | + |
| 17 | +// By setting the `esModuleInterop` option to `true` in `tsconfig.json` |
| 18 | +import mysql from 'mysql2'; |
| 19 | +import mysql from 'mysql2/promise'; |
| 20 | +``` |
| 21 | + |
| 22 | +## Connection |
| 23 | +```ts |
| 24 | +import mysql, { ConnectionOptions } from 'mysql2'; |
| 25 | + |
| 26 | +const access: ConnectionOptions = { |
| 27 | + user: 'test', |
| 28 | + database: 'test', |
| 29 | +}; |
| 30 | + |
| 31 | +const conn = mysql.createConnection(access); |
| 32 | +``` |
| 33 | + |
| 34 | +## Pool Connection |
| 35 | +```ts |
| 36 | +import mysql, { PoolOptions } from 'mysql2'; |
| 37 | + |
| 38 | +const access: PoolOptions = { |
| 39 | + user: 'test', |
| 40 | + database: 'test', |
| 41 | +}; |
| 42 | + |
| 43 | +const conn = mysql.createPool(access); |
| 44 | +``` |
| 45 | + |
| 46 | +## Query and Execute |
| 47 | +### A simple SELECT |
| 48 | +```ts |
| 49 | +conn.query('SELECT 1 + 1 AS `test`;', (_err, rows) => { |
| 50 | + /** |
| 51 | + * @rows: [ { test: 2 } ] |
| 52 | + */ |
| 53 | +}); |
| 54 | +``` |
| 55 | + |
| 56 | +The `rows` output will be these possible types: |
| 57 | +- `RowDataPacket[]` |
| 58 | +- `RowDataPacket[][]` |
| 59 | +- `ResultSetHeader` |
| 60 | +- `ProcedureCallPacket` |
| 61 | +- `OkPacket` |
| 62 | +- `OkPacket[]` |
| 63 | + |
| 64 | +In this example, you need to manually check the output types |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +### Type Specification |
| 69 | +#### RowDataPacket[] |
| 70 | +An array with the returned rows, for example: |
| 71 | +```ts |
| 72 | +import mysql, { RowDataPacket } from 'mysql2'; |
| 73 | + |
| 74 | +const conn = mysql.createConnection({ |
| 75 | + user: 'test', |
| 76 | + database: 'test', |
| 77 | +}); |
| 78 | + |
| 79 | +conn.query<RowDataPacket[]>('SELECT 1 + 1 AS `test`;', (_err, rows) => { |
| 80 | + console.log(rows); |
| 81 | + /** |
| 82 | + * @rows: [ { test: 2 } ] |
| 83 | + */ |
| 84 | +}); |
| 85 | +``` |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | +#### RowDataPacket[][] |
| 90 | +- `rowsAsArray` |
| 91 | + ```ts |
| 92 | + import mysql, { RowDataPacket } from 'mysql2'; |
| 93 | + |
| 94 | + const conn = mysql.createConnection({ |
| 95 | + user: 'test', |
| 96 | + database: 'test', |
| 97 | + rowsAsArray: true, |
| 98 | + }); |
| 99 | + |
| 100 | + const sql = ` |
| 101 | + SELECT 1 + 1 AS test, 2 + 2 AS test; |
| 102 | + `; |
| 103 | + |
| 104 | + conn.query<RowDataPacket[][]>(sql, (_err, rows) => { |
| 105 | + console.log(rows); |
| 106 | + /** |
| 107 | + * @rows: [ [ 2, 4 ] ] |
| 108 | + */ |
| 109 | + }); |
| 110 | + ``` |
| 111 | + |
| 112 | +- `multipleStatements` with multiple queries |
| 113 | + ```ts |
| 114 | + import mysql, { RowDataPacket } from 'mysql2'; |
| 115 | + |
| 116 | + const conn = mysql.createConnection({ |
| 117 | + user: 'test', |
| 118 | + database: 'test', |
| 119 | + multipleStatements: true, |
| 120 | + }); |
| 121 | + |
| 122 | + const sql = ` |
| 123 | + SELECT 1 + 1 AS test; |
| 124 | + SELECT 2 + 2 AS test; |
| 125 | + `; |
| 126 | + |
| 127 | + conn.query<RowDataPacket[][]>(sql, (_err, rows) => { |
| 128 | + console.log(rows); |
| 129 | + /** |
| 130 | + * @rows: [ [ { test: 2 } ], [ { test: 4 } ] ] |
| 131 | + */ |
| 132 | + }); |
| 133 | + ``` |
| 134 | + |
| 135 | +#### ResultSetHeader |
| 136 | +> In progress |
| 137 | +
|
| 138 | +#### ProcedureCallPacket |
| 139 | +> In progress |
| 140 | +
|
| 141 | +#### OkPacket |
| 142 | +> In progress |
| 143 | +
|
| 144 | +#### OkPacket[] |
| 145 | +> In progress |
| 146 | +
|
| 147 | +--- |
| 148 | + |
| 149 | +## Examples |
| 150 | + |
| 151 | +You can also check various code examples using **MySQL2** and **TypeScript** to understand advanced concepts: |
| 152 | +> In progress |
| 153 | +
|
| 154 | +- Extending and using **Interfaces** with `RowDataPacket`. |
| 155 | +- Checking for `ResultSetHeader` or `RowDataPacket[]` using **Procedure Calls** |
| 156 | +- Checking for `ResultSetHeader` or `RowDataPacket[][]` using **Procedure Calls** |
| 157 | +- Creating a custom **MySQL2** **Class** |
0 commit comments