
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
@m1212e/simple-transactions
Advanced tools
A simple transaction wrapper for JavaScript. Allows to register rollback functions during a transaction run. Rollback functions will be called in reverse order if the transaction throws an error.
A simple transaction wrapper for JavaScript. Allows to register rollback functions during a transaction run. Rollback functions will be called in reverse order if the transaction throws an error.
bun add @m1212e/simple-transactions
npm i @m1212e/simple-transactions
import { transaction } from '@m1212e/simple-transactions';
try {
// create a transaction
const returnOfTransactionIsReturnOfFn2 = await transaction((tx) => {
// register a call with a rollback function
// this could be an API fetch to create an entity
// in case of failure, the delete call will be called to rollback the creation
tx({ fn: workingCreationCall, rollback: workingDeletionCall });
// you can register multiple functions with rollback functions inside one transaction
tx({ fn: fn1, rollback: rollback1 });
// the return value of fn2 will be returned by the transaction call
const retOfFn2 = tx({ fn: fn2, rollback: rollback2 });
// when this error is thrown, the rollback begins and after completing, the error will be thrown again by the transaction call
throw new Error("test");
// if there would be not error inside the call before the return statement, the transaction will be successful and the return value of fn2 will be returned
return retOfFn2;
});
} catch (error) {
// Before throwing the error we catch here, rollback2 -> rollback1 -> workingDeletionCall are called and awaited completely
// at this point the transaction is already rolled back
console.error(error.message); // test
}
Sometimes it might be useful to register some calls throughout the lifetime of a transaction, that are supposed to run at the end, after the transaction completed. For this, you can use finalizers. They can be registered as part of a transaction call or manually at any time on a transaction object:
transaction((tx) => {
tx.onFinalize(finalizer); // manually
tx({ fn, rollback, finalizer }); // as part of a transaction call
});
Both ways of registering do not differ in functionality. The finalizers receive the success or failure state of the transaction as parameter (boolean, true for success, false for failure).
You can configure the transaction with a few helpful options passed as the second parameter.
const returnOfTransactionIsReturnOfFn2 = await transaction((tx) => {...},
{ timeout: 1000 } // error after 1 second if the transaction is not completed by then
);
FAQs
A simple transaction wrapper for JavaScript. Allows to register rollback functions during a transaction run. Rollback functions will be called in reverse order if the transaction throws an error.
We found that @m1212e/simple-transactions demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.