Skip to content

Commit fca0b0e

Browse files
committed
redefine/add few stuff
1 parent 0705712 commit fca0b0e

File tree

3 files changed

+19
-33
lines changed
  • module3-crud-and-data-models

3 files changed

+19
-33
lines changed

module3-crud-and-data-models/r1-schema-and-data-models/README.md

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,7 @@
22

33
A database schema is a blueprint or architecture of how our data will look. It doesn’t hold data itself, but instead describes the shape of the data and how it might relate to other tables or models.
44

5-
<<<<<<< HEAD
65
For example, this is a `customer` schema in MongoDB using mongoose.
7-
=======
8-
A database model is a type of data model that determines the logical structure of a database. It is the high level design which defines the kind of tables, the `fields` in those tables and the `relations` between different tables.
9-
10-
The most popular example of a database model is the relational model, which uses a table-based format, which is the one we use in most SQL databases.
11-
12-
### What are database schemas?
13-
14-
A database schema is a `blueprint` or `architecture` of how our data will look. It doesn’t hold data itself, but instead describes the shape of the data and how it might relate to other tables or models.
15-
16-
For example, This is a `Customer` schema in mongoDB using mongoose.
17-
18-
> > > > > > > c7eff66f097a2a47f25f1ab1b06941e77c329eb9
196

207
```js
218
const mongoose = require("mongoose");
@@ -111,15 +98,3 @@ Request:
11198
- [What is REST](https://restfulapi.net/)
11299
- [See this](https://www.educative.io/blog/crud-operations#what) for more information on how CRUD operations are performed directly on `SQL` database.
113100
- [database schemas](https://www.educative.io/blog/what-are-database-schemas-examples#types)
114-
115-
```
116-
117-
```
118-
119-
```
120-
121-
```
122-
123-
```
124-
125-
```

module3-crud-and-data-models/r3-model-view-controller/README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
## Introduction to MVC projects
44

5-
When beginning a new project, there are several different methods that can be used to set up the overall file structure and flow. One of the most commonly used architectural patterns is called MVC. This is an acronym for `Model`, `View`, `Controller`.
6-
This pattern is favored because of its alignment with the computer science design principle, Separation of Concerns. By dividing up responsibilities within our file structure, we can encapsulate information to be referred to via abstraction and maintain cleaner codebases.
5+
When beginning a new project (especially, when using express), there are several different methods that can be used to set up the overall file structure and flow. One of the most commonly used architectural patterns is called MVC. this acronym stands for "Model, View, Controller".
6+
This pattern is favored because of its alignment with the computer science design principle, [**separation of concerns**](https://en.wikipedia.org/wiki/Separation_of_concerns). By dividing up responsibilities within our file structure, for example, we can have our db connection work in one file and api routes in another file, ...etc.
77

88
```
99
app-name-here
@@ -17,9 +17,17 @@ package.json
1717
package-lock.json
1818
```
1919

20+
### Components Of MVC
21+
22+
**Model:** This consists of the structure of our data and handle the database.
23+
24+
**View:** The part of our application which is shown to the user.
25+
26+
**Controller:** Controller has all the logic to control and respond to the action, the user performs using the views.
27+
2028
### Models
2129

22-
In the models folder, the files will contain our schema definition and expected fields for the model.
30+
In this folder, you can write the functionality & logics related to the Database (if you aren't using ORM) like insert, fetch, update, delete queries. It takes the query request from the controller & sends the response back to the controller.
2331
The naming convention for the model files is: `name-here-model.js`.
2432

2533
Here’s an example model using JavaScript and mongoose.
@@ -53,6 +61,7 @@ module.exports = mongoose.model("Post", post);
5361
### Controllers
5462

5563
Controllers are the layer between the Model and the View. the views can use the controllers to `add`, `read`, `delete`, ...etc data.
64+
In controllers, you can write the functionality & logic to develop dynamic web applications. It takes the data request from the views & sends it to the model and sends the response back to the views.
5665
The naming convention for the model files is: `name-here-controller.js`.
5766

5867
Here is an example:
@@ -76,8 +85,10 @@ router.get('/posts', (req, res) => {
7685

7786
### Views
7887

79-
In a back-end application, views are usually not implemented and rather we create a front-end app using maybe `React` to call our `api` end-points to manipulate the data in the back-end.
88+
In this folder, you can write HTML code for displaying a web page on the web browser. You can send the data from the controller to view for displaying data dynamically.
89+
In a back-end application, views are usually not implemented and rather we create a front-end app using maybe `React` to call our `api` end-points to manipulate and dispaly the data in the back-end.
8090

8191
## Resources
8292

8393
- [MVC Architecture with NodeJS and mongoose](https://medium.com/geekculture/mvc-architecture-with-express-server-e35aedfe7889)
94+
- [express MVC structure](https://codingstatus.com/express-mvc-structure/)

module3-crud-and-data-models/r3.2-models-and-controllers-in-mongoose/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = Food;
3131
And we are required to do the CRUD operation on this model, how can we acheive that with mongoose?
3232
we will first create a `food-conroller` for all the food requests and place all requests in there.
3333

34-
#### Read
34+
- #### Read
3535

3636
To get all the foods from our MongoDB using mongoose we can simply use the `find({})` function.
3737

@@ -54,7 +54,7 @@ app.get("/foods", async (request, response) => {
5454
module.exports = app;
5555
```
5656

57-
#### Create
57+
- #### Create
5858

5959
Create or add a new `food` item to our foods.
6060

@@ -72,7 +72,7 @@ app.post("/food", async (request, response) => {
7272
// ...
7373
```
7474

75-
#### Update
75+
- #### Update
7676

7777
To update an item, we need to first make sure it exist by using the id to find it and then updating it, using the`findByIdAndUpdate()`, and then save the new item in the database.
7878

@@ -91,7 +91,7 @@ app.patch("/food/:id", async (request, response) => {
9191
// ...
9292
```
9393

94-
#### Delete
94+
- #### Delete
9595

9696
To delete, we need to check if the item exist by using the id to find it and then delete it, we us `findByIdAndDelete()`.
9797

0 commit comments

Comments
 (0)