[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

I know this post is old, but I’m glad I found it. I was actively searching for something like this and had a really hard time finding it!

Do we have any resources/docs available that point out stable, well maintained packages in our ecosystem for trying to build different things… i.e. a solid back end set of dependencies?

Yeah, this is definitely something we should look into. Maybe we should have a sort of community blog? The big thing here is to get search engines to pick up the libs we have and their usages. Might be good to have a community blog where we detail various use cases and approaches, so that gets a bit of search engine cover.

This is one of those things that it’d be fantastic if someone would be willing to contribute to. Write a series of blog posts together with various authors of useful tools and libs in the community where you basically introduce and advertise that tool/lib.

1 Like