| Copyright | (c) 2026 The Polytropos Authors |
|---|---|
| License | EUPL-1.2 OR Apache-2.0 |
| Maintainer | nadia.yvette.chambers@ik.me |
| Stability | experimental |
| Portability | non-portable (GHC extensions) |
| Safe Haskell | None |
| Language | GHC2021 |
Polytropos.Persistent
Description
Bridges polytropos open records with the Yesod ecosystem's
persistent relational database framework.
Architectural Overview & Rationale
PersistRow provides fast marshalling of records to and from
[ rows: PersistValue]toPersistFieldsRecord serializes the flat
payload in row order, and fromPersistValuesRecord reconstructs the
record by filling the payload slots directly, in \(O(N)\), with no
intermediate tuples. The walk is heterogeneous: each field uses its own
PersistField instance.
'PersistField (Record r)' lets a whole record travel through
persistent's value machinery as a PersistList (a nested-column
analogue), and pairs an integer key with a record in the
shape of persistent's PolyRowEntityEntity, marshalling to the
key-then-fields row layout backends use for Entity rows.
When to Use This Module
- When persisting
polytroposopen records with Yesod and thepersistentORM.
Worked Example
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -fplugin=Polytropos.Plugin #-}
import Database.Persist
import Polytropos
import Polytropos.Persistent
type PersonRow = Record '["name" ':= Text, "age" ':= Int]
person :: PersonRow
person = REC { name = "Alice", age = 30 }
-- Convert to persistent values:
vals :: [PersistValue]
vals = toPersistFieldsRecord personSynopsis
- class PersistRow (r :: Row) where
- toPersistFieldsRecord :: Record r -> [PersistValue]
- fromPersistValuesRecord :: [PersistValue] -> Either Text (Record r)
- class AllToPersist (r :: Row) where
- toVals :: Proxy r -> [Identity (Any :: Type)] -> [PersistValue]
- class AllFromPersist (r :: Row) where
- fromVals :: Proxy r -> [PersistValue] -> Either Text [Identity (Any :: Type)]
- data PolyRowEntity (r :: Row) = PolyRowEntity {
- polyRowKey :: Int64
- polyRowRecord :: Record r
- toPolyRowEntity :: forall (r :: Row). Int64 -> Record r -> PolyRowEntity r
- fromPolyRowEntity :: forall (r :: Row). PersistRow r => PolyRowEntity r -> (Int64, [PersistValue])
Row Marshalling
class PersistRow (r :: Row) where Source #
Fast marshalling of a polytropos record to and from a [
database row, in field order.PersistValue]
Methods
toPersistFieldsRecord :: Record r -> [PersistValue] Source #
fromPersistValuesRecord :: [PersistValue] -> Either Text (Record r) Source #
Instances
| (AllToPersist r, AllFromPersist r) => PersistRow r Source # | |
Defined in Polytropos.Persistent Methods toPersistFieldsRecord :: Record r -> [PersistValue] Source # fromPersistValuesRecord :: [PersistValue] -> Either Text (Record r) Source # | |
Row Constraint Walks
class AllToPersist (r :: Row) where Source #
Heterogeneous encode walk: every field of the row r has a
PersistField instance at its own type; toVals serializes the erased
payload slots in row order.
Methods
toVals :: Proxy r -> [Identity (Any :: Type)] -> [PersistValue] Source #
Instances
| AllToPersist ('[] :: [Pair Symbol Type]) Source # | |
Defined in Polytropos.Persistent | |
| (PersistField a, AllToPersist r) => AllToPersist ((n ':= a) ': r) Source # | |
Defined in Polytropos.Persistent | |
class AllFromPersist (r :: Row) where Source #
Heterogeneous decode walk: fills the payload slots from a
[ row in O(n).PersistValue]
Methods
fromVals :: Proxy r -> [PersistValue] -> Either Text [Identity (Any :: Type)] Source #
Instances
| AllFromPersist ('[] :: [Pair Symbol Type]) Source # | |
Defined in Polytropos.Persistent | |
| (PersistField a, AllFromPersist r) => AllFromPersist ((n ':= a) ': r) Source # | |
Defined in Polytropos.Persistent | |
Entity Wrapper
data PolyRowEntity (r :: Row) Source #
Key-plus-record wrapper in the shape of persistent's Entity for a
record table: an integer key followed by the record's field values.
Constructors
| PolyRowEntity | |
Fields
| |
toPolyRowEntity :: forall (r :: Row). Int64 -> Record r -> PolyRowEntity r Source #
Wrap a record under the given integer key.
fromPolyRowEntity :: forall (r :: Row). PersistRow r => PolyRowEntity r -> (Int64, [PersistValue]) Source #
Unwrap an entity to its key and field values (key first, matching the
id | fields layout backends use for entity rows).
Orphan instances
| PersistRow r => PersistField (Record r) Source # | A whole record travels through persistent's value machinery as a
|
Methods toPersistValue :: Record r -> PersistValue # fromPersistValue :: PersistValue -> Either Text (Record r) # | |