bolty-0.1.0.2: Haskell driver for Neo4j (BOLT protocol 4.4-5.4)
Safe HaskellNone
LanguageGHC2021

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

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.

newtype Decode a Source #

A decoder for a single Bolt value.

Constructors

Decode 

Instances

Instances details
Applicative Decode Source # 
Instance details

Defined in Database.Bolty.Decode

Methods

pure :: a -> Decode a #

(<*>) :: Decode (a -> b) -> Decode a -> Decode b #

liftA2 :: (a -> b -> c) -> Decode a -> Decode b -> Decode c #

(*>) :: Decode a -> Decode b -> Decode b #

(<*) :: Decode a -> Decode b -> Decode a #

Functor Decode Source # 
Instance details

Defined in Database.Bolty.Decode

Methods

fmap :: (a -> b) -> Decode a -> Decode b #

(<$) :: a -> Decode b -> Decode a #

Monad Decode Source # 
Instance details

Defined in Database.Bolty.Decode

Methods

(>>=) :: Decode a -> (a -> Decode b) -> Decode b #

(>>) :: Decode a -> Decode b -> Decode b #

return :: a -> Decode a #

newtype RowDecoder a Source #

A decoder for a full result row. Compose with Applicative to decode multiple columns.

Constructors

RowDecoder 

Fields

Instances

Instances details
Applicative RowDecoder Source # 
Instance details

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 # 
Instance details

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

class ToBolt a where Source #

Types that can be encoded to a Ps value for use as query parameters. All PackStream instances are automatically ToBolt instances.

Methods

toBolt :: a -> Ps Source #

Instances

Instances details
PackStream a => ToBolt a Source # 
Instance details

Defined in Database.Bolty.Decode

Methods

toBolt :: a -> Ps Source #

Primitive decoders

int64 :: Decode Int64 Source #

Decode a BoltInteger narrowed to Int64. Fails if the value is out of range.

nullable :: Decode a -> Decode (Maybe a) Source #

Make a decoder nullable: returns Nothing for BoltNull, otherwise applies the inner decoder.

list :: Decode a -> Decode (Vector a) Source #

Decode a BoltList, applying the element decoder to each item.

UUID decoder

uuid :: Decode UUID Source #

Decode a UUID from a BoltString.

Time decoders

utcTime :: Decode UTCTime Source #

Decode a UTCTime from a BoltString (ISO 8601) or BoltDateTime.

day :: Decode Day Source #

Decode a Day from a BoltString (ISO 8601).

timeOfDay :: Decode TimeOfDay Source #

Decode a TimeOfDay from a BoltString (ISO 8601).

Aeson decoder

aesonValue :: Decode Value Source #

Decode any Bolt value to an Value.

Node property decoders

nodeProperty :: Text -> Decode a -> Decode a Source #

Extract a property from a BoltNode by key and decode it.

nodePropertyOptional :: Text -> Decode a -> Decode (Maybe a) Source #

Extract a property from a BoltNode by key, returning Nothing if the key is missing.

nodeLabels :: Decode (Vector Text) Source #

Extract labels from a BoltNode.

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

Record-level decoders

column :: Int -> Decode a -> RowDecoder a Source #

Decode a value at a positional column index.

field :: Text -> Decode a -> RowDecoder a Source #

Decode a value by column name.

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.

Internal converters (exported for testing)

psToBolt :: Ps -> Bolt Source #

Convert a Ps value to a Bolt value.

boltToPs :: Bolt -> Ps Source #

Convert a Bolt value to a Ps value.