Curious to hear what Elixir users think of my library Typegres - the magic of Ecto, reimagined for Typescript and Postgres

Hey everyone – after coming off of 3 years building with Ecto, I’ve been working non-stop on Typegres, a new query builder that strongly aligns with Ecto’s philosophy around composabilty & purity.

The core, novel idea: write class methods that compile directly to composable SQL (try it in the playground)

// Extend your models
class Post extends posts.asClass<Post>() {
  // by writing methods
  trendingScore() {
    return this.likes.divide(this.ageInHours().plus(2)); // → SQL, not JS!
  }
}
// and use them anywhere in your queries:
await Post.select((p) => ({
  content: p.content,
  trending: p.trendingScore(),
}))
  .orderBy((p) => p.trendingScore()) // Everything composes into a single query, just like Ecto

What you’ll appreciate coming from Ecto:

  1. Composability: Everything is composable SQL expressions (and since it’s Typescript, it’s fully typed too)
  2. Purity: no N+1 queries or obscure magic – just a translation layer meant to maximize the synergy between TypeScript and Postgres

What makes Typegres different:

  1. True OOP in SQL: Encapsulate business logic in methods (post.trendingScore()) instead of scattered helper functions (and relations and graph-based querying naturally follow as a result of the composability). This a huge step towards resolving the object-relational impedance mismatch
  2. Postgres, Postgres, Postgres: coverage for all 3000+ built-in functions/operators without SQL strings (with coverage for all statements in the works)

Github:

As a huge admirer of Ecto, I’d love to hear what this community thinks. Does this approach to unifying objects and SQL resonate with you?

2 Likes

Very cool! I think Ecto gets a lot right, so I’m glad to see others working on the problem from that direction rather than ORMs.

Probably there won’t be a ton of traction on this forum about the OOP aspects of Typegres since Elixir isn’t object-oriented. But since you brought up Ecto, how does Typgres compare to Linq?

From what I understand, the Ecto developers drew inspiration from Linq. And I believe Linq was originally for C#, an OOP language, so there may be more overlap in terms of design goals.

2 Likes

Thanks! I didn’t know about LINQ inspiring Ecto, it definitely is an inspiration for Typegres.

The design goals of Typegres and LINQ are similar in the sense of providing a query builder that feels idiomatic in the host language (Typescript/C# respectively). In fact the fluent API looks pretty similar in both (similar to pipe-based query building – e.g., |> where in Ecto)

The similarities mostly end there though. LINQ was designed to be generic over arbitrary datasources (e.g., in memory, xml files, etc.) Typegres is designed to feel like you’re importing 100% of Postgres, with additional idiomatic TypeScript on top.

Since you know Ecto well - are there specific features or patterns from it that you think are essential to get right or additional avenues you’d think are worth exploring?