Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check on duplicate keys on .transactWrite() #68

Open
SimonJang opened this issue Oct 3, 2019 · 5 comments
Open

Check on duplicate keys on .transactWrite() #68

SimonJang opened this issue Oct 3, 2019 · 5 comments

Comments

@SimonJang
Copy link
Contributor

The transaction fails when trying to write with the same key multiple times in the same transaction. We might need to warn the user or add it as additional information when the transactions errors?

@SamVerschueren
Copy link
Owner

This is a good idea!

I would just try to invoke the transaction. When it fails, it would be nice to provide more details on what could go wrong. AWS itself returns a ConditionalCheckFailedException but you don't know exactly what went wrong.

So if it fails, we should try to find out why it fails. Are you trying to update the same record twice for example.

@LukvonStrom
Copy link

Hi all,
is this stil something tbd in Hacktoberfest 2020?

@SimonJang
Copy link
Contributor Author

@LukvonStrom this issue still needs to be resolved. @SamVerschueren correct me if I am wrong.

@SamVerschueren
Copy link
Owner

Exactly! Feel free to pick it up @LukvonStrom for hacktoberfest 2020. It was created last year for hacktoberfest but wasn't picked up.

@LukvonStrom
Copy link

LukvonStrom commented Sep 30, 2020

@SamVerschueren @SimonJang I'm proposing the following to get the duplicate keys per possible action:

let actionMap = {}
for (let writeItem of this.actions) {
    let keys = Object.values(item.params.ExpressionAttributeNames);
    let type = "";

    if (action instanceof UpdateItem) {
        type = "update";
    }

    if (action instanceof InsertItem) {
        type = "insert";
    }

    if (action instanceof DeleteItem) {
        type = "delete";
    }

    if (actionMap.hasOwnProperty(type)) {
        actionMap[type].keys = actionMap[type].keys.concat(keys);
    } else {
        actionMap.push({
            type,
            keys
        })
    }
}

for (let writeAction of Object.keys(actionMap)) {

    // taken under CC BY-SA 3.0 from https://stackoverflow.com/a/35922651 
    let duplicates = writeAction.keys.reduce(function(acc, el, i, arr) {
        if (arr.indexOf(el) !== i && acc.indexOf(el) < 0) acc.push(el);
        return acc;
    }, []);

    console.error("Duplicate key(s)", duplicates, "for", writeAction.type, "action")

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants