- [Instructor] We will now define the models
for our sequelize in our shopper application.
And spoiler alert, these models will then also be used
to automatically create the tables and relationships
in the database.
So let's head to server, models
and in models, I will now create a new directory sequelize.
And in sequelize, I create a file index.js.
So we could now go ahead and also create
one dedicated file per database table
like we did for mongoose,
but with sequelize that's rather complicated
as order and order item will always be used together,
I decided to just leave them in one file.
So we can just go ahead now and add const,
and then we want to bring in the data types
that sequelize provides, so in curly brackets
I now add DataTypes equals require sequelize.
So this will now
basically fetch the property data types from sequelize.
Then I start with module.export,
so this is what we actually want to export.
And we will export a function
that takes sequelize as an argument.
So later then we have to pass and sequelize in some way,
and we will see how this works soon.
First I start with defining the order,
so at const order equals, now I'm using sequelize.define
then just name the entity it should be order,
and then I add a property object.
And there I have a user ID
and this user ID should be DataType.STRING.
And then know that this mongoose user ID is 24 bytes long.
So I can just add that here,
then I add the email address
and this gets DataTypes.STRING,
and I also want to start the status as we said before
and this is DataTypes.STRING again.
If not edited explicitly sequelize we'll create
this primary key, the ID field for us.
Next, I will define the order items
so at const OrderItem equals sequelize.define
and now the name is OrderItem
and again a pass in the properties here,
so the column names sku,
this is an integer, then the quantity,
it's again an integer, the name of the item, that's a string
and the price of the item that's DataTypes DECIMAL,
so that's the pretty much the same as we had
for the price value before,
for our Bitcoins so it's 10, 2.
So it has two digits after the comma.
Now what's left is we also want to define the relationships
between those two tables.
And for that, I now add Order.hasMany,
so one order has many OrderItem.
And we also have to define the opposite relationship,
so add OrderItem belongsTo, and it belongs to order.
And there, I add a few more options
because with this relationship,
we can define a few triggers,
and in this case onDelete, we want to CASCADE.
This means that my sequel will take care
that all order items belonging to an order, I'll delete it
if an order is deleted.
So that's really handled by the database management system
and not by the programming code.
And we also want to say that foreign key,
so at foreignKey must not be null, so add allowNull false.
This means that each order item
really has to have a relation to the order table.
And then at the very end, I add sequelize.sync.
That this will instruct sequelize to sync them all
that we defined here into the database.
This may now look very familiar to mongoose models for you
but there is a huge difference.
In mongoose basically
all mongoose creates on the database are the indexes
but it does not create a database schema,
it just uses the model information that you have
to delete the stored data
or give you the object to operate on.
With sequelize and mysql really
this information, this structure,
is now written to the database,
so that's really different.
Now to try this out,
we will already implement a part
at least of the order service.
So I head to services, order service
and there I'll start by bringing in these models,
so I'll add const Models equals require
one level up models sequelize.
So this will default that to the index file
in the sequelize folder,
and then I define the order service,
which is a class OrderService in the end,
and let's not forget module.exports equals OrderService
and in there, I just define the constructor for now.
And the constructor takes a sequelize instance
and it will now call Models sequelize.
So this will now run the code that we just wrote,
because as you saw here this module exports a function
and we will execute the function everything what's in there,
if we call it like that,
and then I add this.client equals sequelize
because we need it when we continue implementing
this order service,
and then we also want to store the models that we have
in this models and then get this
on the property of sequelize.models.
Still nothing is happening because somewhere
we have to also use this order service now
so that I head over to the basket page,
so this is in routes, basket index.js,
and there I will now bring in the order service for that
then just copy the basket service line
OrderService equals require
and want to require the order service.
And we can instantiate this order service
for all routes just here after this module experts
interrupt the declaration,
so I add const order equals new OrderService
near passing config.mysql.client.
So now everything should (indistinct) up
of course lead to then
we will use to order service
and already you see, I just saved
and now by default sequelize logs out everything it does
or SQL queries to the console and we see
that it now executed this create table statement
with everything we just declared.
In the next video we will review
the structure in my SQL workbench
and then I will also show you
one really cool feature of this app.