Define a custom type using Persistent generated types

219 Views Asked by At

I believe this question is more related to the Haskell structure itself than Persistent's, but I've been looking through some questions here and I stumbled upon this.

Now, I wonder, is there a way to use a type generated through mkPersist inside a custom type generated through derivePersistField? Now, taking the following,

{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

module Model where

import Database.Persist.TH
import Data.Text
import Extra

share
    [ mkPersist sqlSettings
    , mkMigrate "migrateAll"
    , mkDeleteCascade sqlSettings
    ] [persistLowerCase|
        User
            ident    Text
            passwd   Text
            person   Person
        Regular
            name     Text
            username Text
            birth    Day
        Admin
            authLvl  Int
    |]
{-# LANGUAGE TemplateHaskell #-}

module Extra where

import Database.Persist.TH

data Person = PersonRegular | PersonAdmin
    deriving (Show, Read, Eq)
derivePersistField "Person"

The problem is, Extra needs to be imported in Model, that is fine; however, If I were to use the RegularId, from the Regular table, inside the Person type,

data Person = PersonRegular RegularId | PersonAdmin AdminId

I would also need to import Model in Extra, which leads to an import cycle.

Is there a way to fix that and achieve my objective? To put everything in the same module is ruled out, since it'll throw me a GHC stage restriction. I also thought about using {-# SOURCE #-} and hs-boot, but for some reason stack still complained about import cycle, but with the hs-boot file instead.

0

There are 0 best solutions below