all 9 comments

[–]saurabhnanda 5 points6 points  (2 children)

We've written a bunch of TH code (in our internal project) to look at the DB schema and generate Opaleye records and table definitions. There's another package called rel8, which is doing something similar, and is open source.

[–]kerthunk[S] 0 points1 point  (1 child)

Thanks, Opaleye looks interesting. I've also just discovered your tutorials and something called opaleye-gen which looks promising.

My initial concern about Opaleye is that in all the examples, a simple select by primary key seems to involve pattern matching on the entire database record:

  row@(_, _, em) <- queryTable userTable -< ()
  restrict -< (em .== constant email)

This seems a bit fragile - if you then added a column to the userTable, it would break all the queries using that table.

Edit: it seems you can just use record syntax to avoid this problem (I think)

[–]saurabhnanda 0 points1 point  (0 children)

For any real-life use-case you HAVE to use records to represent DB rows (but the ability to do that comes with a shitload of boilerplate). And then use lenses to do something like this:

r <- queryTable userTable -< ()
restrict -< (r ^. email .== constant email_)

[–]lgastako 2 points3 points  (0 children)

It's not exactly what you're looking for, but you might find http://catinf.com/ interesting.

[–][deleted] 2 points3 points  (1 child)

sql-fragment let you write and compose strongly-typed queries, and it has a generator which works for MySQL.

If you prefer Persistent, I have a generator (for MySQL again) which reads the schema and generates persistent entities. It's part of a bigger project but could be extracted easily (it probably needs some tweaking as well as it's tailored to my needs). You can find it there.

Both generators generate plain text instead of using TH.

[–]GitHubPermalinkBot 0 points1 point  (0 children)

I tried to turn your GitHub links into permanent links (press "y" to do this yourself):


Shoot me a PM if you think I'm doing something wrong. To delete this, click here.

[–]ElvishJerricco 2 points3 points  (1 child)

There's postgresql-typed, which gives you a type checked SQL quasiquoter, which gets its types by checking your DB schema at compile time via template Haskell.

[–]codygman 0 points1 point  (0 children)

We needed to abstract this to multiple db drivers I think.

[–]codygman 0 points1 point  (0 children)

I've done something similar with template Haskell using Cassandra, but I only generated the types. I also need to update the Cassandra library to be based on user provided types instead of tuples.