Immersion 2
Day Twelve
3/15/16

Code is like a box of chocolate you never know which is piece is gonna go err. Today is about getting a grasp on working concurrently from the command line on the database and server, with the client code in a text editor. I had no idea it was so simple yet so complex. The deeper I go behind the curtains into how the Internet is made 20% frustration and 80% fascination abounds.

Picking up on my last post about MySQL today we took the next step and exported our databases from the cloud9 ide on to our local machines. Let me show you how that was done for those of you like us on the Git gimmick. If you followed the prior steps correctly you should be ready to open a text editor and get to work. If something went wrong check out this video series for a visual aid. Honestly either way this is a great video on the subject of Sequelize.

So at this point you should recreate the root folder, sub folders and files from the project you pushed to your git repo. Now on the command line cd into that root folder like so, for windows power shell users.

***cd into project root folder***
PS C:\root\folder_name…> cd project_root_folder_name

***initialize a git repo in the project root folder***
PS C:\root\folder_name...\project_root_folder_name> git init

***install the sequelize dependencies***
npm install — save express
npm install — save mysql
npm install — save body-parser
npm install — save sequelize

Now start up another instance of PowerShell and bring up MySQL.

***Start MySQL***
PS C:\root\...> mysql -uroot -p
***then enter your password***
***Show Databases***
mysql> SHOW DATABASES;

***Use db_name Database***
mysql> USE db_name;

Now open up your project_root_folder_name in your text editor i.e. Atom, Sublime, VIM, et. al. Create an app.js or equivalent type file and require sequelize like so:

‘use strict’;
const 
Sequelize = require(‘sequelize’);

You are going to want something that begins to resemble something like this as you move along in mapping your database.

‘use strict’;
const
Sequelize = require(‘sequelize’);
let server;
const
express = require(‘express’),
app = express(),
bodyParser = require(‘body-parser’);
app
.use(function(req, res, next) {
console.log(req.method, req.url);
next();
})
.use(bodyParser.json());
module.exports.close = function() {
console.log(‘shutting down the server…’);
server.close();
};
/* sequelize initialization - replace 'password' with your actual password */
const sequelize = new Sequelize(‘product’, ‘root’, ‘password’, {
host: ‘localhost’,
dialect: ‘mysql’,
pool: {
max: 5,
min: 0,
idle: 10000
},
define: {
timestamps: false,
}
});
const productService = require(“./service/product”)(sequelize);
const colorService = require(“./service/color”)(sequelize);
//sync the model with the database
sequelize.sync().then(function(res) {
app.route(‘/products’)
.get(productService.get)
.post(productService.create)
app.route(‘/products/:id’)
.get(productService.getID)
.post(productService.create)
app.route(‘/colors’)
.get(colorService.get)
.post(colorService.create)
app.route(‘/colors/:id’)
.get(colorService.getID)
.post(colorService.create)
server = app.listen(process.env.PORT || 3000, process.env.IP || “0.0.0.0”, function() {
var addr = server.address();
console.log(“Server listening at”, addr.address + “:” + addr.port);
});
})
.catch(function(e) {
console.log(‘Error in sequelize.sync(): ‘ + e);
});

Now you are beginning to work like a true pro, young grasshopper. Just keep hacking, I mean hopping away…

P.S. You’re sequelizing now fool!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.