How to add context to a Sequelize hook?

▼魔方 西西 提交于 2019-12-13 15:23:11

问题


I want to use a hook to perform access control on a row so that users can only access rows they own. The hook would add an additional where clause to ensure that the user is the owner of the row (ex: WHERE ownerId=user.id).

The problem is, how do I pass the user id to Sequelize so that the hook has it when the hook fires? I've looked through the documentation and it's not obvious to me if this is possible.

Thanks

reference: http://sequelize.readthedocs.org/en/latest/docs/hooks/


回答1:


To add context you can include whatever you like in the options object. For example let's say you are using Model.findAll(). You would set up your options like this:

options = {
  attributes: ['id', 'column1', 'column2'],
  where: { someColumn: 'aValue' }
}

options.context = 'foo';

Then in the hook you'll be able to access options.context and use it however you like. Of course you probably should not use any of the "reserved" words used by Sequelize to filter queries.



来源:https://stackoverflow.com/questions/28449896/how-to-add-context-to-a-sequelize-hook

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!