mirror of
https://github.com/matt-fidd/stratos.git
synced 2026-01-01 16:19:26 +00:00
Migrate existing db connections to use DatabaseConnectionPool
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
'use strict';
|
||||
|
||||
// Import required modules
|
||||
const mysql = require('mysql2/promise');
|
||||
const path = require('path');
|
||||
|
||||
// Import user defined modules
|
||||
const DatabaseConnectionPool =
|
||||
require(path.join(__dirname, '../../lib/DatabaseConnectionPool'));
|
||||
|
||||
// Initialise maps for table creation and constraint queries
|
||||
const tableCreate = new Map();
|
||||
const tableConstraints = new Map();
|
||||
@@ -272,25 +275,24 @@ tableConstraints.set('test_fk0', `
|
||||
ON UPDATE NO ACTION;
|
||||
`);
|
||||
|
||||
/**
|
||||
* initialise() Initialises a database and applies the stratos schema to it
|
||||
*
|
||||
* @param {object} [dbOptions]
|
||||
* - An object in the form found in config/db.json supplying the db to
|
||||
* connect to
|
||||
*
|
||||
* @return {void}
|
||||
*/
|
||||
async function initialise(dbOptions) {
|
||||
/*
|
||||
Initialises a database using the options object provided
|
||||
and applies schema to it
|
||||
|
||||
Arguments:
|
||||
- database options object loaded in from
|
||||
config/db.json
|
||||
*/
|
||||
|
||||
// Connect to the database
|
||||
const c = await mysql.createConnection(dbOptions);
|
||||
const dbConnectionPool = await new DatabaseConnectionPool(dbOptions);
|
||||
|
||||
// Run the creation statment for each table
|
||||
for (const [ tableName, sql ] of tableCreate) {
|
||||
console.log(`Creating table ${tableName}`);
|
||||
|
||||
try {
|
||||
await c.execute(sql);
|
||||
await dbConnectionPool.runQuery(sql);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
throw new Error(`Unable to create table ${tableName}`);
|
||||
@@ -304,7 +306,7 @@ async function initialise(dbOptions) {
|
||||
console.log(`Creating constraint ${fkName}`);
|
||||
|
||||
try {
|
||||
await c.execute(sql);
|
||||
await dbConnectionPool.runQuery(sql);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
throw new Error('Unable to create constraint ' +
|
||||
@@ -312,20 +314,7 @@ async function initialise(dbOptions) {
|
||||
}
|
||||
}
|
||||
|
||||
// Drop the database connection
|
||||
c.end();
|
||||
|
||||
console.log('\nFinished initialising database');
|
||||
await dbConnectionPool.close();
|
||||
}
|
||||
|
||||
// Import data from config/db.json
|
||||
let dbOptions;
|
||||
try {
|
||||
dbOptions = require(path.join(__dirname, '../../config/db.json'));
|
||||
console.log('DB config loaded\n');
|
||||
} catch {
|
||||
return console.error('Missing or malformed config ' +
|
||||
'(config/db.json)');
|
||||
}
|
||||
|
||||
initialise(dbOptions);
|
||||
initialise();
|
||||
|
||||
@@ -3,9 +3,12 @@
|
||||
// Import required modules
|
||||
const bcrypt = require('bcrypt');
|
||||
const crypto = require('crypto');
|
||||
const mysql = require('mysql2/promise');
|
||||
const path = require('path');
|
||||
|
||||
// Import user defined modules
|
||||
const DatabaseConnectionPool =
|
||||
require(path.join(__dirname, '../../lib/DatabaseConnectionPool'));
|
||||
|
||||
// Object storing configurations for the db tables
|
||||
const tableDetails = {
|
||||
parent: {
|
||||
@@ -281,7 +284,7 @@ const relationships = {
|
||||
]
|
||||
};
|
||||
|
||||
async function cleanDb(c) {
|
||||
async function cleanDb(dbConnectionPool) {
|
||||
/*
|
||||
Cleans the database of any existing records that will
|
||||
conflict with the test data
|
||||
@@ -303,22 +306,22 @@ async function cleanDb(c) {
|
||||
];
|
||||
|
||||
for (const table of deletionList)
|
||||
await c.execute(`DELETE FROM ${table};`);
|
||||
await dbConnectionPool.runQuery(`DELETE FROM ${table};`);
|
||||
}
|
||||
|
||||
/**
|
||||
* insertData() Inserts test data into a database
|
||||
*
|
||||
* @param {object} [dbOptions]
|
||||
* - An object in the form found in config/db.json supplying the db to
|
||||
* connect to
|
||||
*
|
||||
* @return {void}
|
||||
*/
|
||||
async function insertData(dbOptions) {
|
||||
/*
|
||||
Inserts test data into the database defined in dbOptions
|
||||
const dbConnectionPool = await new DatabaseConnectionPool(dbOptions);
|
||||
|
||||
Arguments:
|
||||
- database options object loaded in from
|
||||
config/db.json
|
||||
*/
|
||||
|
||||
// Connect to the database
|
||||
const c = await mysql.createConnection(dbOptions);
|
||||
|
||||
await cleanDb(c);
|
||||
await cleanDb(dbConnectionPool);
|
||||
|
||||
// Run the creation statment for each table
|
||||
for (const table of Object.keys(data)) {
|
||||
@@ -354,7 +357,7 @@ async function insertData(dbOptions) {
|
||||
console.log(sql);
|
||||
|
||||
try {
|
||||
await c.execute(sql,
|
||||
await dbConnectionPool.runQuery(sql,
|
||||
Object.values(dataToInsert));
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
@@ -405,7 +408,7 @@ async function insertData(dbOptions) {
|
||||
console.log(sql);
|
||||
|
||||
try {
|
||||
await c.execute(sql,
|
||||
await dbConnectionPool.runQuery(sql,
|
||||
Object.values(dataToInsert));
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
@@ -413,18 +416,7 @@ async function insertData(dbOptions) {
|
||||
}
|
||||
}
|
||||
|
||||
// Drop the database connection
|
||||
c.end();
|
||||
await dbConnectionPool.close();
|
||||
}
|
||||
|
||||
// Import data from config/db.json
|
||||
let dbOptions;
|
||||
try {
|
||||
dbOptions = require(path.join(__dirname, '../../config/db.json'));
|
||||
console.log('DB config loaded\n');
|
||||
} catch {
|
||||
return console.error('Missing or malformed config ' +
|
||||
'(config/db.json)');
|
||||
}
|
||||
|
||||
insertData(dbOptions);
|
||||
insertData();
|
||||
|
||||
Reference in New Issue
Block a user