diff --git a/backend/helpers/seeder.js b/backend/helpers/seeder.js new file mode 100644 index 0000000..91eca7a --- /dev/null +++ b/backend/helpers/seeder.js @@ -0,0 +1,75 @@ +require("dotenv").config({ path: __dirname + "/./../.env" }) +const mongoose = require("mongoose") +const axios = require("axios") +const Character = require("../models/character.model") +const Episode = require("../models/episode.model") +const database_url = process.env.DATABASE_URL + +seed = async () => { + connectDb() + await seedCharacters() + await seedEpisodes() + closeDb() +} + +connectDb = async () => { + mongoose + .connect(database_url, { + useNewUrlParser: true, + useUnifiedTopology: true, + }) + .then(() => { + console.log("Database Connected") + return true + }) + .catch((err) => { + console.error("Error : Cannot connect to Database " + err) + return false + }) +} + +closeDb = async () => { + mongoose.connection.close() +} + +seedCharacters = async () => { + let charactersEndpoint = "https://finalspaceapi.com/api/v0/character" + let characters + await axios + .get(charactersEndpoint) + .then((res) => (characters = res.data)) + .then(() => console.log("Characters data Fetched")) + .catch((err) => console.log("Error: characters data didn't fetched " + err)) + + try { + for (var character of characters) { + let newCharacter = new Character(character) + await newCharacter.save() + } + console.log("Characters seeded to database successfully") + } catch (err) { + console.log("Error: characters didn't seeded to database " + err) + } +} + +seedEpisodes = async () => { + let episodesEndpoint = "https://finalspaceapi.com/api/v0/episode" + let episodes + await axios + .get(episodesEndpoint) + .then((res) => (episodes = res.data)) + .then(() => console.log("Episodes data Fetched")) + .catch((err) => console.log("Error: episodes data didn't fetched " + err)) + + try { + for (var episode of episodes) { + let newEpisode = new Episode(episode) + await newEpisode.save() + } + console.log("Episodes seeded to database successfully") + } catch (err) { + console.log("Error: episodes didn't seeded to database " + err) + } +} + +seed() diff --git a/backend/package-lock.json b/backend/package-lock.json index 5beac51..83104aa 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -240,7 +240,15 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/astral-regex/-/astral-regex-1.0.0.tgz", "integrity": "sha512-+Ryf6g3BKoRc7jfp7ad8tM4TtMiaWvbF/1/sQcZPkkS7ag3D5nMBCe2UfOTONtAkaG0tO0ij3C5Lwmf1EiyjHg==", - "dev": true + "dev": true, + "axios": { + "version": "0.20.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.20.0.tgz", + "integrity": "sha512-ANA4rr2BDcmmAQLOKft2fufrtuvlqR+cXNNinUmvfeSNCOF98PZL+7M/v1zIdGo7OLjEA9J2gXJL+j4zGsl0bA==", + "dev": true, + "requires": { + "follow-redirects": "^1.10.0" + } }, "balanced-match": { "version": "1.0.0", @@ -995,6 +1003,10 @@ "version": "2.0.2", "resolved": "https://registry.npmjs.org/flatted/-/flatted-2.0.2.tgz", "integrity": "sha512-r5wGx7YeOwNWNlCA0wQ86zKyDLMQr+/RB8xy74M4hTphfmjlijTSSXGuH8rnvKZnfT9i+75zmd8jcKdMR4O6jA==", + "follow-redirects": { + "version": "1.13.0", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.0.tgz", + "integrity": "sha512-aq6gF1BEKje4a9i9+5jimNFIpq4Q1WiwBToeRK5NvZBd/TRsmW8BsJfOEGkr76TbOyPVD3OVDN910EcUNtRYEA==", "dev": true }, "forwarded": { @@ -2420,3 +2432,5 @@ } } } +} +} \ No newline at end of file diff --git a/backend/package.json b/backend/package.json index eecd97d..6b3abc1 100644 --- a/backend/package.json +++ b/backend/package.json @@ -8,11 +8,13 @@ "start": "node server.js", "prod": "NODE_ENV=production node server.js", "lint": "eslint .", - "lint:fix": "eslint --fix --ext .js,.jsx ." + "lint:fix": "eslint --fix --ext .js,.jsx .", + "seed":"node ./helpers/seeder.js" }, "author": "Ashutosh K Singh", "license": "BSD-3-Clause", "dependencies": { + "axios": "^0.20.0", "cors": "^2.8.5", "express": "^4.17.1", "express-rate-limit": "^5.1.3",