polytropos-persistent
Copyright(c) 2026 The Polytropos Authors
LicenseEUPL-1.2 OR Apache-2.0
Maintainernadia.yvette.chambers@ik.me
Stabilityexperimental
Portabilitynon-portable (GHC extensions)
Safe HaskellNone
LanguageGHC2021

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 [PersistValue] rows: 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 PolyRowEntity pairs an integer key with a record in the shape of persistent's Entity, marshalling to the key-then-fields row layout backends use for Entity rows.

When to Use This Module

  • When persisting polytropos open records with Yesod and the persistent ORM.

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 person
Synopsis

Row Marshalling

class PersistRow (r :: Row) where Source #

Fast marshalling of a polytropos record to and from a [PersistValue] database row, in field order.

Instances

Instances details
(AllToPersist r, AllFromPersist r) => PersistRow r Source # 
Instance details

Defined in Polytropos.Persistent

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

Instances details
AllToPersist ('[] :: [Pair Symbol Type]) Source # 
Instance details

Defined in Polytropos.Persistent

Methods

toVals :: Proxy ('[] :: [Pair Symbol Type]) -> [Identity (Any :: Type)] -> [PersistValue] Source #

(PersistField a, AllToPersist r) => AllToPersist ((n ':= a) ': r) Source # 
Instance details

Defined in Polytropos.Persistent

Methods

toVals :: Proxy ((n ':= a) ': r) -> [Identity (Any :: Type)] -> [PersistValue] Source #

class AllFromPersist (r :: Row) where Source #

Heterogeneous decode walk: fills the payload slots from a [PersistValue] row in O(n).

Methods

fromVals :: Proxy r -> [PersistValue] -> Either Text [Identity (Any :: Type)] Source #

Instances

Instances details
AllFromPersist ('[] :: [Pair Symbol Type]) Source # 
Instance details

Defined in Polytropos.Persistent

Methods

fromVals :: Proxy ('[] :: [Pair Symbol Type]) -> [PersistValue] -> Either Text [Identity (Any :: Type)] Source #

(PersistField a, AllFromPersist r) => AllFromPersist ((n ':= a) ': r) Source # 
Instance details

Defined in Polytropos.Persistent

Methods

fromVals :: Proxy ((n ':= a) ': r) -> [PersistValue] -> Either Text [Identity (Any :: Type)] Source #

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 

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 PersistList of its fields.

Instance details