Database.Persist.Class

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

Safe Haskell | None
Language | Haskell98

Database.Persist.Class

Contents

- PersistStore
- PersistUnique
- PersistQuery
- DeleteCascade
- PersistEntity
- PersistField
- PersistConfig
- Lifting
- JSON utilities

Synopsis

- class (PersistEntity record, PersistEntityBackend record ~ backend, PersistCore backend) => ToBackendKey backend record where
- class PersistCore backend where
  - data BackendKey backend
- type PersistStore a = PersistStoreWrite a
- class (Show (BackendKey backend), Read (BackendKey backend), Eq (BackendKey backend), Ord (BackendKey backend), PersistCore backend, PersistField (BackendKey backend), ToJSON (BackendKey backend), FromJSON (BackendKey backend)) => PersistStoreRead backend where
- class (Show (BackendKey backend), Read (BackendKey backend), Eq (BackendKey backend), Ord (BackendKey backend), PersistStoreRead backend, PersistField (BackendKey backend), ToJSON (BackendKey backend), FromJSON (BackendKey backend)) => PersistStoreWrite backend where
- type PersistRecordBackend record backend = (PersistEntity record, PersistEntityBackend record ~ BaseBackend backend)
- getJust :: (PersistStoreRead backend, Show (Key record), PersistRecordBackend record backend, MonadIO m) => Key record -> ReaderT backend m record
- getJustEntity :: (PersistEntityBackend record ~ BaseBackend backend, MonadIO m, PersistEntity record, PersistStoreRead backend) => Key record -> ReaderT backend m (Entity record)
- getEntity :: (PersistStoreWrite backend, PersistRecordBackend e backend, MonadIO m) => Key e -> ReaderT backend m (Maybe (Entity e))
- belongsTo :: (PersistStoreRead backend, PersistEntity ent1, PersistRecordBackend ent2 backend, MonadIO m) => (ent1 -> Maybe (Key ent2)) -> ent1 -> ReaderT backend m (Maybe ent2)
- belongsToJust :: (PersistStoreRead backend, PersistEntity ent1, PersistRecordBackend ent2 backend, MonadIO m) => (ent1 -> Key ent2) -> ent1 -> ReaderT backend m ent2
- insertEntity :: (PersistStoreWrite backend, PersistRecordBackend e backend, MonadIO m) => e -> ReaderT backend m (Entity e)
- insertRecord :: (PersistEntityBackend record ~ BaseBackend backend, PersistEntity record, MonadIO m, PersistStoreWrite backend) => record -> ReaderT backend m record
- type PersistUnique a = PersistUniqueWrite a
- class (PersistCore backend, PersistStoreRead backend) => PersistUniqueRead backend where
- class (PersistUniqueRead backend, PersistStoreWrite backend) => PersistUniqueWrite backend where
- getByValue :: (MonadIO m, PersistUniqueRead backend, PersistRecordBackend record backend) => record -> ReaderT backend m (Maybe (Entity record))
- insertBy :: (MonadIO m, PersistUniqueWrite backend, PersistRecordBackend record backend) => record -> ReaderT backend m (Either (Entity record) (Key record))
- replaceUnique :: (MonadIO m, Eq record, Eq (Unique record), PersistRecordBackend record backend, PersistUniqueWrite backend) => Key record -> record -> ReaderT backend m (Maybe (Unique record))
- checkUnique :: (MonadIO m, PersistRecordBackend record backend, PersistUniqueRead backend) => record -> ReaderT backend m (Maybe (Unique record))
- onlyUnique :: (MonadIO m, PersistUniqueWrite backend, PersistRecordBackend record backend) => record -> ReaderT backend m (Unique record)
- type PersistQuery a = PersistQueryWrite a
- class (PersistCore backend, PersistStoreRead backend) => PersistQueryRead backend where
- class (PersistQueryRead backend, PersistStoreWrite backend) => PersistQueryWrite backend where
- selectSource :: (PersistQueryRead (BaseBackend backend), MonadResource m, PersistEntity record, PersistEntityBackend record ~ BaseBackend (BaseBackend backend), MonadReader backend m, HasPersistBackend backend) => [Filter record] -> [SelectOpt record] -> Source m (Entity record)
- selectKeys :: (PersistQueryRead (BaseBackend backend), MonadResource m, PersistEntity record, BaseBackend (BaseBackend backend) ~ PersistEntityBackend record, MonadReader backend m, HasPersistBackend backend) => [Filter record] -> [SelectOpt record] -> Source m (Key record)
- selectList :: (MonadIO m, PersistQueryRead backend, PersistRecordBackend record backend) => [Filter record] -> [SelectOpt record] -> ReaderT backend m [Entity record]
- selectKeysList :: (MonadIO m, PersistQueryRead backend, PersistRecordBackend record backend) => [Filter record] -> [SelectOpt record] -> ReaderT backend m [Key record]
- class (PersistStoreWrite backend, PersistEntity record, BaseBackend backend ~ PersistEntityBackend record) => DeleteCascade record backend where
- deleteCascadeWhere :: (MonadIO m, DeleteCascade record backend, PersistQueryWrite backend) => [Filter record] -> ReaderT backend m ()
- class (PersistField (Key record), ToJSON (Key record), FromJSON (Key record), Show (Key record), Read (Key record), Eq (Key record), Ord (Key record)) => PersistEntity record where
  - type PersistEntityBackend record
  - data Key record
  - data EntityField record :: * -> *
  - data Unique record
- class PersistField a where
- class PersistConfig c where
  - type PersistConfigBackend c :: (* -> *) -> * -> *
  - type PersistConfigPool c
- entityValues :: PersistEntity record => Entity record -> [PersistValue]
- class HasPersistBackend backend where
  - type BaseBackend backend
- class HasPersistBackend backend => IsPersistBackend backend
- liftPersist :: (MonadIO m, MonadReader backend m, HasPersistBackend backend) => ReaderT (BaseBackend backend) IO b -> m b
- keyValueEntityToJSON :: (PersistEntity record, ToJSON record, ToJSON (Key record)) => Entity record -> Value
- keyValueEntityFromJSON :: (PersistEntity record, FromJSON record, FromJSON (Key record)) => Value -> Parser (Entity record)
- entityIdToJSON :: (PersistEntity record, ToJSON record, ToJSON (Key record)) => Entity record -> Value
- entityIdFromJSON :: (PersistEntity record, FromJSON record, FromJSON (Key record)) => Value -> Parser (Entity record)
- toPersistValueJSON :: ToJSON a => a -> PersistValue
- fromPersistValueJSON :: FromJSON a => PersistValue -> Either Text a

# Documentation

class (PersistEntity record, PersistEntityBackend record ~ backend, PersistCore backend) => ToBackendKey backend record where #

`ToBackendKey` converts a `PersistEntity` `Key` into a `BackendKey` This can be used by each backend to convert between a `Key` and a plain Haskell type. For Sql, that is done with `toSqlKey` and `fromSqlKey`.

By default, a `PersistEntity` uses the default `BackendKey` for its Key and is an instance of ToBackendKey

A `Key` that instead uses a custom type will not be an instance of `ToBackendKey`.

Minimal complete definition

toBackendKey, fromBackendKey

Methods

toBackendKey :: Key record -> BackendKey backend #

fromBackendKey :: BackendKey backend -> Key record #

# PersistStore

class PersistCore backend #

Associated Types

data BackendKey backend #

type PersistStore a = PersistStoreWrite a #

A backwards-compatible alias for those that don't care about distinguishing between read and write queries. It signifies the assumption that, by default, a backend can write as well as read.

class (Show (BackendKey backend), Read (BackendKey backend), Eq (BackendKey backend), Ord (BackendKey backend), PersistCore backend, PersistField (BackendKey backend), ToJSON (BackendKey backend), FromJSON (BackendKey backend)) => PersistStoreRead backend where #

Minimal complete definition

get

Methods

get :: (MonadIO m, PersistRecordBackend record backend) => Key record -> ReaderT backend m (Maybe record) #

