An architectural pattern that splits applications into sections each of which have a unique purpose.
Models determine how data is structured in a database.
Views are what the user sees and can interact with
Controllers listen for requests, interact with the models, request appropriate views and respond with those views to the client.
In web apps, we add some extra pieces.
Routers listen for user requests and send them to the appropriate controller.
The database stores data our models define. They interact with the models.
What does it all look like?
Taking the previous slides and combining them into the image before, we see
const TodoSchema = new mongoose.Schema({
todo: {
type: String,
required: true,
},
completed: {
type: Boolean,
required: true,
},
date: {
type: Date,
required: false,
min: new Date(),
}
})
Mongoose is an Object Data Modeling library which acts as a wrapper over MongoClient that makes working with mongoDB more straightforward by provide schema-based models and validation for programs to work with. It transforms data objects in code into a suitable form for storage as documents in MongoDB.
It provides the model component of the MVC in our MERN app.
Inside the project Root
The MVC architecture allows developers to quickly locate pertinent sections of code, swap out different implementations of components with minimal impact on other parts of the code.
For example, suppose you want to use a relational database instead of a non-relational one. Just change the implementation of the model to use the new relational database and everything else still works.
MVC is scalable, speeds up development through coherent organization, and supports multiple methods of displaying or interacting with the app.