Search code examples
abacfaunadb

How to get record's "Ref" in FaunaDB predicate function?


I am trying to prepare “write” predicate function. The idea is to allow user changing only his own profile. I am comparing the “id” from Token and record “Ref”. By the logic it should work like this:

  q.Lambda(
    ['old', 'new'],
    q.Equals(
      q.Select(["id"], q.CurrentIdentity()),
      q.Select(['ref'], q.Var('old'))
    )
  )
)

But it doesn’t: “Error: Insufficient privileges to perform the action.” By the “predicate function” documentation:

write, history_write: the old data, the new data, and a reference to the document to be written.

I changed the function, but the error still appears. The code:

q.Query(
  q.Lambda(
    ['old', 'new', 'ref'],
    q.Equals(
      q.Select(["id"], q.CurrentIdentity()),
      q.Var('ref')
    )
  )
)

I also tried to hardcode the user ID to this function and it works well:

q.Query(
  q.Lambda(
    ['old', 'new', 'ref'],
    q.Equals(
      q.Select(["id"], q.CurrentIdentity()),
      "295870713291604487"    
    )
  )
)

What I am doing wrong?


Solution

  • Try this:

    q.Query(
      q.Lambda(
        ['old', 'new', 'ref'],
        q.Equals(
          q.CurrentIdentity(),
          q.Var('ref')
        )
      )
    )
    

    Similar to this: https://github.com/Vadorequest/rwa-faunadb-reaflow-nextjs-magic/blob/main/fauna/roles/Editor.ts#L73-L81

    The q.Var('ref') returns a Ref object, not an id.