[Haskell-cafe] ORM for haskell?

Chris Eidhof chris at eidhof.nl
Fri Jul 3 06:51:20 EDT 2009

On 3 jul 2009, at 11:28, Jochem Berndsen wrote:

> Chris Eidhof wrote:
>> I've something working that sort of does this. You define your  
>> model in
>> the following way:
>> data User = User {name :: String, password :: String, age :: Int,  
>> post
>> :: BelongsTo Post}
>> data Post = Post {title :: String, body :: String}
>> Then there's some boilerplate code (that ultimately will be  
>> generated by
>> TH), and from that moment on you can do things like this:
>> test = do
>>  conn <- connectSqlite3 "example.sqlite3"
>>  runDB conn $ do
>>    user <- fromJust <$> find typeUser 1
>>    user' <- fillBelongsTo user relPost
>>    return (post user')
>> By default, no relations will be fetched, but by doing the  
>> fillBelongsTo
>> the user will be updated. I currently have support for new, update  
>> and
>> find. All of this code is very alpha, and only works using HDBC and
>> Sqlite3, but still.
> So in this example, both user and user' are of type User, but if I ask
> for "post user", this is undefined?
> I have done something similar as you, except that I filled the related
> field with an unsafePerformIO fetching the related data from the  
> database.

No, it will never be undefined. The BelongsTo datatype is defined as  

data BelongsTo a = BTNotFetched | BTId Int | BTFetched (Int, a)
So either there is no information available (BTNotFetched), we know  
the id (the foreign key, BTId) or we know the id and the value  
(BTFetched). It is currently just a proof of concept, but for me, an  
extend version of this will do. I think that almost every mapping from  
Haskell datatypes to a RDBMS will be slightly awkward, this is my way  
to find a balance ;)


More information about the Haskell-Cafe mailing list