Skip to content

Instantly share code, notes, and snippets.

@qxjit
Created September 19, 2018 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qxjit/3d08cee0ae6c0044350c999b4e28785a to your computer and use it in GitHub Desktop.
Save qxjit/3d08cee0ae6c0044350c999b4e28785a to your computer and use it in GitHub Desktop.
A simple example of mapping a Haskell field to multiple columns in Orville
module RelationalMapExample where
import qualified Data.Int as Int
import qualified Data.Text as Text
import qualified Database.Orville as O
data Update key = Update
{ updateId :: key
, updateLocation :: Text.Text
, updateStatus :: UpdateStatus
}
data UpdateStatus = UpdateStatus
{ updateStatusCode :: Int.Int32
, updateStatusDescription :: Text.Text
} deriving (Eq, Show)
updateStatusTable :: O.TableDefinition (Update Int) (Update ()) Int
updateStatusTable =
O.mkTableDefinition $
O.TableParams
{ O.tblName = "update"
, O.tblPrimaryKey = updateIdField
, O.tblMapper =
Update
<$> O.readOnlyField updateIdField
<*> O.attrField updateLocation (O.textField "location" 255)
<*> O.mapAttr updateStatus updateStatusMap
, O.tblGetKey = updateId
, O.tblSafeToDelete = []
, O.tblComments = O.noComments
}
updateIdField :: O.FieldDefinition Int
updateIdField = O.automaticIdField "id" `O.withFlag` O.PrimaryKey
updateStatusMap :: O.RelationalMap UpdateStatus UpdateStatus
updateStatusMap =
UpdateStatus
<$> O.attrField updateStatusCode (O.int32Field "status_code")
<*> O.attrField updateStatusDescription (O.textField "status_description" 255)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment