liveBook · Manning

Beam

As explained in Functional Design and Architecture: Examples in Haskell

beam is a Haskell library for declaring relational database schemas and constructing type-safe SQL queries across multiple database backends.

beam provides a Haskell interface for declaring relational schemas and constructing type-safe SQL queries without building another type-level database abstraction. It targets applications that need typed models while supporting multiple SQL backends. section 12.2.1 section 12.2.2

Its design uses advanced Haskell machinery—type families, multiparameter type classes, phantom types, existential types, ADTs, and HKD-style records—to represent tables, schemas, keys, and queries. This broad genericity supports unknown backends and backend-specific plugins, but makes the interface difficult to understand and tightly coupled when embedded in a larger framework. section 12.2.2

Listing 12.3 DBMeteor table definition
import Database.Beam (Columnar, Beamable)
  
data DBMeteorT f = DBMeteor              #1
  { _id          :: Columnar f Int       #2
  , _size        :: Columnar f Int
  , _mass        :: Columnar f Int
  , _azimuth     :: Columnar f Int
  , _altitude    :: Columnar f Int
  , _timestamp   :: Columnar f UTCTime
  }
  deriving (Generic, Beamable)           #3

A representative table declaration uses an HKD-style parameterized record, Columnar fields, Generic derivation, and Beamable derivation to model meteor data. section 12.2.2

Schema and query flow

A database model declares DBMeteorT with typed fields such as _id, _mass, and _timestamp. A Table instance identifies the primary key, CatalogueDB registers the table for a backend, and a query composes select, filter_, and all_ to retrieve meteors matching a size. Backend execution then runs the selected statement through a connection. section 12.2.2 section 12.2.2

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Explains schema declarations, primary keys, database settings, queries, and type-level machinery, while emphasizing interface complexity and coupling.
Examples
Uses a meteor table, an associated primary-key ADT, a CatalogueDB schema, filtered selections, and SQLite execution.
What it assumes
Assumes familiarity with Haskell and relational databases, while exposing the need for type-level design and relational-algebra knowledge.
What you can do after
Build it from scratch: declare Beam tables and keys, configure a database, compose typed queries, and execute them through a backend connection.
How it connects
Connects relational databases with SQL, Haskell type systems, ADTs, HKD types, type families, type classes, and backend abstraction.

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.