From 59467b8bde670ee8b094259259c695656fb9c85a Mon Sep 17 00:00:00 2001 From: matt Date: Sun, 27 Feb 2022 01:12:55 +0000 Subject: [PATCH] Split db utility modules down into individual parts --- gulpfile.js | 12 +- utility/db/MySQLDate.js | 15 +++ utility/db/cleanDb.js | 33 +++++ utility/db/{dbInit.js => initDb.js} | 14 +-- utility/db/insertTestData.js | 88 +++++++++++++ utility/db/{dbTestData.js => testData.js} | 145 +++------------------- 6 files changed, 167 insertions(+), 140 deletions(-) create mode 100644 utility/db/MySQLDate.js create mode 100644 utility/db/cleanDb.js rename utility/db/{dbInit.js => initDb.js} (96%) create mode 100644 utility/db/insertTestData.js rename utility/db/{dbTestData.js => testData.js} (64%) diff --git a/gulpfile.js b/gulpfile.js index 571b16d..8d3f9cc 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -10,10 +10,14 @@ const prompt = require('prompt-sync')({ sigint: true }); const rename = require('gulp-rename'); const sass = require('gulp-sass')(require('sass')); -const dbInit = require(path.join(__dirname, 'utility', 'db', 'dbInit')); -const dbTestData = require(path.join(__dirname, 'utility', 'db', 'dbTestData')); const importJSON = require(path.join(__dirname, 'lib', 'importJSON')); +const dbModule = (name) => path.join(__dirname, 'utility/db', name); + +const cleanDb = require(dbModule('cleanDb')); +const initDb = require(dbModule('initDb')); +const insertTestData = require(dbModule('insertTestData')); + // Set src and destination paths for css compilation const cssPaths = { src: 'src/stylesheets/main.scss', @@ -134,10 +138,10 @@ exports.watchStyles = () => { }; // Create tables and relationships in database -exports.dbInit = dbInit; +exports.dbInit = initDb; // Clean all data and insert test data into database -exports.dbTestData = series(dbTestData.clean, dbTestData.insert); +exports.dbTestData = series(initDb, cleanDb, insertTestData); // Build stylesheet, and generate config files exports.default = diff --git a/utility/db/MySQLDate.js b/utility/db/MySQLDate.js new file mode 100644 index 0000000..6a71975 --- /dev/null +++ b/utility/db/MySQLDate.js @@ -0,0 +1,15 @@ +'use strict'; + +// Class for easy insertion of test dates into the database +class MySQLDate extends Date { + toString() { + return this.toISOString().slice(0, 19).replace('T', ' '); + } + + alterDays(amount) { + this.setDate(this.getDate() + amount); + return this; + } +} + +module.exports = MySQLDate; diff --git a/utility/db/cleanDb.js b/utility/db/cleanDb.js new file mode 100644 index 0000000..a2f8905 --- /dev/null +++ b/utility/db/cleanDb.js @@ -0,0 +1,33 @@ +'use strict'; + +// Import required modules +const path = require('path'); + +// Import user defined modules +const DatabaseConnectionPool = + require(path.join(__dirname, '../../lib/DatabaseConnectionPool')); + +// Import test data object +const { data } = require('./testData'); + +/** + * cleanDb() Removes all records from the tables in the database to be inserted + * into + * + * @return {void} + */ +async function cleanDb() { + const conn = await new DatabaseConnectionPool(); + + // Remove records from tables in reverse order to which they depend on + // each other + const tables = Object.keys(data).reverse(); + tables.push('sessions'); + + for (const table of tables) + await conn.runQuery(`DELETE FROM ${table};`); + + conn.close(); +} + +module.exports = cleanDb; diff --git a/utility/db/dbInit.js b/utility/db/initDb.js similarity index 96% rename from utility/db/dbInit.js rename to utility/db/initDb.js index a87cd6f..db1150c 100644 --- a/utility/db/dbInit.js +++ b/utility/db/initDb.js @@ -248,19 +248,19 @@ tableConstraints.set('test_fk0', ` `); /** - * initialise() Initialises a database and applies the stratos schema to it + * dbInit() Initialises a database and applies the stratos schema to it * * @return {void} */ -async function initialise() { - const dbConnectionPool = await new DatabaseConnectionPool(); +async function dbInit() { + const conn = await new DatabaseConnectionPool(); // Run the creation statment for each table for (const [ tableName, sql ] of tableCreate) { console.log(`Creating table ${tableName}`); try { - await dbConnectionPool.runQuery(sql); + await conn.runQuery(sql); } catch (e) { console.error(e); throw new Error(`Unable to create table ${tableName}`); @@ -274,7 +274,7 @@ async function initialise() { console.log(`Creating constraint ${fkName}`); try { - await dbConnectionPool.runQuery(sql); + await conn.runQuery(sql); } catch (e) { console.error(e); throw new Error('Unable to create constraint ' + @@ -282,7 +282,7 @@ async function initialise() { } } - await dbConnectionPool.close(); + await conn.close(); } -module.exports = initialise; +module.exports = dbInit; diff --git a/utility/db/insertTestData.js b/utility/db/insertTestData.js new file mode 100644 index 0000000..221b4bf --- /dev/null +++ b/utility/db/insertTestData.js @@ -0,0 +1,88 @@ +'use strict'; + +// Import required modules +const crypto = require('crypto'); +const bcrypt = require('bcrypt'); +const path = require('path'); + +// Import user defined modules +const DatabaseConnectionPool = + require(path.join(__dirname, '../../lib/DatabaseConnectionPool')); + +// Import test data object +const { data, details } = require('./testData'); + +/** + * insertData() Inserts test data into a database + * + * @return {void} + */ +async function insertTestData() { + const conn = await new DatabaseConnectionPool(); + + // Run the creation statment for each table + for (const table of Object.keys(data)) { + let counter = 0; + + for (const record of data[table]) { + const dataToInsert = { ...record }; + + if (details?.[table]?.['id'] === 'uuid') { + dataToInsert[`${table}Id`] = + crypto.randomUUID(); + } else { + dataToInsert[`${table}Id`] = counter + 1; + } + + if (details?.[table]?.['hashPassword']) { + dataToInsert['password'] = + await bcrypt.hash( + dataToInsert['password'], + 10); + } + + if (details?.[table]?.['link']) + delete dataToInsert[`${table}Id`]; + + if (record?.lookups) { + delete dataToInsert.lookups; + + for (let [ key, index ] of + Object.entries(record.lookups)) { + + const resolveTable = key.split('Id')[0]; + index--; + + dataToInsert[key] = + data[resolveTable][index][key]; + } + } + + data[table][counter] = dataToInsert; + + const qs = '?, '.repeat(Object.keys( + dataToInsert).length).slice(0, -2); + + const sql = ` + insert into ${table} ` + + `(${Object.keys(dataToInsert)})` + + ` values ` + + `(${qs});`; + + console.log(sql.trim()); + + try { + await conn.runQuery(sql, + Object.values(dataToInsert)); + } catch (e) { + console.error(e); + } + + counter++; + } + } + + conn.close(); +} + +module.exports = insertTestData; diff --git a/utility/db/dbTestData.js b/utility/db/testData.js similarity index 64% rename from utility/db/dbTestData.js rename to utility/db/testData.js index fbef2b4..c3e2acf 100644 --- a/utility/db/dbTestData.js +++ b/utility/db/testData.js @@ -1,28 +1,8 @@ -'use strict'; - -// Import required modules -const bcrypt = require('bcrypt'); -const crypto = require('crypto'); -const path = require('path'); - // Import user defined modules -const DatabaseConnectionPool = - require(path.join(__dirname, '../../lib/DatabaseConnectionPool')); - -// Class for easy insertion of test dates into the database -class mySQLDate extends Date { - toString() { - return this.toISOString().slice(0, 19).replace('T', ' '); - } - - alterDays(amount) { - this.setDate(this.getDate() + amount); - return this; - } -} +const MySQLDate = require('./MySQLDate'); // Object storing configurations for the db tables -const tableDetails = { +const details = { parent: { hashPassword: true, id: 'uuid' @@ -226,84 +206,84 @@ const data = { ], test: [ { - testDate: new mySQLDate(), + testDate: new MySQLDate(), lookups: { classId: 1, testTemplateId: 1 } }, { - testDate: new mySQLDate().alterDays(1), + testDate: new MySQLDate().alterDays(1), lookups: { classId: 2, testTemplateId: 1 } }, { - testDate: new mySQLDate().alterDays(30), + testDate: new MySQLDate().alterDays(30), lookups: { classId: 4, testTemplateId: 1 } }, { - testDate: new mySQLDate().alterDays(-10), + testDate: new MySQLDate().alterDays(-10), lookups: { classId: 1, testTemplateId: 4 } }, { - testDate: new mySQLDate().alterDays(-100), + testDate: new MySQLDate().alterDays(-100), lookups: { classId: 1, testTemplateId: 2 } }, { - testDate: new mySQLDate().alterDays(50), + testDate: new MySQLDate().alterDays(50), lookups: { classId: 4, testTemplateId: 2 } }, { - testDate: new mySQLDate().alterDays(10), + testDate: new MySQLDate().alterDays(10), lookups: { classId: 4, testTemplateId: 3 } }, { - testDate: new mySQLDate().alterDays(-15), + testDate: new MySQLDate().alterDays(-15), lookups: { classId: 3, testTemplateId: 3 } }, { - testDate: new mySQLDate().alterDays(-108), + testDate: new MySQLDate().alterDays(-108), lookups: { classId: 3, testTemplateId: 3 } }, { - testDate: new mySQLDate().alterDays(-4), + testDate: new MySQLDate().alterDays(-4), lookups: { classId: 2, testTemplateId: 4 } }, { - testDate: new mySQLDate().alterDays(-8), + testDate: new MySQLDate().alterDays(-8), lookups: { classId: 3, testTemplateId: 4 } }, { - testDate: new mySQLDate().alterDays(1), + testDate: new MySQLDate().alterDays(1), lookups: { classId: 3, testTemplateId: 4 @@ -474,100 +454,7 @@ const data = { ] }; -/** - * cleanDb() Removes all records from the tables in the database to be inserted - * into - * - * @return {void} - */ -async function cleanDb() { - const conn = await new DatabaseConnectionPool(); - - // Remove records from tables in reverse order to which they depend on - // each other - const tables = Object.keys(data).reverse(); - tables.push('sessions'); - - for (const table of tables) - await conn.runQuery(`DELETE FROM ${table};`); - - conn.close(); -} - -/** - * insertData() Inserts test data into a database - * - * @return {void} - */ -async function insertData() { - const conn = await new DatabaseConnectionPool(); - - // Run the creation statment for each table - for (const table of Object.keys(data)) { - let counter = 0; - - for (const record of data[table]) { - const dataToInsert = { ...record }; - - if (tableDetails?.[table]?.['id'] === 'uuid') { - dataToInsert[`${table}Id`] = - crypto.randomUUID(); - } else { - dataToInsert[`${table}Id`] = counter + 1; - } - - if (tableDetails?.[table]?.['hashPassword']) { - dataToInsert['password'] = - await bcrypt.hash( - dataToInsert['password'], - 10); - } - - if (tableDetails?.[table]?.['link']) - delete dataToInsert[`${table}Id`]; - - if (record?.lookups) { - delete dataToInsert.lookups; - - for (let [ key, index ] of - Object.entries(record.lookups)) { - - const resolveTable = key.split('Id')[0]; - index--; - - dataToInsert[key] = - data[resolveTable][index][key]; - } - } - - data[table][counter] = dataToInsert; - - const qs = '?, '.repeat(Object.keys( - dataToInsert).length).slice(0, -2); - - const sql = ` - insert into ${table} ` + - `(${Object.keys(dataToInsert)})` + - ` values ` + - `(${qs});`; - - console.log(sql.trim()); - - try { - await conn.runQuery(sql, - Object.values(dataToInsert)); - } catch (e) { - console.error(e); - } - - counter++; - } - } - - conn.close(); -} - module.exports = { - insert: insertData, - clean: cleanDb + data: data, + details: details };