Repodb.Schema

repodb · API reference

type table = {
  name : string;
  schema : string option;
}

Table and column definitions used to generate portable DDL.

Schema is for describing the database shape, not for decoding rows. A table definition contains a table name, typed columns, column constraints, and table-level constraints. Use it in migrations, DDL generation, tests, and documentation of your model modules.

A typical table definition:

let users = Schema.table "users"

  let users_def =
    Schema.define "users"
      [ Schema.id_column ();
        Schema.column "email" Types.string ~not_null:true ~unique:true;
        Schema.column "name" Types.string ~not_null:true;
        Schema.column "team_id" Types.int64
          ~references:
            (Schema.references ~table:"teams" ~column:"id"
               ~on_delete:(Some Schema.Cascade) ())
      ]
    |> Schema.with_check ~name:"users_email_nonempty" "length(email) > 0"

Render with the generic SQL dialect or choose a concrete backend:

Schema.table_def_to_sql_dialect
    ~dialect:Repodb.Driver.PostgreSQL users_def

Prefer default_expr constructors such as Current_timestamp, Text, and Bool over raw strings when possible. The Raw constructor and ~default argument are escape hatches for backend-specific expressions; keep them close to migrations where backend assumptions are visible.

val table : ?schema:string option -> string -> table
val table_name : table -> string
type fk_action = 
  | Cascade
  | Restrict
  | SetNull
  | SetDefault
  | NoAction
type foreign_key_ref = {
  fk_table : string;
  fk_column : string;
  fk_on_delete : fk_action option;
  fk_on_update : fk_action option;
}
type column_constraint = 
  | PrimaryKey
  | NotNull
  | Unique
  | Default of default_expr
  | Check of string
  | ForeignKey of foreign_key_ref
and default_expr = 
  | Raw of string
  | Current_timestamp
  | Null
  | Bool of bool
  | Int of int
  | Int64 of int64
  | Float of float
  | Text of string
type 'a column = {
  col_name : string;
  col_type : 'a Types.t;
  col_constraints : column_constraint list;
}
type wrapped_column = 
  | Column : 'a column -> wrapped_column
type table_def = {
  tbl_table : table;
  tbl_columns : wrapped_column list;
  tbl_primary_key : string list option;
  tbl_unique : string list list;
  tbl_checks : (string option * string) list;
}
val add_if : bool -> 'a -> 'a list -> 'a list
val add_opt : 'a option -> ('a -> 'b) -> 'b list -> 'b list
val default_expr_of_args : 
  default_expr:default_expr option ->
  default:string option ->
  default_expr option
val column : 
  ?primary_key:bool ->
  ?not_null:bool ->
  ?unique:bool ->
  ?default:string ->
  ?default_expr:default_expr ->
  ?check:string ->
  ?references:foreign_key_ref ->
  string ->
  'a Types.t ->
  'a column
val references : 
  ?on_delete:fk_action option ->
  ?on_update:fk_action option ->
  table:string ->
  column:string ->
  unit ->
  foreign_key_ref
val define : ?schema:string option -> string -> 'a column list -> table_def
val with_primary_key : string list -> table_def -> table_def
val with_unique : string list -> table_def -> table_def
val with_check : ?name:string -> string -> table_def -> table_def
val id_column : unit -> int64 column
val timestamps : unit -> Ptime.t column list
val has_constraint : column_constraint -> 'a column -> bool
val is_primary_key : 'a column -> bool
val is_not_null : 'a column -> bool
val is_unique : 'a column -> bool
val render_default_expr : dialect:Driver.dialect -> default_expr -> string
val get_default_dialect : dialect:Driver.dialect -> 'a column -> string option
val get_default : 'a column -> string option
val get_foreign_key : 'a column -> foreign_key_ref option
val fk_action_to_sql : fk_action -> string
val constraint_to_sql_dialect : 
  dialect:Driver.dialect ->
  column_constraint ->
  string
val constraint_to_sql : column_constraint -> string
val column_to_sql_dialect : dialect:Driver.dialect -> wrapped_column -> string
val column_to_sql : wrapped_column -> string
val table_def_to_sql_dialect : dialect:Driver.dialect -> table_def -> string
val table_def_to_sql : table_def -> string