Created
September 19, 2018 18:43
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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