Repodb.Types
repodb · API reference
type _ t =
| Int : int t
| Int32 : int32 t
| Int64 : int64 t
| Float : float t
| String : string t
| Bool : bool t
| Blob : string t
| Ptime : Ptime.t t
| Pdate : Ptime.date t
| Uuid : Uuidm.t t
| Json : string t
| Option : 'a t -> 'a option t
| Array : 'a t -> 'a list t
| Tup2 : 'a t * 'b t -> ('a * 'b) t
| Tup3 : 'a t * 'b t * 'c t -> ('a * 'b * 'c) t
| Tup4 : 'a t * 'b t * 'c t * 'd t -> ('a * 'b * 'c * 'd) t
| Custom : {
encode : 'a -> (string, string) result;
decode : string -> ('a, string) result;
sql_type : string;
} -> 'a tTyped descriptions of OCaml values stored in SQL columns and parameters.
('a t) is Repodb's runtime witness for the OCaml type 'a. Fields, schema columns, expressions, query parameters, and result conversions all use these witnesses to keep database values and OCaml values aligned.
Use the predefined witnesses for ordinary columns:
let id = Types.int64
let email = Types.string
let published_at = Types.option Types.ptime
let tags = Types.array Types.stringThe same witness drives dialect-specific DDL rendering and conversion to and from Driver.Value.t:
Types.sql_type_name_for_dialect Repodb.Driver.PostgreSQL Types.uuid
(* "UUID" *)
let value = Types.to_value Types.bool true
let decoded = Types.of_value Types.bool valueOption ty marks a nullable value. Array ty maps to native arrays only on dialects that support them; SQLite and MySQL render arrays using their fallback SQL type mapping. Json stores JSON text from OCaml's point of view while dialect renderers choose JSONB, JSON, or TEXT.
Use custom for small domain types that have a stable string representation:
type status = Draft | Published
let status =
Types.custom
~sql_type:"TEXT"
~encode:(function Draft -> Ok "draft" | Published -> Ok "published")
~decode:(function
| "draft" -> Ok Draft
| "published" -> Ok Published
| other -> Error ("unknown status: " ^ other))Custom encoders are used for parameters and custom decoders are used when converting driver values back to domain values. Keep custom encodings stable because they become persisted data.
val int : int tval int32 : int32 tval int64 : int64 tval float : float tval string : string tval bool : bool tval blob : string tval ptime : Ptime.t tval pdate : Ptime.date tval uuid : Uuidm.t tval json : string tval option : 'a t -> 'a option tval array : 'a t -> 'a list tval tup2 : 'a t -> 'b t -> ('a * 'b) tval tup3 : 'a t -> 'b t -> 'c t -> ('a * 'b * 'c) tval tup4 : 'a t -> 'b t -> 'c t -> 'd t -> ('a * 'b * 'c * 'd) tval custom :
encode:('a -> (string, string) result) ->
decode:(string -> ('a, string) result) ->
sql_type:string ->
'a tval (**) : 'a t -> 'b t -> ('a * 'b) ttype sql_type_names = {
int : string;
int32 : string;
int64 : string;
float : string;
string : string;
bool : string;
blob : string;
ptime : string;
pdate : string;
uuid : string;
json : string;
array : string option;
tuple : string;
}val postgres_sql_type_names : sql_type_namesval sqlite_sql_type_names : sql_type_namesval mysql_sql_type_names : sql_type_namesval sql_type_name_with : 'a. sql_type_names -> 'a t -> stringval sql_type_name : 'a t -> stringval sql_type_name_for_sqlite : 'a t -> stringval sql_type_name_for_mysql : 'a t -> stringval sql_type_name_for_dialect : 'a. Driver.dialect -> 'a t -> stringval is_nullable : 'a. 'a t -> boolval to_value : 'a. 'a t -> 'a -> Driver.Value.tval of_value : 'a. 'a t -> Driver.Value.t -> ('a, string) result