Safe Haskell | None |
---|---|
Language | Haskell98 |
Database.Persist.Class
Contents
- class MonadIO m => PersistStore m where
- type PersistMonadBackend m
- get :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val) => Key val -> m (Maybe val)
- insert :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val) => val -> m (Key val)
- insert_ :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val) => val -> m ()
- insertMany :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val) => [val] -> m [Key val]
- insertKey :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val) => Key val -> val -> m ()
- repsert :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val) => Key val -> val -> m ()
- replace :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val) => Key val -> val -> m ()
- delete :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val) => Key val -> m ()
- getJust :: (PersistStore m, PersistEntity val, Show (Key val), PersistMonadBackend m ~ PersistEntityBackend val) => Key val -> m val
- belongsTo :: (PersistStore m, PersistEntity ent1, PersistEntity ent2, PersistMonadBackend m ~ PersistEntityBackend ent2) => (ent1 -> Maybe (Key ent2)) -> ent1 -> m (Maybe ent2)
- belongsToJust :: (PersistStore m, PersistEntity ent1, PersistEntity ent2, PersistMonadBackend m ~ PersistEntityBackend ent2) => (ent1 -> Key ent2) -> ent1 -> m ent2
- class PersistStore m => PersistUnique m where
- getBy :: (PersistEntityBackend val ~ PersistMonadBackend m, PersistEntity val) => Unique val -> m (Maybe (Entity val))
- deleteBy :: (PersistEntityBackend val ~ PersistMonadBackend m, PersistEntity val) => Unique val -> m ()
- insertUnique :: (PersistEntityBackend val ~ PersistMonadBackend m, PersistEntity val) => val -> m (Maybe (Key val))
- getByValue :: (PersistEntity value, PersistUnique m, PersistEntityBackend value ~ PersistMonadBackend m) => value -> m (Maybe (Entity value))
- insertBy :: (PersistEntity val, PersistUnique m, PersistEntityBackend val ~ PersistMonadBackend m) => val -> m (Either (Entity val) (Key val))
- replaceUnique :: (Eq record, Eq (Unique record), PersistEntityBackend record ~ PersistMonadBackend m, PersistEntity record, PersistStore m, PersistUnique m) => Key record -> record -> m (Maybe (Unique record))
- class PersistStore m => PersistQuery m where
- update :: (PersistEntity val, PersistEntityBackend val ~ PersistMonadBackend m) => Key val -> [Update val] -> m ()
- updateGet :: (PersistEntity val, PersistMonadBackend m ~ PersistEntityBackend val) => Key val -> [Update val] -> m val
- updateWhere :: (PersistEntity val, PersistEntityBackend val ~ PersistMonadBackend m) => [Filter val] -> [Update val] -> m ()
- deleteWhere :: (PersistEntity val, PersistEntityBackend val ~ PersistMonadBackend m) => [Filter val] -> m ()
- selectSource :: (PersistEntity val, PersistEntityBackend val ~ PersistMonadBackend m) => [Filter val] -> [SelectOpt val] -> Source m (Entity val)
- selectFirst :: (PersistEntity val, PersistEntityBackend val ~ PersistMonadBackend m) => [Filter val] -> [SelectOpt val] -> m (Maybe (Entity val))
- selectKeys :: (PersistEntity val, PersistEntityBackend val ~ PersistMonadBackend m) => [Filter val] -> [SelectOpt val] -> Source m (Key val)
- count :: (PersistEntity val, PersistEntityBackend val ~ PersistMonadBackend m) => [Filter val] -> m Int
- selectList :: (PersistEntity val, PersistQuery m, PersistEntityBackend val ~ PersistMonadBackend m) => [Filter val] -> [SelectOpt val] -> m [Entity val]
- selectKeysList :: (PersistEntity val, PersistQuery m, PersistEntityBackend val ~ PersistMonadBackend m) => [Filter val] -> [SelectOpt val] -> m [Key val]
- class (PersistStore m, PersistEntity a, PersistEntityBackend a ~ PersistMonadBackend m) => DeleteCascade a m where
- deleteCascade :: Key a -> m ()
- deleteCascadeWhere :: (DeleteCascade a m, PersistQuery m) => [Filter a] -> m ()
- class PersistEntity record where
- data EntityField record :: * -> *
- type PersistEntityBackend record
- data Unique record
- persistFieldDef :: EntityField record typ -> FieldDef SqlType
- entityDef :: Monad m => m record -> EntityDef SqlType
- toPersistFields :: record -> [SomePersistField]
- fromPersistValues :: [PersistValue] -> Either Text record
- persistUniqueToFieldNames :: Unique record -> [(HaskellName, DBName)]
- persistUniqueToValues :: Unique record -> [PersistValue]
- persistUniqueKeys :: record -> [Unique record]
- persistIdField :: EntityField record (Key record)
- fieldLens :: EntityField record field -> forall f. Functor f => (field -> f field) -> Entity record -> f (Entity record)
- class PersistField a where
- toPersistValue :: a -> PersistValue
- fromPersistValue :: PersistValue -> Either Text a
- class PersistConfig c where
- type PersistConfigBackend c :: (* -> *) -> * -> *
- type PersistConfigPool c
- loadConfig :: Value -> Parser c
- applyEnv :: c -> IO c
- createPoolConfig :: c -> IO (PersistConfigPool c)
- runPool :: (MonadBaseControl IO m, MonadIO m) => c -> PersistConfigBackend c m a -> PersistConfigPool c -> m a
- keyValueEntityToJSON :: ToJSON e => Entity e -> Value
- keyValueEntityFromJSON :: FromJSON e => Value -> Parser (Entity e)
- entityIdToJSON :: ToJSON e => Entity e -> Value
- entityIdFromJSON :: FromJSON e => Value -> Parser (Entity e)
PersistStore
class MonadIO m => PersistStore m where
Associated Types
type PersistMonadBackend m
Methods
get :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val) => Key val -> m (Maybe val)
Get a record by identifier, if available.
insert :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val) => val -> m (Key val)
Create a new record in the database, returning an automatically created key (in SQL an auto-increment id).
insert_ :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val) => val -> m ()
Same as insert
, but doesn't return a Key
.
insertMany :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val) => [val] -> m [Key val]
Create multiple records in the database.
SQL backends currently use the slow default implementation of
mapM insert
insertKey :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val) => Key val -> val -> m ()
Create a new record in the database using the given key.
repsert :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val) => Key val -> val -> m ()
Put the record in the database with the given key.
Unlike replace
, if a record with the given key does not
exist then a new record will be inserted.
replace :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val) => Key val -> val -> m ()
Replace the record in the database with the given
key. Note that the result is undefined if such record does
not exist, so you must use 'insertKey or repsert
in
these cases.
delete :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val) => Key val -> m ()
Delete a specific record by identifier. Does nothing if record does not exist.
Instances
PersistStore m => PersistStore (MaybeT m) | |
PersistStore m => PersistStore (LoggingT m) | |
PersistStore m => PersistStore (ListT m) | |
PersistStore m => PersistStore (ResourceT m) | |
PersistStore m => PersistStore (IdentityT m) | |
(MonadResource m, MonadLogger m) => PersistStore (SqlPersistT m) | |
(Monoid w, PersistStore m) => PersistStore (WriterT w m) | |
(Monoid w, PersistStore m) => PersistStore (WriterT w m) | |
PersistStore m => PersistStore (StateT s m) | |
PersistStore m => PersistStore (StateT s m) | |
PersistStore m => PersistStore (ReaderT r m) | |
(Error e, PersistStore m) => PersistStore (ErrorT e m) | |
PersistStore m => PersistStore (ContT r m) | |
PersistStore m => PersistStore (ConduitM i o m) | |
(Monoid w, PersistStore m) => PersistStore (RWST r w s m) | |
(Monoid w, PersistStore m) => PersistStore (RWST r w s m) | |
PersistStore m => PersistStore (Pipe l i o u m) |
getJust :: (PersistStore m, PersistEntity val, Show (Key val), PersistMonadBackend m ~ PersistEntityBackend val) => Key val -> m val
Same as get, but for a non-null (not Maybe) foreign key Unsafe unless your database is enforcing that the foreign key is valid
belongsTo :: (PersistStore m, PersistEntity ent1, PersistEntity ent2, PersistMonadBackend m ~ PersistEntityBackend ent2) => (ent1 -> Maybe (Key ent2)) -> ent1 -> m (Maybe ent2)
curry this to make a convenience function that loads an associated model > foreign = belongsTo foeignId
belongsToJust :: (PersistStore m, PersistEntity ent1, PersistEntity ent2, PersistMonadBackend m ~ PersistEntityBackend ent2) => (ent1 -> Key ent2) -> ent1 -> m ent2
same as belongsTo, but uses getJust
and therefore is similarly unsafe
PersistUnique
class PersistStore m => PersistUnique m where
Queries against unique keys (other than the id).
Please read the general Persistent documentation to learn how to create Unique keys. SQL backends automatically create uniqueness constraints, but for MongoDB you must manually place a unique index on the field.
Some functions in this module (insertUnique, insertBy, and replaceUnique) first query the unique indexes to check for conflicts. You could instead optimistically attempt to perform the operation (e.g. replace instead of replaceUnique). However,
- there is some fragility to trying to catch the correct exception and determing the column of failure.
- an exception will automatically abort the current SQL transaction
Methods
getBy :: (PersistEntityBackend val ~ PersistMonadBackend m, PersistEntity val) => Unique val -> m (Maybe (Entity val))
Get a record by unique key, if available. Returns also the identifier.
deleteBy :: (PersistEntityBackend val ~ PersistMonadBackend m, PersistEntity val) => Unique val -> m ()
Delete a specific record by unique key. Does nothing if no record matches.
insertUnique :: (PersistEntityBackend val ~ PersistMonadBackend m, PersistEntity val) => val -> m (Maybe (Key val))
Instances
PersistUnique m => PersistUnique (MaybeT m) | |
PersistUnique m => PersistUnique (LoggingT m) | |
PersistUnique m => PersistUnique (ListT m) | |
PersistUnique m => PersistUnique (ResourceT m) | |
PersistUnique m => PersistUnique (IdentityT m) | |
(MonadResource m, MonadLogger m) => PersistUnique (SqlPersistT m) | |
(Monoid w, PersistUnique m) => PersistUnique (WriterT w m) | |
(Monoid w, PersistUnique m) => PersistUnique (WriterT w m) | |
PersistUnique m => PersistUnique (StateT s m) | |
PersistUnique m => PersistUnique (StateT s m) | |
PersistUnique m => PersistUnique (ReaderT r m) | |
(Error e, PersistUnique m) => PersistUnique (ErrorT e m) | |
PersistUnique m => PersistUnique (ContT r m) | |
(Monoid w, PersistUnique m) => PersistUnique (RWST r w s m) | |
(Monoid w, PersistUnique m) => PersistUnique (RWST r w s m) | |
PersistUnique m => PersistUnique (Pipe l i o u m) |
getByValue :: (PersistEntity value, PersistUnique m, PersistEntityBackend value ~ PersistMonadBackend m) => value -> m (Maybe (Entity value))
A modification of getBy
, which takes the PersistEntity
itself instead
of a Unique
value. Returns a value matching one of the unique keys. This
function makes the most sense on entities with a single Unique
constructor.
insertBy :: (PersistEntity val, PersistUnique m, PersistEntityBackend val ~ PersistMonadBackend m) => val -> m (Either (Entity val) (Key val))
replaceUnique :: (Eq record, Eq (Unique record), PersistEntityBackend record ~ PersistMonadBackend m, PersistEntity record, PersistStore m, PersistUnique m) => Key record -> record -> m (Maybe (Unique record))
PersistQuery
class PersistStore m => PersistQuery m where
Minimal complete definition
update, updateWhere, deleteWhere, selectSource, selectKeys, count
Methods
update :: (PersistEntity val, PersistEntityBackend val ~ PersistMonadBackend m) => Key val -> [Update val] -> m ()
Update individual fields on a specific record.
updateGet :: (PersistEntity val, PersistMonadBackend m ~ PersistEntityBackend val) => Key val -> [Update val] -> m val
Update individual fields on a specific record, and retrieve the updated value from the database.
Note that this function will throw an exception if the given key is not found in the database.
updateWhere :: (PersistEntity val, PersistEntityBackend val ~ PersistMonadBackend m) => [Filter val] -> [Update val] -> m ()
Update individual fields on any record matching the given criterion.
deleteWhere :: (PersistEntity val, PersistEntityBackend val ~ PersistMonadBackend m) => [Filter val] -> m ()
Delete all records matching the given criterion.
selectSource :: (PersistEntity val, PersistEntityBackend val ~ PersistMonadBackend m) => [Filter val] -> [SelectOpt val] -> Source m (Entity val)
Get all records matching the given criterion in the specified order. Returns also the identifiers.
selectFirst :: (PersistEntity val, PersistEntityBackend val ~ PersistMonadBackend m) => [Filter val] -> [SelectOpt val] -> m (Maybe (Entity val))
get just the first record for the criterion
selectKeys :: (PersistEntity val, PersistEntityBackend val ~ PersistMonadBackend m) => [Filter val] -> [SelectOpt val] -> Source m (Key val)
Get the Key
s of all records matching the given criterion.
count :: (PersistEntity val, PersistEntityBackend val ~ PersistMonadBackend m) => [Filter val] -> m Int
The total number of records fulfilling the given criterion.
Instances
PersistQuery m => PersistQuery (MaybeT m) | |
PersistQuery m => PersistQuery (LoggingT m) | |
PersistQuery m => PersistQuery (ListT m) | |
PersistQuery m => PersistQuery (ResourceT m) | |
PersistQuery m => PersistQuery (IdentityT m) | |
(MonadResource m, MonadLogger m) => PersistQuery (SqlPersistT m) | |
(Monoid w, PersistQuery m) => PersistQuery (WriterT w m) | |
(Monoid w, PersistQuery m) => PersistQuery (WriterT w m) | |
PersistQuery m => PersistQuery (StateT s m) | |
PersistQuery m => PersistQuery (StateT s m) | |
PersistQuery m => PersistQuery (ReaderT r m) | |
(Error e, PersistQuery m) => PersistQuery (ErrorT e m) | |
PersistQuery m => PersistQuery (ContT r m) | |
PersistQuery m => PersistQuery (ConduitM i o m) | |
(Monoid w, PersistQuery m) => PersistQuery (RWST r w s m) | |
(Monoid w, PersistQuery m) => PersistQuery (RWST r w s m) | |
PersistQuery m => PersistQuery (Pipe l i o u m) |
selectList :: (PersistEntity val, PersistQuery m, PersistEntityBackend val ~ PersistMonadBackend m) => [Filter val] -> [SelectOpt val] -> m [Entity val]
Call selectSource
but return the result as a list.
selectKeysList :: (PersistEntity val, PersistQuery m, PersistEntityBackend val ~ PersistMonadBackend m) => [Filter val] -> [SelectOpt val] -> m [Key val]
Call selectKeys
but return the result as a list.
DeleteCascade
class (PersistStore m, PersistEntity a, PersistEntityBackend a ~ PersistMonadBackend m) => DeleteCascade a m where
Methods
deleteCascade :: Key a -> m ()
deleteCascadeWhere :: (DeleteCascade a m, PersistQuery m) => [Filter a] -> m ()
PersistEntity
class PersistEntity record where
Persistent serialized Haskell records to the database.
A Database Entity
(A row in SQL, a document in MongoDB, etc)
corresponds to a Key
plus a Haskell record.
For every Haskell record type stored in the database there is a corresponding PersistEntity
instance.
An instance of PersistEntity contains meta-data for the record.
PersistEntity also helps abstract over different record types.
That way the same query interface can return a PersistEntity
, with each query returning different types of Haskell records.
Some advanced type system capabilities are used to make this process type-safe. Persistent users usually don't need to understand the class associated data and functions.
Associated Types
data EntityField record :: * -> *
An EntityField
is parameterised by the Haskell record it belongs to
and the additional type of that field
type PersistEntityBackend record
Persistent allows multiple different backends
data Unique record
Unique keys besided the Key
Methods
persistFieldDef :: EntityField record typ -> FieldDef SqlType
return meta-data for a given EntityField
entityDef :: Monad m => m record -> EntityDef SqlType
retrieve the EntityDef meta-data for the record
toPersistFields :: record -> [SomePersistField]
Get the database fields of a record
fromPersistValues :: [PersistValue] -> Either Text record
Convert from database values to a Haskell record
persistUniqueToFieldNames :: Unique record -> [(HaskellName, DBName)]
persistUniqueToValues :: Unique record -> [PersistValue]
persistUniqueKeys :: record -> [Unique record]
persistIdField :: EntityField record (Key record)
fieldLens :: EntityField record field -> forall f. Functor f => (field -> f field) -> Entity record -> f (Entity record)
PersistField
class PersistField a where
A value which can be marshalled to and from a PersistValue
.
Instances
PersistField Bool | |
PersistField Double | |
PersistField Int | |
PersistField Int8 | |
PersistField Int16 | |
PersistField Int32 | |
PersistField Int64 | |
PersistField Rational | |
PersistField Word | |
PersistField Word8 | |
PersistField Word16 | |
PersistField Word32 | |
PersistField Word64 | |
PersistField String | |
PersistField ByteString | |
PersistField Text | |
PersistField UTCTime | |
PersistField Text | |
PersistField Html | |
PersistField ZonedTime | |
PersistField TimeOfDay | |
PersistField Day | |
PersistField PersistValue | |
PersistField Checkmark | |
PersistField SomePersistField | |
PersistField a => PersistField [a] | |
PersistField a => PersistField (Maybe a) | |
HasResolution a => PersistField (Fixed a) | |
(Ord a, PersistField a) => PersistField (Set a) | |
PersistField entity => PersistField (Entity entity) | |
(PersistField a, PersistField b) => PersistField (a, b) | |
PersistField v => PersistField (Map Text v) | |
PersistField (KeyBackend backend entity) |
PersistConfig
class PersistConfig c where
Represents a value containing all the configuration options for a specific backend. This abstraction makes it easier to write code that can easily swap backends.
Minimal complete definition
Methods
loadConfig :: Value -> Parser c
Load the config settings from a Value
, most likely taken from a YAML
config file.
Modify the config settings based on environment variables.
createPoolConfig :: c -> IO (PersistConfigPool c)
Create a new connection pool based on the given config settings.
runPool :: (MonadBaseControl IO m, MonadIO m) => c -> PersistConfigBackend c m a -> PersistConfigPool c -> m a
Run a database action by taking a connection from the pool.
Instances
(PersistConfig c1, PersistConfig c2, (~) * (PersistConfigPool c1) (PersistConfigPool c2), (~) ((* -> *) -> * -> *) (PersistConfigBackend c1) (PersistConfigBackend c2)) => PersistConfig (Either c1 c2) |
JSON utilities
keyValueEntityToJSON :: ToJSON e => Entity e -> Value
Predefined toJSON
. The resulting JSON looks like
{"key": 1, "value": {"name": ...}}
.
The typical usage is:
instance ToJSON User where toJSON = keyValueEntityToJSON
keyValueEntityFromJSON :: FromJSON e => Value -> Parser (Entity e)
Predefined parseJSON
. The input JSON looks like
{"key": 1, "value": {"name": ...}}
.
The typical usage is:
instance FromJSON User where parseJSON = keyValueEntityFromJSON
entityIdToJSON :: ToJSON e => Entity e -> Value
Predefined toJSON
. The resulting JSON looks like
{"id": 1, "name": ...}
.
The typical usage is:
instance ToJSON User where toJSON = entityIdToJSON
entityIdFromJSON :: FromJSON e => Value -> Parser (Entity e)
Predefined parseJSON
. The input JSON looks like
{"id": 1, "name": ...}
.
The typical usage is:
instance FromJSON User where parseJSON = entityIdFromJSON