fix(CollectionRedis): do not fail when entry does not exist (#590)

This commit is contained in:
Julien Fontanet 2017-07-31 14:35:31 +02:00 committed by GitHub
parent 2a6c476189
commit 76357fb918

View File

@ -95,24 +95,23 @@ export default class Redis extends Collection {
} }
const { id } = model const { id } = model
const success = await redis.sadd(prefix + '_ids', id) const newEntry = await redis.sadd(prefix + '_ids', id)
// The entry already exists an we are not in replace mode. if (!newEntry) {
if (!success && !replace) { if (!replace) {
throw new ModelAlreadyExists(id) throw new ModelAlreadyExists(id)
} }
// TODO: Remove existing fields. // remove the previous values from indexes
if (indexes.length !== 0) {
// remove the previous values from indexes const previous = await redis.hgetall(`${prefix}:${id}`)
if (replace && indexes.length !== 0) { await asyncMap(indexes, index => {
const previous = await redis.hgetall(`${prefix}:${id}`) const value = previous[index]
await asyncMap(indexes, index => { if (value !== undefined) {
const value = previous[index] return redis.srem(`${prefix}_${index}:${value}`, id)
if (value !== undefined) { }
return redis.srem(`${prefix}_${index}:${value}`, id) })
} }
})
} }
const params = [] const params = []
@ -192,7 +191,7 @@ export default class Redis extends Collection {
if (indexes.length !== 0) { if (indexes.length !== 0) {
promise = Promise.all([ promise, asyncMap(ids, id => promise = Promise.all([ promise, asyncMap(ids, id =>
redis.hgetall(`${prefix}:${id}`).then(values => redis.hgetall(`${prefix}:${id}`).then(values =>
asyncMap(indexes, index => { values != null && asyncMap(indexes, index => {
const value = values[index] const value = values[index]
if (value !== undefined) { if (value !== undefined) {
return redis.srem(`${prefix}_${index}:${value}`, id) return redis.srem(`${prefix}_${index}:${value}`, id)