Skip to content

Commit

Permalink
Implement draft Multi-Entry Transaction Commit Functionality
Browse files Browse the repository at this point in the history
New helper function `commit` to handle the simultaneous transaction commit for multiple entries
  • Loading branch information
Serhii Chyzhyk committed Feb 9, 2024
1 parent e677d0f commit 80b026e
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/helper/commit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Entry from "../Entry";
import { TransactionError } from "../errors";
import * as mongoose from "mongoose";

export async function commit(...entries: Entry[]) {
const mongooseSession = await mongoose.startSession();
try {
mongooseSession.startTransaction();
const journals = await Promise.all(entries.map(entry => entry.commit()));
await mongooseSession.commitTransaction();
return journals;
} catch (error) {
await mongooseSession.abortTransaction();
throw new TransactionError(`Failure to commit entries: ${(error as Error).message}`, entries.length,500);
} finally {
await mongooseSession.endSession();
}
}

0 comments on commit 80b026e

Please sign in to comment.