[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:
spock:
https://github.com/agrafix/Spock
rethinkdb driver:
https://github.com/AtnNn/haskell-rethinkdb
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
[{"name":"tesla","id":"7781ee7e-1e43-4608-bb96-fe10cac3b53a"}]
λ> firstCompany <- run h $ table "companies" ! "name" :: IO [Datum]
λ> firstCompany
["tesla"]
λ> :info Datum
data Datum
= Null
| Bool Bool
| String Text
| Number Double
| Array Database.RethinkDB.Datum.Array
| Object Database.RethinkDB.Datum.Object
| Time time-1.5.0.1:Data.Time.LocalTime.LocalTime.ZonedTime
| Point LonLat
| Line GeoLine
| Polygon GeoPolygon
| Binary bytestring-0.10.6.0:Data.ByteString.Internal.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
database:
```
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
https://github.com/agrafix/Spock/blob/1ee54503ad67f62af795a31772040f56f7ae08fd/Spock-core/src/Web/Spock/Core.hs#L55
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