Skip to content

Instantly share code, notes, and snippets.

@epost
Created July 9, 2017 19:28
Show Gist options
  • Save epost/9440e4be65555dfa41794cdbd73f6756 to your computer and use it in GitHub Desktop.
Save epost/9440e4be65555dfa41794cdbd73f6756 to your computer and use it in GitHub Desktop.
purecript-crud-1
module Main where
import Prelude
import Control.Monad.Free
import Data.Exists
import Data.Foldable (fold)
import Data.Maybe
import Data.Symbol
import TryPureScript
prog5 = do
pid <- createPerson $ Person { id: Nothing, name: "Erik", year: 1979 }
cid <- createCompany $ Company { id: Nothing, ownerId: pid, name: "Shinsetsu" }
pure pid
------------------------------------------------------------------
main =
render $ fold
[ h1 (text "Free CRUD monad")
, pt "Ho ho ho!"
]
where pt = p <<< text
------------------------------------------------------------------
type PersonRec =
{ id :: Maybe PersonId
, name :: String
, year :: Int
}
type PersonId = Int
newtype Person = Person PersonRec
type CompanyRec =
{ id :: Maybe CompanyId
, ownerId :: PersonId -- a Company must have an owner
, name :: String
}
type CompanyId = Int
newtype Company = Company CompanyRec
------------------------------------------------------------------
-- TODO hack; wrapping up entities of different types (Person,
-- , Company, ...) in a sum type like this allows us to
-- store entities in Crud constructors such that they all
-- have the same type (Entity); otherwise we'd need something
-- like `| forall e. Create e a` where e is an entity type.
--
-- This means that we'd have to write/generate domain
-- (schema, app) specific data types.
data Entity
= PersonE Person
| CompanyE Company
data CrudF next
= Create Entity next
type Crud = Free CrudF
-- create :: Entity -> Crud DbId
-- create x = liftF (Create x 42)
createPerson :: Person -> Crud DbId
createPerson x = liftF (Create (PersonE x) 42)
createCompany :: Company -> Crud DbId
createCompany x = liftF (Create (CompanyE x) 42)
------------------------------------------------------------------
-- TODO hack, unifies PersonId and CompanyId which are both aliases for Int atm
type DbId = Int
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment