Ad

Question About Model In MVC Architecture In Web App Using NodeJs And MySQL

I am new to NodeJs and I am trying to create a web application using express framework and MySql. I get that in MVC architecture the views are for example the *.ejs files. The controllers are supposed to have the logic and the models should focus on the database.

But still I am not quite sure what is supposed to be inside the model. I have the following code in my controller (probably wrong, not following mvc design):

const mysql = require('mysql');
const db = mysql.createConnection(config);
db.query(query, (err, result) => {
    if (err) {
        return res.redirect('/');
    }
    res.render('index.ejs', {
        users: result
    });
});

Now from what I've read the controller should ask the model to execute the query to the database, get the results and render the view (index.ejs').

My question is this: What should be inside the model.js file? Can I make something like this?

controller.js

const db = require('./models/model.js');
db.connect();
const results = db.query(query);
if(results != null) {
     res.render('index.ejs'){
          users: result
     });
}

model.js will make a query to mysql handle any errors and return the result.

From what I've read I have two options. Option1: pass callback function to model and let the model render the view (I think that's wrong, model should not communicate with view, or not?) Option2: possible use of async/await and wait for model to return the results but I am not sure if this is possible or how.

Ad

Answer

The model is the programmatic representation of the data stored in the database. Say I have an employees table with the following schema:

name: string
age: number
company: company_foreign_key

And another table called companies

name: string
address: string

I therefore have two models: Company and Employee.

The model's purpose is to load database data and provide a convenient programmatic interface to access and act upon this data

So, my controller might look like this:

var db = require('mongo');
var employeeName = "bob";
db.connect(function(err, connection){

  const Employee = require('./models/Employee.js'); // get model class

  let employeeModel = new Employee(connection); // instantiate object of model class

  employee.getByName(employeeName, function(err, result){ // convenience method getByName

    employee.getEmployeeCompany(result, function(err, companyResult){ // convenience method getEmployeeCompany

     if(companyResultl) { // Controller now uses the results from model and passes those results to a view

       res.render('index.ejs')
        company: companyResult
     });
    })
  })
}

})

Basically, the model provides a convenient interface to the raw data in the database. The model executes the queries underneath, and provides convenient methods as a public interface for the controller to access. E.g., the employee model, given an employee object, can find the employee's company by executing that query.

The above is just an example and, given more thought, a better interface could be thought up. In fact, Mongoose provides a great example of how to set up model interfaces. If you look at how Mongoose works, you can use the same principles and apply them to your custom model implementation.

Ad
source: stackoverflow.com
Ad