Skip to content

Commit 032bfcd

Browse files
committed
refactor init-countries.js and self host countries data
1 parent 1f83d88 commit 032bfcd

3 files changed

Lines changed: 44 additions & 17 deletions

File tree

docs/development.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ docker-compose up -d
2525
Indexer services should now be started, you can check if it's syncing properly by streaming the logs for the indexer:
2626

2727
```
28-
docker logs indexer_indexer_1 -f
28+
docker logs indexer-ingest-1 -f
2929
```
3030

3131
You should be able to follow tfchain blocks processing:

scripts/countries.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

scripts/init-countries.js

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,51 @@ async function main () {
2121
database: DB_NAME
2222
}
2323

24+
const pool = new Pool(config)
25+
pool.on('error', (err, client) => {
26+
console.error(err)
27+
console.error('--- Unexpected error on idle client, exiting ---')
28+
process.exit(-1)
29+
})
30+
31+
const client = await pool.connect()
32+
33+
// check first if countries and cities already exist
34+
const ExpectedCountriesCount = 250
35+
const ExpectedCitiesCount = 77786
36+
const countriesQuery = 'SELECT * FROM country'
37+
const citiesQuery = 'SELECT * FROM city'
38+
39+
const countriesExist = await client.query(countriesQuery)
40+
const citiesExist = await client.query(citiesQuery)
41+
42+
if (countriesExist.rowCount >= ExpectedCountriesCount && citiesExist.rowCount >= ExpectedCitiesCount) {
43+
console.log('--- Countries and cities already exist, skipping ---')
44+
process.exit(0)
45+
} else {
46+
console.log('Countries or cities do not exist, creating...')
47+
}
48+
49+
// fetch countries
2450
let countries = []
2551
try {
2652
countries = await getCountries()
2753
} catch (error) {
28-
console.log(error)
29-
console.log('--- No Countries were found, a restart is suggested ---')
54+
console.error(error)
55+
console.error("--- Can't fetch countries, exiting ---")
3056
process.exit(-3)
3157
}
3258

59+
// fetch cities
3360
let cities = []
3461
try {
3562
cities = await getCities()
3663
} catch (error) {
37-
console.log(error)
38-
console.log('--- No Cities were found, a restart is suggested ---')
64+
console.error(error)
65+
console.error("--- Can\'t fetch cities, exiting ---")
3966
process.exit(-2)
4067
}
4168

42-
const pool = new Pool(config)
43-
pool.on('error', (err, client) => {
44-
console.error('Unexpected error on idle client', err)
45-
process.exit(-1)
46-
})
47-
48-
const client = await pool.connect()
49-
5069
try {
5170
const countryPromises = countries.data.map((country, index) => {
5271
const text = 'INSERT INTO country(id, country_id, name, code, region, subregion, lat, long) VALUES($1, $2, $3, $4, $5, $6, $7, $8)'
@@ -104,17 +123,24 @@ async function main () {
104123
.then(res => {
105124
console.log(res)
106125
})
107-
.catch(err => { console.log(err); process.exit(1)})
108-
.then(process.exit(0))
126+
.catch(err => {
127+
console.error(err);
128+
process.exit(1)
129+
})
130+
.then(_ => {
131+
console.log('--- Countries and cities inserted successfully ---');
132+
process.exit(0);
133+
})
109134

110135
} catch (error) {
111-
console.log(error)
136+
console.error(error)
137+
console.error("--- Error while inserting countries and cities into db, exiting ---")
112138
process.exit(2)
113139
}
114140
}
115141

116142
async function getCountries () {
117-
return axios.get('https://restcountries.com/v3/all')
143+
return axios.get('https://raw.githubusercontent.com/threefoldtech/tfchain_graphql/master/scripts/countries.json')
118144
}
119145

120146
async function getCities () {

0 commit comments

Comments
 (0)