persistent-1.1.5.1: Type-safe, multi-backend data serialization.

Safe Haskell | None

Database.Persist

Contents

- query combinators

Synopsis

- class PersistField a where
  - toPersistValue :: a -> PersistValue
  - fromPersistValue :: PersistValue -> Either Text a
  - sqlType :: a -> SqlType
  - isNullable :: a -> Bool
- class PersistEntity val where
  - data EntityField val :: * -> *
  - type PersistEntityBackend val
  - data Unique val
  - persistFieldDef :: EntityField val typ -> FieldDef
  - entityDef :: val -> EntityDef
  - toPersistFields :: val -> [SomePersistField]
  - fromPersistValues :: [PersistValue] -> Either Text val
  - halfDefined :: val
  - persistUniqueToFieldNames :: Unique val -> [(HaskellName, DBName)]
  - persistUniqueToValues :: Unique val -> [PersistValue]
  - persistUniqueKeys :: val -> [Unique val]
  - persistIdField :: EntityField val (Key val)
  - fieldLens :: EntityField val field -> forall f. Functor f => (field -> f field) -> Entity val -> f (Entity val)
- class MonadIO m => PersistStore m where
  - type PersistMonadBackend m
  - insert :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val) => val -> m (Key val)
  - insert_ :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val) => val -> m ()
  - 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 ()
  - get :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val) => Key val -> m (Maybe val)
- 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))
- 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
- newtype KeyBackend backend entity = Key {
  - unKey :: PersistValue
  }
- type Key val = KeyBackend (PersistEntityBackend val) val
- data Entity entity = Entity {
  - entityKey :: Key entity
  - entityVal :: entity
  }
- insertBy :: (PersistEntity v, PersistStore m, PersistUnique m, PersistMonadBackend m ~ PersistEntityBackend v) => v -> m (Either (Entity v) (Key v))
- 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
- getByValue :: (PersistEntity v, PersistUnique m, PersistEntityBackend v ~ PersistMonadBackend m) => v -> m (Maybe (Entity v))
- checkUnique :: (PersistEntityBackend val ~ PersistMonadBackend m, PersistEntity val, PersistUnique m) => val -> m Bool
- selectList :: (PersistEntity val, PersistQuery m, PersistEntityBackend val ~ PersistMonadBackend m) => [Filter val] -> [SelectOpt val] -> m [Entity val]
- deleteCascadeWhere :: (DeleteCascade a m, PersistQuery m) => [Filter a] -> m ()
- data SelectOpt v
  - = forall typ . Asc (EntityField v typ)
  - | forall typ . Desc (EntityField v typ)
  - | OffsetBy Int
  - | LimitTo Int
- data Filter v
  - = forall typ . PersistField typ => Filter {
    - filterField :: EntityField v typ
    - filterValue :: Either typ [typ]
    - filterFilter :: PersistFilter
    }
  - | FilterAnd [Filter v]
  - | FilterOr [Filter v]
  - | BackendFilter (BackendSpecificFilter (PersistEntityBackend v) v)
- (=.) :: forall v typ. PersistField typ => EntityField v typ -> typ -> Update v
- (+=.) :: forall v typ. PersistField typ => EntityField v typ -> typ -> Update v
- (-=.) :: forall v typ. PersistField typ => EntityField v typ -> typ -> Update v
- (*=.) :: forall v typ. PersistField typ => EntityField v typ -> typ -> Update v
- (/=.) :: forall v typ. PersistField typ => EntityField v typ -> typ -> Update v
- (==.) :: forall v typ. PersistField typ => EntityField v typ -> typ -> Filter v
- (!=.) :: forall v typ. PersistField typ => EntityField v typ -> typ -> Filter v
- (<.) :: forall v typ. PersistField typ => EntityField v typ -> typ -> Filter v
- (>.) :: forall v typ. PersistField typ => EntityField v typ -> typ -> Filter v
- (<=.) :: forall v typ. PersistField typ => EntityField v typ -> typ -> Filter v
- (>=.) :: forall v typ. PersistField typ => EntityField v typ -> typ -> Filter v
- (<-.) :: forall v typ. PersistField typ => EntityField v typ -> [typ] -> Filter v
- (/<-.) :: forall v typ. PersistField typ => EntityField v typ -> [typ] -> Filter v
- (||.) :: forall v. [Filter v] -> [Filter v] -> [Filter v]

# Documentation

class PersistField a where

A value which can be marshalled to and from a `PersistValue`.

Methods

toPersistValue :: a -> PersistValue

fromPersistValue :: PersistValue -> Either Text a

sqlType :: a -> SqlType

isNullable :: a -> Bool

Instances

