mirror of
https://github.com/matt-fidd/stratos.git
synced 2026-01-01 20:39:28 +00:00
Migrate existing db connections to use DatabaseConnectionPool
This commit is contained in:
@@ -3,5 +3,8 @@
|
|||||||
"port": 1111,
|
"port": 1111,
|
||||||
"user": "username",
|
"user": "username",
|
||||||
"password": "password",
|
"password": "password",
|
||||||
"database": "dbname"
|
"database": "dbname",
|
||||||
|
"waitForConnections": true,
|
||||||
|
"connectionLimit": 10,
|
||||||
|
"queueLimit": 0
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// Import required modules
|
// Import required modules
|
||||||
const mysql = require('mysql2/promise');
|
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
|
// Import user defined modules
|
||||||
|
const DatabaseConnectionPool =
|
||||||
|
require(path.join(__dirname, '../../lib/DatabaseConnectionPool'));
|
||||||
|
|
||||||
// Initialise maps for table creation and constraint queries
|
// Initialise maps for table creation and constraint queries
|
||||||
const tableCreate = new Map();
|
const tableCreate = new Map();
|
||||||
const tableConstraints = new Map();
|
const tableConstraints = new Map();
|
||||||
@@ -272,25 +275,24 @@ tableConstraints.set('test_fk0', `
|
|||||||
ON UPDATE NO ACTION;
|
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) {
|
async function initialise(dbOptions) {
|
||||||
/*
|
const dbConnectionPool = await new DatabaseConnectionPool(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);
|
|
||||||
|
|
||||||
// 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 c.execute(sql);
|
await dbConnectionPool.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}`);
|
||||||
@@ -304,7 +306,7 @@ async function initialise(dbOptions) {
|
|||||||
console.log(`Creating constraint ${fkName}`);
|
console.log(`Creating constraint ${fkName}`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await c.execute(sql);
|
await dbConnectionPool.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 ' +
|
||||||
@@ -312,20 +314,7 @@ async function initialise(dbOptions) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Drop the database connection
|
await dbConnectionPool.close();
|
||||||
c.end();
|
|
||||||
|
|
||||||
console.log('\nFinished initialising database');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Import data from config/db.json
|
initialise();
|
||||||
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);
|
|
||||||
|
|||||||
@@ -3,9 +3,12 @@
|
|||||||
// Import required modules
|
// Import required modules
|
||||||
const bcrypt = require('bcrypt');
|
const bcrypt = require('bcrypt');
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const mysql = require('mysql2/promise');
|
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
|
// Import user defined modules
|
||||||
|
const DatabaseConnectionPool =
|
||||||
|
require(path.join(__dirname, '../../lib/DatabaseConnectionPool'));
|
||||||
|
|
||||||
// Object storing configurations for the db tables
|
// Object storing configurations for the db tables
|
||||||
const tableDetails = {
|
const tableDetails = {
|
||||||
parent: {
|
parent: {
|
||||||
@@ -281,7 +284,7 @@ const relationships = {
|
|||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|
||||||
async function cleanDb(c) {
|
async function cleanDb(dbConnectionPool) {
|
||||||
/*
|
/*
|
||||||
Cleans the database of any existing records that will
|
Cleans the database of any existing records that will
|
||||||
conflict with the test data
|
conflict with the test data
|
||||||
@@ -303,22 +306,22 @@ async function cleanDb(c) {
|
|||||||
];
|
];
|
||||||
|
|
||||||
for (const table of deletionList)
|
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) {
|
async function insertData(dbOptions) {
|
||||||
/*
|
const dbConnectionPool = await new DatabaseConnectionPool(dbOptions);
|
||||||
Inserts test data into the database defined in dbOptions
|
|
||||||
|
|
||||||
Arguments:
|
await cleanDb(dbConnectionPool);
|
||||||
- database options object loaded in from
|
|
||||||
config/db.json
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Connect to the database
|
|
||||||
const c = await mysql.createConnection(dbOptions);
|
|
||||||
|
|
||||||
await cleanDb(c);
|
|
||||||
|
|
||||||
// Run the creation statment for each table
|
// Run the creation statment for each table
|
||||||
for (const table of Object.keys(data)) {
|
for (const table of Object.keys(data)) {
|
||||||
@@ -354,7 +357,7 @@ async function insertData(dbOptions) {
|
|||||||
console.log(sql);
|
console.log(sql);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await c.execute(sql,
|
await dbConnectionPool.runQuery(sql,
|
||||||
Object.values(dataToInsert));
|
Object.values(dataToInsert));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
@@ -405,7 +408,7 @@ async function insertData(dbOptions) {
|
|||||||
console.log(sql);
|
console.log(sql);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await c.execute(sql,
|
await dbConnectionPool.runQuery(sql,
|
||||||
Object.values(dataToInsert));
|
Object.values(dataToInsert));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
@@ -413,18 +416,7 @@ async function insertData(dbOptions) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Drop the database connection
|
await dbConnectionPool.close();
|
||||||
c.end();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Import data from config/db.json
|
insertData();
|
||||||
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);
|
|
||||||
|
|||||||
Reference in New Issue
Block a user