-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
39 lines (32 loc) · 949 Bytes
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import csv from 'csvtojson';
import { tables } from 'harperdb';
const { AirportCode } = tables;
export async function populateAirports() {
// Check to see if the table is empty before populating it
let count = 0;
const records = await AirportCode.search({
select: ['id'],
limit: 1,
});
for await (const _ of records) {
count++;
}
if (count > 0) {
logger.info('AirportCode table is not empty, skipping population');
return;
}
const resp = await fetch(
`https://raw.githubusercontent.com/ip2location/ip2location-iata-icao/refs/heads/master/iata-icao.csv`
);
const data = await resp.text();
const json = await csv().fromString(data);
logger.info(
`Populating ${json.length} records into the AirportCode table...`
);
await transaction((txn) => {
for (const item of json) {
AirportCode.create(item, txn);
}
});
logger.info('Finished populating the AirportCode table');
}