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
Copy file name to clipboardExpand all lines: module3-crud-and-data-models/r1-schema-and-data-models/README.md
+12-9Lines changed: 12 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
## Intoduction To Database Schemas
1
+
## Introduction To Database Schemas
2
2
3
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.
4
4
@@ -31,21 +31,24 @@ CREATE TABLE customer (
31
31
32
32
```
33
33
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`.
35
35
36
36
## Introduction to CRUD operations
37
37
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.
39
39
40
40
### CRUD and REST
41
41
42
-
### What is REST?
42
+
**What is REST?**
43
43
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).
45
48
46
49
### CRUD (CREATE, READ, UPDATE, DELETE)
47
50
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.
49
52
50
53
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.
51
54
@@ -60,11 +63,11 @@ Note: Use `POST` when you want to add a child resource under resources collectio
60
63
61
64
`POST http://www.myrestaurant.com/api/dishes/`
62
65
63
-
We will need to send the `dish` data too.
66
+
We will need to send the dish data too.
64
67
65
68
#### Read
66
69
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.
68
71
69
72
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.
70
73
@@ -86,7 +89,7 @@ Note: Use `PUT` when you want to modify a singular resource which is already a p
86
89
#### Delete
87
90
88
91
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.
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.
14
14
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.
16
16
17
17
### Embedding vs. Referencing
18
18
19
19
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.
20
20
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
22
26
23
27
- To retrive all data in a single query.
24
28
- Avoid expense JOINs or $lookups.
25
29
- Update all data with a single operation.
26
30
- In one-to-one or one-to-many relationships between documents.
27
31
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
+
28
36
#### When to `Reference`
29
37
30
38
- If you need smaller documents or tables, your data will be separated across multiple documents or tables.
31
39
- No duplicate of data.
32
40
- To reduce accessed data not accessed on every query.
33
41
- In many-to-many relationships between tables and documents.
34
42
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:
36
44
37
45
##### 1. Favor embedding over referencing unless there is a compelling reason not to.
Copy file name to clipboardExpand all lines: module3-crud-and-data-models/r3-model-view-controller/README.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
## Introduction to MVC projects
4
4
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".
6
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.
7
7
8
8
```
@@ -27,7 +27,7 @@ package-lock.json
27
27
28
28
### Models
29
29
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.
31
31
The naming convention for the model files is: `name-here-model.js`.
32
32
33
33
Here’s an example model using JavaScript and Mongoose.
Copy file name to clipboardExpand all lines: module3-crud-and-data-models/r4-building-custom-methods-on-mongoose/README.md
+3-2Lines changed: 3 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,7 @@
1
1
## Building Your Own Methods on Mongoose
2
2
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.
0 commit comments