loopbackjs - Is there a way to create a "global" hook that will fire regardless of the model requested -




i want able add row count every model have. know how add using remote or operational hook, far can tell have add code each model want use on. instead, want write 1 hook trigger regardless of model requested.

you can leverage mixin functionality.

in model-confg.js specify location of mixins:

{   "_meta": {     "mixins": [       "loopback/common/mixins",       "loopback/server/mixins",       "../common/mixins",       "./mixins"     ],     ...   },   ... } 

create mixin in designated mixins folder (e.g. server/mixins/<mixin-name>.js) :

module.exports = function(model, options) {   model.observe('before save', function event(ctx, next) {     // row count logic     next();   }); }; 

add mixin model:

{   "name": "mymodel",   "base": "persistedmodel",   "properties": {     ...   },   ...   "mixins": {      "mixinname": true    },   ... } 

another option extension of built-in persistedmodel. add hook , base models on extended persistedmodel.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -

python - Read npy file directly from S3 StreamingBody -