'Fauna delete first item from intersection

I have a fauna db query that will return one match due to the combination of fields being unique:

await faunaClient.query(
  Intersection(
    Match(Index('tokens_search_by_blacklisted'), false),
    Match(Index('tokens_search_by_token'), refreshToken),
    Match(Index('tokens_search_by_type'), tokenTypes.REFRESH)
  )
)

What I want to know is from here how can I delete the first item returned, I know theoretically I can paginate, lambda delete but I don't want to iterate. I would just like to delete the first value returned.

I know if you use Get it will get the first item from a set but sadly delete doesn't do this and I shouldn't have to get the object aka perform a read just to select the ref then delete.



Solution 1:[1]

When you use Match, the result is a set reference for an index-backed set. Such sets include a reference to the covered document, even if the index doesn't explicitly specify the ref field in the values definition.

Once you use Intersection, Union, or Difference, the resulting set only includes the fields defined in an index's values definition: the implicit ref field is lost.

In Fauna, sets are unbounded, so there aren't any concrete items to operate on until the set is materialized via Paginate or Get. So, you'll need to use one of those functions in order to retrieve the tuples from the intersection.

You can set the page size to 1 so that Paginate only returns one item. Paginate's implementation returns as soon as it has a page full of results, so it performs pretty well.

You'll need to be sure that the tuples in the Intersection result includes the document reference to delete, or you won't be able to delete.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 eskwayrd