Node REST API - Part VI
I am a Mongoose Model! Pretty huh!
In Part V we reviewed index.js file - an entry point for our REST API. In this tutorial we will build mongoose models. In Part II of this series we created few normalized documents for our recipes database. Now let’s build mongoose models using each document.
RecipeTypes Model
Our RecipeTypes document looks like below.
[
{
"_id": 1,
"recipe_type": "Italian"
},
{
"_id": 2,
"recipe_type": "Indian"
},
{
"_id": 3,
"recipe_type": "Thai"
},
{
"_id": 4,
"recipe_type": "Mexican"
}
]
In this document we have two columns.
- _id
- recipe_type
Now let’s create a new folder under api folder. Go to express-api project in Sublime Text. Right click on api folder and click New Folder. Name it a model folder.
Project should look like below.
Let’s create a recipetype.model.js file under model folder. Right click on model folder and click New File. Name this file recipetype.model.js. While this file is open in Sublime Text, let’s write our RecipeType model.
/**
* @model recipetype.model.js
* @author Ritesh Patel
* @version 1.0
**/
//require mongoose module
var mongoose = require('mongoose');
//Instantiate mongoose schema
var Schema = mongoose.Schema;
//create recipe type schema
var recipeTypeSchema = new Schema({
"_id":Number,
"recipe_type":String
});
//export schema
module.exports = mongoose.model('RecipeType', recipeTypeSchema);
Pretty straight forward. Just like other 3rd party modules / middlewares we import mongoose. Instantiate mongoose schema and store the reference in a local variable. Next we create a new recipe type schema and assign _id and recipe_type columns to this schema. Additionally we specify column types in our schema definition. Done!
Contributors Model
Now let’s move on to Contributors document.
[
{
"_id": 1,
"contributor_name": "Ritesh Patel",
"account_status": "Active"
},
{
"_id": 2,
"contributor_name": "Bobby Flay",
"account_status": "Active"
}
]
We have three columns for this document. _id, contributor_name & account_status. Let’s create another model file in our project. Right click on model folder and click New File. Name this file contributor.model.js. Here is the code for contributors model.
/**
* @model contributor.model.js
* @author Ritesh Patel
* @version 1.0
**/
//require mongoose model
var mongoose = require('mongoose');
//Instantiate mongoose schema
var Schema = mongoose.Schema;
//create contributor schema
var contributorSchema = new Schema({
"_id":Number,
"contributor_name":String,
"account_status":String
});
//export schema
module.exports = mongoose.model('Contributor', contributorSchema);
Sure enough, id is a Number type, contributor_name is a String type and account_status is also a String type. Easy huh!
Recipe Model
Finally, we look at Recipes document.
{
"_id": 1,
"name ": "Baked Ziti",
"title ": "Delicious Pasta made with marinara sauce & heavenly four cheese!",
"prep_time ": "10 minutes",
"total_time ": "40 minutes",
"difficulty ": "easy",
"recipe ": "This is the key that stores our entire recipe ",
"recipe_type ": 1,
"tag_words ": "Italian, Pasta, Easy Italian Recipe ",
"contributor ": 2,
"stars": 5
}
This document has lot more columns than previous two documents. But no worries, now we have mastered mongoose modeling, right?
Let’s create one more file to hold recipe model. Right click on the model folder and click New File. Name this file recipe.model.js. Below is the code for recipe model.
/**
* @model recipe.model.js
* @author Ritesh Patel
* @version 1.0
**/
//require mongoose module
var mongoose = require('mongoose');
//instantiate mongoose schema
var Schema = mongoose.Schema;
//create recipe schema
var recipeSchema = new Schema({
"_id":Number,
"name":String,
"title":String,
"prep_time":String,
"total_time":String,
"difficulty":String,
"recipe":String,
"recipe_type":Number,
"tag_words":String,
"contributor":Number,
"stars":Number
});
//export schema
module.exports = mongoose.model('Recipe', recipeSchema);
Similar to other previous models we specify columns and column types for this model as well. Mongoose modeling makes it very easy to operate on data directly into MongoDB.
If you followed this tutorial then your project should look something like below. Don't worry about the routes.js file yet. We will save it for the next tutorial.
This wraps up our mongoose modeling tutorial. In Part VII of this series we will implement routes.
See you then!
Hi, I am Ritesh Patel. I live in a beautiful town surrounded by the mountains. C&O Canal is few miles away. State parks are only a distance away & bike trails galore. It is home sweet home Frederick, MD. A passionate developer. Love to cook. Enjoy playing "Bollywood Tunes" on my harmonica. Apart from that just a normal guy.