Untitled
unknown
plain_text
3 years ago
1.5 kB
3
Indexable
function buildUpdateQuery(columns, offset) { let queryItems = []; let values = []; Object.keys(columns).forEach((key, index) => { if (columns[key] !== null && columns[key] !== undefined) { values.push(columns[key]); queryItems.push(`${key}=$${values.length + offset}`); } }); return [queryItems.join(", "), values]; } const test_obj = { col_1: null, col_2: undefined, col_3: "pasta", col_4: "2-2-2022" }; console.log(buildUpdateQuery(test_obj, 1)); // router.put("/:id", auth,(req, res) => { // const { id } = req.params; // const user_id = req.user_id; // const { col_1, col_2, col_3, col_4 } = req.body; // const [queryString, values] = buildUpdateQuery({ col_1, col_2, col_3, col_4 }) // db.query( // `UPDATE <TABLE> SET ${queryString} WHERE id=$1 RETURNING *`, // values // ) // db.query( // "UPDATE goals SET col_1=$2, col_2=$3, col_3=$4, col_4=$5 WHERE id=$1 RETURNING *", // [col_1, col_2, col_3, col_4] // ) // } // router.put("/plans/:plan_id/goals/:goal_id", auth,(req, res) => { // const { plan_id, goal_id } = req.params; // const user_id = req.user_id; // const { title, status, start_date, end_date } = req.body; // db.query( // `SELECT p.user_id, p.id plan_id, g.id goal_id, g.title, g.status, g.start_date, g.end_date, g.create_date FROM goals g // INNER JOIN plans p on p.id = g.plan_id where p.user_id =$1 and g.plan_id=$2 and g.id=$3`, // [user_id, plan_id, goal_id] // ).then((result) => {
Editor is loading...