loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Model.prototype.unsetAttribute not workin in "before save" operation hook

Open Tuetuopay opened this issue 9 years ago • 5 comments

Issue

While trying to restrict the writes to some properties of a model, I lost a few days pulling my hair out on the reference way of doing it : the unsetAttribute method.

Per the docs, to restrict writes on a property, you define an operation hook on your model for the before save hook, then you either

  • delete the property in ctx.data (if present). This is mostly for updates as only some properties might get rewritten
  • Call ctx.instance.unsetAttribute(property) when a full-model save is performed (create or replace)

However, the second one does not works (at least for the create method). The model returned through the REST call is OK, where the property is absent. However, the one saved in the datasource (PostgreSQL for me) still have those fields filled.

Workaround

The only way I could acheive it was by doing ctx.instance[property] = null, in spite of the documentation stating it is not enough.

Sample code :

Model.observe("before save", (ctx, next) => {
    let property = "something";

    // Partial update, works
    if (ctx.data && ctx.data.hasOwnProperty(property))
        // Use current value. ORM fails when nothing is in data
        ctx.data[property] = ctx.currentInstance[property];
    // Full update (create or replace), does not works
    else
        // Unset the property
        ctx.instance.unsetAttribute(property);
    next();
});

This would NOT remove the property in the saved model, only in the one returned by the API when POSTing to create it. Moreover, it may be incorrect by ignoring any default value set in the model.

Another working replacement taking default values in account is : ctx.instance[property] = Model.definition.properties[property].hasOwnProperty("default") ? Model.definition.properties[property].default : null;

Bottomline

Either the docs are broken about the use of unsetAttribute, or the implementation of unsetAttribute is.

PS : you can also see the delete ctx.data[property] is not bulletproof neither.

Tuetuopay avatar Jun 04 '17 01:06 Tuetuopay

@Tuetuopay In your "before save" callback, do you see both ctx.data and ctx.instance?

The example code piece in the LoopBack documentation (below) it checks ctx.instance first, then handle ctx.data. In your code, ctx.data case first. Would that make any difference?

https://loopback.io/doc/en/lb3/Operation-hooks.html

MyModel.observe('before save', function removeUnwantedField(ctx, next) {
  if (ctx.instance) {
    ctx.instance.unsetAttribute('unwantedField');
  } else {
    delete ctx.data.unwantedField;
  }
  next();
});

Setogit avatar Jun 05 '17 20:06 Setogit

@Setogit I do not see it.

I don't think it would make any difference as it could not event branch to the faulty part if it was (my sample code is not 100% accurate as the hasOwnProperty is deeper in the first if). Moreover, data is not even supposed to be present in the operation I spotted the problem in (POST to list, aka create) as, according to the docs, data and instance are exclusive :

instance

This property is provided when the operation affects a single instance and performs a full update/create/delete of all model properties

where + data

When the operation affects multiple instances (e.g. PersistedModel.updateAll()) or performs a partial update of a subset of model properties

Thanks for your time !

Tuetuopay avatar Jun 06 '17 00:06 Tuetuopay

Thanks, @Tuetuopay . Always good to check the fact. :-)

The source of unsetAttribute impl: https://github.com/strongloop/loopback-datasource-juggler/blob/master/lib/dao.js#L2923 ===> Is that what you see in your environment? Might be interesting to put console.log to see which branch it goes in this 5-line function.

The source of unsetAttribute test: https://github.com/strongloop/loopback-datasource-juggler/blob/master/test/datatype.test.js#L229 ===> It's a good concrete example to understand what's expected. From the test code, it seems in your case that the property something is set to null by the unsetAttribute. Do you see it in your environment, or the property something is set to non-null value?

Setogit avatar Jun 06 '17 19:06 Setogit

Please submit a new issue if needed. closing one month or older issues. thanks!

Setogit avatar Jul 21 '17 20:07 Setogit

@Setogit How to use it for nested properties, for example,

ctx.instance ={
  foo: {
   name: 'something',
   unwantedProperty: 'aa'
  },
 bar: [{
  name: 'prop 1',
  unwantedProperty: '22'
 }]
},

here I want to delete `unwantedProperty` in `foo` object and `bar` array.

narenderv7 avatar Apr 26 '19 13:04 narenderv7