|
| 1 | +/** |
| 2 | + * The types are explicity for learning purpose |
| 3 | + */ |
| 4 | + |
| 5 | +import { |
| 6 | + createPool, |
| 7 | + PoolOptions, |
| 8 | + Pool, |
| 9 | + ResultSetHeader, |
| 10 | + RowDataPacket, |
| 11 | +} from 'mysql2/promise'; |
| 12 | + |
| 13 | +interface User extends RowDataPacket { |
| 14 | + id: number; |
| 15 | + name: string; |
| 16 | +} |
| 17 | + |
| 18 | +class MySQL { |
| 19 | + private conn: Pool; |
| 20 | + private credentials: PoolOptions; |
| 21 | + |
| 22 | + constructor(credentials: PoolOptions) { |
| 23 | + this.credentials = credentials; |
| 24 | + this.conn = createPool(this.credentials); |
| 25 | + } |
| 26 | + |
| 27 | + /** A random method to simulate a step before to get the class methods */ |
| 28 | + private ensureConnection() { |
| 29 | + if (!this?.conn) this.conn = createPool(this.credentials); |
| 30 | + } |
| 31 | + |
| 32 | + /** For `SELECT` and `SHOW` */ |
| 33 | + get queryRows() { |
| 34 | + this.ensureConnection(); |
| 35 | + return this.conn.query.bind(this.conn)<RowDataPacket[]>; |
| 36 | + } |
| 37 | + |
| 38 | + /** For `SELECT` and `SHOW` with `rowAsArray` as `true` */ |
| 39 | + get queryRowsAsArray() { |
| 40 | + this.ensureConnection(); |
| 41 | + return this.conn.query.bind(this.conn)<RowDataPacket[][]>; |
| 42 | + } |
| 43 | + |
| 44 | + /** For `INSERT`, `UPDATE`, etc. */ |
| 45 | + get queryResult() { |
| 46 | + this.ensureConnection(); |
| 47 | + return this.conn.query.bind(this.conn)<ResultSetHeader>; |
| 48 | + } |
| 49 | + |
| 50 | + /** For multiple `INSERT`, `UPDATE`, etc. with `multipleStatements` as `true` */ |
| 51 | + get queryResults() { |
| 52 | + this.ensureConnection(); |
| 53 | + return this.conn.query.bind(this.conn)<ResultSetHeader[]>; |
| 54 | + } |
| 55 | + |
| 56 | + /** For `SELECT` and `SHOW` */ |
| 57 | + get executeRows() { |
| 58 | + this.ensureConnection(); |
| 59 | + return this.conn.execute.bind(this.conn)<RowDataPacket[]>; |
| 60 | + } |
| 61 | + |
| 62 | + /** For `SELECT` and `SHOW` with `rowAsArray` as `true` */ |
| 63 | + get executeRowsAsArray() { |
| 64 | + this.ensureConnection(); |
| 65 | + return this.conn.execute.bind(this.conn)<RowDataPacket[][]>; |
| 66 | + } |
| 67 | + |
| 68 | + /** For `INSERT`, `UPDATE`, etc. */ |
| 69 | + get executeResult() { |
| 70 | + this.ensureConnection(); |
| 71 | + return this.conn.execute.bind(this.conn)<ResultSetHeader>; |
| 72 | + } |
| 73 | + |
| 74 | + /** For multiple `INSERT`, `UPDATE`, etc. with `multipleStatements` as `true` */ |
| 75 | + get executeResults() { |
| 76 | + this.ensureConnection(); |
| 77 | + return this.conn.execute.bind(this.conn)<ResultSetHeader[]>; |
| 78 | + } |
| 79 | + |
| 80 | + /** Expose the Pool Connection */ |
| 81 | + get connection() { |
| 82 | + return this.conn; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +(async () => { |
| 87 | + const access: PoolOptions = { |
| 88 | + host: '', |
| 89 | + user: '', |
| 90 | + password: '', |
| 91 | + database: '', |
| 92 | + }; |
| 93 | + |
| 94 | + const mysql = new MySQL(access); |
| 95 | + |
| 96 | + /** Deleting the `users` table, if it exists */ |
| 97 | + await mysql.queryResult('DROP TABLE IF EXISTS `users`;'); |
| 98 | + |
| 99 | + /** Creating a minimal user table */ |
| 100 | + await mysql.queryResult( |
| 101 | + 'CREATE TABLE `users` (`id` INT(11) AUTO_INCREMENT, `name` VARCHAR(50), PRIMARY KEY (`id`));', |
| 102 | + ); |
| 103 | + |
| 104 | + /** Inserting some users */ |
| 105 | + const [inserted] = await mysql.executeResult( |
| 106 | + 'INSERT INTO `users`(`name`) VALUES(?), (?), (?), (?);', |
| 107 | + ['Josh', 'John', 'Marie', 'Gween'], |
| 108 | + ); |
| 109 | + |
| 110 | + console.log('Inserted:', inserted.affectedRows); |
| 111 | + |
| 112 | + /** Getting users */ |
| 113 | + const [users] = await mysql.queryRows( |
| 114 | + 'SELECT * FROM `users` ORDER BY `name` ASC;', |
| 115 | + ); |
| 116 | + |
| 117 | + users.forEach((user: User) => { |
| 118 | + console.log('-----------'); |
| 119 | + console.log('id: ', user.id); |
| 120 | + console.log('name:', user.name); |
| 121 | + }); |
| 122 | + |
| 123 | + await mysql.connection.end(); |
| 124 | +})(); |
| 125 | + |
| 126 | +/** Output |
| 127 | + * |
| 128 | + * Inserted: 4 |
| 129 | + * ----------- |
| 130 | + * id: 4 |
| 131 | + * name: Gween |
| 132 | + * ----------- |
| 133 | + * id: 2 |
| 134 | + * name: John |
| 135 | + * ----------- |
| 136 | + * id: 1 |
| 137 | + * name: Josh |
| 138 | + * ----------- |
| 139 | + * id: 3 |
| 140 | + * name: Marie |
| 141 | + */ |
0 commit comments