Do

Do( expression, ... )
Do( expression, ... )
Do( expression, ... )
Do( expression, ... )
do( expression, ... )
Do( expression, ... )

Description

The Do function evaluates a list of expressions which are provided as arguments. This evaluation occurs sequentially, from left to right, ensuring that modifications made by earlier expressions are seen by later expressions. If one of the expressions evaluated by Do returns an error, the current transaction is terminated and none of the expressions' effects are persisted in the database. If all of the expressions executed by Do succeed, only the results of the last statements executed are returned. If no expressions are provided, Do returns an error.

Parameters

Argument Type Definition and Requirements

expression

List of Expressions

One or more expressions to be evaluated.

Returns

The evaluation of the last expression.

Examples

The query below has a Do statement with two expressions. The first expression creates a document associated with the "magical_creatures" collection, with an id of 1. The second expression retrieves the document from the ref containing the collection "magical_creatures" and id of 1. This was the document just created in the previous statement. Only the results from the second retrieving expression are returned.

client.Query(
  Do(
    Create(
      Ref(Collection("magical_creatures"), "1"),
      Obj("data", Obj("name", "Hen Wen"))),
    Get(Ref(Collection("magical_creatures"), "1"))));
{
  "errors": [ { "code": "unauthorized", "description": "Unauthorized" } ]
}
curl https://db.fauna.com/ \
    -d '{
          "do": [
            {
              "create": { "ref": { "collection": "magical_creatures" }, "id": "1" },
              "params": {
                "object": { "data": { "object": { "name": "Hen Wen" } } }
              }
            },
            {
              "get": { "ref": { "collection": "magical_creatures" }, "id": "1" }
            }
          ]
        }'
HTTP/1.1 401 Unauthorized
{
  "errors": [ { "code": "unauthorized", "description": "Unauthorized" } ]
}
result, _ := client.Query(
    f.Do(
        f.Create(
            f.RefCollection(f.Collection("magical_creatures"), "1"),
            f.Obj{"data": f.Obj{"name": "Hen Wen"}},
        ),
        f.Get(f.RefCollection(f.Collection("magical_creatures"), "1")),
    ),
)

fmt.Println(result)
map[ref:{1 0xc4201a85e0 <nil>} ts:1526946657492696 data:map[name:Hen Wen]]
System.out.println(
    client.query(
        Do(
           Create(
              Ref(Collection(Value("magical_creatures")), Value("1")),
              Obj("data", Obj("name", Value("Hen Wen")))
           ),
          Get(Ref(Collection(Value("magical_creatures")), Value("1")))
        )
    ).get());
{
  ref: ref(id = "1", collection = ref(id = "magical_creatures", collection = ref(id = "collections"))), 
  ts: 1536603599790006, 
  data: {name: "Hen Wen"}
}
client.query(
  q.Do(
    q.Create(
      q.Ref(q.Collection('magical_creatures'), '1'),
      { data: { name: 'Hen Wen' } },
    ),
    q.Get(q.Ref(q.Collection('magical_creatures'), '1')),
  )
)
.then((ret) => console.log(ret))
{ ref: Ref(id=1, collection=Ref(id=magical_creatures, collection=Ref(id=collections))),
  ts: 1526675832587944,
  data: { name: 'Hen Wen' } }
client.query(
  q.do(
    q.create(
      Ref(q.collection("magical_creatures"), "1"),
      {"data": {"name": "Hen Wen"}}
    ),
    q.get(Ref(q.collection("magical_creatures"), "1"))
  ))
{
  "errors": [ { "code": "unauthorized", "description": "Unauthorized" } ]
}
client.query(
  Do(
    Create(
      Ref(Collection("magical_creatures"), "1"),
      Obj("data" -> Obj("name" -> "Hen Wen"))),
    Get(Ref(Collection("magical_creatures"), "1"))))
{
  "errors": [ { "code": "unauthorized", "description": "Unauthorized" } ]
}

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!