Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] before_event of type Update #913

Open
RempelOliveira opened this issue Apr 10, 2024 · 4 comments
Open

[BUG] before_event of type Update #913

RempelOliveira opened this issue Apr 10, 2024 · 4 comments

Comments

@RempelOliveira
Copy link

RempelOliveira commented Apr 10, 2024

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?

@RempelOliveira
Copy link
Author

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

@roman-right
Copy link
Member

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

@RempelOliveira
Copy link
Author

RempelOliveira commented Apr 18, 2024

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/

@gegnew
Copy link

gegnew commented May 13, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants