class (PersistField (Key record), ToJSON (Key record), FromJSON (Key record), Show (Key record), Read (Key record), Eq (Key record), Ord (Key record)) => PersistEntity record wherePersistent 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.
Associated types
type family PersistEntityBackend recordPersistent allows multiple different backends (databases).
data family Key recordBy default, a backend will automatically generate the key Instead you can specify a Primary key made up of unique values.
data family EntityField record :: Type -> TypeAn EntityField is parameterised by the Haskell record it belongs to and the additional type of that field.
As of
persistent-2.11.0.0, it's possible to use theOverloadedLabelslanguage extension to refer to EntityField values polymorphically. See the documentation on SymbolToField for more information.data family Unique recordUnique 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 :: proxy record -> EntityDefRetrieve the EntityDef meta-data for the record.
persistFieldDef :: EntityField record typ -> FieldDefReturn meta-data for a given EntityField.
toPersistFields :: record -> [PersistValue]A meta-operation to get the database fields of a record.
fromPersistValues :: [PersistValue] -> Either Text recordA lower-level operation to convert from database values to a Haskell record.
tabulateEntityA :: Applicative f => (forall a. EntityField record a -> f a) -> f (Entity record)This function allows you to build an
Entity aby specifying an action that returns a value for the field in the callback function. Let's look at an example.parseFromEnvironmentVariables :: IO (Entity User) parseFromEnvironmentVariables = tabulateEntityA $ \userField -> case userField of UserName -> getEnvUSER_NAMEUserAge -> do ageVar <- getEnvUSER_AGEcase readMaybe ageVar of Just age -> pure age Nothing -> error $ "Failed to parse Age from: " <> ageVar UserAddressId -> do addressVar <- getEnvUSER_ADDRESS_IDpure $ AddressKey addressVarpersistUniqueKeys :: record -> [Unique record]A meta operation to retrieve all the Unique keys.
persistUniqueToFieldNames :: Unique record -> NonEmpty (FieldNameHS, FieldNameDB)A lower level operation.
persistUniqueToValues :: Unique record -> [PersistValue]A lower level operation.
fieldLens :: EntityField record field -> forall (f :: Type -> Type). Functor f => (field -> f field) -> Entity record -> f (Entity record)Use a PersistField as a lens.
keyFromRecordM :: Maybe (record -> Key record)Extract a
Key recordfrom arecordvalue. Currently, this is only defined for entities using thePrimarysyntax for natural/composite keys. In a future version ofpersistentwhich incorporates the ID directly into the entity, this will always be Just.