From 19712a5ff34622b75b6af5f073d78a8b9667be72 Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Fri, 31 Jul 2020 17:54:14 -0400 Subject: [PATCH] Create initial MySql helper class Currently only creates and closes connections. --- app.js | 13 +++++++-- package.json | 4 ++- src/config.js | 11 ++++---- src/mysql.js | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 src/mysql.js diff --git a/app.js b/app.js index 8db8fd5..e0d8d69 100644 --- a/app.js +++ b/app.js @@ -2,8 +2,12 @@ * NodeJS application to connect to MySQL and make database queries. */ +// System Imports. +// const mysql = require('mysql2'); + // User Imports. -import config from './src/config.js'; +const config = require('./src/config.js'); +const MySql = require('./src/mysql.js'); /** @@ -13,7 +17,12 @@ function main() { console.log('Starting program.'); console.log(''); - console.log(config['mysql']); + var debug = true; + + // Create MySql helper class. + const mysql = new MySql(config['mysql'], debug); + mysql.open_conn(); + mysql.close_conn(); console.log(''); console.log('Terminating program.'); diff --git a/package.json b/package.json index 8d3487b..a15068b 100644 --- a/package.json +++ b/package.json @@ -6,5 +6,7 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, - "type": "module" + "dependencies": { + "mysql2": "^2.1.0" + } } diff --git a/src/config.js b/src/config.js index 1df93d8..e5e44af 100644 --- a/src/config.js +++ b/src/config.js @@ -5,12 +5,13 @@ var config = { mysql: { - user: 'root', - password: 'root', - database: 'js_test', host: '127.0.0.1', - port: '3306' + port: '3306', + database: 'mysql_bot_example', + user: 'root', + password: 'root' } } -export default config; + +module.exports = config; diff --git a/src/mysql.js b/src/mysql.js new file mode 100644 index 0000000..52de956 --- /dev/null +++ b/src/mysql.js @@ -0,0 +1,77 @@ +/** + * Helper class to handle MySql logic. + */ + +// System Imports. +const mysql = require('mysql2'); + + +class MySql { + + /** + * Class constructor. + */ + constructor(config, debug) { + if (debug) { + console.log('Starting MySql helper class.'); + } + + // Save class variables. + this.config = config; + this.debug = debug; + this.connection = null; + } + + /** + * Creates and returns a MySql connection object, using provided config values. + */ + open_conn() { + if (this.debug) { + console.log('Creating MySql connection...'); + } + + // Check if connection is currently open. + if (this.connection != null) { + // Connection currently exists. Close so we can start a new one. + console.log('Connection already exist. Closing old connection first.'); + this.close_conn(); + } + + // Open MySQL connection. + const connection = mysql.createConnection({ + host: this.config['host'], + port: this.config['port'], + database: this.config['database'], + user: this.config['user'], + password: this.config['password'] + }); + console.log('Connection has been created.'); + + // Save connection to class. + this.connection =connection; + + return connection; + } + + /** + * Closes current MySql connection. + */ + close_conn() { + if (this.debug) { + console.log('Closing MySql connection...'); + } + + // Check if connection is currently open. + if (this.connection != null) { + // Connection currently exists. Safe to close. + this.connection.end(); + console.log('Connection has been terminated.'); + } else { + // Connection does not yet exist. Cannot close. + console.log('There is no connection to close.'); + } + } +} + + +module.exports = MySql; -- GitLab