46 lines
1.4 KiB
JavaScript
46 lines
1.4 KiB
JavaScript
const fs = require('fs')
|
|
const db = require("../models")
|
|
|
|
const Airport = db['Airport']
|
|
const Flight = db['Flight']
|
|
|
|
db.sequelize.sync()
|
|
|
|
fs.readFile(__dirname + '/../data/airports.json', 'utf-8', (error, data) => {
|
|
if (error) {
|
|
console.log(error)
|
|
return
|
|
}
|
|
|
|
data = JSON.parse(data)
|
|
Airport.findAll({attributes: ['icao_code', 'iata_code']}).then(results => {
|
|
let icao_codes = []
|
|
let iata_codes = []
|
|
results.forEach(result => {
|
|
if (result.icao_code)
|
|
icao_codes.push(result.icao_code)
|
|
if (result.iata_code)
|
|
iata_codes.push(result.iata_code)
|
|
})
|
|
|
|
Airport.bulkCreate(data['response'].filter(airport => !icao_codes.includes(airport.icao_code)
|
|
&& !iata_codes.includes(airport.iata_code))).then()
|
|
}).then(() => {
|
|
Airport.findAll({attributes: ['icao_code']}).then(airports => {
|
|
airports.forEach(airport => {
|
|
fs.readFile(__dirname + `/../data/routes/${airport.icao_code}.json`, 'utf-8', (error, data) => {
|
|
if (error) {
|
|
console.log(error)
|
|
return
|
|
}
|
|
|
|
data = JSON.parse(data)
|
|
Flight.destroy({where: {dep_icao: airport.icao_code}}).then(() => {
|
|
Flight.bulkCreate(data['response']).then()
|
|
})
|
|
})
|
|
})
|
|
})
|
|
})
|
|
})
|