node-transactions
v1.1.1
Published
Transactions engine for node.js
Downloads
33
Maintainers
Readme
Node Transactions
Usage
Examples of code are shown in test/index.spec.js
In case you are already using generators-yields
functions - you are completely set up.
If you are writing classic async-callback
code - you would probably like to look at thunkify
package.
##API
Context
The first thing you need is the context
object, which would have all required data for tasks execution.
let context = {
lannisters: false,
starks: true
}
Task
The main brick of the Node Transactions
module is a task
:
task = {
name: 'westeros',
perform: function *doYourJob() { ... },
rollback: function *revertChanges() { ... }
};
Launching
When you have prepared bunch of tasks
and context
- simply launch Transactions engine:
let result = yield new Transactions([task, nextTask, ...], context);
Result
result
would have next properties:
result.success
-enum: [true, false]
result.context[task.name].performResult
- the result of successful execution oftesk.perform
result.error
- the first and onlytask.perform
error (only ifresult.success === false
)result.rollbackErrors
- array of possibletask.rollback
errors
Storing transactions' data
To store the intermediate transaction's data, you need to pass into the context your own implementation of storeTransactionState, which would get next args:
function *storeTransactionState(name, phase, context) { ... }
context.storeTransactionState = storeTransactionState;
Issues
Any bugs or improvements are appreciated and could be posted at https://github.com/ceoworks/node-transactions/issues