Get a record by identifier, if available.

class (Show (BackendKey backend), Read (BackendKey backend), Eq (BackendKey backend), Ord (BackendKey backend), PersistStoreRead backend, PersistField (BackendKey backend), ToJSON (BackendKey backend), FromJSON (BackendKey backend)) => PersistStoreWrite backend where #

Minimal complete definition

insert, insertKey, repsert, replace, delete, update

Methods

insert :: (MonadIO m, PersistRecordBackend record backend) => record -> ReaderT backend m (Key record) #

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

insert_ :: (MonadIO m, PersistRecordBackend record backend) => record -> ReaderT backend m () #

Same as `insert`, but doesn't return a `Key`.

insertMany :: (MonadIO m, PersistRecordBackend record backend) => [record] -> ReaderT backend m [Key record] #

Create multiple records in the database and return their `Key`s.

If you don't need the inserted `Key`s, use `insertMany_`.

The MongoDB and PostgreSQL backends insert all records and retrieve their keys in one database query.

The SQLite and MySQL backends use the slow, default implementation of `mapM insert`.

insertMany_ :: (MonadIO m, PersistRecordBackend record backend) => [record] -> ReaderT backend m () #

Same as `insertMany`, but doesn't return any `Key`s.

The MongoDB, PostgreSQL, SQLite and MySQL backends insert all records in one database query.

insertEntityMany :: (MonadIO m, PersistRecordBackend record backend) => [Entity record] -> ReaderT backend m () #

Same as `insertMany_`, but takes an `Entity` instead of just a record.

Useful when migrating data from one entity to another and want to preserve ids.

The MongoDB backend inserts all the entities in one database query.

The SQL backends use the slow, default implementation of `mapM_ insertKey`.

insertKey :: (MonadIO m, PersistRecordBackend record backend) => Key record -> record -> ReaderT backend m () #

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

repsert :: (MonadIO m, PersistRecordBackend record backend) => Key record -> record -> ReaderT backend 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 :: (MonadIO m, PersistRecordBackend record backend) => Key record -> record -> ReaderT backend 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 :: (MonadIO m, PersistRecordBackend record backend) => Key record -> ReaderT backend m () #

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

update :: (MonadIO m, PersistRecordBackend record backend) => Key record -> [Update record] -> ReaderT backend m () #

Update individual fields on a specific record.

updateGet :: (MonadIO m, PersistRecordBackend record backend) => Key record -> [Update record] -> ReaderT backend m record #

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.

type PersistRecordBackend record backend = (PersistEntity record, PersistEntityBackend record ~ BaseBackend backend) #

A convenient alias for common type signatures

getJust :: (PersistStoreRead backend, Show (Key record), PersistRecordBackend record backend, MonadIO m) => Key record -> ReaderT backend m record #

Same as `get`, but for a non-null (not Maybe) foreign key Unsafe unless your database is enforcing that the foreign key is valid.

getJustEntity :: (PersistEntityBackend record ~ BaseBackend backend, MonadIO m, PersistEntity record, PersistStoreRead backend) => Key record -> ReaderT backend m (Entity record) #

Same as `getJust`, but returns an `Entity` instead of just the record. @since 2.6.1

getEntity :: (PersistStoreWrite backend, PersistRecordBackend e backend, MonadIO m) => Key e -> ReaderT backend m (Maybe (Entity e)) #

Like `get`, but returns the complete `Entity`.

belongsTo :: (PersistStoreRead backend, PersistEntity ent1, PersistRecordBackend ent2 backend, MonadIO m) => (ent1 -> Maybe (Key ent2)) -> ent1 -> ReaderT backend m (Maybe ent2) #

Curry this to make a convenience function that loads an associated model.

    foreign = belongsTo foreignId

belongsToJust :: (PersistStoreRead backend, PersistEntity ent1, PersistRecordBackend ent2 backend, MonadIO m) => (ent1 -> Key ent2) -> ent1 -> ReaderT backend m ent2 #

