1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
-- |
--
-- @since 2.13.0.0
module Database.Persist.FieldDef
( -- * The 'FieldDef' type
FieldDef
-- ** Setters
, setFieldAttrs
, overFieldAttrs
, addFieldAttr
-- ** Helpers
, isFieldNullable
, isFieldMaybe
, isFieldNotGenerated
, isHaskellField
-- * 'FieldCascade'
, FieldCascade(..)
, renderFieldCascade
, renderCascadeAction
, noCascade
, CascadeAction(..)
) where
import Database.Persist.FieldDef.Internal
import Database.Persist.Types.Base
( FieldAttr(..)
, FieldType(..)
, IsNullable(..)
, fieldAttrsContainsNullable
, isHaskellField
)
-- | Replace the 'FieldDef' 'FieldAttr' with the new list.
--
-- @since 2.13.0.0
setFieldAttrs :: [FieldAttr] -> FieldDef -> FieldDef
setFieldAttrs fas fd = fd { fieldAttrs = fas }
-- | Modify the list of field attributes.
--
-- @since 2.13.0.0
overFieldAttrs :: ([FieldAttr] -> [FieldAttr]) -> FieldDef -> FieldDef
overFieldAttrs k fd = fd { fieldAttrs = k (fieldAttrs fd) }
-- | Add an attribute to the list of field attributes.
--
-- @since 2.13.0.0
addFieldAttr :: FieldAttr -> FieldDef -> FieldDef
addFieldAttr fa = overFieldAttrs (fa :)
-- | Check if the field definition is nullable
--
-- @since 2.13.0.0
isFieldNullable :: FieldDef -> IsNullable
isFieldNullable =
fieldAttrsContainsNullable . fieldAttrs
-- | Check if the field is `Maybe a`
--
-- @since 2.13.0.0
isFieldMaybe :: FieldDef -> Bool
isFieldMaybe field =
case fieldType field of
FTApp (FTTypeCon _ "Maybe") _ ->
True
_ ->
False
|