如何防止字段显示
How to prevent a field from showing up
我正在使用 Objectionjs
在我的模型中,我定义了一个密码字段。
我想阻止此字段出现在所有查询中 - select、插入、更新等 - 也就是说,在进行查询后,我不希望此字段出现在对象中返回
我可以使用模型本身的方法来执行此操作,还是我需要在我进行的每个查询中省略该字段?
例如,您可以覆盖 $afterInsert
、$afterUpdate
、$afterGet
并确保在这些挂钩中删除字段。
http://vincit.github.io/objection.js/#_s_afterinsert
如果您只是不喜欢在将模型转换为 json 时显示某些字段,例如将模型返回给客户端,那么覆盖 $formatJson
可能就足够了。
更详细一点,如果您在模型中使用 $formatJson
:
const _ = require('lodash');
// ...
$formatJson(jsonRaw) {
// Remember to call the super class's implementation.
const json = super.$formatJson(jsonRaw);
// Do your conversion here.
return _.pick(json, ['id', 'email']);
}
会做的。
我的2cs.
我正在使用 Objectionjs
在我的模型中,我定义了一个密码字段。
我想阻止此字段出现在所有查询中 - select、插入、更新等 - 也就是说,在进行查询后,我不希望此字段出现在对象中返回
我可以使用模型本身的方法来执行此操作,还是我需要在我进行的每个查询中省略该字段?
例如,您可以覆盖 $afterInsert
、$afterUpdate
、$afterGet
并确保在这些挂钩中删除字段。
http://vincit.github.io/objection.js/#_s_afterinsert
如果您只是不喜欢在将模型转换为 json 时显示某些字段,例如将模型返回给客户端,那么覆盖 $formatJson
可能就足够了。
更详细一点,如果您在模型中使用 $formatJson
:
const _ = require('lodash');
// ...
$formatJson(jsonRaw) {
// Remember to call the super class's implementation.
const json = super.$formatJson(jsonRaw);
// Do your conversion here.
return _.pick(json, ['id', 'email']);
}
会做的。
我的2cs.