| 1 | import 'dotenv/config' |
| 2 | import { parse } from |
| 3 | import fs from 'node:fs |
| 4 | import { prisma } |
| 5 | |
| 6 | const CONCURRENCY = 50 |
| 7 | |
| 8 | const processFile = async |
| 9 | const records = |
| 10 | const parser = |
| 11 | .createReadStream(`${__dirname |
| 12 | .pipe(parse({ |
| 13 | |
| 14 | })); |
| 15 | for await (const |
| 16 | if (record[0] |
| 17 | const [lat, |
| 18 | records.push({ |
| 19 | ident: record[0] |
| 20 | type: record[1] |
| 21 | name: record[2] |
| 22 | elevation: record[3] |
| 23 | continent: record[4] |
| 24 | iso_country: record[5] |
| 25 | iso_region: record[6] |
| 26 | municipality: record[7] |
| 27 | gps_code: record[8] |
| 28 | iata_code: record[9] |
| 29 | long, lat |
| 30 | }) |
| 31 | } |
| 32 | return records; |
| 33 | }; |
| 34 | |
| 35 | async function upsertAirport( |
| 36 | const existingAirport = |
| 37 | where: { ident: record |
| 38 | }) |
| 39 | const { iso_country: |
| 40 | |
| 41 | const data = |
| 42 | |
| 43 | if (existingAirport) { |
| 44 | await prisma.airport |
| 45 | } else { |
| 46 | //@ts-expect-error shush |
| 47 | await prisma.airport |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | (async () => |
| 52 | const records = |
| 53 | const failures: |
| 54 | let done = |
| 55 | |
| 56 | // Process in batches of CONCURRENCY |
| 57 | for (let i |
| 58 | const batch = |
| 59 | const results = |
| 60 | batch.map((record |
| 61 | ); |
| 62 | results.forEach((result |
| 63 | if (result.status |
| 64 | console.error(`Failed: |
| 65 | failures.push(batch |
| 66 | } |
| 67 | }); |
| 68 | done += batch. |
| 69 | console.log(`Progress: |
| 70 | } |
| 71 | |
| 72 | if (failures.length |
| 73 | const headers = |
| 74 | const csvLines = |
| 75 | headers.join(', |
| 76 | ...failures.map( |
| 77 | ]; |
| 78 | const outPath = |
| 79 | fs.writeFileSync(outPath |
| 80 | console.log(`${ |
| 81 | } else { |
| 82 | console.log("All airports imported successfully |
| 83 | } |
| 84 | |
| 85 | console.log("Done |
| 86 | })(); |