Create Schema & Model using Mongoose & Express JS
Create Schema & Model (Collection & Tables) using Mongoose & Express JS
▶️ Create a Model
- Go to the root directory of your project
- Create a folder named
models
using the commandmkdir models
and thencd models
- Create a file named
task.js
using the commandecho > task.js
- Start the server again using the command
npm run dev
- Open the file
task.js
in your editor
▶️ Mongoose Call
- Import mongoose using the command
const mongoose = require('mongoose')
▶️ Create Schema
-
Create a schema using the command
const taskSchema = new mongoose.Schema({})
-
Add the following fields to the schema
title
- Stringdescription
- Stringcompleted
- Boolean
-
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
- Install the
uuid
package using the commandnpm i uuid
- Import the
uuid
package using the commandconst \{ v4: uuidv4 } = require('uuid')
▶️ Add Id Field
- 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)