Seeds File

In the Web Developer Boot Camp we are getting ready to add some new features: comments and users. Before we do this we are creating a seeds file to clear the database and populate it with some new campgrounds that have a few comments each. This will allow us to easier tell if our code is working as we try and add in the new functionality.

Side Note: During this process we will be doing error driven development. This means we will write some code, see what errors it throws and then try and resolve them until we end up with a functioning chunk of software. (This is usually how my personal projects end up going anyway)

For the next bunch of posts I’ll be using the Yelp Camp Application as an example. This is a simple web app that displays campgrounds with a title and breif description. Still using the MEAN stack. You can see what the app.js file looks like below:

var express = require("express"),
    app = express(),
    bodyParser = require("body-parser"),
    mongoose = require("mongoose"),
    Campground = require("./models/campground");

mongoose.connect("mongodb://localhost/yelp_camp");
app.use(bodyParser.urlencoded({ extended: true }));
app.set("view engine", "ejs");

// Campground.create({
//     name: "Granite Hill",
//     image: "https://farm1.staticflickr.com/60/215827008_6489cd30c3.jpg",
//     description: "This is a huge granite hill, no bathrooms. No water. Beautiful granite!"
// }, function(err, campground) {
//     if (err) {
//         console.log("There was an error");
//         console.log(err);
//     }
//     else {
//         console.log("Newly Created Campground");
//         console.log(campground);
//     }<span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>
// });

app.get("/", function(req, res) {
    res.render("landing");
});

//INDEX - Display all campgrounds
app.get("/campgrounds", function(req, res) {
    //Get all campgrounds from DB
    Campground.find({}, function(err, allCampgrounds) {
        if (err) {
            console.log(err);
        }
        else {
            res.render("index", { campgrounds: allCampgrounds });
        }
    });
    // res.render("campgrounds", { campgrounds: campgrounds });
});

//CREATE - add new campground to DB
app.post("/campgrounds", function(req, res) {
    var name = req.body.name;
    var image = req.body.image;
    var desc = req.body.description;
    var newCampground = { name: name, image: image, description: desc };
    //Create new campground and save to database
    Campground.create(newCampground, function(err, newlyCreated) {
        if (err) {
            console.log(err);
        }
        else {
            //redirect back to campgrounds page
            res.redirect("/campgrounds");
        }
    });

});

//NEW - show form or create new campground
app.get("/campgrounds/new", function(req, res) {
    res.render("new");
});

//SHOW - shows info about campground
app.get("/campgrounds/:id", function(req, res) {
    //find the campground with the selected ID
    Campground.findById(req.params.id, function(err, foundCampground) {
        if (err) {
            console.log(err);
        }
        else {
            //render the showpage
            res.render("show", { campground: foundCampground });
        }
    });
    req.params.id

});

app.listen(process.env.PORT, process.env.IP, function() {
    console.log("Yelp Camp Server is Running!");
});

Back to Creating a Seeds File

First step create seeds.js file in working directory.

$ touch seeds.js

In that file we will want to start off by requiring mongoose and the Campground module. Our first bit of code needs to clear the database for our new entires. I’ve written a simple function to clear the database which gives us something to return when the seeds file is required by our app.js.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: