beam-newtype-field (empty) → 0.2.0.0
raw patch · 5 files changed
+121/−0 lines, 5 filesdep +basedep +beam-coredep +beam-migratesetup-changed
Dependencies added: base, beam-core, beam-migrate, lens, postgresql-simple
Files
- LICENSE +7/−0
- Readme.md +9/−0
- Setup.hs +2/−0
- beam-newtype-field.cabal +41/−0
- src/Database/Beam/Wrapped.hs +62/−0
+ LICENSE view
@@ -0,0 +1,7 @@+Copyright 2018 Jappie Klooster++Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Readme.md view
@@ -0,0 +1,9 @@+# A newtype for putting newtypes into beam schemas++This demonstrates how I got rid of a bunch of boilerplate+using the Wrapped typeclass of Control.Lens.++Being quite pleased with myself I wrote a blog post about it:+https://jappieklooster.nl/lens-into-wrapped-newtypes.html++licensed under MIT
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ beam-newtype-field.cabal view
@@ -0,0 +1,41 @@+-- This file has been generated from package.yaml by hpack version 0.28.2.+--+-- see: https://github.com/sol/hpack+--+-- hash: 010af9da15c21e60746ec8c226a65677bbc3427f8214ee1c62a95c49c001c86b++name: beam-newtype-field+version: 0.2.0.0+synopsis: A newtype for wrapping newtypes into beam schemas+description: Please see the README on GitHub at <https://github.com/jappeace/dbfield#readme>+category: Database+homepage: https://github.com/jappeace/dbfield#readme+bug-reports: https://github.com/jappeace/dbfield/issues+author: Jappie Klooster+maintainer: jappieklooster@hotmail.com+copyright: 2018 Jappie Klooster+license: MIT+license-file: LICENSE+build-type: Simple+cabal-version: >= 1.10+extra-source-files:+ Readme.md++source-repository head+ type: git+ location: https://github.com/jappeace/dbfield++library+ exposed-modules:+ Database.Beam.Wrapped+ other-modules:+ Paths_beam_newtype_field+ hs-source-dirs:+ src+ build-depends:+ base >=4.7 && <5+ , beam-core+ , beam-migrate+ , lens+ , postgresql-simple+ default-language: Haskell2010
+ src/Database/Beam/Wrapped.hs view
@@ -0,0 +1,62 @@+{-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE UndecidableInstances #-}++-- | A field for dealing with newtypes, implements a bunch of default+-- db instances for them.+-- Currenlty only works with postgres+module Database.Beam.Wrapped(DBFieldWrap(..)) where++import Control.Lens+import Database.Beam+import Database.Beam.Backend.SQL (HasSqlValueSyntax (..))+import Database.Beam.Backend.SQL.SQL92 (IsSql92DataTypeSyntax,+ IsSql92ExpressionSyntax)+import Database.Beam.Backend.Types (BackendFromField, BeamBackend)+import Database.Beam.Migrate+import GHC.Generics (Generic)+import Database.PostgreSQL.Simple.FromField (FromField (..))+-- | A wrapper for newtyps in the database, the only restriction it+-- places on newtypes is that they implement Wrapped from+-- Control.Lens.Wrapped.+-- The underlying types need to implmeent the various database+-- instances.+newtype DBFieldWrap a = DBFieldWrap+ { _unField :: a+ } deriving (Generic, Show)++instance Wrapped a => Wrapped (DBFieldWrap a)++instance (Wrapped a, BeamBackend be,+ FromBackendRow be (Unwrapped a),+ BackendFromField be (DBFieldWrap a)+ ) =>+ FromBackendRow be (DBFieldWrap a)++instance ( IsSql92ExpressionSyntax be+ , Wrapped a+ , HasSqlEqualityCheck be (Unwrapped a)+ ) =>+ HasSqlEqualityCheck be (DBFieldWrap a)++instance (Wrapped a, FromField (Unwrapped a)) => FromField (DBFieldWrap a) where+ fromField a b = review (_Wrapped' . _Wrapped') <$> fromField a b++instance (Wrapped a, HasSqlValueSyntax be (Unwrapped a)) =>+ HasSqlValueSyntax be (DBFieldWrap a) where+ sqlValueSyntax = sqlValueSyntax . view (_Wrapped' . _Wrapped')++instance ( IsSql92ColumnSchemaSyntax be+ , Wrapped a+ , HasDefaultSqlDataTypeConstraints be (Unwrapped a)+ ) =>+ HasDefaultSqlDataTypeConstraints be (DBFieldWrap a)++instance ( IsSql92DataTypeSyntax be+ , Wrapped a+ , HasDefaultSqlDataType be (Unwrapped a)+ ) =>+ HasDefaultSqlDataType be (DBFieldWrap a) where+ defaultSqlDataType proxy =+ defaultSqlDataType $ view (_Wrapped' . _Wrapped') <$> proxy