Repodb.Repo

repodb · API reference

type 'a result = ('a, Error.db_error) result

Repository operations over a concrete database driver.

Repo.Make(D) turns a Driver.S implementation into high-level database functions: CRUD helpers, typed query execution, preloading, DDL helpers, and transactions. Applications normally define one repo module per backend:

module Repo = Repodb.Repo.Make (Repodb_sqlite.Driver)

Decoding is explicit and local to the caller:

let decode_user row =
    { User.id = Driver.row_int64 row "id";
      email = Driver.row_text row "email";
      name = Driver.row_text row "name" }

  let list_users conn =
    Query.from User.table
    |> Query.order_by Expr.(column User.email)
    |> Repo.all_query conn ~decode:decode_user

All public operations return ('a, Error.db_error) result. Driver errors are normalized where Repodb can recognize them; for example, PostgreSQL and SQLite constraint failures become Error.db_error.Constraint_violation. Use validate_changeset before writes when accepting external input.

transaction conn f runs f on the same connection and commits only when f returns Ok _. For multi-step writes where later operations depend on earlier results, see Multi; for pooled access, combine a repo with Pool.Make or Cqrs.Make.

val parse_pg_constraint_error : 
  string ->
  [> `ForeignKey of string | `Unique of string ] option
val parse_sqlite_constraint_error : String.t -> (string * string) option
val driver_error_to_db_error : 
  dialect:Driver.dialect ->
  string ->
  Error.db_error
module IntMap : sig ... end
module type REPO = sig ... end
val validate_changeset : 'a Changeset.t -> ('a, Error.db_error) result
val build_insert_sql : 
  placeholder:(int -> string) ->
  Schema.table ->
  string list ->
  string
val build_insert_returning_sql : 
  placeholder:(int -> string) ->
  Schema.table ->
  string list ->
  string
val build_update_sql : 
  placeholder:(int -> string) ->
  Schema.table ->
  string list ->
  where_column:string ->
  string
val build_delete_sql : 
  placeholder:(int -> string) ->
  Schema.table ->
  where_column:string ->
  string
val build_select_sql : 
  placeholder:(int -> string) ->
  Schema.table ->
  columns:string list ->
  where_column:string ->
  string
val build_select_all_sql : Schema.table -> columns:string list -> string
val build_preload_sql : 
  placeholder:(int -> string) ->
  related_table:string ->
  fk_column:string ->
  n_ids:int ->
  string
module Make (D : Driver.S) : sig ... end