This defines the API for performing database actions. There are two levels to this API: dealing with fields, and dealing with entities. In SQL, a field corresponds to a column, and should be a single, non-composite value. An entity corresponds to a SQL table. In other words: An entity is a collection of fields.
- data PersistValue
- data SqlType
- = SqlString
- | SqlInteger
- | SqlReal
- | SqlBool
- | SqlDay
- | SqlTime
- | SqlDayTime
- | SqlBlob
- class PersistField a where
- toPersistValue :: a -> PersistValue
- fromPersistValue :: PersistValue -> Either String a
- sqlType :: a -> SqlType
- isNullable :: a -> Bool
- class PersistEntity val where
- type PersistMonad val :: (* -> *) -> * -> *
- data Key val
- data Update val
- data Filter val
- data Order val
- data Unique val
- initialize :: MonadCatchIO m => val -> PersistMonad val m ()
- insert :: MonadCatchIO m => val -> PersistMonad val m (Key val)
- replace :: MonadCatchIO m => Key val -> val -> PersistMonad val m ()
- update :: MonadCatchIO m => Key val -> [Update val] -> PersistMonad val m ()
- updateWhere :: MonadCatchIO m => [Filter val] -> [Update val] -> PersistMonad val m ()
- delete :: MonadCatchIO m => Key val -> PersistMonad val m ()
- deleteBy :: MonadCatchIO m => Unique val -> PersistMonad val m ()
- deleteWhere :: MonadCatchIO m => [Filter val] -> PersistMonad val m ()
- get :: MonadCatchIO m => Key val -> PersistMonad val m (Maybe val)
- getBy :: MonadCatchIO m => Unique val -> PersistMonad val m (Maybe (Key val, val))
- select :: MonadCatchIO m => [Filter val] -> [Order val] -> PersistMonad val m [(Key val, val)]
Fields
data PersistValue Source
A raw value which can be stored in any backend and can be marshalled to
and from a PersistField
.
A SQL data type. Naming attempts to reflect the underlying Haskell datatypes, eg SqlString instead of SqlVarchar. Different SQL databases may have different translations for these types.
class PersistField a whereSource
A value which can be marshalled to and from a PersistValue
.
toPersistValue :: a -> PersistValueSource
fromPersistValue :: PersistValue -> Either String aSource
isNullable :: a -> BoolSource
Entities
class PersistEntity val whereSource
A single database entity. For example, if writing a blog application, a blog entry would be an entry, containing fields such as title and content.
type PersistMonad val :: (* -> *) -> * -> *Source
The monad transformer in which actions for this entity must occur. For example, entities declared to work with a sqlite backend would most likely use a ReaderT monad transformer holding onto a database connection.
By using a monad transformer here, users can allow arbitrary effects to
exist in the underlying monad. For example, Yesod applications can embed
a Handler monad here. The only restriction on that underlying monad is
it must be an instance of MonadCatchIO
.
The unique identifier associated with this entity. In general, backends also define a type synonym for this, such that "type MyEntityId = Key MyEntity".
Fields which can be updated using the update
and updateWhere
functions.
Filters which are available for select
, updateWhere
and
deleteWhere
. Each filter constructor specifies the field being
filtered on, the type of comparison applied (equals, not equals, etc)
and the argument for the comparison.
How you can sort the results of a select
.
Unique keys in existence on this entity.
initialize :: MonadCatchIO m => val -> PersistMonad val m ()Source
Prepare database for this entity, if necessary. In SQL, this creates
values and indices if they don't exist. The first argument is not used,
so you can used undefined
.
insert :: MonadCatchIO m => val -> PersistMonad val m (Key val)Source
Create a new record in the database, returning the newly created identifier.
replace :: MonadCatchIO m => Key val -> val -> PersistMonad val m ()Source
Replace the record in the database with the given key. Result is undefined if such a record does not exist.
update :: MonadCatchIO m => Key val -> [Update val] -> PersistMonad val m ()Source
Update individual fields on a specific record.
updateWhere :: MonadCatchIO m => [Filter val] -> [Update val] -> PersistMonad val m ()Source
Update individual fields on any record matching the given criterion.
delete :: MonadCatchIO m => Key val -> PersistMonad val m ()Source
Delete a specific record by identifier. Does nothing if record does not exist.
deleteBy :: MonadCatchIO m => Unique val -> PersistMonad val m ()Source
Delete a specific record by unique key. Does nothing if no record matches.
deleteWhere :: MonadCatchIO m => [Filter val] -> PersistMonad val m ()Source
Delete all records matching the given criterion.
get :: MonadCatchIO m => Key val -> PersistMonad val m (Maybe val)Source
Get a record by identifier, if available.
getBy :: MonadCatchIO m => Unique val -> PersistMonad val m (Maybe (Key val, val))Source
Get a record by unique key, if available. Returns also the identifier.
select :: MonadCatchIO m => [Filter val] -> [Order val] -> PersistMonad val m [(Key val, val)]Source
Get all records matching the given criterion in the specified order. Returns also the identifiers.