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

Safe HaskellSafe-Infered

Database.Persist

Contents

Synopsis

Documentation

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.

Associated Types

data EntityField val :: * -> *Source

Parameters: val and datatype of the field

type PersistEntityBackend val :: (* -> *) -> * -> *Source

data Unique val :: ((* -> *) -> * -> *) -> *Source

Unique keys in existence on this entity.

class (MonadBaseControl IO m, MonadBaseControl IO (b m)) => PersistStore b m whereSource

Methods

insert :: PersistEntity val => val -> b m (Key b val)Source

Create a new record in the database, returning an automatically created key (in SQL an auto-increment id).

insertKey :: PersistEntity val => Key b val -> val -> b m ()Source

Create a new record in the database using the given key.

repsert :: PersistEntity val => Key b val -> val -> b m ()Source

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 :: PersistEntity val => Key b val -> val -> b m ()Source

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 :: PersistEntity val => Key b val -> b m ()Source

Delete a specific record by identifier. Does nothing if record does not exist.

get :: PersistEntity val => Key b val -> b m (Maybe val)Source

Get a record by identifier, if available.

class PersistStore b m => PersistUnique b m whereSource

Methods

getBy :: (PersistEntityBackend val ~ b, PersistEntity val) => Unique val b -> b m (Maybe (Entity val))Source

Get a record by unique key, if available. Returns also the identifier.

deleteBy :: PersistEntity val => Unique val b -> b m ()Source

Delete a specific record by unique key. Does nothing if no record matches.

insertUnique :: (b ~ PersistEntityBackend val, PersistEntity val) => val -> b m (Maybe (Key b val))Source

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

class PersistStore b m => PersistQuery b m whereSource

Methods

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.

deleteWhere :: PersistEntity val => [Filter val] -> b m ()Source

Delete all records matching the given criterion.

selectSource :: (PersistEntity val, PersistEntityBackend val ~ b) => [Filter val] -> [SelectOpt val] -> Source (ResourceT (b m)) (Entity val)Source

Get all records matching the given criterion in the specified order. Returns also the identifiers.

selectFirst :: (PersistEntity val, PersistEntityBackend val ~ b) => [Filter val] -> [SelectOpt val] -> b m (Maybe (Entity val))Source

get just the first record for the criterion

selectKeys :: PersistEntity val => [Filter val] -> Source (ResourceT (b m)) (Key b val)Source

Get the Keys of all records matching the given criterion.

count :: PersistEntity val => [Filter val] -> b m IntSource

The total number of records fulfilling the given criterion.

newtype Key backend entity Source

Constructors

Key 

Fields

unKey :: PersistValue
 

Instances

Eq (Key backend entity) 
Ord (Key backend entity) 
Read (Key backend entity) 
Show (Key backend entity) 
ToJSON (Key backend entity) 
FromJSON (Key backend entity) 
PathPiece (Key SqlPersist entity) 
PersistField (Key backend entity) 

data Entity entity Source

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 (PersistEntityBackend entity) 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 b m, PersistUnique b m, b ~ PersistEntityBackend v) => v -> b m (Either (Entity v) (Key b v))Source

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 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 :: (PersistStore b m, PersistEntity ent1, PersistEntity ent2) => (ent1 -> Maybe (Key b ent2)) -> ent1 -> b m (Maybe ent2)Source

belongsToJust :: (PersistStore 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

getByValue :: (PersistEntity v, PersistUnique b m, PersistEntityBackend v ~ b) => v -> b m (Maybe (Entity 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.

checkUnique :: (PersistEntityBackend val ~ b, PersistEntity val, PersistUnique b m) => val -> b m BoolSource

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 inserted; returns False on a conflict.

selectList :: (PersistEntity val, PersistQuery b m, PersistEntityBackend val ~ b) => [Filter val] -> [SelectOpt val] -> b m [Entity val]Source

Call select but return the result as a list.

data SelectOpt v Source

Constructors

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

data Filter v Source

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 
FilterAnd [Filter v]

convenient for internal use, not needed for the API

FilterOr [Filter v] 

query combinators

(=.), (/=.), (*=.), (-=.), (+=.) :: forall v typ. PersistField typ => EntityField v typ -> typ -> Update vSource

assign a field a value

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

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

In

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

the OR of two lists of filters