keyed-vals

An abstract Handle for accessing collections in stores like Redis

https://github.com/adetokunbo/keyed-vals#readme

Version on this page:0.2.3.0
LTS Haskell 22.43:0.2.3.1
Stackage Nightly 2024-12-05:0.2.3.1
Latest on Hackage:0.2.3.1

See all snapshots keyed-vals appears in

BSD-3-Clause licensed by Tim Emiola
Maintained by [email protected]
This version can be pinned in stack with:keyed-vals-0.2.3.0@sha256:e91f8439447974d2281458930e25ca2b4a6cbc586800555c0a7123d7cdad99d3,2052

keyed-vals

GitHub CI Stackage Nightly Hackage Hackage Dependencies BSD3

keyed-vals aims to provide a narrow client for storing key-value collections in storage services like Redis.

E.g,

  • Redis supports many other features
  • the abstract Handle declared in keyed-vals just provides combinators that operate on key-value collections stored in some backend
  • so the redis implementation of Handle accesses collections in Redis without exposing its other features.

Example

{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE StandaloneDeriving #-}

import Data.Aeson (FromJSON, ToJSON)
import Data.Text (Text)
import GHC.Generics (Generic)
import KeyedVals.Handle.Codec.Aeson (AesonOf(..))
import KeyedVals.Handle.Codec.HttpApiData (HttpApiDataOf(..))
import qualified KeyedVals.Handle.Mem as Mem
import KeyedVals.Handle.Typed
import Web.HttpApiData (FromHttpApiData (..), ToHttpApiData (..))

{- Usage is fairly simple:

- Declare 'PathOf' and possibly a 'VaryingPathOf' instance for
  storable data types.

They describe how the data type is encoded and decoded and where in the
key-value store the data should be saved.

For example, given the following data type:
-}
data Person = Person
  { name :: Text
  , age  :: Int
  } deriving (Eq, Show, Generic)

{- Suppose each @Person@ is to be stored as JSON, via the @Generic@
implementation, e.g,
-}
instance FromJSON Person
instance ToJSON Person

{- Also suppose each Person is stored with an @Int@ key. To enable that,
define a @newtype@ of @Int@, e.g,
-}
newtype PersonID = PersonID Int
  deriving stock (Eq, Show)
  deriving (ToHttpApiData, FromHttpApiData, Num, Ord) via Int

{- And then suppose the collection of @Person@s is stored at a specific fixed path
in the key-value store. E.g, it is to be used as a runtime cache to speed up
access to person data, so the path @/runtime/cache/persons@ is used.

To specify all of this, first define @DecodeKV@ and @EncodeKV@ instances for
@Person@:
-}
deriving via (AesonOf Person) instance DecodeKV Person
deriving via (AesonOf Person) instance EncodeKV Person

{- .. and do the same for @PersonID@: -}
deriving via (HttpApiDataOf Int) instance DecodeKV PersonID
deriving via (HttpApiDataOf Int) instance EncodeKV PersonID


{- Then declare a @PathOf@ instance that binds the types together with the path: -}
instance PathOf Person where
  type KVPath Person = "/runtime/cache/persons"
  type KeyType Person = PersonID

{- Note: the @DecodeKV@ and @EncodeKV@ deriving statements above were
standalone for illustrative purposes. In most cases, they ought to be part
of the deriving clause of the data type. E.g,
-}
newtype FriendID = FriendID Int
  deriving stock (Eq, Show)
  deriving (ToHttpApiData, FromHttpApiData, Num, Ord) via Int
  deriving (DecodeKV, EncodeKV) via (HttpApiDataOf Int)

{- Now save and fetch @Person@s from a storage backend as follows:

>>> handle <- Mem.new
>>> tim = Person { name = "Tim", age = 48 }
>>> saveTo handle (key 1) tim
Right ()
>>> loadFrom handle (key 1)
Right (Person { name = "Tim", age = 48 })

-}

{- Suppose that in addition to the main collection of @Person@s, it's
necessary to store a distinct list of the friends of each @Person@.
I.e, store a small keyed collection of @Person@s per person.

One way to achieve is to store each such collection at a similar path, e.g
suppose the friends for the person with @anID@ are stored at
@/app/person/<anId>/friends@.

This can be implemented using the existing types along with another newtype
that has @PathOf@ and @VaryingPathOf@ instances as follows
-}

newtype Friend = Friend Person
  deriving stock (Eq, Show)
  deriving (FromJSON, ToJSON, EncodeKV, DecodeKV) via Person

instance PathOf Friend where
  type KVPath Friend = "/app/person/{}/friends"
  type KeyType Friend = FriendID -- as defined earlier

instance VaryingPathOf Friend where
  type PathVar Friend = PersonID
  modifyPath _ = expand -- implements modifyPath by expanding the braces to PathVar


{- This allows @Friends@ to be saved or fetched as follows:

>>> dave = Person { name = "Dave", age = 61 }
>>> saveTo handle (key 2) dave -- save in main person list
Right ()
>>> saveTo handle ( 1 // 2) (Friend dave) -- save as friend of tim (person 1)
Right ()
-}

Changes

Revision history for keyed-vals

keyed-vals uses PVP Versioning.

0.2.3.0 – 2024-01-09

Changed

  • Relax constraints on the containers dependency

0.2.2.0 – 2023-07-11

Changed

  • Relax constraints on the http-api-data dependency
  • Relax constraints on the bytestring dependency

0.2.1.0 – 2023-07-11

Changed

  • Relax constraints on the aeson dependency

0.2.0.0 – 2022-12-15

Changed

Added

  • support for encoding and decoding typed keys and values constrained to specific paths in the key-value store in KeyedVals.Handle.Typed

0.1.0.0 – 2022-11-28

  • Initial version.