Skip to content

Commit eb9693c

Browse files
committed
docs: add ProcedureCallPacket example
1 parent 5d6c214 commit eb9693c

File tree

2 files changed

+111
-2
lines changed

2 files changed

+111
-2
lines changed

documentation/en/TypeScript-Examples.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,5 @@ You can also check some code examples using **MySQL2** and **TypeScript** to und
305305
- [Extending and using **Interfaces** with `RowDataPacket` and `rowAsArray`](../../examples/typescript/row-data-packet-row-as-array.ts)
306306
- [Extending and using **Interfaces** with `RowDataPacket` and `multipleStatements`](../../examples/typescript/row-data-packet-multi-statements.ts)
307307
- [Extending and using **Interfaces** with `RowDataPacket`, `rowAsArray` and `multipleStatements`](../../examples/typescript/row-data-packet-row-as-array-multi-statements.ts)
308-
- Checking for `ResultSetHeader` or `RowDataPacket[]` using **Procedure Calls** (*in progress*)
309-
- Checking for `ResultSetHeader` or `RowDataPacket[][]` using **Procedure Calls** (*in progress*)
308+
- [Checking for `ResultSetHeader`, extending and using **Interfaces** with `RowDataPacket` from `ProcedureCallPacket`](../../examples/typescript/procedure-call-packet.ts)
310309
- Creating a custom **MySQL2** **Class** (*in progress*)
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
ProcedureCallPacket,
9+
ResultSetHeader,
10+
RowDataPacket,
11+
} from 'mysql2/promise';
12+
13+
interface User extends RowDataPacket {
14+
id: number;
15+
name: string;
16+
}
17+
18+
const isResultSetHeader = (data: unknown): data is ResultSetHeader => {
19+
if (!data || typeof data !== 'object') return false;
20+
21+
const keys = [
22+
'fieldCount',
23+
'affectedRows',
24+
'insertId',
25+
'info',
26+
'serverStatus',
27+
'warningStatus',
28+
'changedRows',
29+
];
30+
31+
return keys.every((key) => key in data);
32+
};
33+
34+
(async () => {
35+
const access: ConnectionOptions = {
36+
host: '',
37+
user: '',
38+
password: '',
39+
database: '',
40+
};
41+
42+
const conn = await mysql.createConnection(access);
43+
44+
/** Deleting the `users` table, if it exists */
45+
await conn.query<ResultSetHeader>('DROP TABLE IF EXISTS `users`;');
46+
47+
/** Creating a minimal user table */
48+
await conn.query<ResultSetHeader>(
49+
'CREATE TABLE `users` (`id` INT(11) AUTO_INCREMENT, `name` VARCHAR(50), PRIMARY KEY (`id`));',
50+
);
51+
52+
/** Inserting some users */
53+
const [inserted] = await conn.execute<ResultSetHeader>(
54+
'INSERT INTO `users`(`name`) VALUES(?), (?), (?), (?);',
55+
['Josh', 'John', 'Marie', 'Gween'],
56+
);
57+
58+
console.log('Inserted:', inserted.affectedRows);
59+
60+
/** Deleting the `getUsers` procedure, if it exists */
61+
await conn.query<ResultSetHeader>('DROP PROCEDURE IF EXISTS getUsers');
62+
63+
/** Creating a procedure to get the users */
64+
await conn.query<ResultSetHeader>(`
65+
CREATE PROCEDURE getUsers()
66+
BEGIN
67+
SELECT * FROM users ORDER BY name ASC;
68+
END
69+
`);
70+
71+
/** Getting users */
72+
const [procedureResult] = await conn.query<ProcedureCallPacket<User[][]>>(
73+
'CALL getUsers()',
74+
);
75+
76+
procedureResult.forEach((users) => {
77+
/** By perform a `SELECT` or `SHOW`, The last item of `procedureResult` always be a `ResultSetHeader` */
78+
if (isResultSetHeader(users)) {
79+
console.log('----------------');
80+
console.log('Affected Rows:', users.affectedRows);
81+
} else {
82+
users.forEach((user) => {
83+
console.log('----------------');
84+
console.log('id: ', user.id);
85+
console.log('name:', user.name);
86+
});
87+
}
88+
});
89+
90+
await conn.end();
91+
})();
92+
93+
/** Output
94+
*
95+
* Inserted: 4
96+
* ----------------
97+
* id: 4
98+
* name: Gween
99+
* ----------------
100+
* id: 2
101+
* name: John
102+
* ----------------
103+
* id: 1
104+
* name: Josh
105+
* ----------------
106+
* id: 3
107+
* name: Marie
108+
* ----------------
109+
* Affected Rows: 0
110+
*/

0 commit comments

Comments
 (0)