2023-12-31 02:02:03 +00:00
|
|
|
import knex from './knex.js';
|
|
|
|
|
|
|
|
function curateCountry(countryEntry) {
|
|
|
|
return {
|
|
|
|
name: countryEntry.name,
|
2024-01-03 01:52:41 +00:00
|
|
|
alias: countryEntry.alias,
|
2023-12-31 02:02:03 +00:00
|
|
|
alpha2: countryEntry.alpha2,
|
|
|
|
code: countryEntry.code,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-01-03 01:52:41 +00:00
|
|
|
export async function fetchCountriesByAlpha2(alpha2s, options = {}) {
|
|
|
|
const entries = await knex('countries')
|
|
|
|
.whereIn('alpha2', alpha2s)
|
|
|
|
.orderBy(knex.raw('coalesce(alias, name)'));
|
2023-12-31 02:02:03 +00:00
|
|
|
|
2024-01-03 01:52:41 +00:00
|
|
|
if (options.preserveOrder) {
|
|
|
|
return alpha2s.map((alpha2) => {
|
|
|
|
const countryEntry = entries.find((entry) => entry.alpha2 === alpha2);
|
|
|
|
|
|
|
|
if (!countryEntry) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return curateCountry(countryEntry);
|
|
|
|
}).filter(Boolean);
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries.map((countryEntry) => curateCountry(countryEntry));
|
2023-12-31 02:02:03 +00:00
|
|
|
}
|