reza
reza

Reputation: 6358

Dynamodb put item overwrites the old one

I have table defined as follows:

Type: "AWS::DynamoDB::Table"
  Properties:
    AttributeDefinitions:
      - AttributeName: "deviceId"
        AttributeType: "S"
    KeySchema:
      - AttributeName: "deviceId"
        KeyType: "HASH"

I call the following code to add a new entry

    this.client = new AWS.DynamoDB.DocumentClient();

    public saveItem(entry): Promise<any> {

    let dbEntry = Database.decorateWithStandardFields(JSON.parse(entry));

    const params = {
        TableName: eventLogTable,
        Item: dbEntry
    };

    console.log('save this to db', params);
    return this.client.put(params).promise();
}

the 2 different entries are

     { TableName: 'sls-basic-operations-items-dev',
        Item: 
        { 
          status: 'changed',
          deviceId: 'device12345',
          wkStation: 'xyz',
          Timestamp: 1561050389,
          TTL: 1561136789 
        } 
     }

the second one is only different for Timestamp and TTL values.

    { TableName: 'sls-basic-operations-items-dev',
      Item: 
      { 
        status: 'changed',
        deviceId: 'device12345',
        wkStation: 'xyz',
        Timestamp: 1561050417,
        TTL: 1561136817 
       } 
     }

with this code, I always end up with one item and it is the last one.

What is wrong with this code?

Upvotes: 0

Views: 1286

Answers (1)

Charles
Charles

Reputation: 23783

Your table only has a hash key, so there can only ever be one record with a given deviceID.

Seems like you might want to define timestamp as a sort key.

Upvotes: 1

Related Questions