Adding functionality to import settings from production

This commit is contained in:
JayWll 2020-04-25 15:30:06 -06:00
parent a9e2563204
commit 2643bf4410

View File

@ -1,14 +1,15 @@
const db = require('../db/db') const db = require('../src/db')
const request = require('request') const request = require('request')
const path = require('path') const path = require('path')
require('dotenv').config({path: path.join(__dirname, '../../.env')}) require('dotenv').config({path: path.join(__dirname, '../../.env')})
// The URL from which to export production data // The URL from which to export production data
const url = 'https://jasonsplant.glitch.me/exportall' const readingsurl = 'https://jasonsplant.glitch.me/exportall'
const settingsurl = 'https://jasonsplant.glitch.me/showsettings'
// Get data from the production server // Get readings from the production server
request({ request({
url, url: readingsurl,
headers: { headers: {
'export-key': process.env.SECRET 'export-key': process.env.SECRET
}, },
@ -17,10 +18,7 @@ request({
if (error) return console.log(error) if (error) return console.log(error)
if (response.statusCode != 200) return console.log('Unsuccessful, as indicated by HTTP status') if (response.statusCode != 200) return console.log('Unsuccessful, as indicated by HTTP status')
console.log(response.body.length + ' items retrieved') console.log(response.body.length + ' readings retrieved')
// Sync the database table
await db.Settings.sync()
// Remove existing database items // Remove existing database items
await db.Readings.destroy({truncate: true}) await db.Readings.destroy({truncate: true})
@ -28,3 +26,23 @@ request({
// Add new data to the database // Add new data to the database
await db.Readings.bulkCreate(response.body) await db.Readings.bulkCreate(response.body)
}); });
// Get settings from the production server
request({
url: settingsurl,
headers: {
'export-key': process.env.SECRET
},
json: true
}, async(error, response) => {
if (error) return console.log(error)
if (response.statusCode != 200) return console.log('Unsuccessful, as indicated by HTTP status')
console.log(response.body.length + ' settings retrieved')
// Remove existing database items
await db.Settings.destroy({truncate: true})
// Add new data to the database
await db.Settings.bulkCreate(response.body)
});