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

Add a way to persist a calculation for a resource #383

Open
Dak425 opened this issue Sep 10, 2024 · 0 comments
Open

Add a way to persist a calculation for a resource #383

Dak425 opened this issue Sep 10, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@Dak425
Copy link

Dak425 commented Sep 10, 2024

Is your feature request related to a problem? Please describe.
When defining generated attributes for a resource, I often see people needing to manually edit the migration to use ALTER TABLE to explicitly add the column with the SQL statement used to populate the column.

Describe the solution you'd like
A way to in an Ash resource to define an attribute that is generated, specify the raw SQL statement to populate the field, and have the generated migration use the Ecto syntax for configuring such a column shown here

Describe alternatives you've considered
Continue manually changing migrations or replace generated attribute with calculations that are not persisted in a column

Express the feature either with a change to resource syntax, or with a change to the resource interface

Idea 1 - generate_with attribute option

Using calculation

  attributes do
    attribute :generated_foo, :string, generate_with: :full_name
  end

  calculations do
    calculate :full_name, :string, expr(first_name <> " " <> last_name)
  end

Using raw SQL

  attributes do
    attribute :generated_foo, :string, generate_with: expr(fragment("? || ' ' || ?", first_name, last_name))
  end

Idea 2 - persist calculation option

  calculations do
    calculate :full_name, :string, expr(first_name <> " " <> last_name)

    persist? true
  end

Additional context
Link to the start of the conversation in the Elixir Discord server that lead to this proposal: message

@Dak425 Dak425 added enhancement New feature or request needs review labels Sep 10, 2024
@Dak425 Dak425 changed the title Add an attribute option to make the attribute a persisted calculation in a sense Add a way to persist a calculation for a resource Sep 10, 2024
@zachdaniel zachdaniel moved this from Soon to Someday in Ash Framework Roadmap Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Someday
Development

No branches or pull requests

2 participants