|
| 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 */ |
| 15 | + 0: number; |
| 16 | + /** name */ |
| 17 | + 1: string; |
| 18 | +} |
| 19 | + |
| 20 | +const isResultSetHeader = (data: unknown): data is ResultSetHeader => { |
| 21 | + if (!data || typeof data !== 'object') return false; |
| 22 | + |
| 23 | + const keys = [ |
| 24 | + 'fieldCount', |
| 25 | + 'affectedRows', |
| 26 | + 'insertId', |
| 27 | + 'info', |
| 28 | + 'serverStatus', |
| 29 | + 'warningStatus', |
| 30 | + 'changedRows', |
| 31 | + ]; |
| 32 | + |
| 33 | + return keys.every((key) => key in data); |
| 34 | +}; |
| 35 | + |
| 36 | +(async () => { |
| 37 | + const access: ConnectionOptions = { |
| 38 | + host: '', |
| 39 | + user: '', |
| 40 | + password: '', |
| 41 | + database: '', |
| 42 | + rowsAsArray: true, |
| 43 | + }; |
| 44 | + |
| 45 | + const conn = await mysql.createConnection(access); |
| 46 | + |
| 47 | + /** Deleting the `users` table, if it exists */ |
| 48 | + await conn.query<ResultSetHeader>('DROP TABLE IF EXISTS `users`;'); |
| 49 | + |
| 50 | + /** Creating a minimal user table */ |
| 51 | + await conn.query<ResultSetHeader>( |
| 52 | + 'CREATE TABLE `users` (`id` INT(11) AUTO_INCREMENT, `name` VARCHAR(50), PRIMARY KEY (`id`));', |
| 53 | + ); |
| 54 | + |
| 55 | + /** Inserting some users */ |
| 56 | + const [inserted] = await conn.execute<ResultSetHeader>( |
| 57 | + 'INSERT INTO `users`(`name`) VALUES(?), (?), (?), (?);', |
| 58 | + ['Josh', 'John', 'Marie', 'Gween'], |
| 59 | + ); |
| 60 | + |
| 61 | + console.log('Inserted:', inserted.affectedRows); |
| 62 | + |
| 63 | + /** Deleting the `getUsers` procedure, if it exists */ |
| 64 | + await conn.query<ResultSetHeader>('DROP PROCEDURE IF EXISTS getUsers'); |
| 65 | + |
| 66 | + /** Creating a procedure to get the users */ |
| 67 | + await conn.query<ResultSetHeader>(` |
| 68 | + CREATE PROCEDURE getUsers() |
| 69 | + BEGIN |
| 70 | + SELECT * FROM users ORDER BY name ASC; |
| 71 | + END |
| 72 | + `); |
| 73 | + |
| 74 | + /** Getting users */ |
| 75 | + const [procedureResult] = await conn.query<ProcedureCallPacket<User[][]>>( |
| 76 | + 'CALL getUsers()', |
| 77 | + ); |
| 78 | + |
| 79 | + procedureResult.forEach((users) => { |
| 80 | + /** By perform a `SELECT` or `SHOW`, The last item of `procedureResult` always be a `ResultSetHeader` */ |
| 81 | + if (isResultSetHeader(users)) { |
| 82 | + console.log('----------------'); |
| 83 | + console.log('Affected Rows:', users.affectedRows); |
| 84 | + } else { |
| 85 | + users.forEach((user) => { |
| 86 | + console.log('----------------'); |
| 87 | + console.log('id: ', user[0]); |
| 88 | + console.log('name:', user[1]); |
| 89 | + }); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + await conn.end(); |
| 94 | +})(); |
| 95 | + |
| 96 | +/** Output |
| 97 | + * |
| 98 | + * Inserted: 4 |
| 99 | + * ---------------- |
| 100 | + * id: 4 |
| 101 | + * name: Gween |
| 102 | + * ---------------- |
| 103 | + * id: 2 |
| 104 | + * name: John |
| 105 | + * ---------------- |
| 106 | + * id: 1 |
| 107 | + * name: Josh |
| 108 | + * ---------------- |
| 109 | + * id: 3 |
| 110 | + * name: Marie |
| 111 | + * ---------------- |
| 112 | + * Affected Rows: 0 |
| 113 | + */ |
0 commit comments