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

  • SEARCH
  • Home
  • 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 6712185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:13:47+00:00 2026-05-26T08:13:47+00:00

I want to create a Happstack application with lots of access to a database.

  • 0

I want to create a Happstack application with lots of access to a database. I think that a Monad Stack with IO at the bottom and a Database Write-like monad on top (with log writer in the middle) will work to have a clear functions in each access, example:

itemsRequest :: ServerConfig -> ServerPart Response
itemsRequest cf = dir "items" $ do
  methodM [GET,HEAD]
  liftIO $ noticeM (scLogger cf) "sended job list"

  items <- runDBMonad (scDBConnString cf) $ getItemLists

  case items of
    (Right xs) -> ok $ toResponse $ show xs
    (Left err) -> internalServerError $ toResponse $ show err

With:

getItemList :: MyDBMonad (Error [Item])
getItemList = do
  -- etc...

But I have little knowledge of Monad and Monad Transformers (I see this question as an exercise to learn about it), and I have no idea how to begin the creation of Database Monad, how to lift the IO from happstack to the Database Stack,…etc.

  • 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-05-26T08:13:48+00:00Added an answer on May 26, 2026 at 8:13 am

    You likely want to use ‘ReaderT’:

    type MyMonad a = ReaderT DbHandle ServerPart a
    

    The Reader monad transformer makes a single value accessible using the ask function – in this case, the value we want everyone to get at is the database connection.

    Here, DbHandle is some connection to your database.

    Because ‘ReaderT’ is already an instance of all of the happstack-server type-classes all normal happstack-server functions will work in this monad.

    You probably also want some sort of helper to open and close the database connection:

    runMyMonad :: String -> MyMonad a -> ServerPart a
    runMyMonad connectionString m = do
       db <- liftIO $ connect_to_your_db connectionString
       result <- runReaderT m db
       liftIO $ close_your_db_connection db
    

    (It might be better to use a function like ‘bracket’ here, but I don’t know that there is such an operation for the ServerPart monad)

    I don’t know how you want to do logging – how do you plan to interact with your log-file? Something like:

    type MyMonad a = ReaderT (DbHandle, LogHandle) ServerPart a
    

    and then:

    askDb :: MyMonad DbHandle
    askDb = fst <$> ask
    
    askLogger :: MyMonad LogHandle
    askLogger = snd <$> ask
    

    might be enough. You could then build on those primitives to make higher-level functions. You would also need to change runMyMonad to be passed in a LogHandle, whatever that is.

    Once you get more than two things you want access to it pays to have a proper record type instead of a tuple.

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

Sidebar

Related Questions

I want to create my Rails application with MySQL, because I like it so
I want create something that looks like the MSN Messenger chat forms. I am
I want create 32 views like above view. so that what i modify in
I want create a helper class that containing method like cleanArray, split_char, split_word, etc.
I want create a loading progress bar like Gmail in center and top of
I want to create a Java application bundle for Mac without using Mac. According
I want to create a function that performs a function passed by parameter on
I want to create a simple http proxy server that does some very basic
I have a set of Happstack.State MACID methods that I want to test using
I want create a tarball that when extracted the file(s) will be placed at

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.