Anyone working on ways to have type-safety across the database layer, ideally Postgres?
If not, for the most part, I can query the metadata in Postgres for tables, columns, and whatnot. But I’ve never written a codegen library.
Does someone have advice on how I can best generate Rescript?
- It’d be great if we can write SQL, but writing a parser seems tough. But perhaps I can leverage an existing tool that parses it into a transferable data medium.
- Rescript records for tables
I know it’s not the answer you’re looking for (I’d be interested in having a tool like that too), but you can use Postgraphile to have a type safe access to a postgres DB through graphQL.
Thank you! True, the graphql route is very much type safe. I thought Postgraphile became Prisma. Interesting to see it adopted stil.
What tools are you using to connect to your graphql server?
You could check out the sqlgg tool, which does this for OCaml: https://github.com/ygrek/sqlgg
Porting it to ReScript should be possible. Note the license is GPLv2, but that shouldn’t really be an issue, because it’s just used to generate code, it’s not linked as a library in your app.
Since postgraphile is relay compatible, I use rescript-relay, it works quite well!
I recently bumped into this ocaml library that does precisely this, it should probably be doable to adapt it to rescript too.
I hardly know ppx development. But how do you propose someone going about this?
Because I like sql and how prevalent it is, I do like pgocaml’s choice. Ideal would be something like https://sqlc.dev/ where I can write sql, the compiler turns it into a nice little rescript function for me.
sqlgg looks viable too. just no energy yet :[
I don’t know, I have to take a deeper look at the implementation, but it would bring us a significant value at work so we might consider working on that conversion.
Alternatively, someone can have https://sqlc.dev/ output rescript. The typescript generator is still pending. Works great in Go tho.
I’m working on https://github.com/rpominov/typesafe-sql , but not sure I’ll finish it anytime soon.
Does anybody has prototypes with ReScript code generation?
Looks like @rpominov ’s working on something.
I particularly like what https://www.safeql.dev/guide/introduction.html is doing.
Safeql is a vscode plugin that checks your sql against the database. Most if not all of these implementations rely on Postgres, and at compile time (or whenever the vscode plugin runs), it runs a Describe query for each sql against the db for validation.
I’m working at a typesafe query builder written for and in rescript.
It will be a combination of a codegen (for the basic schema) and a “real” api.
As it is still very experimental, there is no real readme and a lot of code is commented out but you can already get a short overview how it might work by reading the example code.
I really like the look of sqlgg generated code https://ygrek.org/p/sqlgg/demo/demo_caml_gen.ml. I think I’ll try to make it work with ReScript, even though I have zero experience with ocaml ecosystem.
Sitting on this idea inspired by safeql, maybe it’s possible to fork and adapt the rescript vscode into similar fashion.
You inform safeql what functions are called with the sql query and the database it’ll use. The plugin can execute the queries (with describe/explain analyze) and cache the results in the background. I can see some edge cases that makes it hard to differentiate which database to check against.
- Can we adapt the rescript vscode extension to do this? If I understand correctly, most extensions are only displaying and working with whatever is outputted by the language server.
- The ergonomics of using tagged template literals is immensely positive. Rescript would need to support this. See my rfc
Hmm, might be easier to forego the linter style and use a decorator on plain sql functions like gentype.
Extensions can do pretty much anything, so setting up your own extension to try things shouldn’t be that hard. But in general, make it work as a CLI or similar first. Then integrating into an extension is a bit easier.
Hoped you would chime in, thanks @zth!