瀏覽代碼

Database migration to support more weather properties

Paweł Malak 3 年之前
父節點
當前提交
a549149452
共有 4 個文件被更改,包括 60 次插入13 次删除
  1. 35 0
      db/migrations/03_weather.js
  2. 19 12
      models/Weather.js
  3. 3 0
      utils/getExternalWeather.js
  4. 3 1
      utils/init/initialConfig.json

+ 35 - 0
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,
+};

+ 19 - 12
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;
+module.exports = Weather;

+ 3 - 0
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) {

+ 3 - 1
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"
 }