Skip to content

Commit

Permalink
Add getEntity helper method.
Browse files Browse the repository at this point in the history
This method is similar to `insertEntity`.

Closes #616.
  • Loading branch information
cdepillabout committed Oct 11, 2016
1 parent f90dd96 commit d8b53d5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions persistent-test/src/PersistentTest.hs
Expand Up @@ -688,6 +688,12 @@ specs = describe "persistent" $ do
Just p2 <- get k
p2 @== p

it "getEntity" $ db $ do
Entity k p <- insertEntity $ Person "name" 1 Nothing
Just (Entity k2 p2) <- getEntity k
p @== p2
k @== k2

it "repsert" $ db $ do
k <- liftIO (PersonKey `fmap` generateKey)
Nothing <- selectFirst [PersonName ==. "Repsert"] []
Expand Down
1 change: 1 addition & 0 deletions persistent/Database/Persist/Class.hs
Expand Up @@ -11,6 +11,7 @@ module Database.Persist.Class
, BaseBackend(..)
, PersistRecordBackend
, getJust
, getEntity
, belongsTo
, belongsToJust
, insertEntity
Expand Down
11 changes: 11 additions & 0 deletions persistent/Database/Persist/Class/PersistStore.hs
Expand Up @@ -10,6 +10,7 @@ module Database.Persist.Class.PersistStore
, PersistCore (..)
, PersistStoreRead (..)
, PersistStoreWrite (..)
, getEntity
, getJust
, belongsTo
, belongsToJust
Expand Down Expand Up @@ -216,3 +217,13 @@ insertEntity ::
insertEntity e = do
eid <- insert e
return $ Entity eid e

-- | Like @get@, but returns the complete @Entity@.
getEntity ::
( PersistStoreWrite backend
, PersistRecordBackend e backend
, MonadIO m
) => Key e -> ReaderT backend m (Maybe (Entity e))
getEntity key = do
maybeModel <- get key
return $ fmap (key `Entity`) maybeModel

0 comments on commit d8b53d5

Please sign in to comment.