Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 26 additions & 28 deletions server/database/dbConnection.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
import pkg from "pg";
const { Client } = pkg;
import pkg, { Pool as PoolType } from "pg";
const { Pool } = pkg;

import { type DatabaseConnection } from "../types";
import { getConfig } from "./dbConfig";

let db: DatabaseConnection | null = null;
let db: PoolType | null = null;

export const setupDatabaseConnection =
async (): Promise<DatabaseConnection> => {
const { database, dbHost, dbUser, dbPassword, dbPort, dbSsl } = getConfig();
export const setupDatabaseConnection = async (): Promise<PoolType> => {
const { database, dbHost, dbUser, dbPassword, dbPort, dbSsl } = getConfig();

const dbConnection = {
database: database,
user: dbUser,
host: dbHost,
password: dbPassword,
port: parseInt(dbPort, 10),
ssl: dbSsl === true ? { rejectUnauthorized: false } : false,
};
db = new Client(dbConnection);
const dbConnection = {
database: database,
user: dbUser,
host: dbHost,
password: dbPassword,
port: parseInt(dbPort, 10),
ssl: dbSsl === true ? { rejectUnauthorized: false } : false,
};
db = new Pool(dbConnection);

db.connect()
.then(() => {
console.log("Connected to the PostgreSQL database");
})
.catch((error: Error) => {
db = null;
console.error("Error connecting to the PostgreSQL database:", error);
});
db.on("connect", () => {
console.log("Connected to the PostgreSQL database");
});

return db;
};
db.on("error", (error: Error) => {
db = null;
console.error("Error connecting to the PostgreSQL database:", error);
});

return db;
};

