Add database schema and data fetching for Pokémon

This commit is contained in:
vista-man
2025-03-20 12:23:31 +01:00
parent 7aa7bb79f4
commit 896dcf83a7
2 changed files with 177 additions and 0 deletions

78
fetch_and_insert.js Normal file
View File

@@ -0,0 +1,78 @@
const fetch = require('node-fetch');
const mysql = require('mysql');
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '', // Add your MySQL password here
database: 'pokedex'
});
connection.connect();
const fetchPokemonData = async (id) => {
const url = `https://pokeapi.co/api/v2/pokemon/${id}/`;
const url2 = `https://pokeapi.co/api/v2/pokemon-species/${id}/`;
const res = await fetch(url);
const res2 = await fetch(url2);
const data = await res.json();
const data2 = await res2.json();
return { data, data2 };
};
const insertPokemonData = (pokemon) => {
const { data, data2 } = pokemon;
const pokemonQuery = `INSERT INTO pokemon (id, name, height, weight, base_experience, species_url, image_url) VALUES (?, ?, ?, ?, ?, ?, ?)`;
const pokemonValues = [data.id, data.name, data.height, data.weight, data.base_experience, data2.url, data.sprites.other['official-artwork'].front_default];
connection.query(pokemonQuery, pokemonValues, (error, results, fields) => {
if (error) throw error;
console.log(`Inserted Pokémon: ${data.name}`);
});
data.types.forEach(type => {
const typeQuery = `INSERT INTO pokemon_types (pokemon_id, type_id) VALUES (?, (SELECT id FROM types WHERE name = ?))`;
const typeValues = [data.id, type.type.name];
connection.query(typeQuery, typeValues, (error, results, fields) => {
if (error) throw error;
});
});
data.abilities.forEach(ability => {
const abilityQuery = `INSERT INTO pokemon_abilities (pokemon_id, ability_id) VALUES (?, (SELECT id FROM abilities WHERE name = ?))`;
const abilityValues = [data.id, ability.ability.name];
connection.query(abilityQuery, abilityValues, (error, results, fields) => {
if (error) throw error;
});
});
const statsQuery = `INSERT INTO stats (pokemon_id, hp, attack, defense, sp_attack, sp_defense, speed) VALUES (?, ?, ?, ?, ?, ?, ?)`;
const statsValues = [data.id, data.stats[0].base_stat, data.stats[1].base_stat, data.stats[2].base_stat, data.stats[3].base_stat, data.stats[4].base_stat, data.stats[5].base_stat];
connection.query(statsQuery, statsValues, (error, results, fields) => {
if (error) throw error;
});
const speciesQuery = `INSERT INTO species (pokemon_id, genus, flavor_text, growth_rate, base_happiness, capture_rate, gender_rate) VALUES (?, ?, ?, ?, ?, ?, ?)`;
const speciesValues = [data.id, data2.genera[0].genus, data2.flavor_text_entries[0].flavor_text, data2.growth_rate.name, data2.base_happiness, data2.capture_rate, data2.gender_rate];
connection.query(speciesQuery, speciesValues, (error, results, fields) => {
if (error) throw error;
});
data2.egg_groups.forEach(group => {
const eggGroupQuery = `INSERT INTO pokemon_egg_groups (pokemon_id, egg_group_id) VALUES (?, (SELECT id FROM egg_groups WHERE name = ?))`;
const eggGroupValues = [data.id, group.name];
connection.query(eggGroupQuery, eggGroupValues, (error, results, fields) => {
if (error) throw error;
});
});
};
const fetchAndInsertAllPokemon = async () => {
for (let i = 1; i <= 1010; i++) {
const pokemon = await fetchPokemonData(i);
insertPokemonData(pokemon);
}
connection.end();
};
fetchAndInsertAllPokemon();

99
pokedex_database.sql Normal file
View File

@@ -0,0 +1,99 @@
CREATE DATABASE IF NOT EXISTS pokedex;
USE pokedex;
-- Table to store Pokémon details
CREATE TABLE IF NOT EXISTS pokemon (
id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
height INT,
weight INT,
base_experience INT,
species_url VARCHAR(255),
image_url VARCHAR(255)
);
-- Table to store Pokémon types
CREATE TABLE IF NOT EXISTS types (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL
);
-- Table to store Pokémon abilities
CREATE TABLE IF NOT EXISTS abilities (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL
);
-- Table to store Pokémon stats
CREATE TABLE IF NOT EXISTS stats (
id INT PRIMARY KEY AUTO_INCREMENT,
pokemon_id INT,
hp INT,
attack INT,
defense INT,
sp_attack INT,
sp_defense INT,
speed INT,
FOREIGN KEY (pokemon_id) REFERENCES pokemon(id)
);
-- Table to store Pokémon type relationships
CREATE TABLE IF NOT EXISTS pokemon_types (
pokemon_id INT,
type_id INT,
PRIMARY KEY (pokemon_id, type_id),
FOREIGN KEY (pokemon_id) REFERENCES pokemon(id),
FOREIGN KEY (type_id) REFERENCES types(id)
);
-- Table to store Pokémon ability relationships
CREATE TABLE IF NOT EXISTS pokemon_abilities (
pokemon_id INT,
ability_id INT,
PRIMARY KEY (pokemon_id, ability_id),
FOREIGN KEY (pokemon_id) REFERENCES pokemon(id),
FOREIGN KEY (ability_id) REFERENCES abilities(id)
);
-- Table to store Pokémon evolution chains
CREATE TABLE IF NOT EXISTS evolution_chains (
id INT PRIMARY KEY AUTO_INCREMENT,
chain_url VARCHAR(255) NOT NULL
);
-- Table to store Pokémon varieties
CREATE TABLE IF NOT EXISTS varieties (
id INT PRIMARY KEY AUTO_INCREMENT,
pokemon_id INT,
variety_name VARCHAR(100),
image_url VARCHAR(255),
FOREIGN KEY (pokemon_id) REFERENCES pokemon(id)
);
-- Table to store Pokémon species details
CREATE TABLE IF NOT EXISTS species (
id INT PRIMARY KEY AUTO_INCREMENT,
pokemon_id INT,
genus VARCHAR(100),
flavor_text TEXT,
growth_rate VARCHAR(100),
base_happiness INT,
capture_rate INT,
gender_rate INT,
FOREIGN KEY (pokemon_id) REFERENCES pokemon(id)
);
-- Table to store Pokémon egg groups
CREATE TABLE IF NOT EXISTS egg_groups (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL
);
-- Table to store Pokémon egg group relationships
CREATE TABLE IF NOT EXISTS pokemon_egg_groups (
pokemon_id INT,
egg_group_id INT,
PRIMARY KEY (pokemon_id, egg_group_id),
FOREIGN KEY (pokemon_id) REFERENCES pokemon(id),
FOREIGN KEY (egg_group_id) REFERENCES egg_groups(id)
);