My database is SQL Server and I'm trying to use mssql npm package.
Issue I'm facing here is, if I have a rejected promise, I was hoping my transaction to rollback, which I'm doing in the catch. But for some reason, my database is being updated.
const transaction = new sql.Transaction();
try {
await transaction.begin();
let q1;
if (shippingExists) {
q1 = await updateShippedQuery();
} else {
q1 = await insertShippedQuery();
}
const q2 = await editWorkOrderQuery();
const q3 = await addNoteQuery();
const q4 = await editProductQuery();
const q5 = new Promise((resolve, reject) => {
setTimeout(reject, 100, { error: "Delibrate error" });
});
const [query1, query2, query3, query4, query5] = await Promise.all([
q1,
q2,
q3,
q4,
q5,
]);
await transaction.commit();
return { query1, query2, query3, query4, query5 };
} catch (e) {
await transaction.rollback();
console.log(e);
return { error: e.message };
}
};
Example of one function looks like this:
const sql = require("mssql");
const editProductQuery = async () => {
const query = `
UPDATE product
SET available_quantity = 4
WHERE part_number = '10000'
`;
return await sql.query(query);
};
module.exports = editProductQuery;
What should be done here?