Ha Doan
2 min readMay 27, 2020

Using sequelize to access MySql database from Nodejs

Recently I started to learn new thing and practice with different languages and tools, in this article I share my experience how to use MySql Database by sequelize in NodeJs

npm install --save sequelize
npm install --save mysql2
  • Step 2: create sequelize object to connect with MySql database — database.js file
  • Step 3: Define product model in folder mode/product.js
  • Step 4: Sync product model to generate products table in database

Add following code to app.js file

app.js

After add this code, run command node app.js to make changes in your database

  • Step 5: Add new product to product table

in controller/admin.js file, modify postAddProduct function

controller/admin.js
  • Step 6: Query all products from database and show in default page
controller/admin.js
  • Step 7: test your application

Go to http://localhost:3000/admin/add-product to add new product

then go to http://localhost:3000/ to see your product list from database!

The completed code for this part is at https://github.com/hadoan/vooyedu/tree/master/nodejs/mysql-end-part1

Have fun with nodejs!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Responses (3)

Write a response