From 2643bf44105584bb3cca6cfb41f2ff1d8500ab59 Mon Sep 17 00:00:00 2001 From: JayWll Date: Sat, 25 Apr 2020 15:30:06 -0600 Subject: [PATCH] Adding functionality to import settings from production --- nodeapp/utils/import.js | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/nodeapp/utils/import.js b/nodeapp/utils/import.js index 0413359..9ecf511 100644 --- a/nodeapp/utils/import.js +++ b/nodeapp/utils/import.js @@ -1,14 +1,15 @@ -const db = require('../db/db') +const db = require('../src/db') const request = require('request') const path = require('path') require('dotenv').config({path: path.join(__dirname, '../../.env')}) // 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({ - url, + url: readingsurl, headers: { 'export-key': process.env.SECRET }, @@ -17,10 +18,7 @@ request({ if (error) return console.log(error) if (response.statusCode != 200) return console.log('Unsuccessful, as indicated by HTTP status') - console.log(response.body.length + ' items retrieved') - - // Sync the database table - await db.Settings.sync() + console.log(response.body.length + ' readings retrieved') // Remove existing database items await db.Readings.destroy({truncate: true}) @@ -28,3 +26,23 @@ request({ // Add new data to the database 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) +});