Last active
June 24, 2019 10:50
-
-
Save Ratstail91/dc50ce11a9f4408f6ce08c3f69865c76 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//environment variables | |
require('dotenv').config(); | |
//libraries | |
let mysql = require('mysql'); | |
//utilities | |
let { log } = require('./logging.js'); | |
let connection; | |
let connectionWrapper = { //use a wrapper that will always point to the correct database object | |
query: (sql, args) => new Promise( (resolve, reject) => { | |
connection.query(sql, args, (err, rows) => { | |
if (err) return reject(err); | |
return resolve(rows); | |
}); | |
}), | |
close: () => connection.close(), | |
unwrap: () => connection | |
}; | |
const handleDisconnect = () => { | |
//use the config | |
connection = mysql.createConnection({ | |
host: process.env.DB_HOST, | |
user: process.env.DB_USER, | |
password: process.env.DB_PASS, | |
database: process.env.DB_NAME, | |
port: process.env.DB_PORT | |
}); | |
//connect | |
connection.connect((err) => { | |
if (err) { | |
log('Error connecting to mysql: ', err); | |
setTimeout(handleDisconnect, 2000); | |
} else { | |
log('Connected to mysql'); | |
} | |
}); | |
//prepare for failure | |
connection.on('error', (err) => { | |
log('mysql error: ', err); | |
if (err.code === 'PROTOCOL_CONNECTION_LOST') { | |
handleDisconnect(); | |
} else { | |
throw (err); | |
} | |
}); | |
//finally | |
return connectionWrapper; | |
}; | |
module.exports = handleDisconnect; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mysqlMock = { | |
createConnection(config) { | |
return { | |
//the mock connection | |
connected: false, | |
lastQuery: '', | |
nextResult: null, | |
connect(cb) { | |
if (this.connected) { | |
throw 'mysqlMock already connected'; | |
} | |
this.connected = true; | |
if (cb) { | |
cb(); | |
} | |
}, | |
on(msg, cb) { | |
//DO NOTHING | |
}, | |
query(query, params, cb) { | |
if (!this.connected) { | |
throw 'mysqlMock not connected'; | |
} | |
//So much for default arguments in node | |
if (!Array.isArray(params)) { | |
cb = params; | |
params = null; | |
} | |
//do the mysql substitution | |
let counter = 0; | |
this.lastQuery = query.replace(/\?/g, () => `${params[counter++]}`); | |
//no promises afterall | |
if (cb) { | |
cb(undefined, this.nextResult); | |
} | |
}, | |
close() { | |
if (!this.connected) { | |
throw 'mysqlMock not connected'; | |
} | |
this.connected = false; | |
} | |
}; | |
} | |
}; | |
module.exports = mysqlMock; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment