BTC
ETH
SOL
BNB
GOLD
XRP
DOGE
ADA
Back to home
Tech

My 14-Year Journey Away from ORMs – a Series of Insights Leading to Creation of a SQL-First Code Generator

Nikita Volkov spent 14 years wrestling with database abstractions before concluding that PostgreSQL itself should drive your code.

Nikita Volkov spent 14 years wrestling with database abstractions before concluding that PostgreSQL itself should drive your code. He open-sourced pGenie, a code generator that reads your Postgres schema and SQL queries to spit out type-safe Haskell code. No ORMs, no manual type definitions, no schema drift surprises. This matters because in production systems—think PostgREST or IHP—mismatched models cause outages, slow queries, and endless debugging.

Start with the problem: ORMs promise safety but deliver fragility. Volkov built SORM in 2012, a Scala ORM treating database rows as immutable values. It gained users fast in the pre-migration era of SSH deploys and git pulls. But complex queries broke the DSL. Syncing code models with evolving schemas turned into a nightmare. By 2014, he halted development; by 2016, he axed support entirely. Key lesson: Reinventing SQL in a “safer” DSL just complicates life. Databases handle complexity better than abstractions.

Shift to Raw SQL: hasql and Beyond

Volkov pivoted to enhancing SQL integration. In 2014, he released hasql, a Haskell PostgreSQL driver built around raw SQL strings. It provides composable parameter and result handling without hiding the database. Today, it’s one of two dominant Postgres drivers in Haskell, powering major projects. Benchmarks show it outperforms alternatives in throughput and latency—critical for high-load apps where every millisecond counts.

Insight here: Treat the query, not the app model like “User”, as the integration point. Each query defines its own params and returns; reusing row types across queries hides dependencies and invites breakage. This query-centric view cut coupling, but gaps remained. Developers still wrote custom codecs by hand, and SQL lacked schema validation.

In 2019, hasql-th arrived: a compile-time checker porting PostgreSQL’s parser to Haskell. It catches syntax errors before runtime. Still, it ignored the actual schema. Attempts at compile-time DB connections emerged elsewhere, but they demand live databases during builds—unreproducible, flaky, and a security risk in CI/CD pipelines.

pGenie: Database as Source of Truth

pGenie solves this by generating code offline from your Postgres schema dump and SQL files. Dump the schema with pg_dump --schema-only, point pGenie at it plus your .sql queries, and it emits Haskell modules with precise types, encoders, decoders, and session combinators. Queries validate against the real schema at generation time. Change the DB? Regenerate. No drift.

Why this beats ORMs: ORMs generate runtime queries from code, leading to N+1 problems, injection risks if leaky, and “works on my machine” schema mismatches. pGenie flips it—code derives from DB, ensuring fidelity. In Haskell’s type system, this yields zero-cost abstractions: full compile-time safety without runtime overhead. Volkov’s 25 years, including “late-night schema-drift fires,” inform this. It’s battle-tested in his consulting.

Skepticism check: Code generators aren’t magic. Regeneration workflows need discipline—integrate into Makefiles or Nix for reproducibility. Not Haskell-only; similar tools exist (e.g., sqlc for Go/Rust), but pGenie’s deep Postgres/Haskell integration shines. For non-Haskell stacks, adapt the idea: prioritize schema dumps in version control.

Implications for devs: In security-sensitive or finance apps, where audits demand traceability, SQL-first reduces attack surface—no opaque ORM magic. Scale matters too; raw SQL with types handles millions of rows without ORM bloat. Volkov’s arc mirrors industry: From ORM hype (Hibernate, ActiveRecord) to pragmatic SQL (via drivers like pgx, libpq). pGenie lowers the bar for Haskell-Postgres mastery. Grab it on GitHub; test with a schema dump. If it sticks, your deploys get safer, faster.

April 15, 2026 · 3 min · 5 views · Source: Lobsters

Related