[ANN] Petrol 1.0.0 - A high-level typed SQL API for OCaml designed to go fast!

Heyo! Pleased to annouce the release of a new library that I’ve been working on for some personal projects, but thought might be useful for others.

Petrol is a Free software library that provides a high-level OCaml API for interacting with SQL databases. The aim of this interface is to provide a type-safe API to allow developers to define their SQL tables and queries directly in OCaml, thereby avoiding the impedence mismatch and fragility that comes with having to directly write SQL code, as is typical in a normal Caqti-based project.

open Petrol

(* define a new schema *)
let schema = StaticSchema.init ()

(* declare a table *)
let example_table, Expr.[name; age] =
    StaticSchema.declare_table schema ~name:"example"
        field "name" ~ty:Type.text;
        field "age" ~ty:Type.int

Petrol’s DSL allows you to express complex SQL queries as simple OCaml
function compositions:

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

Another selling point of Petrol is that it comes out of the box with a versioning system to gracefully allow changing the schemas in your application over time - this and more is described in the documentation.

Anyway, as usual, I’ve tried to spend some quality time tuning the documentation to make it as easy to pick up as possible, so feel free to have a gander: index (petrol.index)


If anyone’s wondering, the name is a reference to Rust’s excellent Diesel library

1 Like

As someone just starting to explore OCaml and, in particular, from the web development side, I’d like to say thank you so much for publishing this. There’s definitely a gap here in the ecosystem. I hope to play around with it very soon.

1 Like

Hooray for new ocaml sql apis! Thanks @Gopiandcode


Looking great! I’ll give it a try :+1:

1 Like