Search code examples
faunadb

How to run multiple faunadb queries sequentially in the one request?


I want to create a collection, then create an index on that collection. An array of queries is valid FQL, so I tried this:

[
  CreateCollection({ name: "users" }),
  CreateIndex({
    name: "users_by_email",
    permissions: { read: "public" },
    source: Collection("users"),
    terms: [{field: ["data", "email"]}],
    unique: true
  })
]

But it fails with a Cannot read reference error. I think this is because it tries to get the Collection("users") for the second part before creating it.

Is it possible to execute multiple queries sequentially in the one call to faunadb?


Solution

  • I think this may be what you are looking for. The Do function can run a list of statements sequentially

    https://docs.fauna.com/fauna/current/api/fql/functions/do?lang=javascript