How to Use Sequelize ORM In Node


Prerequisites

What Is Sequelize?


As mentioned on the sequelize site, sequelize is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite, and Microsoft SQL Server. It features solid transaction support, relations, eager and lazy loading, read replication and more. Sequelize follows SEMVER. It supports Node v6 and above to use ES6 features.

Project Structure 

|--------- config
               |--------- db.js
|--------- controllers
               |--------- memberController.js
|--------- models
               |--------- member.js
|--------- routes
               |-------- members.js
|--------- views
                | |------- partials
                |              |------- header.ejs
                |              |------- footer.ejs
                |
                |------ home.ejs
                |------ edit.ejs
|--------- app.js
|--------- package.json


Project Setup
Let's begin by setting up our workspace
  • Open the console and type mkdir followed by the directory name
  • Now type cd followed by the directory name.
  • Now type npm init to create a package.json file for our application.

You can learn about package.json file here
# npm init
After typing this command you will be prompted with a few things related to package.json file, such as name, version, etc. Once finished, a package.json file will be generated.

Express Setup
After generating the package.json file, we will install the express framework and some other packages.
# npm install express body-parser ejs 
The installed package will be put in the dependencies section of package.json.
Add a new file app.js in the root. This will be the starting point of our application.
Open the package.json file and in "scripts" write "start":" node app.js".
Now, add the new folders to the project.


Sequelize Setup
To use sequelize, we first need MySql installed on the system.
Let's start by installing sequelize in the project.
# npm install --save sequelize
# npm install --save mysql2   
After installing sequelize, go to the config folder and set up the database connection in the db.js.
config ->db.js

Here you have to provide the name of your database, the username, and the password of the server.
Now open the app.js file and put in the below code.

app.js

Now type run the application and check the db connection. 
# node app.js
In the console, we can see the following if there is no error.
server running at 3000
Executing (default): SELECT 1+1 AS result
Connection has been established successfully.
working

Create Model
  • member.js
Here we define the schema of our members table.
The table will contain 4 fields: name, country, language, and salary.

Create route and controller
 routes -> members.js

controller->memberController.js 
  • here we are using raw:true in findOne(). It will return the raw result. You can see it in detail here.
Create view

 Here we create a partial folder that will contain the part of the template used frequently. It will contain 2 files a). header.ejs, and b). footer.ejs.
We wil be using ejs for the views. To learn more about ejs, click here.
  • views->partials->header.ejs

  • views->partials->footer.ejs

  • views->home.ejs

  • views->edit.ejs
Now update your app.js by adding the below line of code to it.

  • app.get('/') will be the default route when our application starts. 
  • app.use('/member',require('./routes/members')) - this is a route middleware.
  • Whenever the coming request has a '/member' route, then the particular route method in routes/members will be activated.
  • Put this code before assigning the port.


Run the application by typing:
# node app.js
Click to watch a video tutorial

Comments

Popular posts from this blog

Upload And Download File In Node

How To Use Sequelize-Cli In Node

Setup Flask Project