export const getDatabaseConnection = async (): Promise<DatabaseConnection> => {
export const getDatabaseConnection = async (): Promise<PoolType> => {
if (db) {
await ensurePostgresConnection(db);
} else {
Expand All @@ -48,7 +46,7 @@ export const refreshDatabaseConnection = async (): Promise<void> => {
db = await setupDatabaseConnection();
};

async function ensurePostgresConnection(db: DatabaseConnection): Promise<void> {
async function ensurePostgresConnection(db: PoolType): Promise<void> {
try {
await db.query("SELECT 1"); // Simple query to check connection
} catch (error) {
Expand Down
160 changes: 78 additions & 82 deletions server/database/dbOperations.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import { Client } from "pg";
import { Pool } from "pg";

import { type MapRequest } from "../types";

const checkTableExists = (
db: Client,
const checkTableExists = async (
db: Pool,
table: string | undefined,
): Promise<boolean> => {
return new Promise((resolve, reject) => {
const query = `SELECT to_regclass('${table}')`;
db.query<{ to_regclass: string | null }>(
query,
[],
(err: Error, result) => {
if (err) reject(err);
resolve(result.rows[0].to_regclass !== null);
},
const query = `SELECT to_regclass('${table}')`;
let result;
try {
result = await db.query<{ to_regclass: string | null }>(query);
} catch (error) {
throw new Error(
`Failed to check if table exists: ${(error as Error).message}`,
);
});
}
return result.rows[0].to_regclass !== null;
};

const createMapRequestTable = async (
db: Client,
db: Pool,
table: string | undefined,
): Promise<void> => {
console.log(`Table ${table} does not exist. Creating...`);
Expand Down Expand Up @@ -52,17 +51,18 @@ const createMapRequestTable = async (
work_ended TIMESTAMP(6)
)
`;
return new Promise((resolve, reject) => {
db.query(query, (err: Error) => {
if (err) reject(err);
console.log(`Table ${table} created successfully`);
resolve();
});
});
try {
await db.query(query);
console.log(`Table ${table} created successfully`);
} catch (error) {
throw new Error(
`Failed to create table ${table}: ${(error as Error).message}`,
);
}
};

const fetchDataFromTable = async (
db: Client,
db: Pool,
table: string | undefined,
limit: number,
cursor: number | null,
Expand All @@ -77,21 +77,20 @@ const fetchDataFromTable = async (
query = `SELECT * FROM ${table} ORDER BY id DESC LIMIT $1`;
}

return new Promise((resolve, reject) => {
db.query(query, values, (err: Error, result: { rows: MapRequest[] }) => {
if (err) {
reject(err);
} else if (!result || !result.rows) {
reject(new Error("No result returned from query."));
} else {
resolve(result.rows);
}
});
});
let result;
try {
result = await db.query(query, values);
} catch (error) {
throw new Error(`Failed to fetch data from table ${table}: ${error}`);
}
if (!result || !result.rows) {
throw new Error("No result returned from query.");
}
return result.rows;
};

export const fetchData = async (
db: Client,
db: Pool,
table: string | undefined,
limit: number,
cursor: number | null,
Expand All @@ -106,7 +105,7 @@ export const fetchData = async (
};

export const insertDataIntoTable = async (
db: Client,
db: Pool,
table: string | undefined,
data: MapRequest,
): Promise<number> => {
Expand All @@ -121,24 +120,23 @@ export const insertDataIntoTable = async (
const values = Object.values(data);
// Return id so it can be used if needed for error handling
const query = `INSERT INTO ${table} (${columns}) VALUES (${placeholders}) RETURNING id`;
return new Promise((resolve, reject) => {
db.query(
query,
values,
(err: Error, result: { rows: { id: number }[] }) => {
if (err) reject(err);
if (result.rows.length > 0) {
resolve(result.rows[0].id);
} else {
reject(new Error("No rows returned after insert."));
}
},
let result;
try {
result = await db.query(query, values);
} catch (error) {
throw new Error(
`Failed to insert data into table ${table}: ${(error as Error).message}`,
);
});
}
if (result.rows.length > 0) {
return result.rows[0].id;
} else {
throw new Error("No rows returned after insert.");
}
};

export const handleDeleteRequest = async (
db: Client,
db: Pool,
table: string | undefined,
requestId: number | void | null,
): Promise<boolean> => {
Expand All @@ -151,13 +149,24 @@ export const handleDeleteRequest = async (
}

const query = `SELECT file_location, filename FROM ${table} WHERE id = $1`;
const result = await db.query(query, [requestId]);
let result;
try {
result = await db.query(query, [requestId]);
} catch (error) {
throw new Error(
`Failed to fetch file location and filename: ${(error as Error).message}`,
);
}
const { file_location, filename } = result.rows[0];

if (!file_location || !filename) {
console.log("File location or filename is NULL. Deleting row...");
const deleteQuery = `DELETE FROM ${table} WHERE id = $1`;
await db.query(deleteQuery, [requestId]);
try {
await db.query(deleteQuery, [requestId]);
} catch (error) {
throw new Error(`Failed to delete row: ${error}`);
}
return false;
} else {
if (!table) {
Expand All @@ -172,7 +181,7 @@ export const handleDeleteRequest = async (
};

export async function updateDatabaseMapRequest(
db: Client,
db: Pool,
tableName: string,
id: number | void | null,
data: Partial<MapRequest>,
Expand All @@ -193,23 +202,18 @@ export async function updateDatabaseMapRequest(
WHERE id = $${values.length}
`;

return new Promise<void>((resolve, reject) => {
db.query(query, values, (err: Error) => {
if (err) {
console.error(
`Error updating record ${id} in table ${tableName}: ${err.message}`,
);
reject(err);
} else {
console.log(`Record ${id} in table ${tableName} updated.`);
resolve();
}
});
});
try {
await db.query(query, values);
} catch (error) {
throw new Error(
`Error updating record ${id} in table ${tableName}: ${(error as Error).message}`,
);
}
console.log(`Record ${id} in table ${tableName} updated.`);
}

export async function updateDatabaseWithError(
db: Client,
db: Pool,
tableName: string,
id: number | void | null,
errorMessage: string,
Expand All @@ -223,20 +227,12 @@ export async function updateDatabaseWithError(
SET status = 'FAILED', error_message = $1
WHERE id = $2
`;

return new Promise<void>((resolve, reject) => {
db.query(query, [errorMessage, id], (err: Error) => {
if (err) {
console.error(
`Error updating record ${id} in table ${tableName}: ${err.message}`,
);
reject(err);
} else {
console.log(
`Record ${id} in table ${tableName} updated with error message.`,
);
resolve();
}
});
});
try {
await db.query(query, [errorMessage, id]);
} catch (error) {
throw new Error(
`Error updating record ${id} in table ${tableName}: ${(error as Error).message}`,
);
}
console.log(`Record ${id} in table ${tableName} updated with error message.`);
}
4 changes: 0 additions & 4 deletions server/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import { Client } from "pg";

export type DatabaseConnection = typeof Client.prototype;

export type MapStyleKey =
| "bing"
| "google"
Expand Down
Loading