38

What is the best way to interact with a database using Haskell? I'm accustomed to using some sort of ORM (Django's ORM, hibernate, etc.) and something similar would be nice when creating apps with HAppS.

Edit: I'd like to be free to choose from Postgresql MySql and SQLite as far as the actual databases go.

Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
rcreswick
  • 16,483
  • 15
  • 59
  • 70

6 Answers6

16

The library I have in mind is not an ORM, but it may still do what you want.

If you want something that makes your database accesses safe while integrating things into your program nicely then try out HaskellDB. It basically looks at your schema, generates some data structures, and then gives you type safe ways to query. It's been around for quite a while and the community opinion is that it's good and stable.

To use it, you'll need some underlying Haskell DB library like HSQL.

Good luck!

Jason Dagit
  • 13,684
  • 8
  • 33
  • 56
12

The reason that ORM libraries exist is that there is relative big difference between Objects in C# or Java and what you store in a database. This is not so much an issue in Haskell because:

  1. It does not have Objects
  2. Both databases and Haskell list have their inspiration in mathematical set theory, so the friction between them is a lot less than between databases and Objects.
tomjen
  • 3,779
  • 3
  • 29
  • 35
  • 21
    I use SqlAlchemy in Python because it makes easy to assemble/reuse SQL statements without manipulating strings directly. Is there something like this for Haskell? – Paulo Scardine Aug 05 '12 at 23:08
  • 1
    the 'Object' in ORM does not correspond specifically to object-oriented programming, and also applies to Haskell values. and while the impedance between relational and object-oriented models is greater than Haskell's algebraic models, there is still some friction (circular or 'graph' types can be naturally represented in RDBMS but not Haskell) as well as a lot of annoying boilerplate to write. – Ari Fordsham Feb 22 '21 at 14:22
  • As I understand, isn't Haskell object and Relational database based on completely different theories? The former is from typed lambda calculus and the latter from the relational theory? – Xwtek Sep 04 '21 at 14:26
10

Persistent is rather nice to use, and lets you rely on type inference to determine the table your query relates to. For example, if I have the following in my "models" file:

User
    name Text
    age Int

Login
    user UserId
    login Text
    passwd Text

Then I could do this:

Just (Entity uid _)          <- selectFirst [ UserName ==. "exampleUser" ] []
Just (Entity lid Login {..}) <- selectFirst [ LoginUser ==. uid ] []

And it would know which tables I meant. Of course, you probably don't want to be writing partial code like this, but I wanted to emphasize just the queries.

Community
  • 1
  • 1
John Wiegley
  • 6,972
  • 1
  • 16
  • 20
3

I personally used only Database.HDBC which is recommended by "Real World Haskell": http://book.realworldhaskell.org/read/using-databases.html

But I agree that it definitely makes sense to use a higher-level DB access layer, and I'll probably try to move to such a model for future projects. On this topic, I found this post from 2012 which provides a history and comparison of such solutions for Haskell: http://www.yesodweb.com/blog/2012/03/history-of-persistence

From it, I gather that Persistent (documentation) and Groundhog (some documentation, examples) are the most promising libraries in this area. Both libraries support the databases you mention; for Groundhog it's not written in this post but in this announcement you can see that it supports exactly the DBs you are interested in.

Also note this thread on Haskell-beginners in which Esqueletto is mentioned as a better choice for update operations.

Note that Persistent ships with Yesod and as such may have a greater following.

Janus Troelsen
  • 20,267
  • 14
  • 135
  • 196
Emmanuel Touzery
  • 9,008
  • 3
  • 65
  • 81
2

I actually very much like the approach of HAppS (HAppS-State) which allows you to forget about going through the marshalling/unmarshalling cludge of ORM and let's you simply use Haskell's data types.

eelco
  • 1,597
  • 12
  • 8
1

Have you looked through the database mapping and access packages at http://hackage.haskell.org/packages/archive/pkg-list.html#cat:Database

I haven't used them, so can't recommend any particular one. I also don't know what databases you are planning on using.

wnoise
  • 9,764
  • 37
  • 47
  • Updated the question to list databases I'm interested in, but it shouldn't be much of a filtering criteria. I've looked through hackage a bit in the past, but I should probably crawl it again. I would like to hear from people with personal experience though. – rcreswick Sep 19 '08 at 22:01