File size: 1,389 Bytes
c01955c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import mysql from "mysql2/promise"
import logger from "../logger/create.logger.js";


async function connectsql() {
    try {

       const db = mysql.createPool({

    host: process.env.SQL_HOST,
    user: process.env.SQL_USER,
    port: process.env.SQL_PORT ? parseInt(process.env.SQL_PORT) : 3306,
    password: process.env.SQL_PASSWORD,
    database: process.env.SQL_DATABASE,
    waitForConnections: true,
    connectionLimit: 10,
    queueLimit: 0,
    enableKeepAlive: true,
    keepAliveInitialDelay: 0
    // ssl: { rejectUnauthorized: true } // agar SSL enforce ho to

  });
  
    logger.info("mysql pool created");
    // Ensure the questions table exists to prevent ER_NO_SUCH_TABLE errors
    const createTableQuery = `
      CREATE TABLE IF NOT EXISTS questions (
          id INT PRIMARY KEY,
          title VARCHAR(255),
          difficulty VARCHAR(50),
          category VARCHAR(100),
          problem_description TEXT,
          starter_code TEXT,
          example_input TEXT,
          example_output TEXT,
          example_reasoning TEXT,
          learn_content TEXT,
          solution_code TEXT,
          test_cases JSON,
          function_name VARCHAR(255)
      )
    `;
    await db.execute(createTableQuery);

    return db
        
    } catch (error) {
        logger.error("mysql connection failed", error);
    }

    
}


export default connectsql;