groundhog-converters: Extended Converter Library for groundhog embedded types

[ bsd3, database, groundhog, groundhog------------------------, library ] [ Propose Tags ] [ Report a vulnerability ]

Groundhog allows a programmer to represent a haskell datatype by an equivalent type that is more suited for Database work. groundhog-converters provides a selection of common converters that have proved useful.


[Skip to Readme]

Modules

[Index]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0
Change log ChangeLog.md
Dependencies aeson, base (>=4 && <5), bimap, bytestring, containers [details]
License BSD-3-Clause
Author Scott Murphy <scottmurphy09@gmail.com>
Maintainer Scott Murphy <scottmurphy09@gmail.com>
Category Database, Groundhog
Source repo head: git clone https://github.com/plow-technologies/groundhog-converters
Uploaded by scottmurphy09 at 2016-03-18T20:56:34Z
Distributions
Reverse Dependencies 1 direct, 1 indirect [details]
Downloads 1037 total (5 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2016-03-18 [all 1 reports]

Readme for groundhog-converters-0.1.0

[back to package description]

groundhog-converters

Groundhog allows a programmer to represent a haskell datatype by an equivalent type that is more suited for Database work. groundhog-converters provides a selection of common converters that have proved useful.

Usage

-- | Sample DataType 'Group' proviides a Map between an Integer and  a 'Person'
-- However, the person is embedded in the Datatype relative to the SQL database

data Group = Group {
     _people :: Map Integer Person
         }
 deriving (Eq)


-- | A wrapped representation of a Person
data Person = Person { _unPerson :: String}
  deriving (Eq)

instance Arbitrary Person where
  arbitrary = Person <$> arbitrary

-- | An Isomorphism between the representation that is pleasent to use in haskell
-- and the one that makes sense to store i.e. 'PersistEntity' 
personMapConverter :: Converter (Map Integer Person) [(Int64,String)]
personMapConverter = mapConverter `composeConverter` fmapConverter (bicomposeConverter integerConverter personConverter)

-- | This converter is embedded in 'personMapConverter'
personConverter :: Converter Person String
personConverter = (_unPerson,Person)


-- | A declaration for group.
mkPersist defaultCodegenConfig [groundhog|
- entity: Group
  constructors:
  - name: Group
    fields:
      - name: _people
        dbName: people
        exprName: MappedIdToPerson
        converter: personMapConverter
- primitive: Person
  converter: personConverter


|]

How to run tests

cabal configure --enable-tests && cabal build && cabal test