quicksilver

a simple MVC pattern for nodejs projects with builtin "Amazing Business Logic"™

npm install quicksilver
35 downloads in the last month

Quicksilver

a simple "Amazing Business Logic"™ frame work to help give structure to your code.

Code can quickly get out of control, Quicksilver gives developers an easy way to structure code while not getting the in way of actual development. In fact it makes a lot of code easier to read, easier to understand the structure, and easier to understand how the code works.

It all starts with a controller, or a location where logic code is written.

Controllers

Controllers are a collection of stateless functions that have been organized and given structure. Each controller has an api and can expose 'commands' through that api.

Controllers are setup by creating a new controller and creating commands:

QS = require 'QuickSilver'

FirstController = new QS.Controller ->
    @command 'login',->
    @command 'logout',->
    @command 'show secret stuff',->
var QS = require('QuickSilver')

var FirstController = new QS.Controller(function(){
    this.command('login',function(){});
    this.command('logout',function(){});
    this.command('show secret stuff',function(){});
});

Command

Commands are the basic way to organize functions together under a name that can then be ran later. In the same way that a function is a bundle of tightly integrated code that has a specific purpose, a Command is s bundle of loosely integrated code that has a specific purpose. A command is divided into 3 different areas that have 3 different purposes. The 3 purposes are: validating inputs, fetching data and performing computations, and formatting data to be returned. All functions that are part of these 3 areas are called Control Functions.

Control Functions:

Validating Inputs

Validation is the first area of functions that are run. All Validation Control Functions, or ValCF's are run before any other Control Function is run, they are ran in series and anything returned that is not true, is collected and once all ValCF's hav finished running, the errors are presented to the user in an array. Any errors that are thrown at this point are considered as errors, and are NOT collected, instead they stop the thread of execution and are returned to the error callback. Throw errors are NOT considered validation errors, they are errors.

QS = require 'QuickSilver'
FirstController = new QS.Controller ->
    @command 'login',->

        @valcf (data)->
            data.username == 'user' || 'you forgot to give me a username!'

        @valcf (data)->
            data.password == 'secret' || 'you forgot to give me a password!'

options = {}
callbacks = 
    validateFail: (validation_errors)->
        console.error "login did not validate correctly:"
        console.error err for err in validation_errors
    error: (error)->
        console.error "the login command errored out:"
        console.error error.stack

FirstController.run 'login',options,callbacks
var QS = require('QuickSilver')
FirstController = new QS.Controller(function(){
    this.command('login',function(){

        this.valcf(function(data){
            return data.username == 'user' || 'you forgot to give me a username!'
        });

        this.valcf(function(data){
            return data.password == 'secret' || 'you forgot to give me a password!'
        });
    });
});

var options = {}
var callbacks = {
    validateFail: function(validation_errors){
        console.error("login did not validate correctly:");
        for(err in validation_errors){
            console.error(err);
        }
    error: function(error){
        console.error("the login command errored out:");
        console.error(error.stack);
        }
    }};

FirstController.run('login',options,callbacks);
npm loves you