Skip to content

Proposal: Type bound functions #101

@haikyuu

Description

@haikyuu

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions