From a5491494525d25ee85a1588575ccec71e6cd74eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Malak?= Date: Thu, 18 Nov 2021 14:16:57 +0100 Subject: [PATCH] Database migration to support more weather properties --- db/migrations/03_weather.js | 35 +++++++++++++++++++++++++++++++++++ models/Weather.js | 31 +++++++++++++++++++------------ utils/getExternalWeather.js | 3 +++ utils/init/initialConfig.json | 4 +++- 4 files changed, 60 insertions(+), 13 deletions(-) create mode 100644 db/migrations/03_weather.js diff --git a/db/migrations/03_weather.js b/db/migrations/03_weather.js new file mode 100644 index 0000000..bba45df --- /dev/null +++ b/db/migrations/03_weather.js @@ -0,0 +1,35 @@ +const { DataTypes } = require('sequelize'); +const { INTEGER, FLOAT } = DataTypes; +const loadConfig = require('../../utils/loadConfig'); +const getExternalWeather = require('../../utils/getExternalWeather'); + +const up = async (query) => { + await query.addColumn('weather', 'humidity', { + type: INTEGER, + }); + + await query.addColumn('weather', 'windK', { + type: FLOAT, + }); + + await query.addColumn('weather', 'windM', { + type: FLOAT, + }); + + const { WEATHER_API_KEY: secret } = await loadConfig(); + + if (secret) { + await getExternalWeather(); + } +}; + +const down = async (query) => { + await query.removeColumn('weather', 'humidity'); + await query.removeColumn('weather', 'windK'); + await query.removeColumn('weather', 'windM'); +}; + +module.exports = { + up, + down, +}; diff --git a/models/Weather.js b/models/Weather.js index c85c29f..2ac7961 100644 --- a/models/Weather.js +++ b/models/Weather.js @@ -1,16 +1,23 @@ const { DataTypes } = require('sequelize'); const { sequelize } = require('../db'); -const Weather = sequelize.define('Weather', { - externalLastUpdate: DataTypes.STRING, - tempC: DataTypes.FLOAT, - tempF: DataTypes.FLOAT, - isDay: DataTypes.INTEGER, - cloud: DataTypes.INTEGER, - conditionText: DataTypes.TEXT, - conditionCode: DataTypes.INTEGER -}, { - tableName: 'weather' -}); +const Weather = sequelize.define( + 'Weather', + { + externalLastUpdate: DataTypes.STRING, + tempC: DataTypes.FLOAT, + tempF: DataTypes.FLOAT, + isDay: DataTypes.INTEGER, + cloud: DataTypes.INTEGER, + conditionText: DataTypes.TEXT, + conditionCode: DataTypes.INTEGER, + humidity: DataTypes.INTEGER, + windK: DataTypes.FLOAT, + windM: DataTypes.FLOAT, + }, + { + tableName: 'weather', + } +); -module.exports = Weather; \ No newline at end of file +module.exports = Weather; diff --git a/utils/getExternalWeather.js b/utils/getExternalWeather.js index 20edac4..8171807 100644 --- a/utils/getExternalWeather.js +++ b/utils/getExternalWeather.js @@ -21,6 +21,9 @@ const getExternalWeather = async () => { cloud: cursor.cloud, conditionText: cursor.condition.text, conditionCode: cursor.condition.code, + humidity: cursor.humidity, + windK: cursor.wind_kph, + windM: cursor.wind_mph, }); return weatherData; } catch (err) { diff --git a/utils/init/initialConfig.json b/utils/init/initialConfig.json index c815d11..897b43e 100644 --- a/utils/init/initialConfig.json +++ b/utils/init/initialConfig.json @@ -25,5 +25,7 @@ "daySchema": "Sunday;Monday;Tuesday;Wednesday;Thursday;Friday;Saturday", "monthSchema": "January;February;March;April;May;June;July;August;September;October;November;December", "showTime": false, - "defaultTheme": "tron" + "defaultTheme": "tron", + "isKilometer": true, + "weatherData": "cloud" }