From 50cad302cf2c0cac81c8a71acc064dbca0cf7f67 Mon Sep 17 00:00:00 2001 From: vista-man <524715@vistacollege.nl> Date: Thu, 20 Mar 2025 13:19:18 +0100 Subject: [PATCH] Add type insertion logic to fetch_and_insert.js --- fetch_and_insert.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/fetch_and_insert.js b/fetch_and_insert.js index 43b98b0..bc8963c 100644 --- a/fetch_and_insert.js +++ b/fetch_and_insert.js @@ -1,4 +1,4 @@ -const fetch = require('node-fetch'); +const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args)); const mysql = require('mysql'); const connection = mysql.createConnection({ @@ -10,6 +10,19 @@ const connection = mysql.createConnection({ connection.connect(); +const types = [ + 'normal', 'fire', 'water', 'electric', 'grass', 'ice', 'fighting', 'poison', 'ground', 'flying', 'psychic', 'bug', 'rock', 'ghost', 'dragon', 'dark', 'steel', 'fairy' +]; + +const insertTypes = () => { + types.forEach(type => { + const typeQuery = `INSERT INTO types (name) VALUES (?) ON DUPLICATE KEY UPDATE name=name`; + connection.query(typeQuery, [type], (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}/`; @@ -68,6 +81,7 @@ const insertPokemonData = (pokemon) => { }; const fetchAndInsertAllPokemon = async () => { + insertTypes(); for (let i = 1; i <= 1010; i++) { const pokemon = await fetchPokemonData(i); insertPokemonData(pokemon);