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.open_table "user_table"

Then we can easily handle the user management code:

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

let () = Eliom_registration.Action.register
  ~service:create_account_service
  (fun () (name, pwd) -> Ocsipersist.add user_table name pwd)

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