Skip to content

Commit

Permalink
example/h-sql: move arguments to top
Browse files Browse the repository at this point in the history
IMO, this improves the examples by:

- making the arguments stand out more, by moving them to the top
- removing visual clutter from the promise section, which is the focus
  of the example
  • Loading branch information
asymmetric committed Dec 10, 2024
1 parent d44447d commit 21e2ea2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
14 changes: 6 additions & 8 deletions example/h-sql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,20 @@ a library for talking to SQL databases:
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) ->
Lwt.bind (Db.collect_list query ()) Caqti_lwt.or_fail
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 =
<html>
Expand Down
12 changes: 5 additions & 7 deletions example/h-sql/sql.eml.ml
Original file line number Diff line number Diff line change
@@ -1,22 +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) ->
Lwt.bind (Db.collect_list query ()) Caqti_lwt.or_fail
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 =
<html>
Expand Down

0 comments on commit 21e2ea2

Please sign in to comment.