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
2. Executing queries on a MySQL database using Sequelize
5
7
6
-
## Getting Started
8
+
## Docker Version Instructions
9
+
10
+
This lab is Dockerized, so it has all the packages and dependencies already installed and containerized, when you compose it, it will come with all the packages installed and configured inside.
11
+
12
+
Imagine it like a small virtual machine, everything is set and ready, you just need to write code for the solutions and starting the database is slightly different. We do this for you students to not encounter possible errors related to your development environment.
13
+
14
+
You will find additional instructions after this section so you can also learn how to install the dependencies and start your database in a non-dockerized environment.
15
+
16
+
Follow these instructions to get the app ready:
17
+
18
+
1. Start your Docker app on your desktop.
19
+
2. Run the command `npm run start` to build docker images. It might take some time, what this command does is it installs all the needed and pre-configured packages like `mysql2` and `sequelize` and starts and creates a database named `practice` that you otherwise would need to install/configure yourself. You can check your Docker app after it is done and you would see that Docker created 2 images and connected (_bound them together_) them after it's done you can run `npm run verify` to make sure that the container has been built correctly.
20
+
21
+
Ta-da, we are ready to work on the assignment now.
22
+
23
+
### Sequelize Database Connection
24
+
25
+
First, let's create a connection to our practice database and test it out.
26
+
27
+
In the assignment folder, you will find a file called `connect.js`. Enter these lines of code here.
28
+
29
+
```js
30
+
constSequelize=require("sequelize");
31
+
32
+
constpath="mysql://root:secret@db:3306/practice";
33
+
constsequelize=newSequelize(path);
34
+
35
+
sequelize
36
+
.authenticate()
37
+
.then(() => {
38
+
console.log("Connection established successfully.");
39
+
})
40
+
.catch((err) => {
41
+
console.error("Unable to connect to the database: ", err);
42
+
})
43
+
.finally(() => {
44
+
sequelize.close();
45
+
});
46
+
```
47
+
48
+
**Note:** If you'd check the `compose.yaml` file, you can see that `root` is the name of our user, `secret` is the name of our password, `3306` is our port and `practice` is the name of our database. Unlike the normal setup, we don't need to change anything here.
49
+
50
+
Now let's run this file: `npm run execute connect.js`. You should see the output as :
51
+
52
+
```
53
+
[+] Building 0.0s (0/0)
54
+
docker:default
55
+
[+] Building 0.0s (0/0)
56
+
docker:default
57
+
Executing (default): SELECT 1+1 AS result
58
+
Connection established successfully.
59
+
```
60
+
61
+
From this piece of code, you can tell the following:
62
+
63
+
1. Sequelize requires a MySQL connection path that contains the username, password, hostname, database port and database name.
64
+
2. The authenticate method tests the connection by trying to authenticate to the database. We show a message when the connection is established OK.
65
+
3. In case of an error, we show an error message.
66
+
4. In the end, we close the database connection.
67
+
68
+
### Sequelize Model Definition
69
+
70
+
A Model represents a table in the database. Let's create a model representing a table called todo for a todo list application.
71
+
72
+
In the assignment folder, you will find a file called `model.js`. Enter these lines of code here.
73
+
74
+
```js
75
+
constSequelize=require("sequelize");
76
+
77
+
constpath="mysql://root:secret@db:3306/practice";
78
+
constsequelize=newSequelize(path);
79
+
80
+
// The table will have a column called description of type string
81
+
constTodo=sequelize.define("todo", {
82
+
description:Sequelize.STRING,
83
+
});
84
+
85
+
Todo.sync()
86
+
.then(() => {
87
+
console.log('New table "todo" created');
88
+
})
89
+
.catch((err) => {
90
+
console.error("Unable to create table: ", err);
91
+
})
92
+
.finally(() => {
93
+
sequelize.close();
94
+
});
95
+
```
96
+
97
+
Once again, run `npm run execute model.js`. You should see the query being executed in the output and the todo table will be created.
98
+
99
+
## Practice Time
100
+
101
+
Now that we have written some code utilizing Sequelize, let's explore some more of what can be achieved through this ORM by performing some more operations on our Todo table.
102
+
103
+
In this assignment folder, you will find the following files:
104
+
105
+
```
106
+
insert_todo.js
107
+
find_by_id.js
108
+
multi_insert_todo.js
109
+
find_one.js
110
+
count_rows.js
111
+
update_row.js
112
+
delete_row.js
113
+
order_by.js
114
+
```
115
+
116
+
In each file, you will find a comment describing an SQL operation to be done using Sequelize. You must write the required code for each operation in these files one by one.
117
+
118
+
Note: You will have to add the SQL connection and model definition on each file since we will be running each file independently. Example: `npm run execute insert_todo.js`
119
+
120
+
Refer to the [Sequelize documentation](https://sequelize.org/) for support.
121
+
122
+
## Note
123
+
124
+
Run `npm run stop` to stop docker and `npm run reset` in case anything goes wrong and you need to start from scratch
125
+
126
+
127
+
## Submission
128
+
129
+
Once you're ready to submit the assignment, follow these steps on your terminal:
130
+
131
+
1. Stage your changes to all the JS files to be committed: `git add .`
132
+
2. Commit your final changes: `git commit -m "solve assignment"`
133
+
3. Push your commit to the main branch of your assignment repo: `git push origin main`
134
+
135
+
After your changes are pushed, return to this assignment on Canvas for the final step of submission.
136
+
137
+
## Conclusion
138
+
139
+
So far we know how to directly execute queries on a MySQL database. This assignment gave us the chance to work with a MySQL database using Node.js. Of course, we will not be writing separate files for each operation in our actual projects. When building a CRUD API, we will simply execute the Sequelize functions within our API endpoint handlers depending on the required operation. All of this coming soon in Module 3, so stay tuned!
140
+
141
+
## Non-Docker Version Instructions
142
+
7
143
Follow these installation instructions to install MySQL on your computer.
144
+
8
145
1.[Install MySQL on WSL or Linux](https://docs.microsoft.com/en-us/windows/wsl/tutorials/wsl-database#install-mysql)
9
146
2.[Install MySQL on MacOS](https://flaviocopes.com/mysql-how-to-install/)
10
147
@@ -23,73 +160,89 @@ Let's create a database for practice. Enter the query: `create database practice
23
160
Once our practice database is created, we can exit the prompt: `exit`
24
161
25
162
### Sequelize Setup
163
+
26
164
Sequelize is available via npm. Navigate to your cloned assignment folder and run the command: `npm install sequelize`
27
165
28
166
To use sequelize on a Node.js project, we will also have to install the database driver, so run the command: `npm install mysql2`
29
167
30
-
### Sequelize database connection
168
+
### Sequelize Database Connection
169
+
31
170
First, let's create a connection to our practice database and test it out.
32
171
33
172
In the assignment folder, you will find a file called `connect.js`. Enter these lines of code here.
// The table will have a column called description of type string
74
-
constTodo=sequelize.define('todo', {
75
-
description:Sequelize.STRING
222
+
constTodo=sequelize.define("todo", {
223
+
description:Sequelize.STRING,
76
224
});
77
225
78
226
Todo.sync()
79
-
.then(() => {
80
-
console.log('New table "todo" created');
81
-
}).catch(err=> {
82
-
console.error('Unable to create table: ', err);
83
-
}).finally(() => {
84
-
sequelize.close();
85
-
});
227
+
.then(() => {
228
+
console.log('New table "todo" created');
229
+
})
230
+
.catch((err) => {
231
+
console.error("Unable to create table: ", err);
232
+
})
233
+
.finally(() => {
234
+
sequelize.close();
235
+
});
86
236
```
87
-
Once again add your password in the connection path and then run `node model.js`. You should see the query being executed in the output and the todo table will be created. You can even verfy this on the MySQL Prompt.
237
+
238
+
Once again add your password in the connection path and then run `node model.js`. You should see the query being executed in the output and the todo table will be created. You can even verify this on the MySQL Prompt.
88
239
89
240
## Practice Time
90
-
Now that we have written some code utilizing Sequelize, let's explore some more of what can be acheived through this ORM by performing some more operations on our Todo table.
241
+
242
+
Now that we have written some code utilizing Sequelize, let's explore some more of what can be achieved through this ORM by performing some more operations on our Todo table.
91
243
92
244
In this assignment folder, you will find the following files:
245
+
93
246
```
94
247
insert_todo.js
95
248
find_by_id.js
@@ -100,19 +253,23 @@ update_row.js
100
253
delete_row.js
101
254
order_by.js
102
255
```
256
+
103
257
In each file, you will find a comment describing an SQL operation to be done using Sequelize. You must write the required code for each operation in these files one by one.
104
258
105
259
Note: You will have to add the SQL connection and model definition on each file, since we will be running each file independently.
106
260
107
261
Refer to the [Sequelize documentation](https://sequelize.org/) for support.
108
262
109
263
## Submission
264
+
110
265
Once you're ready to submit the assignment, follow these steps on your terminal:
266
+
111
267
1. Stage your changes to all the JS files to be committed: `git add .`
112
268
2. Commit your final changes: `git commit -m "solve assignment"`
113
269
3. Push your commit to the main branch of your assignment repo: `git push origin main`
114
270
115
271
After your changes are pushed, return to this assignment on Canvas for the final step of submission.
116
272
117
273
## Conclusion
118
-
So far we knew how to directly execute queries on a MySQL database. This assignment gave us the chance to work with a MySQL database using Node.js. Of course we will not be writing separate files for each operation in our actual projects. When building a CRUD API, we will simply execute the Sequelize functions within our API endpoint handlers depending on the required operation. All of this coming soon in Module 3, so stay tuned!
274
+
275
+
So far we know how to directly execute queries on a MySQL database. This assignment gave us the chance to work with a MySQL database using Node.js. Of course, we will not be writing separate files for each operation in our actual projects. When building a CRUD API, we will simply execute the Sequelize functions within our API endpoint handlers depending on the required operation. All of this coming soon in Module 3, so stay tuned!
0 commit comments