-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
203 lines (181 loc) · 4.5 KB
/
database.js
File metadata and controls
203 lines (181 loc) · 4.5 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
assert = require('assert')
var mongoose = require('mongoose');
var Sighting;
var Species;
// Connection URL. In production this should not be visible in a public repository.
const url = 'mongodb://verdant:salainensana@ds135049.mlab.com:35049/bongaukset';
const knownSpecies = [
{
name: 'mallard'
},
{
name: 'redhead'
},
{
name: 'gadwall'
},
{
name: 'canvasback'
},
{
name: 'lesser scaup'
}
];
var currentSpecies;
var sightings = [
{
id: '1',
species: 'gadwall',
description: 'All your ducks are belong to us',
dateTime: '2016-10-01T01:01:00Z',
count: 1
},
{
id: '2',
species: 'lesser scaup',
description: 'This is awesome',
dateTime: '2016-12-13T12:05:00Z',
count: 5
},
{
id: '3',
species: 'canvasback',
description: '...',
dateTime: '2016-11-30T23:59:00Z',
count: 2
},
{
id: '4',
species: 'mallard',
description: 'Getting tired',
dateTime: '2016-11-29T00:00:00Z',
count: 18
},
{
id: '5',
species: 'redhead',
description: 'I think this one is called Alfred J.',
dateTime: '2016-11-29T10:00:01Z',
count: 1
},
{
id: '6',
species: 'redhead',
description: 'If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.',
dateTime: '2016-12-01T13:59:00Z',
count: 1
},
{
id: '7',
species: 'mallard',
description: 'Too many ducks to be counted',
dateTime: '2016-12-12T12:12:12Z',
count: 100
},
{
id: '8',
species: 'canvasback',
description: 'KWAAK!!!1',
dateTime: '2016-12-11T01:01:00Z',
count: 5
}
];
function getNames(list) {
return list.map((obj)=>{
return obj.name.toString();
});
}
const isoDateRegex = /(\d{4})-(\d{2})-(\d{2})T(\d{2})\:(\d{2})\:(\d{2})[+-](\d{2})\:(\d{2})/; // Incomplete regex for ISO dates, not in use
var sightSchema = mongoose.Schema({
id: Number,
species: {
type: String,
validate: {
validator: function(v) {
return currentSpecies.indexOf(v) >= 0;
},
message: "{VALUE} is not a valid enum value for path {PATH}"
}
},
description: String,
dateTime: String,
count: Number
});
var speciesSchema = mongoose.Schema({
name: String
});
function open(){
return new Promise((resolve, reject) => {
mongoose.connect(url);
var conn = mongoose.connection;
conn.on('error', (err)=>{
reject(err);
});
conn.once('open', ()=>{
console.log("Connected correctly to Mongo database server");
var dbOnline = true;
Sighting = mongoose.model("Sighting", sightSchema);
Species = mongoose.model("Species", speciesSchema);
var sightingPromise = Sighting.count({});
var speciesPromise = Species.find({});
Promise.all([sightingPromise, speciesPromise]).then((values) => {
if(values[0] == 0) sightingPromise = Sighting.insertMany(sightings);
if(values[1].length == 0) speciesPromise = Species.insertMany(knownSpecies);
else {
currentSpecies = getNames(values[1]);
speciesPromise = Promise.resolve();
}
Promise.all([sightingPromise, speciesPromise]).then((values) => {
resolve(Sighting);
}).catch((err) => {
reject(err);
});
}).catch((err) => {
reject(err);
});
});
})
}
function close(){
// Close connection
if(dbOnline){
db.close();
}
}
function get(q, o){
return Sighting.find(q, o);
}
function getOne(q, c, o){
return Sighting.findOne(q, c, o);
}
function updateOne(q, body, o){
return Sighting.update(q, { $set: body}, o);
}
function removeOne(q, body){
return Sighting.remove(q);
}
function getSpecies(q, c, o){
return Species.find(q, c, o);
}
function add(body){
return new Promise((resolve, reject) => {
var newSighting = new Sighting(body);
newSighting.save((err, newSighting) => {
if(err) {
reject(err);
}
resolve(newSighting);
});
});
}
var db = {
openConnection : open,
closeConnection: close,
getSightings: get,
getSighting: getOne,
updateSighting: updateOne,
deleteSighting: removeOne,
getSpecies: getSpecies,
addSighting: add
}
module.exports.database = db;