Tutorial
Create Schema & Model

Create Schema & Model using Mongoose & Express JS

Create Schema & Model (Collection & Tables) using Mongoose & Express JS

▶️ Create a Model

  1. Go to the root directory of your project
  2. Create a folder named models using the command mkdir models and then cd models
  3. Create a file named task.js using the command echo > task.js
  4. Start the server again using the command npm run dev
  5. Open the file task.js in your editor

▶️ Mongoose Call

  1. Import mongoose using the command const mongoose = require('mongoose')

▶️ Create Schema

  1. Create a schema using the command const taskSchema = new mongoose.Schema({})

  2. Add the following fields to the schema

    • title - String
    • description - String
    • completed - Boolean
  3. Add the following code to the schema

inside codeblock I explain the code

// task.js
 
// import mongoose module
const mongoose = require('mongoose');
 
// define the task schema
const taskSchema = new mongoose.Schema({
 // id is a string that identifies the task uniquely
 // it is required and cannot be duplicated
 id: {
    type: String,
    required: [true, "Please provide an id for the task."],
    trim: true,
    unique: true
 },
 // keep description as it is
 // description is a string that gives more details about the task
 // it is required and trimmed to remove extra whitespace
 description: {
    type: String,
    required: true,
    trim: true
 },
 // replace completed with status and make it an enum
 // status is a string that indicates whether the task is completed or incomplete
 // it can only have one of the two values specified in the enum option
 // by default, it is set to incomplete
 status: {
    type: String,
    enum: ['completed', 'incomplete'],
    default: 'incomplete'
 },
 
 // keep priority as it is
 // priority is a string that indicates how urgent the task is
 // it can only have one of the four values specified in the enum option
 // by default, it is set to medium
 // if an invalid value is provided, a custom error message will be thrown
 
priority: {
   type : String,
   enum : ['low', 'medium', 'high' , 'urgent'],
   default : 'medium',
   message : '{VALUE} is not supported'
},
 
// keep dueDate as it is
// dueDate is a date that specifies when the task should be completed by
// it has a custom validator function that checks if the due date is after or equal to the creation date
// if not, an error message will be thrown
 
dueDate : {
type : Date,
// validate : {
//   validator : function(value) {
//     return value >= this.createdAt;
//   },
//   message : 'Due date must be after creation date.'
// }
},
 
// keep owner as it is
// owner is an object id that references another model called User
// it indicates who created or owns the task
// it is required for every task document
 
owner : {
  type : mongoose.Schema.Types.ObjectId,
  ref : 'User',
  required : true
}
}, { timestamps : true })
 
// enable timestamps option to create createdAt and updatedAt fields automatically
// this option tells mongoose to add two fields to each document:
// createdAt - a date that indicates when the document was created
// updatedAt - a date that indicates when the document was last modified
// these fields are managed by mongoose and do not need to be defined in the schema
 
// export the task model using mongoose.model() method
// this method takes two arguments:
// -the name of the model (Task) which should match with its collection name (tasks) in MongoDB
// -the schema object (taskSchema) which defines its structure and behavior
 
module.exports = mongoose.model('Task', taskSchema)

▶️ Setup Uuid

This uuid is used to generate unique id for each task

  1. Install the uuid package using the command npm i uuid
  2. Import the uuid package using the command const \{ v4: uuidv4 } = require('uuid')

▶️ Add Id Field

  1. Add the following code to the schema
// task.js
 
// import mongoose module
const mongoose = require('mongoose');
 
// import uuid library and destructure the v4 function
const { v4: uuidv4 } = require('uuid');
 
// define the task schema
const taskSchema = new mongoose.Schema({
    // use uuidv4 as default value for id field
    id: {
       type: String,
       required: [true, "Please provide an id for the task."],
       trim: true,
       unique: true,
       default: uuidv4
    },
    // rest of the schema definition ...
})
 
// export the task model
module.exports = mongoose.model('Task', taskSchema)