API for database actions. The API deals with fields and entities. In SQL, a field corresponds to a column, and should be a single non-composite value. An entity corresponds to a SQL table, so an entity is a collection of fields.
- data PersistValue
- data SqlType
- = SqlString
- | SqlInt32
- | 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
- data EntityField val :: * -> *
- data Unique val :: ((* -> *) -> * -> *) -> *
- persistColumnDef :: EntityField val typ -> ColumnDef
- entityDef :: val -> EntityDef
- toPersistFields :: val -> [SomePersistField]
- fromPersistValues :: [PersistValue] -> Either String val
- halfDefined :: val
- persistUniqueToFieldNames :: Unique val backend -> [String]
- persistUniqueToValues :: Unique val backend -> [PersistValue]
- persistUniqueKeys :: val -> [Unique val backend]
- class (MonadIO (b m), MonadIO m, Monad (b m), Monad m) => PersistBackend b m where
- insert :: PersistEntity val => val -> b m (Key b val)
- replace :: PersistEntity val => Key b val -> val -> b m ()
- update :: PersistEntity val => Key b val -> [Update val] -> b m ()
- updateWhere :: PersistEntity val => [Filter val] -> [Update val] -> b m ()
- delete :: PersistEntity val => Key b val -> b m ()
- deleteBy :: PersistEntity val => Unique val b -> b m ()
- deleteWhere :: PersistEntity val => [Filter val] -> b m ()
- get :: PersistEntity val => Key b val -> b m (Maybe val)
- getBy :: PersistEntity val => Unique val b -> b m (Maybe (Key b val, val))
- selectEnum :: PersistEntity val => [Filter val] -> [SelectOpt val] -> Enumerator (Key b val, val) (b m) a
- selectFirst :: PersistEntity val => [Filter val] -> [SelectOpt val] -> b m (Maybe (Key b val, val))
- selectKeys :: PersistEntity val => [Filter val] -> Enumerator (Key b val) (b m) a
- count :: PersistEntity val => [Filter val] -> b m Int
- data PersistFilter
- data PersistUpdate
- data SelectOpt v
- = forall typ . Asc (EntityField v typ)
- | forall typ . Desc (EntityField v typ)
- | OffsetBy Int
- | LimitTo Int
- data SomePersistField = forall a . PersistField a => SomePersistField a
- selectList :: (PersistEntity val, PersistBackend b m) => [Filter val] -> [SelectOpt val] -> b m [(Key b val, val)]
- insertBy :: (PersistEntity v, PersistBackend b m) => v -> b m (Either (Key b v, v) (Key b v))
- getByValue :: (PersistEntity v, PersistBackend b m) => v -> b m (Maybe (Key b v, v))
- getJust :: (PersistBackend b m, PersistEntity val, Show (Key b val)) => Key b val -> b m val
- belongsTo :: (PersistBackend b m, PersistEntity ent1, PersistEntity ent2) => (ent1 -> Maybe (Key b ent2)) -> ent1 -> b m (Maybe ent2)
- belongsToJust :: (PersistBackend b m, PersistEntity ent1, PersistEntity ent2) => (ent1 -> Key b ent2) -> ent1 -> b m ent2
- checkUnique :: (PersistEntity val, PersistBackend b m) => val -> b m Bool
- class PersistEntity a => DeleteCascade a b where
- deleteCascade :: PersistBackend b m => Key b a -> b m ()
- deleteCascadeWhere :: (DeleteCascade a b, PersistBackend b m) => [Filter a] -> b m ()
- data PersistException
- data Update v = forall typ . PersistField typ => Update {
- updateField :: EntityField v typ
- updateValue :: typ
- updateUpdate :: PersistUpdate
- updateFieldName :: PersistEntity v => Update v -> String
- data Filter v
- = forall typ . PersistField typ => Filter {
- filterField :: EntityField v typ
- filterValue :: Either typ [typ]
- filterFilter :: PersistFilter
- | FilterAnd [Filter v]
- | FilterOr [Filter v]
- = forall typ . PersistField typ => Filter {
- newtype Key backend entity = Key {}
- data EntityDef = EntityDef {
- entityName :: String
- entityAttribs :: [String]
- entityColumns :: [ColumnDef]
- entityUniques :: [UniqueDef]
- entityDerives :: [String]
- type ColumnName = String
- type ColumnType = String
- data ColumnDef = ColumnDef {}
- data UniqueDef = UniqueDef {
- uniqueName :: String
- uniqueColumns :: [ColumnName]
- fst3 :: forall t t1 t2. (t, t1, t2) -> t
- snd3 :: forall t t1 t2. (t, t1, t2) -> t1
- third3 :: forall t t1 t2. (t, t1, t2) -> t2
- limitOffsetOrder :: PersistEntity val => [SelectOpt val] -> (Int, Int, [SelectOpt val])
- class PersistConfig c where
- type PersistConfigBackend c :: (* -> *) -> * -> *
- type PersistConfigPool c
- loadConfig :: TextObject -> Either String c
- withPool :: (Applicative m, MonadControlIO m) => c -> (PersistConfigPool c -> m a) -> m a
- runPool :: MonadControlIO m => c -> PersistConfigBackend c m a -> PersistConfigPool c -> m a
Documentation
data PersistValue Source
A raw value which can be stored in any backend and can be marshalled to
and from a PersistField
.
PersistText Text | |
PersistByteString ByteString | |
PersistInt64 Int64 | |
PersistDouble Double | |
PersistBool Bool | |
PersistDay Day | |
PersistTimeOfDay TimeOfDay | |
PersistUTCTime UTCTime | |
PersistNull | |
PersistList [PersistValue] | |
PersistMap [(Text, PersistValue)] | |
PersistObjectId ByteString | intended especially for MongoDB backend |
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.
SqlString | |
SqlInt32 | |
SqlInteger | FIXME 8-byte integer; should be renamed SqlInt64 |
SqlReal | |
SqlBool | |
SqlDay | |
SqlTime | |
SqlDayTime | |
SqlBlob |
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
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.
data EntityField val :: * -> *Source
Parameters: val and datatype of the field
data Unique val :: ((* -> *) -> * -> *) -> *Source
Unique keys in existence on this entity.
persistColumnDef :: EntityField val typ -> ColumnDefSource
entityDef :: val -> EntityDefSource
toPersistFields :: val -> [SomePersistField]Source
fromPersistValues :: [PersistValue] -> Either String valSource
halfDefined :: valSource
persistUniqueToFieldNames :: Unique val backend -> [String]Source
persistUniqueToValues :: Unique val backend -> [PersistValue]Source
persistUniqueKeys :: val -> [Unique val backend]Source
class (MonadIO (b m), MonadIO m, Monad (b m), Monad m) => PersistBackend b m whereSource
insert :: PersistEntity val => val -> b m (Key b val)Source
Create a new record in the database, returning the newly created identifier.
replace :: PersistEntity val => Key b val -> val -> b m ()Source
Replace the record in the database with the given key. Result is undefined if such a record does not exist.
update :: PersistEntity val => Key b val -> [Update val] -> b m ()Source
Update individual fields on a specific record.
updateWhere :: PersistEntity val => [Filter val] -> [Update val] -> b m ()Source
Update individual fields on any record matching the given criterion.
delete :: PersistEntity val => Key b val -> b m ()Source
Delete a specific record by identifier. Does nothing if record does not exist.
deleteBy :: PersistEntity val => Unique val b -> b m ()Source
Delete a specific record by unique key. Does nothing if no record matches.
deleteWhere :: PersistEntity val => [Filter val] -> b m ()Source
Delete all records matching the given criterion.
get :: PersistEntity val => Key b val -> b m (Maybe val)Source
Get a record by identifier, if available.
getBy :: PersistEntity val => Unique val b -> b m (Maybe (Key b val, val))Source
Get a record by unique key, if available. Returns also the identifier.
selectEnum :: PersistEntity val => [Filter val] -> [SelectOpt val] -> Enumerator (Key b val, val) (b m) aSource
Get all records matching the given criterion in the specified order. Returns also the identifiers.
selectFirst :: PersistEntity val => [Filter val] -> [SelectOpt val] -> b m (Maybe (Key b val, val))Source
get just the first record for the criterion
selectKeys :: PersistEntity val => [Filter val] -> Enumerator (Key b val) (b m) aSource
Get the Key
s of all records matching the given criterion.
count :: PersistEntity val => [Filter val] -> b m IntSource
The total number of records fulfilling the given criterion.
data PersistFilter Source
data PersistUpdate Source
forall typ . Asc (EntityField v typ) | |
forall typ . Desc (EntityField v typ) | |
OffsetBy Int | |
LimitTo Int |
data SomePersistField Source
forall a . PersistField a => SomePersistField a |
selectList :: (PersistEntity val, PersistBackend b m) => [Filter val] -> [SelectOpt val] -> b m [(Key b val, val)]Source
Call select
but return the result as a list.
insertBy :: (PersistEntity v, PersistBackend b m) => v -> b m (Either (Key b v, v) (Key b v))Source
getByValue :: (PersistEntity v, PersistBackend b m) => v -> b m (Maybe (Key b v, v))Source
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.
getJust :: (PersistBackend b m, PersistEntity val, Show (Key b val)) => Key b val -> b m valSource
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 :: (PersistBackend b m, PersistEntity ent1, PersistEntity ent2) => (ent1 -> Maybe (Key b ent2)) -> ent1 -> b m (Maybe ent2)Source
belongsToJust :: (PersistBackend b m, PersistEntity ent1, PersistEntity ent2) => (ent1 -> Key b ent2) -> ent1 -> b m ent2Source
same as belongsTo, but uses getJust
and therefore is similarly unsafe
checkUnique :: (PersistEntity val, PersistBackend b m) => val -> b m BoolSource
class PersistEntity a => DeleteCascade a b whereSource
deleteCascade :: PersistBackend b m => Key b a -> b m ()Source
deleteCascadeWhere :: (DeleteCascade a b, PersistBackend b m) => [Filter a] -> b m ()Source
data PersistException Source
forall typ . PersistField typ => Update | |
|
updateFieldName :: PersistEntity v => Update v -> StringSource
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.
forall typ . PersistField typ => Filter | |
| |
FilterAnd [Filter v] | convenient for internal use, not needed for the API |
FilterOr [Filter v] |
newtype Key backend entity Source
Eq (Key backend entity) | |
Ord (Key backend entity) | |
Read (Key backend entity) | |
Show (Key backend entity) | |
SinglePiece (Key SqlPersist entity) | |
PersistField (Key backend entity) |
Definition
EntityDef | |
|
type ColumnName = StringSource
type ColumnType = StringSource
ColumnDef | |
|
limitOffsetOrder :: PersistEntity val => [SelectOpt val] -> (Int, Int, [SelectOpt val])Source
Config
class PersistConfig c whereSource
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.
type PersistConfigBackend c :: (* -> *) -> * -> *Source
type PersistConfigPool c Source
loadConfig :: TextObject -> Either String cSource
withPool :: (Applicative m, MonadControlIO m) => c -> (PersistConfigPool c -> m a) -> m aSource
I really don't want Applicative here, but it's necessary for Mongo.
runPool :: MonadControlIO m => c -> PersistConfigBackend c m a -> PersistConfigPool c -> m aSource