(Feat): Initial Commit
This commit is contained in:
79
backend-deno/config/database.ts
Normal file
79
backend-deno/config/database.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import { createPool, Pool } from "mysql2/promise";
|
||||
import { load } from "@std/dotenv";
|
||||
|
||||
// Load environment variables
|
||||
await load({ export: true });
|
||||
|
||||
const config = {
|
||||
host: Deno.env.get("DB_HOST") || "localhost",
|
||||
user: Deno.env.get("DB_USER") || "root",
|
||||
password: Deno.env.get("DB_PASSWORD") || "admin123",
|
||||
database: Deno.env.get("DB_NAME") || "work_allocation",
|
||||
port: parseInt(Deno.env.get("DB_PORT") || "3306"),
|
||||
waitForConnections: true,
|
||||
connectionLimit: 10,
|
||||
queueLimit: 0,
|
||||
enableKeepAlive: true,
|
||||
keepAliveInitialDelay: 0,
|
||||
};
|
||||
|
||||
class Database {
|
||||
private pool: Pool | null = null;
|
||||
private static instance: Database;
|
||||
|
||||
private constructor() {}
|
||||
|
||||
static getInstance(): Database {
|
||||
if (!Database.instance) {
|
||||
Database.instance = new Database();
|
||||
}
|
||||
return Database.instance;
|
||||
}
|
||||
|
||||
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);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
return this.pool;
|
||||
}
|
||||
|
||||
async getPool(): Promise<Pool> {
|
||||
if (!this.pool) {
|
||||
return await this.connect();
|
||||
}
|
||||
return this.pool;
|
||||
}
|
||||
|
||||
async query<T>(sql: string, params?: unknown[]): Promise<T> {
|
||||
const pool = await this.getPool();
|
||||
const [rows] = await pool.query(sql, params);
|
||||
return rows as T;
|
||||
}
|
||||
|
||||
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 };
|
||||
}
|
||||
|
||||
async close(): Promise<void> {
|
||||
if (this.pool) {
|
||||
await this.pool.end();
|
||||
this.pool = null;
|
||||
console.log("Database connection closed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const db = Database.getInstance();
|
||||
export default db;
|
||||
40
backend-deno/config/env.ts
Normal file
40
backend-deno/config/env.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { load } from "@std/dotenv";
|
||||
|
||||
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_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"),
|
||||
|
||||
// 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