You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
4
4
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.
14
6
15
7
```js
16
8
constmongoose=require("mongoose");
@@ -28,7 +20,7 @@ const customer = new mongoose.Schema({
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`.
43
35
44
36
### The Difference between data model and schema
45
37
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.
47
39
48
40
## Introduction to CRUD operations
49
41
50
42
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.
51
43
52
44
### CRUD and REST
53
45
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
+
54
52
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.
55
53
56
54
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.
57
55
58
-
#### Create
56
+
-#### Create
59
57
60
58
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.
61
59
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.
62
60
61
+
Note: Use `POST` when you want to add a child resource under resources collection.
62
+
63
63
Request:
64
64
`POST http://www.myrestaurant.com/api/dishes/`
65
65
66
66
We will need to send the `dish` data too.
67
+
`
67
68
68
-
Body -
69
+
-#### Read
69
70
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.
78
72
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.
86
74
87
75
Request:
88
76
`GET http://www.myrestaurant.com/api/dishes/`
89
77
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
124
79
125
80
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.
127
84
128
85
Request:
129
86
`PUT http://www.myrestaurant.com/dishes/1223`
130
87
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
147
89
148
90
The CRUD operation Delete corresponds to the HTTP method DELETE. It is used to remove a resource from the system.
149
91
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.
150
92
151
93
Request:
152
94
`DELETE http://www.myrestaurant.com/dishes/1223`
153
95
154
-
Response: Status Code - 204 (NO CONTENT)
155
-
156
-
Body - None
157
-
158
96
## Resources
159
97
160
98
-[See this](https://www.educative.io/blog/crud-operations#what) for more information on how these operations are performed on `SQL` database.
Copy file name to clipboardExpand all lines: module3-crud-and-data-models/r1.1-principles-of-setting-up-your-schema/README.md
+5-6Lines changed: 5 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
## Principles of setting up your schema
2
2
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.
4
4
5
5
There are few things to focus on when creating a database schema:
6
6
@@ -10,19 +10,18 @@ There are few things to focus on when creating a database schema:
10
10
11
11
### Type of database
12
12
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
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.
20
19
21
20
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.
22
21
23
22
### Embedding vs. Referencing
24
23
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.
26
25
27
26
#### When to `Embed`:
28
27
@@ -47,4 +46,4 @@ In general, there are few `rules` you can follow to better design your schema:
47
46
48
47
## Resources
49
48
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