From b5bc6d6f1cfbf36e354d0bb9be43b17d1c9b0fb8 Mon Sep 17 00:00:00 2001 From: vista-man <524715@vistacollege.nl> Date: Thu, 20 Mar 2025 13:52:49 +0100 Subject: [PATCH] Add ability and egg group insertion logic to fetch_and_insert.js --- fetch_and_insert.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/fetch_and_insert.js b/fetch_and_insert.js index d5d880c..a2a958d 100644 --- a/fetch_and_insert.js +++ b/fetch_and_insert.js @@ -23,6 +23,24 @@ const insertTypes = () => { }); }; +const insertAbilities = (abilities) => { + abilities.forEach(ability => { + const abilityQuery = `INSERT INTO abilities (name) VALUES (?) ON DUPLICATE KEY UPDATE name=name`; + connection.query(abilityQuery, [ability.ability.name], (error, results, fields) => { + if (error) throw error; + }); + }); +}; + +const insertEggGroups = (eggGroups) => { + eggGroups.forEach(group => { + const eggGroupQuery = `INSERT INTO egg_groups (name) VALUES (?) ON DUPLICATE KEY UPDATE name=name`; + connection.query(eggGroupQuery, [group.name], (error, results, fields) => { + if (error) throw error; + }); + }); +}; + const fetchPokemonData = async (id) => { const url = `https://pokeapi.co/api/v2/pokemon/${id}/`; const url2 = `https://pokeapi.co/api/v2/pokemon-species/${id}/`; @@ -127,6 +145,8 @@ const fetchAndInsertAllPokemon = async () => { for (let i = 1; i <= 1010; i++) { try { const pokemon = await fetchPokemonData(i); + insertAbilities(pokemon.data.abilities); + insertEggGroups(pokemon.data2.egg_groups); insertPokemonData(pokemon); } catch (error) { console.error(`Error fetching/inserting Pokémon with ID ${i}:`, error);