prenez-le-train/server/cli/import.js

46 lines
1.4 KiB
JavaScript
Raw Normal View History

2023-08-11 16:07:34 +00:00
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()
})
})
})
})
})
})