PersistField Bool |
PersistField Double |
PersistField Int |
PersistField Int8 |
PersistField Int16 |
PersistField Int32 |
PersistField Int64 |
PersistField Word8 |
PersistField Word16 |
PersistField Word32 |
PersistField Word64 |
PersistField String |
PersistField Text |
PersistField UTCTime |
PersistField ByteString |
PersistField Text |
PersistField Html |
PersistField ZonedTime |
PersistField TimeOfDay |
PersistField Day |
PersistField SomePersistField |
PersistField PersistValue |
PersistField Checkmark |
PersistField a => PersistField [a] |
PersistField a => PersistField (Maybe a) |
(Ord a, PersistField a) => PersistField (Set a) |
(PersistField a, PersistField b) => PersistField (a, b) |
PersistField v => PersistField (Map Text v) |
PersistField (KeyBackend backend entity) |

class PersistEntity val where

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.

Associated Types

data EntityField val :: * -> *

Parameters: val and datatype of the field

type PersistEntityBackend val

data Unique val

Unique keys in existence on this entity.

Methods

persistFieldDef :: EntityField val typ -> FieldDef

entityDef :: val -> EntityDef

toPersistFields :: val -> [SomePersistField]

fromPersistValues :: [PersistValue] -> Either Text val

halfDefined :: val

persistUniqueToFieldNames :: Unique val -> [(HaskellName, DBName)]

persistUniqueToValues :: Unique val -> [PersistValue]

persistUniqueKeys :: val -> [Unique val]

persistIdField :: EntityField val (Key val)

fieldLens :: EntityField val field -> forall f. Functor f => (field -> f field) -> Entity val -> f (Entity val)

class MonadIO m => PersistStore m where

Associated Types

type PersistMonadBackend m

