Created
February 16, 2020 13:30
-
-
Save guy-kdm/0476059dfd16fd39fb1b84d873f875b8 to your computer and use it in GitHub Desktop.
for gil to look at fn=> fn as opposed to a => fn('', a)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Airtable = require('airtable'); | |
const fs = require('fs'); | |
const AIRTABLE_API_KEY = 'keybqbYGu7ewvtNfh'; | |
Airtable.configure({ apiKey: AIRTABLE_API_KEY, endpointUrl: 'https://api.airtable.com' }); | |
const tableId = 'applVr5WzyjAo4xZS'; | |
const base = Airtable.base(tableId); | |
const groupBy = (arr, groupByField) => { | |
return arr.reduce((acc, item) => { | |
const groupName = item[groupByField]; | |
if (!acc[groupName]) | |
return { | |
...acc, | |
[groupName]: [ item ] | |
}; | |
else { | |
acc[groupName].push(item); | |
return acc; | |
} | |
}, {}); | |
}; | |
const flattenSingleItemField = (fieldName) => { | |
return (table) => { | |
return table.map((row) => { | |
return { | |
...row, | |
[fieldName]: row[fieldName][0] | |
}; | |
}); | |
}; | |
}; | |
export const parseNumber = (value) => { | |
//remove commas | |
const valueWithoutComma = value.replace(/,/g, ''); | |
return parseInt(valueWithoutComma); | |
}; | |
const stupidFn = (table) => { | |
return table; | |
}; | |
const flattenSingleItemFieldWrapper = (table) => { | |
const stupid = stupidFn(table) | |
return flattenSingleItemField('city')(stupid); | |
}; | |
const flatten = (fieldName,table) =>{ | |
return table.map((row) => { | |
return { | |
...row, | |
[fieldName]: row[fieldName][0] | |
}; | |
}); | |
} | |
const tableTransformers = { | |
cities: (table)=>{flatten('country',table)}, | |
countries: null, | |
transactionCosts: flattenSingleItemField('city'), | |
operationCosts: flattenSingleItemField('city'), | |
pricing: null, //null because in pricing we have only an city and not city array like in other tables | |
riskStatistics: flattenSingleItemField('country') | |
}; | |
const fetchAndTransformTable = (tableName) => { | |
return base(tableName) | |
.select() | |
.all() | |
.then((records) => records.map((record) => ({ ...record.fields, id: record.id }))) | |
.then((table) => { | |
const transformer = tableTransformers[tableName]; | |
if (transformer) { | |
table = transformer(table); | |
} | |
return { table, tableName }; | |
}); | |
}; | |
export const getCities = async () => { | |
// fetching all relevant tables | |
const tablesDataArray = await Promise.all(Object.keys(tableTransformers).map(fetchAndTransformTable)); | |
// converting tables array to {[tableName]: tableData} object | |
const tablesMap = tablesDataArray.reduce((acc, { table, tableName }) => { | |
return Object.assign(acc, { [tableName]: table }); | |
}, {}); | |
// grouping city attribute records by city | |
const transactionCostsByCity = groupBy(tablesMap.transactionCosts, 'city'); | |
const operationCostsByCity = groupBy(tablesMap.operationCosts, 'city'); | |
const pricingByCity = groupBy(tablesMap.pricing, 'city'); | |
const statisticsByCountry = groupBy(tablesMap.riskStatistics, 'country'); | |
const countriesById = groupBy(tablesMap.countries, 'id'); | |
// creating city data object {cityId: cityData, ...} | |
const citiesMap = tablesMap.cities.reduce((acc, city) => { | |
return { | |
...acc, | |
[city.id]: { | |
...city, | |
transactionCosts: transactionCostsByCity[city.id], | |
operationCosts: operationCostsByCity[city.id], | |
pricing: pricingByCity[city.name], | |
riskStatistics: statisticsByCountry[city.country], | |
country: countriesById[city.country] | |
} | |
}; | |
}, {}); | |
// console.log(citiesMap); | |
// const pp = JSON.stringify(citiesMap); | |
// console.log(pp); | |
// fs.writeFileSync('../abc.json', pp); | |
return citiesMap; | |
}; | |
// getCities(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment