diff --git a/nodeapp/app.js b/nodeapp/app.js index e775a82..a22ed49 100644 --- a/nodeapp/app.js +++ b/nodeapp/app.js @@ -1,38 +1,8 @@ // Initialize project +const db = require('./db/db') const express = require('express'); -const Sequelize = require('sequelize'); -const Op = Sequelize.Op const app = express(); -// Setup database, using credentials set in .env -var sequelize = new Sequelize('database', process.env.DB_USER, process.env.DB_PASS, { - dialect: 'sqlite', - storage: '.data/database.sqlite' -}); -var Readings; - -// Authenticate with the database -sequelize.authenticate() - .then(function(err) { - console.log('Connection has been established successfully.'); - - // Define 'readings' table structure - Readings = sequelize.define('readings', { - timestamp: { - type: Sequelize.DATE - }, - reading: { - type: Sequelize.INTEGER - } - }); - - // Read data - Readings.sync(); - }) - .catch(function (err) { - console.log('Unable to connect to the database: ', err); - }); - // Handle requests for the root page by serving the static index.html from the views folder app.get("/", function(req, res) { res.sendFile(__dirname + "/views/index.html"); @@ -56,11 +26,11 @@ app.get('/getdata', (req, res) => { return; } - Readings.findAll({ + db.Readings.findAll({ attributes: ['timestamp', 'reading'], where: { timestamp: { - [Op.between]: [new Date(req.query.from), new Date(req.query.to)] + [db.Op.between]: [new Date(req.query.from), new Date(req.query.to)] } }, order: [ @@ -98,7 +68,7 @@ app.get('/cleanup', (req, res) => { Readings.destroy({ where: { timestamp: { - [Op.lt]: before + [db.Op.lt]: before } } }).then((numRows) => { diff --git a/nodeapp/db/db.js b/nodeapp/db/db.js new file mode 100644 index 0000000..60751f1 --- /dev/null +++ b/nodeapp/db/db.js @@ -0,0 +1,20 @@ +const Sequelize = require('sequelize'); +const Op = Sequelize.Op + +// Setup database, using credentials set in .env +const sequelize = new Sequelize('database', process.env.DB_USER, process.env.DB_PASS, { + dialect: 'sqlite', + storage: '.data/database.sqlite' +}); + +// Define 'readings' table structure +const Readings = sequelize.define('readings', { + timestamp: { + type: Sequelize.DATE + }, + reading: { + type: Sequelize.INTEGER + } +}); + +module.exports = {Op, Readings} diff --git a/nodeapp/utils/import.js b/nodeapp/utils/import.js index 9d8ae81..b711b11 100644 --- a/nodeapp/utils/import.js +++ b/nodeapp/utils/import.js @@ -1,34 +1,8 @@ -const Sequelize = require('sequelize') -const Op = Sequelize.Op +const db = require('../db/db') const request = require('request') const path = require('path') const dotenv = require('dotenv').config({path: path.join(__dirname, '../../.env')}) -// Database settings -const sequelize = new Sequelize('database', process.env.DB_USER, process.env.DB_PASS, { - dialect: 'sqlite', - storage: '.data/database.sqlite' -}) - -sequelize.authenticate().then((err) => { - console.log('Connection has been established successfully') - - // Define 'readings' table structure - Readings = sequelize.define('readings', { - timestamp: { - type: Sequelize.DATE - }, - reading: { - type: Sequelize.INTEGER - } - }) - - // Read data - Readings.sync() -}).catch((err) => { - console.log('Unable to connect to the database: ', err) -}) - // The URL from which to export production data const url = 'https://jasonsplant.glitch.me/exportall' @@ -45,8 +19,8 @@ request({ console.log(body.length + ' items retrieved') // Remove existing database items - Readings.destroy({truncate: true}) + db.Readings.destroy({truncate: true}) // Add new data to the database - Readings.bulkCreate(body) + db.Readings.bulkCreate(body) });