shiftcode / dynamo-easy

DynamoDB client for NodeJS and browser with a fluent api to build requests. We take care of the type mapping between JS and DynamoDB, customizable trough typescript decorators.
https://shiftcode.github.io/dynamo-easy/
MIT License
204 stars 27 forks source link

Add .ignoreConditionalCheckFailed to WriteRequest #310

Open jthomerson opened 4 years ago

jthomerson commented 4 years ago

Is your feature request related to a problem? Please describe. Often when doing updates, you need to prevent against upserts with something like update.onlyIfAttribute('pk').attributeExists(). It often is not really a failure when the condition fails; it just means "there was no item there to update". You then have to catch the error that DynamoDB throws and ignore the ConditionalCheckFailedException.

Describe the solution you'd like I'd like to be able to do this:

await store.update(file.pk, file.sk)
   .updateAttribute('filename').set(newValue)
   .onlyIfAttribute('pk').attributeExists() // but don't allow upserts
   .ignoreConditionalCheckFailed() // if there's no item there, that's fine
   .exec();

Describe alternatives you've considered I currently have a reusable catch function that I use:

export default function ignoreConditionalCheckFailedException(err: any): void {
   if (err && err.code !== 'ConditionalCheckFailedException') {
      throw err;
   }
}

await update.exec().catch(ignoreConditionalCheckFailedException);