Sequelize Project setup
We will start by setting up a basic express web API application which will use the sequelize ORM to manipulate data on a PostgreSQL database.
I believe you now have all the necessary tools installed on your machine or you can go to the previous article in this series to check out the necessary tools and the links to install them.
Express API setup
Open your terminal and run the following command to create a workspace and navigate into it. Copy and paste the commands below into your terminal.
$ mkdir introduction-to-sequelize
$ cd introduction-to-sequelize
Let’s create a basic and empty NodeJS project using the command below
$ npm init -y
Next, we will install the required dependencies to create a REST API using express.
$ npm install express body-parser helmet --save
Next, we will install the required babel dependencies as we will be using ES2015/ES6 and above.
$ npm install babel-cli babel-core babel-preset-env babel-plugin-transform-class-properties babel-plugin-transform-object-rest-spread nodemon --save-dev
$ touch .babelrc
Good, let's add the snippet below to our .babelrc file.
{
"presets": [
[
"env", {
"targets": {
"node": "current"
}
}
]
],
"plugins": [
"transform-object-rest-spread",
"transform-class-properties"
]
}
Now that we have our express and babel dependencies ready, we will create the server directory to house our application.
$ mkdir server
$ cd server && touch index.js && cd ../
Add the following code snippet to the ./server/index.js file. This will be the entry point of our app. We now have the basic middleware setup for the API.
Next, we will add a router which will contain a handler for the index route as well as other routes(404).
$ cd server && mkdir routes && touch index.js
$ cd ../
Add the code snippet below to the ./server/routes/index.js file. This contains our application router with two route handlers.
Replace the scripts object in your package.json file with the snippet below. This scripts will be used to build, run the app in development and production environments.
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "NODE_ENV=development nodemon --exec babel-node ./server/index.js",
"build": "babel ./server --out-dir ./build",
"prestart": "npm run build",
"start": "node ./build/index.js"
},
Now, let’s start our server.
$ npm run dev
You should see something similar to the image above on your console terminal. CONGRATS!!! 🙌.
Sequelize installation
Next, we will install the required sequelize dependencies. Paste the command below into your terminal.
$ npm install sequelize-cli --save-dev
$ npm install sequelize pg pg-hstore --save
$ touch .sequelizerc
$ touch .env
- sequelize — the sequelize NodeJS package.
- pg — a query library for PostgreSQL which sequelize uses internally to connect to PostgreSQL.
- pg-hstore — a library which sequelize uses for serializing and deserializing JSON data to hstore format.
- sequelize-cli — the sequelize command line interface.
Sequelize setup
Now that we have all our basic dependencies installed we will add the code snippet below to the .sequelize file.
Good, now we will use the sequelize-cli to bootstrap the sequelize setup on our application. Let’s run the command below on our terminal to achieve this.
$ node_modules/.bin/sequelize init
Wonderful, now replace the code in your database.js
file with the snippet below.
require('dotenv').config(); module.exports = {
development: {
username: process.env.DEV_DB_USERNAME,
password: process.env.DEV_DB_PASSWORD,
database: process.env.DEV_DB_NAME,
host: process.env.DEV_DB_HOSTNAME,
dialect: 'postgres',
},
test: {
username: process.env.CI_DB_USERNAME,
password: process.env.CI_DB_PASSWORD,
database: process.env.CI_DB_NAME,
host: '127.0.0.1',
dialect: 'postgres',
},
production: {
username: process.env.PROD_DB_USERNAME,
password: process.env.PROD_DB_PASSWORD,
database: process.env.PROD_DB_NAME,
host: process.env.PROD_DB_HOSTNAME,
dialect: 'postgres',
},
};
Add the following code snippet into your .env
file and update the values with the connection details of for your PostgreSQL database.
DEV_DB_USERNAME=<string>
DEV_DB_PASSWORD=<string>
DEV_DB_NAME=<string>
DEV_DB_HOSTNAME=<string>CI_DB_USERNAME=<string>
CI_DB_PASSWORD=<string>
CI_DB_NAME=<string>
Following all these instructions, you will now have a folder structure similar to this.
Where did all these folders come from? Well, we will get to that so no need to panic.
The Sequelize cli
The sequelize-cli provides a command line interface for us to interact with our sequelize ORM like creating models, migrations, seeders and lots more.
To see all the available commands and operations available on the cli, run the command below in your terminal.
$ node_modules/.bin/sequelize --help
Migrations, Models, and Seeders
We will now see the sequelize-cli in action.
Previously, when we executed the sequelize init command it created lots of folders and files in our workspace. Now we will dive into what those files and folders are used for.
- migrations — This folder contains all the files(migrations) needed to update our database schema and keep it up to date with the latest requirements of our application.
- models — This folder contains the M in the MVC architectural pattern. It contains objects which form the mapping between our tables in our database and our domain-specific objects. They retrieve and store model state in the database.
- seeders — This folder contains files which add data to our database. I’ve found this most useful when following TDD. This combined with migration is a powerful feature.
We will look at these in full in the next article in this series. Click the link below to view the complete code for this article and other articles in this series. Don’t forget to clap or give us a star if this was helpful to you.