Closed bulldog98 closed 9 years ago
You need to modify your HasPostgres instance to include a definition for setLocalPostgresState. You can see some examples of built-in instances here:
I knew that my main problem is that I do not know what function I should use for this
I can't help without more detailed information about your monad.
I have this in my code, can't say if this is right though.
data AuthData a = AuthData
{ -- snip
database :: Lens' a (Snaplet Postgres)
}
instance HasPostgres (Handler a (AuthData a)) where
getPostgresState = (gets database >>= \d -> withTop d getPostgresState)
setLocalPostgresState pg act = do
d <- gets database
orig <- withTop d $ do
orig <- getsSnapletState (view snapletValue)
modifySnapletState (set snapletValue pg)
return orig
act `finally` withTop d (modifySnapletState (set snapletValue orig))
What I have right now is the following:
data App = App
{ _heist :: Snaplet (Heist App) -- ^ the heist subsnaplet
, _sess :: Snaplet SessionManager -- ^ the sessionmanager subsnaplet
, _db :: Snaplet Postgres -- ^ the postgres subsnaplet
, _auth :: Snaplet (AuthManager App) -- ^ the authmanager subsnaplet
}
makeLenses ''App
instance HasPostgres (Handler b App) where
getPostgresState = with db get
Try this:
setLocalPostgresState s = local (set (db . snapletValue) s)
@mightybyte, What do I write if I have a lens defined directly (to be able to use provide it from upper-level snaplet in initializer)?
data MySnaplet b = MS{db :: SnapletLens b Postgres}
instance HasPostgres (Handler b (MySnaplet b)) where
setLocalPostgresState s = ???
Hi,
right now I'm trying to port my old snap project to the new snaplet-postgres-simple release, but I get No instance nor default method for class operation Snap.Snaplet.PostgresqlSimple.Internal.setLocalPostgresState for my Type as soon as I try to load a page with an database query.