Refactoring database and model definition into its own file

This commit is contained in:
JayWll 2020-04-11 12:57:35 -06:00
parent 555ae80afc
commit b64126a7fe
3 changed files with 27 additions and 63 deletions

View File

@ -1,38 +1,8 @@
// Initialize project // Initialize project
const db = require('./db/db')
const express = require('express'); const express = require('express');
const Sequelize = require('sequelize');
const Op = Sequelize.Op
const app = express(); 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 // Handle requests for the root page by serving the static index.html from the views folder
app.get("/", function(req, res) { app.get("/", function(req, res) {
res.sendFile(__dirname + "/views/index.html"); res.sendFile(__dirname + "/views/index.html");
@ -56,11 +26,11 @@ app.get('/getdata', (req, res) => {
return; return;
} }
Readings.findAll({ db.Readings.findAll({
attributes: ['timestamp', 'reading'], attributes: ['timestamp', 'reading'],
where: { where: {
timestamp: { 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: [ order: [
@ -98,7 +68,7 @@ app.get('/cleanup', (req, res) => {
Readings.destroy({ Readings.destroy({
where: { where: {
timestamp: { timestamp: {
[Op.lt]: before [db.Op.lt]: before
} }
} }
}).then((numRows) => { }).then((numRows) => {

20
nodeapp/db/db.js Normal file
View File

@ -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}

View File

@ -1,34 +1,8 @@
const Sequelize = require('sequelize') const db = require('../db/db')
const Op = Sequelize.Op
const request = require('request') const request = require('request')
const path = require('path') const path = require('path')
const dotenv = require('dotenv').config({path: path.join(__dirname, '../../.env')}) 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 // The URL from which to export production data
const url = 'https://jasonsplant.glitch.me/exportall' const url = 'https://jasonsplant.glitch.me/exportall'
@ -45,8 +19,8 @@ request({
console.log(body.length + ' items retrieved') console.log(body.length + ' items retrieved')
// Remove existing database items // Remove existing database items
Readings.destroy({truncate: true}) db.Readings.destroy({truncate: true})
// Add new data to the database // Add new data to the database
Readings.bulkCreate(body) db.Readings.bulkCreate(body)
}); });