-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add middleware to check database connection, handle errors from conne…
…ction pool
- Loading branch information
Showing
3 changed files
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { Request, Response, NextFunction } from "express"; | ||
import { pool } from "../lib/db.js"; | ||
|
||
/** | ||
* A Simple database connection checker middleware, tests the | ||
* database can be connected to and handles connection errors. | ||
*/ | ||
export async function dbConnectionTest( | ||
req: Request<{}, {}, {}, { async?: "true" | "false" }>, | ||
res: Response, | ||
next: NextFunction, | ||
) { | ||
try { | ||
const connection = await pool.connect(); | ||
connection.release(); | ||
next(); | ||
} catch { | ||
next(new Error("Unable to connect to Database")); | ||
} | ||
} |