Hoogle Search

Within LTS Haskell 24.41 (ghc-9.10.3)

Note that Stackage only displays results for the latest LTS and Nightly snapshot. Learn more.

  1. module Database.Persist.ImplicitIdDef

    This module contains types and functions for creating an ImplicitIdDef, which allows you to customize the implied ID column that persistent generates. If this module doesn't suit your needs, you may want to import Database.Persist.ImplicitIdDef.Internal instead. If you do so, please file an issue on GitHub so we can support your needs. Breaking changes to that module will *not* be accompanied with a major version bump.

  2. data ImplicitIdDef

    persistent Database.Persist.ImplicitIdDef

    A specification for how the implied ID columns are created. By default, persistent will give each table a default column named id (customizable by PersistSettings), and the column type will be whatever you'd expect from BackendKey yourBackendType. For The SqlBackend type, this is an auto incrementing integer primary key. You might want to give a different example. A common use case in postgresql is to use the UUID type, and automatically generate them using a SQL function. Previously, you'd need to add a custom Id annotation for each model.

    User
    Id   UUID default="uuid_generate_v1mc()"
    name Text
    
    Dog
    Id   UUID default="uuid_generate_v1mc()"
    name Text
    user UserId
    
    Now, you can simply create an ImplicitIdDef that corresponds to this declaration.
    newtype UUID = UUID ByteString
    
    instance PersistField UUID where
    toPersistValue (UUID bs) =
    PersistLiteral_ Escaped bs
    fromPersistValue pv =
    case pv of
    PersistLiteral_ Escaped bs ->
    Right (UUID bs)
    _ ->
    Left "nope"
    
    instance PersistFieldSql UUID where
    sqlType _ = SqlOther UUID
    
    With this instance at the ready, we can now create our implicit definition:
    uuidDef :: ImplicitIdDef
    uuidDef = mkImplicitIdDef @UUID "uuid_generate_v1mc()"
    
    And we can use setImplicitIdDef to use this with the MkPersistSettings for our block.
    mkPersist (setImplicitIdDef uuidDef sqlSettings) [persistLowerCase| ... |]
    
    TODO: either explain interaction with mkMigrate or fix it. see issue #1249 for more details.

  3. mkImplicitIdDef :: (Typeable t, PersistFieldSql t) => Text -> ImplicitIdDef

    persistent Database.Persist.ImplicitIdDef

    Create an ImplicitIdDef based on the Typeable and PersistFieldSql constraints in scope. This function uses the TypeApplications syntax. Let's look at an example that works with Postgres UUIDs.

    newtype UUID = UUID Text
    deriving newtype PersistField
    
    instance PersistFieldSql UUID where
    sqlType _ = SqlOther "UUID"
    
    idDef :: ImplicitIdDef
    idDef = mkImplicitIdDefTypeable @UUID "uuid_generate_v1mc()"
    
    This ImplicitIdDef will generate default UUID columns, and the database will call the uuid_generate_v1mc() function to generate the value for new rows being inserted. If the type t is Text or String then a max_len attribute of 200 is set. To customize this, use setImplicitIdDefMaxLen.

  4. setImplicitIdDefMaxLen :: Integer -> ImplicitIdDef -> ImplicitIdDef

    persistent Database.Persist.ImplicitIdDef

    Set the maximum length of the implied ID column. This is required for any type where the associated SqlType is a TEXT or VARCHAR sort of thing.

  5. unsafeClearDefaultImplicitId :: ImplicitIdDef -> ImplicitIdDef

    persistent Database.Persist.ImplicitIdDef

    Remove the default attribute of the ImplicitIdDef column. This will require you to provide an ID for the model with every insert, using insertKey instead of insert, unless the type has some means of getting around that in the migrations. As an example, the Postgresql SERIAL type expands to an autoincrementing integer. Postgres will implicitly create the relevant series and set the default to be NEXTVAL(series_name). A default is therefore unnecessary to use for this type. However, for a UUID, postgres *does not* have an implicit default. You must either specify a default UUID generation function, or insert them yourself (again, using insertKey). This function will be deprecated in the future when omiting the default implicit ID column is more fully supported.

  6. data ImplicitIdDef

    persistent Database.Persist.ImplicitIdDef.Internal

    A specification for how the implied ID columns are created. By default, persistent will give each table a default column named id (customizable by PersistSettings), and the column type will be whatever you'd expect from BackendKey yourBackendType. For The SqlBackend type, this is an auto incrementing integer primary key. You might want to give a different example. A common use case in postgresql is to use the UUID type, and automatically generate them using a SQL function. Previously, you'd need to add a custom Id annotation for each model.

    User
    Id   UUID default="uuid_generate_v1mc()"
    name Text
    
    Dog
    Id   UUID default="uuid_generate_v1mc()"
    name Text
    user UserId
    
    Now, you can simply create an ImplicitIdDef that corresponds to this declaration.
    newtype UUID = UUID ByteString
    
    instance PersistField UUID where
    toPersistValue (UUID bs) =
    PersistLiteral_ Escaped bs
    fromPersistValue pv =
    case pv of
    PersistLiteral_ Escaped bs ->
    Right (UUID bs)
    _ ->
    Left "nope"
    
    instance PersistFieldSql UUID where
    sqlType _ = SqlOther UUID
    
    With this instance at the ready, we can now create our implicit definition:
    uuidDef :: ImplicitIdDef
    uuidDef = mkImplicitIdDef @UUID "uuid_generate_v1mc()"
    
    And we can use setImplicitIdDef to use this with the MkPersistSettings for our block.
    mkPersist (setImplicitIdDef uuidDef sqlSettings) [persistLowerCase| ... |]
    
    TODO: either explain interaction with mkMigrate or fix it. see issue #1249 for more details.

  7. ImplicitIdDef :: (EntityNameHS -> FieldType) -> SqlType -> (Bool -> Type -> Type) -> Maybe Text -> Maybe Integer -> ImplicitIdDef

    persistent Database.Persist.ImplicitIdDef.Internal

    No documentation available.

  8. iidDefault :: ImplicitIdDef -> Maybe Text

    persistent Database.Persist.ImplicitIdDef.Internal

    The default expression for the field. Note that setting this to Nothing is unsafe. see https://github.com/yesodweb/persistent/issues/1247 for more information. With some cases - like the Postgresql SERIAL type - this is safe, since there's an implied default.

  9. iidFieldSqlType :: ImplicitIdDef -> SqlType

    persistent Database.Persist.ImplicitIdDef.Internal

    The SqlType for the default column. By default, this is SqlInt64 to correspond with an autoincrementing integer primary key.

  10. iidFieldType :: ImplicitIdDef -> EntityNameHS -> FieldType

    persistent Database.Persist.ImplicitIdDef.Internal

    The field type. Accepts the EntityNameHS if you want to refer to it. By default, Id is appended to the end of the Haskell name.

Page 596 of many | Previous | Next