Skip to content

Commit

Permalink
Add Database mongoDB
Browse files Browse the repository at this point in the history
  • Loading branch information
VuesseEDM committed May 17, 2024
1 parent 02d1807 commit af4aefd
Show file tree
Hide file tree
Showing 3 changed files with 265 additions and 1 deletion.
215 changes: 215 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"license": "ISC",
"dependencies": {
"express": "^4.19.2",
"mongoose": "^8.4.0",
"socket.io": "^4.7.5"
}
}
}
48 changes: 48 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,54 @@ const PORT = process.env.PORT || 3000;
const sampleUrl =
"https://opentdb.com/api.php?amount=10&category=9&difficulty=easy&type=multiple";

//DATABASE

const mongoose = require("mongoose");
const dbURI = "mongodb://localhost:27017/Quiz_Database";

mongoose.connect(dbURI, { useNewUrlParser: true, useUnifiedTopology: true });

const db = mongoose.connection;

db.on(
"error",
console.error.bind(console, "Errore di connessione al database:")
);
db.once("open", () => {
console.log("Connesso al database MongoDB.");
});

const gameSchema = new mongoose.Schema({
id: { type: Number },
users: [{ type: mongoose.Schema.Types.ObjectId, ref: "User" }],
start: { type: Date, default: Date.now },
timestamp: { type: Date, default: Date.now },
});

const userSchema = new mongoose.Schema({
id: { type: Number },
username: { type: String, required: true },
role: { type: String, required: true },
created_at: { type: Date, default: Date.now },
});

const questionSchema = new mongoose.Schema({
id: { type: Number },
question: { type: String, required: true },
game_id: { type: mongoose.Schema.Types.ObjectId, ref: "Game" },
answers: [{ type: String }],
user_id: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
correct: { type: Boolean, required: true },
question_id: { type: mongoose.Schema.Types.ObjectId, ref: "Question" },
answer: { type: Boolean, required: true },
});

const Game = mongoose.model("Game", gameSchema);
const User = mongoose.model("User", userSchema);
const Question = mongoose.model("Question", questionSchema);

//FINE DATABASE

var connectedUsers = 0;
var readyUsers = [];
var ranking = [];
Expand Down

0 comments on commit af4aefd

Please sign in to comment.