From 4f13b32f9a33a37a0bfd54d05044ef77e1d136ee Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Thu, 1 Feb 2018 14:15:57 +0100 Subject: [PATCH] fix(collection/redis): rebuidIndexes don't fail on missing items (#651) Instead of failing when an id refers to a no missing item, simply remove it from the index. --- src/collection/redis.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/collection/redis.js b/src/collection/redis.js index 2517ed5ce..d83260dfd 100644 --- a/src/collection/redis.js +++ b/src/collection/redis.js @@ -64,18 +64,24 @@ export default class Redis extends Collection { return Promise.resolve() } + const idsIndex = `${prefix}_ids` return asyncMap(indexes, index => redis.keys(`${prefix}_${index}:*`).then(keys => keys.length !== 0 && redis.del(keys) ) - ).then(() => asyncMap(redis.smembers(`${prefix}_ids`), id => + ).then(() => asyncMap(redis.smembers(idsIndex), id => redis.hgetall(`${prefix}:${id}`).then(values => - asyncMap(indexes, index => { - const value = values[index] - if (value !== undefined) { - return redis.sadd(`${prefix}_${index}:${String(value).toLowerCase()}`, id) - } - }) + values == null + ? redis.srem(idsIndex, id) // entry no longer exists + : asyncMap(indexes, index => { + const value = values[index] + if (value !== undefined) { + return redis.sadd( + `${prefix}_${index}:${String(value).toLowerCase()}`, + id + ) + } + }) ) )) }