An mtl-style typeclass and transformer for persistent.

Latest on Hackage:

This package is not currently in any snapshots. If you're interested in using it, we recommend adding it to Stackage Nightly. Doing so will make builds more reliable, and allow to host generated Haddocks.

ISC licensed and maintained by Alexis King

monad-persist Build Status

This module provides an mtl-style MonadPersist typeclass for persistent, as well as a PersistT monad transformer that implements it. This makes it easier to use persistent in an arbitrary monad transformer stack, rather than one that must be ReaderT over MonadIO.

import Control.Monad.Logger (runNoLoggingT)
import Control.Monad.Persist
import Data.Text (Text)
import Database.Persist.Sqlite (withSqliteConn)
import Database.Persist.TH

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
  name Text
  email Text

  UniqueEmail email

  deriving Eq Show

ghci> runNoLoggingT $ withSqliteConn ":memory:" $ \conn -> flip runSqlPersistT conn $ do
        runMigration migrateAll
        insert_ User { userName = "Alyssa", userEmail = "" }
        users <- selectList [] []
        return (users :: [Entity User])

Migrating: CREATE TABLE "user"("id" INTEGER PRIMARY KEY,"name" VARCHAR NOT NULL,"email" VARCHAR NOT NULL,CONSTRAINT "unique_email" UNIQUE ("email"))
[Entity {entityKey = UserKey {unUserKey = SqlBackendKey {unSqlBackendKey = 1}}, entityVal = User {userName = "Alyssa", userEmail = ""}}]

For more information, see the documentation on Hackage.

Changes (September 18th, 2018)

  • Added support for persistent version 2.8.0 and above. (November 9th, 2017)

  • Added lifting instance for MonadPersist for IdentityT. (October 10th, 2017)

  • Fixed package dependencies for GHC 7.8 and 7.10. (October 10th, 2017)

  • Added runSqlPoolPersistT (#2). (April 26th, 2017)

  • Removed the need for MonadIO when running PersistT, since MonadBaseControl IO is already enough. (April 24th, 2017)

  • Added instances for MonadThrow, MonadCatch, and MonadMask to PersistT. (April 20th, 2017)

  • Initial release
comments powered byDisqus