awspilot / dynamodb-oop

Speak fluent DynamoDB, write code with fashion, I Promise() 😃
https://awspilot.dev
MIT License
119 stars 28 forks source link

Conditional Insert or Update question. #16

Closed teebu closed 6 years ago

teebu commented 8 years ago

How would I do it? Does this support that?

teebu commented 8 years ago

For example:

I have two date fields:

createdAt updatedAt

How would I go about doing that? I only want to create createdAt once, and update updatedAt every time I updated the record.

adrianpraja commented 8 years ago

Hi,

conditional updates are somehow used internally by this library but undocumented,

insert() = PUT if partition (optionally sort) key do not exist, fail otherwise insert_or_update() = unconditional ADD insert_or_replace() = unconditional PUT

I'm preparing something like:

     DynamoDB
          .table(tableName)
          .where('hash').eq('hashkey')
          .where('range').eq('rangekey')
          .if('number').gt(0)
          .if('active').eq(true)
          .update({
               number: DynamoDB.inc(-1), // decrement until its 0 if 'active'
          }, function( err, data ) {})

although this does not seem to fit you as doing the following will fail with "ConditionalCheckFailedException" when createdAt exists

     .if('createdAt').undefined()

I`ll think of something and get back to you

-adrian

teebu commented 8 years ago

any update on this?

M1chaelTran commented 6 years ago

I'm trying to do the exact same things as @teebu updates on this would be nice after 2 years?

adrianpraja commented 6 years ago

@M1chaelTran , @teebu

From the current docs for updateItem > updateExpression it seems AWS does not allow conditional update at attribute level

You can either:

or

or

Answer is: you can;t do this using one call, I'm closing this issue