Same as `belongsTo`, but uses `getJust` and therefore is similarly unsafe.

insertEntity :: (PersistStoreWrite backend, PersistRecordBackend e backend, MonadIO m) => e -> ReaderT backend m (Entity e) #

Like `insert`, but returns the complete `Entity`.

insertRecord :: (PersistEntityBackend record ~ BaseBackend backend, PersistEntity record, MonadIO m, PersistStoreWrite backend) => record -> ReaderT backend m record #

Like `insertEntity` but just returns the record instead of `Entity`. @since 2.6.1

# PersistUnique

type PersistUnique a = PersistUniqueWrite a #

A backwards-compatible alias for those that don't care about distinguishing between read and write queries. It signifies the assumption that, by default, a backend can write as well as read.

class (PersistCore backend, PersistStoreRead backend) => PersistUniqueRead backend where #

Queries against `Unique` keys (other than the id `Key`).

Please read the general Persistent documentation to learn how to create `Unique` keys.

Using this with an Entity without a Unique key leads to undefined behavior. A few of these functions require a single `Unique`, so using an Entity with multiple `Unique`s is also undefined. In these cases persistent's goal is to throw an exception as soon as possible, but persistent is still transitioning to that.

SQL backends automatically create uniqueness constraints, but for MongoDB you must manually place a unique index on a field to have a uniqueness constraint.

Minimal complete definition

getBy

Methods

getBy :: (MonadIO m, PersistRecordBackend record backend) => Unique record -> ReaderT backend m (Maybe (Entity record)) #

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

class (PersistUniqueRead backend, PersistStoreWrite backend) => PersistUniqueWrite backend where #

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.

Minimal complete definition

deleteBy

Methods

deleteBy :: (MonadIO m, PersistRecordBackend record backend) => Unique record -> ReaderT backend m () #

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

insertUnique :: (MonadIO m, PersistRecordBackend record backend) => record -> ReaderT backend m (Maybe (Key record)) #

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

upsert :: (MonadIO m, PersistRecordBackend record backend) => record -> [Update record] -> ReaderT backend m (Entity record) #

Update based on a uniqueness constraint or insert:

- insert the new record if it does not exist;
- update the existing record that matches the uniqueness contraint.

Throws an exception if there is more than 1 uniqueness contraint.

upsertBy :: (MonadIO m, PersistRecordBackend record backend) => Unique record -> record -> [Update record] -> ReaderT backend m (Entity record) #

Update based on a given uniqueness constraint or insert:

- insert the new record if it does not exist;
- update the existing record that matches the given uniqueness contraint.

getByValue :: (MonadIO m, PersistUniqueRead backend, PersistRecordBackend record backend) => record -> ReaderT backend m (Maybe (Entity record)) #

A modification of `getBy`, which takes the `PersistEntity` itself instead of a `Unique` record. Returns a record matching one of the unique keys. This function makes the most sense on entities with a single `Unique` constructor.

insertBy :: (MonadIO m, PersistUniqueWrite backend, PersistRecordBackend record backend) => record -> ReaderT backend m (Either (Entity record) (Key record)) #

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

replaceUnique :: (MonadIO m, Eq record, Eq (Unique record), PersistRecordBackend record backend, PersistUniqueWrite backend) => Key record -> record -> ReaderT backend m (Maybe (Unique record)) #

Attempt to replace the record of the given key with the given new record. First query the unique fields to make sure the replacement maintains uniqueness constraints.

Return `Nothing` if the replacement was made. If uniqueness is violated, return a `Just` with the `Unique` violation

Since 1.2.2.0

checkUnique :: (MonadIO m, PersistRecordBackend record backend, PersistUniqueRead backend) => record -> ReaderT backend m (Maybe (Unique record)) #

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

Returns `Nothing` if the entity would be unique, and could thus safely be inserted. on a conflict returns the conflicting key

onlyUnique :: (MonadIO m, PersistUniqueWrite backend, PersistRecordBackend record backend) => record -> ReaderT backend m (Unique record) #

Return the single unique key for a record.

# PersistQuery