Methods

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`.

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.

get :: (PersistMonadBackend m ~ PersistEntityBackend val, PersistEntity val) => Key val -> m (Maybe val)

Get a record by identifier, if available.

Instances

PersistStore m => PersistStore (ResourceT m) |
PersistStore m => PersistStore (LoggingT m) |
PersistStore m => PersistStore (ListT m) |
PersistStore m => PersistStore (MaybeT m) |
PersistStore m => PersistStore (IdentityT m) |
(MonadResource m, MonadLogger m) => PersistStore (SqlPersist m) |
PersistStore m => PersistStore (ContT r m) |
(Error e, PersistStore m) => PersistStore (ErrorT e m) |
PersistStore m => PersistStore (ReaderT r m) |
PersistStore m => PersistStore (StateT s m) |
PersistStore m => PersistStore (StateT s m) |
(Monoid w, PersistStore m) => PersistStore (WriterT w m) |
(Monoid w, PersistStore m) => PersistStore (WriterT w 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) |

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 place a unique index on the field.

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))

Like `insert`, but returns `Nothing` when the record couldn't be inserted because of a uniqueness constraint.

Instances

PersistUnique m => PersistUnique (ResourceT m) |
PersistUnique m => PersistUnique (LoggingT m) |
PersistUnique m => PersistUnique (ListT m) |
PersistUnique m => PersistUnique (MaybeT m) |
PersistUnique m => PersistUnique (IdentityT m) |
(MonadResource m, MonadLogger m) => PersistUnique (SqlPersist m) |
PersistUnique m => PersistUnique (ContT r m) |
(Error e, PersistUnique m) => PersistUnique (ErrorT e m) |
PersistUnique m => PersistUnique (ReaderT r m) |
PersistUnique m => PersistUnique (StateT s m) |
PersistUnique m => PersistUnique (StateT s m) |
(Monoid w, PersistUnique m) => PersistUnique (WriterT w m) |
(Monoid w, PersistUnique m) => PersistUnique (WriterT w 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) |

class PersistStore m => PersistQuery m where

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 (ResourceT m) |
PersistQuery m => PersistQuery (LoggingT m) |
PersistQuery m => PersistQuery (ListT m) |
PersistQuery m => PersistQuery (MaybeT m) |
PersistQuery m => PersistQuery (IdentityT m) |
(MonadResource m, MonadLogger m) => PersistQuery (SqlPersist m) |
PersistQuery m => PersistQuery (ContT r m) |
(Error e, PersistQuery m) => PersistQuery (ErrorT e m) |
PersistQuery m => PersistQuery (ReaderT r m) |
PersistQuery m => PersistQuery (StateT s m) |
PersistQuery m => PersistQuery (StateT s m) |
(Monoid w, PersistQuery m) => PersistQuery (WriterT w m) |
(Monoid w, PersistQuery m) => PersistQuery (WriterT w 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) |

newtype KeyBackend backend entity

Constructors

Key |

Fields

unKey :: PersistValue

Instances

Eq (KeyBackend backend entity) |
Ord (KeyBackend backend entity) |
Read (KeyBackend backend entity) |
Show (KeyBackend backend entity) |
ToJSON (KeyBackend backend entity) |
FromJSON (KeyBackend backend entity) |
PathPiece (KeyBackend SqlBackend entity) |
PersistField (KeyBackend backend entity) |

type Key val = KeyBackend (PersistEntityBackend val) val

Helper wrapper, equivalent to `Key (PersistEntityBackend val) val`.

Since 1.1.0

data Entity entity

Datatype that represents an entity, with both its key and its Haskell representation.

When using the an SQL-based backend (such as SQLite or PostgreSQL), an `Entity` may take any number of columns depending on how many fields it has. In order to reconstruct your entity on the Haskell side, `persistent` needs all of your entity columns and in the right order. Note that you don't need to worry about this when using `persistent`'s API since everything is handled correctly behind the scenes.

However, if you want to issue a raw SQL command that returns an `Entity`, then you have to be careful with the column order. While you could use `SELECT Entity.* WHERE ...` and that would work most of the time, there are times when the order of the columns on your database is different from the order that `persistent` expects (for example, if you add a new field in the middle of you entity definition and then use the migration code -- `persistent` will expect the column to be in the middle, but your DBMS will put it as the last column). So, instead of using a query like the one above, you may use `rawSql` (from the Database.Persist.GenericSql module) with its /entity selection placeholder/ (a double question mark `??`). Using `rawSql` the query above must be written as `SELECT ?? WHERE ..`. Then `rawSql` will replace `??` with the list of all columns that we need from your entity in the right order. If your query returns two entities (i.e. `(Entity backend a, Entity backend b)`), then you must you use `SELECT ??, ?? WHERE ...`, and so on.

Constructors

Entity |

Fields

entityKey :: Key entity
entityVal :: entity

Instances

Eq entity => Eq (Entity entity) |
Ord entity => Ord (Entity entity) |
Read entity => Read (Entity entity) |
Show entity => Show (Entity entity) |
PersistEntity a => RawSql (Entity a) |

insertBy :: (PersistEntity v, PersistStore m, PersistUnique m, PersistMonadBackend m ~ PersistEntityBackend v) => v -> m (Either (Entity v) (Key v))

Insert a value, checking for conflicts with any unique constraints. If a duplicate exists in the database, it is returned as `Left`. Otherwise, the new 'Key is returned as `Right`.

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)

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

getByValue :: (PersistEntity v, PersistUnique m, PersistEntityBackend v ~ PersistMonadBackend m) => v -> m (Maybe (Entity v))

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.

checkUnique :: (PersistEntityBackend val ~ PersistMonadBackend m, PersistEntity val, PersistUnique m) => val -> m Bool

Check whether there are any conflicts for unique keys with this entity and existing entities in the database.

Returns `True` if the entity would be unique, and could thus safely be `insert`ed; returns `False` on a conflict.

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.

deleteCascadeWhere :: (DeleteCascade a m, PersistQuery m) => [Filter a] -> m ()

data SelectOpt v

Constructors

forall typ . Asc (EntityField v typ) |
forall typ . Desc (EntityField v typ) |
OffsetBy Int |
LimitTo Int |

data Filter v

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.

Constructors

forall typ . PersistField typ => Filter |

Fields

filterField :: EntityField v typ
filterValue :: Either typ [typ]
filterFilter :: PersistFilter

FilterAnd [Filter v] | convenient for internal use, not needed for the API
FilterOr [Filter v] |
BackendFilter (BackendSpecificFilter (PersistEntityBackend v) v) |

# query combinators

(=.) :: forall v typ. PersistField typ => EntityField v typ -> typ -> Update v

assign a field a value

(+=.) :: forall v typ. PersistField typ => EntityField v typ -> typ -> Update v

assign a field by addition (+=)

(-=.) :: forall v typ. PersistField typ => EntityField v typ -> typ -> Update v

assign a field by subtraction (-=)

(*=.) :: forall v typ. PersistField typ => EntityField v typ -> typ -> Update v

assign a field by multiplication (*=)

(/=.) :: forall v typ. PersistField typ => EntityField v typ -> typ -> Update v

assign a field by division (/=)

(==.) :: forall v typ. PersistField typ => EntityField v typ -> typ -> Filter v

(!=.) :: forall v typ. PersistField typ => EntityField v typ -> typ -> Filter v

(<.) :: forall v typ. PersistField typ => EntityField v typ -> typ -> Filter v

(>.) :: forall v typ. PersistField typ => EntityField v typ -> typ -> Filter v

(<=.) :: forall v typ. PersistField typ => EntityField v typ -> typ -> Filter v

(>=.) :: forall v typ. PersistField typ => EntityField v typ -> typ -> Filter v

(<-.) :: forall v typ. PersistField typ => EntityField v typ -> [typ] -> Filter v

In

(/<-.) :: forall v typ. PersistField typ => EntityField v typ -> [typ] -> Filter v

NotIn

(||.) :: forall v. [Filter v] -> [Filter v] -> [Filter v]

the OR of two lists of filters