Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7729167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:54:01+00:00 2026-06-01T05:54:01+00:00

I am trying to understand simple snaplet construction. Also, when do I actually need

  • 0

I am trying to understand simple snaplet construction.
Also, when do I actually need to make a snaplet and when a simple side library?
And if I do need one how do I make it out of a library?

For example, I have a bunch of DB functions where I wrap my SQL code like below.

data Person = Person {personName :: ByteString, personAge :: Int}

connect :: IO Connection
connect = connectSqlite3 "/somepath/db.sqlite3"

savePerson :: Person -> IO ()
savePerson p = do
c <- connect
run c "INSERT INTO persons (name, age) \
      \VALUES (?, ?)"
      [toSql (personName p), toSql (personAge p)]
commit c
disconnect c

Every function initiates a new connection and closes the connection after commit.
I guess making a snaplet is the way to avoid connection in every function?
In my handler I would use it like this:

insertPerson :: Handler App App ()
insertPerson = do
  par <- getPostParams
  let p = top par
  liftIO $ savePerson p
where
  top m =
    Person {personName = head (m ! (B.pack "name"))
           ,personAge  = read (B.unpack (head (m ! (B.pack "age")))) :: Int
           }

It works so far. My question(s) is/are:
When do I actually need to turn a library into a snaplet? Do I need to turn my simple DB library into a snaplet just to initialize the connection instead of making the connection in every function?

Now, If I make the snaplet …
On the Snap website there is a tiny example of a top level sanaplet but there is no trace of how to make simple pluggble snaplet of your own.

So I added snaplet initialization function to my DB library

dbInit :: SnapletInit b Connection
dbInit = makeSnaplet "DB" "My DB Snaplet" Nothing $ do
  dbc <- liftIO $ connectSqlite3 "/somepath/db.sqlite3"
  onUnload $ disconnect dbc
  return $ dbc

Is this the correct way of doing it? Is this all I need to turn it into a pluggble snaplet?

Then I stack this DB snaplet into the main app

data App = App
  { _heist :: Snaplet (Heist App),
    _dbcon :: Snaplet (Connection)
  }

makeLens ''App

app :: SnapletInit App App
app = makeSnaplet "app" "My app" Nothing $ do
  h <- nestSnaplet "heist" heist $ heistInit "templates"
  d <- nestSnaplet "" dbcon dbInit
  addRoutes routes
  return $ App h d

Now, all I gain is the connection available to my request handlers, right?
So my handler becomes:

insertPerson :: Handler App App ()
insertPerson = do
  par <- getPostParams
  let person = top par
  connection <- gets _dbcon
  liftIO $ savePerson connection person
where
  top m =
    Person {personName = head (m ! (B.pack "name"))
           ,personAge  = read (B.unpack (head (m ! (B.pack "age")))) :: Int
           }

This does not seem to work. What am I doing wrong? Is this the correct way to extract the connection from the snaplet handle (dbcon)? Is this generaly the correct direction to construct a simple snaplet? Do I actually need a snaplet here in my case?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T05:54:03+00:00Added an answer on June 1, 2026 at 5:54 am

    Handler is an instance of MonadState: MonadState v (Handler b v).

    Handler is also an instance of MonadSnaplet and therefore provides the with method:
    with :: Lens v (Snaplet v') -> m b v' a -> m b v a

    dbcon is a Lens App (Snaplet Connection).

    So to get to the Connection we can use:
    conn <- with dbcon get

    You would normally create a snaplet if you were providing functionality that everyone might benefit from. In your case, it’s probably best to take advantage of the HDBC snaplet, which you can use to connect to a sqlite3 db.

    Checkout http://norm2782.github.com/snaplet-hdbc.html for a good tutorial on using the HDBC snaplet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand type members in Scala. I wrote a simple example
I'm trying to create a simple example to understand how to store then retrieve
I'm trying to understand how threads work, and I wrote a simple example where
Im new to symfony and have some simple questions. I am trying to understand
I am trying to understand HTML5 history object. Here is a simple example which
I'm trying to understand WPF binding. As simple as it gets: I have a
I am trying to understand OAuth 2.0( SERVER SIDE FLOW ). Lets take simple
I am trying to understand this simple hashlib code in Python that has been
Just trying to understand that - I have never used it before. How is
I'm trying to understand how I can paint simple graphics in x86 protected mode

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.