After reading the post by @bobbypriambodo , I would like to create a Caqti_async version:
open Async
let new_url = "postgresql://dbuser:password@127.0.0.1:5432/exampledb"
let pool =
match Caqti_async.connect_pool ~max_size:10 (Uri.of_string new_url) with
| Ok pool -> pool
| Error err -> failwith (Caqti_error.show err)
type todo = { id : int; content : string }
type error = Database_error of string
let or_error m =
match%bind m with
| Ok a -> Ok a |> return
| Error e -> Error (Database_error (Caqti_error.show e)) |> return
let migrate_query =
Caqti_request.exec Caqti_type.unit
{|CREATE TABLE todos (
id SERIAL NOT NULL PRIMARY KEY,
content VARCHAR
)|}
let migrate () =
let migrate' (module C : Caqti_async.CONNECTION) = C.exec migrate_query () in
Caqti_async.Pool.use migrate' pool |> or_error
Along with dune:
(library
(name lib)
(libraries core async caqti caqti-async caqti-driver-postgresql)
(preprocess
(pps ppx_let ppx_jane)))
And mli file
open Async
type todo = {
id: int;
content: string;
}
type error=
| Database_error of string
val migrate : unit -> (unit, error) result Deferred.t
However, I got stuck on the utop:
$ dune utop lib
# open Lib;;
# Todos.migrate ();;
(*stuck here, not any response....*)
I’ve tried the Lwt version, it works well with the same postgresql://
schema. Just wondering if there anything that I missed in my code?