Commit 7bd9d697 authored by Tharaka it19975696's avatar Tharaka it19975696 🎧

Added Prisma ORM for DB interactions

parent c7dd5c0c
...@@ -5,4 +5,17 @@ DB_PASSWORD=1234!@#$Qw ...@@ -5,4 +5,17 @@ DB_PASSWORD=1234!@#$Qw
//MySQL //MySQL
MYSQL_HOST=localhost // Replace with your MySQL server host (e.g., localhost)
MYSQL_USER=tharaka // Replace with your MySQL database username
MYSQL_PASSWORD=1234 // Replace with your MySQL database password
MYSQL_DATABASE=MathsSystem // Replace with your MySQL database name
# This was inserted by `prisma init`:
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
DATABASE_URL="mysql://tharaka:1234@localhost:3306/mathssystem"
\ No newline at end of file
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
"version": "0.1.0", "version": "0.1.0",
"dependencies": { "dependencies": {
"@nivo/bar": "^0.85.1", "@nivo/bar": "^0.85.1",
"@prisma/client": "^5.12.1",
"@testing-library/jest-dom": "^5.17.0", "@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0", "@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0", "@testing-library/user-event": "^13.5.0",
...@@ -26,7 +27,8 @@ ...@@ -26,7 +27,8 @@
"web-vitals": "^2.1.4" "web-vitals": "^2.1.4"
}, },
"devDependencies": { "devDependencies": {
"@babel/plugin-proposal-private-property-in-object": "^7.21.11" "@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"prisma": "^5.12.1"
} }
}, },
"node_modules/@aashutoshrathi/word-wrap": { "node_modules/@aashutoshrathi/word-wrap": {
...@@ -3557,6 +3559,68 @@ ...@@ -3557,6 +3559,68 @@
} }
} }
}, },
"node_modules/@prisma/client": {
"version": "5.12.1",
"resolved": "https://registry.npmjs.org/@prisma/client/-/client-5.12.1.tgz",
"integrity": "sha512-6/JnizEdlSBxDIdiLbrBdMW5NqDxOmhXAJaNXiPpgzAPr/nLZResT6MMpbOHLo5yAbQ1Vv5UU8PTPRzb0WIxdA==",
"hasInstallScript": true,
"engines": {
"node": ">=16.13"
},
"peerDependencies": {
"prisma": "*"
},
"peerDependenciesMeta": {
"prisma": {
"optional": true
}
}
},
"node_modules/@prisma/debug": {
"version": "5.12.1",
"resolved": "https://registry.npmjs.org/@prisma/debug/-/debug-5.12.1.tgz",
"integrity": "sha512-kd/wNsR0klrv79o1ITsbWxYyh4QWuBidvxsXSParPsYSu0ircUmNk3q4ojsgNc3/81b0ozg76iastOG43tbf8A==",
"devOptional": true
},
"node_modules/@prisma/engines": {
"version": "5.12.1",
"resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-5.12.1.tgz",
"integrity": "sha512-HQDdglLw2bZR/TXD2Y+YfDMvi5Q8H+acbswqOsWyq9pPjBLYJ6gzM+ptlTU/AV6tl0XSZLU1/7F4qaWa8bqpJA==",
"devOptional": true,
"hasInstallScript": true,
"dependencies": {
"@prisma/debug": "5.12.1",
"@prisma/engines-version": "5.12.0-21.473ed3124229e22d881cb7addf559799debae1ab",
"@prisma/fetch-engine": "5.12.1",
"@prisma/get-platform": "5.12.1"
}
},
"node_modules/@prisma/engines-version": {
"version": "5.12.0-21.473ed3124229e22d881cb7addf559799debae1ab",
"resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-5.12.0-21.473ed3124229e22d881cb7addf559799debae1ab.tgz",
"integrity": "sha512-6yvO8s80Tym61aB4QNtYZfWVmE3pwqe807jEtzm8C5VDe7nw8O1FGX3TXUaXmWV0fQTIAfRbeL2Gwrndabp/0g==",
"devOptional": true
},
"node_modules/@prisma/fetch-engine": {
"version": "5.12.1",
"resolved": "https://registry.npmjs.org/@prisma/fetch-engine/-/fetch-engine-5.12.1.tgz",
"integrity": "sha512-qSs3KcX1HKcea1A+hlJVK/ljj0PNIUHDxAayGMvgJBqmaN32P9tCidlKz1EGv6WoRFICYnk3Dd/YFLBwnFIozA==",
"devOptional": true,
"dependencies": {
"@prisma/debug": "5.12.1",
"@prisma/engines-version": "5.12.0-21.473ed3124229e22d881cb7addf559799debae1ab",
"@prisma/get-platform": "5.12.1"
}
},
"node_modules/@prisma/get-platform": {
"version": "5.12.1",
"resolved": "https://registry.npmjs.org/@prisma/get-platform/-/get-platform-5.12.1.tgz",
"integrity": "sha512-pgIR+pSvhYHiUcqXVEZS31NrFOTENC9yFUdEAcx7cdQBoZPmHVjtjN4Ss6NzVDMYPrKJJ51U14EhEoeuBlMioQ==",
"devOptional": true,
"dependencies": {
"@prisma/debug": "5.12.1"
}
},
"node_modules/@react-spring/animated": { "node_modules/@react-spring/animated": {
"version": "9.7.3", "version": "9.7.3",
"resolved": "https://registry.npmjs.org/@react-spring/animated/-/animated-9.7.3.tgz", "resolved": "https://registry.npmjs.org/@react-spring/animated/-/animated-9.7.3.tgz",
...@@ -15405,6 +15469,22 @@ ...@@ -15405,6 +15469,22 @@
"url": "https://github.com/chalk/ansi-styles?sponsor=1" "url": "https://github.com/chalk/ansi-styles?sponsor=1"
} }
}, },
"node_modules/prisma": {
"version": "5.12.1",
"resolved": "https://registry.npmjs.org/prisma/-/prisma-5.12.1.tgz",
"integrity": "sha512-SkMnb6wyIxTv9ACqiHBI2u9gD6y98qXRoCoLEnZsF6yee5Qg828G+ARrESN+lQHdw4maSZFFSBPPDpvSiVTo0Q==",
"devOptional": true,
"hasInstallScript": true,
"dependencies": {
"@prisma/engines": "5.12.1"
},
"bin": {
"prisma": "build/index.js"
},
"engines": {
"node": ">=16.13"
}
},
"node_modules/process-nextick-args": { "node_modules/process-nextick-args": {
"version": "2.0.1", "version": "2.0.1",
"resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz",
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
"private": true, "private": true,
"dependencies": { "dependencies": {
"@nivo/bar": "^0.85.1", "@nivo/bar": "^0.85.1",
"@prisma/client": "^5.12.1",
"@testing-library/jest-dom": "^5.17.0", "@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0", "@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0", "@testing-library/user-event": "^13.5.0",
...@@ -45,7 +46,8 @@ ...@@ -45,7 +46,8 @@
] ]
}, },
"devDependencies": { "devDependencies": {
"@babel/plugin-proposal-private-property-in-object": "^7.21.11" "@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"prisma": "^5.12.1"
}, },
"proxy": "http://localhost:5000" "proxy": "http://localhost:5000"
} }
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
model performancereport {
reportID Int @id @default(autoincrement())
studentID Int
details String @db.Text
student student @relation(fields: [studentID], references: [studentID], onDelete: Cascade, onUpdate: NoAction, map: "performancereport_ibfk_1")
@@index([studentID], map: "studentID")
}
model practiceset {
setID Int @id @default(autoincrement())
studentID Int
student student @relation(fields: [studentID], references: [studentID], onDelete: Cascade, onUpdate: NoAction, map: "practiceset_ibfk_1")
@@index([studentID], map: "studentID")
}
model question {
questionID Int @id @default(autoincrement())
content String @db.Text
correctAnswer String @db.VarChar(255)
difficultyLevel question_difficultyLevel
bloomsLevel question_bloomsLevel
learningType String @db.VarChar(255)
graphicOrAnimation String? @db.VarChar(255)
subtopicID Int
subtopic subtopic @relation(fields: [subtopicID], references: [subtopicID], onDelete: Cascade, onUpdate: NoAction, map: "question_ibfk_1")
response response[]
@@index([subtopicID], map: "subtopicID")
}
model response {
responseID Int @id @default(autoincrement())
questionID Int
studentID Int
answer String @db.VarChar(255)
timestamp DateTime @db.DateTime(0)
question question @relation(fields: [questionID], references: [questionID], onDelete: Cascade, onUpdate: NoAction, map: "response_ibfk_1")
student student @relation(fields: [studentID], references: [studentID], onDelete: Cascade, onUpdate: NoAction, map: "response_ibfk_2")
@@index([questionID], map: "questionID")
@@index([studentID], map: "studentID")
}
model student {
studentID Int @id @default(autoincrement())
username String @db.VarChar(255)
password String @db.VarChar(255)
learningStyle student_learningStyle
mathematicalProficiency student_mathematicalProficiency
performancereport performancereport[]
practiceset practiceset[]
response response[]
}
model subtopic {
subtopicID Int @id @default(autoincrement())
name String @db.VarChar(255)
learningOutcomes String @db.Text
periods Int
question question[]
}
model teacher {
teacherID Int @id @default(autoincrement())
username String @db.VarChar(255)
password String @db.VarChar(255)
}
enum question_difficultyLevel {
High
Medium
Low
}
enum student_learningStyle {
Visual
Auditory
Kinesthetic
Read_Write @map("Read/Write")
}
enum question_bloomsLevel {
remembering
understanding
applying
}
enum student_mathematicalProficiency {
High
Medium
Low
}
// server/index.js // server/index.js
const express = require('express'); const express = require('express');
const cors = require('cors'); const cors = require('cors');
const mysql = require('mysql'); const { PrismaClient } = require('@prisma/client');
const app = express(); const app = express();
const prisma = new PrismaClient();
app.use(cors()); app.use(cors());
app.use(express.json()); app.use(express.json());
const db = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME
});
app.get('/', (req, res) => { app.get('/', (req, res) => {
res.json({ message: 'Welcome to the server API!' }); res.json({ message: 'Welcome to the server API!' });
}); });
app.get('/test-db', (req, res) => { // Test endpoint to ensure Prisma is connected to the database
db.query('SELECT 1 + 1 AS solution', (err, results) => { app.get('/test-db', async (req, res) => {
if (err) { try {
return res.status(500).send(err.toString()); const response = await prisma.$queryRaw`SELECT 1 + 1 AS solution`;
res.status(200).json({ solution: response[0].solution });
} catch (err) {
res.status(500).send(err.message);
}
});
// Using Prisma Client to get subtopics data
app.get('/subtopics', async (req, res) => {
try {
const subtopics = await prisma.subtopic.findMany();
res.json(subtopics);
} catch (err) {
res.status(500).send(err.message);
} }
res.status(200).json({ solution: results[0].solution });
});
}); });
// Start the server
const PORT = process.env.PORT || 5000; const PORT = process.env.PORT || 5000;
app.listen(PORT, () => { app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`); console.log(`Server running on port ${PORT}`);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment