title: Node.js MySQL model designing
body: <p>I'm developing a node.js application using MySQL database but i'm stuck with making models on the node.js side of my application. I've used mongoose before to produce schemas and use models to do database functions but i couldn't find such support for MySQL. Can anyone suggest a proper way to isolate my database functions in node.js like i could do with mongoose. here's my app.js and users model i'm using right now. </p>
  
  <p>app file: </p>
  
  <pre><code>var express= require("express");
  var bodyParser = require("body-parser");
  var mysql = require("mysql");
  var UserModel= require("./models/User.js")
  var app=express();
  
  var sql = mysql.createConnection({
      host: "localhost",
      user: "root",
      password: "1234",
      database: "dricm"
  });
  
  sql.connect(function (err) {
      if(err){
          console.log("error");
      }else{
          console.log("connected");
      }
  });
  
  app.set("views", "./views");
  
  app.use(express.static("node_modules/bootstrap/dist"));
  app.use(express.static("public"));
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false}));
  
  app.get('/', function (req, res) {
      res.render("signup.jade");
  });
  
  app.post('/signup', function (req, res) {
      var obj= {
          username: req.body.username,
          password: req.body.password
      };
      UserModel.createUser(obj);
      res.redirect("/");
  });
  
  app.listen(3000, function () {
      console.log("server running at 3000");
  });
  </code></pre>
  
  <p>User(probable model)</p>
  
  <pre><code>var mysql= require("mysql");
  var bcrypt = require("bcryptjs");
  
  var sql = mysql.createConnection({
      host: "localhost",
      user: "root",
      password: "1234",
      database: "dricm"
  });
  
  sql.connect(function (err) {
      if(err){
          console.log("error");
      }else{
          console.log("connected");
      }
  });
  
  var User= {
  
  }
  
  User.createUser = function createUser(newUser) {
      bcrypt.genSalt(10, function(err, salt){
          bcrypt.hash(newUser.password,salt, function (err, hash) {
              newUser.password = hash;
              var query = sql.query("INSERT INTO USERS set ?", newUser, function (err, res) {
                  console.log(query);
                  if(err) {
                      console.log("error");
                  }
                  else{
  
                      console.log(res.insertId);
                  }
              });
          });
      });
  
  }
  
  module.exports= User;
  </code></pre>