type PersistQuery a = PersistQueryWrite a #

A backwards-compatible alias for those that don't care about distinguishing between read and write queries. It signifies the assumption that, by default, a backend can write as well as read.

class (PersistCore backend, PersistStoreRead backend) => PersistQueryRead backend where #

Backends supporting conditional read operations.

Minimal complete definition

selectSourceRes, selectKeysRes, count

Methods

selectSourceRes :: (PersistRecordBackend record backend, MonadIO m1, MonadIO m2) => [Filter record] -> [SelectOpt record] -> ReaderT backend m1 (Acquire (Source m2 (Entity record))) #

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

selectFirst :: (MonadIO m, PersistRecordBackend record backend) => [Filter record] -> [SelectOpt record] -> ReaderT backend m (Maybe (Entity record)) #

Get just the first record for the criterion.

selectKeysRes :: (MonadIO m1, MonadIO m2, PersistRecordBackend record backend) => [Filter record] -> [SelectOpt record] -> ReaderT backend m1 (Acquire (Source m2 (Key record))) #

Get the `Key`s of all records matching the given criterion.

count :: (MonadIO m, PersistRecordBackend record backend) => [Filter record] -> ReaderT backend m Int #

The total number of records fulfilling the given criterion.

class (PersistQueryRead backend, PersistStoreWrite backend) => PersistQueryWrite backend where #

Backends supporting conditional write operations

Minimal complete definition

updateWhere, deleteWhere

Methods

updateWhere :: (MonadIO m, PersistRecordBackend record backend) => [Filter record] -> [Update record] -> ReaderT backend m () #

Update individual fields on any record matching the given criterion.

deleteWhere :: (MonadIO m, PersistRecordBackend record backend) => [Filter record] -> ReaderT backend m () #

Delete all records matching the given criterion.

selectSource :: (PersistQueryRead (BaseBackend backend), MonadResource m, PersistEntity record, PersistEntityBackend record ~ BaseBackend (BaseBackend backend), MonadReader backend m, HasPersistBackend backend) => [Filter record] -> [SelectOpt record] -> Source m (Entity record) #

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

selectKeys :: (PersistQueryRead (BaseBackend backend), MonadResource m, PersistEntity record, BaseBackend (BaseBackend backend) ~ PersistEntityBackend record, MonadReader backend m, HasPersistBackend backend) => [Filter record] -> [SelectOpt record] -> Source m (Key record) #

Get the `Key`s of all records matching the given criterion.

selectList :: (MonadIO m, PersistQueryRead backend, PersistRecordBackend record backend) => [Filter record] -> [SelectOpt record] -> ReaderT backend m [Entity record] #

Call `selectSource` but return the result as a list.

selectKeysList :: (MonadIO m, PersistQueryRead backend, PersistRecordBackend record backend) => [Filter record] -> [SelectOpt record] -> ReaderT backend m [Key record] #

Call `selectKeys` but return the result as a list.

# DeleteCascade

class (PersistStoreWrite backend, PersistEntity record, BaseBackend backend ~ PersistEntityBackend record) => DeleteCascade record backend where #

For combinations of backends and entities that support cascade-deletion. “Cascade-deletion” means that entries that depend on other entries to be deleted will be deleted as well.

Minimal complete definition

deleteCascade

Methods

deleteCascade :: MonadIO m => Key record -> ReaderT backend m () #

Perform cascade-deletion of single database entry.

deleteCascadeWhere :: (MonadIO m, DeleteCascade record backend, PersistQueryWrite backend) => [Filter record] -> ReaderT backend m () #

Cascade-deletion of entries satisfying given filters.

# PersistEntity

class (PersistField (Key record), ToJSON (Key record), FromJSON (Key record), Show (Key record), Read (Key record), Eq (Key record), Ord (Key record)) => 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.

Minimal complete definition

keyToValues, keyFromValues, persistIdField, entityDef, persistFieldDef, toPersistFields, fromPersistValues, persistUniqueKeys, persistUniqueToFieldNames, persistUniqueToValues, fieldLens

