| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
Database.Bolty.Decode
Description
Type-safe decoding of query result records.
Instead of manually pattern-matching on Bolt values, build composable
Decode and RowDecoder values that validate types at extraction time.
import Database.Bolty.Decode
data Person = Person { pName :: Text, pAge :: Int64 }
personDecoder :: RowDecoder Person
personDecoder = Person
<$> field "name" text
<*> field "age" int64
Synopsis
- data DecodeError
- newtype Decode a = Decode {
- runDecode :: Bolt -> Either DecodeError a
- newtype RowDecoder a = RowDecoder {
- runRowDecoder :: Vector Text -> Record -> Either DecodeError a
- class FromBolt a where
- rowDecoder :: RowDecoder a
- class ToBolt a where
- bool :: Decode Bool
- int :: Decode PSInteger
- int64 :: Decode Int64
- float :: Decode Double
- text :: Decode Text
- bytes :: Decode ByteString
- nullable :: Decode a -> Decode (Maybe a)
- list :: Decode a -> Decode (Vector a)
- dict :: Decode (HashMap Text Bolt)
- uuid :: Decode UUID
- utcTime :: Decode UTCTime
- day :: Decode Day
- timeOfDay :: Decode TimeOfDay
- aesonValue :: Decode Value
- nodeProperty :: Text -> Decode a -> Decode a
- nodePropertyOptional :: Text -> Decode a -> Decode (Maybe a)
- nodeLabels :: Decode (Vector Text)
- nodeProperties :: Decode (HashMap Text Ps)
- psDecode :: PackStream a => Decode a
- node :: Decode Node
- relationship :: Decode Relationship
- path :: Decode Path
- column :: Int -> Decode a -> RowDecoder a
- field :: Text -> Decode a -> RowDecoder a
- decodeRow :: RowDecoder a -> Vector Text -> Record -> Either DecodeError a
- decodeRows :: RowDecoder a -> Vector Text -> Vector Record -> Either DecodeError (Vector a)
- psToBolt :: Ps -> Bolt
- boltToPs :: Bolt -> Ps
Documentation
data DecodeError Source #
Errors that can occur when decoding a Bolt value or record row.
Constructors
| TypeMismatch Text Text | The value had a different type than expected (expected, got). |
| MissingField Text | A named field was not present in the column list. |
| IndexOutOfBounds Int Int | Column index was out of range (requested index, actual length). |
| EmptyResultSet | The result set contained no records. |
Instances
| Exception DecodeError Source # | |
Defined in Database.Bolty.Decode Methods toException :: DecodeError -> SomeException # fromException :: SomeException -> Maybe DecodeError # displayException :: DecodeError -> String # backtraceDesired :: DecodeError -> Bool # | |
| Show DecodeError Source # | |
Defined in Database.Bolty.Decode Methods showsPrec :: Int -> DecodeError -> ShowS # show :: DecodeError -> String # showList :: [DecodeError] -> ShowS # | |
| Eq DecodeError Source # | |
Defined in Database.Bolty.Decode | |
| TextShow DecodeError Source # | |
Defined in Database.Bolty.Decode Methods showbPrec :: Int -> DecodeError -> Builder # showb :: DecodeError -> Builder # showbList :: [DecodeError] -> Builder # showtPrec :: Int -> DecodeError -> Text # showt :: DecodeError -> Text # showtList :: [DecodeError] -> Text # showtlPrec :: Int -> DecodeError -> Text # showtl :: DecodeError -> Text # showtlList :: [DecodeError] -> Text # | |
A decoder for a single Bolt value.
Constructors
| Decode | |
Fields
| |
newtype RowDecoder a Source #
A decoder for a full result row. Compose with Applicative to decode multiple columns.
Constructors
| RowDecoder | |
Fields
| |
Instances
| Applicative RowDecoder Source # | |
Defined in Database.Bolty.Decode Methods pure :: a -> RowDecoder a # (<*>) :: RowDecoder (a -> b) -> RowDecoder a -> RowDecoder b # liftA2 :: (a -> b -> c) -> RowDecoder a -> RowDecoder b -> RowDecoder c # (*>) :: RowDecoder a -> RowDecoder b -> RowDecoder b # (<*) :: RowDecoder a -> RowDecoder b -> RowDecoder a # | |
| Functor RowDecoder Source # | |
Defined in Database.Bolty.Decode Methods fmap :: (a -> b) -> RowDecoder a -> RowDecoder b # (<$) :: a -> RowDecoder b -> RowDecoder a # | |
class FromBolt a where Source #
Types that can be decoded from a result row without an explicit decoder.
Implement this for your application types to use query
without passing a RowDecoder explicitly:
data Person = Person { name :: Text, age :: Int64 }
instance FromBolt Person where
rowDecoder = Person <$> field "name" text <*> field "age" int64
Methods
rowDecoder :: RowDecoder a Source #
Types that can be encoded to a Ps value for use as query parameters.
All PackStream instances are automatically ToBolt instances.
Instances
| PackStream a => ToBolt a Source # | |
Defined in Database.Bolty.Decode | |
Primitive decoders
int64 :: Decode Int64 Source #
Decode a BoltInteger narrowed to Int64. Fails if the value is out of range.
list :: Decode a -> Decode (Vector a) Source #
Decode a BoltList, applying the element decoder to each item.
UUID decoder
Time decoders
utcTime :: Decode UTCTime Source #
Decode a UTCTime from a BoltString (ISO 8601) or BoltDateTime.
Aeson decoder
Node property decoders
nodeProperty :: Text -> Decode a -> Decode a Source #
Extract a property from a BoltNode by key and decode it.
nodeProperties :: Decode (HashMap Text Ps) Source #
Extract the raw properties HashMap from a BoltNode.
PackStream interop
psDecode :: PackStream a => Decode a Source #
Decode via the PackStream fromPs roundtrip through a node property's Ps value.
Useful for types that already have PackStream instances.
Graph type decoders
relationship :: Decode Relationship Source #
Decode a BoltRelationship.
Record-level decoders
Running decoders
decodeRow :: RowDecoder a -> Vector Text -> Record -> Either DecodeError a Source #
Decode a single record row.
decodeRows :: RowDecoder a -> Vector Text -> Vector Record -> Either DecodeError (Vector a) Source #
Decode all records in a result set. Fails on the first DecodeError.