Hoogle Search
Within LTS Haskell 24.39 (ghc-9.10.3)
Note that Stackage only displays results for the latest LTS and Nightly snapshot. Learn more.
type
PersistUnique a = PersistUniqueWrite apersistent Database.Persist.Class 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 PersistStoreRead backend =>
PersistUniqueRead backendpersistent Database.Persist.Class 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 Uniques 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.
class (PersistUniqueRead backend, PersistStoreWrite backend) =>
PersistUniqueWrite backendpersistent Database.Persist.Class 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.
module Database.Persist.Class.
PersistConfig No documentation available.
-
persistent Database.Persist.Class.PersistConfig 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.
module Database.Persist.Class.
PersistEntity No documentation available.
-
persistent Database.Persist.Class.PersistEntity 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.
module Database.Persist.Class.
PersistField No documentation available.
-
persistent Database.Persist.Class.PersistField 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.
module Database.Persist.Class.
PersistQuery No documentation available.