Hoogle Search
Within LTS Haskell 24.4 (ghc-9.10.2)
Note that Stackage only displays results for the latest LTS and Nightly snapshot. Learn more.
pattern
PersistDbSpecific :: ByteString -> PersistValueesqueleto Database.Esqueleto This pattern synonym used to be a data constructor for the PersistValue type. It was changed to be a pattern so that JSON-encoded database values could be parsed into their corresponding values. You should not use this, and instead prefer to pattern match on PersistLiteral_ directly. If you use this, it will overlap a patern match on the 'PersistLiteral_, PersistLiteral, and PersistLiteralEscaped patterns. If you need to disambiguate between these constructors, pattern match on PersistLiteral_ directly.
PersistDouble :: Double -> PersistValueesqueleto Database.Esqueleto No documentation available.
-
esqueleto Database.Esqueleto 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.
type family
PersistEntityBackend recordesqueleto Database.Esqueleto Persistent allows multiple different backends (databases).
PersistError :: Text -> PersistExceptionesqueleto Database.Esqueleto Generic Exception
-
esqueleto Database.Esqueleto No documentation available.
-
esqueleto Database.Esqueleto This class teaches Persistent how to take a custom type and marshal it to and from a PersistValue, allowing it to be stored in a database.
Examples
Simple Newtype
You can use newtype to add more type safety/readability to a basis type like ByteString. In these cases, just derive PersistField and PersistFieldSql:{-# LANGUAGE GeneralizedNewtypeDeriving #-} newtype HashedPassword = HashedPassword ByteString deriving (Eq, Show, PersistField, PersistFieldSql)
Smart Constructor Newtype
In this example, we create a PersistField instance for a newtype following the "Smart Constructor" pattern.{-# LANGUAGE GeneralizedNewtypeDeriving #-} import qualified Data.Text as T import qualified Data.Char as C -- | An American Social Security Number newtype SSN = SSN Text deriving (Eq, Show, PersistFieldSql) mkSSN :: Text -> Either Text SSN mkSSN t = if (T.length t == 9) && (T.all C.isDigit t) then Right $ SSN t else Left $ "Invalid SSN: " <> t instance PersistField SSN where toPersistValue (SSN t) = PersistText t fromPersistValue (PersistText t) = mkSSN t -- Handle cases where the database does not give us PersistText fromPersistValue x = Left $ "File.hs: When trying to deserialize an SSN: expected PersistText, received: " <> T.pack (show x)
Tips:- This file contain dozens of PersistField instances you can look at for examples.
- Typically custom PersistField instances will only accept a single PersistValue constructor in fromPersistValue.
- Internal PersistField instances accept a wide variety of PersistValues to accomodate e.g. storing booleans as integers, booleans or strings.
- If you're making a custom instance and using a SQL database, you'll also need PersistFieldSql to specify the type of the database column.
class PersistField a =>
PersistFieldSql aesqueleto Database.Esqueleto Tells Persistent what database column type should be used to store a Haskell type.
Examples
Simple Boolean Alternative
data Switch = On | Off deriving (Show, Eq) instance PersistField Switch where toPersistValue s = case s of On -> PersistBool True Off -> PersistBool False fromPersistValue (PersistBool b) = if b then Right On else Right Off fromPersistValue x = Left $ "File.hs: When trying to deserialize a Switch: expected PersistBool, received: " <> T.pack (show x) instance PersistFieldSql Switch where sqlType _ = SqlBool
Non-Standard Database Types
If your database supports non-standard types, such as Postgres' uuid, you can use SqlOther to use them:import qualified Data.UUID as UUID instance PersistField UUID where toPersistValue = PersistLiteralEncoded . toASCIIBytes fromPersistValue (PersistLiteralEncoded uuid) = case fromASCIIBytes uuid of Nothing -> Left $ "Model/CustomTypes.hs: Failed to deserialize a UUID; received: " <> T.pack (show uuid) Just uuid' -> Right uuid' fromPersistValue x = Left $ "File.hs: When trying to deserialize a UUID: expected PersistLiteralEncoded, received: "-- > <> T.pack (show x) instance PersistFieldSql UUID where sqlType _ = SqlOther "uuid"
User Created Database Types
Similarly, some databases support creating custom types, e.g. Postgres' DOMAIN and ENUM features. You can use SqlOther to specify a custom type:CREATE DOMAIN ssn AS text CHECK ( value ~ '^[0-9]{9}$');
instance PersistFieldSQL SSN where sqlType _ = SqlOther "ssn"
CREATE TYPE rainbow_color AS ENUM ('red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet');
instance PersistFieldSQL RainbowColor where sqlType _ = SqlOther "rainbow_color"
-
esqueleto Database.Esqueleto No documentation available.
PersistForeignConstraintUnmet :: Text -> PersistExceptionesqueleto Database.Esqueleto No documentation available.