From 9cc4218bf2c30912caa2e2882cc8eb7f40b4b778 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Wed, 3 Jun 2020 00:18:32 +0200 Subject: [PATCH] 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. --- src/db.js | 33 +++++++++++++++++++++++++++++++++ src/schemas/Event.js | 11 +++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/db.js create mode 100644 src/schemas/Event.js diff --git a/src/db.js b/src/db.js new file mode 100644 index 0000000..41d1367 --- /dev/null +++ b/src/db.js @@ -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 +} \ No newline at end of file diff --git a/src/schemas/Event.js b/src/schemas/Event.js new file mode 100644 index 0000000..92b18a0 --- /dev/null +++ b/src/schemas/Event.js @@ -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); \ No newline at end of file