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

Safe Haskell | None
Language | Haskell2010

Database.Persist.EntityDef

Contents

- The `EntityDef` type
- Construction
- Accessors
- Setters
- Related Types

Description

An `EntityDef` represents metadata about a type that `persistent` uses to store the type in the database, as well as generate Haskell code from it.

Since: 2.13.0.0

Synopsis

- data EntityDef
- getEntityHaskellName :: EntityDef -> EntityNameHS
- getEntityDBName :: EntityDef -> EntityNameDB
- getEntityFields :: EntityDef -> [FieldDef]
- getEntityFieldsDatabase :: EntityDef -> [FieldDef]
- getEntityForeignDefs :: EntityDef -> [ForeignDef]
- getEntityUniques :: EntityDef -> [UniqueDef]
- getEntityUniquesNoPrimaryKey :: EntityDef -> [UniqueDef]
- getEntityId :: EntityDef -> EntityIdDef
- getEntityIdField :: EntityDef -> Maybe FieldDef
- getEntityKeyFields :: EntityDef -> NonEmpty FieldDef
- getEntityComments :: EntityDef -> Maybe Text
- getEntityExtra :: EntityDef -> Map Text [[Text]]
- getEntitySpan :: EntityDef -> Maybe SourceSpan
- isEntitySum :: EntityDef -> Bool
- entityPrimary :: EntityDef -> Maybe CompositeDef
- entitiesPrimary :: EntityDef -> NonEmpty FieldDef
- keyAndEntityFields :: EntityDef -> NonEmpty FieldDef
- keyAndEntityFieldsDatabase :: EntityDef -> NonEmpty FieldDef
- setEntityId :: FieldDef -> EntityDef -> EntityDef
- setEntityIdDef :: EntityIdDef -> EntityDef -> EntityDef
- setEntityDBName :: EntityNameDB -> EntityDef -> EntityDef
- overEntityFields :: ([FieldDef] -> [FieldDef]) -> EntityDef -> EntityDef
- data EntityIdDef
  - = EntityIdField !FieldDef
  - | EntityIdNaturalKey !CompositeDef

# The `EntityDef` type

data EntityDef Source #

An `EntityDef` represents the information that `persistent` knows about an Entity. It uses this information to generate the Haskell datatype, the SQL migrations, and other relevant conversions.

#### Instances

Instances details

Read EntityDef Source # |
Instance details Defined in Database.Persist.Types.Base Methods readsPrec :: Int -> ReadS EntityDef Source # readList :: ReadS [EntityDef] Source # readPrec :: ReadPrec EntityDef Source # readListPrec :: ReadPrec [EntityDef] Source #
Show EntityDef Source # |
Instance details Defined in Database.Persist.Types.Base Methods showsPrec :: Int -> EntityDef -> ShowS Source # show :: EntityDef -> String Source # showList :: [EntityDef] -> ShowS Source #
Eq EntityDef Source # |
Instance details Defined in Database.Persist.Types.Base Methods (==) :: EntityDef -> EntityDef -> Bool Source # (/=) :: EntityDef -> EntityDef -> Bool Source #
Ord EntityDef Source # |

Instance details

Defined in Database.Persist.Types.Base

Methods

compare :: EntityDef -> EntityDef -> Ordering Source #

(<) :: EntityDef -> EntityDef -> Bool Source #

(<=) :: EntityDef -> EntityDef -> Bool Source #

(>) :: EntityDef -> EntityDef -> Bool Source #

(>=) :: EntityDef -> EntityDef -> Bool Source #

max :: EntityDef -> EntityDef -> EntityDef Source #

min :: EntityDef -> EntityDef -> EntityDef Source #

Lift EntityDef Source # |
Instance details Defined in Database.Persist.Types.Base Methods lift :: Quote m => EntityDef -> m Exp Source # liftTyped :: forall (m :: Type -> Type). Quote m => EntityDef -> Code m EntityDef Source #

# Construction

# Accessors

getEntityHaskellName :: EntityDef -> EntityNameHS Source #

Retrieve the Haskell name of the given entity.

Since: 2.13.0.0

getEntityDBName :: EntityDef -> EntityNameDB Source #

Return the database name for the given entity.

Since: 2.13.0.0

getEntityFields :: EntityDef -> [FieldDef] Source #

Retrieve the list of `FieldDef` that makes up the fields of the entity.

This does not return the fields for an `Id` column or an implicit `id`. It will return the key columns if you used the `Primary` syntax for defining the primary key.

This does not return fields that are marked `SafeToRemove` or `MigrationOnly` - so it only returns fields that are represented in the Haskell type. If you need those fields, use `getEntityFieldsDatabase`.

Since: 2.13.0.0

getEntityFieldsDatabase :: EntityDef -> [FieldDef] Source #

This returns all of the `FieldDef` defined for the `EntityDef`, including those fields that are marked as `MigrationOnly` (and therefore only present in the database) or `SafeToRemove` (and a migration will drop the column if it exists in the database).

For all the fields that are present on the Haskell-type, see `getEntityFields`.

Since: 2.13.0.0

getEntityForeignDefs :: EntityDef -> [ForeignDef] Source #

Since: 2.13.0.0

