Giter Club home page Giter Club logo

mongo_todo_refactor's Introduction

Todo Refactor

Adding Mongo

Objective
Take an existing application and review CRUD
Review Mongoose CRUD operations
Apply Mongoose CRUD to an existing in memory application to add persistence

Getting Started

Use the following directions

  • git clone this repo
  • npm run setup
  • npm run start

Take a few minustes to review the application.

Reviewing

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");

varrepl = 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

Refactoring

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.

CREATING

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);
    });
});

Removing

Let refactor the delete method

app.delete("/todos/:id", function (req, res) {
  db.Todo.findAndRemove({
    _id: req.params.id
  }, function (err, todo) {
    res.send(204) // success No Content
  });
});

Clean Up

Delete the old todos array. It's not needed. :D

  • Go through all the public/javascripts/app.js code and change everywhere you see index to _id. Good luck! Yikes!!!!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.