This module contains code for documenting a set of
persistent
entity definitions. All the library provides is a means to render a
Markdown document with table and column documentation and comments. A
further expansion could use the information here to generate
PostgreSQL
COMMENTs on the fields and tables too.
Getting Started
You probably already have a
persistent entity definitions
somewhere, and they probably look like this:
share [mkPersist sqlSettings] [persistUpperCase|
User
firstName Text.Text
active Bool
deriving Show Eq Read Ord
|]
The
persistUpperCase QuasiQuoter parses the block of text and
returns a value of type
[EntityDef]. We need to get
our hands on that definition so we can document it. We'll use the
mkEntityDefList function to expose it:
share
[ mkPersist sqlSettings
, mkEntityDefList "entityDefs"
] [persistUpperCase|
User
firstName Text.Text
active Bool
deriving Show Eq Read Ord
|]
You may want to factor out the quasiquoter into a term and import from
another module. This has an important downside: the ID fields from the
QuasiQuoter are given as
Int64 regardless of what they
actually are. It's not possible for the persistent quasiquoter to
properly know the types of the IDs.
Documentating The Schema
Now, we're going to use the
document function to link up the
entityDefs with a documentation expression (type
EntityDoc).
docs :: [EntityDef]
docs = document entityDefs $ do
pure ()
The
EntityDoc type is a monad, and we'll use
do
notation to sequence multiple entity definitions.
docs :: [EntityDef]
docs = document entityDefs $ do
User --^ do
pure ()
The
--^ operator mimics the Haddock comment syntax. We use the
constructor of the entity (in this case,
User). On the right,
we provide documentation for the entity. The right hand expression
will have the type
FieldDoc, and we can use
do
notation to construct it.
We can use string literals to document the entity itself, with the
OverloadedStrings extension enabled. The string literals are
concatenated, and used to provide entity-level comments. You'll need
to manage whitespace yourself, though.
docs :: [EntityDef]
docs = document entityDefs $ do
User --^ do
"This is user documentation. "
"You can have multiple lines, but you need to watch out for spaces. "
"The lines will be combined."
We can also document the entity fields. We do this using the
#
operator.
docs :: [EntityDef]
docs = document entityDefs $ do
User --^ do
"This is user documentation. "
"You can have multiple lines, but you need to watch out for spaces. "
"The lines will be combined."
UserFirstName # "The user's first name."
UserActive # "Whether or not the user is able to log in."
This attaches the comment to the entity field.
Rendering the Documentation
Finally, we'll use
render and provide a
Renderer to
generate documentation. For an example of what this looks like, check
out the file
test/example.md in the repository (linked from
the README).
renderedDocs :: Text
renderedDocs = render markdownTableRenderer docs