diff --git a/example/h-sql/README.md b/example/h-sql/README.md index aab16638..8d36c439 100644 --- a/example/h-sql/README.md +++ b/example/h-sql/README.md @@ -7,27 +7,29 @@ prepared statements using [Caqti](https://paurkedal.github.io/ocaml-caqti/caqti/Caqti_connect_sig/module-type-S/module-type-CONNECTION/index.html), a library for talking to SQL databases: +> [!TIP] +> `let%lwt` is syntactic sugar for `Lwt.bind`, so the two forms below are equivalent (a third option would be using `>>=`). +> +> For more information, see [here](https://ocsigen.org/lwt/latest/api/Lwt#3_Callbacks). + ```ocaml module type DB = Caqti_lwt.CONNECTION module T = Caqti_type -let list_comments = +let list_comments (module Db : DB) = let query = let open Caqti_request.Infix in - (T.unit ->* T.(tup2 int string)) + (T.unit ->* T.(t2 int string)) "SELECT id, text FROM comment" in - fun (module Db : DB) -> - let%lwt comments_or_error = Db.collect_list query () in - Caqti_lwt.or_fail comments_or_error + Lwt.bind (Db.collect_list query ()) Caqti_lwt.or_fail -let add_comment = +let add_comment text (module Db : DB) = let query = let open Caqti_request.Infix in (T.string ->. T.unit) "INSERT INTO comment (text) VALUES ($1)" in - fun text (module Db : DB) -> - let%lwt unit_or_error = Db.exec query text in - Caqti_lwt.or_fail unit_or_error + let%lwt unit_or_error = Db.exec query text in + Caqti_lwt.or_fail unit_or_error let render comments request = diff --git a/example/h-sql/sql.eml.ml b/example/h-sql/sql.eml.ml index c542c4d8..66ecc1da 100644 --- a/example/h-sql/sql.eml.ml +++ b/example/h-sql/sql.eml.ml @@ -1,23 +1,20 @@ module type DB = Caqti_lwt.CONNECTION module T = Caqti_type -let list_comments = +let list_comments (module Db : DB) = let query = let open Caqti_request.Infix in (T.unit ->* T.(t2 int string)) "SELECT id, text FROM comment" in - fun (module Db : DB) -> - let%lwt comments_or_error = Db.collect_list query () in - Caqti_lwt.or_fail comments_or_error + Lwt.bind (Db.collect_list query ()) Caqti_lwt.or_fail -let add_comment = +let add_comment text (module Db : DB) = let query = let open Caqti_request.Infix in (T.string ->. T.unit) "INSERT INTO comment (text) VALUES ($1)" in - fun text (module Db : DB) -> - let%lwt unit_or_error = Db.exec query text in - Caqti_lwt.or_fail unit_or_error + let%lwt unit_or_error = Db.exec query text in + Caqti_lwt.or_fail unit_or_error let render comments request =