Skip to content

Commit 5178fc6

Browse files
committed
General code review fixes
1 parent c3c5f55 commit 5178fc6

File tree

6 files changed

+32
-20
lines changed

6 files changed

+32
-20
lines changed
1.51 MB
Loading

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Intoduction To Database Schemas
1+
## Introduction To Database Schemas
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

@@ -31,21 +31,24 @@ CREATE TABLE customer (
3131

3232
```
3333

34-
We can see that the schema definition for the `customer` has a `name` which is a type `string` and a `zipcode` that is of type `number`.
34+
We can see that the schema definition for the `customer` has a `name` that is type `string` and a `zipcode` that is of type `number`.
3535

3636
## Introduction to CRUD operations
3737

38-
When we are building APIs, we want our models to provide four basic types of functionality. The model must be able to Create, Read, Update, and Delete resources. Computer scientists often refer to these functions by the acronym `CRUD`. A model should have the ability to perform at most these four functions in order to be complete.
38+
When we are building APIs, we want our models to provide four basic types of functionality. The model must be able to Create, Read, Update, and Delete resources. Computer scientists often refer to these functions by the acronym CRUD. A model should have the ability to perform at most these four functions in order to be complete.
3939

4040
### CRUD and REST
4141

42-
### What is REST?
42+
**What is REST?**
4343

44-
REST is a set of architectural constraints. API developers can implement REST in a variety of ways. When a client request is made via a RESTful API, it transfers a representation of the state of the resource to the requester or endpoint. This information, or representation, is delivered in one of several formats, JSON is the most generally popular file format to use.
44+
REST is a set of architectural constraints. API developers can implement REST in a variety of ways. When a client request is made via a RESTful API, it transfers data as an object in some format to the requester or endpoint. There are several formats, JSON is the most generally popular file format to use.
45+
46+
**Resource in REST**
47+
A resource in REST is a like an Object or an Entity in a Database. Once a resource is identified then its representation is to be decided using a standard format so that the server can send the resource in the decided on format and client can understand the same format. A resource can be any information that can be named (e.g. a person, a user, an invoice, a collection of invoices, etc).
4548

4649
### CRUD (CREATE, READ, UPDATE, DELETE)
4750

48-
In a REST environment, CRUD often corresponds to the HTTP methods `POST`, `GET`, `PUT`, and `DELETE`, respectively. These are the fundamental elements of a persistent storage system.
51+
In a REST environment, CRUD often corresponds to the [HTTP request methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods) `POST`, `GET`, `PUT`, and `DELETE`, respectively. These are the fundamental elements of a persistent storage system.
4952

5053
For example, imagine we are working with a system that is keeping track of meals and their corresponding prices for a restaurant. Let’s look at how we would implement CRUD operations.
5154

@@ -60,11 +63,11 @@ Note: Use `POST` when you want to add a child resource under resources collectio
6063

6164
`POST http://www.myrestaurant.com/api/dishes/`
6265

63-
We will need to send the `dish` data too.
66+
We will need to send the dish data too.
6467

6568
#### Read
6669

67-
To read resources in a REST environment, we use the GET method. in practice, reading a resource shouldn't change any information - it should only retrieve it.
70+
To read resources in a REST environment, we use the GET method. In practice, reading a resource shouldn't change any information - it should only retrieve it.
6871

6972
To read resources in a REST environment, we use the GET method. Reading a resource should never change any information - it should only retrieve it. REST itself is more like a set of guidelines. Technically, you can change data in a `GET` request, but since we are creating a RESTful API, you shouldn't do that. Having a GET request that updates data in your database would be confusing to the users of your API and violate the expected behavior of REST.
7073

@@ -86,7 +89,7 @@ Note: Use `PUT` when you want to modify a singular resource which is already a p
8689
#### Delete
8790

8891
The CRUD operation Delete corresponds to the HTTP method DELETE. It is used to remove a resource from the system.
89-
Let’s say that the world avocado shortage has reached a critical point, and we can no longer afford to serve this modern delicacy at all. We should go into the database and delete the item that corresponds to `Avocado Toast`, which we know has an `id` of 1223.
92+
Let’s say that the world avocado shortage has reached a critical point, and we can no longer afford to serve this modern delicacy at all. We should go into the database and delete the item that corresponds to **Avocado Toast**, which we know has an `id` of 1223.
9093

9194
##### Request:
9295

module3-crud-and-data-models/r1.1-principles-of-setting-up-your-schema/README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,39 @@ Without a clean, efficient, consistent database schema, you’ll struggle to mak
88

99
Most developers don't see the difference between relational database schema and MongoDB schema, but in reality, it is not the same
1010

11-
<img src="https://i.imgur.com/3yobiPB.jpg" alt="meme" width="400"/>
11+
<img src="../assets/relationalDBmeme.png" alt="meme" width="400"/>
1212

1313
In a nutshell, in relational databases, models are usually independent of queries, there is no duplication of data as data will _mostly_ be separated into different tables, and it is rigid, you will have to define types and fields for your schema beforehand.
1414

15-
in the other hand, `MongoDB`, you have more flexibility, there are no rules, documents can have new data or fields at any point of time, no need to define types.
15+
In the other hand, `MongoDB`, you have more flexibility, there are no rules, documents can have new data or fields at any point of time, no need to define types.
1616

1717
### Embedding vs. Referencing
1818

1919
One of the key points to establish a good schema design (especially, if you are using MongoDB) is whether to embed or reference your data.
2020

21-
#### When to `Embed`:
21+
#### Embedding
22+
23+
When a collection has a document, and, this document contains another document, another document contains another sub-document, and so on. This is called embedding or nesting.
24+
25+
#### When to Embed
2226

2327
- To retrive all data in a single query.
2428
- Avoid expense JOINs or $lookups.
2529
- Update all data with a single operation.
2630
- In one-to-one or one-to-many relationships between documents.
2731

32+
#### Referencing
33+
34+
When a value of a field in your table or collection referes to a value of another field in another table or collection.
35+
2836
#### When to `Reference`
2937

3038
- If you need smaller documents or tables, your data will be separated across multiple documents or tables.
3139
- No duplicate of data.
3240
- To reduce accessed data not accessed on every query.
3341
- In many-to-many relationships between tables and documents.
3442

35-
In general, there are few `rules` you can follow to better design your schema:
43+
In general, there are few **rules** you can follow to better design your schema:
3644

3745
##### 1. Favor embedding over referencing unless there is a compelling reason not to.
3846

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

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

33
## Introduction to MVC projects
44

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".
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".
66
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
```
@@ -27,7 +27,7 @@ package-lock.json
2727

2828
### Models
2929

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.
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.
3131
The naming convention for the model files is: `name-here-model.js`.
3232

3333
Here’s an example model using JavaScript and Mongoose.
@@ -91,4 +91,4 @@ router.get('/posts', (req, res) => {
9191
## Resources
9292

9393
- [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/)
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module.exports = app;
5656

5757
- #### Create
5858

59-
Create or add a new `food` item to our foods.
59+
Create or add a new food item to our foods.
6060

6161
Example:
6262

@@ -113,4 +113,4 @@ app.delete("/food/:id", async (request, response) => {
113113

114114
## Resources
115115

116-
- [CRUD operations in mongoose](https://mongoosejs.com/docs/models.html#Querying)
116+
- [CRUD operations in Mongoose](https://mongoosejs.com/docs/models.html#Querying)

module3-crud-and-data-models/r4-building-custom-methods-on-mongoose/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## Building Your Own Methods on Mongoose
22

3-
In Mongoose, instances of Models are documents. Documents have many of their own built-in instance methods. We may also define our own custom document instance methods.
3+
In Mongoose, instances of models are documents. Documents have many of their own built-in instance methods (called static methods). We may also define our own custom document instance methods.
4+
We use instance methods to add new custom methods to our model that didn't exist before, that will help us reduce our code duplications if that method is used in multiple places, and, imporove the overall code structure.
45

56
For example:
67

@@ -27,4 +28,4 @@ dog.findSimilarTypes((err, dogs) => {
2728

2829
## Resources
2930

30-
- [instance methods in Mongoose](https://mongoosejs.com/docs/guide.html#methods)
31+
- [Instance methods in Mongoose](https://mongoosejs.com/docs/guide.html#methods)

0 commit comments

Comments
 (0)