Return empty array if nothing matches

I’ve got an fql query that looks like this.

q.Get(q.Match(q.Index("usersByEmail"), email))

My goal is to look up a user by email address. Return the user if it is found and otherwise return null.

Is this the correct approach? I’m seeing the query throw an exception if nothing matches.

@bradgarropy Welcome to Fauna Forums!

Get expects a valid ref. You can use

If(
 Exists(Match(Index("usersByEmail"), email)),
 Get(Match(Index("usersByEmail"), email)),
 null
)

Hi @Jay-Fauna,

Can you please refer me to an article that explains how’s Fauna keeping code like this performant?
The naive implementation of such code will have to find the result in the index twice, but I assume this is not how it really works.
Even if the first result is cached, how does this code make minimum disk/network io calls? Where does it actually run?

Thanks

@danbars I am afraid we may not have any docs/blogs to explain how our Query Coordinator works.

To summarize above expression in all is one Fauna query aka transaction.
Coordinator receives an expression, parses, evaluates, identifies read and/or write effects , delegates requests to nodes owning respective data, uses cache as needed and bundles the response back.

Not sure if that was helpful but I will reach out to team to see if someone can write up in more detail.

This is a good read on Fauna Transactions.

Interesting article. Thanks for the link @Jay-Fauna

@danbars, you can use the Let function. Perhaps you knew that and was why you said “naive implementation”. Sorry if that’s the case. But to share anyway:

Let(
  {
    match: Match(Index("usersByEmail"), email)
  },
  If(
   Exists(Var(‘match’)),
   Get(Var(‘match’)),
   null
  )
)

@ptpaterson, I was more interested to know how Fauna implemented the processor so it is optimized .
Thanks for the reply anyway :wink: