Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
db.findOne({ aggregateId, aggregateVersion }, (err, doc) => {
if (doc !== null) {
return reject(
new ConcurrentError(
// eslint-disable-next-line max-len
`Can not save the event because aggregate '${aggregateId}' is not actual at the moment. Please retry later.`
)
)
}
db.insert(event, error => (error ? reject(error) : resolve()))
})
})
error => {
if (!error) {
resolve()
} else if (error.errorType === 'uniqueViolated') {
reject(
new ConcurrentError(
// eslint-disable-next-line max-len
`Can not save the event because aggregate '${
event.aggregateId
}' is not actual at the moment. Please retry later.`
)
)
} else {
reject(error)
}
}
)
${serializedEvent},
${byteLength}
)`
)
break
} catch (error) {
const errorMessage =
error != null && error.message != null ? error.message : ''
if (errorMessage.indexOf('subquery used as an expression') > -1) {
throw new Error('Event store is frozen')
} else if (errorMessage.indexOf('duplicate key') < 0) {
throw error
} else if (errorMessage.indexOf('aggregateIdAndVersion') > -1) {
throw new ConcurrentError(event.aggregateId)
}
await new Promise(resolve => setTimeout(resolve, fullJitter(retry)))
}
}
}