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 :: Type -> Type) a. SqlPersistTFor db m a -> SqlPersistT m a
- specializeQuery :: forall db (m :: Type -> Type) 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
FromJSON (BackendKey (SqlFor a)) Source # | |
Defined in Database.Persist.Typed Methods parseJSON :: Value -> Parser (BackendKey (SqlFor a)) # parseJSONList :: Value -> Parser [BackendKey (SqlFor a)] # omittedField :: Maybe (BackendKey (SqlFor a)) # | |
ToJSON (BackendKey (SqlFor a)) Source # | |
Defined in Database.Persist.Typed Methods toJSON :: BackendKey (SqlFor a) -> Value # toEncoding :: BackendKey (SqlFor a) -> Encoding # toJSONList :: [BackendKey (SqlFor a)] -> Value # toEncodingList :: [BackendKey (SqlFor a)] -> Encoding # omitField :: BackendKey (SqlFor a) -> Bool # | |
Bounded (BackendKey (SqlFor a)) Source # | |
Defined in Database.Persist.Typed | |
Enum (BackendKey (SqlFor a)) Source # | |
Defined in Database.Persist.Typed Methods succ :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) # pred :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) # toEnum :: Int -> BackendKey (SqlFor a) # fromEnum :: BackendKey (SqlFor a) -> Int # enumFrom :: BackendKey (SqlFor a) -> [BackendKey (SqlFor a)] # enumFromThen :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) -> [BackendKey (SqlFor a)] # enumFromTo :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) -> [BackendKey (SqlFor a)] # enumFromThenTo :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) -> BackendKey (SqlFor a) -> [BackendKey (SqlFor a)] # | |
Num (BackendKey (SqlFor a)) Source # | |
Defined in Database.Persist.Typed Methods (+) :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) -> BackendKey (SqlFor a) # (-) :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) -> BackendKey (SqlFor a) # (*) :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) -> BackendKey (SqlFor a) # negate :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) # abs :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) # signum :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) # fromInteger :: Integer -> BackendKey (SqlFor a) # | |
Read (BackendKey (SqlFor a)) Source # | |
Defined in Database.Persist.Typed Methods readsPrec :: Int -> ReadS (BackendKey (SqlFor a)) # readList :: ReadS [BackendKey (SqlFor a)] # readPrec :: ReadPrec (BackendKey (SqlFor a)) # readListPrec :: ReadPrec [BackendKey (SqlFor a)] # | |
Integral (BackendKey (SqlFor a)) Source # | |
Defined in Database.Persist.Typed Methods quot :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) -> BackendKey (SqlFor a) # rem :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) -> BackendKey (SqlFor a) # div :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) -> BackendKey (SqlFor a) # mod :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) -> BackendKey (SqlFor a) # quotRem :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) -> (BackendKey (SqlFor a), BackendKey (SqlFor a)) # divMod :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) -> (BackendKey (SqlFor a), BackendKey (SqlFor a)) # toInteger :: BackendKey (SqlFor a) -> Integer # | |
Real (BackendKey (SqlFor a)) Source # | |
Defined in Database.Persist.Typed Methods toRational :: BackendKey (SqlFor a) -> Rational # | |
Show (BackendKey (SqlFor a)) Source # | |
Defined in Database.Persist.Typed | |
Eq (BackendKey (SqlFor a)) Source # | |
Defined in Database.Persist.Typed Methods (==) :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) -> Bool # (/=) :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) -> Bool # | |
Ord (BackendKey (SqlFor a)) Source # | |
Defined in Database.Persist.Typed Methods compare :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) -> Ordering # (<) :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) -> Bool # (<=) :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) -> Bool # (>) :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) -> Bool # (>=) :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) -> Bool # max :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) -> BackendKey (SqlFor a) # min :: BackendKey (SqlFor a) -> BackendKey (SqlFor a) -> BackendKey (SqlFor a) # | |
FromHttpApiData (BackendKey (SqlFor a)) Source # | |
Defined in Database.Persist.Typed Methods parseUrlPiece :: Text -> Either Text (BackendKey (SqlFor a)) # parseHeader :: ByteString -> Either Text (BackendKey (SqlFor a)) # parseQueryParam :: Text -> Either Text (BackendKey (SqlFor a)) # | |
ToHttpApiData (BackendKey (SqlFor a)) Source # | |
Defined in Database.Persist.Typed Methods toUrlPiece :: BackendKey (SqlFor a) -> Text # toEncodedUrlPiece :: BackendKey (SqlFor a) -> Builder # toHeader :: BackendKey (SqlFor a) -> ByteString # toQueryParam :: BackendKey (SqlFor a) -> Text # toEncodedQueryParam :: BackendKey (SqlFor a) -> Builder # | |
PathPiece (BackendKey (SqlFor a)) Source # | |
Defined in Database.Persist.Typed Methods fromPathPiece :: Text -> Maybe (BackendKey (SqlFor a)) # toPathPiece :: BackendKey (SqlFor a) -> Text # | |
PersistField (BackendKey (SqlFor a)) Source # | |
Defined in Database.Persist.Typed Methods toPersistValue :: BackendKey (SqlFor a) -> PersistValue # fromPersistValue :: PersistValue -> Either Text (BackendKey (SqlFor a)) # | |
PersistFieldSql (BackendKey (SqlFor a)) Source # | |
Defined in Database.Persist.Typed | |
newtype BackendKey SqlReadBackend | |
Defined in Database.Persist.Sql.Orphan.PersistStore | |
newtype BackendKey SqlWriteBackend | |
Defined in Database.Persist.Sql.Orphan.PersistStore | |
newtype BackendKey SqlBackend | |
Defined in Database.Persist.Sql.Orphan.PersistStore | |
type Rep (BackendKey SqlReadBackend) | |
Defined in Database.Persist.Sql.Orphan.PersistStore type Rep (BackendKey SqlReadBackend) = D1 ('MetaData "BackendKey" "Database.Persist.Sql.Orphan.PersistStore" "persistent-2.14.6.3-Ao7NKS1xky27sAKB9vEMO2" 'True) (C1 ('MetaCons "SqlReadBackendKey" 'PrefixI 'True) (S1 ('MetaSel ('Just "unSqlReadBackendKey") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Int64))) | |
type Rep (BackendKey SqlWriteBackend) | |
Defined in Database.Persist.Sql.Orphan.PersistStore type Rep (BackendKey SqlWriteBackend) = D1 ('MetaData "BackendKey" "Database.Persist.Sql.Orphan.PersistStore" "persistent-2.14.6.3-Ao7NKS1xky27sAKB9vEMO2" 'True) (C1 ('MetaCons "SqlWriteBackendKey" 'PrefixI 'True) (S1 ('MetaSel ('Just "unSqlWriteBackendKey") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Int64))) | |
type Rep (BackendKey SqlBackend) | |
Defined in Database.Persist.Sql.Orphan.PersistStore type Rep (BackendKey SqlBackend) = D1 ('MetaData "BackendKey" "Database.Persist.Sql.Orphan.PersistStore" "persistent-2.14.6.3-Ao7NKS1xky27sAKB9vEMO2" 'True) (C1 ('MetaCons "SqlBackendKey" 'PrefixI 'True) (S1 ('MetaSel ('Just "unSqlBackendKey") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Int64))) | |
newtype BackendKey (SqlFor a) Source # | |
Defined in Database.Persist.Typed |
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 =SqlPersistTFor
MainDb getStuff ::MonadIO
m => 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 :: Type -> Type) 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 :: Type -> Type) 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 ::ReaderT
SqlBackend
m a ->ReaderT
(SqlFor
MainDb) m a mainQuery =specializeQuery
accountQuery ::ReaderT
SqlBackend
m a ->ReaderT
(SqlFor
AccountDb) 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