persistent-template-classy: Generate classy lens field accessors for persistent models

[ bsd3, database, lens, library, yesod ] [ Propose Tags ]

Generate classy lens field accessors for persistent models.


[Skip to Readme]
Versions [faq] 0.1.0.0, 0.1.0.1
Dependencies base (>=4.7 && <5), lens, persistent, persistent-sqlite, persistent-template, template-haskell, text [details]
License BSD-3-Clause
Copyright 2018 Kostiantyn Rybnikov
Author Kostiantyn Rybnikov
Maintainer k-bx@k-bx.com
Category Database, Lens, Yesod
Home page https://github.com/k-bx/persistent-template-classy#readme
Uploaded by k_bx at Fri Nov 2 16:30:25 UTC 2018
Distributions NixOS:0.1.0.1
Downloads 321 total (22 in the last 30 days)
Rating (no votes yet) [estimated by rule of succession]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs not available [build log]
Last success reported on 2018-11-02 [all 3 reports]

Modules

  • Database
    • Persist
      • TH
        • Database.Persist.TH.Classy

Downloads

Maintainer's Corner

For package maintainers and hackage trustees


Readme for persistent-template-classy-0.1.0.1

[back to package description]

persistent-template-classy

Generate classy lens for your Persistent fields. For example:

{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}

import Control.Lens (Lens', (^.), lens)
import Control.Monad.IO.Class (liftIO)
import Database.Persist
import Database.Persist.Sqlite
import Database.Persist.TH
import Database.Persist.TH.Classy

$(mkClassyClass "name")
$(mkClassyClass "age")
$(mkClassyClass "title")
$(mkClassyClass "authorId")

share
  [mkPersist sqlSettings, mkMigrate "migrateAll", mkClassyInstances]
  [persistLowerCase|
Person
    name String
    age Int Maybe
    deriving Show
BlogPost
    title String
    authorId PersonId
    deriving Show
|]

main :: IO ()
main =
  runSqlite ":memory:" $ do
    runMigration migrateAll
    let johnDoe = Person "John Doe" $ Just 35
    johnId <- insert johnDoe
    liftIO $ print $ johnDoe ^. name
    liftIO $ print $ johnDoe ^. age
    let post = BlogPost "My fr1st p0st" johnId
    liftIO $ print $ post ^. title

This would essentially generate this additional code:

class HasName ev a | ev -> a where
  name :: Lens' ev a
class HasAge ev a | ev -> a where
  age :: Lens' ev a
class HasTitle ev a | ev -> a where
  title :: Lens' ev a
class HasAuthorId ev a | ev -> a where
  authorId :: Lens' ev a

instance HasName Person String where
  name = (lens personName) (\ x y -> x {personName = y})
instance HasAge Person Int where
  age = (lens personAge) (\ x y -> x {personAge = y})
instance HasTitle BlogPost String where
  title = (lens blogPostTitle) (\ x y -> x {blogPostTitle = y})
instance HasAuthorId BlogPost PersonId where
  authorId
    = (lens blogPostAuthorId) (\ x y -> x {blogPostAuthorId = y})

Class-generation is separated from instance-generation because it's wise to keep it in a single place in your project and reuse with a non-persistent code.