awslabs / dynamodb-data-mapper-js

A schema-based data mapper for Amazon DynamoDB.
https://awslabs.github.io/dynamodb-data-mapper-js/
Apache License 2.0
816 stars 106 forks source link

batchDelete, passing Model with wrong partition key, it does not delete the Item but returns the given argument #196

Open maufarinelli opened 3 years ago

maufarinelli commented 3 years ago

I was trying to use batchDelete in our app, get the result (which are the deleted Items) and process other things regarding those deleted Items.

In the docs, it says: Returns an async iterable of items that have been deleted (deleted items are yielded when the delete has been accepted by DynamoDB).

Basically, I have a wrapper function in a service to do that. Following the code:

async batchDelete<T>(
    Model: new () => T,
    items: Array<DocumentClient.ItemCollectionKeyAttributeMap>,
  ): Promise<T[]> {
    const  itemsToDelete = items.map((item) => Object.assign(new Model(), item));
    const deleteResult = this.mapper.batchDelete(itemsToDelete);

    const deletedItems: Array<T> = [];
    for await (const deletedItem of deleteResult) {
      deletedItems.push(deletedItem);
    }

    return deletedItems;
  }
}

In our DynamoDB, we have

Primary partition key | projectId (String)
Primary sort key         | entityId (String)

If we pass an existing entityId but a wrong projectId, the batchDelete does not delete the Item, but it does returns actually the given argument. I mean, if we pass to the function above the right Model class and [{ projectId: 'wrong-project-id', entityId: as existing entityId }], batchDelete is returning [{ projectId: 'wrong-project-id', entityId: as existing entityId }] event if the Item has not been deleted

Could someone check if we are doing something wrong, or is there a bug in this function?

kikeiz commented 1 year ago

Did you get to solve the issue? I´m facing the same problem