Boris Zagoruiko
Boris Zagoruiko

Reputation: 13174

How to extend waterline with custom query methods?

I want to create and use some custom query methods for waterline.

For example method makeItAwesome:

Article.makeItAwesome({
    id: 42
}).exec(function(err, awesomeArticle) {
    /* ... */
});

Of course I can add this makeItAwesome to model's static methods but what if I want common stuff reusable for other models. Does sails.js or waterline by itself have built-in ways of doing it or I should do it by myself?

If not where is the best way to keep this stuff? Must it be a separate module to require in each model or I can somewhere extend each model prototype once?

Upvotes: 2

Views: 1073

Answers (1)

Meeker
Meeker

Reputation: 5979

You can put methods you want on every model in a common js file and use lodash to extend your models with these defaults. Or simply reference the imported actions when defining your model.

module.exports = _.extends(makeItAwsomeDefinition, { thisModelDefinition })

-- OR --

You can add default methods to your config/models.js file

-- OR --

If your just using blueprints, you can create custom actions and put them in api/blueprints

-- OR --

A simpler method I might use, is to create a service and put my custom methods in there. ModelMethodsService.makeItAwsome('MODELNAME',{structOfQueryParams})

References

Upvotes: 2

Related Questions