Associated Types

type PersistEntityBackend record #

Persistent allows multiple different backends (databases).

data Key record #

By default, a backend will automatically generate the key Instead you can specify a Primary key made up of unique values.

data EntityField record :: * -> * #

An `EntityField` is parameterised by the Haskell record it belongs to and the additional type of that field.

data Unique record #

Unique keys besides the `Key`.

Methods

keyToValues :: Key record -> [PersistValue] #

A lower-level key operation.

keyFromValues :: [PersistValue] -> Either Text (Key record) #

A lower-level key operation.

persistIdField :: EntityField record (Key record) #

A meta-operation to retrieve the `Key` `EntityField`.

entityDef :: Monad m => m record -> EntityDef #

Retrieve the `EntityDef` meta-data for the record.

persistFieldDef :: EntityField record typ -> FieldDef #

Return meta-data for a given `EntityField`.

toPersistFields :: record -> [SomePersistField] #

A meta-operation to get the database fields of a record.

fromPersistValues :: [PersistValue] -> Either Text record #

A lower-level operation to convert from database values to a Haskell record.

persistUniqueKeys :: record -> [Unique record] #

A meta operation to retrieve all the `Unique` keys.

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

A lower level operation.

persistUniqueToValues :: Unique record -> [PersistValue] #

A lower level operation.

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

Use a `PersistField` as a lens.

# PersistField

class PersistField a where #

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

Minimal complete definition

toPersistValue, fromPersistValue

Methods

toPersistValue :: a -> PersistValue #

fromPersistValue :: PersistValue -> Either Text a #

Instances

