Skip to content

Commit

Permalink
docs: add a basic custom MySQL2 class exampleusing TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
wellwelwel committed Jul 8, 2023
1 parent 67ec12b commit b63b94c
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 1 deletion.
2 changes: 1 addition & 1 deletion documentation/en/TypeScript-Examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
141 changes: 141 additions & 0 deletions examples/typescript/baisc-custom-class.ts
Original file line number Diff line number Diff line change
@@ -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)<RowDataPacket[]>;
}

/** For `SELECT` and `SHOW` with `rowAsArray` as `true` */
get queryRowsAsArray() {
this.ensureConnection();
return this.conn.query.bind(this.conn)<RowDataPacket[][]>;
}

/** For `INSERT`, `UPDATE`, etc. */
get queryResult() {
this.ensureConnection();
return this.conn.query.bind(this.conn)<ResultSetHeader>;
}

/** For multiple `INSERT`, `UPDATE`, etc. with `multipleStatements` as `true` */
get queryResults() {
this.ensureConnection();
return this.conn.query.bind(this.conn)<ResultSetHeader[]>;
}

/** For `SELECT` and `SHOW` */
get executeRows() {
this.ensureConnection();
return this.conn.execute.bind(this.conn)<RowDataPacket[]>;
}

/** For `SELECT` and `SHOW` with `rowAsArray` as `true` */
get executeRowsAsArray() {
this.ensureConnection();
return this.conn.execute.bind(this.conn)<RowDataPacket[][]>;
}

/** For `INSERT`, `UPDATE`, etc. */
get executeResult() {
this.ensureConnection();
return this.conn.execute.bind(this.conn)<ResultSetHeader>;
}

/** For multiple `INSERT`, `UPDATE`, etc. with `multipleStatements` as `true` */
get executeResults() {
this.ensureConnection();
return this.conn.execute.bind(this.conn)<ResultSetHeader[]>;
}

/** 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
*/

0 comments on commit b63b94c

Please sign in to comment.