Skip to content

Commit 570a544

Browse files
committed
Re modify code structure
1 parent 9688d91 commit 570a544

File tree

9 files changed

+281
-75
lines changed

9 files changed

+281
-75
lines changed

.env_sample

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DATABASE=mydb
2+
ENDPOINT=MYSQL_ENDPOINT
3+
PASSWORD=PASSWORD
4+
MYSQL_PORT=3306
5+
USER=admnin

index.js

Lines changed: 0 additions & 69 deletions
This file was deleted.

package-lock.json

Lines changed: 116 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
"description": "Api to prcoess json and store it on RDS",
55
"main": "index.js",
66
"scripts": {
7-
"start": "node index.js"
7+
"start": "node ./src/index.js"
88
},
99
"author": "Gauraang",
1010
"license": "ISC",
1111
"dependencies": {
1212
"dotenv": "^16.0.3",
1313
"express": "^4.18.2",
14-
"mysql": "^2.18.1"
14+
"mysql": "^2.18.1",
15+
"mysql2": "^2.3.3"
1516
}
1617
}

src/api/routes/routes.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const {addData, showUsersTable, clearUsersTable} = require('../../db/models/users');
2+
3+
const express = require('express');
4+
const { json } = require('express');
5+
const router = express.Router();
6+
router.use(express.json());
7+
8+
router.get('/', (req, res) => {
9+
res.status(200).send("You have reached the home page");
10+
})
11+
12+
router.get('/show', async (req,res) => {
13+
try{
14+
const table = await showUsersTable();
15+
console.log("Table returned successfully");
16+
res.send(table);
17+
}catch(error){
18+
res.status(500).send({ error: 'An error occurred while fetching users' });
19+
}
20+
})
21+
22+
router.post('/clear', async (req, res) => {
23+
try{
24+
await clearUsersTable();
25+
console.log("Table cleared successfully");
26+
res.send("Table cleared successfuly");
27+
}catch(error){
28+
res.status(500).send({ error: 'An error occurred while fetching users' });
29+
}
30+
})
31+
32+
33+
router.post('/data', async (req,res) => {
34+
const json = req.body;
35+
try{
36+
await addData(json);
37+
console.log("Data added to the table successfully");
38+
res.send("Data added to the table successfully");
39+
}catch(error){
40+
console.log(error);
41+
res.status(500).send({ error: 'An error occurred while adding data' });
42+
}
43+
})
44+
45+
module.exports = router;

src/db.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
require('dotenv').config()
2+
const mysql = require('mysql2/promise');
3+
4+
console.log(process.env.ENDPOINT);
5+
const pool = mysql.createPool({
6+
host: process.env.ENDPOINT,
7+
user: process.env.USER,
8+
password: process.env.PASSWORD,
9+
database: process.env.DATABASE,
10+
port: process.env.MYSQL_PORT,
11+
waitForConnections: true,
12+
connectionLimit: 10,
13+
queueLimit: 0
14+
});
15+
16+
17+
// Query function
18+
async function query(sql, params) {
19+
try {
20+
// Get a connection from the pool
21+
const connection = await pool.getConnection();
22+
// Use the connection to run the query
23+
const [rows] = await connection.query(sql, params);
24+
// Release the connection back to the pool
25+
connection.release();
26+
return rows;
27+
} catch (error) {
28+
console.error(error);
29+
throw error;
30+
}
31+
}
32+
33+
34+
35+
async function createTable(tableName, schema){
36+
const sql = `
37+
CREATE TABLE ${tableName} (
38+
${schema}
39+
)
40+
`;
41+
await query(sql);
42+
}
43+
44+
async function showTable(tableName){
45+
const sql = `select * from ${tableName}`
46+
const table = await query(sql);
47+
return table;
48+
}
49+
50+
async function clearTable(tableName){
51+
const sql = `TRUNCATE TABLE ${tableName}`;
52+
await query(sql);
53+
}
54+
55+
56+
module.exports = {
57+
query,
58+
createTable,
59+
showTable,
60+
clearTable,
61+
};

0 commit comments

Comments
 (0)