GithubHelp home page GithubHelp logo

logic's Introduction

logic build status

Data aggregation framework.

Usage

npm install logic

then

var logic = require('logic');

Test

Clone repository, then

npm install
npm test

Overview

Logic helps you to define relationships between different data sources and combine it into the one solid responce. It knows nothing about how this data sources work.

There are two types of logics:

  • provider based logic, which leads to some data
  • abstract logic, which is used to organise other logics (abstract or provider based)

Abstract logic declaration

logic.define('logic-name', {

  /**
   * `logic-name` depends on this three,
   * where 1 level of array will be ran in series
   * and 2 level of array will be ran in parallel
   *
   * If any of this logics will be failed
   * the root one also will be failed
   */
  deps: ['logic-name-1', ['logic-name-2', 'logic-name-3']],
  
  /**
   * List of params `logic-name` depends on
   */
  params: {
    id: Array,
    name: String
  },
  
  /**
   * `logic-name-1` hook,
   * will be called before its execution
   * @param {Object} evt
   */
  'logic-name-1': function(evt) {
    evt.name;     // 'logic-name-1'
    evt.params;   // params this logic will be requested with,
                  // inherited from the root logic `logic-name`
                  // can be overwritten, then will go with it's own params
    evt.options;  // logic options
    evt.results;  // array of already requested logics
    evt.preventDefault();   // if called logic will be fullfiled
                            // with returned value immediately
    return { status: 'prevented' };
  },
  
  /**
   * Callback to be called when all logic are fullfiled
   * @param {Array} results
   * @param {Object} params
   * @param {Object} options
   */
  ready: function(results, params, options) {
    results; // array of results
             // in the order you declare it in `deps`
    params;  // params root logic was called with
    
    return {
        'logic-name-1': results[0],
        'logic-name-2': results[1],
        'logic-name-3': results[2]
    };
  }

});

Logic provider

Provider must decide if it will process logic with a given name, params and options or not. If so it should return function, or anything else otherwice (undefined or null is better). You can add as many providers, as you want, but only first match will be executed. logic knows nothing about how provider works, it can be HTTP request, DB query, memory cache, etc. The only rule: provider function should return A+ promise.

logic.provider(function(name, params, options) {
    if (name === 'accepted-logic') {
        return function(name, params, options) {
            return promise( name + JSON.stringify(params) );
        }
    }
});

Logic execution

Now in your API you can use just a single call, and logic will take care about all the dependencies.

logic('logic-name', params, options)
    .then(function(result) {});

logic's People

Contributors

ab-5v avatar

Stargazers

Anne Thorpe avatar Coder of Salvation / Leon van Kammen avatar Varya Stepanova avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.