|
| 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 | +Requires **TypeScript** `>=4.5.2`. |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## Usage |
| 16 | +You can import **MySQL2** in two ways: |
| 17 | +- By setting the `esModuleInterop` option to `true` in `tsconfig.json` |
| 18 | +```ts |
| 19 | +import mysql from 'mysql2'; |
| 20 | +import mysql from 'mysql2/promise'; |
| 21 | +``` |
| 22 | + |
| 23 | +- By setting the `esModuleInterop` option to `false` in `tsconfig.json` |
| 24 | +```ts |
| 25 | +import * as mysql from 'mysql2'; |
| 26 | +import * as mysql from 'mysql2/promise'; |
| 27 | +``` |
| 28 | + |
| 29 | +### Connection |
| 30 | +```ts |
| 31 | +import mysql, { ConnectionOptions } from 'mysql2'; |
| 32 | + |
| 33 | +const access: ConnectionOptions = { |
| 34 | + user: 'test', |
| 35 | + database: 'test', |
| 36 | +}; |
| 37 | + |
| 38 | +const conn = mysql.createConnection(access); |
| 39 | +``` |
| 40 | + |
| 41 | +### Pool Connection |
| 42 | +```ts |
| 43 | +import mysql, { PoolOptions } from 'mysql2'; |
| 44 | + |
| 45 | +const access: PoolOptions = { |
| 46 | + user: 'test', |
| 47 | + database: 'test', |
| 48 | +}; |
| 49 | + |
| 50 | +const conn = mysql.createPool(access); |
| 51 | +``` |
| 52 | + |
| 53 | +### Query and Execute |
| 54 | +#### A simple query |
| 55 | +```ts |
| 56 | +conn.query('SELECT 1 + 1 AS `test`;', (_err, rows) => { |
| 57 | + /** |
| 58 | + * @rows: [ { test: 2 } ] |
| 59 | + */ |
| 60 | +}); |
| 61 | + |
| 62 | +conn.execute('SELECT 1 + 1 AS `test`;', (_err, rows) => { |
| 63 | + /** |
| 64 | + * @rows: [ { test: 2 } ] |
| 65 | + */ |
| 66 | +}); |
| 67 | +``` |
| 68 | + |
| 69 | +The `rows` output will be these possible types: |
| 70 | +- `RowDataPacket[]` |
| 71 | +- `RowDataPacket[][]` |
| 72 | +- `ResultSetHeader` |
| 73 | +- `ResultSetHeader[]` |
| 74 | +- `ProcedureCallPacket` |
| 75 | + |
| 76 | +In this example, you need to manually check the output types |
| 77 | + |
| 78 | +--- |
| 79 | + |
| 80 | +## Type Specification |
| 81 | +### RowDataPacket[] |
| 82 | +An array with the returned rows, for example: |
| 83 | + |
| 84 | +```ts |
| 85 | +import mysql, { RowDataPacket } from 'mysql2'; |
| 86 | + |
| 87 | +const conn = mysql.createConnection({ |
| 88 | + user: 'test', |
| 89 | + database: 'test', |
| 90 | +}); |
| 91 | + |
| 92 | +// SELECT |
| 93 | +conn.query<RowDataPacket[]>('SELECT 1 + 1 AS `test`;', (_err, rows) => { |
| 94 | + console.log(rows); |
| 95 | + /** |
| 96 | + * @rows: [ { test: 2 } ] |
| 97 | + */ |
| 98 | +}); |
| 99 | + |
| 100 | +// SHOW |
| 101 | +conn.query<RowDataPacket[]>('SHOW TABLES FROM `test`;', (_err, rows) => { |
| 102 | + console.log(rows); |
| 103 | + /** |
| 104 | + * @rows: [ { Tables_in_test: 'test' } ] |
| 105 | + */ |
| 106 | +}); |
| 107 | +``` |
| 108 | + |
| 109 | +Using `rowsAsArray` option as `true`: |
| 110 | + |
| 111 | +```ts |
| 112 | +import mysql, { RowDataPacket } from 'mysql2'; |
| 113 | + |
| 114 | +const conn = mysql.createConnection({ |
| 115 | + user: 'test', |
| 116 | + database: 'test', |
| 117 | + rowsAsArray: true, |
| 118 | +}); |
| 119 | + |
| 120 | +// SELECT |
| 121 | +conn.query<RowDataPacket[]>('SELECT 1 + 1 AS test, 2 + 2 AS test;', (_err, rows) => { |
| 122 | + console.log(rows); |
| 123 | + /** |
| 124 | + * @rows: [ [ 2, 4 ] ] |
| 125 | + */ |
| 126 | +}); |
| 127 | + |
| 128 | +// SHOW |
| 129 | +conn.query<RowDataPacket[]>('SHOW TABLES FROM `test`;', (_err, rows) => { |
| 130 | + console.log(rows); |
| 131 | + /** |
| 132 | + * @rows: [ [ 'test' ] ] |
| 133 | + */ |
| 134 | +}); |
| 135 | +``` |
| 136 | + |
| 137 | +--- |
| 138 | + |
| 139 | +### RowDataPacket[][] |
| 140 | +Using `multipleStatements`option as `true` with multiple queries: |
| 141 | + ```ts |
| 142 | + import mysql, { RowDataPacket } from 'mysql2'; |
| 143 | + |
| 144 | + const conn = mysql.createConnection({ |
| 145 | + user: 'test', |
| 146 | + database: 'test', |
| 147 | + multipleStatements: true, |
| 148 | + }); |
| 149 | + |
| 150 | + const sql = ` |
| 151 | + SELECT 1 + 1 AS test; |
| 152 | + SELECT 2 + 2 AS test; |
| 153 | + `; |
| 154 | + |
| 155 | + conn.query<RowDataPacket[][]>(sql, (_err, rows) => { |
| 156 | + console.log(rows); |
| 157 | + /** |
| 158 | + * @rows: [ [ { test: 2 } ], [ { test: 4 } ] ] |
| 159 | + */ |
| 160 | + }); |
| 161 | + ``` |
| 162 | + |
| 163 | +--- |
| 164 | + |
| 165 | +### ResultSetHeader |
| 166 | +For `INSERT`, `UPDATE`, `DELETE`, `TRUNCATE`, etc.: |
| 167 | +```ts |
| 168 | +import mysql, { ResultSetHeader } from 'mysql2'; |
| 169 | + |
| 170 | +const conn = mysql.createConnection({ |
| 171 | + user: 'test', |
| 172 | + database: 'test', |
| 173 | +}); |
| 174 | + |
| 175 | +const sql = ` |
| 176 | + SET @1 = 1; |
| 177 | +`; |
| 178 | + |
| 179 | +conn.query<ResultSetHeader>(sql, (_err, result) => { |
| 180 | + console.log(result); |
| 181 | + /** |
| 182 | + * @result: ResultSetHeader { |
| 183 | + fieldCount: 0, |
| 184 | + affectedRows: 0, |
| 185 | + insertId: 0, |
| 186 | + info: '', |
| 187 | + serverStatus: 2, |
| 188 | + warningStatus: 0, |
| 189 | + changedRows: 0 |
| 190 | + } |
| 191 | + */ |
| 192 | +}); |
| 193 | +``` |
| 194 | + |
| 195 | +--- |
| 196 | + |
| 197 | +### ResultSetHeader[] |
| 198 | +For multiples `INSERT`, `UPDATE`, `DELETE`, `TRUNCATE`, etc. when using `multipleStatements` as `true`: |
| 199 | + |
| 200 | +```ts |
| 201 | +import mysql, { ResultSetHeader } from 'mysql2'; |
| 202 | + |
| 203 | +const conn = mysql.createConnection({ |
| 204 | + user: 'test', |
| 205 | + database: 'test', |
| 206 | + multipleStatements: true, |
| 207 | +}); |
| 208 | + |
| 209 | +const sql = ` |
| 210 | + SET @1 = 1; |
| 211 | + SET @2 = 2; |
| 212 | +`; |
| 213 | + |
| 214 | +conn.query<ResultSetHeader[]>(sql, (_err, results) => { |
| 215 | + console.log(results); |
| 216 | + /** |
| 217 | + * @results: [ |
| 218 | + ResultSetHeader { |
| 219 | + fieldCount: 0, |
| 220 | + affectedRows: 0, |
| 221 | + insertId: 0, |
| 222 | + info: '', |
| 223 | + serverStatus: 10, |
| 224 | + warningStatus: 0, |
| 225 | + changedRows: 0 |
| 226 | + }, |
| 227 | + ResultSetHeader { |
| 228 | + fieldCount: 0, |
| 229 | + affectedRows: 0, |
| 230 | + insertId: 0, |
| 231 | + info: '', |
| 232 | + serverStatus: 2, |
| 233 | + warningStatus: 0, |
| 234 | + changedRows: 0 |
| 235 | + } |
| 236 | + ] |
| 237 | + */ |
| 238 | +}); |
| 239 | +``` |
| 240 | + |
| 241 | +--- |
| 242 | + |
| 243 | +### ProcedureCallPacket |
| 244 | +By performing a **Call Procedure** using `INSERT`, `UPDATE`, etc., the return will be a `ProcedureCallPacket<ResultSetHeader>` (even if you perform multiples queries and set `multipleStatements` to `true`): |
| 245 | + |
| 246 | +```ts |
| 247 | +import mysql, { ProcedureCallPacket, ResultSetHeader } from 'mysql2'; |
| 248 | + |
| 249 | +const conn = mysql.createConnection({ |
| 250 | + user: 'test', |
| 251 | + database: 'test', |
| 252 | +}); |
| 253 | + |
| 254 | +/** ResultSetHeader */ |
| 255 | +conn.query('DROP PROCEDURE IF EXISTS myProcedure'); |
| 256 | + |
| 257 | +/** ResultSetHeader */ |
| 258 | +conn.query(` |
| 259 | + CREATE PROCEDURE myProcedure() |
| 260 | + BEGIN |
| 261 | + SET @1 = 1; |
| 262 | + SET @2 = 2; |
| 263 | + END |
| 264 | + `); |
| 265 | + |
| 266 | +/** ProcedureCallPacket */ |
| 267 | +const sql = 'CALL myProcedure()'; |
| 268 | + |
| 269 | +conn.query<ProcedureCallPacket<ResultSetHeader>>(sql, (_err, result) => { |
| 270 | + console.log(result); |
| 271 | + /** |
| 272 | + * @result: ResultSetHeader { |
| 273 | + fieldCount: 0, |
| 274 | + affectedRows: 0, |
| 275 | + insertId: 0, |
| 276 | + info: '', |
| 277 | + serverStatus: 2, |
| 278 | + warningStatus: 0, |
| 279 | + changedRows: 0 |
| 280 | + } |
| 281 | + */ |
| 282 | +}); |
| 283 | +``` |
| 284 | + |
| 285 | +> For `CREATE PROCEDURE` and `DROP PROCEDURE`, these returns will be the *default* `ResultSetHeader`. |
| 286 | +
|
| 287 | +By using `SELECT` and `SHOW` queries in a **Procedure Call**, it groups the results as: |
| 288 | +```tsx |
| 289 | +/** ProcedureCallPacket<RowDataPacket[]> */ |
| 290 | +[RowDataPacket[], ResultSetHeader] |
| 291 | +``` |
| 292 | + |
| 293 | +For `ProcedureCallPacket<RowDataPacket[]>`, please see the following examples. |
| 294 | + |
| 295 | +--- |
| 296 | + |
| 297 | +## Examples |
| 298 | +You can also check some code examples using **MySQL2** and **TypeScript** to understand advanced concepts: |
| 299 | + |
| 300 | +- [Extending and using **Interfaces** with `RowDataPacket`](../../examples/typescript/row-data-packet.ts) |
| 301 | +- [Extending and using **Interfaces** with `RowDataPacket` and `rowAsArray`](../../examples/typescript/row-data-packet-row-as-array.ts) |
| 302 | +- [Extending and using **Interfaces** with `RowDataPacket` and `multipleStatements`](../../examples/typescript/row-data-packet-multi-statements.ts) |
| 303 | +- [Extending and using **Interfaces** with `RowDataPacket`, `rowAsArray` and `multipleStatements`](../../examples/typescript/row-data-packet-row-as-array-multi-statements.ts) |
| 304 | +- [Checking for `ResultSetHeader`, extending and using **Interfaces** with `RowDataPacket` from `ProcedureCallPacket`](../../examples/typescript/procedure-call-packet.ts) |
| 305 | +- [Checking for `ResultSetHeader`, extending and using **Interfaces** with `RowDataPacket` and `rowAsArray` from `ProcedureCallPacket`](../../examples/typescript/procedure-call-packet-row-as-array.ts) |
| 306 | +- [Creating a basic custom **MySQL2** **Class**](../../examples/typescript/basic-custom-class.ts) |
0 commit comments