I am currently following a tutorial on implementing a database (Mongoose) in a simple website created using Express-framework. I do not have any problem understanding the concept of models, but I fail to make sense of the lines following the comment "Virtual for book's URL" in the code attached. How do these lines operate, and what role does having a virtual property have in this context?
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var BookSchema = new Schema(
{
title: {type: String, required: true},
author: {type: Schema.Types.ObjectId, ref: 'Author', required: true},
summary: {type: String, required: true},
isbn: {type: String, required: true},
genre: [{type: Schema.Types.ObjectId, ref: 'Genre'}]
}
);
// Virtual for book's URL
BookSchema
.virtual('url')
.get(function () {
return '/catalog/book/' + this._id;
});
//Export model
module.exports = mongoose.model('Book', BookSchema);
Virtuals are typically used for computed properties on documents.
BookSchema
// 'url' is the name of the virtual
.virtual('url')
.get(function () {
// you are computing dynamic url
return '/catalog/book/' + this._id;
});
Important thing about virtuals is it does not store anything on database. Imagine you stored "name" and "lastname" but you want to display fullname without having "fullname" column. You would be using repeated data on database if you add fullname
property to your schema, this will slow down and will also cost you. Instead you write virtual:
YourSchema.get(function(){
return this.firstname + ' ' + this.lastname
})
You can also go one step further and return fullname
property as it is in a column in database. mongoose Schema takes options object
let options={
toJSON:{
virtuals: true
}
}
set the virtual
let YourSchemaVirtual=YourSchema.virtual('fullname')
now when querying the database, get the collection
YourModel.findOne({firstname:"name"}).exec(function(err,user){
res.send(user)
})
In return value you will see fullname
field computed as this.firstname + ' ' + this.lastname
even though you have no fullname
stored in database.