Search code examples
databasenext.jsfaunadb

How to update a document in Fauna without knowing the ref id


I'm trying to update a document within a collection in Fauna, but I don't know the ref id besides looking in the database. I've been looking at this post: Can I update a FaunaDB document without knowing its ID? as I get the exact same error but I don't know exactly how to implement it into my own code.

I'm trying to update a document containing a hairdresserId within data.

{
  "ref": Ref(Collection("hairdressers"), "328130955075125442"),
  "ts": 1649343283790000,
 data: {
  "hairdresserId": "328027762241568962",
  }
}

This is my API file:

import { updateJobProfileInfo } from "@/utils/Fauna"
import { getSession } from "next-auth/react"

export default async (req, res) => {
  const session = await getSession({ req })
  if (!session) return res.status(401)

  const hairdresserId = session.user.id
  if (req.method !== "PUT") {
    return res.status(405).json({ msg: "Method not allowed" })
  }

  const {
    image,
    coverImage,
    bio,
    education,
    phone,
    email,
    status,
    preferences,
  } = req.body

  try {
    const updated = await updateJobProfileInfo(
      hairdresserId,
      image,
      coverImage,
      bio,
      education,
      phone,
      email,
      status,
      preferences
    )
    return res.status(200).json(updated)
  } catch (err) {
    console.error(err)
    res.status(500).json({ msg: "Something went wrong." })
  }
  res.end()
}

This is my fauna function:

const updateJobProfileInfo = async (
  hairdresserId,
  image,
  coverImage,
  bio,
  education,
  phone,
  email,
  status,
  preferences
) => {
  return await faunaClient.query(
    q.Update(q.Ref(q.Collection("hairdressers"), hairdresserId), {
      data: {
        image,
        coverImage,
        bio,
        education,
        phone,
        email,
        status,
        preferences,
      },
    })
  )
}

How can I update a document within my hairdressers collection when I don't have the ref but I know that the document contains hairdresserId ?


Solution

  • I was able to solve it by replacing this bit within the fauna function:

    q.Update(q.Ref(q.Collection("hairdressers"), hairdresserId),
    

    with this:

    q.Update(
          q.Select(
            "ref",
            q.Get(q.Match(q.Index("hairdresser_by_id"), hairdresserId))
          ),
    

    I had to create an index with the following term hairdresserId and I named it hairdresser_by_id.

    This is the complete fauna function:

    const updateJobProfileInfo = async (
      hairdresserId,
      image,
      coverImage,
      bio,
      education,
      phone,
      email,
      status,
      preferences
    ) => {
      return await faunaClient.query(
        q.Update(
          q.Select(
            "ref",
            q.Get(q.Match(q.Index("hairdresser_by_id"), hairdresserId))
          ),
          {
            data: {
              image,
              coverImage,
              bio,
              education,
              phone,
              email,
              status,
              preferences,
            },
          }
        )
      )
    }