Repository: 101-Coconsulting/TC3005B.501-Backend
Pull request #276: 259 task automatic db setup
Author: login: A09271
Labels: name: enhancement
description: New feature or request
name: Database
description: Database related issues
name: chore
description: Something needs to be done
Created: 2025-05-20T00:57:04Z | Merged: 2025-05-27T00:35:10Z

# Chore PR

## Before Submitting

- [x] This is a required activity that does not directly relate to any User Stories.
- [x] PR is linked to a standalone `chore` Task Issue: #259 
- [x] PR is from a feature branch from `development`.
- [x] PR is being made to the `development` branch.
- [x] Use **only** `git` conventional commits of type `style`, `docs`, `refactor`, or `chore`.

## Description

Added a script to automatically run all of the db files

## Dependent Issues

n/a

## Affected Issues

#271
For quality of life purposes, probably most of them.


Diff:
diff --git a/README.md b/README.md
index dacfa08d..c44c80a5 100644
--- a/README.md
+++ b/README.md
@@ -72,46 +72,9 @@ Now you should have 6 new files in the [`/certs`](/certs) directory and should b
 > [!Caution]
 > After creating the certificates, when making a commit be sure not to be uploading the certificates to the repository.
 
-### Configuring the Database
-
-For the database to be operational, some initial configuration is required.
-
-#### Setup MariaDB
-
-In order to properly setup MariaDB, the following steps are required:
-
-1. [Download `mariadb`](https://mariadb.com/kb/en/where-to-download-mariadb/).
-2. It is recommended that you [secure your MariaDB installation](https://mariadb.com/kb/en/mysql_secure_installation/).
-3. [Start the `mariadb` server](https://mariadb.com/kb/en/starting-and-stopping-mariadb-automatically/).
-4. Go to the [/database/Scheme](/database/Scheme) directory.
-    ```sh
-    cd database/Scheme
-    ```
-5. [Run the `mariadb` client in batch mode](https://mariadb.com/kb/en/mariadb-command-line-client/). With `DB_USER` and `DB_USER_PASSWORD` being your created `mariadb` user and its password.
-    1. Load database scheme [/database/Scheme/Scheme.sql](/database/Scheme/Scheme.sql).
-        ```sh
-        mariadb -u DB_USER -p DB_USER_PASSWORD < Scheme.sql
-        ```
-    2. Load database initial prepopulation [/database/Scheme/Prepopulate.sql](/database/Scheme/Prepopulate.sql).
-        ```sh
-        mariadb -u DB_USER -p DB_USER_PASSWORD < Prepopulate.sql
-        ```
-    3. Load database triggers [/database/Scheme/Triggers.sql](/database/Scheme/Triggers.sql).
-        ```sh
-        mariadb -u DB_USER -p DB_USER_PASSWORD < Triggers.sql
-        ```
-    4. Load database views [/database/Scheme/Views.sql](/database/Scheme/Views.sql).
-        ```sh
-        mariadb -u DB_USER -p DB_USER_PASSWORD < Views.sql
-        ```
-    5. Load database dummy data [/database/Scheme/Dummy.sql](/database/Scheme/Dummy.sql).
-        ```sh
-        mariadb -u DB_USER -p DB_USER_PASSWORD < Dummy.sql
-        ```
-
 ### Environment Variables
 
-Finally, it is crucial that a local `.env` file is created. Based off of the [`.env.example`](/.env.example) file provided, which includes all necessary environment variables to be set in order for the server to be able to connect to the `mariadb` database, as well as the required JSON Web Token(JWT) information required for verifying authorized requests and encryption.
+It is crucial that a local `.env` file is created. Based off of the [`.env.example`](/.env.example) file provided, which includes all necessary environment variables to be set in order for the server to be able to connect to the `mariadb` database, as well as the required JSON Web Token(JWT) information required for verifying authorized requests and encryption.
 
 1. Go to the [root directory](/) of your local repository.
 2. Create your `.env` file based off of the [`.env.example`](/.env.example) file.
@@ -146,6 +109,37 @@ Finally, it is crucial that a local `.env` file is created. Based off of the [`.
     # CORS_ORIGIN=http://localhost:3000
     ```
 
+### Configuring the Database
+
+For the database to be operational, some initial configuration is required.
+
+#### Setup MariaDB
+
+In order to properly setup MariaDB, the following steps are required:
+
+1. [Download `mariadb`](https://mariadb.com/kb/en/where-to-download-mariadb/).
+2. It is recommended that you [secure your MariaDB installation](https://mariadb.com/kb/en/mysql_secure_installation/).
+3. [Start the `mariadb` server](https://mariadb.com/kb/en/starting-and-stopping-mariadb-automatically/).
+4. Setup the Database utilizing the `pnpm` or `npm` scripts.
+    - For development (with dummy data)
+        - With `pnpm`
+            ```sh
+            pnpm run dummy_db
+            ```
+        - With `npm`
+            ```sh
+            npm run dummy_db
+            ```
+    - For production (empty database)
+        - With `pnpm`
+            ```sh
+            pnpm run empty_db
+            ```
+        - With `npm`
+            ```sh
+            npm run empty_db
+            ```
+
 ### Running
 
 To run the Backend, ensure the `mariadb` server is running, and utilize whichever package manager you used for dependencies to run the project.
diff --git a/database/Schema/Triggers.sql b/database/Schema/Triggers.sql
index 771cb230..f63c3668 100644
--- a/database/Schema/Triggers.sql
+++ b/database/Schema/Triggers.sql
@@ -1,7 +1,5 @@
 USE CocoScheme;
 
-DELIMITER $$
-
 CREATE OR REPLACE TRIGGER DeactivateRequest
 BEFORE UPDATE ON Request
 FOR EACH ROW
@@ -9,7 +7,7 @@ BEGIN
     IF NEW.request_status_id IN (9, 10) THEN
         SET NEW.active = FALSE;
     END IF;
-END$$
+END;
 
 CREATE OR REPLACE TRIGGER CreateAlert
 AFTER INSERT ON Request
@@ -19,7 +17,7 @@ BEGIN
         INSERT INTO Alert (request_id, message_id) VALUES
             (NEW.request_id, NEW.request_status_id);
     END IF;
-END$$
+END;
 
 CREATE OR REPLACE TRIGGER ManageAlertAfterRequestUpdate
 AFTER UPDATE ON Request
@@ -33,7 +31,7 @@ BEGIN
         SET message_id = NEW.request_status_id
         WHERE request_id = NEW.request_id;
     END IF;
-END$$
+END;
 
 CREATE OR REPLACE TRIGGER ResetRejectedReceipts
 AFTER UPDATE ON Request
@@ -45,6 +43,4 @@ BEGIN
         WHERE request_id = NEW.request_id
         AND validation = 'Rechazado';
     END IF;
-END$$
-
-DELIMITER ;
+END;
diff --git a/database/config/dev_db.js b/database/config/dev_db.js
new file mode 100644
index 00000000..e9648e8b
--- /dev/null
+++ b/database/config/dev_db.js
@@ -0,0 +1,62 @@
+import dotenv from 'dotenv';  // For environment variable loading.
+import mariadb from 'mariadb';  // For connection to `mariadb` DataBase.
+
+import fs from "fs";  // For accesing the FileSystem an reading the `.sql` scripts.
+
+dotenv.config();
+
+const pool = mariadb.createPool({
+    multipleStatements: true,
+    host: process.env.DB_HOST,
+    port: process.env.DB_PORT,
+    user: process.env.DB_USER,
+    password: process.env.DB_PASSWORD,
+});
+
+const environment = process.argv[2];
+
+async function devdb() {
+    let conn;
+
+    try {
+        conn = await pool.getConnection();
+
+        console.log("Executing Scheme.sql...");
+        await conn.query({
+            sql: fs.readFileSync("./database/Schema/Scheme.sql", "utf8")
+        });
+        console.log("Scheme.sql executed.");
+
+        console.log("Executing Prepopulate.sql...");
+        await conn.importFile({file: "./database/Schema/Prepopulate.sql"});
+        console.log("Prepopulate.sql executed.");
+
+        console.log("Executing Triggers.sql...");
+        await conn.query({
+            sql: fs.readFileSync("./database/Schema/Triggers.sql", "utf8")
+        });
+        console.log("Triggers.sql executed.");
+
+        console.log("Executing Views.sql...");
+        await conn.importFile({file: "./database/Schema/Views.sql"});
+        console.log("Views.sql executed.");
+
+        if (environment === 'dev') {
+            console.log("Executing Dummy.sql...");
+            await conn.importFile({file: "./database/Schema/Dummy.sql"});
+            console.log("Dummy.sql executed.");
+        }
+
+    } catch (error) {
+        console.error(error);
+    } finally {
+        if (conn){
+            conn.release();
+        }
+        pool.end()
+            .then(() => console.log("Database connection pool closed."))
+            .catch(err => console.error("Error closing database connection pool:", err));
+    }
+}
+
+devdb();
diff --git a/package.json b/package.json
index 20797a93..30618c73 100644
--- a/package.json
+++ b/package.json
@@ -7,6 +7,8 @@
     "scripts": {
         "start": "node index.js",
         "dev": "nodemon index.js",
+        "dummy_db": "node ./database/config/dev_db.js dev",
+        "empty_db": "node ./database/config/dev_db.js prod",
         "test": "echo \"Error: no test specified\" && exit 1"
     },
     "repository": {


Reviews:
user: login: paydelimon22
body: Shouldn't the dummy data script also be ran?

Edit: Nevermind...
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-20T01:56:42Z
user: login: andresjaramillo7
body: Looks good to me
state: DISMISSED
author_association: COLLABORATOR
submitted_at: 2025-05-20T02:08:01Z
user: login: BassedWarrior
body: Check for code style, some questions on the purpose of some code, and definitely shouldn't make the `Triggers.sql` file no longer valid as an SQL script.

This looks great, but I need it to follow some sort of standard.
state: CHANGES_REQUESTED
author_association: MEMBER
submitted_at: 2025-05-20T03:17:35Z
user: login: paydelimon22
body: The code looks a lot cleaner and I am glad that choosing whether to insert dummy data or not does not have to be two separate scripts. I think the only thing that couldn't be addressed was the fact that the triggers can not be run as their own script but I think it is still worthwhile to implement the changes made @BassedWarrior 
state: DISMISSED
author_association: MEMBER
submitted_at: 2025-05-22T16:19:10Z
user: login: BassedWarrior
body: I believe that with the changes I've made, this PR could be merged. It was an excellent job by @A09271. I just ironed out some quirks.
state: APPROVED
author_association: MEMBER
submitted_at: 2025-05-25T03:10:50Z
user: login: BassedWarrior
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-25T03:15:02Z
user: login: BassedWarrior
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-25T03:15:49Z
user: login: BassedWarrior
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-25T03:16:14Z
user: login: a01029143
body: ⣿⣿⣿⣿⣿⣿⠿⠟⠛⠿⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⠋⢀⣴⣶⣶⣄⡀⠙⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⡇⠀⣿⣿⣿⣿⣿⣿⣆⠈⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣷⠀⢻⣿⣿⣿⣿⣿⣿⡇⠸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣇⠈⣿⣿⣿⣿⣿⣿⡇⠀⣿⣿⡿⠿⠿⠿⠿⠿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⡄⠸⣿⣿⣿⣿⣿⡇⠀⠀⢀⣀⣀⣠⣤⣤⣄⡀⠀⠉⠻⣿⣿⣿
⣿⣿⣿⣿⣿⡷⠀⣹⣿⣿⣿⣿⣿⣿⣿⣿⠿⠟⠛⠛⠛⠛⣿⣷⣄⠀⠸⣿⣿
⣿⣿⣿⣿⡿⠁⢰⣿⣿⣿⣿⣿⣿⣿⠋⢀⣴⣶⣿⣿⣿⣿⣿⣿⣿⡆⢀⣿⣿
⣿⣿⠿⠛⠃⠀⣿⣿⣿⣿⣿⣿⣿⡇⢠⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⠀⠈⠻⣿
⡟⠁⣠⣴⣶⣴⣿⣿⣿⣿⣿⣿⣿⣷⠀⠙⠿⠿⠿⠛⠛⠉⠉⣀⣴⣾⡆⠀⢸
⡇⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢠⡶⠀⣴⣶⣶⣶⣾⣿⣿⣿⣿⡇⠀⣿
⡇⠀⣿⣿⣿⣿⣿⡿⠿⠿⠛⠋⣀⣴⣿⣧⠈⢿⣿⣿⣿⣿⣿⣿⡿⠋⠀⣰⣿
⡇⠀⢻⣿⣿⣿⣿⣷⣶⣶⣾⣿⣿⣿⣿⣿⠇⠀⡉⠛⠛⠋⢉⣀⣤⣦⠀⠹⣿
⣷⠀⠸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⠀⣾⣿⣿⣿⣿⣿⣿⣿⣿⠂⠀⣿
⣿⣧⠀⠹⣿⣿⣿⣿⠟⠛⢿⣿⣿⣿⣿⣷⣄⠘⠿⢿⣿⣿⣿⠿⠟⠋⢀⣸⣿
⣿⣿⣧⡀⠀⠙⠛⠋⢀⣄⣀⠈⠙⠛⠛⠛⠋⠁⠀⢀⣀⣀⣀⣠⣤⣶⣿⣿⣿
⣿⣿⣿⣿⣶⡶⠶⣶⣿⣿⣿⣿⣷⣶⣶⣶⣶⣶⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
state: APPROVED
author_association: COLLABORATOR
submitted_at: 2025-05-26T23:24:07Z
user: login: paydelimon22
body: This was a very good contribution and is ready to merged to development :+1: 
state: APPROVED
author_association: MEMBER
submitted_at: 2025-05-27T00:35:01Z

Review comments:
diff_hunk: @@ -0,0 +1,65 @@
+import dotenv from 'dotenv';
+import mariadb from 'mariadb';
+
+import path from "path";
+import { fileURLToPath } from 'url';
+
+const __filename = fileURLToPath(import.meta.url);
+const __dirname = path.dirname(__filename);
+
+dotenv.config();
+
+const pool = mariadb.createPool({
+  multipleStatements: true,
+  host: process.env.DB_HOST,
+  port: process.env.DB_PORT,
+  user: process.env.DB_USER,
+  password: process.env.DB_PASSWORD,
+});
+
+import fs from "fs";
+
+
+async function devdb() {
+    let conn;
+    const schema = fs.readFileSync("./database/Schema/Scheme.sql", 'utf8');
+        const prepop = fs.readFileSync("./database/Schema/Prepopulate.sql", 'utf8');
+        const triggers = fs.readFileSync("./database/Schema/Triggers.sql", 'utf8');
+        const views = fs.readFileSync("./database/Schema/Views.sql", 'utf8');
+        const dummy = fs.readFileSync("./database/Schema/Dummy.sql", 'utf8');
path: database/config/dev_db.js
user: login: BassedWarrior
body: Bad indenting.
```suggestion
    const schema = fs.readFileSync("./database/Schema/Scheme.sql", 'utf8');
    const prepop = fs.readFileSync("./database/Schema/Prepopulate.sql", 'utf8');
    const triggers = fs.readFileSync("./database/Schema/Triggers.sql", 'utf8');
    const views = fs.readFileSync("./database/Schema/Views.sql", 'utf8');
    const dummy = fs.readFileSync("./database/Schema/Dummy.sql", 'utf8');
```
created_at: 2025-05-20T02:40:24Z
author_association: MEMBER
diff_hunk: @@ -0,0 +1,65 @@
+import dotenv from 'dotenv';
+import mariadb from 'mariadb';
+
+import path from "path";
+import { fileURLToPath } from 'url';
+
+const __filename = fileURLToPath(import.meta.url);
+const __dirname = path.dirname(__filename);
+
+dotenv.config();
+
+const pool = mariadb.createPool({
+  multipleStatements: true,
+  host: process.env.DB_HOST,
+  port: process.env.DB_PORT,
+  user: process.env.DB_USER,
+  password: process.env.DB_PASSWORD,
+});
+
+import fs from "fs";
path: database/config/dev_db.js
user: login: BassedWarrior
body: Group `import` statements together and include comments for import purpose. Also stick to 4-space indenting.
```suggestion
import dotenv from 'dotenv';  // For environment variable loading.
import mariadb from 'mariadb';  // For connection to `mariadb` DataBase.

import fs from "fs";  // For accesing the FileSystem an reading the `.sql` scripts.

import path from "path";
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

dotenv.config();

const pool = mariadb.createPool({
    multipleStatements: true,
    host: process.env.DB_HOST,
    port: process.env.DB_PORT,
    user: process.env.DB_USER,
    password: process.env.DB_PASSWORD,
});
```
created_at: 2025-05-20T02:44:14Z
author_association: MEMBER
diff_hunk: @@ -0,0 +1,65 @@
+import dotenv from 'dotenv';
+import mariadb from 'mariadb';
+
+import path from "path";
+import { fileURLToPath } from 'url';
+
+const __filename = fileURLToPath(import.meta.url);
+const __dirname = path.dirname(__filename);
path: database/config/dev_db.js
user: login: BassedWarrior
body: What's the purpose of this?
created_at: 2025-05-20T02:44:30Z
author_association: MEMBER
diff_hunk: @@ -0,0 +1,65 @@
+import dotenv from 'dotenv';
+import mariadb from 'mariadb';
+
+import path from "path";
+import { fileURLToPath } from 'url';
+
+const __filename = fileURLToPath(import.meta.url);
+const __dirname = path.dirname(__filename);
+
+dotenv.config();
+
+const pool = mariadb.createPool({
+  multipleStatements: true,
+  host: process.env.DB_HOST,
+  port: process.env.DB_PORT,
+  user: process.env.DB_USER,
+  password: process.env.DB_PASSWORD,
+});
+
+import fs from "fs";
+
+
+async function devdb() {
+    let conn;
+    const schema = fs.readFileSync("./database/Schema/Scheme.sql", 'utf8');
+        const prepop = fs.readFileSync("./database/Schema/Prepopulate.sql", 'utf8');
+        const triggers = fs.readFileSync("./database/Schema/Triggers.sql", 'utf8');
+        const views = fs.readFileSync("./database/Schema/Views.sql", 'utf8');
+        const dummy = fs.readFileSync("./database/Schema/Dummy.sql", 'utf8');
+    try {
+        conn = await pool.getConnection();
+        
+        console.log("Executing Scheme.sql...");
+        await conn.query(schema);
+        console.log("Scheme.sql executed.");
+
+        console.log("Executing Prepopulate.sql...");
+        await conn.query(prepop);
+        console.log("Prepopulate.sql executed.");
+
+        console.log("Executing Triggers.sql...");
+        await conn.query(triggers);
+        console.log("Triggers.sql executed.");
+
+        console.log("Executing Views.sql...");
+        await conn.query(views);
+        console.log("Views.sql executed.");
+
+        console.log("Executing Dummy.sql...");
+        await conn.query(dummy);
+        console.log("Dummy.sql executed.");
+        
+    } catch (error) {
+        console.error(error);
+    } finally {
+        if (conn){
+        conn.release();
+        }
+        pool.end()
+          .then(() => console.log("Database connection pool closed."))
+          .catch(err => console.error("Error closing database connection pool:", err));
+    }
+}
+
+devdb();
path: database/config/dev_db.js
user: login: BassedWarrior
body: Should this include an `await`?
```suggestion
await devdb();
```
created_at: 2025-05-20T02:45:08Z
author_association: MEMBER
diff_hunk: @@ -0,0 +1,60 @@
+import dotenv from 'dotenv';
+import mariadb from 'mariadb';
+
+import path from "path";
+import { fileURLToPath } from 'url';
+
+const __filename = fileURLToPath(import.meta.url);
+const __dirname = path.dirname(__filename);
+
+dotenv.config();
+
+const pool = mariadb.createPool({
+  multipleStatements: true,
+  host: process.env.DB_HOST,
+  port: process.env.DB_PORT,
+  user: process.env.DB_USER,
+  password: process.env.DB_PASSWORD,
+});
+
+import fs from "fs";
+
+
+async function proddb() {
+    let conn;
+    const schema = fs.readFileSync("./database/Schema/Scheme.sql", 'utf8');
+        const prepop = fs.readFileSync("./database/Schema/Prepopulate.sql", 'utf8');
+        const triggers = fs.readFileSync("./database/Schema/Triggers.sql", 'utf8');
+        const views = fs.readFileSync("./database/Schema/Views.sql", 'utf8');
path: database/config/prod_db.js
user: login: BassedWarrior
body: Also indentation error
```suggestion
    const schema = fs.readFileSync("./database/Schema/Scheme.sql", 'utf8');
    const prepop = fs.readFileSync("./database/Schema/Prepopulate.sql", 'utf8');
    const triggers = fs.readFileSync("./database/Schema/Triggers.sql", 'utf8');
    const views = fs.readFileSync("./database/Schema/Views.sql", 'utf8');
```
created_at: 2025-05-20T02:45:35Z
author_association: MEMBER
diff_hunk: @@ -0,0 +1,60 @@
+import dotenv from 'dotenv';
+import mariadb from 'mariadb';
+
+import path from "path";
+import { fileURLToPath } from 'url';
+
+const __filename = fileURLToPath(import.meta.url);
+const __dirname = path.dirname(__filename);
+
+dotenv.config();
+
+const pool = mariadb.createPool({
+  multipleStatements: true,
+  host: process.env.DB_HOST,
+  port: process.env.DB_PORT,
+  user: process.env.DB_USER,
+  password: process.env.DB_PASSWORD,
+});
+
+import fs from "fs";
+
+
+async function proddb() {
+    let conn;
+    const schema = fs.readFileSync("./database/Schema/Scheme.sql", 'utf8');
+        const prepop = fs.readFileSync("./database/Schema/Prepopulate.sql", 'utf8');
+        const triggers = fs.readFileSync("./database/Schema/Triggers.sql", 'utf8');
+        const views = fs.readFileSync("./database/Schema/Views.sql", 'utf8');
+    try {
+        conn = await pool.getConnection();
+        
+        console.log("Executing Scheme.sql...");
+        await conn.query(schema);
+        console.log("Scheme.sql executed.");
+
+        console.log("Executing Prepopulate.sql...");
+        await conn.query(prepop);
+        console.log("Prepopulate.sql executed.");
+
+        console.log("Executing Triggers.sql...");
+        await conn.query(triggers);
+        console.log("Triggers.sql executed.");
+
+        console.log("Executing Views.sql...");
+        await conn.query(views);
+        console.log("Views.sql executed.");
+        
+    } catch (error) {
+        console.error(error);
+    } finally {
+        if (conn){
+        conn.release();
+        }
+        pool.end()
+          .then(() => console.log("Database connection pool closed."))
+          .catch(err => console.error("Error closing database connection pool:", err));
+    }
+}
+
+proddb();
path: database/config/prod_db.js
user: login: BassedWarrior
body: Same question regarding `await`
```suggestion
await proddb();
```
created_at: 2025-05-20T02:45:56Z
author_association: MEMBER
diff_hunk: @@ -0,0 +1,60 @@
+import dotenv from 'dotenv';
+import mariadb from 'mariadb';
+
+import path from "path";
+import { fileURLToPath } from 'url';
+
+const __filename = fileURLToPath(import.meta.url);
+const __dirname = path.dirname(__filename);
+
+dotenv.config();
+
+const pool = mariadb.createPool({
+  multipleStatements: true,
+  host: process.env.DB_HOST,
+  port: process.env.DB_PORT,
+  user: process.env.DB_USER,
+  password: process.env.DB_PASSWORD,
+});
+
+import fs from "fs";
path: database/config/prod_db.js
user: login: BassedWarrior
body: Same rearrangement of `import`s and comments. Also stick to 4-space indenting.
```suggestion
import dotenv from 'dotenv';  // For environment variable loading.
import mariadb from 'mariadb';  // For connection to `mariadb` DataBase.

import fs from "fs";  // For accesing the FileSystem an reading the `.sql` scripts.

import path from "path";
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

dotenv.config();

const pool = mariadb.createPool({
    multipleStatements: true,
    host: process.env.DB_HOST,
    port: process.env.DB_PORT,
    user: process.env.DB_USER,
    password: process.env.DB_PASSWORD,
});
```
created_at: 2025-05-20T02:47:36Z
author_association: MEMBER
diff_hunk: @@ -0,0 +1,60 @@
+import dotenv from 'dotenv';
+import mariadb from 'mariadb';
+
+import path from "path";
+import { fileURLToPath } from 'url';
+
+const __filename = fileURLToPath(import.meta.url);
+const __dirname = path.dirname(__filename);
path: database/config/prod_db.js
user: login: BassedWarrior
body: Again don't know the purpose of this.
created_at: 2025-05-20T02:49:31Z
author_association: MEMBER
diff_hunk: @@ -0,0 +1,65 @@
+import dotenv from 'dotenv';
+import mariadb from 'mariadb';
+
+import path from "path";
+import { fileURLToPath } from 'url';
+
+const __filename = fileURLToPath(import.meta.url);
+const __dirname = path.dirname(__filename);
+
+dotenv.config();
+
+const pool = mariadb.createPool({
+  multipleStatements: true,
+  host: process.env.DB_HOST,
+  port: process.env.DB_PORT,
+  user: process.env.DB_USER,
+  password: process.env.DB_PASSWORD,
+});
+
+import fs from "fs";
+
+
+async function devdb() {
+    let conn;
+    const schema = fs.readFileSync("./database/Schema/Scheme.sql", 'utf8');
+        const prepop = fs.readFileSync("./database/Schema/Prepopulate.sql", 'utf8');
+        const triggers = fs.readFileSync("./database/Schema/Triggers.sql", 'utf8');
+        const views = fs.readFileSync("./database/Schema/Views.sql", 'utf8');
+        const dummy = fs.readFileSync("./database/Schema/Dummy.sql", 'utf8');
+    try {
+        conn = await pool.getConnection();
+        
+        console.log("Executing Scheme.sql...");
+        await conn.query(schema);
+        console.log("Scheme.sql executed.");
+
+        console.log("Executing Prepopulate.sql...");
+        await conn.query(prepop);
+        console.log("Prepopulate.sql executed.");
+
+        console.log("Executing Triggers.sql...");
+        await conn.query(triggers);
+        console.log("Triggers.sql executed.");
+
+        console.log("Executing Views.sql...");
+        await conn.query(views);
+        console.log("Views.sql executed.");
+
+        console.log("Executing Dummy.sql...");
+        await conn.query(dummy);
+        console.log("Dummy.sql executed.");
+        
+    } catch (error) {
+        console.error(error);
+    } finally {
+        if (conn){
+        conn.release();
+        }
+        pool.end()
+          .then(() => console.log("Database connection pool closed."))
+          .catch(err => console.error("Error closing database connection pool:", err));
path: database/config/dev_db.js
user: login: BassedWarrior
body: Stick to 4-space indenting.
```suggestion
        if (conn){
            conn.release();
        }
        pool.end()
            .then(() => console.log("Database connection pool closed."))
            .catch(err => console.error("Error closing database connection pool:", err));
```
created_at: 2025-05-20T02:50:01Z
author_association: MEMBER
diff_hunk: @@ -0,0 +1,60 @@
+import dotenv from 'dotenv';
+import mariadb from 'mariadb';
+
+import path from "path";
+import { fileURLToPath } from 'url';
+
+const __filename = fileURLToPath(import.meta.url);
+const __dirname = path.dirname(__filename);
+
+dotenv.config();
+
+const pool = mariadb.createPool({
+  multipleStatements: true,
+  host: process.env.DB_HOST,
+  port: process.env.DB_PORT,
+  user: process.env.DB_USER,
+  password: process.env.DB_PASSWORD,
+});
+
+import fs from "fs";
+
+
+async function proddb() {
+    let conn;
+    const schema = fs.readFileSync("./database/Schema/Scheme.sql", 'utf8');
+        const prepop = fs.readFileSync("./database/Schema/Prepopulate.sql", 'utf8');
+        const triggers = fs.readFileSync("./database/Schema/Triggers.sql", 'utf8');
+        const views = fs.readFileSync("./database/Schema/Views.sql", 'utf8');
+    try {
+        conn = await pool.getConnection();
+        
+        console.log("Executing Scheme.sql...");
+        await conn.query(schema);
+        console.log("Scheme.sql executed.");
+
+        console.log("Executing Prepopulate.sql...");
+        await conn.query(prepop);
+        console.log("Prepopulate.sql executed.");
+
+        console.log("Executing Triggers.sql...");
+        await conn.query(triggers);
+        console.log("Triggers.sql executed.");
+
+        console.log("Executing Views.sql...");
+        await conn.query(views);
+        console.log("Views.sql executed.");
+        
+    } catch (error) {
+        console.error(error);
+    } finally {
+        if (conn){
+        conn.release();
+        }
+        pool.end()
+          .then(() => console.log("Database connection pool closed."))
+          .catch(err => console.error("Error closing database connection pool:", err));
path: database/config/prod_db.js
user: login: BassedWarrior
body: Stick to 4-space indentation
```suggestion
        if (conn){
            conn.release();
        }
        pool.end()
            .then(() => console.log("Database connection pool closed."))
            .catch(err => console.error("Error closing database connection pool:", err));
```
created_at: 2025-05-20T02:50:39Z
author_association: MEMBER
diff_hunk: @@ -7,6 +7,8 @@
     "scripts": {
         "start": "node index.js",
         "dev": "nodemon index.js",
+        "dbd": "node ./database/config/dev_db.js",
+        "pdb": "node ./database/config/prod_db.js",
path: package.json
user: login: BassedWarrior
body: Maybe use clearer names?
```suggestion
        "setup-db-dummy": "node ./database/config/dev_db.js",
        "setup-db-empty": "node ./database/config/prod_db.js",
```
created_at: 2025-05-20T02:51:42Z
author_association: MEMBER
path: database/Schema/Triggers.sql
user: login: BassedWarrior
body: I deffinitely think that this should be kept as a valid `.sql` file. Which it currently isn't. Hence I'd suggest instead finding how to fix the `con.query` method used to execute them to be able to deal with using the `DELIMITER` directives.

Looking at the documentation, I found this [connection.batch()](https://github.com/mariadb-corporation/mariadb-connector-nodejs/blob/master/documentation/promise-api.md#connectionbatchsql-values--promise) method which sounds like _batch mode_ (`mariadb < Script.sql`). But I don't know if that's how it works.
created_at: 2025-05-20T02:55:49Z
author_association: MEMBER
path: database/Schema/Triggers.sql
user: login: BassedWarrior
body: Although I did end up figuring out that the correct method was [`connection.importFile()`](https://github.com/mariadb-corporation/mariadb-connector-nodejs/blob/master/documentation/promise-api.md#connectionimportfileoptions--promise), the pros of having this script outweigh the hassle of the file `database/Schema/Triggers.sql` no longer be CLI-compatible. As outlined by @paydelimon22 in https://github.com/101-Coconsulting/TC3005B.501-Backend/pull/276#pullrequestreview-2861855792, and as per my commit 8c786e0 body. But it will regardless be a very important note for the @101-Coconsulting/db team for the creation of future triggers and the like.
created_at: 2025-05-25T03:15:01Z
author_association: MEMBER
diff_hunk: @@ -0,0 +1,60 @@
+import dotenv from 'dotenv';
+import mariadb from 'mariadb';
+
+import path from "path";
+import { fileURLToPath } from 'url';
+
+const __filename = fileURLToPath(import.meta.url);
+const __dirname = path.dirname(__filename);
+
+dotenv.config();
+
+const pool = mariadb.createPool({
+  multipleStatements: true,
+  host: process.env.DB_HOST,
+  port: process.env.DB_PORT,
+  user: process.env.DB_USER,
+  password: process.env.DB_PASSWORD,
+});
+
+import fs from "fs";
+
+
+async function proddb() {
+    let conn;
+    const schema = fs.readFileSync("./database/Schema/Scheme.sql", 'utf8');
+        const prepop = fs.readFileSync("./database/Schema/Prepopulate.sql", 'utf8');
+        const triggers = fs.readFileSync("./database/Schema/Triggers.sql", 'utf8');
+        const views = fs.readFileSync("./database/Schema/Views.sql", 'utf8');
+    try {
+        conn = await pool.getConnection();
+        
+        console.log("Executing Scheme.sql...");
+        await conn.query(schema);
+        console.log("Scheme.sql executed.");
+
+        console.log("Executing Prepopulate.sql...");
+        await conn.query(prepop);
+        console.log("Prepopulate.sql executed.");
+
+        console.log("Executing Triggers.sql...");
+        await conn.query(triggers);
+        console.log("Triggers.sql executed.");
+
+        console.log("Executing Views.sql...");
+        await conn.query(views);
+        console.log("Views.sql executed.");
+        
+    } catch (error) {
+        console.error(error);
+    } finally {
+        if (conn){
+        conn.release();
+        }
+        pool.end()
+          .then(() => console.log("Database connection pool closed."))
+          .catch(err => console.error("Error closing database connection pool:", err));
+    }
+}
+
+proddb();
path: database/config/prod_db.js
user: login: BassedWarrior
body: During testing, this has proven not to be needed.
created_at: 2025-05-25T03:15:49Z
author_association: MEMBER
diff_hunk: @@ -0,0 +1,65 @@
+import dotenv from 'dotenv';
+import mariadb from 'mariadb';
+
+import path from "path";
+import { fileURLToPath } from 'url';
+
+const __filename = fileURLToPath(import.meta.url);
+const __dirname = path.dirname(__filename);
+
+dotenv.config();
+
+const pool = mariadb.createPool({
+  multipleStatements: true,
+  host: process.env.DB_HOST,
+  port: process.env.DB_PORT,
+  user: process.env.DB_USER,
+  password: process.env.DB_PASSWORD,
+});
+
+import fs from "fs";
+
+
+async function devdb() {
+    let conn;
+    const schema = fs.readFileSync("./database/Schema/Scheme.sql", 'utf8');
+        const prepop = fs.readFileSync("./database/Schema/Prepopulate.sql", 'utf8');
+        const triggers = fs.readFileSync("./database/Schema/Triggers.sql", 'utf8');
+        const views = fs.readFileSync("./database/Schema/Views.sql", 'utf8');
+        const dummy = fs.readFileSync("./database/Schema/Dummy.sql", 'utf8');
+    try {
+        conn = await pool.getConnection();
+        
+        console.log("Executing Scheme.sql...");
+        await conn.query(schema);
+        console.log("Scheme.sql executed.");
+
+        console.log("Executing Prepopulate.sql...");
+        await conn.query(prepop);
+        console.log("Prepopulate.sql executed.");
+
+        console.log("Executing Triggers.sql...");
+        await conn.query(triggers);
+        console.log("Triggers.sql executed.");
+
+        console.log("Executing Views.sql...");
+        await conn.query(views);
+        console.log("Views.sql executed.");
+
+        console.log("Executing Dummy.sql...");
+        await conn.query(dummy);
+        console.log("Dummy.sql executed.");
+        
+    } catch (error) {
+        console.error(error);
+    } finally {
+        if (conn){
+        conn.release();
+        }
+        pool.end()
+          .then(() => console.log("Database connection pool closed."))
+          .catch(err => console.error("Error closing database connection pool:", err));
+    }
+}
+
+devdb();
path: database/config/dev_db.js
user: login: BassedWarrior
body: During testing, this has proven not to be needed.
created_at: 2025-05-25T03:16:14Z
author_association: MEMBER

PR comments:
user: login: paydelimon22
created_at: 2025-05-20T02:26:40Z
body: I think it might be redundant to have two scripts that are very similar except for the addition of dummy data. If this could be passed as an argument, I think the code would be a lot cleaner. However, if this is too complicated to implement, the current version is also fine
author_association: MEMBER
user: login: paydelimon22
created_at: 2025-05-20T17:00:23Z
body: I will close this PR temporarily to avoid confusions for the current release but once PR #223 is merged I will reopen this
author_association: MEMBER
user: login: BassedWarrior
created_at: 2025-05-25T03:02:08Z
body: I agree that the benefits of this outweigh the downside of making the `database/Schema/Triggers.sql` file no longer CLI compatible. I did however make some adjustments that preferred the use of `importFile` over just reading the files and treating them as `query`s.

And also took the liberty of already including the documentation for running the scripts, and scrapping the old instructions that detailed manually loading each script.

author_association: MEMBER
user: login: BassedWarrior
created_at: 2025-05-25T03:09:33Z
body: Also just merged to get rid of conflicts, and got rid of some whitespace and missing newline at the end of the file.

author_association: MEMBER