Hacker News new | ask | show | jobs
by guntars 1246 days ago
I would prefer 1. if it had full type safety. I’m imagining some sort of a build process that looks for any .sql files in the project and spits out .sql.d.ts in the same directory with the input and output types according to the current schema of the database. Another nice thing about a setup like this imo would be that the .sql files would have the full support of the editor for completions and execution, unlike sql fragments in a Typescript file.
4 comments

Slonik comes pretty close https://github.com/gajus/slonik. It does not inspect your DB schema, but has worked very well in our SQL-heavy architecture.
F# can do this, either as part of the compilation via type providers [1] or, in a more lightweight manner, via Roslyn analyzers embedded in the editor [2].

Unsurprisingly, Haskell can also do this via Template Haskell [3], but I haven't used it.

[1] https://github.com/demetrixbio/FSharp.Data.Npgsql

[2] https://github.com/Zaid-Ajaj/Npgsql.FSharp.Analyzer

[3] https://hackage.haskell.org/package/postgresql-typed

On a similar note, there's also sqlx for rust [1] - and I find it extra impressive that they manage to support not only postgres, but sqlite and and mysql too.

[1] https://github.com/launchbadge/sqlx

For Postgres there is https://github.com/adelsz/pgtyped, sounds pretty much like what you describe?
I wrote a thing that connects to your live database and then dumps out d.ts files.