mirror of
				https://github.com/KevinMidboe/ISPDowntimeMonitor.git
				synced 2025-10-29 17:50:12 +00:00 
			
		
		
		
	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:
		
							
								
								
									
										33
									
								
								src/db.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								src/db.js
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										11
									
								
								src/schemas/Event.js
									
									
									
									
									
										Normal 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);
 | 
			
		||||
		Reference in New Issue
	
	Block a user