const mysql = require('mysql') var env = process.env.NODE_ENV || 'testing'; const config = require('../config/config')[env] // ==== USER ACOOUNT DB CONNECTION ==== var userConnection = mysql.createConnection({ host: config.database.host, user: config.database.user, password: config.database.password, port: config.database.port, database: config.database.dbUser, multipleStatements: true }) userConnection.connect(function(err) { if (err) throw err; }) userConnection.query('USE '+config.database.dbUser) // user db connection test userConnection.query('SELECT 1 + 5 AS solution', function (err, rows, fields) { if (err) throw err console.log('Solution = ', rows[0].solution) }) //userConnection.end() // ALTERNATIVE approach: close db connection manually after every query /* var dbconn = function dbconn(query, values, next) { var connection = mysql.createConnection({ host: config.database.host, user: config.database.user, password: config.database.password, port: config.database.port, database: config.database.db }) connection.connect(function(err) { if (err) throw err; }) connection.query(query, values, function(err) { connection.end(); // close the connection if (err) { throw err; } // Execute the callback next.apply(this, arguments); }); } */ // ==== PROJECT DB CONNECTION ==== var projectConnection = mysql.createConnection({ host: config.database.host_project, user: config.database.user, password: config.database.password, port: config.database.port, database: config.database.dbProject }) projectConnection.connect(function(err) { if (err) throw err; }) projectConnection.query('USE '+config.database.dbProject) // projectdb connection test projectConnection.query('SELECT 10 + 5 AS project', function (err, rows, fields) { if (err) throw err console.log('Project = ', rows[0].project) }) //projectConnection.end() var connection = { user: userConnection, project: projectConnection } module.exports = connection