Bulk import?

I’ve successfully set up a database with the GraphQL interface. I’ve got a JSON file with about 1000 items I’d like to bulk import to the database (or it could be a csv file). I don’t really want to write JavaScript to do it. Is there a utility I can use to bulk import documents to a collection? Or is it possible to paste such a large mutation in the Fauna GraphQL Playground?

3 Likes

I also would like to know.
my first thought would be to write a throttled script that posts a mutation with a bunch of mutations

You should create a User Defined Function (UDF) that takes multiple documents at once.

Thanks! Is there an example in the Fauna docs that shows how to create a UDF that takes multiple documents? Or a blog post somewhere?

I had trouble figuring out UDFs using the docs alone but here is an example of a function I set up for one of my projects:

Query(
  Lambda(
    "gearlist",
    Foreach(
      Var("gearlist"),
      Lambda(
        "newItem",
        Let(
          {
            itemID: Select("id", Var("newItem")),
            itemSet: Match(Index("itemByID"), Var("itemID"))
          },
          If(
            IsEmpty(Var("itemSet")),
            Create(Collection("Item"), { data: Var("newItem") }),
            Let(
              {
                itemRef: Select("ref", Get(Var("itemSet"))),
                itemCurrentData: Select("data", Get(Var("itemRef"))),
                newItemStats: [
                  Select("name", Var("newItem")),
                  Select("quality", Var("newItem")),
                  Select("phase", Var("newItem")),
                  Select("reqLvl", Var("newItem")),
                  Select("drop", Var("newItem")),
                  Select("id", Var("newItem"))
                ],
                doStatsMatch: Map(
                  Var("newItemStats"),
                  Lambda(
                    "statValue",
                    ContainsValue(Var("statValue"), Var("itemCurrentData"))
                  )
                )
              },
              If(
                Not(All(Var("doStatsMatch"))),
                Update(Var("itemRef"), { data: Var("newItem") }),
                false
              )
            )
          )
        )
      )
    )
  )
)

I simply provide an array of new document objects. This could be improved by adding in deeper checks for updated props, missing items, etc. but this is working for me. By the way, I’ve run this function using hundreds of documents at a time. Hope this helps!

5 Likes

Hi @David-Torres-Design nice work, this is a very clever solution! @DanielKehoe I want to let you know that we at Fauna appreciate the need for a bulk import tool and we’re looking to slot that into the backlog. At present, you’d need to roll your own, as David and @man2xxl have, but as I say, we’re looking to get a tool to you in the near future.

3 Likes

Thanks @Bryan_Fauna, I look forward to seeing an official bulk import feature.

1 Like