Home > Software design >  `@aws-sdk/lib-dynamodb` DynamoDB delete() function returning unhelpful response
`@aws-sdk/lib-dynamodb` DynamoDB delete() function returning unhelpful response

Time:12-23

I am using DynamoDBDocument library as:


import { DynamoDBDocument } from "@aws-sdk/lib-dynamodb";


export class DdbConector {
  ddbDoc: DynamoDBDocument;

  constructor(ddbClient: DynamoDBDocument) {
    this.ddbDoc = ddbClient;
  }


async deleteItem(TableName: string, id: string): Promise<void> {
    const res  = await this.ddbDoc.delete( { TableName, Key: {id} } )
    console.log(res) // shared the response below
  }

}

With this implementation, I am expecting to find out if the delete() request was successful or not. Here is the output of the response:

This is the successful scenario's response. That means the item is deleted from DynamoDB:

{
  '$metadata': {
    httpStatusCode: 200,
    requestId: 'c7a1e277-dc3e-40a1-9122-ad580484b56d',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  Attributes: undefined,
  ConsumedCapacity: undefined,
  ItemCollectionMetrics: undefined
}

And this is an unsuccessful scenario response. In this scenario, the Item is not found in DynamoDB:


{
    '$metadata': {
        httpStatusCode: 200,
        requestId: '3fdd38b8-e7e2-4bfe-9541-7f0aab49d6b6',
        extendedRequestId: undefined,
        cfId: undefined,
        attempts: 1,
        totalRetryDelay: 0
    },
    Attributes: undefined,
    ConsumedCapacity: {
    CapacityUnits: 1,
    GlobalSecondaryIndexes: undefined,
    LocalSecondaryIndexes: undefined,
    ReadCapacityUnits: undefined,
    Table: undefined,
    TableName: 'Customers',
    WriteCapacityUnits: undefined
    },
    ItemCollectionMetrics: undefined
}

I want to implement the delete operation on DynamoDB. But since DynamoDB response does not show success or failure, I have to get the item for the second time to see if the delete() function succeeded or not. Any suggestions for fixing this properly?

CodePudding user response:

You have to define 'unsuccessful' - it sounds like you are saying if you ask DDB to delete an item, and it is not there to be deleted, that that is a failure - but DDB does not consider that an error. Asking DDB to delete an item that doesn't exist, is not an error - as far as DDB is concerned, you asked it to make sure the item is not in the table anymore - it not being there, is a success.

A true error from dynamodb will give you a something other than a 200 HTTP code back.

CodePudding user response:

Just like other DynamoDB write API's: PutItem, UpdateItem etc.... they return an empty response if successful. Likewise, calling GetItem on an item which does not exist will return a 200 response with an empty result {}.

You can be absolutely certain that DynamoDB deleted the item if it existed when you receive a 200 response. If you need a way to determine id the item existed or not when at the time you tried to delete it, you can set ReturnValues in your request, and you will be returned the old item in the Attributes parameter of the payload:

const input = {
    TableName: 'Stack01',
    Key: { id: '123' },
    ReturnValues: 'ALL_OLD'
  }

If item existed:

{
  '$metadata': {
    httpStatusCode: 200,
    requestId: 'DFR8QEIGITITJACG1IUOFKUN0BVV4KQNSO5AEMVJF66Q9ASUAAJG',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  Attributes: { id: '123' },
  ConsumedCapacity: undefined,
  ItemCollectionMetrics: undefined
}

If item did not exist:

{
  '$metadata': {
    httpStatusCode: 200,
    requestId: '1O55POUC8JIVCO2VCVMF1AUAKBVV4KQNSO5AEMVJF66Q9ASUAAJG',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  Attributes: undefined,
  ConsumedCapacity: undefined,
  ItemCollectionMetrics: undefined
}

Therefore, you can implement your own logic depending on the existence of the Attributes value, should you expect the item to exist yet it did not, you may want to add additional logic to ensure you are trying to delete with the correct key.

  • Related