Skip to content

Commit 9e56bd4

Browse files
committed
docs: add RowDataPacket[] with rowsAsArray example
1 parent 8f14666 commit 9e56bd4

File tree

2 files changed

+107
-31
lines changed

2 files changed

+107
-31
lines changed

documentation/en/TypeScript-Examples.md

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -106,37 +106,38 @@ conn.query<RowDataPacket[]>('SHOW TABLES FROM `test`;', (_err, rows) => {
106106
});
107107
```
108108

109-
---
109+
Using `rowsAsArray` option as `true`:
110110

111-
### RowDataPacket[][]
112-
- When `rowsAsArray` option is `true`
113-
```ts
114-
import mysql, { RowDataPacket } from 'mysql2';
111+
```ts
112+
import mysql, { RowDataPacket } from 'mysql2';
115113

116-
const conn = mysql.createConnection({
117-
user: 'test',
118-
database: 'test',
119-
rowsAsArray: true,
120-
});
114+
const conn = mysql.createConnection({
115+
user: 'test',
116+
database: 'test',
117+
rowsAsArray: true,
118+
});
121119

122-
// SELECT
123-
conn.query<RowDataPacket[][]>('SELECT 1 + 1 AS test, 2 + 2 AS test;', (_err, rows) => {
124-
console.log(rows);
125-
/**
126-
* @rows: [ [ 2, 4 ] ]
127-
*/
128-
});
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+
});
129127

130-
// SHOW
131-
conn.query<RowDataPacket[][]>('SHOW TABLES FROM `test`;', (_err, rows) => {
132-
console.log(rows);
133-
/**
134-
* @rows: [ [ 'test' ] ]
135-
*/
136-
});
137-
```
128+
// SHOW
129+
conn.query<RowDataPacket[]>('SHOW TABLES FROM `test`;', (_err, rows) => {
130+
console.log(rows);
131+
/**
132+
* @rows: [ [ 'test' ] ]
133+
*/
134+
});
135+
```
138136

139-
- When `multipleStatements` is `true` with multiple queries
137+
---
138+
139+
### RowDataPacket[][]
140+
Using `multipleStatements`option as `true` with multiple queries:
140141
```ts
141142
import mysql, { RowDataPacket } from 'mysql2';
142143

@@ -162,7 +163,7 @@ conn.query<RowDataPacket[]>('SHOW TABLES FROM `test`;', (_err, rows) => {
162163
---
163164

164165
### ResultSetHeader
165-
For `INSERT`, `UPDATE`, `DELETE`, `TRUNCATE`, etc.
166+
For `INSERT`, `UPDATE`, `DELETE`, `TRUNCATE`, etc.:
166167
```ts
167168
import mysql, { ResultSetHeader } from 'mysql2';
168169

@@ -194,7 +195,7 @@ conn.query<ResultSetHeader>(sql, (_err, result) => {
194195
---
195196

196197
### ResultSetHeader[]
197-
For multiples `INSERT`, `UPDATE`, `DELETE`, `TRUNCATE`, etc. when using `multipleStatements` as `true`
198+
For multiples `INSERT`, `UPDATE`, `DELETE`, `TRUNCATE`, etc. when using `multipleStatements` as `true`:
198199

199200
```ts
200201
import mysql, { ResultSetHeader } from 'mysql2';
@@ -240,9 +241,7 @@ conn.query<ResultSetHeader[]>(sql, (_err, results) => {
240241
---
241242

242243
### ProcedureCallPacket
243-
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`).
244-
245-
> For `CREATE PROCEDURE` and `DROP PROCEDURE`, these returns will be the *default* `ResultSetHeader`.
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`):
246245

247246
```ts
248247
import mysql, { ProcedureCallPacket, ResultSetHeader } from 'mysql2';
@@ -283,6 +282,8 @@ conn.query<ProcedureCallPacket<ResultSetHeader>>(sql, (_err, result) => {
283282
});
284283
```
285284

285+
> For `CREATE PROCEDURE` and `DROP PROCEDURE`, these returns will be the *default* `ResultSetHeader`.
286+
286287
By using `SELECT` and `SHOW` queries in a **Procedure Call**, it groups the results as:
287288
```tsx
288289
// ProcedureCallPacket<RowDataPacket[]>
@@ -291,6 +292,7 @@ By using `SELECT` and `SHOW` queries in a **Procedure Call**, it groups the resu
291292
// ProcedureCallPacket<RowDataPacket[][]>
292293
[...RowDataPacket[][], ResultSetHeader]
293294
```
295+
294296
For `ProcedureCallPacket<RowDataPacket[]>` and `ProcedureCallPacket<RowDataPacket[][]>`, please see the following examples.
295297

296298
---
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* The types are explicity for learning purpose
3+
* By extending the `RowDataPacket`, you can use your Interface in `query` and `execute`
4+
*/
5+
6+
import mysql, {
7+
ConnectionOptions,
8+
ResultSetHeader,
9+
RowDataPacket,
10+
} from 'mysql2/promise';
11+
12+
interface User extends RowDataPacket {
13+
/** id */
14+
0: number;
15+
/** name */
16+
1: string;
17+
}
18+
19+
(async () => {
20+
const access: ConnectionOptions = {
21+
host: '',
22+
user: '',
23+
password: '',
24+
database: '',
25+
};
26+
27+
const conn = await mysql.createConnection(access);
28+
29+
/** Deleting the `users` table, if it exists */
30+
await conn.query<ResultSetHeader>('DROP TABLE IF EXISTS `users`;');
31+
32+
/** Creating a minimal user table */
33+
await conn.query<ResultSetHeader>(
34+
'CREATE TABLE `users` (`id` INT(11) AUTO_INCREMENT, `name` VARCHAR(50), PRIMARY KEY (`id`));',
35+
);
36+
37+
/** Inserting some users */
38+
const [inserted] = await conn.execute<ResultSetHeader>(
39+
'INSERT INTO `users`(`name`) VALUES(?), (?), (?), (?);',
40+
['Josh', 'John', 'Marie', 'Gween'],
41+
);
42+
43+
console.log('Inserted:', inserted.affectedRows);
44+
45+
/** Getting users */
46+
const [users] = await conn.query<User[]>(
47+
'SELECT * FROM `users` ORDER BY `name` ASC;',
48+
);
49+
50+
users.forEach((user) => {
51+
console.log('-----------');
52+
console.log('id: ', user[0]);
53+
console.log('name:', user[1]);
54+
});
55+
56+
await conn.end();
57+
})();
58+
59+
/** Output
60+
*
61+
* Inserted: 4
62+
* -----------
63+
* id: 4
64+
* name: Gween
65+
* -----------
66+
* id: 2
67+
* name: John
68+
* -----------
69+
* id: 1
70+
* name: Josh
71+
* -----------
72+
* id: 3
73+
* name: Marie
74+
*/

0 commit comments

Comments
 (0)