Handling ref in Node JS server with Faunadb

109 views Asked by At

This might be a stupid question. I am building a server with Node JS that exposes an API to clients. I was wondering what is the recommended way to handle "ref" in the query results.

For example, I have a query like this -

q.Map(
   q.Paginate(
      q.Match(q.Index("network_by_creator"), q.Select("ref", q.Call(q.Function("getUserByUsername"), username))),
       options
    ),
   q.Lambda("x", q.Get(q.Var("x")))
)

This returns all the networks created by the user. An example result looks like this -

{
    "before": [
        {
            "@ref": {
                "id": "279699094284272133",
                "collection": {
                    "@ref": {
                        "id": "networks",
                        "collection": {
                            "@ref": {
                                "id": "collections"
                            }
                        }
                    }
                }
            }
        }
    ],
    "data": [
        {
            "ref": {
                "@ref": {
                    "id": "279699094284272133",
                    "collection": {
                        "@ref": {
                            "id": "networks",
                            "collection": {
                                "@ref": {
                                    "id": "collections"
                                }
                            }
                        }
                    }
                }
            },
            "ts": 1603000692605000,
            "data": {
                "creator": {
                    "@ref": {
                        "id": "279656553326313989",
                        "collection": {
                            "@ref": {
                                "id": "users",
                                "collection": {
                                    "@ref": {
                                        "id": "collections"
                                    }
                                }
                            }
                        }
                    }
                },
                "name": "Hello2"
            }
        }
    ]
}

I was wondering should I remove the "ref" fields from the objects before sending them to the client (the "name" field can be used to search a network), or should I extract the "id" from the "ref" and send it along? In any case, can that be done through FQL, instead of querying the result and manually modifying?

1

There are 1 answers

0
eskwayrd On

A Reference is a compound value, comprised of the document's collection reference and a document ID.

You can certainly extract the document ID to send to the client, provided that when the client sends it to your API, you know which collection to use. If your API transits data for multiple collections, you should also return the collection reference name too.

Here is a query that demonstrates how to pull out the components of a Reference:

> Let(
  {
    ref: Ref(Collection("networks"), "279699094284272133")
  },
  {
    collection: Select("collection", Var("ref")),
    collection_Name: Select(["collection", "id"], Var("ref")),
    documentID: Select("id", Var("ref"))
  }
)
{
  collection: Collection("networks"),
  collection_Name: 'networks',
  documentID: '279699094284272133'
}

Just use the appropriate Select expression to extract what you need.