Sunteți pe pagina 1din 5

CONNECTING MONGOOSE DATABASE TO YOUR SERVER

Delete catsData.js folder if it exists- which just contained export.module =[];


CREATE A MODELS FOLDER
CREATE A cats.js (OR WHATEVER THIS MODEL IS GOING TO BE CALLED)

Inside this cats.js file inside the model folder is where you create the SCHEMA (or the
blueprint that structures the data for that specific object)

const mongoose = require("mongoose");


const {Schema} = mongoose;

//Schema is a constructor which determines the template for all


documents
//in a collection

//EVERY COLLECTION NEEDS A SCHEMA


//below the CatSchema is a constructor that makes a constructor
//inside your new Schema you tell it what type each key should be
const catSchema = new Schema({
name: String,
breed: {
required:true,
type:String
},
age: Number,
adoptionStatus:{
type: String,
default : "not adopted"
}
})
// to define a collection you need to create a model like below

const CatModel = mongoose.model("cats", catSchema)


module.exports = CatModel;

END OF NEW MODEL FOLDER-FILE REQUIREMENTS


**IN THE cats.js IN THE ROUTES FOLDER YOULL NO LONGER NEED THE
JAVASCRIPT PORTIONS OF THE GET REQUESTS

const express = require('express');


const catRouter = express.Router();
const uuid = require('uuid');

const CatModel = require("../models/cats.js")

//routes go here
//the route should be just / because in the index theres already an
implied /cats
catRouter.route("/")
.get((req, res) => {
//use .find() to get database document
CatModel.find(​req.query​,(err, foundCats) =>{
if (err) return res.send(err);
res.status(200).send(foundCats);
})
})

​ const {query}= req;


const queriedCats = cats.filter(cat => {
for(let key in query){
if(!cat.hasOwnProperty(key) || String(cat[key]) !==
query[key]){
return false;
}
}
return true;
})
res.status(200).send(queriedCats);
​ })
​ .post((req, res) => {
//make a new new instance of a CatModel
(document) set on the req.body
const newCat = new CatModel(req.body)​;
newCat.save((err, addedCat)=>{
if(err) return res.send(err);
res.status(201).send(addedCat);
})
})
//use save() to add a cat to the database

//give data in request body an id


const newCat = req.body;
newCat._id = uuid();

// save the data in request body to the 'database'


cats.push(newCat);
//send back the data that was added
res.status(201).send(newCat);
});

// GET one request


catRouter.route("/cats/:id")
catRouter.route("/​:id"​)
.get((req, res) => {
​ //use findOne() to get specific cat from database
​CatModel.findOne(​{_id: req.params.id}​, (err, foundCat) => {
if (err) return res.send(err);
if(!foundCat) return res.status(404)
res.status(200).send(foundCat);
})
})
//use findOne() to get specific cat from database
// get param id:
const { id } = req.params;
//find cat matching id:
const foundCat = cats.filter(cat => cat._id === id)[0];
// send back the cat
res.status(200).send(foundCat);
})
// DELETE one request
​ .delete((req, res) => {
CatModel.findOneAndRemove(​{_id: req.params.id}​,(err, deletedCat) =>
{
if (err) return res.send(err);
if(!deletedCat) return res.status(404).send({message:"Cat not
found, or has already been deleted."});
res.status(200).send({message: "Your item has been deleted"});
})
})

//use findByIdAndDelete() to delete from database

const { id } = req.params;
// find and remove cat matching id:
cats = cats.filter(cat => cat._id !== id);
//send back the message the cat was removed
res.status(204).send();
})
//PUT one
//youll need to include ​{_id:req.params.id}​, set the ​req.body,​ then
set a new value on the body with​ {new:true}
//​{new:true} ​bypasses the default and sends back the updated version
instead of the old version

​ .put((req, res) => {


CatModel.findOneAndUpdate(​{_id: req.params.id}​,
​ req.body, {new: true}​, (err, updatedCat) => {
if (err) return res.send(err);
res.status(200).send(updatedCat);
})
})
//find the param id
const { id } = req.params;
let editedCat = req.body;
//map through cats and replace the cat w/ matching id with
req.body
cats = cats.map(cat => cat._id === id ? editedCat = { ...cat,
...editedCat } : cat);
res.status(200).send(editedCat);
})

module.exports = catRouter;

S-ar putea să vă placă și