- Logistics
- Introduction to Docker
- What is Docker?
- Installation
- Setting up MySQL
- Sequelize
Location: Covel 227
Time: 6:15 - 8:15
Teachers:
- Shannon Phu
Slides:
E-Library Demo: How to Apply Sequelize
Attendance Code: #SaveSoundcloud
Course Overview Table of Contents:
Docker is a tool that allows developers, sys-admins etc. to easily deploy their applications in a sandbox (called containers) to run on the host operating system, i.e. Linux.
The key benefit of Docker is that it allows users to package an application with all of its dependencies into a standardized unit for software development.
Unlike virtual machines, containers do not have the high overhead and hence enable more efficient usage of the underlying system and resources.
The industry standard today is to use Virtual Machines (VMs) to run software applications. A VM simulates an operating system; it's kind of like a computer running as software inside another computer.
VMs are great at providing full process isolation for applications: there are very few ways a problem in the host operating system can affect the software running in the guest operating system, and vice-versa. But this isolation comes at great cost — the computational overhead spent virtualizing hardware for a guest OS to use is substantial.
Containers take a different approach: by leveraging the low-level mechanics of the host operating system, containers provide most of the isolation of virtual machines at a fraction of the computing power.
On Mac:
Click here and follow the instructions to download Docker.
On Windows:
Click here and follow the instructions to download Docker.
Run Docker, then use your Terminal/Command Prompt to see if everything's set up.
On both Linux/Mac and Windows:
$ docker version
This should output:
Client:
Version: 17.09.0-ce
API version: 1.32
Go version: go1.8.3
Git commit: afdb6d4
Built: Tue Sep 26 22:40:09 2017
OS/Arch: darwin/amd64
Server:
Version: 17.09.0-ce
API version: 1.32 (minimum version 1.12)
Go version: go1.8.3
Git commit: afdb6d4
Built: Tue Sep 26 22:45:38 2017
OS/Arch: linux/amd64
Experimental: true
All set!
We'll be using Docker to run MySQL.
- Open up Terminal/Command Prompt
- Run the following command which installs and starts running your database:
You will see a long hash output, for example
docker run --name bruinplay-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:8.0
bc6635833ed86be942067e0641df0bd3125447613e38c0b5cece9991f671c3ff
. The content of this text doesn't matter. - Type
docker ps
. Your Docker container should be running MySQL and you should see something like:
- To stop the database from running, type
docker stop bruinplay-mysql
- To restart a currently existing container (ie you have't removed it), type
docker start bruinplay-mysql
- To remove the container and uninstall MySQL in that container type
docker rm bruinplay-mysql
What is Sequelize?
- It is a library that can work with any relational database and abstracts and handles all the raw SQL queries that you'd traditionally made. It makes your life easier working with databases!
In your node.js project directory, do the following steps:
- Make sure you are running MySQL in Docker either by starting it for the first time with
or restarting a stopped instance with
docker run --name bruinplay-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:8.0
docker start bruinplay-mysql
- Run
npm install mysql2 --save
- Run
npm install sequelize --save
- In your
server.js
or app entry point include sequelizeconst Sequelize = require('sequelize');
- In
server.js
configure your node app to work with the currently running database connectionconst sequelize = new Sequelize('mysql', 'root', "my-secret-pw", { host: 'localhost', dialect: 'mysql' });
- In
server.js
make the actual connection from your app to the databasesequelize .authenticate() .then(function() { console.log('Connection has been established successfully.'); }) .catch(function(err) { console.error('Unable to connect to the database:', err); });
- Start using Sequelize in
server.js
to create, read, update, or delete data in your MySQL database!
- You can put the following code in server.js.
We start off by first creating a database table:
var Orders = sequelize.define('orders', {
order_id: {type: Sequelize.INTEGER},
customer_id: {type: Sequelize.INTEGER},
order_date: {type: Sequelize.DATE},
amount: {type: Sequelize.DOUBLE},
});
Here we are making a table named orders
internally, that we'll refer to as Orders
in our node app. It has 4 columns, order_id
which contains integers, customer_id
which contains integers, order_date
which contains a Date type, and amount
containing doubles.
Then we can start using the empty table we just made!
- C: Create
- R: Read
- U: Update
- D: Destroy
Orders.create({
order_id: 558,
customer_id: 101,
order_date: new Date(),
amount: 12.00
}).then(function(newOrder) {
console.log("Inserted new order with id 558" + newOrder);
});
Orders.findAll({
where: {
customer_id: 101
}
}).then(function(orders) {
console.log("Found all orders where customer_id is 101: " + orders);
});
Orders.findAll({
where: {
order_id: 555
},
limit: 1
}).then(function(singleOrder) {
return singleOrder.update({
amount: 100
});
}).then(function(order) {
console.log("Order #555's price is now $100: " + orders);
});
Order.findAll({
where: {
customer_id: 101
}
}).then(function(orders) {
orders.destroy();
});