TypeSafe way to query data from SqlLite?

Thanks @hyphenrf, just beat me to the punch! :smile:

Yep, I have made a new type-safe SQLite and Postgres OCaml library Petrol (announcement, repo).

The constraint is that you have to declare your tables in OCaml directl[1], but using this, we can get truly ‘Type safe’ queries (as @yawaramin alludes to), with your query types being constrained to be correct w.r.t your tables by construction:

(* create a query *)
let insert_person ~name:n ~age:a db =
    Query.insert ~table:example_table
        ~values:Expr.[
            name := s n;
            age := i a
         ]
    |> Request.make_zero
    |> Petrol.exec db

As for whether it works for real world projects, I’ve been using it to run an activitypub server for almost a month now, and things are going great!

[1] (although means you get access to migration support for free)

3 Likes