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

Feature/derived state #689

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

codingnuclei
Copy link

@codingnuclei codingnuclei commented Mar 2, 2024

Related to: #688

This is a RFC PR showcasing a possible enhancement to entities.

It enables an entity schema to define attributes which are derived.
It follows similar patterns already used by this library.

One thing to note is that the derived value is evaluated after all the other attributes have been formatted.

Usage:

const rating = (store: any) => {
  return 'some calculated value'
};

export const Store = new Entity({
  name: 'Store',
  attributes: {
    // .... removed for brevity
    oneStars: { type: 'number' },
    twoStars: { type: 'number' },
    threeStars: { type: 'number' },
    fourStars: { type: 'number' },
    fiveStars: { type: 'number' },
    rating: {
      derive: rating,
    }
  },
  table: MyTable,
} as const);

Future considerations:

  • I have not looked at testing/docs/types in this RFC but based on feedback will make this a full fledge PR ready for merging.
  • derived state which depends on other derived state
  • ignore derived state fields in put/update request

@codingnuclei codingnuclei mentioned this pull request Mar 9, 2024
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

Successfully merging this pull request may close these issues.

None yet

1 participant