npm install --save @krab/op
This module provides a building-block for domain logic.
Any software can be writtent using op
in such a way that
all of the domain logic in the software will be completely unit-testable.
Usage given below.
import Op from '@krab/op'; // in projects with ES7 modules
const Op = require('@krab/op/common'); // in projects with common JS modules
const domainOp = new Op(({db, api}) => {
return db.getResults() // returns a promise
.then((dbResults) => api.processData(dbResults))
;
});
// OR
const domainOp = new Op(async ({db, api}) => {
const dbResults = await db.getResults();
const apiResults = await api.processData(dbResults);
return apiResults;
}, { // this param is optional
doc: 'Does somthing with db and api',
types: {
db: (db) => db instanceof Db,
api: (api) => api instanceof Api
}
});
domainOp.mount({db: new Db(config.db), api: new Api(config.api)});
const results = await domainOp.run();
domainOp.run().then((results) => {});
// OR
domainOp.run({db: new Db(config.db), api: new Api(config.api)});
- An
op
is either async, or returns a Promise. - Failure to comply with
1.
will result in a runtime-error. - An
op
can have adoc
string attached to it. - An
op
can havetypes
associated with it, which will be used to validate the params supplied to theop
. - An
op
can be run in 2 ways:op.run(params)
orop.mount(params).run()
. op.mount
mounts a set ofparams
in the op, so thatop.run
can be called without any arguments.- An
op
can be cloned usingop.clone()
. - An
op.vent
is an event-aggregator on which the events'start'
,'err'
,'done'
, and'end'
are emitted at appropriate times in the lifecyclt of theop
.
op
uses @krab/vent
(link). Each instance of class Op
has a property vent
, which is an event aggregator attached to that instance.
Usage of op.vent
given below.
const op = new Op(...);
op.vent.on('start', ({op, params}) => {...});
op.vent.on('err', ({op, err, params}) => {...});
op.vent.on('done', ({op, result, params}) => {...});
op.vent.on('end', ({op, success, params}) => {...});