persistent-2.14.5.2: Type-safe, multi-backend data serialization.
Safe HaskellSafe-Inferred
LanguageHaskell2010

Database.Persist.Sql.Util

Synopsis

Documentation

isIdField :: forall record typ. PersistEntity record => EntityField record typ -> Bool Source #

hasNaturalKey :: EntityDef -> Bool Source #

Returns True if the entity has a natural key defined with the Primary keyword.

A natural key is a key that is inherent to the record, and is part of the actual Haskell record. The opposite of a natural key is a "surrogate key", which is not part of the normal domain object. Automatically generated ID columns are the most common surrogate ID, while an email address is a common natural key.

User
    email String
    name String
    Primary email

Person
    Id   UUID
    name String

Follower
    name String

Given these entity definitions, User would return True, because the Primary keyword sets the email column to be the primary key. The generated Haskell type would look like this:

data User = User
    { userEmail :: String
    , userName :: String
    }

Person would be false. While the Id syntax allows you to define a custom ID type for an entity, the Id column is a surrogate key.

The same is true for Follower. The automatically generated autoincremented integer primary key is a surrogate key.

There's nothing preventing you from defining a Primary definition that refers to a surrogate key. This is totally fine.

Since: 2.11.0

hasCompositePrimaryKey :: EntityDef -> Bool Source #

Returns True if the provided entity has a custom composite primary key. Composite keys have multiple fields in them.

User
    email String
    name String
    Primary userId

Profile
    personId PersonId
    email    String
    Primary personId email

Person
    Id   UUID
    name String

Follower
    name String

Given these entity definitions, only Profile would return True, because it is the only entity with multiple columns in the primary key. User has a single column natural key. Person has a custom single column surrogate key defined with Id. And Follower has a default single column surrogate key.

Since: 2.11.0

mkUpdateText' :: PersistEntity record => (FieldNameDB -> Text) -> (Text -> Text) -> Update record -> Text Source #

mkInsertValues :: PersistEntity rec => rec -> [PersistValue] Source #

Make a list PersistValue suitable for database inserts. Pairs nicely with the function mkInsertPlaceholders.

Does not include generated columns.

Since: 2.11.0.0

mkInsertPlaceholders Source #

Arguments

:: EntityDef 
-> (FieldNameDB -> Text)

An escape function

-> [(Text, Text)] 

Returns a list of escaped field names and "?" placeholder values for performing inserts. This does not include generated columns.

Does not include generated columns.

Since: 2.11.0.0