1
0
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:
2022-03-23 23:29:55 +00:00
parent bd662661ee
commit 4c2a078530
14 changed files with 162 additions and 144 deletions

View File

@@ -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
);
}
}
}