-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathdatabase.js
More file actions
27 lines (22 loc) · 764 Bytes
/
database.js
File metadata and controls
27 lines (22 loc) · 764 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
const mongoose = require('mongoose');
const config = require('./dbConfig.json');
const _dbName = 'voter';
const _userCollectionName = 'user';
const _candidateCollectionName = 'candidate';
if (!config.userName) {
throw Error('Database not configured. Set environment variables');
}
const userSchema = new mongoose.Schema({
email: String,
votes: [{ type: String }],
});
exports.UserCol = mongoose.model(_userCollectionName, userSchema);
const candidateSchema = new mongoose.Schema({
name: String,
url: String,
votes: Number,
id: String,
});
exports.CandidateCol = mongoose.model(_candidateCollectionName, candidateSchema);
const dbUrl = `mongodb+srv://${config.userName}:${config.password}@${config.hostname}/${_dbName}`;
mongoose.connect(dbUrl);