Defining the User Model

One of the first things that you should do when you begin implementing an application is define the model for the objects that will be stored in the database. In this case you need a User object. Defining the model first helps you get a better idea of how to implement the routes and controllers.

Listing 26.1 implements the User model for this chapter’s application. It is very simple but provides a good example of what you can do. You can easily enhance this example as much as needed.

Notice that the schema defined implements a unique username as well as email, color, and hashed_password fields. The final line creates the model in Mongoose.

Listing 26.1 users_model.js: Implementing the User model for Mongoose


01 var mongoose = require('mongoose'),
02     Schema = mongoose.Schema;
03 var UserSchema = new Schema({
04     username: { type: String, unique: true },
05     email: String,
06     color: String,
07     hashed_password: String
08 });
09 mongoose.model('User', UserSchema);


..................Content has been hidden....................

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