diff --git a/app.js b/app.js index 8db8fd5edad7c239deae6f1ef4e26318fbb26daf..e0d8d69ddc7e1dea1b400b6434f970eb1d15491b 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 8d3487b11324cb48324d8f5006f60e5123fdb802..a15068b144960a9cb6fc5335bf6ba0e4a1bdeb44 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 1df93d8bf6e1a52d42c0bfc207cd3ff95d899a13..e5e44af27c37bbf75908e62f22438b0a79748b5a 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 0000000000000000000000000000000000000000..52de9567e50d3d9b781a18c9a87c8d9888ccad30 --- /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;