forked from kshitijkota/Blockheads
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
31 lines (26 loc) · 934 Bytes
/
database.js
File metadata and controls
31 lines (26 loc) · 934 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const mongoose = require('mongoose');
// Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/voting', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
// Check if the connection is successful
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
db.once('open', () => {
console.log('Connected to MongoDB');
});
// Define schema for voters
const voterSchema = new mongoose.Schema({
address: { type: String, required: true, unique: true },
isRegistered: { type: Boolean, default: false },
hasVoted: { type: Boolean, default: false },
});
// Define schema for election results
const resultSchema = new mongoose.Schema({
candidateName: { type: String, required: true },
voteCount: { type: Number, default: 0 },
});
// Create models for the schemas
const VoterModel = mongoose.model('Voter', voterSchema);
const ResultModel = mongoose.model('Result', resultSchema);