- [Instructor] To use MySQL in our sample project,
we will now use Sequelize.
Sequelize is a popular database abstraction layer
for Node.js, and it's not exclusive for MySQL.
It supports MySQL, but also SQLite, PostgreSQL,
and Microsoft SQL Server.
Additionally to being a database abstraction layer,
it's also an object relational mapper, an ORM,
very similar to Mongoose,
which is an object document mapper.
So as we did it with Mongoose
we could not do something like that.
I could define a car and a property, make on it,
and I can tell Sequelize that this should be a string.
And then I call sequelize.sync
in a special line different from Mongoose
because this will synchronize the database model
here with the database.
So basically, it really creates the static table
for us in MySQL.
Then I call Car.create and pass in the make,
and it's Ford in my case,
and this, again, returns a promise
and in the then block I can then, for instance,
log out the car that I just stored into the database.
You see, that's very similar
to what we are doing with Mongoose.
It maps objects in Node.js to entities in our database.
Now let's look at something Mongoose doesn't provide
because it doesn't need it, it's transactions.
So how can transactions be created with Sequelize?
For that, I call sequelize.transaction
and here in the callback I set the owner of this car
so there is some relation between the car and its owner.
Doing that in a transaction means now
that if something goes wrong along the way,
nothing will happen and the car will not be created at all.
As I said before,
with transactions it's always all or nothing.
Similarly to Mongoose,
Sequelize also has a very complete documentation
in the Getting Started section
and I recommend you to just have a quick look
before you proceed.