Code Monkey home page Code Monkey logo

sails-util-mvcsloader's Introduction

sails-util-mvcsloader

NPM

Load models, controllers, services, policies and config from specified directories and inject them into the main Sails app.

This is partically usefull to extend your Sails app from hooks.

You can have create new models/controllers/etc in your hook, and have them loaded into the main Sails app.
You can even extend models/controllers/etc already existing in your main Sails app with specific methods/properties from your hooks.

Installation

You have to install it in the project from where you will load the models/controllers/etc.

If you want to load MVCS from a hook in your main Sails' app (living in the hooks/ folder), install it in your Sails app's main folder.

If you want to load MVCS from an installable hook (separate NPM package), install it directly in your installable hook's main folder.

Run this command to install and add the package as a dependency in your package.json :

npm install --save sails-util-mvcsloader

How to use it

You can find a complete hook example in the example folder.

Using this module is pretty easy.

In your hook's index.js file, require the module and pass your Sails app as first argument :

module.exports = function(sails) {
    var loader = require('sails-util-mvcsloader')(sails);

    ...

    return {
        initialize: function (next) {
            ...
        }
    };
};

Loading config / policies

You can load config and policies with the configure method.
This method is synchronous and MUST be called before the hook initialisation, outside the initialize method.
If you don't want/need to load config or policies, you don't have to call this method.

Use it like this (complete example below):

// Load policies under ./api/policies and config under ./config
loader.configure();

Or like this if you want to load from specific directories:

loader.configure({
    policies: __dirname + '/api/policies',// Path to the policies to load
    config: __dirname + '/config' // Path to the config to load
});

Loading models / controllers / services

To load models, controllers and services, call the inject method.
This method is asynchronous and must be called after the configure method (presented above), if you need to load config and policies.

Use it like this (complete example below):

/*
    Load models under ./api/models
    Load controllers under ./api/controllers
    Load services under ./api/services
*/
loader.inject(function (err) {
    return next(err);
});

Or like this if you want to load from specific directories:

loader.adapt({
    controllers: __dirname + '/controllers', // Path to the controllers to load
    models: __dirname + '/models', // Path to the models to load
    services: __dirname + '/services' // Path to the services to load
}, function (err) {
    return next(err);
});

Complete example

Here is a complete example. It's the index.js file of a Sails hook.

module.exports = function(sails) {
    var loader = require('sails-util-mvcsloader')(sails);

    // Load policies under ./api/policies and config under ./config
    loader.configure();

    /*
        OR if you want to set a custom path :

        loader.configure({
            policies: __dirname + '/api/policies',// Path to your hook's policies
            config: __dirname + '/config'// Path to your hook's config
        });
     */

    return {
        initialize: function (next) {
            /*
                Load models under ./api/models
                Load controllers under ./api/controllers
                Load services under ./api/services
            */
            loader.inject(function (err) {
                return next(err);
            });

            /*
                OR if you want to set a custom path :

                loader.inject({
                    controllers: __dirname + '/controllers', // Path to your hook's controllers
                    models: __dirname + '/models', // Path to your hook's models
                    services: __dirname + '/services' // Path to your hook's services
                }, function (err) {
                    return next(err);
                });
             */
        }
    };
}

Used by

sails-hook-passport

Development

Contributing

For now, we use 4 spaces instead of 2.
For the rest, please follow the Felix's Node.js Style Guide.

We use semantic versioning for the NPM package.

Contributors

TODO

  • Add support for loading :
    • Views
    • Assets
  • Add tests
  • Add Grunt for auto-JSHint & tests

sails-util-mvcsloader's People

Contributors

leeroybrun avatar salakar avatar jaumard avatar cloudtaxi avatar

Watchers

James Cloos avatar Kent Chen 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.