Orchestrating multiple dependent expressions in the same transaction with Do

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 4 years ago

There are a couple of different way to execute multiple expressions in FQL. Do is a function that lets us execute expressions in order and fail the entire query if one of the expressions fails.

Do(
  Create(Collection("products")), {
    data: {
      "name": "Bacon",
      "description": "crispy, hot",
      "price": 4.27,
      "quantity": 1000,
      "warehouse": Ref(Collection("warehouses"), "244075679449088521"),
      "backorderLimit": 15,
      "backordered": false
    }
  }),
  Paginate(Match(Index("all_products")))
)

Instructor: [0:00] The do function in FQL allows us to evaluate a series of expressions in order from left to right. This means we can do insertions. Later, after we've done the insertion, we can query for that ID and get it returned.

[0:14] If any of the expressions in a do call fail, all we get back is an error and none of the insertions are committed. The do function only returns the results of the last statement.

[0:24] In the FaunaDB console in our products collection, we have three documents. We have stock for cups, beef chick, and pizza,. Using the do function, we can create a new product and then return the paginated match on the index of all products.

[0:41] Note that because the do function executes from left to right, the create function will execute first, and the paginate is able to access the results of the create. When we run this query, we can see that we returned four documents in our products collection instead of the three we had before.

[0:59] If we refresh the products collection, we can see that bacon is now inserted into the products collection.

egghead
egghead
~ 6 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today