How to use ppx syntax for PGOCaml in ocsigen?

Hello!
I am using ocsgigen-start and I try to run following code with ppx PGOCaml syntax:

let%server () = Eliom_registration.Action.register
    ~service:post_info
    (fun () info ->
       let%lwt () =
         let dbh = PGOCaml.connect () in
         let get_lastname firstname =
           [%pgsql dbh "SELECT lastname FROM users WHERE firstname = $firstname"] in
         let firstname = "a" in
         Printf.printf "%s's lastname is %s\n" firstname (get_lastname firstname);
         PGOCaml.close(dbh) in
       Lwt.return ()
    )

But unfortunately, pgsql syntax does not seem to be recognized during the build, as states:

File "demo_form.eliom", line 86, characters 13-18:
Error: Uninterpreted extension 'pgsql'.
Makefile.os:213: recipe for target '_server/demo_form.type_mli' failed
make: *** [_server/demo_form.type_mli] Error 2

I find it strange because following line is included in Makefile.options:

SERVER_DB_PACKAGES    := pgocaml pgocaml.ppx

How do you think I can fix it ?
Thanks a lot for your help :slight_smile:

Hello! Cool to see people checking out Ocsigen :smiley:
It seems pgocaml changed the name of the ppx package. We have pgocaml_ppx in our SERVER_DB_PACKAGES here, and it works fine.

name of the opam file in here: https://github.com/darioteixeira/pgocaml

We ought to patch the distillery template now. Thanks for finding that out :slight_smile:

EDIT: template is actually up to date. What’s your version of ocsigen-start?

4 Likes

I even consider to use it in production :wink: (I hope I won’t have to much devops troubles with it.)

Thanks a lot for your help ! This was not the right tip to go through because I am working on version 2.4 of ocsigen-start (I tried and the package pgocaml_ppx was not recognized).
In fact the problem was that ppx syntax extension of pg_ocaml is handled by the Makefile only for *_db.ml files. So I just wrote a file with such extension to define my function that makes a db request, and I imported this function. Here is a working code:

(* inside a demo_form_db.ml file *)
open Os_db
let get_lastname firstname =
  full_transaction_block (fun dbh ->
    [%pgsql dbh "SELECT lastname FROM ocsigen_start.users WHERE firstname = $firstname"])

(* inside a demo_form.eliom file *)
let%server () = Eliom_registration.Action.register
    ~service:post_info
    (fun () info ->
       let firstname = "a" in
       let%lwt l = (Demo_form_db.get_lastname firstname) in
       let lastname = List.hd l in
       Lwt_unix.write_string
         Lwt_unix.stdout
         ("firstname: "^firstname^"; lastname: "^lastname)
         0
         (String.length ("firstname: "^firstname^"; lastname: "^lastname))
       ;
       Lwt.return ()
    )

That is nice that there is a new release, but, in my experience, it is really difficult to install ocsigen-start (it involves a lot of debug). I just tried to opam install it on a fresh 4.07.1 switch and it was a failure. I think the only way for reliantly and repeatably install successfully ocsigen-start is to import a working existing config for now. (By the way I would appreciate that one shares a working config of this new release with me :wink: ) I hope this will be simpler (or at least that a procedure that does not imply so much debug is available) in the future because I like a lot this framework :slight_smile: Using OCaml for both back-end and front-end is so nice !