use mongoose transaction helper

use the helper instead of manually commiting/aborting
to deal with transient transaction errors
This commit is contained in:
Aleksandr Saitgalin
2022-01-05 17:27:45 +06:00
parent 3e7679d913
commit 254642be97
+2 -10
View File
@@ -138,19 +138,11 @@ api.createGroup = {
}
let savedGroup;
const session = await Group.startSession();
try {
session.startTransaction();
await Group.db.transaction(async session => {
await user.save({ session });
savedGroup = await group.save({ session });
await session.commitTransaction();
} catch (error) {
await session.abortTransaction();
throw error;
} finally {
session.endSession();
}
});
// Instead of populate we make a find call manually because of https://github.com/Automattic/mongoose/issues/3833
// await Q.ninvoke(savedGroup, 'populate', ['leader', nameFields]);