(Feat-Fix): Lots of fixes done, reporting system fixed, stricter types
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { createPool, Pool } from "mysql2/promise";
|
||||
import { createPool, Pool, PoolConnection } from "mysql2/promise";
|
||||
import { load } from "@std/dotenv";
|
||||
|
||||
// Load environment variables
|
||||
@@ -33,14 +33,17 @@ class Database {
|
||||
async connect(): Promise<Pool> {
|
||||
if (!this.pool) {
|
||||
this.pool = createPool(config);
|
||||
|
||||
|
||||
// Test connection
|
||||
try {
|
||||
const connection = await this.pool.getConnection();
|
||||
console.log("✅ Database connected successfully");
|
||||
connection.release();
|
||||
} catch (error) {
|
||||
console.error("❌ Database connection failed:", (error as Error).message);
|
||||
console.error(
|
||||
"❌ Database connection failed:",
|
||||
(error as Error).message,
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -60,12 +63,39 @@ class Database {
|
||||
return rows as T;
|
||||
}
|
||||
|
||||
async execute(sql: string, params?: unknown[]): Promise<{ insertId: number; affectedRows: number }> {
|
||||
async execute(
|
||||
sql: string,
|
||||
params?: unknown[],
|
||||
): Promise<{ insertId: number; affectedRows: number }> {
|
||||
const pool = await this.getPool();
|
||||
const [result] = await pool.execute(sql, params);
|
||||
return result as { insertId: number; affectedRows: number };
|
||||
}
|
||||
|
||||
// Get a connection for transaction support
|
||||
async getConnection(): Promise<PoolConnection> {
|
||||
const pool = await this.getPool();
|
||||
return await pool.getConnection();
|
||||
}
|
||||
|
||||
// Execute within a transaction
|
||||
async transaction<T>(
|
||||
callback: (connection: PoolConnection) => Promise<T>,
|
||||
): Promise<T> {
|
||||
const connection = await this.getConnection();
|
||||
try {
|
||||
await connection.beginTransaction();
|
||||
const result = await callback(connection);
|
||||
await connection.commit();
|
||||
return result;
|
||||
} catch (error) {
|
||||
await connection.rollback();
|
||||
throw error;
|
||||
} finally {
|
||||
connection.release();
|
||||
}
|
||||
}
|
||||
|
||||
async close(): Promise<void> {
|
||||
if (this.pool) {
|
||||
await this.pool.end();
|
||||
|
||||
@@ -5,36 +5,41 @@ await load({ export: true });
|
||||
export const config = {
|
||||
// Server
|
||||
PORT: parseInt(Deno.env.get("PORT") || "3000"),
|
||||
|
||||
|
||||
// Database
|
||||
DB_HOST: Deno.env.get("DB_HOST") || "localhost",
|
||||
DB_USER: Deno.env.get("DB_USER") || "root",
|
||||
DB_PASSWORD: Deno.env.get("DB_PASSWORD") || "admin123",
|
||||
DB_NAME: Deno.env.get("DB_NAME") || "work_allocation",
|
||||
DB_PORT: parseInt(Deno.env.get("DB_PORT") || "3306"),
|
||||
|
||||
|
||||
// JWT - Security: Use strong secret in production
|
||||
JWT_SECRET: Deno.env.get("JWT_SECRET") || "work_alloc_jwt_secret_key_change_in_production_2024",
|
||||
JWT_SECRET: Deno.env.get("JWT_SECRET") ||
|
||||
"work_alloc_jwt_secret_key_change_in_production_2024",
|
||||
JWT_EXPIRES_IN: Deno.env.get("JWT_EXPIRES_IN") || "7d",
|
||||
|
||||
|
||||
// Security settings
|
||||
BCRYPT_ROUNDS: parseInt(Deno.env.get("BCRYPT_ROUNDS") || "12"),
|
||||
RATE_LIMIT_WINDOW_MS: parseInt(Deno.env.get("RATE_LIMIT_WINDOW_MS") || "900000"), // 15 minutes
|
||||
RATE_LIMIT_MAX_REQUESTS: parseInt(Deno.env.get("RATE_LIMIT_MAX_REQUESTS") || "100"),
|
||||
|
||||
RATE_LIMIT_WINDOW_MS: parseInt(
|
||||
Deno.env.get("RATE_LIMIT_WINDOW_MS") || "900000",
|
||||
), // 15 minutes
|
||||
RATE_LIMIT_MAX_REQUESTS: parseInt(
|
||||
Deno.env.get("RATE_LIMIT_MAX_REQUESTS") || "100",
|
||||
),
|
||||
|
||||
// CORS
|
||||
CORS_ORIGIN: Deno.env.get("CORS_ORIGIN") || "http://localhost:5173",
|
||||
|
||||
|
||||
// Environment
|
||||
NODE_ENV: Deno.env.get("NODE_ENV") || "development",
|
||||
|
||||
|
||||
isDevelopment(): boolean {
|
||||
return this.NODE_ENV === "development";
|
||||
},
|
||||
|
||||
|
||||
isProduction(): boolean {
|
||||
return this.NODE_ENV === "production";
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
export default config;
|
||||
|
||||
Reference in New Issue
Block a user