Skip to content
Snippets Groups Projects
Commit eb1ced18 authored by Federico Hurtado's avatar Federico Hurtado
Browse files

Create db config file with startup function.

parent 358cd2e6
No related branches found
No related tags found
No related merge requests found
// /config/db.js
const mysql = require("mysql2");
const fs = require("fs");
const path = require("path");
require("dotenv").config();
const pool = mysql.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER || "corps_directory_dev",
database: process.env.DB_NAME || "corps_directory_db",
password: process.env.DB_PASSWORD || "corps_db_password",
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
});
console.log("Host: ", process.env.DB_HOST);
// Function to execute the SQL file
const executeSQLFile = (filePath) => {
const sql = fs.readFileSync(filePath, "utf8");
pool.getConnection((err, connection) => {
if (err) {
console.error("Error connecting to MySQL:", err.message);
return;
}
// Execute the SQL from the file
connection.query(sql, (err, result) => {
if (err) {
console.error("Error executing SQL file:", err.message);
connection.release();
return;
}
console.log("SQL file executed successfully.");
connection.release();
});
});
};
// Execute the SQL file during startup
const schemaFilePath = path.join(__dirname, "./schema.sql");
executeSQLFile(schemaFilePath);
module.exports = pool;
-- schema.sql
-- Create the 'people' table if it doesn't exist
CREATE TABLE IF NOT EXISTS people (
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL
);
......@@ -5,16 +5,7 @@ require("dotenv").config();
const app = express();
app.use(express.json());
// mysql connection
const pool = mysql.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER || "corps_directory_dev",
database: process.env.DB_NAME || "corps_directory_db",
password: process.env.DB_PASSWORD || "corps_db_password",
waitForConnections: true,
connectionLimit: 10, // Max number of connections in the pool
queueLimit: 0, // Unlimited queue
});
const pool = require("./config/database/database.config"); // This will now run the schema.sql file on startup
// Check connection and create the 'people' table on initialization
pool.getConnection((err, connection) => {
......@@ -23,25 +14,6 @@ pool.getConnection((err, connection) => {
return;
}
console.log("Connected to MySQL");
const createTable = `
CREATE TABLE IF NOT EXISTS people (
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL
)
`;
connection.query(createTable, (err, result) => {
if (err) {
console.error("Error creating table:", err.message);
connection.release(); // Release the connection back to the pool
return;
}
console.log("Table 'people' exists or was created.");
connection.release(); // Release the connection back to the pool
});
});
// POST endpoint to add a new person
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment