Skip to content

Instantly share code, notes, and snippets.

View aeciolevy's full-sized avatar

Aecio Levy aeciolevy

  • VentionCo
  • Canada
View GitHub Profile
@aeciolevy
aeciolevy / Example.js
Created March 3, 2019 17:18
Example of mongoose transaction. MongoDB transaction example
exports.deleteUser = async (req, res, next) {
const session = await mongoose.startSession();
try {
const { id } = req.params;
// Start session
await session.startTransaction();
// deleteMany in this session
const [errorOp, result] = await toAll([App.deleteMany({ user: id }).session(session), UserModel.findByIdAndRemove(id).session(session)]);
if (errorOp) {
throw new ErrorRequest(STATUS_CODE.UNPROCESSABLE, errorOp.message);