Chris Chong
Chris Chong

Reputation: 371

Baqend onUpdate Handler

Will doing partialupdate() cause code in a data class' onUpdate Handler to run?

I have this setup in the data class:

exports.onUpdate = function(db, obj) {
  DB.log.info(obj.ShiftID);
  db.Shifts.load(obj.ShiftID)
  .then((Shift) => {
    DB.log.info(Shift);
    if (Shift.User == db.User.me) {
      Shift.User = null;
      Shift.status = 0;
      return Shift.update();
    }
  })
};

(yes, role 2 for node has permissions to query and update the Shifts data class)

But I am getting zero logs when I make a partialupdate(). Do I need to do a real update query...load the object, modify the data, update()?

Also it seems that this code causes the partialupdate() to not run at all, but when I delete the handler, it starts working again.

Upvotes: 1

Views: 67

Answers (2)

Florian Bücklers
Florian Bücklers

Reputation: 176

Yes, that is currently an unimplemented feature since a partial update can't execute an onUpdate handler since there is no object which can be passed to the update handler.

On the other hand, a partial update can't be executed directly since that will result in a security issue (since your onUpdate handler can contain validation code etc.)

So we currently reject any partial update on a class which has an onUpdate handler because there doesn't exist a way how we can actually validate the partial update against your onUpdate code.

We have planned that you can define an extra onPartial handler where you can take some extra steps before the partialUpdate is executed. But that handler will only get the partial update and not the object itself.

Upvotes: 1

Chris Chong
Chris Chong

Reputation: 371

I'm pretty sure that partialupdate() will not cause the onUpdate Handler to run.

When I put the log line in and edit the records using website data manager it does log as expected. Not a big deal, I can just rewrite the query to be a full update.

BUT having any code in there does break partialupdate() which is not good.

Here is the code I'm using that works as long as there is nothing in the onUpdateHandler:

  requestShift(shiftID) {
    db.ready().then((db) => {
      db.Applicants.find()
      .where({
         "shiftID": { "$in": [shiftID] },
      })
      .singleResult((applicants) => {
        return applicants.partialUpdate()
        .add("applicants", db.User.me.id)
        .add("photos", this.props.UserData.photo)
        .execute()
      })
      Alert.alert(
        'Confirmation',
        'Shift has been requested.',
      )
      this.props.navigation.dispatch(goToFindShifts)
    })
  }

Upvotes: 0

Related Questions