-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Multi-Entry Transaction Commit Functionality
This commit introduces a new function commit designed to handle the transactional commit of multiple entries within a MongoDB session. Using the mongoTransaction utility, it ensures atomicity and data integrity during the commit process.
- Loading branch information
1 parent
90270be
commit 0e580bb
Showing
2 changed files
with
9 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,11 @@ | ||
import Entry from "../Entry"; | ||
import { TransactionError } from "../errors"; | ||
import * as mongoose from "mongoose"; | ||
import { mongoTransaction } from "./mongoTransaction"; | ||
import { IJournal } from "../models/journal"; | ||
|
||
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(); | ||
} | ||
export async function commit(...entries: Entry[]): Promise<IJournal[]> { | ||
let journals: IJournal[] = []; | ||
await mongoTransaction(async session => { | ||
journals = await Promise.all(entries.map(entry => entry.commit({ session }))); | ||
}); | ||
return journals; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters