This repository has been archived by the owner on Jan 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
farmstand-mongodb.js
165 lines (155 loc) · 4.22 KB
/
farmstand-mongodb.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
// Set up our DB API globals.
var Db = require('mongodb').Db;
var Connection = require('mongodb').Connection;
var Server = require('mongodb').Server;
var BSON = require('mongodb').BSON;
var ObjectID = require('mongodb').ObjectID;
// Main DB provider object
FarmstandProvider = function(host, port, user, pass) {
this.db = new Db(process.env.OPENSHIFT_APP_NAME, new Server(host, port, { auto_reconnect: true }, {}));
this.db.open(function(error, db){
db.authenticate(user, pass, function(error, result) {});
});
};
// This gets us in the to 'locations' collection, which is where
// the farmstand records live.
FarmstandProvider.prototype.getLocations = function(callback) {
this.db.collection('locations', function(error, location_collection) {
if (error) {
callback(error);
}
else {
callback(null, location_collection);
}
});
};
// This gets us in the to 'state_geocodes' collection, which is where
// the state geocode records live. We use these to tell Google Maps
// how to center the viewport around a given state, and at what zoom level.
FarmstandProvider.prototype.getStateGeos = function(callback) {
this.db.collection('state_geocodes', function(error, state_geocodes_collection) {
if (error) {
callback(error);
}
else {
callback(null, state_geocodes_collection);
}
});
};
// Grab all of the farmstands for a given state.
// This also explicitly orders the records by market name.
FarmstandProvider.prototype.findByState = function(state, callback) {
this.getLocations(function(error, location_collection) {
if (error) {
callback(error);
}
else {
var cursor = location_collection.find({ 'LocAddState': state });
cursor.sort({ 'MktName': 1 });
cursor.toArray(function(error, result) {
if (error) {
callback(error);
}
else {
callback(null, result);
}
});
}
});
};
// Grab all of the distinct state names in the database.
// We use this to populate the UI state select list.
FarmstandProvider.prototype.allStates = function(callback) {
this.getLocations(function(error, location_collection) {
if (error) {
callback(error);
}
else {
location_collection.distinct('LocAddState', function(error, result) {
if (error) {
callback(error);
}
else {
callback(null, result);
}
});
}
});
};
// Write the suppiled records into the database.
// This is for new records only, not "upserts"
FarmstandProvider.prototype.saveLoc = function(location, callback) {
this.getLocations(function(error, location_collection) {
if (error) {
callback(error);
}
else {
location_collection.insert(location, function(error, result) {
if (error) {
callback(error);
}
else {
callback(null, result);
}
});
}
});
};
// Update the indicated farmstand record with Lat/Lon data.
FarmstandProvider.prototype.setLocGeo = function(locGeo, callback) {
this.getLocations(function(error, location_collection) {
if (error) {
callback(error);
}
else {
var oid = new ObjectID(locGeo.id);
location_collection.update({ _id: oid }, { $set: { 'LocLat': locGeo.lat, 'LocLon': locGeo.lon }}, {}, function(error, result) {
if (error) {
callback(error);
}
else {
callback(null, result);
}
});
}
});
};
// Get the previously stored state geolocation data.
FarmstandProvider.prototype.allStateGeo = function(callback) {
this.getStateGeos(function(error, state_geocode_collection) {
if (error) {
callback(error);
}
else {
var cursor = state_geocode_collection.find({});
cursor.toArray(function(error, result) {
if (error) {
callback(error);
}
else {
callback(null, result);
}
});
}
});
};
// Write the suppiled records into the database.
FarmstandProvider.prototype.setStateGeo = function(state, callback) {
this.getStateGeos(function(error, state_geocode_collection) {
if (error) {
callback(error);
}
else {
state_geocode_collection.insert(state, function(error, result) {
if (error) {
callback(error);
}
else {
callback(null, result);
}
});
}
});
};
// Export the DB provider for use in other modules.
exports.FarmstandProvider = FarmstandProvider;