| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Database.Persist.Typed
Description
This module defines types and helpers for type-safe access to multiple database schema.
Synopsis
- mkSqlSettingsFor :: Name -> MkPersistSettings
- newtype SqlFor db = SqlFor {}
- data family BackendKey backend
- type SqlPersistTFor db = ReaderT (SqlFor db)
- type ConnectionPoolFor db = Pool (SqlFor db)
- type SqlPersistMFor db = ReaderT (SqlFor db) (NoLoggingT (ResourceT IO))
- runSqlPoolFor :: MonadUnliftIO m => SqlPersistTFor db m a -> ConnectionPoolFor db -> m a
- runSqlConnFor :: MonadUnliftIO m => SqlPersistTFor db m a -> SqlFor db -> m a
- generalizePool :: ConnectionPoolFor db -> ConnectionPool
- specializePool :: ConnectionPool -> ConnectionPoolFor db
- generalizeQuery :: forall db m a. SqlPersistTFor db m a -> SqlPersistT m a
- specializeQuery :: forall db m a. SqlPersistT m a -> SqlPersistTFor db m a
- generalizeSqlBackend :: SqlFor db -> SqlBackend
- specializeSqlBackend :: SqlBackend -> SqlFor db
- toSqlKeyFor :: ToBackendKey (SqlFor a) record => Int64 -> Key record
- fromSqlKeyFor :: ToBackendKey (SqlFor a) record => Key record -> Int64
Schema Definition
mkSqlSettingsFor :: Name -> MkPersistSettings Source #
Use the SqlFor type for the database connection backend. Use this instead
of sqlSettings and provide a quoted type name.
data MainDb
share [ mkPersist (mkSqlSettingsFor ''MainDb), mkMigrate "migrateAll" ] [persistLowerCase|
User
name Text
age Int
deriving Show Eq
|]
The entities generated will have the PersistEntityBackend defined to be
instead of SqlFor MainDbSqlBackend. This is what provides the type
safety.
Since: 0.0.1.0
A wrapper around SqlBackend type. To specialize this to a specific
database, fill in the type parameter.
Since: 0.0.1.0
Constructors
| SqlFor | |
Fields | |
Instances
data family BackendKey backend #
Instances
Specialized aliases
type SqlPersistTFor db = ReaderT (SqlFor db) Source #
This type signature represents a database query for a specific database. You will likely want to specialize this to your own application for readability:
data MainDb type MainQueryT =SqlPersistTForMainDb getStuff ::MonadIOm => StuffId -> MainQueryT m (Maybe Stuff)
Since: 0.0.1.0
type ConnectionPoolFor db = Pool (SqlFor db) Source #
A Pool of database connections that are specialized to a specific
database.
Since: 0.0.1.0
type SqlPersistMFor db = ReaderT (SqlFor db) (NoLoggingT (ResourceT IO)) Source #
A specialization of SqlPersistM that uses the underlying db database
type.
Since: 0.0.1.0
Running specialized queries
runSqlPoolFor :: MonadUnliftIO m => SqlPersistTFor db m a -> ConnectionPoolFor db -> m a Source #
Run a SqlPersistTFor action on an appropriate database.
Since: 0.0.1.0
runSqlConnFor :: MonadUnliftIO m => SqlPersistTFor db m a -> SqlFor db -> m a Source #
Run a SqlPersistTFor action on the appropriate database connection.
Since: 0.0.1.0
Specializing and generalizing
generalizePool :: ConnectionPoolFor db -> ConnectionPool Source #
Generalize a to an ordinary Pool (SqlFor db)ConnectionPool. This
renders the pool unusable for model-specific code that relies on the type
safety, but allows you to use it for general-purpose SQL queries.
Since: 0.0.1.0
specializePool :: ConnectionPool -> ConnectionPoolFor db Source #
Specialize a ConnectionPool to a . You should apply
this whenever you create or initialize the database connection pooling to
avoid potentially mixing the database pools up.Pool (SqlFor db)
Since: 0.0.1.0
generalizeQuery :: forall db m a. SqlPersistTFor db m a -> SqlPersistT m a Source #
Generalizes a query from a specific database to one that is database agnostic.
Since: 0.0.1.0
specializeQuery :: forall db m a. SqlPersistT m a -> SqlPersistTFor db m a Source #
Specialize a query to a specific database. You should define aliases for this function for each database you use.
data MainDb data AccountDb mainQuery ::ReaderTSqlBackendm a ->ReaderT(SqlForMainDb) m a mainQuery =specializeQueryaccountQuery ::ReaderTSqlBackendm a ->ReaderT(SqlForAccountDb) m a accountQuery =specializeQuery
Since: 0.0.1.0
generalizeSqlBackend :: SqlFor db -> SqlBackend Source #
Generalizes a SqlFor backend to be database agnostic.
Since: 0.0.1.0
specializeSqlBackend :: SqlBackend -> SqlFor db Source #
Specializes a SqlBackend for a specific database.
Since: 0.0.1.0
Key functions
toSqlKeyFor :: ToBackendKey (SqlFor a) record => Int64 -> Key record Source #
Persistent's toSqlKey and fromSqlKey hardcode the SqlBackend, so we
have to reimplement them here.
Since: 0.0.1.0
fromSqlKeyFor :: ToBackendKey (SqlFor a) record => Key record -> Int64 Source #
Persistent's toSqlKey and fromSqlKey hardcode the SqlBackend, so we
have to reimplement them here.
Since: 0.0.1.0