Skip to content

Commit

Permalink
Use a pool instead of a single connection to DB
Browse files Browse the repository at this point in the history
  • Loading branch information
claitz committed Sep 25, 2023
1 parent efe4652 commit 86626a4
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 20 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ yarn-error.log*

# typescript
*.tsbuildinfo
next-env.d.ts
next-env.d.ts

# IDE
.idea
6 changes: 3 additions & 3 deletions pages/api/opendaoc/create-account.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NextApiRequest, NextApiResponse } from "next";
import connection from '@/utils/db';
import pool from '@/utils/db';
import getConfig from 'next/config'
import { containsProhibitedCharacters, cryptPassword } from '@/utils/auth';

Expand Down Expand Up @@ -29,7 +29,7 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {
}

// Check if the user already exists in the database
connection.query('SELECT * FROM account WHERE Name = ?', [username], (err, results) => {
pool.query('SELECT * FROM account WHERE Name = ?', [username], (err, results) => {
if (err) {
console.error(err);
return res.status(200).json({ success: false, message: 'Internal server error' });
Expand All @@ -42,7 +42,7 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {
{
const passwordHash = cryptPassword(password);
// User doesn't exist, create a new account
connection.query('INSERT INTO account (Account_ID, Name, Password, PrivLevel, CreationDate, LastTimeRowUpdated, DiscordID, DiscordName) VALUES (?, ?, ?, 1, NOW(), NOW(), ?, ?)', [username, username, passwordHash, discordId, discordName], (insertErr) => {
pool.query('INSERT INTO account (Account_ID, Name, Password, PrivLevel, CreationDate, LastTimeRowUpdated, DiscordID, DiscordName) VALUES (?, ?, ?, 1, NOW(), NOW(), ?, ?)', [username, username, passwordHash, discordId, discordName], (insertErr) => {
if (insertErr) {
console.error(insertErr);
return res.status(200).json({ success: false, message: 'Failed to create account' });
Expand Down
6 changes: 3 additions & 3 deletions pages/api/opendaoc/link-account.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextApiRequest, NextApiResponse } from 'next';
import connection from '@/utils/db';
import pool from '@/utils/db';
import { containsProhibitedCharacters, cryptPassword } from '@/utils/auth';

export default function handler(req: NextApiRequest, res: NextApiResponse) {
Expand All @@ -23,7 +23,7 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {

const passwordHash = cryptPassword(password);

connection.query('SELECT * FROM account WHERE Name = ? and Password = ?', [username, passwordHash], (err, results) => {
pool.query('SELECT * FROM account WHERE Name = ? and Password = ?', [username, passwordHash], (err, results) => {
if (err) {
console.error(err);
return res.status(200).json({ success: false, message: 'Internal server error' });
Expand All @@ -34,7 +34,7 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {
}
else
{
connection.query('UPDATE account SET DiscordID = ?, DiscordName = ?, LastTimeRowUpdated = NOW() WHERE Name = ?', [discordId, discordName, username], (err, results) => {
pool.query('UPDATE account SET DiscordID = ?, DiscordName = ?, LastTimeRowUpdated = NOW() WHERE Name = ?', [discordId, discordName, username], (err, results) => {
if (err) {
console.error(err);
return res.status(200).json({ success: false, message: 'Internal server error' });
Expand Down
4 changes: 2 additions & 2 deletions pages/api/opendaoc/update-password.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NextApiRequest, NextApiResponse } from "next";
import connection from '@/utils/db';
import pool from '@/utils/db';
import { containsProhibitedCharacters, cryptPassword } from '@/utils/auth';

export default function handler(req: NextApiRequest, res: NextApiResponse) {
Expand All @@ -24,7 +24,7 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {

const passwordHash = cryptPassword(newPassword);

connection.query('UPDATE account SET Password = ? WHERE Name = ?', [passwordHash, username], (err, results) => {
pool.query('UPDATE account SET Password = ? WHERE Name = ?', [passwordHash, username], (err, results) => {
if (err) {
console.error(err);
return res.status(200).json({ success: false, message: 'Internal server error' });
Expand Down
4 changes: 2 additions & 2 deletions utils/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import crypto from 'crypto';
import getConfig from 'next/config';
import connection from '@/utils/db';
import pool from '@/utils/db';
import mysql from 'mysql2';

const { publicRuntimeConfig } = getConfig();
Expand Down Expand Up @@ -60,7 +60,7 @@ export function getAccountFromDiscord(discord_id: string | undefined): Promise<s
}

// Check if the user already exists in the database
connection.query('SELECT * FROM account WHERE DiscordID = ?', [discord_id], (err, results) => {
pool.query('SELECT * FROM account WHERE DiscordID = ?', [discord_id], (err, results) => {
if (err) {
console.error(err);
reject(err);
Expand Down
15 changes: 6 additions & 9 deletions utils/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ import getConfig from 'next/config';

const { publicRuntimeConfig } = getConfig();

const connection = mysql.createConnection({
// Create a connection pool
const pool = mysql.createPool({
host: publicRuntimeConfig.DATABASE_HOST,
user: publicRuntimeConfig.DATABASE_USER,
password: publicRuntimeConfig.DATABASE_PASSWORD,
database: publicRuntimeConfig.DATABASE_NAME,
waitForConnections: true,
connectionLimit: 10, // You can adjust this value based on your needs
queueLimit: 0
});

connection.connect((err) => {
if (err) {
console.error("Failed to connect to the database:", err);
throw err;
}
});

export default connection;
export default pool;

0 comments on commit 86626a4

Please sign in to comment.