/
Main.purs
70 lines (56 loc) · 2.32 KB
/
Main.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
module Test.Main where
import Prelude (Unit, pure, show, bind, (<>),
($), class Show, unit)
import Control.Monad.Aff (launchAff)
import Control.Monad.Aff.Console (logShow, log)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE)
import Control.Monad.Eff.Exception (EXCEPTION)
import Control.Monad.State.Trans (evalStateT)
import Control.Monad.Trans.Class (lift)
import Data.Argonaut.Decode (class DecodeJson, gDecodeJson)
import Data.Argonaut.Encode (class EncodeJson, gEncodeJson)
import Data.Generic (class Generic)
import Data.Lens (lens)
import Data.Maybe (Maybe(..))
import Data.NonEmpty ((:|))
import GoogleCloud.Datastore
data HereIsData
= HereIsData
{ bar :: String
, baz :: Int
, quux :: Boolean }
testData :: HereIsData
testData = HereIsData { bar: "hello", baz: 123, quux: true }
testData2 :: HereIsData
testData2 = HereIsData { bar: "hello_number 2", baz: 456, quux: false }
derive instance genericHereIsData :: Generic HereIsData
instance encodeHereIsData :: EncodeJson HereIsData where
encodeJson = gEncodeJson
instance decodeHereIsData :: DecodeJson HereIsData where
decodeJson = gDecodeJson
instance showHereIsData :: Show HereIsData where
show (HereIsData x) = "HereIsData: " <> x.bar <> " " <> show x.baz <> " " <> show x.quux
data Config
= Config { datastore :: Datastore }
instance configHasDatastore :: HasDatastore Config where
_datastore = lens (\(Config x) -> (x.datastore)) (\d x -> Config { datastore: x })
main :: Eff (console :: CONSOLE, datastore :: DATASTORE, err :: EXCEPTION) Unit
main = do
ds <- configuredDatastore (ProjectId "hello") (Credentials "")
launchAff $ do
res <- evalStateT prog (Config { datastore: ds })
log "Finished evaluating program"
logShow res
pure unit
where prog = do
key <- keyWithId (Kind "FOO") (Id 1)
key2 <- keyWithId (Kind "FOO") (Id 2)
save key Upsert testData2
save key2 Upsert testData
getres :: Array HereIsData <- get (key :| []) Nothing
q <- createQuery Nothing (Kind "FOO") [
limit 2
]
qres :: Array HereIsData <- lift (queryRunUntilComplete q (Just Eventual))
pure qres