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_defPrefer 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 -> tableval table_name : table -> stringtype fk_action =
| Cascade
| Restrict
| SetNull
| SetDefault
| NoActiontype 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_refand default_expr =
| Raw of string
| Current_timestamp
| Null
| Bool of bool
| Int of int
| Int64 of int64
| Float of float
| Text of stringtype 'a column = {
col_name : string;
col_type : 'a Types.t;
col_constraints : column_constraint list;
}type wrapped_column =
| Column : 'a column -> wrapped_columntype 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 listval add_opt : 'a option -> ('a -> 'b) -> 'b list -> 'b listval default_expr_of_args :
default_expr:default_expr option ->
default:string option ->
default_expr optionval 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 columnval references :
?on_delete:fk_action option ->
?on_update:fk_action option ->
table:string ->
column:string ->
unit ->
foreign_key_refval define : ?schema:string option -> string -> 'a column list -> table_defval with_primary_key : string list -> table_def -> table_defval with_unique : string list -> table_def -> table_defval with_check : ?name:string -> string -> table_def -> table_defval id_column : unit -> int64 columnval timestamps : unit -> Ptime.t column listval has_constraint : column_constraint -> 'a column -> boolval is_primary_key : 'a column -> boolval is_not_null : 'a column -> boolval is_unique : 'a column -> boolval render_default_expr : dialect:Driver.dialect -> default_expr -> stringval get_default_dialect : dialect:Driver.dialect -> 'a column -> string optionval get_default : 'a column -> string optionval get_foreign_key : 'a column -> foreign_key_ref optionval fk_action_to_sql : fk_action -> stringval constraint_to_sql_dialect :
dialect:Driver.dialect ->
column_constraint ->
stringval constraint_to_sql : column_constraint -> stringval column_to_sql_dialect : dialect:Driver.dialect -> wrapped_column -> stringval column_to_sql : wrapped_column -> stringval table_def_to_sql_dialect : dialect:Driver.dialect -> table_def -> stringval table_def_to_sql : table_def -> string