RethinkDB:​​将 ReQL 转换为 Integer 类型

RethinkDB: convert ReQL to an Integer type

无法弄清楚如何将 reql 转换为类型

我插入一个文档并返回结果,这是一个带有 "errors" 键的对象。我只需要看看它是否为 0?

像这样一个简单的函数:

savePerson :: Handler ()
savePerson = do
  let load = fromJust . extract ["data", "attributes"]
  doc <- load <$> jsonBody'
  res <- runDB $ table "articles" # insert doc
  if (res R.! "errors") == 0
  then setStatus status204
  else setStatus status500

这显然无法编译,但它给出了我想要的示例。 我需要将 reql 转换为 Integer 以进行比较。 如何将类型从 reql 转换为 haskell 类型?

谢谢

编辑

抱歉没有提供某些数据类型。

import Web.Spock (runQuery)
import Database.RethinkDB.NoClash


runDB :: (Result a, Expr q) => q -> Handler a
runDB act = runQuery $ \hdl -> run hdl act

runDB' :: Expr q => q -> Handler Datum
runDB' act = runQuery $ \hdl -> run' hdl act

EDIT2

感谢 AtnNn 对第一个示例的回答。但我仍然无法弄清楚 ReQL 转换。这是我的错,我不能想出一个很好的例子。我会尝试另一个。 我简单地得到一些 json,用 Aeson 构建响应 json 并将其发回删除 id 属性

fetchPerson :: Text -> Handler ()
fetchPerson oid = do
  res <- runDB' $ table "articles" # get (expr oid)
  json $ object [
    "data" .= [
      "type" .= ("articles"::Text),
      "id" .= oid,
      "attributes" .= (res # without ["id"])
      ]
    ]

现在,(res # without ["id"]) 仍然给我 ReQL,对吗?我需要将其转换为 ToJSON 实例或 Aeson Value 类型的类型。 我该怎么做?我通常如何将 ReQL 转换为 haskell 类型?

谢谢

您可以将 Result 实例用于 WriteResponse following the example in the docs for insert

res <- runDB $ table "articles" # insert doc
if writeResponseErrors res == 0

R.! 运算符用于构造 ReQL 类型的查询,而不是检查结果。