So i am going with mysql2 driver to use Mysql with node. Let's say i have 25 to 30 maybe more fields in a row, would i have to manually write every column after extracting it from body. My current approach is as below, here's the complete controller for better understanding.
exports.registerUser = async (req, res, next) => {
let errors = validationResult(req)
if (!errors.isEmpty()) {
let err = new Error()
err.errors = errors
err.status = 400
next(err)
}
console.log('entered')
let { user_role_id, name, email_id, password,
mobile_no, work_exp_years, work_exp_month,
resume, current_location } = req.body
try {
let query = `INSERT INTO user_account_details
(user_role_id, name, email_id, password,
mobile_no, work_exp_years, work_exp_month,
resume, current_location) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
let [insertRow] = await mysql.execute(query, [user_role_id, name, email_id, password,
mobile_no, work_exp_years, work_exp_month,
resume, current_location])
if (!insertRow) {
throw new Error('Insert error')
}
return res.status(200).json({ result: insertRow })
} catch (err) {
let error = new Error()
error.errors = err
next(error)
}
}
So, my only concern is the part where i am inserting fields in a row, i feel like i am writing column names a lot of times, is there any way i can just take the body and insert it into row like in mongoose?