We’re using eventing functions to update user notification counts in a destination bucket based on changes in a source bucket. Our query:
SELECT * FROM user_notification_counts WHERE userId = userId;
which uses the index:
CREATE INDEX `user_notification_by_userId` ON `user_notification_counts`(`userId`)
Despite setting N1QL Consistency to request and introducing delays before querying, we still face discrepancies between the source and destination counts. Any insights on improving consistency?
Sorry for the confusion this is the function we use
function OnUpdate(doc, meta) {
let userId = parseInt(meta.id.split("-")[0]);
var count = SELECT count(1) as count FROM user_notification_counts where userId = $userId;
for (var c of count) {
if (c.count && c.count> 0 ) {
destination_bucket[userId] = c
log("Doc created/updated", userId);
}
else{
delete destination_bucket[userId]
log("Doc deleted", userId);
}
}
count.close()
}
function OnDelete(meta, options) {
let userId = parseInt(meta.id.split("-")[0]);
var count = SELECT count(1) as count FROM user_notification_counts where userId = $userId;
for (var c of count) {
if (c.count && c.count > 0 ) {
destination_bucket[userId] = c
log("Doc created/updated", userId);
}
else{
delete destination_bucket[userId]
log("Doc deleted", userId);
}
}
count.close()
}
We think the query returns the stale counts before the change in the source bucket is applied. We saw no changes after setting the N1QL Consistency to Request in function settings
The destination bucket is the projection of the source bucket. It is updated with the latest count from the query every time the source bucket is updated.