1
0
mirror of https://github.com/matt-fidd/stratos.git synced 2026-01-01 19:59:27 +00:00

Refactored database connection system to use a shared pool per-request

This commit is contained in:
2022-03-23 23:29:55 +00:00
parent bd662661ee
commit 4c2a078530
14 changed files with 162 additions and 144 deletions

View File

@@ -5,7 +5,6 @@ const crypto = require('crypto');
// Import user defined modules
const Class = require('./Class');
const DatabaseConnectionPool = require('./DatabaseConnectionPool');
const Test = require('./Test');
/**
@@ -42,10 +41,14 @@ class TestTemplate {
*/
maxMark;
#conn;
/**
* @param {string} testTemplateId - The id of the template to fetch
*/
constructor(testTemplateId) {
constructor(conn, testTemplateId) {
this.#conn = conn;
const sql = `
select
testTemplateId as id,
@@ -59,13 +62,10 @@ class TestTemplate {
`;
return (async () => {
const conn = await new DatabaseConnectionPool();
const record = await conn.runQuery(sql, [
const record = await this.#conn.runQuery(sql, [
testTemplateId,
]);
conn.close();
if (!record.length)
throw new Error('No test template found');
@@ -79,11 +79,11 @@ class TestTemplate {
}
getAccount() {
return new (require('./Account'))(this.accountId);
return new (require('./Account'))(this.#conn, this.accountId);
}
async assignClass(classId, date) {
const c = await new Class(classId);
const c = await new Class(this.#conn, classId);
const id = crypto.randomUUID();
const epochDate = date.getTime() / 1000;
@@ -97,34 +97,25 @@ class TestTemplate {
(?, ?, ?, FROM_UNIXTIME(?));
`;
const conn = await new DatabaseConnectionPool();
const result = await conn.runQuery(sql, [
await this.#conn.runQuery(sql, [
id,
this.id,
c.id,
epochDate
]);
conn.close();
if (!result.length)
throw new Error('Could not assign class');
return new Test(id);
return new Test(this.#conn, id);
}
get classes() {
}
static async createTestTemplate(accountId, name, maxMark) {
const a = await new (require('./Account'))(accountId);
static async createTestTemplate(conn, accountId, name, maxMark) {
const a = await new (require('./Account'))(conn, accountId);
const id = crypto.randomUUID();
const conn = await new DatabaseConnectionPool();
const sql = `
insert into testTemplate(
testTemplateId,
@@ -142,12 +133,10 @@ class TestTemplate {
maxMark
]);
conn.close();
if (!result)
throw new Error('Could not create test template');
return new TestTemplate(id);
return new TestTemplate(conn, id);
}
}