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
- The starting template is in this folder https://github.com/hadoan/vooyedu/tree/master/nodejs/mvc-begin
- Step 1: install required npm libraries: sequelize and mysql2
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

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

- Step 6: Query all products from database and show in default page

- 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!