Since I can't map over a record, the best method I have found is to just use brute force:
data Item = Item {
vendor :: Int,
lotNumber :: Int,
description :: String,
reserve :: Maybe Double,
preSaleBids :: Maybe Double,
salePrice :: Maybe Double,
purchaser :: Maybe Int,
saleID :: Maybe Int
}
hsToDb :: Item -> [SqlValue]
hsToDb (Item a b c d e f g h) = [toSql a, toSql b, toSql c, toSql d, toSql e, toSql f, toSql g, toSql h]
dbToHs :: [SqlValue] -> Item
dbToHs [a,b,c,d,e,f,g,h] = Item (fromSql a) (fromSql b) (fromSql c) (fromSql d) (fromSql e) (fromSql f) (fromSql g) (fromSql h)
This code looks ugly and also requires updating if I change the length of my Item record so I was wondering whether there's a clever way of generalizing this idea.
Depending on what library you're using the type
(Int, Int, String, ...)may already have a (as in e.g.sqlite-simple)FromRowinstance. If you needItemto be a new type you can use theGeneralizedNewtypeDerivingextension to get that instance for free. Barring this you could of course define your own type class/helper method to make it more DRY.