How to build a REST API with Express and SequelizePéter KellerBlockedUnblockFollowFollowingMay 13When I started learning Node I made the same mistake as most bootcamp graduates these days.
I went straight for NoSQL databases without even considering SQL as an option and I had no idea what I had missed out on until when I was recently exposed to Ruby on Rails and SQL.
I learned the difference between the two type of databases and managed to love both of them, equally.
Because of my newly found knowledge I decided to write a walkthrough on how to build a REST API using Express and Sequelize.
Photo by Caspar Camille RubinLet’s start by installing the npm packages that we will use:npm initnpm install express sequelize sqlite3 body-parser –savenpm install sequelize-cli -gsequelize initThe command “sequelize init” will create the backbone of our database architecture and will allow us to easily connect to our database to insert/retrieve/delete records.
Once that command is completed you will see a couple of extra folders and files in your app and your folder structure should look something like this:|- express-sequelize|– config|— config.
json|– migrations|– models|— index.
js|– node_modules|– seeders|– package.
jsonThe planned database models and the relationships between them will look something like this:For this tutorial I will use SQLite3 for the sake of simplicity; in order to use that with Sequelize we have to go into our config/config.
json and replace the content of the file with the following:Now that our environment is all set up, it’s time to create the models for our database.
Sequelize-CLI allows us to create models on the fly with no effort; we simply have to write the name of the model and the name of the columns in the database that will belong to the current model and the type of data that we will store.
Just like this:sequelize model:create –name Physician –attributes name:stringsequelize model:create –name Patient –attributes name:stringsequelize model:create –name Appointment –attributes physicianId:integer,patientId:integerAfter we’re done with this, you’ll see that we have 3 new files inside our models folder.
All we have to do now is to commit these changes to the database by migrating the changes with the following command:sequelize db:migrateThis means that our models are created in the database (but, we still have to create the associations between them).
We’ll have 2 main models: Physician and Patient.
They will be connected by a join table: Appointment.
A Physician can have many Patients and vice versa through an Appointment.
Let’s start with the Physician.
All we have to do is define the association inside the “associate’’ function.
The way we do this is on line 11.
We select the current model and use the belongsToMany() function that will receive the model we want to connect it to and the optional arguments.
In this case we have to include our join table Appointment.
We have to do the same with our Patient model:And now it’s time to connect these two models with our join model by defining a relationship where Appointment belongs to a Patient and a Physician.
This means that our database is ready.
It’s time to get started with the Node server.
Nothing complicated, just a simple Express server.
First of all let’s create a file called server.
js and let’s paste this code inside.
All that is left is to define the routes where our server will receive requests.
For this I will create 3 files inside a folder named “routes”, one file for each model.
Each file will have the following routes included:'GET' /name'GET' /name/:id'POST' /name'PUT' /name/:id'DELETE' /name/:id In these files we’ll import our models from the database that we’ll use and Sequelize will allow us to call methods on these models that execute specific queries in the database.
The content of your files will be almost identical, but with different model names.
This is what “physicianRoutes.
js” will look like:If you compare it to “patientRoutes.
js”, the only main difference you will see is the model names.
js”:These three files will help us to carry out the basic CRUD functionality in our app.
Of course we can create more endpoints where we can describe what to do in specific cases or what to return.
This is all for now, if you start up your server and populate your database you can Create, Read, Update and Delete your records.
Have fun!.. More details