Objective |
---|
Take an existing application and review CRUD |
Review Mongoose CRUD operations |
Apply Mongoose CRUD to an existing in memory application to add persistence |
Use the following directions at this repo
git clone
this reponpm run setup
npm run start
Take a few minustes to review the application.
Let's make a models
file. This file will store all of our model definitions. Later we will have to separate this into multiple files.
touch models.js
And let's define our Todo
model inside of it.
var mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/todos_app");
However these three lines assume we already have mongoose
already installed. We should install it before we forget. It's hard to remember what you need to install before you need it, but once you realize you do, then you should take that opportunity to do it.
npm install --save mongoose
Great! We can now define our Todo
Schema.
var mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/todos_app");
var todoSchema = new mongoose.Schema({
title: {
type: String,
default: ""
},
description: {
type: String,
default: ""
},
completed: {
type: Boolean,
default: false
}
});
var Todo = mongoose.model("Todo", todoSchema);
module.exports.Todo = Todo;
Note that the last step will allow us to interact with the Todo
model when we require this file later. Let's see that this works.
Go into the node repl.
var db = require("./models");
db.Todo.create({
title: "DO LAUNDRY",
description: "Two loads!"
}, function (err, todo) {
console.log("TODO CREATED");
console.log(todo);
});
It can be hard to do this every time you go into Node, so I make a little console.js
file that does this for me.
console.js
var REPL = require("repl");
var db = require("./models");
var repl = REPL.start("Todo > ");
repl.context.db = db;
repl.on("exit", function () {
console.log("GOODBYE!!");
process.exit();
});
We can then run this using
node console.js
Let's add our models.js
file to our application, index.js
.
var db = require("./models");
Then let's refactor GET /todos
app.get("/todos", function (req, res) {
db.Todo.find({},
function (err, todos) {
res.send(todos);
});
});
Then go to localhost:3000 to verify this is working.
Let's now refactor our POST /todos
.
app.post("/todos", function (req, res) {
db.Todo.create(req.body.todo,
function (err, todo) {
res.send(201, todo);
});
});
Let refactor the delete method
app.delete("/todos/:id", function (req, res) {
db.Todo.findByIdAndRemove({
_id: req.params.id
}, function (err, todo) {
res.send(204) // success No Content
});
});
Delete the old todos
array. It's not needed. :D
- Go through all the
public/javascripts/app.js
code and change everywhere you seeindex
to_id
. Good luck! Yikes!!!!
To view your data we want to try the following:
npm install mongo-express
and then set it up
mv node_modules/mongo-express/config.default.js node_modules/mongo-express/config.js
Now we edit the config file.
useBasicAuth: false
Then we run it to see our data
node node_modules/mongo-express/