Skip to content

Commit

Permalink
add database seeder to backend (#64) (#91)
Browse files Browse the repository at this point in the history
* add database seeder to backend (#64)

* fix LGTM alerts (#64)

* move seeder file to helpers folder
  • Loading branch information
mhalsubhi authored Oct 9, 2020
1 parent ae09ab4 commit fe2e052
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
75 changes: 75 additions & 0 deletions backend/helpers/seeder.js
Original file line number Diff line number Diff line change
@@ -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()
16 changes: 15 additions & 1 deletion backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit fe2e052

Please sign in to comment.