-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongowrapper.js
175 lines (155 loc) · 4.49 KB
/
mongowrapper.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
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
module.exports = (function() {
'use strict';
var status = 'init';
let mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/smple');
var db = mongoose.connection;
db.on("error", err => {
console.log('[MONGO] Error', err);
});
db.once("open", function(callback) {
console.log("[MONGO] Connection succeeded.");
status = 'connected';
});
var Schema = mongoose.Schema;
var eventSchema = new Schema({
title: String,
location: {
lat: Number,
lng: Number,
},
bands: [{
name: String,
desc: String,
fullimage: String,
thumbnail: String,
spotifyUri: String,
}],
eventInfo: {
title: String,
address: String,
venueUrl: String,
ticketUrl: String,
date: Date,
}
});
eventSchema.index({
'location': '2dsphere'
});
var Event = mongoose.model('Event', eventSchema);
var areaSchema = new Schema({
name: String,
lat: Number,
lng: Number,
id: Number,
lastCached: Date
});
var Area = mongoose.model('Area', areaSchema);
function addEvent(e, callback) {
if (!callback) callback = () => {};
Event.update({
title: e.title
}, e, {
upsert: true
}, (err, stats) => {
callback(err);
});
}
function addArea(area, callback) {
area.lastCached = new Date();
Area.update({
id: area.id
}, area, {
upsert: true
}, (err, stats) => {
if (err) {
console.log('MONGO ERROR upserting', err)
callback(err);
} else {
callback(null, stats);
}
});
}
function getAreaLastUpdated(area, callback) {
Area.findOne({
id: area.id
}, (err, area) => {
callback(err, area);
});
}
function getNearPlaces(pos, dateRange, range, callback) {
if (typeof(range) === 'function') {
callback = range;
range = 1000;
}
if (!callback) callback = () => {};
if (!(dateRange.to instanceof Date) || !(dateRange.from instanceof Date) || !(pos.lat) || !(pos.lng)) {
callback('Invalid args');
return;
}
Event.find({
location: {
$geoWithin: {
$center: [
[pos.lat, pos.lng], range / 6378.1
]
}
},
'eventInfo.date': {
"$gte": dateRange.from,
"$lt": dateRange.to
}
}, (err, result) => {
if (err) console.log('[MONGO] Error getting entries from db', err);
callback(err, result);
});
}
return {
addEvent,
getNearPlaces,
addArea,
getAreaLastUpdated
}
})();
if (!module.parent) {
console.log('Starting as standalone');
var db = module.exports;
db.addEntry({
title: "eyyyyy1",
location: {
lat: 51.5,
lng: -2.6
},
bands: [{
name: "The Lumineers",
desc: "The Lumineers are an American folk rock band based in Denver, Colorado, who formed as early as 2005 but didn’t release their self-titled debut record until April of 2012.",
fullimage: "images/bandimages/fkatwigs.jpg",
thumbnail: "images/thumbnails/fkatwigs.jpg",
spotifyUri: ""
}, {
name: "Supporting Band",
desc: "hey boss",
fullimage: "https://pbs.twimg.com/profile_images/642798007621185536/Y6x_U5gS.jpg",
thumbnail: "https://pbs.twimg.com/profile_images/642798007621185536/Y6x_U5gS.jpg",
spotifyUri: ""
}],
eventInfo: {
title: "O2 Academy Bristol",
address: "O2 Academy Bristol\nFrogmore Street\nBS1 5NA\nBristol, UK\n0117 927 9227",
venueUrl: "http://www.o2academybristol.co.uk",
ticketUrl: "http://www.songkick.com/tickets/20220218",
date: "2016-03-01"
}
}, err => {
console.log(err);
});
db.getNearPlaces({
lat: 51.4,
lng: -2.6
}, 1000, (err, results) => {
if (err)
console.log('rip', err);
else
console.log(results);
});
}