This article will continued Part 1, in the part 1 we create two models, in the part 2 we will know how to use model to do CRUD(Create, Read, Update, Delete).
If you are lost, do not know what is this, you may read part 1 first.
We need a page to display master information, first edit your index route in the routes directory, it could look likes below.
First, require the models then we can easy to access our database.
Now we must read data from the database, all models is an object and name is what you defined in the models directory, in this case, we need to call Master model so syntax will be models.Master.
Next we call findAll method to read all data from table, in the next line we can see .then(), right! Sequelize is using promise to instead of callback.
We completed a basic route and using Sequelize to read data from table, next we must create a view to render html page, now open index.pug in the views directory and do a little edit.
Default index could be likes below.
Remove all below content block and add a little simple html.
For now, a simple web page is done, we can run the app, in the application root, run command likes below.
And open browser and go to http://localhost:3000/, we can see a simple page is here and show No Master.
We have master list now, next step we can make a master detail page, this page must have master and pet info, let’s add a new route first.
Here just a little different, we used url params and we can see a keyword named include, if we need to read data from relations table, we must give model name in here.
Next make a view for master detail, create a new file in the views directory and named info.pug and putsome simple html.
We can restart the application and open http://localhost:3000/, we completed a Node.js with Express and Sequelize!
You can find the source code in the GitHub.
Let me know if you have any question, thanks!