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

geddy-rest

v0.0.8

Published

RESTfull api for Geddy

Downloads

20

Readme

GeddyJs REST Helper

Use this simple library to avoid code repetition for Controllers REST actions. With one single line, you provide all the methods you need for your controller, in order to have a fully REST api.

It is made to be used with GeddyJs.

Instalation

  1. Run: npm install --save geddy-rest
  2. Add the folowing line inside your controller:
require('geddy-test').RESTify(this, MyModel);
  1. Add the folowing line inside /config/routes.js:
require('geddy-test').route(router, 'mymodel');
  1. You are done, and you can access trough:
GET /:model         ---> Find All
GET /:model/:id     ---> Find First
POST /:model/:id    ---> Create
PUT /:model/:id     ---> Update
DELETE /:model/:id  ---> Destroy

If you want named paths with GET method, read below.

Nested findings

Imagine the folowing situation: User hasMany Photos. If you need to read the User photos, you would need to go to an endpoint like GET /photos?userId=<theUserId>, after getting the user information in GET /users/<theUserId>.

With that in mind, I made a simple helper that allows you to do even more with Geddy. This situation will become GET /users/<theUserId>/photos, in a really simple way.

We present you, nested associations with REST:

// Add this to your config/routes.js to allow use of nested findings
require('geddy-test').RestApi.route(router, 'users', {
  find: {
    // Enables Nested finding route
    nested: true
  }
});

// User Controller
// We are nesting the model method 'getPhotos' as 'photos'
require('geddy-test').RESTify(this, MyModel, {
  find: {
    nested: {
      photos: 'getPhotos'
    }
  }
});

// You can even nest multiple actions, and provide a custom callback like:
function specialMethod(cb){
  // Send back data to be replaced
  cb({custom: 'data', to: 'be', shown: true});
}

require('geddy-test').RESTify(this, MyModel, {
  find: {
    nested: {
      photos: 'getPhotos',
      friends: 'getFriends',
      address: 'getAddress',
      special: specialMethod
    }
  }
});

The example above would generate the folowing routes:

GET /users/:id/photos  -> find then getPhotos
GET /users/:id/friends -> find then getFriends
GET /users/:id/address -> find then getAddress
GET /users/:id/special -> find then run specialMethod

Using it as a middleware

You can also use those methods for your own purpose. Look at some examples:

var Users = function (){
  // Inject methods into controller
  require('geddy-rest').RESTify(this, geddy.model.User);

  // Should route to: /users/:id/resume
  this.resume = function (req, res, params){

    // Execute custom find method
    this.find(req, res, params, function afterFind(err, model, cb){

      var data = {
        userName: model.name,
        userFriendsCount: model.getFriends().length
      };

      // Continue with it...
      return cb(data);

      // Or render it yourself
      respond(data);

    });
  }
}

Advanced usage

  • RESTify(controller, model, opts)

    Restify creates methods inside your given controller. It uses the model to create/find/delete/update.

    You can also assign some options:

    • opts.[create|find|update|destroy]: [false | Object] If set to false, will not generate the method.

      example:

      // Only enables find method
      RESTify(this, MyModel, {
        create: false,
        destroy: false,
        update: false,
      });
    • opts.[create|find|update|destroy].action: String Use this to change the desired method to be saved. You can, for example, generate it and use it the way you want:

      RESTify(this, MyModel, {
        find: {
          action: 'myFindAction'
        }
      });
          
      this.find = function (req, res, params){
        // Do whatever you want....
        var a = 2*9;
        // Delegate it to the REST find method
        this.myFindAction(req, res, params);
      }
    • opts.[create|find|update|destroy].beforeRender: function

      Use this property to receive actions just before rendering the data. You can either render it yourself (just don't call the next function), or process something before rendering and delegate it to the REST action egain:

      this._checkDbFirst = function (err, models, next){
        // You render the content here. Just don't call next.
        respond(models);
            
        // But if you do, pass the models to render
        next(models);
      }
          
      RESTify(this, MyModel, {
        create: {
          action: 'find',
          beforeRender: this._checkDbFirst
        }
      });
    • opts.find.nested: Object

      Put each nested method as the key, and the callback as the value. (Read above for more info);

    • GeddyJs is a well tought Framework. If you need to perform actions either before or after a call to the REST endpoint, use the methods .before and .after:

      // Calls prepareThings before find and create
      this.before(prepareThings, {only: ['find', 'create']});
      // Calls finishThingsUp after update and destroy
      this.after(finishThingsUp, {only: ['update', 'destroy']});
  • route(Router, ModelName, opts)

    This will generate routes acordingly to your needs. It should be placed inside /config/routes.js.

    If you need to alter the default routes, or HTTP methods you can access it exacly like the RESTify method:

    // Change the default route of 'find' to 'get'
    // Change the destroy method to 'GET'
    // Disables update route
    route(router, MyModelName, {
      find: {
        action: 'get'
      },
      destroy: {
        method: 'GET'
      },
      update: false
    });

    One last usefull option, is the opts.strict, which defaults is set to true.

    If set to false, will generate PATHS to facilitate your life (during development?):

    GET /:model/find
    GET /:model/find/:id
    GET /:model/create
    GET /:model/destroy/:id
    GET /:model/update/:id