@@ -828,11 +828,19 @@ type family FieldTypeOf (maybe :: Type) where
828828 FieldTypeOf ty = TypeError
829829 ('Text "FieldTypeOf error: non-Maybe type " ':<>: 'ShowType ty)
830830
831+type family NullityTypeOf (hask :: Type) where
832+ NullityTypeOf (Maybe hask) = 'Null (PG hask)
833+ NullityTypeOf hask = 'NotNull (PG hask)
834+
831835-- | Calculate the types of fields.
832836type family FieldTypesOf (fields :: [Type]) where
833837 FieldTypesOf '[] = '[]
834838 FieldTypesOf (field ': fields) = FieldTypeOf field ': FieldTypesOf fields
835839
840+type family NullityTypesOf (hasks :: [Type]) where
841+ NullityTypesOf '[] = '[]
842+ NullityTypesOf (hask ': hasks) = NullityTypeOf hask ': NullityTypesOf hasks
843+
836844-- | Inspect the code of an algebraic datatype and ensure it's a product,
837845-- otherwise generate a type error
838846type family RecordCodeOf (hask :: Type) (code ::[[Type]]) :: [Type] where
@@ -840,5 +848,8 @@ type family RecordCodeOf (hask :: Type) (code ::[[Type]]) :: [Type] where
840848 RecordCodeOf hask _tys = TypeError
841849 ('Text "RecordCodeOf error: non-Record type " ':<>: 'ShowType hask)
842850
843−type family ParamsFrom (hask :: Type) :: [NullityType]
844−type family ResultFrom (hask :: Type) :: RelationType
851+type family ParamsFrom (hask :: Type) :: [NullityType] where
852+ ParamsFrom hask = NullityTypesOf (RecordCodeOf hask (Code hask))
853+
854+type family ResultFrom (hask :: Type) :: RelationType where
855+ ResultFrom hask = ZipAs (FieldNamesFrom hask) (ParamsFrom hask)