shiftcode / dynamo-easy

DynamoDB client for NodeJS and browser with a fluent api to build requests. We take care of the type mapping between JS and DynamoDB, customizable trough typescript decorators.
https://shiftcode.github.io/dynamo-easy/
MIT License
206 stars 27 forks source link

Batch Write with empty array #320

Open fredspivock opened 4 years ago

fredspivock commented 4 years ago

I need to check if my array is empty before doing a batch write

Would be nice if it just ignored empty arrays.

(also handling batch over 25 would be amazing)

Thanks!

itsdarrylnorris commented 4 years ago

Hey @fredspivock ,

Do you have some example code?

fredspivock commented 4 years ago

Here is an example, I have to check if they are empty before adding them to a batchwrite, if the array is empty it will error. I want it to ignore empty arrays would remove my need to do this check.

try {
        let questionStoreInstance = questionStore.batchWrite();
        if (questionsToDelete.length !== 0) {
            questionStoreInstance = questionStoreInstance.delete(questionsToDelete);
        }

        if ((questionsToCreate.length + questionsToUpdate.length) !== 0) {
            questionStoreInstance = questionStoreInstance.put([...questionsToCreate, ...questionsToUpdate])
        }

        await questionStoreInstance.exec();
    } catch (e) {
        logger.error(`Cannot update questions: ${e}`);
        return buildReturnBody(e);
    }