如何防止字段出现

时间:2017-11-10 18:20:32

标签: knex.js objection.js

我正在使用Objectionjs

在我的模型中,我定义了一个密码字段。

我想阻止此字段显示所有查询 - 选择,插入,更新等 - 也就是说,在进行查询后,我不希望此字段出现在返回的对象中

我可以使用模型本身的方法来执行此操作,还是需要省略每个查询中的字段?

2 个答案:

答案 0 :(得分:1)

您可以覆盖$afterInsert$afterUpdate$afterGet,并确保在这些挂钩中删除字段。

http://vincit.github.io/objection.js/#_s_afterinsert

如果您不喜欢在将模型转换为json时显示某些字段,例如将模型返回到客户端,则覆盖$formatJson可能就足够了。

答案 1 :(得分:0)

如果您在模型中使用$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。

相关问题