Skip to content

Commit 87e97c0

Browse files
authored
Merge pull request #2 from tildetilde/modeling
Modeling
2 parents 7da754b + 53cebc7 commit 87e97c0

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 🥇 Step 1: Finalize backend using MongoDB & Mongoose
44

5-
- [ ] Create a `Thought` model in `models/Thought.js`
5+
- [x] Create a `Thought` model in `models/Thought.js`
66
- Fields: `message` (string, required, min/max length), `hearts`, `createdAt`
77
- [ ] Seed the database with sample thoughts
88
- [ ] Create route: `GET /thoughts` → return latest (e.g. 20)

controllers/thoughtsController.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { v4 as uuidv4 } from "uuid";
2-
31
// controllers/thoughtsController.js
42
import loadThoughts from "../utils/loadThoughts.js";
53
import saveThoughts from "../utils/saveThoughts.js";

models/Thought.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import mongoose from "mongoose";
2+
3+
const ThoughtSchema = new mongoose.Schema({
4+
message: {
5+
type: String,
6+
required: [true, "A message is required"],
7+
minlength: [5, "A message must be at least 5 characters long"],
8+
maxlength: [140, "A message can't be longer than 140 characters"],
9+
trim: true,
10+
},
11+
hearts: {
12+
type: Number,
13+
default: 0,
14+
min: [0, "Hearts cannot be negative"],
15+
},
16+
createdAt: {
17+
type: Date,
18+
default: Date.now,
19+
},
20+
});
21+
22+
export const Thought = mongoose.model("Thought", ThoughtSchema);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"@babel/node": "^7.16.8",
1414
"@babel/preset-env": "^7.16.11",
1515
"cors": "^2.8.5",
16+
"dotenv": "^16.5.0",
1617
"express": "^4.17.3",
1718
"express-list-endpoints": "^7.1.1",
1819
"nodemon": "^3.0.1"

utils/seedThoughts.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import mongoose from "mongoose";
2+
import dotenv from "dotenv";
3+
import fs from "fs";
4+
import { Thought } from "../models/Thought.js";
5+
6+
dotenv.config();
7+
8+
const mongoUrl =
9+
process.env.MONGO_URL || "mongodb://localhost:27017/happyThoughts";
10+
mongoose.connect(mongoUrl);
11+
12+
const thoughtData = JSON.parse(fs.readFileSync("./data/data.json"));
13+
14+
const seedDatabase = async () => {
15+
try {
16+
await Thought.deleteMany();
17+
await Thought.insertMany(thoughtData);
18+
console.log(`🌱 Successfully seeded ${thoughtData.length} thoughts!`);
19+
} catch (err) {
20+
console.error("❌ Seeding error:", err);
21+
} finally {
22+
mongoose.disconnect();
23+
}
24+
};
25+
26+
seedDatabase();

0 commit comments

Comments
 (0)