32 lines
878 B
OCaml
32 lines
878 B
OCaml
|
type user = { id : int; name : string }
|
||
|
|
||
|
let conn_str = "sqlite3:./data.db"
|
||
|
|
||
|
module type DB = Caqti_lwt.CONNECTION
|
||
|
|
||
|
let get_all_users_q = Caqti_request.Infix()
|
||
|
|
||
|
let pool =
|
||
|
let uri = Uri.of_string conn_str in
|
||
|
match Caqti_lwt.connect_pool ~max_size:10 uri with
|
||
|
| Ok pool -> pool
|
||
|
| Error err -> failwith (Caqti_error.show err)
|
||
|
|
||
|
let get_users =
|
||
|
let get_users' (module C: Caqti_lwt.CONNECTION) =
|
||
|
C.fold get_all_users_q
|
||
|
|
||
|
let find_users =
|
||
|
let open Lwt_result.Infix in
|
||
|
fun (module Db : Caqti_lwt.CONNECTION) ->
|
||
|
Db.collect_list find_users_request () >>| List.map make_user
|
||
|
|
||
|
let () =
|
||
|
let uri = Uri.of_string conn_str in
|
||
|
match Lwt_main.run (Caqti_lwt.connect uri >>= find_users) with
|
||
|
| Ok users ->
|
||
|
List.iter (fun user -> Printf.printf "%d: %s\n" user.id user.name) users
|
||
|
| Error err ->
|
||
|
Format.eprintf "%a@." Caqti_error.pp err
|
||
|
|