mirror of
https://github.com/matt-fidd/stratos.git
synced 2026-01-01 22:59:28 +00:00
Split db utility modules down into individual parts
This commit is contained in:
12
gulpfile.js
12
gulpfile.js
@@ -10,10 +10,14 @@ const prompt = require('prompt-sync')({ sigint: true });
|
|||||||
const rename = require('gulp-rename');
|
const rename = require('gulp-rename');
|
||||||
const sass = require('gulp-sass')(require('sass'));
|
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 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
|
// Set src and destination paths for css compilation
|
||||||
const cssPaths = {
|
const cssPaths = {
|
||||||
src: 'src/stylesheets/main.scss',
|
src: 'src/stylesheets/main.scss',
|
||||||
@@ -134,10 +138,10 @@ exports.watchStyles = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Create tables and relationships in database
|
// Create tables and relationships in database
|
||||||
exports.dbInit = dbInit;
|
exports.dbInit = initDb;
|
||||||
|
|
||||||
// Clean all data and insert test data into database
|
// 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
|
// Build stylesheet, and generate config files
|
||||||
exports.default =
|
exports.default =
|
||||||
|
|||||||
15
utility/db/MySQLDate.js
Normal file
15
utility/db/MySQLDate.js
Normal file
@@ -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;
|
||||||
33
utility/db/cleanDb.js
Normal file
33
utility/db/cleanDb.js
Normal file
@@ -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;
|
||||||
@@ -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}
|
* @return {void}
|
||||||
*/
|
*/
|
||||||
async function initialise() {
|
async function dbInit() {
|
||||||
const dbConnectionPool = await new DatabaseConnectionPool();
|
const conn = await new DatabaseConnectionPool();
|
||||||
|
|
||||||
// Run the creation statment for each table
|
// Run the creation statment for each table
|
||||||
for (const [ tableName, sql ] of tableCreate) {
|
for (const [ tableName, sql ] of tableCreate) {
|
||||||
console.log(`Creating table ${tableName}`);
|
console.log(`Creating table ${tableName}`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await dbConnectionPool.runQuery(sql);
|
await conn.runQuery(sql);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
throw new Error(`Unable to create table ${tableName}`);
|
throw new Error(`Unable to create table ${tableName}`);
|
||||||
@@ -274,7 +274,7 @@ async function initialise() {
|
|||||||
console.log(`Creating constraint ${fkName}`);
|
console.log(`Creating constraint ${fkName}`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await dbConnectionPool.runQuery(sql);
|
await conn.runQuery(sql);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
throw new Error('Unable to create constraint ' +
|
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;
|
||||||
88
utility/db/insertTestData.js
Normal file
88
utility/db/insertTestData.js
Normal file
@@ -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;
|
||||||
@@ -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
|
// Import user defined modules
|
||||||
const DatabaseConnectionPool =
|
const MySQLDate = require('./MySQLDate');
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Object storing configurations for the db tables
|
// Object storing configurations for the db tables
|
||||||
const tableDetails = {
|
const details = {
|
||||||
parent: {
|
parent: {
|
||||||
hashPassword: true,
|
hashPassword: true,
|
||||||
id: 'uuid'
|
id: 'uuid'
|
||||||
@@ -226,84 +206,84 @@ const data = {
|
|||||||
],
|
],
|
||||||
test: [
|
test: [
|
||||||
{
|
{
|
||||||
testDate: new mySQLDate(),
|
testDate: new MySQLDate(),
|
||||||
lookups: {
|
lookups: {
|
||||||
classId: 1,
|
classId: 1,
|
||||||
testTemplateId: 1
|
testTemplateId: 1
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testDate: new mySQLDate().alterDays(1),
|
testDate: new MySQLDate().alterDays(1),
|
||||||
lookups: {
|
lookups: {
|
||||||
classId: 2,
|
classId: 2,
|
||||||
testTemplateId: 1
|
testTemplateId: 1
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testDate: new mySQLDate().alterDays(30),
|
testDate: new MySQLDate().alterDays(30),
|
||||||
lookups: {
|
lookups: {
|
||||||
classId: 4,
|
classId: 4,
|
||||||
testTemplateId: 1
|
testTemplateId: 1
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testDate: new mySQLDate().alterDays(-10),
|
testDate: new MySQLDate().alterDays(-10),
|
||||||
lookups: {
|
lookups: {
|
||||||
classId: 1,
|
classId: 1,
|
||||||
testTemplateId: 4
|
testTemplateId: 4
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testDate: new mySQLDate().alterDays(-100),
|
testDate: new MySQLDate().alterDays(-100),
|
||||||
lookups: {
|
lookups: {
|
||||||
classId: 1,
|
classId: 1,
|
||||||
testTemplateId: 2
|
testTemplateId: 2
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testDate: new mySQLDate().alterDays(50),
|
testDate: new MySQLDate().alterDays(50),
|
||||||
lookups: {
|
lookups: {
|
||||||
classId: 4,
|
classId: 4,
|
||||||
testTemplateId: 2
|
testTemplateId: 2
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testDate: new mySQLDate().alterDays(10),
|
testDate: new MySQLDate().alterDays(10),
|
||||||
lookups: {
|
lookups: {
|
||||||
classId: 4,
|
classId: 4,
|
||||||
testTemplateId: 3
|
testTemplateId: 3
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testDate: new mySQLDate().alterDays(-15),
|
testDate: new MySQLDate().alterDays(-15),
|
||||||
lookups: {
|
lookups: {
|
||||||
classId: 3,
|
classId: 3,
|
||||||
testTemplateId: 3
|
testTemplateId: 3
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testDate: new mySQLDate().alterDays(-108),
|
testDate: new MySQLDate().alterDays(-108),
|
||||||
lookups: {
|
lookups: {
|
||||||
classId: 3,
|
classId: 3,
|
||||||
testTemplateId: 3
|
testTemplateId: 3
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testDate: new mySQLDate().alterDays(-4),
|
testDate: new MySQLDate().alterDays(-4),
|
||||||
lookups: {
|
lookups: {
|
||||||
classId: 2,
|
classId: 2,
|
||||||
testTemplateId: 4
|
testTemplateId: 4
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testDate: new mySQLDate().alterDays(-8),
|
testDate: new MySQLDate().alterDays(-8),
|
||||||
lookups: {
|
lookups: {
|
||||||
classId: 3,
|
classId: 3,
|
||||||
testTemplateId: 4
|
testTemplateId: 4
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testDate: new mySQLDate().alterDays(1),
|
testDate: new MySQLDate().alterDays(1),
|
||||||
lookups: {
|
lookups: {
|
||||||
classId: 3,
|
classId: 3,
|
||||||
testTemplateId: 4
|
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 = {
|
module.exports = {
|
||||||
insert: insertData,
|
data: data,
|
||||||
clean: cleanDb
|
details: details
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user