Arun Febi
Arun Febi

Reputation: 119

Sails.js - how to bind Custom blueprint action for all the models?

I have created a custom blueprint action count in file api/blueprints/count.js.

I want to generalizing this action to all the model. The problem is when I am adding the custom blueprint action like this

         'get /:model/count': {blueprint: 'count'}

I got this error on lifting the application :

           error: count :: Ignoring attempt to bind route (/:model/count) to blueprint action (`count`), but no valid model was specified and we couldn't guess one based on the path.

I solve the issues by specifying the model name in the configuration property

        'get /:model/count': {blueprint: 'count', model: 'user'}

or specifying it in the address

         'get /user/count': {blueprint: 'count'}

The problem in specifying like this is that I need to add route for every other model also. Is there any way I can generalize this route to something like this 'get /:model/count': {blueprint: 'count'}.

It will great if we have this feature.

Please Help.

Upvotes: 2

Views: 944

Answers (2)

Kristian Ačkar
Kristian Ačkar

Reputation: 945

Based on @ghaiklor answer/code i created sails installable hook "sails-hook-blueprint-count" to enable count blueprint api methods.

"sails-hook-blueprint-count" package is available through npm repository (https://www.npmjs.com/package/sails-hook-blueprint-count) and you can install it with

npm install sails-hook-blueprint-count

command.

Then, when you lift sails app you can use routes like

GET /:model/count

or

GET /:model/count?where={:criteria}

:criteria is the same like in find where blueprint methods (http://sailsjs.org/documentation/reference/blueprint-api/find-where).

Response will be json with format

{ count : COUNT }

Upvotes: 0

Eugene Obrezkov
Eugene Obrezkov

Reputation: 2986

We've implemented custom hook for this purpose. You can add it to your api/hooks folder.

/**
 * Adds support for count blueprint and binds :model/count route for each RESTful model.
 */

import _ from 'lodash';
import actionUtil from 'sails/lib/hooks/blueprints/actionUtil';
import pluralize from 'pluralize';

const defaultCountBlueprint = (req, res) => {
  let Model = actionUtil.parseModel(req);
  let countQuery = Model.count();

  countQuery.then(count => res.ok({count}));
};

export default function (sails) {
  return {
    initialize: cb => {
      let config = sails.config.blueprints;
      let countFn = _.get(sails.middleware, 'blueprints.count') || defaultCountBlueprint;

      sails.on('router:before', () => {
        _.forEach(sails.models, model => {
          let controller = sails.middleware.controllers[model.identity];

          if (!controller) return;

          let baseRoute = [config.prefix, model.identity].join('/');

          if (config.pluralize && _.get(controller, '_config.pluralize', true)) {
            baseRoute = pluralize(baseRoute);
          }

          let route = baseRoute + '/count';

          sails.router.bind(route, countFn, null, {controller: model.identity});
        });

      });

      cb();
    }
  }
};

Upvotes: 3

Related Questions