Db functions and event Schema w/ mongoose.

Create mongoose schema for Event and db.js to export ways we want to
interface with the database.
This commit is contained in:
2020-06-03 00:18:32 +02:00
committed by KevinMidboe
parent 7fd5a5b049
commit 9cc4218bf2
2 changed files with 44 additions and 0 deletions

33
src/db.js Normal file
View File

@@ -0,0 +1,33 @@
const mongoose = require('mongoose');
mongoose.connect("mongodb://localhost:27017/ispmonitor", {
useNewUrlParser: true
});
const Event = require('./schemas/Event');
const commitServiceEventToDatabase = async (serviceMessages, pdfFilename) => {
try {
// we only care about the second message
const message = serviceMessages[1]
const event = new Event({
date: new Date(),
isOk: message.isOk,
message: message.statusText,
pdfFilename
})
await event.save();
return serviceMessages
} catch (err) {
console.error('error from mongoose:')
console.error(err)
return serviceMessages
}
}
const getAllEvents = () => Event.find().exec()
module.exports = {
commitServiceEventToDatabase,
getAllEvents
}

11
src/schemas/Event.js Normal file
View File

@@ -0,0 +1,11 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const Event = new Schema({
date: Date,
isOk: Boolean,
message: String,
pdfFilename: String
})
module.exports = mongoose.model('Event', Event);