-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo.js
35 lines (27 loc) · 1000 Bytes
/
mongo.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
const mongoose = require('mongoose');
const dbUrl = require('./credentials.json').url;
function currentDate() {
return Date.now();
}
// set up default mongoose connection
mongoose.connect(dbUrl);
// set mongoose promise to global promise
mongoose.Promise = global.Promise;
// get default connection
const db = mongoose.connection;
// bind connection to error event
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
// define the schema for a set of data related to one sensor.
const SensorDataSetSchema = new mongoose.Schema({
name: String,
currentValue: Number,
preferredValue: Number,
values: [Number],
accumulationPeriod: Number,
accumulator: {type: Number, default: 0},
timeAccumulatorStart: {type: Number, default: currentDate},
timeCurrentValueLastUpdated: Number,
timeValuesLastModified: {type: Number, default: 0}
});
// compile model from schema
module.exports = mongoose.model('sensor_data_set', SensorDataSetSchema);