Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sails.js: Lifecycle callbacks for Models: Do they support beforeFind and afterFind?

In sails.js, Models support lifecycle callbacks for validate, create, update and destroy.

Is there support for callbacks for find() or query as well? Like beforeFind() and afterFind()?

The idea is same. I would want to validate / modify parameters before the query is run or after the query is run.

Any thoughts?

like image 415
rsmoorthy Avatar asked Jan 27 '15 10:01

rsmoorthy


2 Answers

As of writing this it does NOT support these requests, however their is a pull request https://github.com/balderdashy/waterline/pull/525

You can use policies to do this in the mean time.

like image 147
Meeker Avatar answered Nov 10 '22 00:11

Meeker


i don't get why this was left out in the first place. It's actually logical to want to add some data to the fetched model data after each model find.

The closest thing to afterFind in the documentation as of writting is customToJson model setting.

customToJSON: function() { 
    // Return a shallow copy of this record with the password and ssn removed. 
    return _.omit(this, ['password', 'ssn']) 
}

you basically do your stuff before the return omit part. I still don't get why these lifecycles were omitted.

I think I am going to write a hook to provide these for now. I will post it here.

like image 35
Emmanuel Mahuni Avatar answered Nov 10 '22 01:11

Emmanuel Mahuni