mirror of
https://github.com/matt-fidd/stratos.git
synced 2026-01-01 22:59:28 +00:00
Move db utility modules from self-contained to importable
This commit is contained in:
@@ -13,7 +13,6 @@ const tableConstraints = new Map();
|
|||||||
|
|
||||||
// For each table, set tableCreate.tableName equal to the creation statment
|
// For each table, set tableCreate.tableName equal to the creation statment
|
||||||
// for that table
|
// for that table
|
||||||
|
|
||||||
tableCreate.set('account', `
|
tableCreate.set('account', `
|
||||||
CREATE TABLE IF NOT EXISTS account (
|
CREATE TABLE IF NOT EXISTS account (
|
||||||
accountId varchar(36) NOT NULL PRIMARY KEY,
|
accountId varchar(36) NOT NULL PRIMARY KEY,
|
||||||
@@ -251,14 +250,10 @@ tableConstraints.set('test_fk0', `
|
|||||||
/**
|
/**
|
||||||
* initialise() Initialises a database and applies the stratos schema to it
|
* 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}
|
* @return {void}
|
||||||
*/
|
*/
|
||||||
async function initialise(dbOptions) {
|
async function initialise() {
|
||||||
const dbConnectionPool = await new DatabaseConnectionPool(dbOptions);
|
const dbConnectionPool = 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) {
|
||||||
@@ -290,4 +285,4 @@ async function initialise(dbOptions) {
|
|||||||
await dbConnectionPool.close();
|
await dbConnectionPool.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
initialise();
|
module.exports = initialise;
|
||||||
|
|||||||
@@ -478,33 +478,29 @@ const data = {
|
|||||||
* cleanDb() Removes all records from the tables in the database to be inserted
|
* cleanDb() Removes all records from the tables in the database to be inserted
|
||||||
* into
|
* into
|
||||||
*
|
*
|
||||||
* @param {dbConnectionPool} [dbConnectionPool] - The database connection
|
|
||||||
*
|
|
||||||
* @return {void}
|
* @return {void}
|
||||||
*/
|
*/
|
||||||
async function cleanDb(dbConnectionPool) {
|
async function cleanDb() {
|
||||||
|
const conn = await new DatabaseConnectionPool();
|
||||||
|
|
||||||
// Remove records from tables in reverse order to which they depend on
|
// Remove records from tables in reverse order to which they depend on
|
||||||
// each other
|
// each other
|
||||||
const tables = Object.keys(data).reverse();
|
const tables = Object.keys(data).reverse();
|
||||||
tables.push('sessions');
|
tables.push('sessions');
|
||||||
|
|
||||||
for (const table of tables)
|
for (const table of tables)
|
||||||
await dbConnectionPool.runQuery(`DELETE FROM ${table};`);
|
await conn.runQuery(`DELETE FROM ${table};`);
|
||||||
|
|
||||||
|
conn.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* insertData() Inserts test data into a database
|
* 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}
|
* @return {void}
|
||||||
*/
|
*/
|
||||||
async function insertData(dbOptions) {
|
async function insertData() {
|
||||||
const dbConnectionPool = await new DatabaseConnectionPool(dbOptions);
|
const conn = await new DatabaseConnectionPool();
|
||||||
|
|
||||||
await cleanDb(dbConnectionPool);
|
|
||||||
|
|
||||||
// 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)) {
|
||||||
@@ -558,7 +554,7 @@ async function insertData(dbOptions) {
|
|||||||
console.log(sql.trim());
|
console.log(sql.trim());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await dbConnectionPool.runQuery(sql,
|
await conn.runQuery(sql,
|
||||||
Object.values(dataToInsert));
|
Object.values(dataToInsert));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
@@ -568,7 +564,10 @@ async function insertData(dbOptions) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await dbConnectionPool.close();
|
conn.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
insertData();
|
module.exports = {
|
||||||
|
insert: insertData,
|
||||||
|
clean: cleanDb
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user