mirror of
https://github.com/matt-fidd/stratos.git
synced 2026-01-01 20:39:28 +00:00
Refactored database connection system to use a shared pool per-request
This commit is contained in:
@@ -1,15 +1,13 @@
|
||||
/* eslint-disable no-empty-function, getter-return */
|
||||
'use strict';
|
||||
|
||||
const DatabaseConnectionPool = require('./DatabaseConnectionPool');
|
||||
|
||||
const Class = require('./Class');
|
||||
const TestTemplate = require('./TestTemplate');
|
||||
const User = require('./User');
|
||||
|
||||
class Account extends User {
|
||||
constructor(id) {
|
||||
super('account', id);
|
||||
constructor(conn, id) {
|
||||
super(conn, 'account', id);
|
||||
}
|
||||
|
||||
async getTestTemplates() {
|
||||
@@ -22,11 +20,10 @@ class Account extends User {
|
||||
accountId = ?;
|
||||
`;
|
||||
|
||||
const conn = await new DatabaseConnectionPool();
|
||||
const records = await conn.runQuery(sql, [ this.id ]);
|
||||
const records = await this._conn.runQuery(sql, [ this.id ]);
|
||||
|
||||
const promises = records.map(record => {
|
||||
return new TestTemplate(record.id);
|
||||
return new TestTemplate(this._conn, record.id);
|
||||
});
|
||||
|
||||
const objects = await Promise.all(promises);
|
||||
@@ -36,17 +33,19 @@ class Account extends User {
|
||||
|
||||
createTestTemplate(name, maxMark) {
|
||||
return TestTemplate.createTestTemplate(
|
||||
this._conn,
|
||||
this.id,
|
||||
name,
|
||||
maxMark);
|
||||
}
|
||||
|
||||
createClass(name, subjectId) {
|
||||
return Class.createClass(this.id, name, subjectId);
|
||||
return Class.createClass(this._conn, this.id, name, subjectId);
|
||||
}
|
||||
|
||||
static async createAccount(fname, oname, lname, email, password) {
|
||||
static async createAccount(conn, fname, oname, lname, email, password) {
|
||||
return await super.createUser(
|
||||
conn,
|
||||
'account',
|
||||
fname,
|
||||
oname,
|
||||
|
||||
38
lib/Class.js
38
lib/Class.js
@@ -3,8 +3,6 @@
|
||||
|
||||
const crypto = require('crypto');
|
||||
|
||||
const DatabaseConnectionPool = require('./DatabaseConnectionPool');
|
||||
|
||||
const Subject = require('./Subject');
|
||||
const Test = require('./Test');
|
||||
|
||||
@@ -57,10 +55,14 @@ class Class {
|
||||
*/
|
||||
students;
|
||||
|
||||
#conn;
|
||||
|
||||
/**
|
||||
* @param {string} classID - The id of the class to fetch
|
||||
*/
|
||||
constructor(classId) {
|
||||
constructor(conn, classId) {
|
||||
this.#conn = conn;
|
||||
|
||||
const sql = `
|
||||
select
|
||||
classId as id,
|
||||
@@ -95,9 +97,7 @@ class Class {
|
||||
`;
|
||||
|
||||
return (async () => {
|
||||
const conn = await new DatabaseConnectionPool();
|
||||
|
||||
const record = await conn.runQuery(sql, [
|
||||
const record = await this.#conn.runQuery(sql, [
|
||||
classId
|
||||
]);
|
||||
|
||||
@@ -108,16 +108,14 @@ class Class {
|
||||
this[k] = v;
|
||||
|
||||
const [ teacherRes, studentRes ] = await Promise.all([
|
||||
conn.runQuery(teacherSQL, [
|
||||
this.#conn.runQuery(teacherSQL, [
|
||||
classId
|
||||
]),
|
||||
conn.runQuery(studentSQL, [
|
||||
this.#conn.runQuery(studentSQL, [
|
||||
classId
|
||||
])
|
||||
]);
|
||||
|
||||
conn.close();
|
||||
|
||||
this.teacherIds = teacherRes.map(record => record.id);
|
||||
this.studentIds = studentRes.map(record => record.id);
|
||||
|
||||
@@ -140,7 +138,7 @@ class Class {
|
||||
}
|
||||
|
||||
getSubject() {
|
||||
return new (require('./Subject'))(this.subjectId);
|
||||
return new (require('./Subject'))(this.#conn, this.subjectId);
|
||||
}
|
||||
|
||||
getUsers(ids, type) {
|
||||
@@ -153,7 +151,8 @@ class Class {
|
||||
throw new Error('Invalid type');
|
||||
|
||||
const promises = ids.map(id => {
|
||||
return new (require(`./${types[type]}`))(id);
|
||||
const classFile = `./${types[type]}`;
|
||||
return new (require(classFile))(this.#conn, id);
|
||||
});
|
||||
|
||||
return Promise.all(promises);
|
||||
@@ -190,11 +189,10 @@ class Class {
|
||||
|
||||
sql += `order by t.testDate ${order};`;
|
||||
|
||||
const conn = await new DatabaseConnectionPool();
|
||||
const tests = await conn.runQuery(sql, [ this.id ]);
|
||||
const tests = await this.#conn.runQuery(sql, [ this.id ]);
|
||||
|
||||
const testObjects = tests.map(test => {
|
||||
return new Test(test.testId);
|
||||
return new Test(this.#conn, test.testId);
|
||||
});
|
||||
|
||||
return await Promise.all(testObjects);
|
||||
@@ -220,14 +218,12 @@ class Class {
|
||||
|
||||
}
|
||||
|
||||
static async createClass(accountId, name, subjectId) {
|
||||
const s = await new Subject(subjectId);
|
||||
const a = await new (require('./Account'))(accountId);
|
||||
static async createClass(conn, accountId, name, subjectId) {
|
||||
const s = await new Subject(conn, subjectId);
|
||||
const a = await new (require('./Account'))(conn, accountId);
|
||||
|
||||
const id = crypto.randomUUID();
|
||||
|
||||
const conn = await new DatabaseConnectionPool();
|
||||
|
||||
let sql = `
|
||||
insert into class(
|
||||
classId,
|
||||
@@ -256,7 +252,7 @@ class Class {
|
||||
id
|
||||
]);
|
||||
|
||||
return new Class(id);
|
||||
return new Class(conn, id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,16 +4,17 @@
|
||||
const User = require('./User');
|
||||
|
||||
class Parent extends User {
|
||||
constructor(id) {
|
||||
super('parent', id);
|
||||
constructor(conn, id) {
|
||||
super(conn, 'parent', id);
|
||||
}
|
||||
|
||||
get children() {
|
||||
|
||||
}
|
||||
|
||||
static async createParent(fname, oname, lname, email, password) {
|
||||
static async createParent(conn, fname, oname, lname, email, password) {
|
||||
return await super.createUser(
|
||||
conn,
|
||||
'parent',
|
||||
fname,
|
||||
oname,
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
const bcrypt = require('bcrypt');
|
||||
const crypto = require('crypto');
|
||||
|
||||
const DatabaseConnectionPool = require('./DatabaseConnectionPool');
|
||||
|
||||
class PasswordReset {
|
||||
userId;
|
||||
|
||||
@@ -14,7 +12,11 @@ class PasswordReset {
|
||||
|
||||
expires;
|
||||
|
||||
constructor(userId, token) {
|
||||
#conn;
|
||||
|
||||
constructor(conn, userId, token) {
|
||||
this.#conn = conn;
|
||||
|
||||
const sql = `
|
||||
select
|
||||
userId,
|
||||
@@ -29,14 +31,11 @@ class PasswordReset {
|
||||
`;
|
||||
|
||||
return (async () => {
|
||||
const conn = await new DatabaseConnectionPool();
|
||||
const record = await conn.runQuery(sql, [
|
||||
const record = await this.#conn.runQuery(sql, [
|
||||
userId,
|
||||
token
|
||||
]);
|
||||
|
||||
conn.close();
|
||||
|
||||
if (!record.length)
|
||||
throw new Error('No password reset found');
|
||||
|
||||
@@ -48,7 +47,7 @@ class PasswordReset {
|
||||
}
|
||||
|
||||
get user() {
|
||||
return new (require('./User'))(null, this.userId);
|
||||
return new (require('./User'))(this.#conn, null, this.userId);
|
||||
}
|
||||
|
||||
static async hashToken(u) {
|
||||
@@ -63,9 +62,8 @@ class PasswordReset {
|
||||
return [ nonce, token ];
|
||||
}
|
||||
|
||||
static async generatePasswordReset(userId) {
|
||||
const u = await new (require('./User'))(null, userId);
|
||||
const conn = await new DatabaseConnectionPool();
|
||||
static async generatePasswordReset(conn, userId) {
|
||||
const u = await new (require('./User'))(conn, null, userId);
|
||||
|
||||
let sql = `
|
||||
delete from passwordReset
|
||||
@@ -101,9 +99,7 @@ class PasswordReset {
|
||||
if (!result)
|
||||
throw new Error('Could not create password reset');
|
||||
|
||||
conn.close();
|
||||
|
||||
return new PasswordReset(u.id, token);
|
||||
return new PasswordReset(conn, u.id, token);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
const User = require('./User');
|
||||
|
||||
class Student extends User {
|
||||
constructor(id) {
|
||||
super('student', id);
|
||||
constructor(conn, id) {
|
||||
super(conn, 'student', id);
|
||||
}
|
||||
|
||||
get classes() {
|
||||
@@ -16,8 +16,9 @@ class Student extends User {
|
||||
|
||||
}
|
||||
|
||||
static async createStudent(fname, oname, lname, email, password) {
|
||||
static async createStudent(conn, fname, oname, lname, email, password) {
|
||||
return await super.createUser(
|
||||
conn,
|
||||
'student',
|
||||
fname,
|
||||
oname,
|
||||
|
||||
@@ -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);
|
||||
|
||||
21
lib/Test.js
21
lib/Test.js
@@ -1,9 +1,6 @@
|
||||
/* eslint-disable no-empty-function, getter-return */
|
||||
'use strict';
|
||||
|
||||
// Import user defined modules
|
||||
const DatabaseConnectionPool = require('./DatabaseConnectionPool');
|
||||
|
||||
/**
|
||||
* A class that represents the date of a test
|
||||
*/
|
||||
@@ -62,10 +59,14 @@ class Test {
|
||||
*/
|
||||
date;
|
||||
|
||||
#conn;
|
||||
|
||||
/**
|
||||
* @param {string} testId - The id of the test to fetch
|
||||
*/
|
||||
constructor(testId) {
|
||||
constructor(conn, testId) {
|
||||
this.#conn = conn;
|
||||
|
||||
const sql = `
|
||||
select
|
||||
testId as id,
|
||||
@@ -79,13 +80,10 @@ class Test {
|
||||
`;
|
||||
|
||||
return (async () => {
|
||||
const conn = await new DatabaseConnectionPool();
|
||||
const record = await conn.runQuery(sql, [
|
||||
const record = await this.#conn.runQuery(sql, [
|
||||
testId,
|
||||
]);
|
||||
|
||||
conn.close();
|
||||
|
||||
if (!record.length)
|
||||
throw new Error('No test found');
|
||||
|
||||
@@ -107,11 +105,14 @@ class Test {
|
||||
}
|
||||
|
||||
getClass() {
|
||||
return new (require('./Class'))(this.classId);
|
||||
return new (require('./Class'))(this.#conn, this.classId);
|
||||
}
|
||||
|
||||
getTestTemplate() {
|
||||
return new (require('./TestTemplate'))(this.templateId);
|
||||
return new (require('./TestTemplate'))(
|
||||
this.#conn,
|
||||
this.templateId
|
||||
);
|
||||
}
|
||||
|
||||
async hasAccess(u) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
55
lib/User.js
55
lib/User.js
@@ -3,8 +3,6 @@
|
||||
const bcrypt = require('bcrypt');
|
||||
const crypto = require('crypto');
|
||||
|
||||
const DatabaseConnectionPool = require('./DatabaseConnectionPool');
|
||||
|
||||
const Class = require('./Class');
|
||||
const PasswordReset = require('./PasswordReset');
|
||||
const Test = require('./Test');
|
||||
@@ -28,9 +26,13 @@ class User {
|
||||
|
||||
type = null;
|
||||
|
||||
constructor(type, userId) {
|
||||
_conn;
|
||||
|
||||
constructor(conn, type, userId) {
|
||||
type = type ?? false;
|
||||
|
||||
this._conn = conn;
|
||||
|
||||
let types = [];
|
||||
let knownType = false;
|
||||
if (type) {
|
||||
@@ -41,7 +43,6 @@ class User {
|
||||
}
|
||||
|
||||
return (async () => {
|
||||
const conn = await new DatabaseConnectionPool();
|
||||
const queryPromises = [];
|
||||
|
||||
for (const type of types) {
|
||||
@@ -59,13 +60,12 @@ class User {
|
||||
${type}Id = ?;
|
||||
`;
|
||||
|
||||
queryPromises.push(conn.runQuery(sql, [
|
||||
queryPromises.push(this._conn.runQuery(sql, [
|
||||
userId
|
||||
]));
|
||||
}
|
||||
|
||||
const typeResults = await Promise.all(queryPromises);
|
||||
conn.close();
|
||||
|
||||
for (const [ i, result ] of typeResults.entries()) {
|
||||
if (!result.length)
|
||||
@@ -92,7 +92,10 @@ class User {
|
||||
`${type.substring(0, 1).toUpperCase()}`+
|
||||
`${type.substring(1)}`;
|
||||
|
||||
return new (require(`./${className}`))(this.id);
|
||||
return new (require(`./${className}`))(
|
||||
this._conn,
|
||||
this.id
|
||||
);
|
||||
}
|
||||
|
||||
throw new Error('No user found');
|
||||
@@ -121,8 +124,6 @@ class User {
|
||||
async changePassword(password) {
|
||||
const newPassword = await User.hashPassword(password);
|
||||
|
||||
const conn = await new DatabaseConnectionPool();
|
||||
|
||||
const sql = `
|
||||
update
|
||||
${this.type}
|
||||
@@ -132,11 +133,11 @@ class User {
|
||||
${this.type}Id = ?;
|
||||
`;
|
||||
|
||||
await conn.runQuery(sql, [ newPassword, this.id ]);
|
||||
await this._conn.runQuery(sql, [ newPassword, this.id ]);
|
||||
}
|
||||
|
||||
generatePasswordReset() {
|
||||
return PasswordReset.generatePasswordReset(this.id);
|
||||
return PasswordReset.generatePasswordReset(this._conn, this.id);
|
||||
}
|
||||
|
||||
login(req) {
|
||||
@@ -180,11 +181,10 @@ class User {
|
||||
|
||||
sql += `order by t.testDate ${order};`;
|
||||
|
||||
const conn = await new DatabaseConnectionPool();
|
||||
const tests = await conn.runQuery(sql, [ this.id ]);
|
||||
const tests = await this._conn.runQuery(sql, [ this.id ]);
|
||||
|
||||
const testObjects = tests.map(test => {
|
||||
return new Test(test.testId);
|
||||
return new Test(this._conn, test.testId);
|
||||
});
|
||||
|
||||
return await Promise.all(testObjects);
|
||||
@@ -206,11 +206,10 @@ class User {
|
||||
c.name;
|
||||
`;
|
||||
|
||||
const conn = await new DatabaseConnectionPool();
|
||||
const classes = await conn.runQuery(sql, [ this.id ]);
|
||||
const classes = await this._conn.runQuery(sql, [ this.id ]);
|
||||
|
||||
const classObjects = classes.map(c => {
|
||||
return new Class(c.classId);
|
||||
return new Class(this._conn, c.classId);
|
||||
});
|
||||
|
||||
return await Promise.all(classObjects);
|
||||
@@ -220,9 +219,15 @@ class User {
|
||||
return await bcrypt.hash(password, 10);
|
||||
}
|
||||
|
||||
static async createUser(type, fname, oname, lname, email, password) {
|
||||
const conn = await new DatabaseConnectionPool();
|
||||
|
||||
static async createUser(
|
||||
conn,
|
||||
type,
|
||||
fname,
|
||||
oname,
|
||||
lname,
|
||||
email,
|
||||
password
|
||||
) {
|
||||
const uuid = crypto.randomUUID();
|
||||
const hashedPassword = await User.hashPassword(password);
|
||||
|
||||
@@ -250,7 +255,7 @@ class User {
|
||||
let res;
|
||||
switch (type) {
|
||||
case 'account':
|
||||
res = new (require('./Account'))(uuid);
|
||||
res = new (require('./Account'))(conn, uuid);
|
||||
break;
|
||||
default:
|
||||
throw new Error(
|
||||
@@ -260,8 +265,7 @@ class User {
|
||||
return res;
|
||||
}
|
||||
|
||||
static async getUserByEmail(email) {
|
||||
const conn = await new DatabaseConnectionPool();
|
||||
static async getUserByEmail(conn, email) {
|
||||
const types = [ 'account', 'student', 'parent' ];
|
||||
|
||||
const idPromises = [];
|
||||
@@ -291,7 +295,10 @@ class User {
|
||||
const className =
|
||||
`${type.substring(0, 1).toUpperCase()}`+
|
||||
`${type.substring(1)}`;
|
||||
return new (require(`./${className}`))(id);
|
||||
return new (require(`./${className}`))(
|
||||
conn,
|
||||
id
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user