Skip to content

Commit 7dd4fc0

Browse files
committed
feat: add seed script with default data for corex
1 parent 89eff86 commit 7dd4fc0

8 files changed

Lines changed: 74 additions & 1 deletion

File tree

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
PORT=5000
2+
MONGODB_URI=mongodb://127.0.0.1:27017/corex
3+
JWT_SECRET=replace_with_a_local_jwt_secret
4+
NODE_ENV=development

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"scripts": {
2525
"start": "node server.js",
2626
"dev": "nodemon server.js",
27-
"lint": "eslint . --ext .js"
27+
"lint": "eslint . --ext .js",
28+
"seed": "node scripts/seed.js"
2829
},
2930
"keywords": [
3031
"hacktoberfest",
@@ -43,6 +44,7 @@
4344
"license": "MIT",
4445
"dependencies": {
4546
"cors": "^2.8.5",
47+
"dotenv": "^17.2.2",
4648
"express": "^5.1.0",
4749
"mongoose": "^8.18.2"
4850
},

scripts/db/products.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[
2+
{
3+
"name": "CoreX Whey Protein",
4+
"price": 49.99,
5+
"category": "Protein",
6+
"description": "High-quality whey protein for muscle recovery.",
7+
"image": "https://via.placeholder.com/200"
8+
},
9+
{
10+
"name": "CoreX Creatine",
11+
"price": 24.99,
12+
"category": "Creatine",
13+
"description": "Boost your performance with pure creatine monohydrate.",
14+
"image": "https://via.placeholder.com/200"
15+
}
16+
]

scripts/seed.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// scripts/seed.js
2+
import mongoose from "mongoose";
3+
import "dotenv/config";
4+
import Product from "../src/models/product.model.js";
5+
import { readFile } from "fs/promises";
6+
import path from "path";
7+
8+
if (!process.env.MONGODB_URI) {
9+
console.error("❌ MONGODB_URI is missing. Please create a .env file based on .env.example and set your MongoDB connection string.");
10+
process.exit(1);
11+
}
12+
13+
async function getSeedProducts() {
14+
const filePath = path.resolve("scripts/db/products.json");
15+
try {
16+
const data = await readFile(filePath, "utf-8");
17+
return JSON.parse(data);
18+
} catch (err) {
19+
console.error("❌ Failed to read products.json", err);
20+
process.exit(1);
21+
}
22+
}
23+
24+
async function seedDB() {
25+
try {
26+
await mongoose.connect(process.env.MONGODB_URI);
27+
const seedProducts = await getSeedProducts();
28+
await Product.deleteMany();
29+
await Product.insertMany(seedProducts);
30+
console.log("✅ Seed data inserted");
31+
process.exit();
32+
} catch (err) {
33+
console.error("❌ Error seeding DB", err);
34+
process.exit(1);
35+
}
36+
}
37+
38+
seedDB();

src/controllers/product.controller.js

Whitespace-only changes.

src/models/product.model.js

Whitespace-only changes.

src/routes/product.routes.js

Whitespace-only changes.

0 commit comments

Comments
 (0)