BeanieODM/beanie

[BUG] before_event of type Update

RempelOliveira opened this issue · 7 comments

Describe the bug

When using "before event" of type "Updated", the values ​​of the fields in the "self" variable aren't the values ​​passed in the request, they are the values ​​readed from the database.

To Reproduce

Database Records:
[{
   "_id": "123",
    "name": "Name 01"
}]
db_record = await Model.find_one(Model.id == "123")
await db_record.set({"name": "Name 02"})

class Model(Document):
    name: str

    @before_event(Update)
    def check_activation(self) -> "Model":
        return self  # self.name will be the value "Name 01" instead of "Name 02"

Expected behavior

My understanding is that this event serves to manipulate the input data before being inserted into the database, however, only when using the "Replace" type is it possible to have access to them, but I wouldn't like to use replace because I just need to update a few fields, not all the collection.

Additional context

My understanding is that this event serves to manipulate the input data before being inserted into the database, however, only when using the "Replace" type is it possible to have access to them, but I wouldn't like to use replace because I just need to update a few fields, not all the collection.

Using this event with "Insert" I can also access the request data, I can't only using "Update" ;/

Am I wrong about how I see this feature or is it a bug?
If it is the expected behavior, how can I do something like the example provided?

I just realized that this event can't save changes as well.

Hi @RempelOliveira ,
This event can be used for some other things, like calling api before update and something like this. It can not change the update query itself and doesn't know the state of the object after query be applied

Hi @roman-right, thanks for answering my question.

In my opinion, if the Insert/Replace event can access the data sent in the request and can change them before creating the record in the database, the Update event should also. This implementation is very partial if it was only designed to allow doing any something else not related to the operation itself. In fact, events of this type in other ODMs usually allow us to do exactly what I described.

The documentation itself suggests that these events are used to change data before saving or updating it.

https://beanie-odm.dev/tutorial/event-based-actions/

🤯 I just also ran into this issue. I'm fairly sure this worked on a previous version; did something change?

This issue is stale because it has been open 30 days with no activity.

This issue was closed because it has been stalled for 14 days with no activity.