hasql-generate
Safe HaskellNone
LanguageGHC2021

Hasql.Generate.TH

Synopsis

Documentation

data GenerateConfig Source #

Configuration for a table, view, or type code-generation request.

Use fromTable, fromView, or fromType to create a default config, then chain modifier functions with (&) to customise it:

   fromTable "public" "users"
       & withDerivations [''Show, ''Eq, ''Generic]
       & withOverrides [("timestamptz", ''UTCTime)]
       & withDefaultedCols ["id"]
   

fromTable :: String -> String -> GenerateConfig Source #

Create a GenerateConfig for the given schema and table with no derivations and no type overrides. Generates full CRUD code.

fromType :: String -> String -> GenerateConfig Source #

Create a GenerateConfig for the given schema and enum type with no derivations. Generates a Haskell sum type, a PgCodec instance, and a PgColumn instance. Overrides are ignored for types.

The splice must appear before any fromTable whose table has a column of this enum type, since TH processes splices top to bottom.

Orphan instance warning: The generated PgColumn instance will trigger GHC's -Worphans warning because the functional dependency's determining types are Symbol literals, which GHC never considers local to any user module. This is expected and harmless. Add the following pragma to any module that uses fromType:

   {-# OPTIONS_GHC -Wno-orphans #-}
   

fromView :: String -> String -> GenerateConfig Source #

Create a GenerateConfig for the given schema and view with no derivations and no type overrides. Generates read-only code: a record type, a decoder, a SELECT statement, and a HasView instance.

generate :: Config -> GenerateConfig -> Q [Dec] Source #

Terminal step that introspects a PostgreSQL database at compile time and generates the provided data construct:

Usage: $(generate def (fromTable "public" "users" & withDerivations [''Show, ''Eq]))

withDefaultedCols :: [String] -> GenerateConfig -> GenerateConfig Source #

Exclude the named columns from INSERT statements. Each column must exist in the table and have a database default (atthasdef = true in pg_catalog); a compile-time error is raised otherwise.

   fromTable "public" "users" & withDefaultedCols ["id", "created_at"]
   

withDerivations :: [Name] -> GenerateConfig -> GenerateConfig Source #

Append derivation class Names to the config.

withOverrides :: [(String, Name)] -> GenerateConfig -> GenerateConfig Source #

Append per-table PG type overrides. Each pair maps a PostgreSQL type name (e.g. "timestamptz") to a Haskell type Name (e.g. ''UTCTime). The override type must still have a PgCodec instance.