Refactoring database and model definition into its own file
This commit is contained in:
parent
555ae80afc
commit
b64126a7fe
@ -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) => {
|
||||
|
20
nodeapp/db/db.js
Normal file
20
nodeapp/db/db.js
Normal 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}
|
@ -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)
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user