[ANN] Type safe PostgreSQL in ReScript through pgtyped-rescript

Introducing pgtyped-rescript - type safe SQL in ReScript.

I’ve recently spent some time porting pgtyped to emit ReScript instead of TypeScript. This means you can write (Posgres)SQL queries in SQL, and execute those queries with full type safety through ReScript.

Here’s a readme that shows examples and how to get started.

And here’s a quick example of how it looks:
books.sql

/* @name findBookById */
SELECT * FROM books WHERE id = :id!;

pgtyped-rescript will generate a books__sql.res file from that. That file will contain a function to execute the query:

let res = await Books__sql.findBookById({id: 1}, ~client)

This will be fully typed - params, and results.

First release

Please don’t hesitate to add your feedback about this first release to the issue tracker.

Future

There are plenty of interesting things to explore here. For example, this first release supports standalone .sql files for type generation. Upcoming releases will figure out some sort of “SQL-in-ReScript”, likely similar to:

let findBookById = %sql(`
  /* @name findBookById */
  SELECT * FROM books WHERE id = :id!;
`)

let res = await findBookById({id: 1}, ~client)

Interested in any thoughts and any feedback. Thanks!

18 Likes

It’s what I was looking for for a long time :fire: I’m amazed by your productivity :grin:

1 Like

And I like that you’ve chosen sql as a source of truth

3 Likes

Wow. I wonder how much it depends on Postgre flavor of .sql, i.e., can it be used, with, say, SQLite.

EDIT: It seems not, ’cause pgtyped relies on db engine to infer types from queries

1 Like

Yes, exactly, this is Postgres only. But there are other similar tools for other databases that one could probably port if needed.

2 Likes