mirror of
https://github.com/matt-fidd/stratos.git
synced 2026-01-01 17:59:25 +00:00
Refactored database connection system to use a shared pool per-request
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
const DatabaseConnectionPool = require('./DatabaseConnectionPool');
|
||||
|
||||
class Subject {
|
||||
/**
|
||||
* The id of the subject
|
||||
@@ -15,10 +13,14 @@ class Subject {
|
||||
*/
|
||||
name;
|
||||
|
||||
#conn;
|
||||
|
||||
/**
|
||||
* @param {number} subjectID - The id of the subject to fetch
|
||||
*/
|
||||
constructor(subjectId) {
|
||||
constructor(conn, subjectId) {
|
||||
this.#conn = conn;
|
||||
|
||||
const sql = `
|
||||
select
|
||||
subjectId as id,
|
||||
@@ -30,13 +32,10 @@ class Subject {
|
||||
`;
|
||||
|
||||
return (async () => {
|
||||
const conn = await new DatabaseConnectionPool();
|
||||
const record = await conn.runQuery(sql, [
|
||||
const record = await this.#conn.runQuery(sql, [
|
||||
subjectId,
|
||||
]);
|
||||
|
||||
conn.close();
|
||||
|
||||
if (!record.length)
|
||||
throw new Error('No subject found');
|
||||
|
||||
@@ -47,7 +46,7 @@ class Subject {
|
||||
})();
|
||||
}
|
||||
|
||||
static async getAllSubjects() {
|
||||
static async getAllSubjects(conn) {
|
||||
const sql = `
|
||||
select
|
||||
subjectId as id
|
||||
@@ -55,14 +54,12 @@ class Subject {
|
||||
subject;
|
||||
`;
|
||||
|
||||
const conn = await new DatabaseConnectionPool();
|
||||
const records = await conn.runQuery(sql);
|
||||
conn.close();
|
||||
|
||||
const objectPromises = [];
|
||||
|
||||
records.forEach(record => {
|
||||
objectPromises.push(new Subject(record.id));
|
||||
objectPromises.push(new Subject(conn, record.id));
|
||||
});
|
||||
|
||||
const objects = await Promise.all(objectPromises);
|
||||
|
||||
Reference in New Issue
Block a user