2

I am using Objectionjs

In my Model, I have defined a password field.

I want to prevent this field from showing up for all queries - select, inserts, updates etc - that is, after making a query, I don't want this field to be present in the object returned

Is there a method on the model itself that I can use to do this or do I need to omit the field in each query that I make?

callmekatootie
  • 10,989
  • 15
  • 69
  • 104

2 Answers2

3

You can for example override $afterInsert, $afterUpdate, $afterGet and make sure that fields are deleted in those hooks.

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

If you just don't like that some fields are show when you are converting model to json, for example for returning model to client, then overriding $formatJson might be enough.

Mikael Lepistö
  • 18,909
  • 3
  • 68
  • 70
2

To elaborate a little bit more, if your are using $formatJson in your Model:

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']);
}

Will do it.

My 2cs.

cass
  • 1,286
  • 14
  • 13