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

Derived state #688

Open
codingnuclei opened this issue Mar 2, 2024 · 5 comments
Open

Derived state #688

codingnuclei opened this issue Mar 2, 2024 · 5 comments

Comments

@codingnuclei
Copy link

codingnuclei commented Mar 2, 2024

Hi,

Is it possible to have an entity with derived state?

i.e. if an item in a table has the attributes: oneStar twoStar threeStar fourStar fiveStar. When i do a entity.get() (once has fetch the item) a new attribute rating is calculated based off the numbers stored in the attributes previously mentioned. This derived state should not be saved/exist in the table.

I have played around with default and format etc but have had no joy

@naorpeled
Copy link
Collaborator

naorpeled commented Mar 3, 2024

Hey @codingnuclei,
first of all, amazing initiative.

This is something that we've discussed in the past but haven't reached a final verdict on.

@jeremydaly @ThomasAribart wdyt?

@codingnuclei
Copy link
Author

Hey - so I have added quite a few things now to: #689

I think it gives a real flavour of what I am proposing. It is pretty non-intrusive with patterns and existing code.

Would really value some feedback as to whether this is something the team wants within the toolbox.

😄

@jeremydaly
Copy link
Owner

This should be possible with format. It receives both the value of the attribute AND the entire object. You should be able to conditionally return based off of other attributes.

@codingnuclei
Copy link
Author

Hey - yea i was initially hoping to use the format functionality but wasn't able to get it working.

From what I can see, the formatItem function loops over the response from the DB and matches the response fields against the entity schema:

return Object.keys(item).reduce((acc, field) => {

const transformedValue =
attributes[field] && attributes[field].format
? (attributes[field] as Required<PureAttributeDefinition>).format(fieldValue, item)

So my understanding is that if the field is not in the response Item then what is declared in the entity makes no difference...

Hoping I am missing something :)

@codingnuclei
Copy link
Author

@jeremydaly any thoughts on the above? 😀

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