Skip to content

Commit

Permalink
created universal query function to query database
Browse files Browse the repository at this point in the history
  • Loading branch information
dddictionary committed Jun 5, 2024
1 parent 27ec86c commit 27b2e1b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 14 deletions.
10 changes: 1 addition & 9 deletions routes/databaseFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,7 @@ import { Client } from "pg";
* Function to add all items from values to database
* Assumes values array correctly maps to the database schema (no empty values, etc.)
*/
export function addToDB(client: Client, query: string, values: Array<any>) {
try {
return client.query(query, values);
} catch (e: any) {
throw Error(e);
}
}

export function getFromDB(client: Client, query: string, values: Array<any>) {
export function queryDatabase(client: Client, query: string, values: Array<any>) {
try {
return client.query(query, values);
} catch (e: any) {
Expand Down
8 changes: 4 additions & 4 deletions routes/projectsDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Router } from "express";
import connectDB from "../connectDB";
import express from "express";
import logger from "../utils/logger";
import { addToDB, getFromDB } from "./databaseFunctions";
import { queryDatabase } from "./databaseFunctions";
import { QueryResult } from "pg";

const router: Router = Router();
Expand Down Expand Up @@ -47,7 +47,7 @@ router.get("/get", async (req: any, res: any) => {

// execute the query, making sure to provide the values for the filters
try {
const data: QueryResult = await getFromDB(req.client, baseQuery, values);
const data: QueryResult = await queryDatabase(req.client, baseQuery, values);
return res.status(200).send(data.rows);
} catch {
return res.status(500).json({"message": "Error retrieving data"});
Expand All @@ -60,7 +60,7 @@ router.post("/add", (req: any, res: any) => {
INSERT INTO projects (name, "short-desc", "long-desc", team, link, image, "tech-stack", cohort, topic)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`;
try {
addToDB(req.client, query, values);
queryDatabase(req.client, query, values);
return res.status(200).send("Project added successfully");
} catch (err: any) {
return res.status(400).send(err.message);
Expand Down Expand Up @@ -98,7 +98,7 @@ router.put("/update", async (req: any, res: any) => {
console.log(query);
console.log(values);
try {
await addToDB(req.client, query, values);
await queryDatabase(req.client, query, values);
return res.status(200).send("Project updated successfully");
} catch (err: any) {
return res.status(400).json({ message: err.message });
Expand Down
2 changes: 1 addition & 1 deletion server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ app.use("/projects", projectsDB);

// any other route will return a 404
app.get("*", (req: any, res: any) => {
res.status(404).json({ message: "Page not found" });
res.status(404).json({ message: "Page not found. Invalid path or method provided to make this request." });
});

app.listen(PORT, () => {
Expand Down

0 comments on commit 27b2e1b

Please sign in to comment.