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