Reader small image

You're reading from  Mongoose for Application Development

Product typeBook
Published inAug 2013
Reading LevelIntermediate
PublisherPackt
ISBN-139781782168195
Edition1st Edition
Languages
Right arrow
Author (1)
Simon Holmes
Simon Holmes
author image
Simon Holmes

Simon Holmes started his journey as a web developer in the late 1990s. He built his first website for a project at university and soon saw what the industry had to offer when he promptly sold it! Following university, Simon worked his way through the ranks of design agency life, learning the skills of becoming a full-stack web developer. From server management and database design to building dynamic UIs from Photoshop files, it all fell under Simon's remit. Having witnessed first-hand the terrible JavaScript code so prevalent in the early 2000s, Simon is very much enjoying its resurgence as a powerful, structured language. Simon now works in SaaS, which is very heavy on the JavaScript.
Read more about Simon Holmes

Right arrow

Chapter 11. Plugins – Re-using Code

In this chapter, we will introduce the Mongoose plugin architecture and see how we can use it to create modular re-usable code. We will look at the syntax and how to write them and include them in our code.

By the end of this chapter, you will understand how to create and use Mongoose plugins. You will also know where to go to find existing plugins that others have written, and contribute to the community by submitting your own. You will have created some plugins in the MongoosePM application.

Reusable schema plugins


If we look at our schemas, we can see some common elements that we are repeating. For example, each of our schemas has an identical modifiedOn path, and our project and task schemas each have identical createdBy and createdOn paths.

If you're at all familiar with the DRY (Don't Repeat Yourself) principle of coding, you'll probably want to tidy these bits up and just declare them once. This is where the Mongoose plugin architecture comes in.

Creating a schema plugin

Let's start by creating a schema extension for adding createdOn and createdBy. Inside our model/db.js file, we can add the following code, preferably above the definitions for our two schemas:

var creationInfo = function creationInfo (schema, options) {
  schema.add({createdOn: { type: Date, default: Date.now }});
  schema.add({createdBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true}});
};

This exposes a function that will allow us to plug in the paths createdOn and createdBy, to some...

Sharing with the community


Mongoose plugins are not just re-usable for you, you can share them with the rest of the community. By packaging them up as npm modules, and tagging them with "mongoose", they will become available in the Mongoose plugins directory.

There are already a large number of community-submitted plugins available that include adding support for a long number SchemaType, validation helpers, and authentication and user management plugins.

You can find the directory, and submission guidelines at the following link:

http://plugins.mongoosejs.com

Summary


In this chapter, we've seen how we can take repeated paths from our schemas and turn them into a single Mongoose plugin. We've learned how to create them as separate files, include them in our application, and also use schema middleware to set data on a save operation. We have used all of this knowledge to create plugins and add them to our MongoosePM application, tidying up our code and removing repetition.

This is the final chapter of Mongoose for Application Development. By now you should have the tools and understanding to enable you to build a web application based on everything Mongoose has to offer. I'm sure that by now you will agree that Mongoose is a very useful addition to the Node.js and MongoDB technology stack.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mongoose for Application Development
Published in: Aug 2013Publisher: PacktISBN-13: 9781782168195
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Simon Holmes

Simon Holmes started his journey as a web developer in the late 1990s. He built his first website for a project at university and soon saw what the industry had to offer when he promptly sold it! Following university, Simon worked his way through the ranks of design agency life, learning the skills of becoming a full-stack web developer. From server management and database design to building dynamic UIs from Photoshop files, it all fell under Simon's remit. Having witnessed first-hand the terrible JavaScript code so prevalent in the early 2000s, Simon is very much enjoying its resurgence as a powerful, structured language. Simon now works in SaaS, which is very heavy on the JavaScript.
Read more about Simon Holmes