Warning: Reason support is experimental. We are looking for beta-tester and contributors.

Lightweight database using Ocsipersist

Ocsipersist is a module for persistent references and tables.

For persistent references, Eliom has a higher level interface, called Eliom references, and you probably want to use it instead of using Ocsipersist directly. For example persistent Eliom references of scope session group can be used to store information about a user (if the session group corresponds to the user).

Ocsipersist can still be useful for creating persistent key-value tables, if you do not need the full power of a SQL database.

This tutorial shows how to implement a table for storing users and passwords.

We first create a table:

let user_table = Ocsipersist.Polymorphic.open_table "user_table"

Then we can easily handle the user management code:

let check_pwd name pwd =
    lwt saved_password = Ocsipersist.Polymorphic.find user_table name in
    Lwt.return (pwd = saved_password)
    Not_found -> Lwt.return false

let () = Eliom_registration.Action.register
  (fun () (name, pwd) -> Ocsipersist.Polymorphic.add user_table name pwd)

let () = Eliom_registration.Action.register
  (fun () (name, password) ->
    match%lwt check_pwd name password with
      | true -> Eliom_reference.set username (Some name)
      | false -> Lwt.return ())