Yesod.Persist.Core

yesod-persistent-1.6.0.8: Some helpers for using Persistent from Yesod.

Safe Haskell | None
Language | Haskell2010

Yesod.Persist.Core

Description

Defines the core functionality of this package. This package is distinguished from Yesod.Persist in that the latter additionally exports the persistent modules themselves.

Synopsis

- class Monad (YesodDB site) => YesodPersist site where
  - type YesodPersistBackend site
  - runDB :: YesodDB site a -> HandlerFor site a
- defaultRunDB :: PersistConfig c => (site -> c) -> (site -> PersistConfigPool c) -> PersistConfigBackend c (HandlerFor site) a -> HandlerFor site a
- class YesodPersist site => YesodPersistRunner site where
  - getDBRunner :: HandlerFor site (DBRunner site, HandlerFor site ())
- defaultGetDBRunner :: (IsSqlBackend backend, YesodPersistBackend site ~ backend) => (site -> Pool backend) -> HandlerFor site (DBRunner site, HandlerFor site ())
- newtype DBRunner site = DBRunner {
  - runDBRunner :: forall a. YesodDB site a -> HandlerFor site a
  }
- runDBSource :: YesodPersistRunner site => ConduitT () a (YesodDB site) () -> ConduitT () a (HandlerFor site) ()
- respondSourceDB :: YesodPersistRunner site => ContentType -> ConduitT () (Flush Builder) (YesodDB site) () -> HandlerFor site TypedContent
- type YesodDB site = ReaderT (YesodPersistBackend site) (HandlerFor site)
- get404 :: forall (m :: Type -> Type) backend val. (MonadIO m, PersistStoreRead backend, PersistRecordBackend val backend) => Key val -> ReaderT backend m val
- getBy404 :: forall backend val (m :: Type -> Type). (PersistUniqueRead backend, PersistRecordBackend val backend, MonadIO m) => Unique val -> ReaderT backend m (Entity val)
- insert400 :: forall (m :: Type -> Type) backend val. (MonadIO m, PersistUniqueWrite backend, PersistRecordBackend val backend, SafeToInsert val) => val -> ReaderT backend m (Key val)
- insert400_ :: forall (m :: Type -> Type) backend val. (MonadIO m, PersistUniqueWrite backend, PersistRecordBackend val backend, SafeToInsert val) => val -> ReaderT backend m ()

# Documentation

class Monad (YesodDB site) => YesodPersist site where #

Associated Types

type YesodPersistBackend site #

Methods

runDB :: YesodDB site a -> HandlerFor site a #

Allows you to execute database actions within Yesod Handlers. For databases that support it, code inside the action will run as an atomic transaction.

#### Example Usage

Expand

    userId <- runDB $ do
      userId <- insert $ User "username" "email@example.com"
      insert_ $ UserPreferences userId True
      pure userId

defaultRunDB :: PersistConfig c => (site -> c) -> (site -> PersistConfigPool c) -> PersistConfigBackend c (HandlerFor site) a -> HandlerFor site a #

Helper for creating `runDB`.

Since 1.2.0

class YesodPersist site => YesodPersistRunner site where #

Since 1.2.0

Methods

getDBRunner :: HandlerFor site (DBRunner site, HandlerFor site ()) #

This function differs from `runDB` in that it returns a database runner function, as opposed to simply running a single action. This will usually mean that a connection is taken from a pool and then reused for each invocation. This can be useful for creating streaming responses; see `runDBSource`.

It additionally returns a cleanup function to free the connection. If your code finishes successfully, you must call this cleanup to indicate changes should be committed. Otherwise, for SQL backends at least, a rollback will be used instead.

Since 1.2.0

defaultGetDBRunner :: (IsSqlBackend backend, YesodPersistBackend site ~ backend) => (site -> Pool backend) -> HandlerFor site (DBRunner site, HandlerFor site ()) #

Helper for implementing `getDBRunner`.

Since 1.2.0

newtype DBRunner site #

Constructors

DBRunner |
Fields runDBRunner :: forall a. YesodDB site a -> HandlerFor site a

runDBSource :: YesodPersistRunner site => ConduitT () a (YesodDB site) () -> ConduitT () a (HandlerFor site) () #

Like `runDB`, but transforms a `Source`. See `respondSourceDB` for an example, practical use case.

Since 1.2.0

respondSourceDB :: YesodPersistRunner site => ContentType -> ConduitT () (Flush Builder) (YesodDB site) () -> HandlerFor site TypedContent #

Extends `respondSource` to create a streaming database response body.

type YesodDB site = ReaderT (YesodPersistBackend site) (HandlerFor site) #

get404 :: forall (m :: Type -> Type) backend val. (MonadIO m, PersistStoreRead backend, PersistRecordBackend val backend) => Key val -> ReaderT backend m val #

Get the given entity by ID, or return a 404 not found if it doesn't exist.

getBy404 :: forall backend val (m :: Type -> Type). (PersistUniqueRead backend, PersistRecordBackend val backend, MonadIO m) => Unique val -> ReaderT backend m (Entity val) #

Get the given entity by unique key, or return a 404 not found if it doesn't exist.

insert400 :: forall (m :: Type -> Type) backend val. (MonadIO m, PersistUniqueWrite backend, PersistRecordBackend val backend, SafeToInsert val) => val -> ReaderT backend m (Key val) #

Create a new record in the database, returning an automatically created key, or raise a 400 bad request if a uniqueness constraint is violated.

Since: 1.4.1

insert400_ :: forall (m :: Type -> Type) backend val. (MonadIO m, PersistUniqueWrite backend, PersistRecordBackend val backend, SafeToInsert val) => val -> ReaderT backend m () #

Same as `insert400`, but doesn’t return a key.

Since: 1.4.1