Quickstart

This page walks you through connecting to Neo4j from OCaml and running your first query with the neodriver packages.

Prerequisites

Adding the driver to your project

Install the packages from opam:

opam install neodriver neodriver_eio

When developing against a local checkout of the driver, pin the packages instead:

cd ocaml-neo4j-driver
opam pin add neodriver_packstream .
opam pin add neodriver_core .
opam pin add neodriver_eio .
opam pin add neodriver .
opam install neodriver neodriver_eio eio_main

eio_main provides the Eio_main.run entry point used by the example below.

Declare the dependency in your executable's dune file:

(executable
 (name hello)
 (libraries neodriver neodriver_eio eio_main))

A minimal program

Save the following as hello.ml:

open Neodriver

let () =
  Eio_main.run (fun env ->
    let net = Eio.Stdenv.net env in
    let clock = Eio.Stdenv.mono_clock env in
    Eio.Switch.run (fun sw ->
      let session =
        match
          Driver.connect ~uri:"bolt://localhost:7687"
            ~auth:(Conn.basic_auth ~credentials:"your_password" ())
            net clock sw
        with
        | Ok session -> session
        | Error error -> failwith (Errors.to_string error)
      in
      (match Session.run session ~query:"RETURN 1 AS n" ~parameters:[] with
      | Ok result -> (
          match Neo4jResult.values result with
          | Ok [ [ Values.Int n ] ] -> Printf.printf "n = %Ld\n%!" n
          | _ -> ())
      | Error error -> failwith (Errors.to_string error));
      Session.close session))

Run it:

dune exec ./hello.exe

You should see n = 1.

What is going on

Next steps