I have the following index:
CreateIndex({
name: 'userRefSortedByFullName',
source: Collection('users'),
terms: [{ field: ['ref'] }],
values: [
{ field: ['data', 'firstName'] },
{ field: ['data', 'middleName'] },
{ field: ['data', 'surname'] },
{ field: ['ref'] }
]
})
This index was made only for sorting a list of users by name, in ascending order.
Now I have another index:
CreateIndex({
name: 'friendsByUserId',
source: Collection('friendships'),
terms: [{ field: ['data', 'userId'] }],
values: [
{ field: ['data', 'numTimesOpened'], reverse: true },
{ field: ['data', 'userId'] },
{ field: ['ref'] }
]
})
What I want to do, is to get the list of friends of a user and sort them based on how many times that user has opened that friend’s profile (indicated by numTimesOpened
) in descending order and by full name in ascending order.
If I do:
Map(
Paginate(
Join(
Match(Index("friendsByUserId"), authUser.id),
Lambda(
["numTimesOpened", "userId", "ref"],
Match(
Index("userRefSortedByFullName"),
Ref(Collection("users"), Var("userId"))
)
)
),
{
size: 20,
after: nextToken ? Ref(Collection("friendships"), nextToken) : [],
}
),
Lambda(
["numTimesOpened", "userId", "ref"],
Merge(
Get(Collection("users"), Var("userId")),
{ numTimesOpened: Var("numTimesOpened") }
)
)
);
Will Join
still consider the first-level order clause which is to sort by numTimesOpened
? or will it disregard that and simply sort by name alone?
I haven’t tried it yet but will definitely try it soon. I wanted to check here if someone else has done something like this and has achieved it.
EDIT 1:
Okay, so I just tried adding this query, and it didn’t work as I expected, simply because the last lambda function will not be called with what values I was expecting it to be called with, which should contain numTimesOpened
that I need to return to the frontend.