@@ -4,6 +4,7 @@
44 , FlexibleContexts
55 , OverloadedLabels
66 , OverloadedStrings
7+ , PolyKinds
78 , TypeApplications
89 , TypeOperators
910#-}
@@ -23,14 +24,15 @@ import qualified Generics.SOP as SOP
2324import qualified GHC.Generics as GHC
2425
2526type Schema =
26− '[ "users" :::
27− '[ "id" ::: ('[ 'Default, 'Unique] :=> 'NotNull 'PGint4)
28− , "name" ::: ('[] :=> 'NotNull 'PGtext)
27+ '[ "users" ::: '[] :=>
28+ '[ "id" ::: AsSet '[ 'Default, 'Unique] :=> 'NotNull 'PGint4
29+ , "name" ::: '[] :=> 'NotNull 'PGtext
2930 ]
30− , "emails" :::
31− '[ "id" ::: ('[ 'Default, 'Unique] :=> 'NotNull 'PGint4)
32− , "user_id" ::: ('[] :=> 'NotNull 'PGint4)
33− , "email" ::: ('[] :=> 'Null 'PGtext)
31+ , "emails" ::: AsSet
32+ '[ 'ForeignKey '["user_id"] "users" '["id"] ] :=>
33+ '[ "id" ::: AsSet '[ 'Default, 'Unique] :=> 'NotNull 'PGint4
34+ , "user_id" ::: '[] :=> 'NotNull 'PGint4
35+ , "email" ::: '[] :=> 'Null 'PGtext
3436 ]
3537 ]
3638
@@ -38,29 +40,27 @@ setup :: Definition '[] Schema
3840setup =
3941 createTable #users
4042 ( serial `As` #id :*
41− (text & notNull) `As` #name :* Nil )
42− [ primaryKey (Column #id :* Nil) ]
43+ (text & notNull) `As` #name :* Nil ) Nil
4344 >>>
4445 createTable #emails
4546 ( serial `As` #id :*
4647 (int & notNull) `As` #user_id :*
4748 text `As` #email :* Nil )
48− [ primaryKey (Column #id :* Nil)
49− , foreignKey (Column #user_id :* Nil) #users (Column #id :* Nil)
50− OnDeleteCascade OnUpdateCascade ]
49+ ( foreignKey (Column #user_id :* Nil) #users (Column #id :* Nil)
50+ OnDeleteCascade OnUpdateCascade :* Nil )
5151
5252teardown :: Definition Schema '[]
5353teardown = dropTable #emails >>> dropTable #users
5454
5555insertUser :: Manipulation Schema
56− '[ '[] ::=> 'NotNull 'PGtext]
57− '[ "fromOnly" ::: ('[] ::=> 'NotNull 'PGint4) ]
56+ '[ '[] :=> 'NotNull 'PGtext]
57+ '[ "fromOnly" ::: ('[] :=> 'NotNull 'PGint4) ]
5858insertUser = insertInto #users
5959 ( Values (def `As` #id :* param @1 `As` #name :* Nil) [] )
6060 OnConflictDoNothing (Returning (#id `As` #fromOnly :* Nil))
6161
6262insertEmail :: Manipulation Schema
63− '[ '[] ::=> 'NotNull 'PGint4, '[] ::=> 'Null 'PGtext] '[]
63+ '[ '[] :=> 'NotNull 'PGint4, '[] :=> 'Null 'PGtext] '[]
6464insertEmail = insertInto #emails ( Values
6565 ( def `As` #id :*
6666 param @1 `As` #user_id :*
@@ -68,8 +68,8 @@ insertEmail = insertInto #emails ( Values
6868 OnConflictDoNothing (Returning Nil)
6969
7070getUsers :: Query Schema '[]
71− '[ "userName" ::: ('[] ::=> 'NotNull 'PGtext)
72− , "userEmail" ::: ('[] ::=> 'Null 'PGtext) ]
71+ '[ "userName" ::: ('[] :=> 'NotNull 'PGtext)
72+ , "userEmail" ::: ('[] :=> 'Null 'PGtext) ]
7373getUsers = select
7474 (#u ! #name `As` #userName :* #e ! #email `As` #userEmail :* Nil)
7575 ( from (Table (#users `As` #u)