haskell-opaleye icon indicating copy to clipboard operation
haskell-opaleye copied to clipboard

Documentation on db types to haskell types convertion

Open matsumonkie opened this issue 9 years ago • 5 comments

First thing first, thanks a lot for this awesome project ! I'm in the process the try to understand the source as I find it really interresting.

As a Haskell beginner, I sometime found a bit hard to use Opaleye. Issue #197 is something I have also ran into. I thought it could be nice to add some documentation on how to handle convertions.

What do you think ?

matsumonkie avatar Sep 10 '16 07:09 matsumonkie

Why does one need to derive FromField AND QueryRunnerColumnDefault? Looking at the API surface, it seems that the latter has all the information required to teach Opaleye how to convert a PGText to Gender:

instance QueryRunnerColumnDefault PGText Gender where

Also, what about the other way round? How does Opaleye know how to convert a Gender to PGText (or whatever else raw SQL is going to accept)?

saurabhnanda avatar Sep 10 '16 19:09 saurabhnanda

Also, I'm not clear how PGText came into the picture? In case of an ENUM datatype, wouldn't Postgres send it as a completely new type, altogether?

saurabhnanda avatar Sep 10 '16 19:09 saurabhnanda

Also, a link to https://hackage.haskell.org/package/postgresql-simple-0.5.2.1/docs/Database-PostgreSQL-Simple-FromField.html in the documentation might be a good idea. It contains a more detailed example of how to handle parsing failures as well.

saurabhnanda avatar Sep 10 '16 19:09 saurabhnanda

Also, I'm not clear how PGText came into the picture? In case of an ENUM datatype, wouldn't Postgres send it as a completely new type, altogether?

I think Postgres handle an enum type internally as an Int but present it as a string. Your enum type should probably as a PGString, not sure though.

Also, a link to https://hackage.haskell.org/package/postgresql-simple-0.5.2.1/docs/Database PostgreSQL-Simple-FromField.html in the documentation might be a good idea. It contains a more detailed example of how to handle parsing failures as well.

Yeah you're right, I have changed the PR. Thanks

Also, what about the other way round? How does Opaleye know how to convert a Gender to PGText (or whatever else raw SQL is going to accept)?

I don't know yet, still learning Opaleye :-)

matsumonkie avatar Sep 12 '16 04:09 matsumonkie

Thanks very much for this. I don't really have time at the moment to handle all the inbound Opaleye requests I'm getting but I will get to this sooner or later!

tomjaguarpaw avatar Sep 12 '16 17:09 tomjaguarpaw