-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrap all account deletion operations in one database transaction
- Loading branch information
Showing
6 changed files
with
70 additions
and
47 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
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
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
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { EntityManager } from "typeorm" | ||
|
||
import AppDataSource from "../AppDataSource"; | ||
|
||
/** | ||
* Run the provided callback in a database transaction. The `entityManager` can | ||
* be passed as an argument down the call stack to make the transaction cover | ||
* any database operation that can take the `EntityManager` to use as an | ||
* argument. | ||
* | ||
* This function accepts `Err` `Result`s to signal that the transaction should | ||
* be aborted, in addition to the conventional signals of throwing an exception | ||
* or returning a rejected `Promise`. | ||
*/ | ||
export async function runTransaction<T>(runInTransaction: (entityManager: EntityManager) => Promise<T>): Promise<T> { | ||
return await AppDataSource.manager.transaction(async (entityManager) => { | ||
const result = await runInTransaction(entityManager); | ||
if ("err" in result && "val" in result) { | ||
if (result["err"]) { | ||
return Promise.reject(result["val"]); | ||
} | ||
} | ||
}); | ||
} |
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
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