snaplet-persistent-0.5: persistent snaplet for the Snap Framework

Safe HaskellNone
LanguageHaskell98

Snap.Snaplet.Persistent

Contents

Synopsis

Documentation

initPersist :: SqlPersistT (NoLoggingT IO) a -> SnapletInit b PersistState Source

Initialize Persistent with an initial SQL function called right after the connection pool has been created. This is most useful for calling migrations upfront right after initialization.

Example:

initPersist (runMigrationUnsafe migrateAll)

where migrateAll is the migration function that was auto-generated by the QQ statement in your persistent schema definition in the call to mkMigrate.

initPersistGeneric :: Initializer b PersistState (Pool SqlBackend) -> SqlPersistT (NoLoggingT IO) a -> SnapletInit b PersistState Source

Backend-agnostic initalization with an initial SQL function called right after the connection pool has been created. This is most useful for calling migrations upfront right after initialization.

Example:

initPersist mkPool (runMigrationUnsafe migrateAll)

where migrateAll is the migration function that was auto-generated by the QQ statement in your persistent schema definition in the call to mkMigrate.

mkPool is a function to construct a pool of connections to your database

class MonadIO m => HasPersistPool m where Source

Implement this type class to have any monad work with snaplet-persistent. A default instance is provided for (Handler b PersistState).

mkPgPool :: MonadIO m => Config -> m ConnectionPool Source

Constructs a connection pool from Config.

mkSnapletPgPool :: (MonadIO (m b v), MonadSnaplet m) => m b v ConnectionPool Source

Conscruts a connection pool in a snaplet context.

runPersist Source

Arguments

:: HasPersistPool m 
=> SqlPersistT (ResourceT (NoLoggingT IO)) b

Run given Persistent action in the defined monad.

-> m b 

Runs a SqlPersist action in any monad with a HasPersistPool instance.

withPool :: MonadIO m => ConnectionPool -> SqlPersistT (ResourceT (NoLoggingT IO)) a -> m a Source

Run a database action

Utility Functions

mkKey :: ToBackendKey SqlBackend entity => Int -> Key entity Source

Make a Key from an Int.

mkKeyBS :: ToBackendKey SqlBackend entity => ByteString -> Key entity Source

Makes a Key from a ByteString. Calls error on failure.

mkKeyT :: ToBackendKey SqlBackend entity => Text -> Key entity Source

Makes a Key from Text. Calls error on failure.

showKey :: ToBackendKey SqlBackend e => Key e -> Text Source

Makes a Text representation of a Key.

showKeyBS :: ToBackendKey SqlBackend e => Key e -> ByteString Source

Makes a ByteString representation of a Key.

mkInt :: ToBackendKey SqlBackend a => Key a -> Int Source

Converts a Key to Int. Fails with error if the conversion fails.

mkWord64 :: ToBackendKey SqlBackend a => Key a -> Word64 Source

Converts a Key to Word64. Fails with error if the conversion fails.

followForeignKey :: (PersistEntity a, HasPersistPool m, PersistEntityBackend a ~ SqlBackend) => (t -> Key a) -> Entity t -> m (Maybe (Entity a)) Source

Follows a foreign key field in one entity and retrieves the corresponding entity from the database.