Skip to content

Commit e2d9c77

Browse files
authored
Merge pull request #1 from ReCoded-Org/dockerizing
Feat: Dockerized
2 parents b88ca05 + ce19655 commit e2d9c77

17 files changed

+600
-46
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
npm-debug.log

Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# syntax=docker/dockerfile:1
2+
3+
FROM node:18-alpine
4+
WORKDIR /app
5+
COPY . .
6+
RUN npm install
7+
EXPOSE 3000
8+
COPY entry.sh ./entry.sh
9+
RUN chmod +x ./entry.sh
10+
CMD ["./entry.sh"]

README.md

Lines changed: 185 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,147 @@
11
# Sequelize Practice
2+
23
The objectives of this assignment are:
4+
35
1. Understanding the use of an ORM
46
2. Executing queries on a MySQL database using Sequelize
57

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+
const Sequelize = require("sequelize");
31+
32+
const path = "mysql://root:secret@db:3306/practice";
33+
const sequelize = new Sequelize(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+
const Sequelize = require("sequelize");
76+
77+
const path = "mysql://root:secret@db:3306/practice";
78+
const sequelize = new Sequelize(path);
79+
80+
// The table will have a column called description of type string
81+
const Todo = 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+
7143
Follow these installation instructions to install MySQL on your computer.
144+
8145
1. [Install MySQL on WSL or Linux](https://docs.microsoft.com/en-us/windows/wsl/tutorials/wsl-database#install-mysql)
9146
2. [Install MySQL on MacOS](https://flaviocopes.com/mysql-how-to-install/)
10147

@@ -23,73 +160,89 @@ Let's create a database for practice. Enter the query: `create database practice
23160
Once our practice database is created, we can exit the prompt: `exit`
24161

25162
### Sequelize Setup
163+
26164
Sequelize is available via npm. Navigate to your cloned assignment folder and run the command: `npm install sequelize`
27165

28166
To use sequelize on a Node.js project, we will also have to install the database driver, so run the command: `npm install mysql2`
29167

30-
### Sequelize database connection
168+
### Sequelize Database Connection
169+
31170
First, let's create a connection to our practice database and test it out.
32171

33172
In the assignment folder, you will find a file called `connect.js`. Enter these lines of code here.
173+
34174
```js
35-
const Sequelize = require('sequelize');
175+
const Sequelize = require("sequelize");
36176

37-
const path = 'mysql://root:yourpassword@localhost:3306/practice';
177+
const path = "mysql://root:yourpassword@localhost:3306/practice";
38178
const sequelize = new Sequelize(path);
39179

40-
sequelize.authenticate()
41-
.then(() => {
42-
console.log('Connection established successfully.');
43-
}).catch(err => {
44-
console.error('Unable to connect to the database: ', err);
45-
}).finally(() => {
46-
sequelize.close();
47-
});
180+
sequelize
181+
.authenticate()
182+
.then(() => {
183+
console.log("Connection established successfully.");
184+
})
185+
.catch((err) => {
186+
console.error("Unable to connect to the database: ", err);
187+
})
188+
.finally(() => {
189+
sequelize.close();
190+
});
48191
```
192+
49193
**Note**: Add your MySQL password in the path for the database connection, by replacing `yourpassword`.
50194

51195
Now, let's run this file: `node connect.js`. You should see the output as:
196+
52197
```
53198
Executing (default): SELECT 1+1 AS result
54199
Connection established successfully.
55200
```
56201

57202
From this piece of code, you can tell the following:
58-
1. Sequelize requires a MySQL connection path which contains the username, password, the host name, database port and database name.
59-
2. The authenticate method tests the connection by trying to authenticate to the database. We show a message when the connection was established OK.
203+
204+
1. Sequelize requires a MySQL connection path that contains the username, password, hostname, database port and database name.
205+
2. The authenticate method tests the connection by trying to authenticate to the database. We show a message when the connection is established OK.
60206
3. In case of an error, we show an error message.
61207
4. In the end, we close the database connection.
62208

63209
### Sequelize model definition
210+
64211
A Model represents a table in the database. Let's create a model representing a table called `todo` for a todo list application.
65212

66213
In the assignment folder, you will find a file called `model.js`. Enter these lines of code here.
214+
67215
```js
68-
const Sequelize = require('sequelize');
216+
const Sequelize = require("sequelize");
69217

70-
const path = 'mysql://root:yourpassword@localhost:3306/practice';
218+
const path = "mysql://root:yourpassword@localhost:3306/practice";
71219
const sequelize = new Sequelize(path);
72220

73221
// The table will have a column called description of type string
74-
const Todo = sequelize.define('todo', {
75-
description: Sequelize.STRING
222+
const Todo = sequelize.define("todo", {
223+
description: Sequelize.STRING,
76224
});
77225

78226
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+
});
86236
```
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.
88239

89240
## 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.
91243

92244
In this assignment folder, you will find the following files:
245+
93246
```
94247
insert_todo.js
95248
find_by_id.js
@@ -100,19 +253,23 @@ update_row.js
100253
delete_row.js
101254
order_by.js
102255
```
256+
103257
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.
104258

105259
Note: You will have to add the SQL connection and model definition on each file, since we will be running each file independently.
106260

107261
Refer to the [Sequelize documentation](https://sequelize.org/) for support.
108262

109263
## Submission
264+
110265
Once you're ready to submit the assignment, follow these steps on your terminal:
266+
111267
1. Stage your changes to all the JS files to be committed: `git add .`
112268
2. Commit your final changes: `git commit -m "solve assignment"`
113269
3. Push your commit to the main branch of your assignment repo: `git push origin main`
114270

115271
After your changes are pushed, return to this assignment on Canvas for the final step of submission.
116272

117273
## 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!

compose.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
services:
2+
app:
3+
build:
4+
context: .
5+
dockerfile: Dockerfile
6+
entrypoint: ["./entry.sh"]
7+
ports:
8+
- "3000:3000"
9+
environment:
10+
MYSQL_HOST: mysql
11+
MYSQL_USER: root
12+
MYSQL_PASSWORD: secret
13+
MYSQL_DB: practice
14+
db:
15+
image: mysql:latest
16+
volumes:
17+
- mysql_data:/var/lib/mysql
18+
environment:
19+
MYSQL_ROOT_PASSWORD: secret
20+
MYSQL_DATABASE: practice
21+
ports:
22+
- "3307:3306"
23+
volumes:
24+
mysql_data:

connect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
// Enter your code below
1+
// Enter your code below

count_rows.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// Use Sequelize to count the number of rows in the todo table
2-
// Enter your code below
2+
// Enter your code below

delete_row.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// Use Sequelize to delete a row in the todo table
22
// For example, delete the todo with description: Watch Netlfix
3-
// Enter your code below
3+
// Enter your code below

entry.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
3+
# Check if a script name is provided as an argument
4+
if [ -n "$1" ]; then
5+
node "$1"
6+
else
7+
echo "No script specified."
8+
fi

find_by_id.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// Use Sequelize to find a row in the todo table by ID
2-
// Enter your code below
2+
// Enter your code below

find_one.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// Use Sequelize to find the first row where description includes the word 'Visit'
22
// Try to use async/await instead of promises this time
3-
// Enter your code below
3+
// Enter your code below

insert_todo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// Use Sequelize to insert a new row in the todo table
2-
// Enter your code below
2+
// Enter your code below

model.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
// Enter your code below
1+
// Enter your code below

multi_insert_todo.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
// Insert the values from given array below
33

44
const todosToInsert = [
5-
{ description: 'Tai chi in the morning' },
6-
{ description: 'Visit friend' },
7-
{ description: 'Visit grocery store' },
8-
{ description: 'Listen to music' },
9-
{ description: 'Watch Netlfix' },
10-
{ description: 'Walk for an hour' }
5+
{ description: "Tai chi in the morning" },
6+
{ description: "Visit friend" },
7+
{ description: "Visit grocery store" },
8+
{ description: "Listen to music" },
9+
{ description: "Watch Netlfix" },
10+
{ description: "Walk for an hour" },
1111
];
1212

13-
// Enter your code below
13+
// Enter your code below

0 commit comments

Comments
 (0)