Does fauna supports Upserts?

Hello!

I’ve been making some fauna testing. But I can’t find an easy way to Upsert a document based on some parameters. This is my approach using JS.

const {data} = await client.query(
  q.Paginate(
    q.Match(q.Index('unique_Cart_cart_id'), cart.cart_id)
  )
)

const exists = data[0]

if(!exists){
  await // Update Document
} else {
  await // Create Document
}
1 Like

You can use FQL for that:

q.If(
   q.Exists(q.Match(q.Index('unique_Cart_cart_id'), cart.cart_id)), 
   q.Update(...),
   q.Create(...)
)
5 Likes