From b63b94c05c93a0f34d746d15a8fc180665b2201f Mon Sep 17 00:00:00 2001 From: wellwelwel <46850407+wellwelwel@users.noreply.github.com> Date: Sat, 8 Jul 2023 17:09:29 -0300 Subject: [PATCH] docs: add a basic custom MySQL2 class exampleusing TypeScript --- documentation/en/TypeScript-Examples.md | 2 +- examples/typescript/baisc-custom-class.ts | 141 ++++++++++++++++++++++ 2 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 examples/typescript/baisc-custom-class.ts diff --git a/documentation/en/TypeScript-Examples.md b/documentation/en/TypeScript-Examples.md index b744fe2fea..7cc9f7bbea 100644 --- a/documentation/en/TypeScript-Examples.md +++ b/documentation/en/TypeScript-Examples.md @@ -307,4 +307,4 @@ You can also check some code examples using **MySQL2** and **TypeScript** to und - [Extending and using **Interfaces** with `RowDataPacket`, `rowAsArray` and `multipleStatements`](../../examples/typescript/row-data-packet-row-as-array-multi-statements.ts) - [Checking for `ResultSetHeader`, extending and using **Interfaces** with `RowDataPacket` from `ProcedureCallPacket`](../../examples/typescript/procedure-call-packet.ts) - [Checking for `ResultSetHeader`, extending and using **Interfaces** with `RowDataPacket` and `rowAsArray` from `ProcedureCallPacket`](../../examples/typescript/procedure-call-packet-row-as-array.ts) -- Creating a custom **MySQL2** **Class** (*in progress*) +- [Creating a basic custom **MySQL2** **Class**](../../examples/typescript/baisc-custom-class.ts) diff --git a/examples/typescript/baisc-custom-class.ts b/examples/typescript/baisc-custom-class.ts new file mode 100644 index 0000000000..dc9da8f4dd --- /dev/null +++ b/examples/typescript/baisc-custom-class.ts @@ -0,0 +1,141 @@ +/** + * The types are explicity for learning purpose + */ + +import { + createPool, + PoolOptions, + Pool, + ResultSetHeader, + RowDataPacket, +} from 'mysql2/promise'; + +interface User extends RowDataPacket { + id: number; + name: string; +} + +class MySQL { + private conn: Pool; + private credentials: PoolOptions; + + constructor(credentials: PoolOptions) { + this.credentials = credentials; + this.conn = createPool(this.credentials); + } + + /** A random method to simulate a step before to get the class methods */ + private ensureConnection() { + if (!this?.conn) this.conn = createPool(this.credentials); + } + + /** For `SELECT` and `SHOW` */ + get queryRows() { + this.ensureConnection(); + return this.conn.query.bind(this.conn); + } + + /** For `SELECT` and `SHOW` with `rowAsArray` as `true` */ + get queryRowsAsArray() { + this.ensureConnection(); + return this.conn.query.bind(this.conn); + } + + /** For `INSERT`, `UPDATE`, etc. */ + get queryResult() { + this.ensureConnection(); + return this.conn.query.bind(this.conn); + } + + /** For multiple `INSERT`, `UPDATE`, etc. with `multipleStatements` as `true` */ + get queryResults() { + this.ensureConnection(); + return this.conn.query.bind(this.conn); + } + + /** For `SELECT` and `SHOW` */ + get executeRows() { + this.ensureConnection(); + return this.conn.execute.bind(this.conn); + } + + /** For `SELECT` and `SHOW` with `rowAsArray` as `true` */ + get executeRowsAsArray() { + this.ensureConnection(); + return this.conn.execute.bind(this.conn); + } + + /** For `INSERT`, `UPDATE`, etc. */ + get executeResult() { + this.ensureConnection(); + return this.conn.execute.bind(this.conn); + } + + /** For multiple `INSERT`, `UPDATE`, etc. with `multipleStatements` as `true` */ + get executeResults() { + this.ensureConnection(); + return this.conn.execute.bind(this.conn); + } + + /** Expose the Pool Connection */ + get connection() { + return this.conn; + } +} + +(async () => { + const access: PoolOptions = { + host: '', + user: '', + password: '', + database: '', + }; + + const mysql = new MySQL(access); + + /** Deleting the `users` table, if it exists */ + await mysql.queryResult('DROP TABLE IF EXISTS `users`;'); + + /** Creating a minimal user table */ + await mysql.queryResult( + 'CREATE TABLE `users` (`id` INT(11) AUTO_INCREMENT, `name` VARCHAR(50), PRIMARY KEY (`id`));', + ); + + /** Inserting some users */ + const [inserted] = await mysql.executeResult( + 'INSERT INTO `users`(`name`) VALUES(?), (?), (?), (?);', + ['Josh', 'John', 'Marie', 'Gween'], + ); + + console.log('Inserted:', inserted.affectedRows); + + /** Getting users */ + const [users] = await mysql.queryRows( + 'SELECT * FROM `users` ORDER BY `name` ASC;', + ); + + users.forEach((user: User) => { + console.log('-----------'); + console.log('id: ', user.id); + console.log('name:', user.name); + }); + + await mysql.connection.end(); +})(); + +/** Output + * + * Inserted: 4 + * ----------- + * id: 4 + * name: Gween + * ----------- + * id: 2 + * name: John + * ----------- + * id: 1 + * name: Josh + * ----------- + * id: 3 + * name: Marie + */