Defining the schemas and models

For the purposes of the application we are building, we're going to have only two different, unique schemas and associated models: an Image model and a Comment model. If we were to take this application to production and really build it out with all of the necessary features, we would expect to have many more models as well.

First, create a new directory labeled models in your project, and we will store the Node.js modules for each of our models here. Create three files named image.js, comment.js, and index.js in this directory. Let's take a look at the Image model first. Copy the following block of code into the models/image.js file:

const mongoose = require('mongoose'),
Schema = mongoose.Schema,
path = require('path');

const ImageSchema = new Schema({
title: { type: String },
description: { type: String },
filename: { type: String },
views: { type: Number, 'default': 0 },
likes: { type: Number, 'default': 0 },
timestamp: { type: Date, 'default': Date.now }
});

ImageSchema.virtual('uniqueId')
.get(function() {
return this.filename.replace(path.extname(this.filename), '');
});

module.exports = mongoose.model('Image', ImageSchema);

First, we define our ImageSchema with the various fields that we are going to store in MongoDB for each of the images. We created a virtual property of uniqueId, which is just the filename with the file extension removed. As we want our Image model to be available throughout the rest of our app, we export it using module.exports. Note that we are exporting the model, not the schema (as the schema itself is fairly useless to us). Let's set up a similar model for comments. Copy the following block of code into the models/comment.js file:

const mongoose = require('mongoose'), 
    Schema = mongoose.Schema, 
    ObjectId = Schema.ObjectId; 
 
const CommentSchema = new Schema({ 
image_id:   { type: ObjectId }, 
email:      { type: String }, 
name:       { type: String }, 
gravatar:   { type: String }, 
comment:    { type: String }, 
timestamp:  { type: Date, 'default': Date.now } 
}); 
 
CommentSchema.virtual('image') 
.set(function(image){ 
    this._image = image; 
})
.get(function() { return this._image; }); module.exports = mongoose.model('Comment', CommentSchema);

There are a few important things to take note of with this model. First, we have
a field labeled image_id, which has an ObjectId type. We're going to use this
field to store the relationship between the comment and image that it was posted to.
The ObjectId that gets stored in this field is the _id of the related image document from MongoDB.

We also define virtual on the comment schema labeled image, which we provide a getter and setter for. The image virtual property will be how we attach the related image when we retrieve comments later in our controllers. For every comment, we are going to iterate through and look up its associated image and attach that image object as a property of the comment.

Handling the name of collections
You name your models using singular terms, and Mongoose will recognize this and create your collections using pluralized model names. So, a model defined as Image will have a collection in MongoDB named images. Mongoose tries to be smart about this; however, a model defined as Person will have a corresponding collection named people, and so on. (And yes, octopus will result in octopi!)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.69.163