30

According to this closed issue in sails: https://github.com/balderdashy/sails/issues/835

CRUD Blueprint Overrides "absolutely, this is coming in v0.10"

I'd like to modify the blueprints in my sailsjs service to allow named roots (consuming in ember). Currently I'm having to customize every controller I create with actions that are largely duplicates of what is already in the blueprints.

I suspect that I can move this code out of my controllers now and into a blueprints override area, but I'm not clear on where to put that code.

Any examples or even just a pointer to the relevant code in sails the .10 repo would be greatly appreciated.

Travis Webb
  • 14,688
  • 7
  • 55
  • 109
davepreston
  • 1,260
  • 1
  • 10
  • 20

3 Answers3

41

Update

In order to override blueprints in Sails 1.0 in the manner described below, you must first install the "custom blueprints" plugin for your project (npm install sails-hook-custom-blueprints).


To override blueprints in Sails v0.10, you create an api/blueprints folder and add your blueprint files (e.g. find.js, create.js, etc.) within. You can take a look at the code for the default actions in the Sails blueprints hook for a head start.

Adding custom blueprints is also supported, but they currently do not get bound to routes automatically. If you create a /blueprints/foo.js file, you can bind a route to it in your /config/routes.js file with (for example):

'GET /myRoute': {blueprint: 'foo'}
sgress454
  • 24,870
  • 4
  • 74
  • 92
  • 1
    Thank you for such a quick response. Just to clarify. Since I want to keep most of the current functionality I copied the entire find.js out of the hooks code (and also needed the ActionUtil.js file because of the dependency). – davepreston Mar 08 '14 at 20:23
  • Sounds like a good plan. The ActionUtil file just includes a helper function to populate all of your associations; if you'd rather return slimmer objects from your blueprints you can leave it out! – sgress454 Mar 08 '14 at 20:55
  • These files are not on my node-modules/sails. Also how do i make a new blueprint for routes?It should be on bootstrap or there is a better way to do it? – FabioCosta Mar 14 '14 at 16:34
  • Sorry, blueprint overriding is a v0.10 feature only. You can install the newest version with `npm install -g sails@beta`, and there's a migration guide [here](https://github.com/balderdashy/sails-docs/blob/master/Migration-Guide.md). Also updated this answer with info about custom blueprint support. – sgress454 Mar 14 '14 at 17:17
  • This answer is correct, however, I did find I needed some additional detail to implement. Specifically, I had to understand that my model had to be passed in the hash, and that I could also pass any other options I needed to be available to my blueprint, as such: 'GET /myRoute': {blueprint: 'foo', model: 'modelNameInQuotes-lowercase', opt1: 'foo', ...} – Morris Singer May 01 '14 at 16:43
  • Is this still true in 1.0? I have added find.js under api/blueprints but the build in sails blueprint find still gets called. – pondermatic Sep 13 '18 at 01:51
  • @pondermatic see edit above -- you need to install a hook for it to work in 1.0. – sgress454 Sep 21 '18 at 17:03
  • Thanks, yeah, finally figured that out and ended up going with express-mung. – pondermatic Sep 22 '18 at 18:12
11

you can add actions with these names inside your controller to override default behaviour

to change destroy behavior

module.exports = {
  destroy: function(req,res){
    Goal.update({ id: req.param('id') }, { deleted: true })
    .exec(function (err, goal) {
            if (err) return res.json(err, 400);
            return res.json(goal[0]);
     });
  }
}
Nour Sammour
  • 2,822
  • 1
  • 20
  • 19
  • 2
    Is it possible to wrap default behavior with controller-specific logic? I want to return status 202 rather than 200 in certain circumstances for a particular resource on create. – shaunc Sep 20 '15 at 13:09
  • 1
    yes you can ``` return res.send(202,{ message:'foo '}) ``` – Nour Sammour Sep 20 '15 at 21:55
  • 1
    Thanks! ... hmm... what I mean by default behavior is "call the blueprint" -- in my case I'm using 'sails-generate-ember-blueprints' so the blueprint needs to be called. Now that I consider -- the problem is that the blueprint actually calls send, so I'd need to proxy the response object I think (grumble...). – shaunc Sep 20 '15 at 22:24
  • @shaunc did you actually solve your problem ? How did you call the default blueprint action and modify the answer ? – Sw0ut Jan 01 '19 at 09:53
  • @SwOut ... I think its possible but I'm not using sails anymore -- sorry. – shaunc Jan 03 '19 at 16:10
2

It is possible to use the build in blueprints, but with policies running first. These policies might verify that the user is logged in, has the correct access, or similar. Really handy!

On each model, you have available callbacks both before and after data has been stored. Dig in: http://sailsjs.com/documentation/concepts/models-and-orm/lifecycle-callbacks

There is no default callback available for blueprints result. But don't give up. It is still possible to use the build in blueprints, and only modify the output. It might not be the most elegant solution, but it works well. Check out my “hack” here: Sails blueprints lifecycle

Community
  • 1
  • 1
qualbeen
  • 1,534
  • 4
  • 16
  • 27