Consider the following data:
[
[
"2021-06-01T14:17:45.032122Z",
"51342145",
Ref(Collection("chatMessages"), "1241454")
],
[
"2021-06-01T14:17:40.338061Z",
"51342145",
Ref(Collection("chatMessages"), "245634545")
]
]
And the following indexes:
CreateIndex({
name: 'chatMessagesBySenderId',
source: Collection('chatMessages'),
terms: [
{ field: ['data', 'senderId'] }
],
values: [
{ field: ['data', 'createdAt'], reverse: true },
{ field: ['data', 'recipientId'] },
{ field: ['ref'] }
]
})
CreateIndex({
name: 'chatMessagesByRecipientId',
source: Collection('chatMessages'),
terms: [
{ field: ['data', 'recipientId'] }
],
values: [
{ field: ['data', 'createdAt'], reverse: true },
{ field: ['data', 'senderId'] },
{ field: ['ref'] }
]
})
I wanted to do:
Paginate(
Distinct(
Union(
Match(Index("chatMessagesByRecipientId"), authUser.id),
Match(Index("chatMessagesBySenderId"), authUser.id)
)
)
);
and I wanted it to return:
{
data: [
[
"2021-06-01T14:17:45.032122Z",
"51342145",
Ref(Collection("chatMessages"), "1241454")
]
]
}
In this case, I wanted to use the data[1]
which is the 51342145
for uniqueness.
I’m unsure if this can be achieved in another way, but the best solution I can think of is to accept a second argument for Distinct
which is a callback that will return data that will be used for uniqueness:
Paginate(
Distinct(
Union(
Match(Index("chatMessagesByRecipientId"), authUser.id),
Match(Index("chatMessagesBySenderId"), authUser.id)
),
Lambda(
['createdAt', 'id', 'ref'],
Var('id')
)
)
);
In the case above, Distinct
will call the callback which will return the Var('id')
which it will use to check for uniqueness.