Skip to content

Commit 1d781cc

Browse files
committed
fix lesson 1
1 parent 9769a4a commit 1d781cc

File tree

2 files changed

+42
-92
lines changed
  • module3-crud-and-data-models

2 files changed

+42
-92
lines changed
Lines changed: 37 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
## Intoduction to schemas and data modeling
1+
## Intoduction to database schemas
22

3-
### Data modeling
3+
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-
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.
6-
7-
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.
8-
9-
### What are database schemas?
10-
11-
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.
12-
13-
For example, This is a `Customer` schema in mangodb using mongoose.
5+
For example, this is a `customer` schema in MongoDB using mongoose.
146

157
```js
168
const mongoose = require("mongoose");
@@ -28,7 +20,7 @@ const customer = new mongoose.Schema({
2820
module.exports = mongoose.model("Customer", customer);
2921
```
3022

31-
The same `Customer` schema in a MySql database.
23+
The same `customer` schema in a MySql database.
3224

3325
```sql
3426
CREATE TABLE customer (
@@ -39,123 +31,82 @@ CREATE TABLE customer (
3931

4032
```
4133

42-
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` which is a type `string` and a `zipcode` that is of type `number`.
4335

4436
### The Difference between data model and schema
4537

46-
The database schema is one that contains list of attributes and instructions to tell the database engine how data is organised whereas Data model is a collection of conceptional tools for describing data, data-relationship and consistency constraints.
38+
The database schema is one that contains list of attributes and instructions to tell the database engine how data is organised whereas data model is a collection of conceptional tools for describing data, data-relationship and consistency constraints.
4739

4840
## Introduction to CRUD operations
4941

5042
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.
5143

5244
### CRUD and REST
5345

46+
### What is REST?
47+
48+
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.
49+
50+
### CRUD (CREATE, READ, UPDATE, DELETE)
51+
5452
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.
5553

5654
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.
5755

58-
#### Create
56+
- #### Create
5957

6058
To create resources in a REST environment, we most commonly use the HTTP POST method. POST creates a new resource of the specified resource type.
6159
Imagine that we are adding a new food item to the stored list of dishes for this restaurant, and the dish objects are stored in a dishes resource.
6260

61+
Note: Use `POST` when you want to add a child resource under resources collection.
62+
6363
Request:
6464
`POST http://www.myrestaurant.com/api/dishes/`
6565

6666
We will need to send the `dish` data too.
67+
`
6768

68-
Body -
69+
- #### Read
6970

70-
```json
71-
{
72-
"dish": {
73-
"name": "Avocado Toast",
74-
"price": 8
75-
}
76-
}
77-
```
71+
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.
7872

79-
Response: Status Code - 201 (Created)
80-
81-
This creates a new item with a `name` value of `“Avocado Toast”` and a price value of 8. Upon successful creation, the server should return a header with a link to the newly-created resource.
82-
83-
#### Read
84-
85-
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.
73+
Note: technically, you can change data in a `GET` request but since we are creating a RESTful API, you shouldn't do that, and, in-general, changing data in a `GET` request would be confusing to the users of your api.
8674

8775
Request:
8876
`GET http://www.myrestaurant.com/api/dishes/`
8977

90-
Response: Status Code - 200 (OK)
91-
92-
Your response should have a list of `dishes`.
93-
94-
Body -
95-
96-
```json
97-
{
98-
"dishes": [
99-
{
100-
"id": 1,
101-
"name": "Spring Rolls",
102-
"price": 6
103-
},
104-
{
105-
"id": 2,
106-
"name": "Mozzarella Sticks",
107-
"price": 7
108-
},
109-
{
110-
"id": 1223,
111-
"name": "Avocado Toast",
112-
"price": 8
113-
},
114-
{
115-
"id": 1224,
116-
"name": "Muesli and Yogurt",
117-
"price": 5
118-
}
119-
]
120-
}
121-
```
122-
123-
#### Update
78+
- #### Update
12479

12580
PUT is the HTTP method used for the CRUD operation, Update.
126-
So if the price of Avocado Toast has gone up, we should go into the database and update that information
81+
So if the price of Avocado Toast has gone up, we should go into the database and update that information using PUT.
82+
83+
Note: Use `PUT` when you want to modify a singular resource which is already a part of resources collection. `PUT` replaces the resource with the data you send in its entirety.
12784

12885
Request:
12986
`PUT http://www.myrestaurant.com/dishes/1223`
13087

131-
and we send the new data.
132-
133-
Body -
134-
135-
```json
136-
{
137-
"dish": {
138-
"name": "Avocado Toast",
139-
"price": 10
140-
}
141-
}
142-
```
143-
144-
Response: Status Code - 200 (OK)
145-
146-
#### Delete
88+
- #### Delete
14789

14890
The CRUD operation Delete corresponds to the HTTP method DELETE. It is used to remove a resource from the system.
14991
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.
15092

15193
Request:
15294
`DELETE http://www.myrestaurant.com/dishes/1223`
15395

154-
Response: Status Code - 204 (NO CONTENT)
155-
156-
Body - None
157-
15896
## Resources
15997

16098
- [See this](https://www.educative.io/blog/crud-operations#what) for more information on how these operations are performed on `SQL` database.
99+
- [What is REST](https://restfulapi.net/)
161100
- [database schemas](https://www.educative.io/blog/what-are-database-schemas-examples#types)
101+
102+
```
103+
104+
```
105+
106+
```
107+
108+
```
109+
110+
```
111+
112+
```

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Principles of setting up your schema
22

3-
Poorly designed databases can cause many problems, including a waste of resources, difficult maintenance, and faulty performance. That's why having a great database schema design is a crucial part of effective data management.
3+
Poorly designed databases can cause many problems, including wasting resources, making maintenance difficult, and hindering performance. That's why having a great database schema design is a crucial part of effective data management.
44

55
There are few things to focus on when creating a database schema:
66

@@ -10,19 +10,18 @@ There are few things to focus on when creating a database schema:
1010

1111
### Type of database
1212

13-
Most developers don't see the difference between relation database schema and MongoDB schema, but in reality, it is not the same
13+
Most developers don't see the difference between relational database schema and MongoDB schema, but in reality, it is not the same
1414

1515
<img src="https://i.imgur.com/3yobiPB.jpg" alt="meme" width="400"/>
1616

1717
In a nutshell,
18-
19-
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.
18+
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.
2019

2120
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.
2221

2322
### Embedding vs. Referencing
2423

25-
One of the key points to establish a good schema design (especially, if you are using MongoDB) is weither to embed or reference your data, as it can make a big difference in the performance and hardware use.
24+
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.
2625

2726
#### When to `Embed`:
2827

@@ -47,4 +46,4 @@ In general, there are few `rules` you can follow to better design your schema:
4746

4847
## Resources
4948

50-
- [schema design best practices](https://www.youtube.com/watch?v=leNCfU5SYR8)
49+
- [schema design best practices](https://www.mongodb.com/developer/article/mongodb-schema-design-best-practices/)

0 commit comments

Comments
 (0)