PersistField Bool # |
Methods toPersistValue :: Bool -> PersistValue # fromPersistValue :: PersistValue -> Either Text Bool #
PersistField Double # |
Methods toPersistValue :: Double -> PersistValue # fromPersistValue :: PersistValue -> Either Text Double #
PersistField Int # |
Methods toPersistValue :: Int -> PersistValue # fromPersistValue :: PersistValue -> Either Text Int #
PersistField Int8 # |
Methods toPersistValue :: Int8 -> PersistValue # fromPersistValue :: PersistValue -> Either Text Int8 #
PersistField Int16 # |
Methods toPersistValue :: Int16 -> PersistValue # fromPersistValue :: PersistValue -> Either Text Int16 #
PersistField Int32 # |
Methods toPersistValue :: Int32 -> PersistValue # fromPersistValue :: PersistValue -> Either Text Int32 #
PersistField Int64 # |
Methods toPersistValue :: Int64 -> PersistValue # fromPersistValue :: PersistValue -> Either Text Int64 #
PersistField Rational # |
Methods toPersistValue :: Rational -> PersistValue # fromPersistValue :: PersistValue -> Either Text Rational #
PersistField Word # |
Methods toPersistValue :: Word -> PersistValue # fromPersistValue :: PersistValue -> Either Text Word #
PersistField Word8 # |
Methods toPersistValue :: Word8 -> PersistValue # fromPersistValue :: PersistValue -> Either Text Word8 #
PersistField Word16 # |
Methods toPersistValue :: Word16 -> PersistValue # fromPersistValue :: PersistValue -> Either Text Word16 #
PersistField Word32 # |
Methods toPersistValue :: Word32 -> PersistValue # fromPersistValue :: PersistValue -> Either Text Word32 #
PersistField Word64 # |
Methods toPersistValue :: Word64 -> PersistValue # fromPersistValue :: PersistValue -> Either Text Word64 #
PersistField ByteString # |
Methods toPersistValue :: ByteString -> PersistValue # fromPersistValue :: PersistValue -> Either Text ByteString #
PersistField Text # |
Methods toPersistValue :: Text -> PersistValue # fromPersistValue :: PersistValue -> Either Text Text #
PersistField UTCTime # |
Methods toPersistValue :: UTCTime -> PersistValue # fromPersistValue :: PersistValue -> Either Text UTCTime #
PersistField Text # |
Methods toPersistValue :: Text -> PersistValue # fromPersistValue :: PersistValue -> Either Text Text #
PersistField Natural # |
Methods toPersistValue :: Natural -> PersistValue # fromPersistValue :: PersistValue -> Either Text Natural #
PersistField Html # |
Methods toPersistValue :: Html -> PersistValue # fromPersistValue :: PersistValue -> Either Text Html #
PersistField TimeOfDay # |
Methods toPersistValue :: TimeOfDay -> PersistValue # fromPersistValue :: PersistValue -> Either Text TimeOfDay #
PersistField Day # |
Methods toPersistValue :: Day -> PersistValue # fromPersistValue :: PersistValue -> Either Text Day #
PersistField PersistValue # |
Methods toPersistValue :: PersistValue -> PersistValue # fromPersistValue :: PersistValue -> Either Text PersistValue #
PersistField Checkmark # |
Methods toPersistValue :: Checkmark -> PersistValue # fromPersistValue :: PersistValue -> Either Text Checkmark #
PersistField SomePersistField # |
Methods toPersistValue :: SomePersistField -> PersistValue # fromPersistValue :: PersistValue -> Either Text SomePersistField #
PersistField [Char] # |
Methods toPersistValue :: [Char] -> PersistValue # fromPersistValue :: PersistValue -> Either Text [Char] #
PersistField a => PersistField [a] # |
Methods toPersistValue :: [a] -> PersistValue # fromPersistValue :: PersistValue -> Either Text [a] #
PersistField a => PersistField (Maybe a) # |
Methods toPersistValue :: Maybe a -> PersistValue # fromPersistValue :: PersistValue -> Either Text (Maybe a) #
HasResolution a => PersistField (Fixed a) # |
Methods toPersistValue :: Fixed a -> PersistValue # fromPersistValue :: PersistValue -> Either Text (Fixed a) #
PersistField v => PersistField (IntMap v) # |
Methods toPersistValue :: IntMap v -> PersistValue # fromPersistValue :: PersistValue -> Either Text (IntMap v) #
(Ord a, PersistField a) => PersistField (Set a) # |
Methods toPersistValue :: Set a -> PersistValue # fromPersistValue :: PersistValue -> Either Text (Set a) #
PersistField a => PersistField (Vector a) # |
Methods toPersistValue :: Vector a -> PersistValue # fromPersistValue :: PersistValue -> Either Text (Vector a) #
(PersistEntity record, PersistField record, PersistField (Key record)) => PersistField (Entity record) # |
Methods toPersistValue :: Entity record -> PersistValue # fromPersistValue :: PersistValue -> Either Text (Entity record) #
(PersistField a, PersistField b) => PersistField (a, b) # |
Methods toPersistValue :: (a, b) -> PersistValue # fromPersistValue :: PersistValue -> Either Text (a, b) #
PersistField v => PersistField (Map Text v) # |
Methods toPersistValue :: Map Text v -> PersistValue # fromPersistValue :: PersistValue -> Either Text (Map Text v) #

# 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

loadConfig, createPoolConfig, runPool

Associated Types

type PersistConfigBackend c :: (* -> *) -> * -> * #

type PersistConfigPool c #

Methods

loadConfig :: Value -> Parser c #

Load the config settings from a `Value`, most likely taken from a YAML config file.

applyEnv :: c -> IO c #

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) # |
Associated Types type PersistConfigBackend (Either c1 c2) :: (* -> *) -> * -> * # type PersistConfigPool (Either c1 c2) :: * # Methods loadConfig :: Value -> Parser (Either c1 c2) # applyEnv :: Either c1 c2 -> IO (Either c1 c2) # createPoolConfig :: Either c1 c2 -> IO (PersistConfigPool (Either c1 c2)) # runPool :: (MonadBaseControl IO m, MonadIO m) => Either c1 c2 -> PersistConfigBackend (Either c1 c2) m a -> PersistConfigPool (Either c1 c2) -> m a #

entityValues :: PersistEntity record => Entity record -> [PersistValue] #

Get list of values corresponding to given entity.

# Lifting

