npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sails-util-mvcsloader

v0.4.0

Published

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

Downloads

817

Readme

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