mongoosejs / kareem

Next-generation take on pre/post function hooks
Apache License 2.0
82 stars 8 forks source link

Support deleting / setting to undefined variables in hooks #29

Open a3957273 opened 2 years ago

a3957273 commented 2 years ago
hooks.pre('cook', function () { new Promise(resolve => {
  this = undefined // obviously doesn't work
  resolve()
})})

function() {
  return new Promise(resolve => {
    resolve()
  });
});

var obj = { exists: true };

hooks.execPre('cook', obj, function(error) {
  assert.equal(null, error);
  assert.equal(undefined, obj);
});

At the moment you can alter objects passed through kareem hooks, but you're unable to remove them / set them to undefined. Is there any chance it could be changed so that we could? One suggestion would be to allow the returned value from the hook to be an array which match the arguments returned.

a3957273 commented 2 years ago

Our use case is in Mongoose, which allows you to set pre/post hooks on Database lookups (https://github.com/Automattic/mongoose/issues/11426). The intention is to do authorisation checks within Mongoose hooks in order to ensure users cannot see documents they are not supposed to even if app developers forget to do authorisation checks, as they're all done at the ORM level (https://github.com/gchq/Bailo/pull/37). We can already splice arrays to remove them from find() lookups, but find() return objects which we cannot set to undefined.