liveBook · Manning

AsValueEntity

As explained in Functional Design and Architecture: Examples in Haskell

AsValueEntity is a book-specific Haskell type class that defines conversions between a database entity representation and its corresponding domain type.

AsValueEntity connects a database entity representation with its domain type. It keeps conversion logic separate from the generic loading workflow, so a typed load can return a domain value instead of exposing storage details. section 11.2.4 section 11.2.3

The class defines two directional conversions:

  • toValueEntity converts a domain value into the associated ValueEntity representation used by the database model. section 11.2.4
  • fromValueEntity receives a KeyEntity and stored ValueEntity, then reconstructs the domain value. section 11.2.4
  • loadEntity combines AsValueEntity with DBEntity: it loads raw data, decodes it, and converts the typed value into the requested destination type. section 11.2.4 section 11.2.3

Meteor conversion

The Meteor instance maps Meteor to KVDBMeteor in toValueEntity. Its fromValueEntity implementation accepts MeteorKey and KVDBMeteor, then rebuilds Meteor while leaving the primary-key decision explicit. section 11.2.4

class DBEntity db entity | entity -> db where
  toDBKey                                           #1
    :: KeyEntity entity -> KVDBKey 
  toDBValue                                         #2
    :: ValueEntity entity -> KVDBValue
  fromDBValue                                       #3
   :: KVDBValue -> Maybe (ValueEntity entity)

The surrounding type-level interface shows how ValueEntity participates in database-specific key, value, and decoding operations. section 11.2.4

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Defines two conversion functions and shows their use in generic loading, while deferring primary-key design to relational persistence.
Examples
Uses Meteor, MeteorKey, KVDBMeteor, and the generic loadEntity function to demonstrate conversion and retrieval.
What it assumes
Assumes familiarity with Haskell type classes and associated types while introducing their role in database models.
What you can do after
Apply it via loadEntity to retrieve decoded database values as domain types.
How it connects
Ties domain types to DBEntity, KeyEntity, ValueEntity, and typed key-value database loading.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Design and Architecture: Examples in Haskell. The section links point to the parts of the book it draws on.