roddyyaga / ppx_rapper

Syntax extension for writing SQL in OCaml
Other
139 stars 18 forks source link

insert and return id #7

Closed mudrz closed 3 years ago

mudrz commented 3 years ago

hey @roddyyaga , I can't understand how to simply insert a record and get the resulting id

module User = struct
  type t = { name: string; address: string }
end
    [%rapper execute {sql|
        INSERT INTO public.users(name, address)
        VALUES (
          %string{name},
          %string{address}
        )
        RETURNING @int{id}
     |sql} record_in]

but it seems that the resulting type is incorrect - I get Type unit is not compatible with type int

also is there a way to omit the types after % and @ when using record, since those are already defined in the records themselves?

roddyyaga commented 3 years ago

The problem there is that you're using execute, which is for queries that don't return a result. You should use get_one instead.

There's no way to omit the types currently and I think it would be difficult to implement something to do that. One problem is that the information is flowing the other way: it's that the preprocessor generates a function that expects a record with structure { name: string; address: string} and your type has that structure, rather than generating a function that takes the User type specifically.

mudrz commented 3 years ago

this worked, thanks Roddy I'll close the issue, I guess for the automated fields parsing to work the record would need to be a part of the ppx