Best practice for SaaS multi-tenancy

Hi @danbars,

At the time a query starts, Fauna get a consistent snapshot of the database and execute the query against that snapshot. If something changes in the meanwhile, the whole transaction is aborted and restarted.
Getting a client revision might be a good idea in any case, and you can modify the function and get all in one shot:

CreateFunction({
  name: "updateSaaSDoc",
  role: null,
  body: Query(
    Lambda(['fooId','ownerId','clientRevision','payload'],
      Let(
        {
          doc: Get(Match('refByDocId',Var('fooId'))),
          ref: Select(['ref'],Var('doc')),
          owner: Select(['data','ownerId'],Var('doc')),
          revision: Select(['data','revision'],Var('doc'))
        },
        If(and(Equals(Var('owner'),Var('ownerId')), 
               Equals(Var('clientRevision'),Var('revision'))),
           Update(Var('ref'),{data:Merge(Var('payload'),Add(Var('revision'),1))}),
           "Owner or revision mismatch")
      )
    )
  )
}
)

hope this help.

Luigi