This repository has been archived by the owner on Jun 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
117 lines (100 loc) · 2.7 KB
/
database.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*******************************
* Configuration
******************************/
// Connection string
var dbConnectionString = "mongodb://localhost/elections";
/*******************************
* Required modules
******************************/
//Mongoose for MongoDB ORM
var mongoose = require('mongoose');
/*******************************
* Database
******************************/
// Connect to the db
mongoose.connect(dbConnectionString);
// Define our Schemas
var Schema = mongoose.Schema;
// Schema for keeping track of vote totals
var VoteTotals = new Schema({
constituency: {
type: String,
required: true,
unique: true,
index: true
},
votes: {
conservative: Number,
labour: Number,
libdem: Number,
ukip: Number,
green: Number,
novote: Number
}
});
// Define increment() so that we can do that super quick
VoteTotals.statics.increment = function (constituency, party, callback) {
var inc = {};
inc['votes.' + party] = 1;
return this.collection.update({
'constituency': constituency
}, {
$inc: inc
}, {
upsert: true
}, callback);
};
// Schema for keeping track of actual votes
var Vote = new Schema({
email: {
type: String,
required: true,
unique: true,
index: true
},
constituency: {
type: String,
required: true,
index: true
},
party: {
type: String,
required: true,
index: true
}
});
// Define some middleware to increment vote counters when votes are saved
// In each, "this" is the document about to be saved
// Update the constituency counter
Vote.pre('save', function (next) {
VoteTotals.increment(this.constituency, this.party, next);
});
// Update the uk counter
Vote.pre('save', function (next) {
VoteTotals.increment("uk", this.party, next);
});
// Initialise the schemas
mongoose.model('Vote', Vote);
var Vote = mongoose.model('Vote');
mongoose.model('VoteTotals', VoteTotals);
var VoteTotals = mongoose.model('VoteTotals');
/*******************************
* Exported functions
******************************/
// Return the current votes for every constituency to the callback
function getCurrentVotes(callback) {
// Return all documents in VoteTotals schema
VoteTotals.find({}, function (err, totals) {
callback(err, totals);
});
}
// Save a vote into the db and update the totals
function saveVote(voteToSave, callback) {
// Try to put the vote into the Vote schema
var vote = new Vote(voteToSave);
vote.save(function (err) {
callback(err);
});
}
exports.getCurrentVotes = getCurrentVotes;
exports.saveVote = saveVote;