htsn-import-0.0.1: src/TSN/Database.hs
-- | Standalone database functions for dealing with Groundhog types.
--
module TSN.Database ( insert_or_select )
where
import Database.Groundhog ( AutoKey, insertByAll )
import Database.Groundhog.Core ( PersistBackend, PersistEntity )
-- | Attempt to insert the given object returning its new primary
-- key. If any unique constraints would be violated, instead return
-- the primary key of the existing record that caused the collision.
insert_or_select :: (PersistBackend m, PersistEntity a)
=> a
-> m (AutoKey a)
insert_or_select x = do
tmp <- insertByAll x
return $ (either id id) tmp