Skip to content

Latest commit

 

History

History
67 lines (49 loc) · 1.7 KB

File metadata and controls

67 lines (49 loc) · 1.7 KB

Haskell Persistent Conventions

Use explicit type application to specify tables

It is nearly always a good idea for a database operation to include somewhere a type application for each table involved in the query. Selects usually should have a type application on table.

Missing a type application:

select $ do
  people <- from $ table
  where_ $ people ^. #name ==. val "Robin"
  pure people

Better:

select $ do
  people <- from $ table @Person
  where_ $ people ^. #name ==. val "Robin"
  pure people

Refer to entity fields by symbol

Enable the OverloadedLabels Haskell extension.

Do not:

select $ do
  people <- from $ table @Person
  where_ $ people ^. PersonName ==. val "Robin"
  pure people

Do this:

select $ do
  people <- from $ table @Person
  where_ $ people ^. #name ==. val "Robin"
  pure people

Modules that define Persistent entities should generally not even export their fully qualified entity fields e.g. pattern PersonName, as there ought to be no cause to use them.

Generate entity record with unprefixed field names

The mkPersist settings should be:

sqlSettings {mpsFieldLabelModifier = const id}

This prevents Persistent from prefixing the Haskell record field names, in accordance with our Haskell best practices that rely on NoFieldSelectors, DuplicateRecordFields, and OverloadedRecordDot.