I have a use case where I would like to query all connections from the connections
collection, and at the same time find the right document using employerId
and employeeId
from their respective collections (employer and employee collection) using Fauna DB. So, if I get an employerId back from the query, I would like to get the data back from the document within the employer collection.
A document from the connections collection looks like this:
{
"ref": Ref(Collection("connections"), "336420921043583169"),
"ts": 1657094868435000,
"data": {
"employerId": "330616765804445892",
"employeeId": "330616700633350340",
"isEmployeeApproved": true,
"isEmployerApproved": true,
"connectionAcceptedOnDate": "2022-07-06T08:07:47.846Z"
}
}
I tried to create my function as the following, where I'm getting the data from the connections
collection but I do not get anything from the other collections mentioned using the employerId
and employeeId
:
const getConnections = async () => {
return await faunaClient.query(
q.Map(
q.Paginate(q.Match(q.Index("connections"))),
q.Lambda("connectionDoc", q.Get(q.Var("connectionDoc")))
),
q.Ref(Collection("employer"), q.Select("employerId", q.Var("connectionDoc"))),
q.Ref(
Collection("employees"),
q.Select("employeeId", q.Var("connectionDoc"))
),
q.Get(q.Var("employerDoc")),
q.Get(q.Var("employeeDoc"))
)
}
The ref I'm getting seems to be the first document within the connections
collection. This is what I'm getting if I console log it:
data: Array [ {…}, {…} ]
0: Object { ref: {…}, ts: 1656490913630000, data: {…} }
data: Object { employeeId: "330074643026149574", employerId: "331454006483222721", isEmployeeApproved: true, … }
ref: Object { "@ref": {…} }
"@ref": Object { id: "335787295330271425", collection: {…} }
collection: Object { "@ref": {…} }
"@ref": Object { id: "connections", collection: {…} }
collection: Object { "@ref": {…} }
"@ref": Object { id: "collections" }
id: "collections"
<prototype>: Object { … }
<prototype>: Object { … }
id: "connection_request"
<prototype>: Object { … }
<prototype>: Object { … }
id: "335787295330271425"
<prototype>: Object { … }
<prototype>: Object { … }
ts: 1656490913630000
<prototype>: Object { … }
How can I select the employerId
and look up the right document from the employer
collection? I would like to do the same for the employeeId
and the employee
collection in the same query.
Updates
A suggestion has been made:
const connections = async () => {
return await faunaClient.query(
q.Map(q.Paginate(q.Documents(Collection("connections")))),
q.Lambda(
"ref",
q.Let(
{
ref: q.Ref(Collection("connections"), id),
connectionDoc: q.Get(q.Var("ref")),
employerId: q.Select(["data", "employerId"], q.Var("connectionDoc")),
employeeId: q.Select(
["data", "employeeId"],
q.Var("connectionDoc")
),
employerRef: q.Ref(Collection("employers"), q.Var("employerId")),
employeeRef: q.Ref(
Collection("employees"),
q.Var("employeeId")
),
employerDoc: q.Get(q.Var("employerRef")),
employeeDoc: q.Get(q.Var("employeeRef")),
},
{
ref: q.Var("ref"),
ts: q.Select(["ts"], q.Var("connectionDoc")),
data: {
employee: q.Var("employeeDoc"),
employer: q.Var("employerDoc"),
isEmployeeApproved: q.Select(
["data", "isEmployeeApproved"],
q.Var("connectionDoc")
),
isEmployerApproved: q.Select(
["data", "isEmployerApproved"],
q.Var("connectionDoc")
),
connectionAcceptedOnDate: q.Select(
["data", "connectionAcceptedOnDate"],
q.Var("connectionDoc")
),
},
}
)
)
)
}
I receive the following error message when trying to use the function above:
InvalidArity: Map function requires 2 argument(s) but 1 were given
For more info, see the docs: https://docs.fauna.com/fauna/current/api/fql/functions/map