I'm building a Haskell Servant
API using an SQL library called Squeal
:
https://github.com/morphismtech/squeal
I need help getting the types correct so the app compiles.
My Schema is of type
type Schema = '["users" ::: UsersTable, ...]
type Schemas = Public Schema
Where Public
is a type family for a single schema database. It's from:
http://hackage.haskell.org/package/squeal-postgresql-0.5.1.0/docs/Squeal-PostgreSQL-Schema.html
I'm trying to pass the Connection Pool in a Reader like this:
import qualified Squeal.PostgreSQL as S
import qualified Squeal.PostgreSQL.Pool as SPG
newtype AppT m a
= AppT
{ runApp :: ReaderT SquealPool (ExceptT ServerError m) a
} deriving
( Functor, Applicative, Monad, MonadReader SquealPool, MonadError ServerError
, MonadIO
)
type App = AppT IO
type SquealPool = SPG.Pool (SQ.K S.Connection Schema)
My SQL query & session are something like this:
emailQuery :: Query_ Schemas (Only Text) UserEmail
emailQuery = select (#email `as` #email)
(from (table #users) & where_ (#email .== param @1))
emailTakenSession
:: (MonadReader SquealPool m, MonadPQ Schemas m, MonadIO m)
=> Text
-> m UserEmail
emailTakenSession email = do
result <- runQueryParams emailQuery (Only email)
email <- getRow 1 result
return email
Finally, I'm using them in the Servant
handlers like this:
emailTaken :: MonadIO m => Text -> AppT m APIEmail
emailTaken emailStr = do
pool <- ask -- this produces error
result <- liftIO $ runPoolPQ (Q.emailTakenSession emailStr) pool
return $ APIEmail result True
Problem
The compiler reports an error in ask
in emailTaken
:
* Couldn't match kind `[(ghc-prim-0.5.3:GHC.Types.Symbol,
Squeal.PostgreSQL.Schema.SchemumType)]'
with `Squeal.PostgreSQL.Schema.SchemumType'
From what I understand, it's trying to match type family Schemas
with type Schema
.
Question
How do I need to edit the type signatures to get this to compile and work?
Particularly emailTakenSession
is probably off at least.
Solution
In the interest of completeness for other readers, I needed to change
type SquealPool = SPG.Pool (SQ.K S.Connection Schema)
into
type SquealPool = SPG.Pool (S.K S.Connection '["public" ::: Schema])
The type family would resolve to this anyway, and this way I'm not providing an illegal construct (a type family) to MonadReader derivation in AppT
.