traxxx-web/src/countries.js

38 lines
913 B
JavaScript
Raw Normal View History

import knex from './knex.js';
function curateCountry(countryEntry) {
return {
name: countryEntry.name,
alias: countryEntry.alias,
alpha2: countryEntry.alpha2,
code: countryEntry.code,
};
}
export async function fetchCountriesByAlpha2(alpha2s, options = {}) {
const entries = await knex('countries')
.whereIn('alpha2', alpha2s)
.orderBy(knex.raw('coalesce(alias, name)'));
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));
}
2024-10-22 01:12:42 +00:00
export async function fetchCountries() {
const entries = await knex('countries')
.orderBy(knex.raw('coalesce(alias, name)'));
return entries.map((countryEntry) => curateCountry(countryEntry));
}