getEntityUniques :: EntityDef -> [UniqueDef] Source #

Retrieve the list of `UniqueDef` from an `EntityDef`. As of version 2.14, this will also include the primary key on the entity, if one is defined. If you do not want the primary key, see `getEntityUniquesNoPrimaryKey`.

Since: 2.13.0.0

getEntityUniquesNoPrimaryKey :: EntityDef -> [UniqueDef] Source #

Retrieve the list of `UniqueDef` from an `EntityDef`. This does not include a `Primary` key, if one is defined. A future version of `persistent` will include a `Primary` key among the `Unique` constructors for the `Entity`.

Since: 2.14.0.0

getEntityId :: EntityDef -> EntityIdDef Source #

Since: 2.13.0.0

getEntityIdField :: EntityDef -> Maybe FieldDef Source #

Since: 2.13.0.0

getEntityKeyFields :: EntityDef -> NonEmpty FieldDef Source #

Since: 2.13.0.0

getEntityComments :: EntityDef -> Maybe Text Source #

getEntityExtra :: EntityDef -> Map Text [[Text]] Source #

getEntitySpan :: EntityDef -> Maybe SourceSpan Source #

Gets the `Source` of the definition of the entity.

Note that as of this writing the span covers the entire file or quasiquote where the item is defined due to parsing limitations. This may be changed in a future release to be more accurate.

Since: 2.15.0.0

isEntitySum :: EntityDef -> Bool Source #

Since: 2.13.0.0

entityPrimary :: EntityDef -> Maybe CompositeDef Source #

entitiesPrimary :: EntityDef -> NonEmpty FieldDef Source #

Return the `[`FieldDef`]` for the entity keys.

keyAndEntityFields :: EntityDef -> NonEmpty FieldDef Source #

Returns a `NonEmpty` list of `FieldDef` that correspond with the key columns for an `EntityDef`.

keyAndEntityFieldsDatabase :: EntityDef -> NonEmpty FieldDef Source #

Returns a `NonEmpty` list of `FieldDef` that correspond with the key columns for an `EntityDef` including those fields that are marked as `MigrationOnly` (and therefore only present in the database) or `SafeToRemove` (and a migration will drop the column if it exists in the database).

For fields on the Haskell type use `keyAndEntityFieldsDatabase`

Since: 2.15.1.0

# Setters

setEntityId :: FieldDef -> EntityDef -> EntityDef Source #

Set an `entityId` to be the given `FieldDef`.

Since: 2.13.0.0

setEntityIdDef :: EntityIdDef -> EntityDef -> EntityDef Source #

Since: 2.13.0.0

setEntityDBName :: EntityNameDB -> EntityDef -> EntityDef Source #

Since: 2.13.0.0

overEntityFields :: ([FieldDef] -> [FieldDef]) -> EntityDef -> EntityDef Source #

Perform a mapping function over all of the entity fields, as determined by `getEntityFieldsDatabase`.

Since: 2.13.0.0

# Related Types

data EntityIdDef Source #

The definition for the entity's primary key ID.

Since: 2.13.0.0

Constructors

EntityIdField !FieldDef | The entity has a single key column, and it is a surrogate key - that is, you can't go from `rec -> Key rec`. Since: 2.13.0.0
EntityIdNaturalKey !CompositeDef | The entity has a natural key. This means you can write `rec -> Key rec` because all the key fields are present on the datatype. A natural key can have one or more columns. Since: 2.13.0.0

#### Instances

Instances details

Read EntityIdDef Source # |
Instance details Defined in Database.Persist.Types.Base Methods readsPrec :: Int -> ReadS EntityIdDef Source # readList :: ReadS [EntityIdDef] Source # readPrec :: ReadPrec EntityIdDef Source # readListPrec :: ReadPrec [EntityIdDef] Source #
Show EntityIdDef Source # |
Instance details Defined in Database.Persist.Types.Base Methods showsPrec :: Int -> EntityIdDef -> ShowS Source # show :: EntityIdDef -> String Source # showList :: [EntityIdDef] -> ShowS Source #
Eq EntityIdDef Source # |
Instance details Defined in Database.Persist.Types.Base Methods (==) :: EntityIdDef -> EntityIdDef -> Bool Source # (/=) :: EntityIdDef -> EntityIdDef -> Bool Source #
Ord EntityIdDef Source # |

Instance details

Defined in Database.Persist.Types.Base

Methods

compare :: EntityIdDef -> EntityIdDef -> Ordering Source #

(<) :: EntityIdDef -> EntityIdDef -> Bool Source #

(<=) :: EntityIdDef -> EntityIdDef -> Bool Source #

(>) :: EntityIdDef -> EntityIdDef -> Bool Source #

(>=) :: EntityIdDef -> EntityIdDef -> Bool Source #

max :: EntityIdDef -> EntityIdDef -> EntityIdDef Source #

min :: EntityIdDef -> EntityIdDef -> EntityIdDef Source #

Lift EntityIdDef Source # |
Instance details Defined in Database.Persist.Types.Base Methods lift :: Quote m => EntityIdDef -> m Exp Source # liftTyped :: forall (m :: Type -> Type). Quote m => EntityIdDef -> Code m EntityIdDef Source #