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

Proposal: Type bound functions #101

Open
haikyuu opened this issue Dec 5, 2024 · 0 comments
Open

Proposal: Type bound functions #101

haikyuu opened this issue Dec 5, 2024 · 0 comments

Comments

@haikyuu
Copy link

haikyuu commented Dec 5, 2024

With the advent of functions with insert/update/delete, it's very tempting to be able to define functions on types themselves.
Schema

type Movie {
  title: str;
  multi actors: Actor;
  multi reviews: Review;
  getWithRating: function() {
    select Movie {
      title,
      actors: {
        name
      },
      rating := math::mean(.reviews.score)
    } 
  }
}

And then:

with m := (select Movie limit 1), select m.withRating()

I know it's possible to do a similar behavior by passing a movie as the first parameter. But it's extremely convenient this way. And it would enable moving more of the business logic to the database with a minimal cognitive overhead.

If a schema has 40 types and each type has 2-3 functions:

  • Currently, a developer would have to browse through 80-120 functions to find one to use.
  • But with this, he wouldn't have to think about it. Plus it would be possible to get autocomplete in queries and client libraries ...

I don't have any stats, but I don't think people define functions all that much either in Postgres nor other databases. Maybe because ORMs don't expose that in a nice way, or maybe they'd rather implement stuff in the programming language.

I think this would be aligned with EdgeDB because it enables offloading more to the database while preserving programming language ergonomics.

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

1 participant