- [Instructor] As you know, we store our basket in Redis,
and now I want to transfer these basket items
into an order when a user hits buy now,
and this order should then be stored in MySQL,
this is also a perfect use case for it,
and as you might've guessed,
we will use Sequalize as an abstraction for that.
So let's head over to Visual Studio Code,
and there I am in my shopper directory,
I also have the application running.
I stop it now.
Please make sure that you are also in the shopper directory
and there I now enter npm install sequelize
and also MySQL too,
because Sequelize works with
a lot of different database systems,
so we have to explicitly install
the database driver that we want to use,
and while this is installing
I can already head over to server,
config, index.js,
and again, as we did with the other databases
I will now add a new property, mysql,
and in this MySQL property,
I add a property, options,
and there I will now define some options
that Sequelize needs,
so I first define the host,
that's local host,
then I define the port,
that's 3406.
The database should be shopper eventually.
The dialect, so that's now basically the database backend
that Sequelize will use.
that's mysqlL,
and Sequelize will then discover our database drivers,
so it will look for MySQL or MySQL2,
and will then use MySQL2 that we just installed,
and the username is root
and the password is mypassword.
One word of caution here,
never check in a real password for a real database system
into your version control system.
We are just developing locally
and the database also runs locally.
So this is in this case, no problem.
I will also add one more property,
should look familiar.
That's now client again,
and I set it to null.
Next I head over into bin, start.js
and at the very beginning, I will now require Sequelize,
so I add const Sequelize,
and I'm using uppercase here,
equals require sequelize.
So the package name is lowercase,
but the variable is uppercase,
and now after this connectToRedis function,
we will now add a new function,
connectToMySQL,
and there I will first
instantiate Sequelize by adding const sequelize, lowercase,
equals new, uppercase, Sequelize,
and there I pass in config.MySQL.options.
This connectToMySQL function does again
not have to be asynchronous,
because Sequelize again will lazily create this connection
and we don't have to wait for it.
I still want to know if the connection really works.
So we'll now add sequelize,
and there I called .authenticate
and this will now really make the database connection,
but we still don't have to wait for it,
and authenticate returns a promise,
so I can add then
and in the then block,
we only end up there if the connection worked,
so I can now add console.info,
successfully connected to MySQL,
and if we have a then block,
we of course also need a catch block,
and there I catch the error,
and do a console.error, error,
and as before with Redis,
I will now process exit with the status code one,
because something went wrong,
we don't want to continue,
and in the end, I return sequelize from there.
Now let's scroll down a little bit,
and after this code that handles the Redis connection,
I will now add const mysql,
equals connectToMySQL,
and as we did it before with Redis,
I will now store this object in config.mysql.client,
because as with Redis,
we need this to have
this Sequelize MySQL connection
available to the whole application.
So I set this to mysql,
and now let's see if this works.
So I'll run npm run dev
and I see that I'm getting an error.
It says unknown database shopper.
So with Redis and Mongo DB,
this was never a problem
because the database was created on the fly for us,
but not so with MySQL.
Here, we really have to create the database now,
and for that, I head over to MySQLWorkbench
and there I will now just create the database.
So I click right here on the left menu
and click on Create Schema, and I name the schema shopper,
and again, I can leave everything at default
and I hit apply,
and now we have our database shopper.
Now let's head back into Visual Studio Code again,
and let's try this out one more time.
So restart the application,
npm run dev,
and now we see that we successfully connected to MySQL.