mirror of
https://github.com/Alvin-Zilverstand/pokedex.git
synced 2026-03-06 13:25:11 +01:00
Add type insertion logic to fetch_and_insert.js
This commit is contained in:
@@ -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 mysql = require('mysql');
|
||||||
|
|
||||||
const connection = mysql.createConnection({
|
const connection = mysql.createConnection({
|
||||||
@@ -10,6 +10,19 @@ const connection = mysql.createConnection({
|
|||||||
|
|
||||||
connection.connect();
|
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 fetchPokemonData = async (id) => {
|
||||||
const url = `https://pokeapi.co/api/v2/pokemon/${id}/`;
|
const url = `https://pokeapi.co/api/v2/pokemon/${id}/`;
|
||||||
const url2 = `https://pokeapi.co/api/v2/pokemon-species/${id}/`;
|
const url2 = `https://pokeapi.co/api/v2/pokemon-species/${id}/`;
|
||||||
@@ -68,6 +81,7 @@ const insertPokemonData = (pokemon) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const fetchAndInsertAllPokemon = async () => {
|
const fetchAndInsertAllPokemon = async () => {
|
||||||
|
insertTypes();
|
||||||
for (let i = 1; i <= 1010; i++) {
|
for (let i = 1; i <= 1010; i++) {
|
||||||
const pokemon = await fetchPokemonData(i);
|
const pokemon = await fetchPokemonData(i);
|
||||||
insertPokemonData(pokemon);
|
insertPokemonData(pokemon);
|
||||||
|
|||||||
Reference in New Issue
Block a user