[Haskell-beginners] help writing a simple api which queries for a resource and displays this info

xu xiut xiut.xu at gmail.com
Sun Aug 21 22:05:52 UTC 2016

Hello, I am wanting to store something in a database, then retrieve it.
That is my entire ambition for the day.

Technologies chosen:
rethinkdb driver:

I chose to go with Rethinkdb, but i'm open to using Postgresql.

If someone requests for /companies, I want to list all the companies in the
"companies" table.
-- ghci
λ> companies <- run h $ table "companies" :: IO [Datum]
λ> companies
λ> firstCompany <- run h $ table "companies" ! "name" :: IO [Datum]
λ> firstCompany
λ> :info Datum
data Datum
  = Null
  | Bool Bool
  | String Text
  | Number Double
  | Array Database.RethinkDB.Datum.Array
  | Object Database.RethinkDB.Datum.Object
  | Time time-
  | Point LonLat
  | Line GeoLine
  | Polygon GeoPolygon
  | Binary bytestring-
    -- Defined in ‘Database.RethinkDB.Datum’
instance Eq Datum -- Defined in ‘Database.RethinkDB.Datum’
instance Ord Datum -- Defined in ‘Database.RethinkDB.Datum’
instance Show Datum -- Defined in ‘Database.RethinkDB.Datum’
instance Expr Datum -- Defined in ‘Database.RethinkDB.ReQL’
instance Result Datum -- Defined in ‘Database.RethinkDB.Driver’
instance ToDatum Datum -- Defined in ‘Database.RethinkDB.Datum’
instance FromDatum Datum -- Defined in ‘Database.RethinkDB.Datum’
15:40 < lpaste> xuxu revised “investigating the Datum
                data type”: “investigating the Datum data
                type” at http://lpaste.net/179391

I don't know how to display the result.

>From Spock's readme, here's an example where I don't need to query the
main =
     runSpock 3000 $ spockT id $
     do get "companies" $
            text $ T.concat ["tesla", "google"]
How to convert that into something a little more practial where I do
utilize a db?
Here's what I have right now, but I'm sure it doesn't compile
{-# LANGUAGE OverloadedStrings #-}

import Web.Spock
import qualified Data.Text as T
import qualified Database.RethinkDB as R
import qualified Database.RethinkDB.NoClash

default (Datum, ReQL, String, Int, Double)

main :: IO ()
main =
    -- 1. i don't know if the following line will work
    let h = connect "localhost" 28015 Nothing in
        runSpock 3000 $ spockT id $
        -- 2. list all companies
        do get "companies" $
               text $ T.concat -- something

I don't exactly know how "text" works.

If I'm reading
correctly, spockT is using a reader monad. I don't know how it works with
the Text type, but I have a suspicion I need to convert Rethink's Datum
type into a Text type.

I would be absolutely thrilled if anyone is able to help with this.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160821/a49cc985/attachment.html>

More information about the Beginners mailing list