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

Allow Relations in Model Extension needs Dependencies #20211

Closed
defrex opened this issue Jul 13, 2023 · 1 comment
Closed

Allow Relations in Model Extension needs Dependencies #20211

defrex opened this issue Jul 13, 2023 · 1 comment

Comments

@defrex
Copy link

defrex commented Jul 13, 2023

Problem

The new extensions feature is great! My team is already using it for a few things.

However, we'd love to add computed props based on relationships.

Here is a simple example. Given these models

model Account {
  id           Int           @id @default(autoincrement())
  transactions Transaction[]
}

model Transaction {
  id        Int      @id @default(autoincrement())
  amount    Int
  account   Account? @relation(fields: [accountId], references: [id])
  accountId Int?
}

I'd like to add balance as a computed property of Account

prisma.$extends({
  result: {
    account: {
      balance: {
        needs: {
          transactions: true, // <- Type 'boolean' is not assignable to type 'never'.
        },
        compute(account) {
          return sumBy(account.transactions, "amount");
        },
      },
    },
  },
});

Suggested solution

Allow requiring certain relations to be include'd in order to compute a prop.

Alternatives

The alternative I'm currently considering is overwriting query to dynamically add the fields (or maybe a wrapper-class) if the relations are present.

The problem with that approach is that the types won't be correct.

@SevInf
Copy link
Contributor

SevInf commented Jul 13, 2023

Hi @defrex!
Thank you for kind words and for opening this request. We plan to eventually implement it (sorry, no ETA yet) and we already have a separate issue for tracking that. I am going to close this as a duplicate. You can follow the progress in #20091

@SevInf SevInf closed this as completed Jul 13, 2023
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