Basic Javascript REST API with DB and integration tests Part 1 - Express/Sequelize
Through this series I intend to explain every step fully, if you follow these steps exactly you will get a working project. However I will also be placing links throughout where you can read up on concepts and programs in more detail. If you are reading this to learn I recommend taking the time to read a little further on each step where you can.
Now onto the details, in this series we will be going through every step required to create a very basic RESTful API that uses:
- Express for the server
- Sequelize for the database set up
- Containerised Postgres as the database
- Jest and Supertest to conduct API tests
Thank you’s and Sources
At the beginning of this walkthrough I wanted to state my sources and how this article has been put together. As always in the open source world nothing is brand new and everything is iterative, in this case this walkthrough is an amalgamation of tutorials and articles I found online that helped me build this code from scratch. So thanks to these guys and definitely check their work out if you can.
Pre-requisites
I will include the versions of the installed software as I write, you should be able to complete the steps on newer versions but be aware that commands/syntax and compatibility may change
- Installed Node.JS and NPM - Installation (Node v16.14.0 & npm 8.3.1)
- Docker - Installation (Docker 20.10.11)
- docker-compose - Installation (docker-compose version 1.29.2)
- I recommend an IDE of some sort to help write the code, I am using VSCode - Installation (1.65.2)
- A terminal, I will be using BASH. A terminal will be included on Linux and OSX. I’d recommend WSL on Windows Installation
Steps
Install Express and set up the scaffolding
-
Download Express generator
npm install express-generator -g
-
Navigate to your projects folder
-
Scaffold an Express project
express node-sequelize-postgres-jest-supertest --view=ejs
-
Move into the newly created project
cd node-sequelize-postgres-jest-supertest
-
Install pre-packaged node packages
npm install
Add and configure Sequelize
-
Install sequelize-cli globally
npm install -g sequelize-cli npm install sequelize-cli
-
Install sequelize in the project
npm install sequelize
-
Install the Postgres modules
npm install pg pg-hstore
-
Create sequelize config file
touch .sequelizerc
-
Open the project in your IDE
-
Edit the newly made .sequelizerc file and enter this text. For more info on what this config file does see here
const path = require('path'); module.exports = { "config": path.resolve('./config', 'config.json'), "models-path": path.resolve('./models'), "seeders-path": path.resolve('./seeders'), "migrations-path": path.resolve('./migrations') };
-
Initialize sequelize
sequelize init
-
Edit the file in config/config.js so it looks like below
{ "development": { "username": "postgres", "password": "postgres", "database": "remakingeden", "host": "127.0.0.1", "dialect": "postgres" }, "test": { "username": "postgres", "password": "postgres", "database": "remakingeden_test", "host": "127.0.0.1", "dialect": "postgres" }, "production": { "username": "postgres", "password": "postgres", "database": "remakingeden_production", "host": "127.0.0.1", "dialect": "postgres" } }
Well done! You now have the scaffolding of a basic express project with sequelize installed and configured.
Wrap up
So far we have looked at creating a generic express project and adding modules for sequelize and postgres. We have edited the config to make it ready to be used in the next stage of the walkthrough. This is a brilliant foundation on which to build the remaining API and tests.
If you have got lost at all, or would just like to learn more about what we have done please click on the links throughout the steps and read the extra documentation.
What is next?
Next up we will look at creating a docker-compose file that will start a postgres container on our local machine with just one command. This will then be ready to fill with data using sequelize.