Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

opsmap

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

opsmap

Map of runnable interdependent operations

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

opsmap Build Status Code Climate

Map of runnable interdependent operations

NPM

Example

var OpsMap = require('opsmap');
var UserModel = require('./models/user.js');
var BlogModel = require('./models/blog.js');

// declare map of interdependent operations
var blog = new OpsMap('blog');

blog.set('username', 'edinella'); // already resolved values can be setted

blog.op('user', function() {
  return UserModel.findOne({username: 'edinella'}).exec(); // returns a promise
});

blog.op('userPosts', function(user) {
  return BlogModel.find({author: user.id}).exec();
});

// run userPosts operation, returns a promise
blog.run('userPosts').then(yep).fail(nope);

// handle results
function yep(result) {
  console.log(result);
}

// handle errors
function nope(err) {
  console.error(err);
}

How to use

Install with NPM:

npm install --save opsmap

Then require it:

var OpsMap = require('opsmap');

API

constructor(alias): create a new OpsMap with an alias

To produce the instance, OpsMap should be called with new operator.

var report = new OpsMap('report');

set(token, value): defines a value for injection (alias: setCache)

Register the final value.

report.set('dateFormat', 'DD/MM/YYYY HH:mm:ss');

op(token, factoryFn): defines a operation that generates a value for injection (alias: operation)

To produce the instance, factoryFn will be called once (with instance context) and its result will be used.

The factoryFn function arguments should be the tokens of the operations that we need resolved here.

report.op('data', function(filters){
  return MyModel.find(filters).exec();
});

run(token): runs an operation, returns a promise

If any operation throws an error or gets rejected, and you omit the rejection handler, the execution will be stopped and error would be forwarded to this resultant promise.

// result is a promise
var myStats = report.run('stats');

// handle results
myStats.then(function(result) {
  console.log(result);
});

// handle errors, including those unhandled from inside operations
myStats.fail(function(err) {
  console.error(err);
});

Inspiration

Angular.js DI

githalytics.com alpha

Keywords

FAQs

Package last updated on 21 May 2015

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc