Awk script to generate types for SQL rows with ppx_rapper

Heya!

I found it very tedious to write out the types when using the record_out function with ppx_rapper, and using tuples was very error prone. So I wrote a small script to generate the types for me in awk, so if you have:

let my_query =
  [%rapper
    get_opt
      {sql|
      SELECT @int{id}, @string{username}, @bool{following}, @string?{bio}
      FROM users
      WHERE username <> %string{wrong_user} AND id > %int{min_id}
      |sql} record_out]

it can be used to generate the record type:

type my_query_result = 
 { id : int
 ; username : string
 ; following : bool
 ; bio : string option
 }

Which ppx_rapper will then automatically use.

I use it together with the Kakoune editor, so you can see how I use it in practice in this recording.

The full script and explanation for how to use it can be found here. Feel free to use it however you’d like.

1 Like