class HasPersistBackend backend where #

Class which allows the plucking of a `BaseBackend backend` from some larger type. For example, `instance HasPersistBackend (SqlReadBackend, Int) where type BaseBackend (SqlReadBackend, Int) = SqlBackend persistBackend = unSqlReadBackend . fst`

Minimal complete definition

persistBackend

Associated Types

type BaseBackend backend #

Methods

persistBackend :: backend -> BaseBackend backend #

Instances

HasPersistBackend SqlWriteBackend # |
Associated Types type BaseBackend SqlWriteBackend :: * # Methods persistBackend :: SqlWriteBackend -> BaseBackend SqlWriteBackend #
HasPersistBackend SqlReadBackend # |
Associated Types type BaseBackend SqlReadBackend :: * # Methods persistBackend :: SqlReadBackend -> BaseBackend SqlReadBackend #
HasPersistBackend SqlBackend # |
Associated Types type BaseBackend SqlBackend :: * # Methods persistBackend :: SqlBackend -> BaseBackend SqlBackend #

class HasPersistBackend backend => IsPersistBackend backend #

Class which witnesses that `backend` is essentially the same as `BaseBackend backend`. That is, they're isomorphic and `backend` is just some wrapper over `BaseBackend backend`.

Minimal complete definition

mkPersistBackend

Instances

IsPersistBackend SqlWriteBackend # |
Methods mkPersistBackend :: BaseBackend SqlWriteBackend -> SqlWriteBackend #
IsPersistBackend SqlReadBackend # |
Methods mkPersistBackend :: BaseBackend SqlReadBackend -> SqlReadBackend #
IsPersistBackend SqlBackend # |
Methods mkPersistBackend :: BaseBackend SqlBackend -> SqlBackend #

liftPersist :: (MonadIO m, MonadReader backend m, HasPersistBackend backend) => ReaderT (BaseBackend backend) IO b -> m b #

# JSON utilities

keyValueEntityToJSON :: (PersistEntity record, ToJSON record, ToJSON (Key record)) => Entity record -> Value #

Predefined `toJSON`. The resulting JSON looks like `{"key": 1, "value": {"name": ...}}`.

The typical usage is:

    instance ToJSON (Entity User) where
        toJSON = keyValueEntityToJSON

keyValueEntityFromJSON :: (PersistEntity record, FromJSON record, FromJSON (Key record)) => Value -> Parser (Entity record) #

Predefined `parseJSON`. The input JSON looks like `{"key": 1, "value": {"name": ...}}`.

The typical usage is:

    instance FromJSON (Entity User) where
        parseJSON = keyValueEntityFromJSON

entityIdToJSON :: (PersistEntity record, ToJSON record, ToJSON (Key record)) => Entity record -> Value #

Predefined `toJSON`. The resulting JSON looks like `{"id": 1, "name": ...}`.

The typical usage is:

    instance ToJSON (Entity User) where
        toJSON = entityIdToJSON

entityIdFromJSON :: (PersistEntity record, FromJSON record, FromJSON (Key record)) => Value -> Parser (Entity record) #

Predefined `parseJSON`. The input JSON looks like `{"id": 1, "name": ...}`.

The typical usage is:

    instance FromJSON (Entity User) where
        parseJSON = entityIdFromJSON

toPersistValueJSON :: ToJSON a => a -> PersistValue #

Convenience function for getting a free `PersistField` instance from a type with JSON instances.

Example usage in combination with `fromPersistValueJSON`:

    instance PersistField MyData where
      fromPersistValue = fromPersistValueJSON
      toPersistValue = toPersistValueJSON

fromPersistValueJSON :: FromJSON a => PersistValue -> Either Text a #

Convenience function for getting a free `PersistField` instance from a type with JSON instances. The JSON parser used will accept JSON values other that object and arrays. So, if your instance serializes the data to a JSON string, this will still work.

Example usage in combination with `toPersistValueJSON`:

    instance PersistField MyData where
      fromPersistValue = fromPersistValueJSON
      toPersistValue = toPersistValueJSON