zoran119
zoran119

Reputation: 11307

How to query a database from a snaplet

My application is defined as:

data App = App { _api :: Snaplet Api, _db :: Snaplet Postgres }

makeLenses ''App

instance HasPostgres (Handler b App) where
    getPostgresState = with db get
    setLocalPostgresState s = local (set (db . snapletValue) s)

where _api is a snaplet I am creating. It is defined as:

data Api = Api

I would now like to use query_ function inside the Api snaplet. The only way I can do so is to define Api as data Api = Api { _db :: Snaplet Postgres }, but it seems that this will initialise postgres snaplet twice, which I don't think is right. I'm not sure how to get _db from the 'parent' App either.

Am I on the right path here? What's the correct way to access the database in the Api snaplet?

Upvotes: 0

Views: 121

Answers (1)

mightybyte
mightybyte

Reputation: 7282

Why are you creating this Api snaplet? Are you reusing the things in Api across two or more web apps? If not, I would recommend not using a snaplet. Then everything will work perfectly. Handler App App will have a HasPostgres instance and you can go on your merry way.

Upvotes: 0

Related Questions