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,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)
});