DeepHorus is an innovative tech media and intelligence platform focused on AI, blockchain, startups, and emerging technologies.
Published on

Typegres 0.3 – SQL-as-your-API, safely (using Cap'n Web RPC)

Authors
  • avatar
    Name
    DeepHorus
    Twitter

Typegres 0.3 has been released as a TypeScript API framework that lets clients compose SQL queries securely over RPC, using Cap'n Web RPC. The npm package aims to provide 'SQL-as-your-API' while keeping data boundaries intact. It is pre-1.0 and not yet recommended for production workloads, but a browser playground using PGlite is available.

Traditional API frameworks like Hasura and PostgREST auto-generate endpoints, but Typegres instead lets developers wrap tables in a stable public interface. This decoupling allows schema refactoring without breaking clients, as the interface remains constant. Queries are fully typed and compiled to a single SQL statement, preserving the efficiency of hand-written SQL.

The framework enforces data encapsulation by defining allowed operations as methods on the interface. For example, a User table can expose a todos() method that only returns todos for that user. Mutations are also restricted to hydrated instances, ensuring clients cannot bypass business logic.

Over RPC, Typegres exposes a server-validated entry point, such as forToken(token), which clients can compose against. Client queries cross the wire via Cap'n Web, where the server validates them against the exposed surface. This allows clients to use rich Postgres features like ilike and window functions without shipping new endpoints.

Unlike ORMs like Prisma or Drizzle, which are local development tools, Typegres is designed for remote database access. It can be used alongside an ORM or as a standalone API layer. The project supports Postgres and SQLite, with code-generated surfaces from each engine's catalog and functions, avoiding a lowest-common-denominator abstraction.

The framework also addresses tRPC's limitation: while tRPC requires predefined procedures, Typegres pushes composition to the server, letting clients ask new questions without new endpoints. This makes it suited for applications with evolving data needs.

Typegres 0.3 is still in early development, but its approach signals a shift toward safer, more flexible database APIs. The project's use of Cap'n Web RPC and support for SQLite via Cloudflare Durable Objects highlights its modern edge. For developers, it promises a future where APIs stay stable while client queries remain unbounded.