Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
8463645-6003CEM-Backend-Resit-Deferral/docs/jsdocs/helpers_database.js.html
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
101 lines (78 sloc)
3.58 KB
This file contains 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>JSDoc: Source: helpers/database.js</title> | |
<script src="scripts/prettify/prettify.js"> </script> | |
<script src="scripts/prettify/lang-css.js"> </script> | |
<!--[if lt IE 9]> | |
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> | |
<![endif]--> | |
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> | |
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> | |
</head> | |
<body> | |
<div id="main"> | |
<h1 class="page-title">Source: helpers/database.js</h1> | |
<section> | |
<article> | |
<pre class="prettyprint source linenums"><code>/** | |
* A module to run SQL queries on MySQL on behalf of the API models. | |
* @module helpers/database | |
* @author Miltos Skondras | |
* @see models/* for the models that require this module | |
*/ | |
const mysql = require('promise-mysql'); | |
const info = require('../config'); | |
const { v4: uuidv4 } = require('uuid'); | |
/** | |
* Run an SQL query against the DB, end the connection and return the result. | |
* @param {string} Query SQL query string in sqljs format | |
* @param {array|number|string} values The values to inject in to the query string. | |
* @returns {object} mysqljs results object containing indexable rows | |
* @throws {DatabaseException} Custom exception for DB query failures | |
*/ | |
exports.run_query = async function run_query(query, values) { | |
try { | |
const connection = await mysql.createConnection(info.config); | |
const data = await connection.query(query, values); | |
await connection.end(); | |
return data; | |
} catch (error) { | |
/** | |
* Don't let unknown errors propagate up to the response object | |
* as it may contain sensitive server information. | |
* Instead log it somehow and throw a generic error. | |
*/ | |
const errorId = uuidv4(); | |
console.error(Date.now(), errorId, query, values, error.message); | |
throw new DatabaseException("Database error.", error.code, errorId); | |
} | |
} | |
/** | |
* A custom error constructor to re-raise DB errors in a sanitised way. | |
* @class | |
* @param {string} message - the error message | |
* @param {number|string} code - the original error's error code | |
* @param {string} id - a UUID identifier for the error instanced | |
*/ | |
function DatabaseException(message, code, id) { | |
this.message = message; | |
this.code = code; | |
this.id = id; | |
this.name = 'DatabaseException'; | |
}</code></pre> | |
</article> | |
</section> | |
</div> | |
<nav> | |
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-controllers_auth.html">controllers/auth</a></li><li><a href="module-controllers_validation.html">controllers/validation</a></li><li><a href="module-helpers_database.html">helpers/database</a></li><li><a href="module-models_applications.html">models/applications</a></li><li><a href="module-models_users.html">models/users</a></li><li><a href="module-permissions_applications.html">permissions/applications</a></li><li><a href="module-permissions_users.html">permissions/users</a></li><li><a href="module-routes_applications.html">routes/applications</a></li><li><a href="module-routes_users.html">routes/users</a></li><li><a href="module-strategies_basic.html">strategies/basic</a></li></ul><h3>Classes</h3><ul><li><a href="module-helpers_database-DatabaseException.html">DatabaseException</a></li></ul> | |
</nav> | |
<br class="clear"> | |
<footer> | |
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a> on Sun Nov 28 2021 19:50:25 GMT+0000 (GMT) | |
</footer> | |
<script> prettyPrint(); </script> | |
<script src="scripts/linenumber.js"> </script> | |
</body> | |
</html> |