paurkedal / ocaml-caqti

Cooperative-threaded access to relational data
https://paurkedal.github.io/ocaml-caqti/index.html
GNU Lesser General Public License v3.0
299 stars 36 forks source link

Declaring types that have many columns? #96

Closed rawleyfowler closed 1 year ago

rawleyfowler commented 1 year ago

Thank you for this amazing package. I am following the example in the examples directory, but I cannot figure out how to create a type that doesn't fall within tup2 tup3 or tup4 for example if I had a type:

module Post = struct
  type t = {
    uri : string;
    title : string;
    content : string;
    created_at : Ptime.t;
    updated_at : Ptime.t
  }
end

How can I use this type with Caqti, if the type didn't have created_at or updated_at I could write it like:

let post =
  let open Post in
  let encode {uri; title; description} = Ok (uri, title, description) in
  let decode (uri, title, description) = Ok {uri; title; description} in
  let _t = Caqti_type.(tup3 string string string) in
  custom ~encode ~decode _t

How can I declare a record type that is larger than tup4?

anmonteiro commented 1 year ago

The tupN combinators can be nested. See https://github.com/paurkedal/ocaml-caqti/issues/47#issuecomment-704650179

paurkedal commented 1 year ago

Yes, nesting is the only way to handle more than 4 columns with the current interface.

rawleyfowler commented 1 year ago

Okay, that makes sense. So basically:

let _t = Caqti_type.(tup2 (tup3 string string string) (tup2 ptime ptime))

Thanks!