Thursday, November 7

Stored Procedure for node.js

User.createuser = (req, result) => {
      sql.query("INSERT INTO Users SET ?", req, (err, res) => {
        if (err) {
          console.log("error: ", err);
          result(err, null);
          return;
        }
    
        // if (await User.findOne({ where: { username: req.body.username } })) {
        //   throw 'Username "' + req.body.username + '" is already taken';
        // }
    
        console.log("created User: ", { id: res.insertId, ...req });
        result(null, { id: res.insertId, ...req });
      });
    };

Example json object for api create route (post method)

{   

    "username": "seve",
    "user_name": "sev123",
    "user_surname": "avcı",
    "email": "sevre@gmail.com",
    "user_type": "user",
    "password": "123456"
}

The “INSERT INTO Users SET ?” query that I use in my code, I want it as a stored procedure, I want it to handle the stored procedure id itself, what is the equivalent of this code’s stored procedure?