Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
"@babel/core": "^7.17.9",
"@babel/node": "^7.16.8",
"@babel/preset-env": "^7.16.11",
"bcrypt": "^6.0.0",
"cors": "^2.8.5",
"dotenv": "^17.2.3",
"express": "^4.17.3",
"mongoose": "^9.1.5",
"nodemon": "^3.0.1"
}
}
36 changes: 36 additions & 0 deletions seed-database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import "dotenv/config";
import mongoose from "mongoose"
import thoughtsData from "./data.json" with { type: "json" }

const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/happythoughts"
mongoose.connect(mongoUrl)



// defining 'though schema' simple for seeding
const ThoughtSchema = new mongoose.Schema({
message: String,
hearts: Number,
createdAt: Date
})

const Thought = mongoose.model("Thought", ThoughtSchema)

// function to populate the database
const seedDatabase = async () => {
try {
// delete all existing thoughts
await Thought.deleteMany()
// insert all thoughts from data.json
await Thought.insertMany(thoughtsData)
console.log("Database seeded successfully!")
} catch (error) {
console.error("Error seeding database:", error)
} finally {

// closes the database connection when done
mongoose.connection.close()
}
}

seedDatabase()
Loading