-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
53 lines (48 loc) · 1.47 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
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/race_index');
var raceSchema = new mongoose.Schema({
name: { type: String, index: true },
thumbnail: { type: String },
image: { type: String },
description: { type: String },
attributes: [String],
});
mongoose.model('Race', raceSchema);
module.exports = function (host, port) {
return (function () {
function getRaceByName(name, callback) {
var race = mongoose.model('Race');
race.find({'name': name}, function (err, races) {
if (err !== null) {
console.log(err);
callback(null);
}
else if (races.length > 0) {
callback(races[0]);
}
else {
callback(null);
}
});
}
function getRaces(callback) {
var race = mongoose.model('Race');
race.find({}, ['name', 'thumbnail'], function (err, races) {
if (err === null) {
callback(races);
}
else {
console.log(err);
callback([]);
}
});
}
return {
getRace: function (name, callback) {
getRaceByName(name, callback);
},
getRaces: getRaces
};
}());
};
var mongo = require('mongodb');