31

I want to add id + some values to a DynamoDb just once. If the id exists already it should do nothing or update

I can go with

search 

if not found > insert

if found > do nothing or update (for now do nothing is fine)

But hopfully there is a better way to do it. The id should be the key to check for.

That's the code in node:

const dynamodbParams = {
        TableName: process.env.DYNAMODB_TABLE_BLICKANALYTICS,
        Item: {
          id: userId,
          createdAt: timestamp
        },
      };

      dynamoDb.put(dynamodbParams).promise()
      .then(data => {
        console.log('saved: ', dynamodbParams);
      })
      .catch(err => {
        console.error(err);
      });  

I use this in yml. Don't know if there are options to set this up in yml

resources:
  Resources:
    DynamoDbTableExpenses:
      Type: 'AWS::DynamoDB::Table'
      DeletionPolicy: Retain
      Properties:
        AttributeDefinitions:
          -
            AttributeName: id
            AttributeType: S
          -  
            AttributeName: createdAt
            AttributeType: N
        KeySchema:
          -
            AttributeName: id
            KeyType: HASH
          -
            AttributeName: createdAt
            KeyType: RANGE            
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        TableName: ${self:provider.environment.DYNAMODB_TABLE_BLICKANALYTICS}

Tobi
  • 1,702
  • 2
  • 23
  • 42

1 Answers1

69

You can do the whole thing with a single UpdateItem operation:

const dynamodbParams = {
    TableName: process.env.DYNAMODB_TABLE_BLICKANALYTICS,
    Key: {id: userId},
    UpdateExpression: 'SET createdAt = if_not_exists(createdAt, :ca)',
    ExpressionAttributeValues: {
        ':ca': {'S': timestamp}
    }
};
dynamoDb.updateItem(params, function(err, data) {
    if (err) {
        console.log(err, err.stack);
    } else {
        console.log(data);
    }
}

If you only want to do insert if not exists, you can easily do that with PutItem:

const dynamodbParams = {
    TableName: process.env.DYNAMODB_TABLE_BLICKANALYTICS,
    Item: {
        id: userId,
        createdAt: timestamp
    },
    ConditionExpression: 'attribute_not_exists(id)'
};
dynamodb.putItem(params, function(err, data) {
    if (err) {
        console.log(err, err.stack);
    } else {
        console.log(data);
    }
}

You can come up with more complex ways how to set or update attributes in an item by combining the condition expressions and update expressions.

Note I have not fully tested the code, so please comment if there's any error, but it should work.

Milan Cermak
  • 7,476
  • 3
  • 44
  • 59
  • 2
    Thanks the update seems to be the thing im looking for. I had to use dynamoDb.put(dynamodbParams).promise() because "TypeError: dynamoDb.putItem is not a function" in my case. With put it works. I now get a "ConditionalCheckFailedException: The conditional request failed" if there already is an entry in the database. – Tobi Mar 12 '19 at 07:56
  • Can mention that this is a standard database method called "upsert". It's in older databases like Postgres. – dwu39 Nov 26 '19 at 05:27
  • 2
    I'd suggest to edit this and let people know that if ConditionExpression resolves to `false`, the query will return a 400 err – Alberto S. May 12 '22 at 15:15