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

Attribute Watching: subtract() results in not passing the new value to the setter #453

Open
julian99m opened this issue Nov 29, 2024 · 3 comments

Comments

@julian99m
Copy link

Describe the bug
When calling the subtract() method on a watched attribute, the setter of the watching attribute will not be called with the new value, but rather with the value that was subtracted from the original value.
Example: original value is 5, subtract method is called with parameter 2, the setter will not be called with 3 (new value), but with 2 (parameter of the subtract-call).

ElectroDB Version
3.0.1

ElectroDB Playground Link
ElectroDB Playground
(The behavior can be observed in the resulting update operation)

Entity/Service Definitions
see Playground

Expected behavior
Without further explanation in the docs, I would expect the new value (3) to be passed to the setter.
If the observed behavior is correct, it should be explicitly mentioned in the docs. But as the operation is not given in the setter callback, the provided value can not be interpreted and thus holds no valuable information, so I think the behavior should be changed, if possible.

@tywalch
Copy link
Owner

tywalch commented Dec 1, 2024

Hey @julian99m 👋

Great question, this could definitely use more explanation on the docs!

Without further explanation in the docs, I would expect the new value (3) to be passed to the setter.

This is fair to assume, though unfortunately not possible with a single request to DynamoDB. The setter is called prior to your mutation request to DynamoDB, so ElectroDB won't have access to the result of the subtraction (or addition) operation.

But as the operation is not given in the setter callback

I was actually surprised to see the counter value in the second argument, I had only expected that value to be there if it was called with a set operation. Let me do some research into how that is implemented.

Let me know if this raises any additional questions you have in the meantime 👍

@tywalch
Copy link
Owner

tywalch commented Dec 8, 2024

I looked into the scope of work it would take to add operation context to the values passed to set; unfortunately, it is a lot. It has been a while since I dug into that area of the library, but it appears to supply all values associated with the mutation consistently (bittersweet).

To address the titular concern, I want to reiterate that any value passed to an attribute setter is a value that was provided to the operation, not the values stored in the database. To do this, a write-after-read operation would be necessary, which is also not the type of operation ElectroDB would perform because it is unsafe.

@julian99m
Copy link
Author

Hi @tywalch,
thanks for the clarification and for doing the research 😄
I understand the technical limitation that is associated to my "desired" behavior, but I would still suggest that a more explicit description of the provided value (especially for the case of add/subtract/append/.... operations, in contrast to the set operation) should be added to the documentation.

Thanks! :)

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

2 participants